How to set a keyword to write fully to the CSV file

358 views Asked by At

This script is working in so far that the output is correct. However it is not populating the CSV file for me. But only populating the last iteration of the loop. Being new to IDL, I need to grasp this concept of the keyword.

I believe I need a keyword, but my attempts of inserting this have all failed. Can some amend the script so that the csv file populates fully please.

PRO Lat_Lon_Alt_Array

; This program is the extract the Latitute, Longigitude & Altitute
; with the Site name and file code. 
; The purpose is to output the above dimensions from the station files
; into a csv file.

COMPILE_OPt IDL2

the_file_list = file_search('D:/Rwork/Project/25_Files/','*.nc')

FOR filein =  0, N_ElEMENTS (the_file_list)-1 DO BEGIN

station = NCDF_OPEN(the_file_list[filein])
 NCDF_VARGET, station, 'station_name', St_Name           
 NCDF_VARGET, station, 'lat', latitude
 NCDF_VARGET, station, 'lon', longitude
 NCDF_VARGET, station, 'alt', height

 latitude=REFORM(latitude,1)
 longitude=REFORM(longitude,1)
 height=REFORM(height,1)

 Print,the_file_list[filein]
 Print, 'name'
 Print, St_Name
 Print,'lat'
 Print,latitude
 Print,'lon'
 print,longitude
 Print,'alt'
 Print,height

 ; Add each station data to the file

 WRITE_CSV, 'LatLon.csv', the_file_list[filein],latitude,longitude,height 

 ENDFOR
 RETURN
 END
1

There are 1 answers

0
shouston On BEST ANSWER

WRITE_CSV overwrites the file every time it is called, hence you only ever see the last entry.

Create arrays to hold all the values before the for loop:

n_files = N_ElEMENTS(the_file_list)
latitude_arr = DBLARR(n_files) ; Assuming type is double
longitude_arr = DBLARR(n_files)
height_arr = DBLARR(n_files)

In your for loop fill them with:

latitude_arr[filein] = latitude
longitude_arr[filein] = longitude
height_arr[filein] = height

Then after the for loop, write them with:

WRITE_CSV, 'LatLon.csv', the_file_list, latitude_arr, longitude_arr, height_arr