getcdfvar.pro 2.11 KB
function getCdfVar, files,  paramName, dims, StartDouble, IntDouble, Time, Data

 Size = N_elements(Time);
 TIMESHIFT = 62167219200.D0 
 startIndex = 0L;
 
 datapath = getenv("USER_DATA_PATH") eq '' ?  '../DATA/' : getenv("USER_DATA_PATH");

 for i = 0, n_elements(files) - 1 do begin
   
     fileName = datapath + files[i];
     id = CDF_OPEN(fileName);
 
     CDF_CONTROL, id, SET_ZMODE=2

    ; CDF_CONTROL, id, GET_VAR_INFO=info, VARIABLE=0, /ZVARIABLE 
     dataType = "UNKNOWN"
     timeId = 0L;
     while (dataType ne "CDF_EPOCH" AND dataType ne "CDF_EPOCH16") do begin
           rest = cdf_varinq(id, timeId, /ZVARIABLE) 
           dataType = rest.datatype;
           timeId += 1L;
     endwhile
 
     timeId -= 1L;
     recs = getcdfnbrecs_(FileName, timeId);
 
   ; CDF_CONTROL, id, GET_VAR_INFO=info, VARIABLE=0, /ZVARIABLE 
    ;EXTENDRECS MAXALLOCREC MAXREC MAXRECS NINDEXENTRIES NINDEXRECORDS
    
    if (dataType eq "CDF_EPOCH16") then begin 
     CDF_VARGET, id,  timeId, EPOCH16, REC_COUNT = recs+1, /ZVARIABLE
     EPOCH = dblarr(recs);
     for j = 0, recs -1 do begin
        CDF_EPOCH16, EPOCH16[j], yr, mo, dy, hr, min, sec, milli, micro, pico, /BREAKDOWN_EPOCH 
        CDF_EPOCH, EPOCH8, yr, mo, dy, hr, min, sec, milli, /COMPUTE_EPOCH 
        EPOCH[j] = EPOCH8;
     endfor
    endif else CDF_VARGET, id,  timeId, EPOCH, REC_COUNT = recs+1, /ZVARIABLE
       
    Time1 =  reform(EPOCH/1000.D0 - TIMESHIFT) 
 
      num = where(Time1 GE StartDouble AND Time1 LT StartDouble+IntDouble, Index);
   
      if ((Index eq 0 AND startIndex eq 0) OR (startIndex + Index gt Size))  then begin       
             return, 0
      endif
 
      if (Index gt 0) then begin
            Time[startIndex:startIndex+Index-1] = Time1[num];
        
            varId = CDF_VARNUM(id, paramName);
            CDF_VARGET, id,  varId, data1, REC_COUNT = recs+1, /ZVARIABLE
        
            if (dims eq 1) then  Data[startIndex:startIndex+Index-1] = reform(Data1[num])  $
            else Data[*, startIndex:startIndex+Index-1] = Data1[*, num]
      endif
      CDF_CLOSE, id
      startIndex += Index;     
  endfor  
  
 return,startIndex;
end