PDA

View Full Version : Column Names in DC_WRITE_FREE



Unregistered
10-04-2006, 06:49 PM
Hi all,
Is there a way to include column names/labels when writing a data file using DC_WRITE_FREE? Any advice would be appreciated.

donb
10-05-2006, 10:28 AM
Yes, DC_WRITE_FREE can write the column headers / variables names to the output file as long as the first variable in the parameter list contains an array of strings which are those headers/names. The rest of the variables on parameter list would be the data. Depending on how you want the output file to 'look', a Row organization for the output would work best.

Another option is to write the headers/names to a file using the OPENW, PRINTF commands; then use DC_WRITE_FREE to write the data using Column organization; and then concatenate the two files using either PV-WAVE or an operating system command.

I hope this helps,

Don B.

brian
10-16-2006, 02:25 PM
Hello,

This may assist. Items to be aware of:

1) Row oriented output is incomplete. Should be easy compared to column oriented
2) vars is a string array containing variable names to be printed
3) The default is column oriented with column headers
4) variable length output is allowed, even scalar (note consecutive delimiters in the output)
5) DT output available in 1 format, easy change with 2 additional args in function
6) output array may be large because it's square to accommodate variable length data and retain "shape" (accommodates DC_READ_FREE).
7) The ..LOCALS command will need be modified according to a maximum expected size of the output data.
8) needs error checking added for "positional" parameters



FUNCTION write_file, file, vars, delim, Row=row, NoHeading=noheading

;number of vars for both column and row orientation
nvars=N_ELEMENTS(vars)
;space for data in execute
..LOCALS 10
varsio=vars

;column oriented
IF NOT KEYWORD_SET(Row) THEN BEGIN
;find max number of elements and create local vars
nelem=-1
FOR i=0,nvars-1 DO BEGIN
s=EXECUTE('UPVAR, vars(i), ' + varsio(i) + ', Level=-1')
s=EXECUTE('result=N_ELEMENTS('+ varsio(i) +')')
IF nelem LT result THEN nelem = result
ENDFOR

;create square array for output
IF NOT KEYWORD_SET(NoHeading) THEN BEGIN
vararray=STRARR(nelem+1, nvars)
vararray(0,*)=varsio
first=1
ENDIF ELSE BEGIN
vararray=STRARR(nelem, nvars)
first=0
ENDELSE

FOR i=0,nvars-1 DO BEGIN
s=EXECUTE('tmp = N_TAGS(' + varsio(i) + ')')
IF tmp EQ 0 THEN BEGIN
s=EXECUTE('vararray(first:N_ELEMENTS('+ varsio(i) +') - 1 + first MOD 2, '+ STRTRIM(STRING(i), 2) +') = ' + varsio(i))
ENDIF ELSE BEGIN
;dt variable
s=EXECUTE('DT_TO_STR, ' + varsio(i) + ', d, t, Date_Fmt=1, Time_Fmt=-1')
d=d+ ' ' +t
s=EXECUTE('vararray(first:N_ELEMENTS('+ varsio(i) +') - 1 + first MOD 2, '+ STRTRIM(STRING(i), 2) +') = d'); + varsio(i))
ENDELSE
ENDFOR

;file io
OPENW, unit, file, /Get_Lun
FOR i=0,nelem - 1 + first MOD 2 DO WRITEU, unit, STRJOIN(vararray(i,*), delim)+'\012'
ENDIF ELSE BEGIN
;Row oriented file
;file io
OPENW, unit, file, /Get_Lun
ENDELSE
FREE_LUN, unit
RETURN,0
END




PRO drive_write_file
DECLARE FUNC, WRITE_FILE

;space for data in execute
..LOCALS 10

;variable names
vars=['col1','col2','col3','col4','col5','col6']

;assign data to vars w/ same name as array elements, file and delimiter
FOR i=1,N_ELEMENTS(vars)-1 DO s=EXECUTE(vars(i) + '=DINDGEN(10) * ' + STRING(i+1))
col1 = DTGEN(TODAY(), 10)
col3=DINDGEN(5)
col6=2.5
delim=','
file='c:\vni\example.csv'

s=WRITE_FILE(file,vars,delim)
END




col1,col2,col3,col4,col5,col6
10/16/2006 15:13:41.000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 2.50000
10/17/2006 15:13:41.000, 2.0000000, 1.0000000, 4.0000000, 5.0000000,
10/18/2006 15:13:41.000, 4.0000000, 2.0000000, 8.0000000, 10.000000,
10/19/2006 15:13:41.000, 6.0000000, 3.0000000, 12.000000, 15.000000,
10/20/2006 15:13:41.000, 8.0000000, 4.0000000, 16.000000, 20.000000,
10/21/2006 15:13:41.000, 10.000000,, 20.000000, 25.000000,
10/22/2006 15:13:41.000, 12.000000,, 24.000000, 30.000000,
10/23/2006 15:13:41.000, 14.000000,, 28.000000, 35.000000,
10/24/2006 15:13:41.000, 16.000000,, 32.000000, 40.000000,
10/25/2006 15:13:41.000, 18.000000,, 36.000000, 45.000000,


Regards,

brian

green12
05-25-2009, 09:29 AM
DO you know the pv-wave command for combining the two data files?

green12
05-29-2009, 10:01 AM
i see how to do it for strings but what about files?

donb
06-02-2009, 02:05 PM
The code below is one way to concatenate two files using PV-WAVE commands:


PRO APPEND_FILES, file1, file2
;
; add error checking to make sure the params are strings,
; and that the files exist, etc. Current code assumes two
; valid filenames (strings) are passed to this routine.
;
; open the 2 files
OPENR, lun1, file1, /Get_lun & OPENR, lun2, file2, /Get_lun
; return information about each of the files
f1info = FSTAT(lun1) & f2info = FSTAT(lun2)
; get the number of bytes in each of the files
f1size = f1info.size & f2size = f2info.size
; create arrays to hold the content for each file
arr1 = BYTARR(f1size) & arr2 = BYTARR(f2size)
; read the files
READU, lun1, arr1 & READU, lun2, arr2
; close the files and release the LUNs
FREE_LUN, lun1 & FREE_LUN, lun2
; open the new output file
OPENW, outlun, 'newfile.dat', /Get_lun
; write the content of the first file into the new file
PRINTF, outlun, STRING(arr1)
; write the content of the second file into the new file
PRINTF, outlun, STRING(arr2)
; close the new file and release the LUN
FREE_LUN, outlun
END

Depending on your machine, it's much easier to do this at the OS level:


Unix Shell: % cat file1 file2 >> out.put
Windows DOS window: c:\ type file1 file2 > out.put

If you want to stay in PV-WAVE and you're at the $MAIN$ level, you can do this by accessing the OS with the "$" command:


Unix machine: WAVE> $ cat file1 file2 >> out.put
Windows machine: WAVE> $ type file1 file2 > out.put

If you're within a .pro file and need to access the OS, use the PV-WAVE SPAWN command.

Hope this helps,

Don B.