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 | ;- |
---|
75 | PRO 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 | @cm_general ; needed for iodir |
---|
83 | ; |
---|
84 | ; should we open the file? |
---|
85 | IF size(fileid, /type) EQ 7 THEN $ |
---|
86 | cdfid = ncdf_open(isafile(fileid, title = 'which file must be open by ncdf_getatt?', IODIRECTORY = iodir, _extra = ex)) $ |
---|
87 | ELSE cdfid = fileid |
---|
88 | ; default definitions |
---|
89 | units = '' |
---|
90 | add_offset = 0. |
---|
91 | scale_factor = 1. |
---|
92 | missing_value = 'no' |
---|
93 | calendar = 'greg' |
---|
94 | long_name = '' |
---|
95 | standard_name = '' |
---|
96 | ; |
---|
97 | varinq = ncdf_varinq(cdfid, varid) |
---|
98 | IF varinq.natts GT 0 THEN BEGIN |
---|
99 | FOR a = 0, varinq.natts-1 DO BEGIN |
---|
100 | attname = ncdf_attname(cdfid, varid, a) |
---|
101 | CASE strlowcase(attname) OF |
---|
102 | 'units':BEGIN |
---|
103 | ncdf_attget, cdfid, varid, attname, tmp |
---|
104 | units = strcompress(strtrim(tmp, 2)) |
---|
105 | END |
---|
106 | 'calendar':BEGIN |
---|
107 | ncdf_attget, cdfid, varid, attname, tmp |
---|
108 | tmp = strtrim(tmp, 2) |
---|
109 | CASE tmp OF |
---|
110 | 'noleap':calendar = 'noleap' |
---|
111 | '360d':calendar = '360d' |
---|
112 | 'greg':calendar = 'greg' |
---|
113 | '360_day':calendar = '360d' |
---|
114 | '365_day':calendar = 'noleap' |
---|
115 | 'gregorian': calendar = 'greg' |
---|
116 | ELSE:notused = report('Unknown calendar: '+tmp+', we use greg calendar.') |
---|
117 | ENDCASE |
---|
118 | END |
---|
119 | 'long_name':BEGIN |
---|
120 | ncdf_attget, cdfid, varid, attname, tmp |
---|
121 | long_name = strtrim(tmp, 2) |
---|
122 | END |
---|
123 | 'standard_name':BEGIN |
---|
124 | ncdf_attget, cdfid, varid, attname, tmp |
---|
125 | standard_name = strtrim(tmp, 2) |
---|
126 | END |
---|
127 | 'add_offset':ncdf_attget, cdfid, varid, attname, add_offset |
---|
128 | 'scale_factor':ncdf_attget, cdfid, varid, attname, scale_factor |
---|
129 | '_fillvalue':ncdf_attget, cdfid, varid, attname, missing_value |
---|
130 | 'missing_value':ncdf_attget, cdfid, varid, attname, missing_value |
---|
131 | ELSE: |
---|
132 | ENDCASE |
---|
133 | ENDFOR |
---|
134 | ENDIF |
---|
135 | ; |
---|
136 | IF size(missing_value, /type) EQ 1 THEN BEGIN |
---|
137 | missing_value = strlowcase(string(missing_value)) |
---|
138 | IF strmid(missing_value, 0, 1, /reverse_offset) EQ 'f' THEN $ |
---|
139 | missing_value = strmid(missing_value, 0, strlen(missing_value)-1) |
---|
140 | IF isnumber(string(missing_value), tmp) EQ 1 THEN BEGIN |
---|
141 | missing_value = tmp |
---|
142 | ENDIF ELSE BEGIN |
---|
143 | ras = report('Warning: missing value is not a number: ' + missing_value) |
---|
144 | missing_value = 'no' |
---|
145 | ENDELSE |
---|
146 | ENDIF |
---|
147 | ; |
---|
148 | IF keyword_set(double) THEN BEGIN |
---|
149 | add_offset = double(add_offset) |
---|
150 | scale_factor = double(scale_factor) |
---|
151 | ENDIF ELSE BEGIN |
---|
152 | add_offset = float(add_offset) |
---|
153 | scale_factor = float(scale_factor) |
---|
154 | ENDELSE |
---|
155 | ; |
---|
156 | IF size(fileid, /type) EQ 7 THEN ncdf_close, cdfid |
---|
157 | |
---|
158 | return |
---|
159 | END |
---|