2

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
Tim
  • 1,659
  • 1
  • 21
  • 33
klex52s
  • 437
  • 1
  • 7
  • 19

2 Answers2

3

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
endear

I also changed a few other things about your code:

  • FINDFILE is obsolete, use FILE_SEARCH
  • Don't index arrays using parentheses, use brackets (and compile_opt strictarr).
  • Use single quotes instead of double quotes for string literals.
mgalloy
  • 2,356
  • 1
  • 12
  • 10
0

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
planetmaker
  • 5,884
  • 3
  • 28
  • 37
  • I am getting an error saying that the arrays are unable to be concatenated because the dimensions do not agree...Is this because bt is dimensionless? – klex52s Mar 01 '16 at 20:00
  • My code should work as long as `e1_data` is a scalar or a real 1D-array (and not a pseudo 1D with dimensions [1,n] or similar). In those cases you need to `reform` the data before. Should it be a different format you need to treat it differently. – planetmaker Mar 01 '16 at 20:28
  • My array is two dimensional. Reform can be used to convert it to a one dimensional array? – klex52s Mar 02 '16 at 01:33
  • yes... under certain conditions. check IDL's help on `reform` – planetmaker Mar 02 '16 at 11:19
  • This appears to have worked. What I did was: reform(e1_data, n_elements(e1_data), 1). – klex52s Mar 02 '16 at 14:47
  • I would be careful with reassigning to `bt` inside the loop like this. Every time through the loop, a new `bt` will be allocated with one extra element, the old `bt` values will be copied into the new `bt`, and then the extra element will be copied in. This gets very inefficient for large arrays. Using a list or preallocating `bt` of the correct size is much more efficient. – mgalloy Mar 03 '16 at 18:21
  • that's true, if you know the size beforehand. However if the number of elements in e1_data is different for different files, then the size of bt cannot be obtained in advance. – planetmaker Mar 03 '16 at 19:21