source: trunk/SRC/ReadWrite/ncdf_getatt.pro

Last change on this file was 501, checked in by smasson, 8 years ago

set of bugfixes...

  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1;+
2;
3; @file_comments
4; Get variable attributes from a NetCDF file
5;
6; @categories
7; Read NetCDF file
8;
9; @param fileid {in}{required}{type=salar string or long}
10; if fileid is a scalar string then it is the name of the file (with
11; the full path) to be opened (in that case, the file will be opened
12; and closed within ncdf_getatt).
13; if fileid is a scalar long then it is the id of the file return by a call
14; to ncdf_open outside of ncdf_getatt (in that case, the file will
15; NOT be opened and closed within ncdf_getatt)
16;
17; @param varid {in}{required}{type=salar string or long}
18; The netCDF variable ID, returned from a previous call to NCDF_VARDEF
19; or NCDF_VARID, or the name of the variable.
20;
21; @keyword add_offset
22; Set this keyword to a named variable in which the value of
23; add_offset attribute is returned. Return 0. if this attribute
24; doesn't exist.
25;
26; @keyword scale_factor
27; Set this keyword to a named variable in which the value of
28; scale_factor attribute is returned. Return 0. if this attribute
29; doesn't exist.
30;
31; @keyword missing_value
32; Set this keyword to a named variable in which the value of
33; missing_value or _fillvalue attribute is returned. Return the scalar
34; 'no' if this attribute doesn't exist.
35;
36; @keyword units
37; Set this keyword to a named variable in which the value of
38; units attribute is returned. Return the empty string '' if this attribute
39; doesn't exist.
40;
41; @keyword calendar
42; Set this keyword to a named variable in which the value of
43; calendar attribute is returned. Return the string 'greg' if this attribute
44; doesn't exist.
45;
46; @keyword long_name
47; Set this keyword to a named variable in which the value of
48; long_name is returned. Return empty string if this attribute
49; does not exist.
50;
51; @keyword DOUBLE {default=0} {type=1 or 0}
52; activate to force double-precision floating-point value of
53; add_offset and scale_factor
54;
55; @keyword standard_name
56; Set this keyword to a named variable in which the value of
57; standard_name (CF convention) is returned. Return empty string
58; if this attribute does not exist.
59;
60; @keyword interval_operation
61; Set this keyword to a named variable in which the value of
62; interval_operation is returned. Return -1 if this attribute
63; does not exist.
64;
65; @keyword _EXTRA
66; defined only to be able to call <pro>ncdf_getatt</pro> with the
67; _EXTRA keyword
68;
69; @examples
70;
71;   IDL> ncdf_getatt, cdfid, 'sst', add_offset = add_offset, scale_factor = scale_factor, units = units
72;
73; @history
74; August 2007: Sebastien Masson (smasson\@lodyc.jussieu.fr)
75;
76; @version
77; $Id$
78;
79;-
80PRO ncdf_getatt, fileid, varid, ADD_OFFSET = add_offset, SCALE_FACTOR = scale_factor $
81                 , MISSING_VALUE = missing_value, UNITS = units, CALENDAR = calendar $
82                 , LONG_NAME = long_name, STANDARD_NAME = standard_name, DOUBLE = double $
83                 , INTERVAL_OPERATION = interval_operation, _extra = ex
84;
85  compile_opt idl2, strictarrsubs
86;
87  @cm_general                      ;   needed for iodir
88;
89; should we open the file?
90  IF size(fileid, /type) EQ 7 THEN $
91     cdfid = ncdf_open(isafile(fileid, title = 'which file must be open by ncdf_getatt?', IODIRECTORY = iodir, _extra = ex)) $
92  ELSE cdfid = fileid
93; default definitions
94  units = ''
95  IF n_elements(add_offset) EQ 0 THEN add_offset = 0.
96  IF n_elements(scale_factor) EQ 0 THEN scale_factor = 1.
97  missing_value = 'no'
98  calendar = 'greg'
99  long_name = ''
100  standard_name = ''
101  interval_operation = -1
102;
103  varinq = ncdf_varinq(cdfid, varid)
104  IF varinq.natts GT 0 THEN BEGIN
105    FOR a = 0, varinq.natts-1 DO BEGIN
106      attname = ncdf_attname(cdfid, varid, a)
107      CASE strlowcase(attname) OF
108        'units':BEGIN
109          ncdf_attget, cdfid, varid, attname, tmp
110          units = strcompress(strtrim(tmp, 2))
111        END
112        'calendar':BEGIN
113          ncdf_attget, cdfid, varid, attname, tmp
114          tmp = strtrim(tmp, 2)
115          CASE tmp OF
116            'noleap':calendar = 'noleap'
117            '365day':calendar = 'noleap'
118            '365days':calendar = 'noleap'
119            '365_day':calendar = 'noleap'
120            '365_days':calendar = 'noleap'
121            '360d':calendar = '360d'
122            '360day':calendar = '360d'
123            '360days':calendar = '360d'
124            '360_day':calendar = '360d'
125            '360_days':calendar = '360d'
126            'standard':calendar = 'greg'
127            'greg':calendar = 'greg'
128            'gregorian':calendar = 'greg'
129            ELSE:notused = report('Unknown calendar: '+tmp+', we use greg calendar.')
130          ENDCASE
131        END
132        'long_name':BEGIN
133           ncdf_attget, cdfid, varid, attname, tmp
134           long_name = strtrim(tmp, 2)
135        END
136        'standard_name':BEGIN
137           ncdf_attget, cdfid, varid, attname, tmp
138           standard_name = strtrim(tmp, 2)
139        END
140        'add_offset':ncdf_attget, cdfid, varid, attname, add_offset
141        'scale_factor':ncdf_attget, cdfid, varid, attname, scale_factor
142        '_fillvalue':ncdf_attget, cdfid, varid, attname, missing_value
143        'missing_value':ncdf_attget, cdfid, varid, attname, missing_value
144        'interval_operation':BEGIN
145          ncdf_attget, cdfid, varid, attname, tmp
146          tmp = string(tmp)
147          interval_operation = fix(strmid(tmp,0,strpos(tmp,' ')))
148        END
149        ELSE:
150      ENDCASE
151    ENDFOR
152  ENDIF
153;
154  IF size(missing_value, /type) EQ 1 THEN BEGIN
155    missing_value = strlowcase(string(missing_value))
156    IF strmid(missing_value, 0, 1, /reverse_offset) EQ 'f' THEN $
157       missing_value = strmid(missing_value, 0, strlen(missing_value)-1)
158    IF isnumber(string(missing_value), tmp) EQ 1 THEN BEGIN
159      missing_value = tmp
160    ENDIF ELSE BEGIN
161      ;; ras = report('Warning: missing value is not a number: ' + missing_value)
162      ;; missing_value = 'no'
163      missing_value = (byte(strlowcase(a)))[0]
164    ENDELSE
165  ENDIF
166;
167  IF keyword_set(double) THEN BEGIN
168    add_offset = double(add_offset)
169    scale_factor = double(scale_factor)
170  ENDIF ELSE BEGIN
171    add_offset = float(add_offset)
172    scale_factor = float(scale_factor)
173  ENDELSE
174;
175  IF size(fileid, /type) EQ 7 THEN ncdf_close, cdfid
176
177  return
178END
Note: See TracBrowser for help on using the repository browser.