param_info.pro 10.1 KB
; $Id: param_info.pro,v 1.4 2009/03/30 15:45:33 budnik Exp $
;
;+
; NAME:
;       PARAM_INFO
;
; PURPOSE:
;       gets CDAWEB parameter Info 
;         to create AMDA parameter automatically
;
;
; CALLING SEQUENCE:
;       
;       PARAM_INFO, CDF_name, ParID
;
; INPUTS:
;       CDF_name:  CDF file name (master CDF or data CDF) 
;       ParID  :   variable name in CDF notation
;       
; KEYWORD PARAMETERS:
;    
;      
; OUTPUTS:
;          string "paramID%....."
;       
; MODIFICATION HISTORY:
;       Written by:     Elena Budnik
;       September, 2007       
;-

 pro Param_Info, CDF_name, ParID

 NODATA = "%-9999"

;------------------- List of Param Attributes used for AMDA ----------------

 AttributesGlobal = ["Source_name", "Descriptor", "Data_type", "Instrument_type"]
 AttributesCommon = ["FIELDNAM", "DISPLAY_TYPE", "UNITS", "FILLVAL", "Frame", "VALIDMIN","VALIDMAX", "SI_conversion"]

; ALSO EXIST "DICT_KEY", "CATDESC", "AVG_TYPE"
 AttributeScalar = ["LABLAXIS"] 
 AttributeVector = ["LABL_PTR_1"]
      
   if (file_test(CDF_name) eq 0) then begin
           print, NODATA
           return
   endif
 
   Output = "paramID%" + strjoin(strsplit(ParID,"%&.><",/EXTRACT),"_");
   cdfID = cdf_open(CDF_name)

;---------------- Get Global Attributes ----------------------
     On_IoError, NoGlobalAttr 
     for i = 0, n_elements(AttributesGlobal) - 1 do begin  
      if (cdf_attexists(cdfid, AttributesGlobal[i])) then cdf_attget, cdfid, AttributesGlobal[i], 0, attvalue $
          else cdf_attget, cdfid, strupcase(AttributesGlobal[i]), 0, attvalue
           Output +=  "&&"+strupcase(AttributesGlobal[i])+"%" + strcompress(string(attvalue))
           continue;
           NoGlobalAttr: Output += "&&"+strupcase(AttributesGlobal[i]) + NODATA
     endfor
   
     On_IoError, NULL
;--------------- Get parameter Number -----------------------
    
     par_num = cdf_varnum(cdfid, ParID, IsZVar);

;----------- Attributes & Dims for R-vars -----------------------------   
     if (IsZVar eq 0) then begin   ; r-variable
           glob = cdf_inquire(cdfid)
           info = cdf_varinq(cdfid, par_num)
           outDim = ""

           if (glob.ndims eq 0) then dim = 1 else  begin
                numDim = where(info.dimvar eq 1, ndim)
                if (ndim GT 0) then dim = glob.dim[numDim] else dim = 1
           endelse
           if (n_elements(dim) eq 1) then outDim = string(dim,format='(i3.3)') else  $
             for j = 0, n_elements(dim)-1 do outDim +=string(dim[j],format='(i3.3)')+","

