1 | PRO ncdf_read,filename,info,dinfo,vinfo,gatts,vatts,data |
---|
2 | ; -------- ---- ----- ----- ----- ----- ---- |
---|
3 | ; | | | | | | |
---|
4 | ; general info --' | | | | `-- data |
---|
5 | ; dimension info --------' | | `-------- variable attributes |
---|
6 | ; variable info --------------' `-------------- global attributes |
---|
7 | ; |
---|
8 | ; ================== |
---|
9 | ; read a NetCDF file |
---|
10 | ; ================== |
---|
11 | ; |
---|
12 | ; NB The data is read into a rather nasty combination of structures, arrays, |
---|
13 | ; and pointers, which is, unfortunately, necessary in order to cope with |
---|
14 | ; the full generality of the data format. Here is the sort of syntax you |
---|
15 | ; might use to get at elements of the returned data -- cumbersome because |
---|
16 | ; IDL doesn't support C-type "a->b" shorthand for "(*a).b". |
---|
17 | ; |
---|
18 | ; "INFO.NDIMS" |
---|
19 | ; "INFO.NVARS" |
---|
20 | ; "INFO.NGATTS" |
---|
21 | ; "INFO.RECDIM" |
---|
22 | ; |
---|
23 | ; "DINFO[idim].NAME" |
---|
24 | ; "DINFO[idim].SIZE" |
---|
25 | ; |
---|
26 | ; "VINFO[ivar].NAME" |
---|
27 | ; "VINFO[ivar].NAME" |
---|
28 | ; "VINFO[ivar].DATATYPE" |
---|
29 | ; "VINFO[ivar].NDIMS" |
---|
30 | ; "VINFO[ivar].NATTS" |
---|
31 | ; "VINFO[ivar].DIM[ivdim]" |
---|
32 | ; |
---|
33 | ; "GATTS.NAME" |
---|
34 | ; "GATTS.DATATYPE" |
---|
35 | ; "GATTS.LENGTH" |
---|
36 | ; "*GATTS.VALUES" or maybe "STRING(*GATTS.VALUES)" |
---|
37 | ; |
---|
38 | ; "(*VATTS[ivar])[iatt].NAME" |
---|
39 | ; "(*VATTS[ivar])[iatt].DATATYPE" |
---|
40 | ; "(*VATTS[ivar])[iatt].LENGTH" |
---|
41 | ; "*(*VATTS[ivar])[iatt].VALUES" or maybe |
---|
42 | ; "STRING(*(*VATTS[ivar])[iatt].VALUES)" |
---|
43 | ; |
---|
44 | ; "*DATA[ivar]" or maybe "(*DATA[ivar])[idim1,idim2,idim3,...]" |
---|
45 | ; |
---|
46 | ;---------------------------------------------------------------------- |
---|
47 | ; |
---|
48 | compile_opt idl2, strictarrsubs |
---|
49 | ; |
---|
50 | |
---|
51 | |
---|
52 | ;; open file |
---|
53 | |
---|
54 | id=ncdf_open(filename) |
---|
55 | |
---|
56 | |
---|
57 | ;; info |
---|
58 | |
---|
59 | info=ncdf_inquire(id) |
---|
60 | |
---|
61 | |
---|
62 | ;; dimension info |
---|
63 | |
---|
64 | dinfo=replicate({name:"",size:0L},info.ndims) |
---|
65 | for idim=0,info.ndims-1 do begin $ |
---|
66 | ncdf_diminq,id,idim,name,size |
---|
67 | dinfo[idim].name=name |
---|
68 | dinfo[idim].size=size |
---|
69 | endfor |
---|
70 | |
---|
71 | |
---|
72 | ;; variable info |
---|
73 | |
---|
74 | vinfo=replicate({ name:"", $ |
---|
75 | datatype:"", $ |
---|
76 | ndims:0l, natts:0l, $ |
---|
77 | dim:lonarr(info.ndims) $ |
---|
78 | },info.nvars) |
---|
79 | |
---|
80 | for ivar=0,info.nvars-1 do begin |
---|
81 | var=ncdf_varinq(id,ivar) |
---|
82 | vinfo[ivar].name=var.name |
---|
83 | vinfo[ivar].datatype=var.datatype |
---|
84 | vinfo[ivar].ndims=var.ndims |
---|
85 | vinfo[ivar].natts=var.natts |
---|
86 | vinfo[ivar].dim=var.dim |
---|
87 | endfor |
---|
88 | |
---|
89 | |
---|
90 | ;; global attributes |
---|
91 | |
---|
92 | if info.ngatts gt 0 then begin |
---|
93 | gatts=replicate({name:'', $ |
---|
94 | datatype:'', $ |
---|
95 | length:0L, $ |
---|
96 | values:ptr_new()}, $ |
---|
97 | info.ngatts) |
---|
98 | for iatt=0,info.ngatts-1 do begin |
---|
99 | name=ncdf_attname(id,iatt,/global) |
---|
100 | gatts[iatt].name=name |
---|
101 | att=ncdf_attinq(id,name,/global) |
---|
102 | gatts[iatt].length=att.length |
---|
103 | gatts[iatt].datatype=att.datatype |
---|
104 | ncdf_attget,id,name,vals,/global |
---|
105 | gatts[iatt].values=ptr_new(vals) |
---|
106 | endfor |
---|
107 | endif else begin |
---|
108 | ;; arbitary scalar value |
---|
109 | ;; an empty list would be sensible but IDL doesn't support it |
---|
110 | gatts=-1 |
---|
111 | endelse |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | ;; variable attributes |
---|
116 | |
---|
117 | vatts=replicate(ptr_new(),info.nvars) |
---|
118 | for ivar=0,info.nvars-1 do begin |
---|
119 | if vinfo[ivar].natts gt 0 then begin |
---|
120 | vatts[ivar]=ptr_new(replicate({name:'', $ |
---|
121 | datatype:'', $ |
---|
122 | length:0L, $ |
---|
123 | values:ptr_new()}, $ |
---|
124 | vinfo[ivar].natts)) |
---|
125 | for iatt=0,vinfo[ivar].natts-1 do begin |
---|
126 | name=ncdf_attname(id,ivar,iatt) |
---|
127 | (*vatts[ivar])[iatt].name=name |
---|
128 | att=ncdf_attinq(id,ivar,name) |
---|
129 | (*vatts[ivar])[iatt].length=att.length |
---|
130 | (*vatts[ivar])[iatt].datatype=att.datatype |
---|
131 | ncdf_attget,id,ivar,name,vals |
---|
132 | (*vatts[ivar])[iatt].values=ptr_new(vals) |
---|
133 | endfor |
---|
134 | endif else begin |
---|
135 | vatts[ivar]=ptr_new(-1) |
---|
136 | ;; Pointer to arbitrary scalar -- analogous to case of lack of |
---|
137 | ;; global attributes above. We could put a <NullPointer> here |
---|
138 | ;; instead, but try to be friendlier to code that might try |
---|
139 | ;; to dereference it. |
---|
140 | endelse |
---|
141 | endfor |
---|
142 | |
---|
143 | |
---|
144 | ;; data |
---|
145 | |
---|
146 | data=replicate(ptr_new(),info.nvars) |
---|
147 | for ivar=0,info.nvars-1 do begin |
---|
148 | ncdf_varget,id,ivar,val |
---|
149 | data[ivar]=ptr_new(val) |
---|
150 | endfor |
---|
151 | |
---|
152 | |
---|
153 | end |
---|