source: trunk/SRC/ReadWrite/ncdf_getatt.pro @ 378

Last change on this file since 378 was 372, checked in by pinsard, 16 years ago

improvements of headers (alignments)

  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1;+
2;
3; @file_comments
4; Get variable attibutes 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 _EXTRA
61; defined only to be able to call <pro>ncdf_getatt</pro> with the
62; _EXTRA keyword
63;
64; @examples
65;
66;   IDL> ncdf_getatt, cdfid, 'sst', add_offset = add_offset, scale_factor = scale_factor, units = units
67;
68; @history
69; August 2007: Sebastien Masson (smasson\@lodyc.jussieu.fr)
70;
71; @version
72; $Id$
73;
74;-
75PRO ncdf_getatt, fileid, varid, ADD_OFFSET = add_offset, SCALE_FACTOR = scale_factor $
76                 , MISSING_VALUE = missing_value, UNITS = units, CALENDAR = calendar $
77                 , LONG_NAME = long_name, STANDARD_NAME = standard_name, DOUBLE = double $
78                 , _extra = ex
79;
80  compile_opt idl2, strictarrsubs
81;
82; should we open the file?
83  IF size(fileid, /type) EQ 7 THEN cdfid = ncdf_open(fileid) ELSE cdfid = fileid
84; default definitions
85  units = ''
86  add_offset = 0.
87  scale_factor = 1.
88  missing_value = 'no'
89  calendar = 'greg'
90  long_name = ''
91  standard_name = ''
92;
93  varinq = ncdf_varinq(cdfid, varid)
94  IF varinq.natts GT 0 THEN BEGIN
95    FOR a = 0, varinq.natts-1 DO BEGIN
96      attname = ncdf_attname(cdfid, varid, a)
97      CASE strlowcase(attname) OF
98        'units':BEGIN
99          ncdf_attget, cdfid, varid, attname, tmp
100          units = strtrim(tmp, 2)
101        END
102        'calendar':BEGIN
103          ncdf_attget, cdfid, varid, attname, tmp
104          tmp = strtrim(tmp, 2)
105          CASE tmp OF
106            'noleap':calendar = 'noleap'
107            '360d':calendar = '360d'
108            'greg':calendar = 'greg'
109            '360_day':calendar = '360d'
110            '365_day':calendar = 'noleap'
111            'gregorian': calendar = 'greg'
112            ELSE:notused = report('Unknown calendar: '+tmp+', we use greg calendar.')
113          ENDCASE
114        END
115        'long_name':BEGIN
116           ncdf_attget, cdfid, varid, attname, tmp
117           long_name = strtrim(tmp, 2)           
118        END
119        'standard_name':BEGIN
120           ncdf_attget, cdfid, varid, attname, tmp
121           standard_name = strtrim(tmp, 2)           
122        END
123        'add_offset':ncdf_attget, cdfid, varid, attname, add_offset
124        'scale_factor':ncdf_attget, cdfid, varid, attname, scale_factor
125        '_fillvalue':ncdf_attget, cdfid, varid, attname, missing_value
126        'missing_value':ncdf_attget, cdfid, varid, attname, missing_value
127        ELSE:
128      ENDCASE
129    ENDFOR
130  ENDIF
131;
132  IF size(missing_value, /type) EQ 1 THEN BEGIN
133    missing_value = strlowcase(string(missing_value))
134    IF strmid(missing_value, 0, 1, /reverse_offset) EQ 'f' THEN $
135       missing_value = strmid(missing_value, 0, strlen(missing_value)-1)
136    IF isnumber(string(missing_value), tmp) EQ 1 THEN BEGIN
137      missing_value = tmp
138    ENDIF ELSE BEGIN
139      ras = report('Warning: missing value is not a number: ' + missing_value)
140      missing_value = 'no'
141    ENDELSE
142  ENDIF
143;
144  IF keyword_set(double) THEN BEGIN
145    add_offset = double(add_offset)
146    scale_factor = double(scale_factor)
147  ENDIF ELSE BEGIN
148    add_offset = float(add_offset)
149    scale_factor = float(scale_factor)
150  ENDELSE
151;
152  IF size(fileid, /type) EQ 7 THEN ncdf_close, cdfid
153 
154  return
155END
Note: See TracBrowser for help on using the repository browser.