;----------  CDF data types to DD

            case info.datatype of
             "CDF_CHAR": datatype = 0; DD_CHAR
             
             "CDF_BYTE": datatype = 5;
             "CDF_INT1": datatype = 5;
             
             "CDF_INT2":  datatype = 4; DD_SHORT
             "CDF_UCHAR": datatype = 4;
             "CDF_UINT1": datatype = 4;
             
             "CDF_INT4":  datatype = 1; DD_INT 
             "CDF_UINT2": datatype = 1;

             "CDF_FLOAT": datatype = 2; DD_FLOAT
             "CDF_REAL4": datatype = 2; 

             "CDF_DOUBLE": datatype = 3; DD_DOUBLE
             "CDF_REAL8":  datatype = 3;
             "CDF_UINT4":  datatype = 3;
            end
            
            Output += "&&DATATYPE%" + strcompress(string(datatype)) + "&&DIMENSION%" + outDim 
              On_IoError, NoAttr
          
            for i = 0, N_elements(AttributesCommon)-1 do begin
             if (cdf_attexists(cdfid, AttributesCommon[i])) then begin
                cdf_attget, cdfid, AttributesCommon[i], par_num, attvalue
                Output +=  "&&"+strupcase(AttributesCommon[i])+"%" + strcompress(string(attvalue)) 
             endif else Output += "&&"+strupcase(AttributesCommon[i])+NODATA 
              continue
              NoAttr : Output += "&&"+strupcase(AttributesCommon[i])+NODATA     
           endfor
               
           if (n_elements(dim) eq 1 AND dim[0] eq 1) then begin
              On_IoError, NoAttr1
              cdf_attget, cdfid, AttributeScalar[0], par_num, attvalue
              Output +=  "&&"+AttributeScalar[0]+"%" + strcompress(string(attvalue)) 
              GOTO, CONT   
              NoAttr1: Output += "&&"+AttributeScalar[0]+NODATA
           endif else begin
              On_IoError, NoAttr2
              cdf_attget, cdfid, "LABL_PTR_1", par_num, attvalue 
              var_num = cdf_varnum(cdfid, attvalue, IsZvar)
              temp=strarr(dim)
              if (IsZVar) then  cdf_varget, cdfid, var_num, temp, /STRING, /ZVariable $
                  else cdf_varget, cdfid, var_num, temp, /STRING
              labelOutput = ""
; Crazy stuff - don't understand how to treat
              t_sort = temp(sort(reform(temp,N_elements(temp))))
              t_temp = t_sort(uniq(t_sort))
              for j = 0, N_elements(t_temp) - 1  do labelOutput += string(t_temp[j])+"$"
              Output +=  "&&LABLAXIS%" + strcompress(labelOutput)
              GOTO, CONT 
               NoAttr2:  On_IoError, NoAttr3
                 cdf_attget, cdfid, "DEPEND_1", par_num, attvalue
                 var_num = cdf_varnum(cdfid, attvalue, IsZvar)
                 cdf_attget, cdfid, "LABL_PTR_1", var_num, attvalue
                 var_num = cdf_varnum(cdfid, attvalue, IsZvar)
                 temp=strarr(dim)
                 if (IsZVar) then  cdf_varget,cdfid, var_num, temp, /STRING, /ZVARIABLE $
                   else cdf_varget,cdfid, var_num, temp, /STRING
                    labelOutput = ""
                  t_temp = temp(uniq(temp))
                  for j = 0, N_elements(t_temp)-1 do labelOutput += string(t_temp[j])+"$"
                  Output +=  "&&LABLAXIS%" + strcompress(labelOutput)
                  GOTO, CONT 

              NoAttr3: Output += "&&LABLAXIS"+NODATA
           endelse
            CONT: On_IoError, NoAttr4 
            if (cdf_attexists(cdfid, "VIRTUAL")) then begin
               cdf_attget, cdfid, "VIRTUAL", par_num, attvalue
               if (attvalue eq "TRUE") then begin
                  cdf_attget, cdfid, "COMPONENT_0", par_num, attvalue
                  Output += "&&VIRTUAL%" + attvalue         
              endif
            endif
            NoAttr4: On_IoError, NULL
           
      endif else begin ; z-Variable

;----------- Attributes & Dims for Z-vars -----------------------------   

        info = cdf_varinq(cdfid, par_num, /Z)
        if (n_elements(info.dimvar) GT 1) then begin
              print, NODATA
              return
        endif

        if (info.dimvar eq 0) then dim = 1 else dim = info.dim
;----------  CDF data types to DD

            case info.datatype of
             "CDF_CHAR": datatype = 0; DD_CHAR
             
             "CDF_BYTE": datatype = 5;
             "CDF_INT1": datatype = 5;
             
             "CDF_INT2": datatype = 4; DD_SHORT
             "CDF_UCHAR": datatype = 4;
             "CDF_UINT1": datatype = 4;
             
             "CDF_INT4": datatype = 1; DD_INT 
             "CDF_UINT2": datatype = 1;

             "CDF_FLOAT": datatype = 2; DD_FLOAT
             "CDF_REAL4": datatype = 2; 

             "CDF_DOUBLE": datatype = 3; DD_DOUBLE
             "CDF_REAL8":  datatype = 3;
             "CDF_UINT4":  datatype = 3;
            end
        Output += "&&DATATYPE%" + strcompress(string(datatype))+"&&DIMENSION%"+string(dim,format='(i3.3)')
        On_IoError, NoAttrZ
 
        for i = 0, N_elements(AttributesCommon)-1 do begin
          if (cdf_attexists(cdfid, AttributesCommon[i])) then begin 
             cdf_attget, cdfid, AttributesCommon[i], par_num, attvalue, /ZVARIABLE
             Output +=  "&&"+strupcase(AttributesCommon[i])+"%" + strcompress(string(attvalue)) 
           endif else Output += "&&"+strupcase(AttributesCommon[i])+NODATA
            continue 
            NoAttrZ: Output += "&&"+strupcase(AttributesCommon[i])+NODATA
       endfor
       
          if (dim eq 1) then begin 
              On_IoError, NoAttrZ1
              cdf_attget, cdfid, AttributeScalar[0], par_num, attvalue, /ZVARIABLE
              Output +=  "&&"+AttributeScalar[0]+"%" + strcompress(string(attvalue))  
              GOTO, CONT1   
              NoAttrZ1: Output += "&&"+AttributeScalar[0]+NODATA
           endif else begin
              On_IoError, NoAttrZ2
              cdf_attget, cdfid, "LABL_PTR_1", par_num, attvalue, /ZVARIABLE
              var_num = cdf_varnum(cdfid, attvalue, IsZvar)
              temp=strarr(dim)
              if (IsZVar) then  cdf_varget,cdfid, var_num, temp, /STRING, /ZVARIABLE $
                   else cdf_varget,cdfid, var_num, temp, /STRING
              labelOutput = ""
              t_temp  = temp(uniq(temp))
              for j = 0, N_elements(t_temp)-1 do labelOutput += string(t_temp[j])+"$"
              Output +=  "&&LABLAXIS%" + strcompress(labelOutput)
              GOTO, CONT1 
              NoAttrZ2: 
               On_IoError, NoAttrZ3
                 cdf_attget, cdfid, "DEPEND_1", par_num, attvalue, /ZVARIABLE
                 var_num = cdf_varnum(cdfid, attvalue, IsZvar)
                 cdf_attget, cdfid, "LABL_PTR_1", var_num, attvalue, /ZVARIABLE
                 var_num = cdf_varnum(cdfid, attvalue, IsZvar)
                 temp=strarr(dim)
                 if (IsZVar) then  cdf_varget,cdfid, var_num, temp, /STRING, /ZVARIABLE $
                   else cdf_varget,cdfid, var_num, temp, /STRING
                    labelOutput = ""
                  t_temp  = temp(uniq(temp))  
                  for j = 0, N_elements(t_temp)-1 do labelOutput += string(t_temp[j])+"$"
                  Output +=  "&&LABLAXIS%" + strcompress(labelOutput)
                  GOTO, CONT1 

             NoAttrZ3: Output += "&&LABLAXIS"+NODATA
           endelse
            CONT1: On_IoError, NoAttrZ4  
            if (cdf_attexists(cdfid, "VIRTUAL",/ZVARIABLE)) then begin
              cdf_attget, cdfid, "VIRTUAL", par_num, attvalue, /ZVARIABLE
              if (attvalue eq "TRUE") then begin
                 cdf_attget, cdfid, "COMPONENT_0", par_num, attvalue, /ZVARIABLE
                 Output += "&&VIRTUAL%" + attvalue               
              endif
            endif
            NoAttrZ4: On_IoError, NULL  
     endelse
     cdf_close,cdfID
     print, output
    
 return 
 end