如何设置关键字以完全写入CSV文件

时间:2017-09-04 12:10:43

标签: idl-programming-language

到目前为止,此脚本正在运行,输出正确。但是,它不会为我填充CSV文件。但只填充循环的最后一次迭代。作为IDL的新手,我需要掌握关键字的这个概念。

我相信我需要一个关键字,但我尝试插入这个关键字都失败了。 有些人可以修改脚本,以便csv文件完全填充。

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 个答案:

答案 0 :(得分:1)

每次调用文件时,

WRITE_CSV都会覆盖该文件,因此您只能看到最后一个条目。

创建数组以保存for循环之前的所有值:

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)

for循环中填写:

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

然后在for循环之后,用:

写下它们
WRITE_CSV, 'LatLon.csv', the_file_list, latitude_arr, longitude_arr, height_arr