Add fourth column into an original file

50 views
Skip to first unread message

Khaled Tahoon

unread,
Jan 31, 2025, 6:39:57 PMJan 31
to idl-pvwave
I have an original ASCII data file with 3 columns and 1000 raw.  I wrote IDL code to calculate a new parameter with one column and 1000 raw. I would like to modify my own code to merge directly the new parameter  into original file to becomes 4 columns and 1000 raw.
thanks in advance
Khaled

Nikola Vitas

unread,
Feb 3, 2025, 1:51:32 PMFeb 3
to idl-pvwave
The solution may depend on the exact content of the ascii file, but something like this should give you the idea.


filename_old = 'old.dat'
filename_old = 'new.dat'

ncol = 3
nrow = 1000
old = DBLARR(ncol, nrow)
new = DBLARR(ncol+1, nrow)

OPENR, 1, filename_old
READF, 1, old
CLOSE, 1

new[0:ncol-1, *] = old
new[ncol, *] = ... ; new values

OPENW, 1, filename_new
PRINTF, 1, new
CLOSE, 1


Message has been deleted

Khaled Tahoon

unread,
Feb 3, 2025, 4:15:33 PMFeb 3
to idl-pvwave
thank you for help
Just I have 132 files, each one contains  9 columns and 1000 raw. what I need, I select I last column from all files and print them in one file name out.txt
the outcome file becomes a matrix of 132 columns and 1000 raw. for this idea I wrote the following code

input_dir_model = 'C:\Users\Desktop\Input_data'                                                         ; This directory contains 132 files
fileList_model = FILE_SEARCH(input_dir_model,'*', /FULLY_QUALIFY_PATH )   ; select all files in the input directory

output_dir_pw = 'C:\Users\Desktop\out_parameter'

FOR i = 0, n_elements(file_list_model) -1  DO BEGIN

date=file_lines( file_list_model (i))  ;     the number of lines in each file
t_data=fltarr(9,date)                      ;     the number of columns in each files
openr, lun,  fileList_model (i),/get_lun  
readf, lun, t_data
free_lun, lun
X1  =reform(t_data[0,*])    ; the first column
X2   =reform(t_data[1,*])   ; the second column
X3   =reform(t_data[2,*])
X4    =reform(t_data[3,*])
X5    =reform(t_data[4,*])
X6    =reform(t_data[5,*])
X7   =reform(t_data[6,*])
X8    =reform(t_data[7,*])
X9    =reform(t_data[8,*])    ; the last column



OPENW, lun, ' output_dir_pw \temp.txt\out.txt', /get_lun; , /append
PRINTF, lun, [X9[0:i,*]], format='(3x, f8.4)'     
free_lun,lun
end
it's suppose that the code print X9's  columns in out.txt.
Unfortunately, The code don't  run correctly
Best regards
Reply all
Reply to author
Forward
0 new messages