在idl中读写多个数据文件

reading and writing multiple data files in idl

我是 IDL 的新手,正在尝试从某个文件夹中读取多个二进制数据文件,然后将它们以文本格式一次全部写入到某个文件夹中。我目前有一个代码,一次只能读写一个文件,我必须手动进行命名,但是我需要一次读写所有文件,以避免手动读取时出现命名错误。这是我的代码 assist.I 需要一次读取和写入多个数据文件。

    pro readfitacf

; Open the raw file for read only 


  inp=FitOpen('/media/New Volume/SANAE/2010_fitacf/2010 Jan/20100114.0931.17.san.fitacf',/read)


;  Search for a specific time in the file

;prm.bmnum = 15

;prm.nrang = 5

filename = '/project/2010 Jan/20100114.0931.17.san.txt'

OPENW,5,filename

其中Fitopen是一个函数如下


      function FitOpen,fname,atme=atme,lib=lib, $
               native=native,external=external, $
                read=read,write=write,update=update
mode=0

if (KEYWORD_SET(lib) eq 0) then lib=getenv('LIB_FITIDL')

if KEYWORD_SET(native) then mode=1

if KEYWORD_SET(external) then mode=2

if (mode eq 0) and (file_test(lib) eq 1) then mode=2 $

else if (mode eq 0) then mode=1


if (mode eq 1) then begin

if KEYWORD_SET(read) then openr,unit,fname,/GET_LUN,/SWAP_IF_BIG_ENDIAN

if KEYWORD_SET(write) then openw,unit,fname,/GET_LUN, /SWAP_IF_BIG_ENDIAN

if KEYWORD_SET(update) then openu,unit,fname,/GET_LUN, /SWAP_IF_BIG_ENDIAN

endif else begin

if KEYWORD_SET(read) then openr,unit,fname,/GET_LUN,/STDIO

if KEYWORD_SET(write) then openw,unit,fname,/GET_LUN,/STDIO

if KEYWORD_SET(update) then openu,unit,fname,/GET_LUN,/STDIO

endelse


return, unit

end

您可以使用 FILE_SEARCH to search for specific files with a pattern and get an array of file paths. Then, just loop over this array. To get the base name of the file use for example FILE_BASENAME:

files = FILE_SEARCH('directory1/*.fitacf')

FOR i = 0, N_ELEMENTS(files) - 1 DO BEGIN
    inp=FitOpen(files[i],/read)
    filename = 'directory2/'  + FILE_BASENAME(files[i], '.fitacf') + '.txt'
    OPENW, unit, filename, /GET_LUN
    ; do stuff
    FREE_LUN, unit, inp
ENDFOR