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 | ; @examples |
---|
47 | ; IDL> ncdf_getatt, cdfid, 'sst', add_offset = add_offset, scale_factor = scale_factor, units = units |
---|
48 | ; |
---|
49 | ; @history |
---|
50 | ; August 2007: Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
51 | ; |
---|
52 | ; @version |
---|
53 | ; $Id$ |
---|
54 | ; |
---|
55 | ;- |
---|
56 | PRO ncdf_getatt, fileid, varid, add_offset=add_offset $ |
---|
57 | , scale_factor=scale_factor, missing_value=missing_value $ |
---|
58 | , units=units, calendar=calendar |
---|
59 | ; |
---|
60 | compile_opt idl2, strictarrsubs |
---|
61 | ; |
---|
62 | ; should we open the file? |
---|
63 | IF size(fileid, /type) EQ 7 THEN cdfid = ncdf_open(fileid) ELSE cdfid = fileid |
---|
64 | ; default definitions |
---|
65 | units = '' |
---|
66 | add_offset = 0. |
---|
67 | scale_factor = 1. |
---|
68 | missing_value = 'no' |
---|
69 | calendar = 'greg' |
---|
70 | ; |
---|
71 | varinq = ncdf_varinq(cdfid, varid) |
---|
72 | IF varinq.natts GT 0 THEN BEGIN |
---|
73 | FOR a = 0, varinq.natts-1 DO BEGIN |
---|
74 | attname = ncdf_attname(cdfid, varid, a) |
---|
75 | CASE strlowcase(attname) OF |
---|
76 | 'units':BEGIN |
---|
77 | ncdf_attget, cdfid, varid, attname, tmp |
---|
78 | units = strtrim(tmp, 2) |
---|
79 | END |
---|
80 | 'calendar':BEGIN |
---|
81 | ncdf_attget, cdfid, varid, attname, tmp |
---|
82 | tmp = strtrim(tmp, 2) |
---|
83 | CASE tmp OF |
---|
84 | 'noleap':calendar = 'noleap' |
---|
85 | '360d':calendar = '360d' |
---|
86 | 'greg':IF n_elements(calendar) EQ 0 THEN calendar = 'greg' |
---|
87 | ELSE:BEGIN |
---|
88 | ; notused = report('Unknown calendar: '+tmp+', we use greg calendar.') |
---|
89 | calendar = 'greg' |
---|
90 | END |
---|
91 | ENDCASE |
---|
92 | END |
---|
93 | 'add_offset':ncdf_attget, cdfid, varid, attname, add_offset |
---|
94 | 'scale_factor':ncdf_attget, cdfid, varid, attname, scale_factor |
---|
95 | '_fillvalue':ncdf_attget, cdfid, varid, attname, missing_value |
---|
96 | 'missing_value':ncdf_attget, cdfid, varid, attname, missing_value |
---|
97 | ELSE: |
---|
98 | ENDCASE |
---|
99 | ENDFOR |
---|
100 | ENDIF |
---|
101 | ; |
---|
102 | IF size(missing_value, /type) EQ 1 THEN BEGIN |
---|
103 | missing_value = strlowcase(string(missing_value)) |
---|
104 | IF strmid(missing_value, 0, 1, /reverse_offset) EQ 'f' THEN $ |
---|
105 | missing_value = strmid(missing_value, 0, strlen(missing_value)-1) |
---|
106 | IF isnumber(string(missing_value), tmp) EQ 1 THEN BEGIN |
---|
107 | missing_value = tmp |
---|
108 | ENDIF ELSE BEGIN |
---|
109 | ras = report('Warning: missing value is not a number: ' + missing_value) |
---|
110 | missing_value = 'no' |
---|
111 | ENDELSE |
---|
112 | ENDIF |
---|
113 | |
---|
114 | IF size(fileid, /type) EQ 7 THEN ncdf_close, cdfid |
---|
115 | |
---|
116 | return |
---|
117 | END |
---|