Results 1 to 2 of 2

Thread: Help with opening a file that's space delimited

  1. #1

    Help with opening a file that's space delimited

    Greetings all,

    I am very new at PV-Wave, and I was going to see if anyone out there can help my opening a file that's space delimited and has columns. I originally created it in Excel but was told to make it space delimited and save as a .dat file. The file is attached as a .txt since .dat isn't allowed.

    Any suggestions would me appreciated - I've tried DC_READ_FREE so far without any luck, with the following (again I am very new and still learning so please go easy with me):

    blank = ' '
    zero=0.0d0
    izero=0
    Date = REPLICATE({!DT},3)
    Distance = izero
    WestReading = zero
    WestDifference = zero
    EastReading = zero
    EastDifference = zero
    Uncertainty = zero
    Leg = izero
    snow = blank
    freeboard = izero
    icedepth = izero
    LocalTime = zero

    status = DC_READ_FREE('FileforForums.dat', Date, $
    Distance, WestReading, WestDifference, EastReading, EastDifference, $
    Uncertainty, Leg, snow, freboard, icedepth, LocalTime, /Column, Delim = [' '],$
    Dt_Template=[1,-1])

    FOR I = 0,12 DO BEGIN
    PRINT, Date(I), Distance(I)

    ENDFOR

    Thanks!
    VV5
    Attached Files Attached Files

  2. #2
    Grand Poobah donb's Avatar
    Join Date
    Aug 2005
    Location
    Colorado
    Posts
    147
    Hello VV5, and welcome to PV-WAVE!!

    The DC (Data Connection) routines are designed to read such files, and provide a LOT of flexibility.

    The first thing I noticed is you have 12 variables in the file and only 11 columns of data; in somewhat a random fashion I removed 'icedepth' from the list of variable names in your DC_READ_FREE example. Also, Ive changed the Dt_Template date option from 1 to 4 format 4 is for dd-mmm-yy which is the date format used in your data file; the time format (-1) is correct.

    The next thing is that you predefined both the data type and the 'shape' of all the variables prior to the DC_READ command. One of the key points of DC_READ is that it reads data from the file until the variable is full (default behavior). In this case, because you predefined everything as a scalar and not an array, at best a single line would have been read from the file. The other default behavior is to read everything into a float data type - so you really only need predefine string and date/time variables. You can always cast the floats returned from DC_READ if needed (integer values for example).

    The third item of note is you can't mix data types within a column (with the exception of missing_data/missing_value pairs). Because the first line in the file is the list of variables names (all 'string' types), and the rest of the file is the real data (date, floats, integers, time), you'll want to skip the first line of the file (Nskip=1) before DC_READ starts the rest of it's processing.

    Lastly, one of the best features of DC_READ is that you don't need to know how many lines are in the file - using the RESIZE keyword tells PV-WAVE to continue to grow the 'shape' of the variables in the RESIZE list until you reach the end of the file!

    Try these commands, and happy PV-WAVE programming!

    Code:
    !Century_Divider=0
    date={!DT}
    status = DC_READ_FREE('fileforforums.txt', $
       Date, Distance, WestReading, WestDifference, EastReading, $
       EastDifference, Uncertainty, Leg, snow, freboard, Date, $
       /Column, Nskip=1, Dt_Template=[4,-1], Resize=[INDGEN(11)+1])
    DT_PRINT, date
    PRINT, Uncertainty
    Don Boughey
    PV-WAVE Advocate

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •