;+ ; ; @file_comments ; Get variable attibutes from a NetCDF file ; ; @categories ; Read NetCDF file ; ; @param fileid {in}{required}{type=salar string or long} ; if fileid is a scalar string then it is the name of the file (with ; the full path) to be opened (in that case, the file will be opened ; and closed within ncdf_getatt). ; if fileid is a scalar long then it is the id of the file return by a call ; to ncdf_open outside of ncdf_getatt (in that case, the file will ; NOT be opened and closed within ncdf_getatt) ; ; @param varid {in}{required}{type=salar string or long} ; The netCDF variable ID, returned from a previous call to NCDF_VARDEF ; or NCDF_VARID, or the name of the variable. ; ; @keyword add_offset ; Set this keyword to a named variable in which the value of ; add_offset attribute is returned. Return 0. if this attribute ; doesn't exist. ; ; @keyword scale_factor ; Set this keyword to a named variable in which the value of ; scale_factor attribute is returned. Return 0. if this attribute ; doesn't exist. ; ; @keyword missing_value ; Set this keyword to a named variable in which the value of ; missing_value or _fillvalue attribute is returned. Return the scalar ; 'no' if this attribute doesn't exist. ; ; @keyword units ; Set this keyword to a named variable in which the value of ; units attribute is returned. Return the empty string '' if this attribute ; doesn't exist. ; ; @keyword calendar ; Set this keyword to a named variable in which the value of ; calendar attribute is returned. Return the string 'greg' if this attribute ; doesn't exist. ; ; @examples ; IDL> ncdf_getatt, cdfid, 'sst', add_offset = add_offset, scale_factor = scale_factor, units = units ; ; @history ; August 2007: Sebastien Masson (smasson\@lodyc.jussieu.fr) ; ; @version ; $Id$ ; ;- PRO ncdf_getatt, fileid, varid, add_offset = add_offset, scale_factor = scale_factor, missing_value = missing_value, units = units, calendar = calendar ; compile_opt idl2, strictarrsubs ; ; should we open the file? IF size(fileid, /type) EQ 7 THEN cdfid = ncdf_open(fileid) ELSE cdfid = fileid ; default definitions units = '' add_offset = 0. scale_factor = 1. missing_value = 'no' calendar = 'greg' ; varinq = ncdf_varinq(cdfid, varid) IF varinq.natts GT 0 THEN BEGIN FOR a = 0, varinq.natts-1 DO BEGIN attname = ncdf_attname(cdfid, varid, a) CASE strlowcase(attname) OF 'units':BEGIN ncdf_attget, cdfid, varid, attname, tmp units = strtrim(tmp, 2) END 'calendar':BEGIN ncdf_attget, cdfid, varid, attname, tmp tmp = strtrim(tmp, 2) CASE tmp OF 'noleap':calendar = 'noleap' '360d':calendar = '360d' 'greg':IF n_elements(calendar) EQ 0 THEN calendar = 'greg' ELSE:BEGIN ; notused = report('Unknown calendar: '+tmp+', we use greg calendar.') calendar = 'greg' END ENDCASE END 'add_offset':ncdf_attget, cdfid, varid, attname, add_offset 'scale_factor':ncdf_attget, cdfid, varid, attname, scale_factor '_fillvalue':ncdf_attget, cdfid, varid, attname, missing_value 'missing_value':ncdf_attget, cdfid, varid, attname, missing_value ELSE: ENDCASE ENDFOR ENDIF ; IF size(missing_value, /type) EQ 1 THEN BEGIN missing_value = strlowcase(string(missing_value)) IF strmid(missing_value, 0, 1, /reverse_offset) EQ 'f' THEN $ missing_value = strmid(missing_value, 0, strlen(missing_value)-1) IF isnumber(string(missing_value), tmp) EQ 1 THEN BEGIN missing_value = tmp ENDIF ELSE BEGIN ras = report('Warning: missing value is not a number: ' + missing_value) missing_value = 'no' ENDELSE ENDIF IF size(fileid, /type) EQ 7 THEN ncdf_close, cdfid return END