Read values from multiple files in IDL
Matthew Harrington
I have several files from which I'm trying to extract variables for brightness temperature, BT. I want to put all the variables into one array. This is what I have and so far. I've opened all the files but I can't figure out how to combine all the values.
filelist = FINDFILE(in_path+"ATMS-v11r1_npp_s"+date_str+"*nc",count=nfiles)
FOR i = 0, nfiles -1 DO BEGIN
PE1_fid=NCDF_OPEN(filelist(i))
field = 'BT'
NCDF_VARGET, pe1_fid, field, pe1_data
ENDFOR 2 Answers
You didn't say what size/type of variable BT is. I assumed it was a float scalar, but if not you would modify the definition of bt below to match.
filelist = file_search(in_path + 'ATMS-v11r1_npp_s' + date_str + '*nc', $ count=nfiles)
bt = fltarr(nfiles)
for i = 0, nfiles - 1 do begin pe1_fid = ncdf_open(filelist[i]) field = 'BT' ncdf_varget, pe1_fid, field, e1_data bt[i] = e1_data
endearI also changed a few other things about your code:
FINDFILEis obsolete, useFILE_SEARCH- Don't index arrays using parentheses, use brackets (and
compile_opt strictarr). - Use single quotes instead of double quotes for string literals.
With IDL version 8 or later you can simplify the code a bit, independent how many entries each of the e1_data fields contains:
filelist = FILE_SEARCH(in_path + 'ATMS-v11r1_npp_s' + date_str + '*nc', count=nfiles)
bt = []
FOR i = 0,nfiles-1 DO BEGIN pe1_fid = = ncdf_open(filelist[i]) field = 'BT' ncdf_varget, pe1_fid, field, e1_data bt = [bt, e1_data]
ENDFOR 7