1 | ;+ |
---|
2 | ; @file_comments |
---|
3 | ; |
---|
4 | ; |
---|
5 | ; @categories |
---|
6 | ; |
---|
7 | ; |
---|
8 | ; @param FILENAME |
---|
9 | ; The file's name |
---|
10 | ; |
---|
11 | ; @keyword NODATA |
---|
12 | ; |
---|
13 | ; |
---|
14 | ; @keyword NOATTRIBUTES |
---|
15 | ; |
---|
16 | ; |
---|
17 | ; @returns |
---|
18 | ; |
---|
19 | ; |
---|
20 | ; @restrictions |
---|
21 | ; |
---|
22 | ; |
---|
23 | ; @examples |
---|
24 | ; |
---|
25 | ; |
---|
26 | ; @history |
---|
27 | ; |
---|
28 | ; |
---|
29 | ; @version |
---|
30 | ; $Id$ |
---|
31 | ; |
---|
32 | ;- |
---|
33 | FUNCTION ncdf_struct,filename,nodata=nodata,noattributes=noattributes |
---|
34 | ; |
---|
35 | ; Read entire netcdf file into a structure. |
---|
36 | ; |
---|
37 | ; (Structure contains metadata; actual array contents are on heap, with |
---|
38 | ; pointers contained in the structure. Heap variables not created if |
---|
39 | ; "nodata" specified.) |
---|
40 | ; |
---|
41 | ; Use ncdf_struct_free to free heap memory. |
---|
42 | ; |
---|
43 | ; (Some data is duplicated for ease of access, in particular if there is |
---|
44 | ; a variable name matching a dimension name, then a pointer to the variable |
---|
45 | ; contents is accessible via the substructures corresponding to the dimension |
---|
46 | ; and every other variable that uses it.) |
---|
47 | ; |
---|
48 | ; Alan Iwi 27/6/02 |
---|
49 | ; |
---|
50 | ; |
---|
51 | compile_opt idl2, strictarrsubs |
---|
52 | ; |
---|
53 | |
---|
54 | id=ncdf_open(filename) |
---|
55 | |
---|
56 | g=ncdf_inquire(id) |
---|
57 | ndim=g.ndims |
---|
58 | nvar=g.nvars |
---|
59 | natt=g.ngatts |
---|
60 | |
---|
61 | if ndim gt 0 then begin |
---|
62 | dnames=strarr(ndim) |
---|
63 | dsizes=lonarr(ndim) |
---|
64 | for idim=0,ndim-1 do begin |
---|
65 | ncdf_diminq,id,idim,dname,dsize |
---|
66 | dnames[idim]=dname |
---|
67 | dsizes[idim]=dsize |
---|
68 | endfor |
---|
69 | endif |
---|
70 | |
---|
71 | if natt gt 0 and not keyword_set(noattributes) then begin |
---|
72 | anames=strarr(natt) |
---|
73 | for iatt=0,natt-1 do begin |
---|
74 | aname=ncdf_attname(id,/global,iatt) |
---|
75 | ainq=ncdf_attinq(id,/global,aname) |
---|
76 | ncdf_attget,id,/global,aname,aval |
---|
77 | if (ainq.datatype eq 'CHAR') then aval=string(aval) |
---|
78 | if iatt eq 0 then begin |
---|
79 | atts=create_struct(aname,aval) |
---|
80 | endif else begin |
---|
81 | atts=create_struct(atts,aname,aval) |
---|
82 | endelse |
---|
83 | anames[iatt]=aname |
---|
84 | endfor |
---|
85 | g=create_struct(g,'gatts',atts,'gattnames',anames) |
---|
86 | endif |
---|
87 | |
---|
88 | if nvar gt 0 then begin |
---|
89 | vnames=strarr(nvar) |
---|
90 | for ivar=0,nvar-1 do begin |
---|
91 | v=ncdf_varinq(id,ivar) |
---|
92 | |
---|
93 | vname=v.name |
---|
94 | vndim=v.ndims |
---|
95 | vnatt=v.natts |
---|
96 | vname=v.name |
---|
97 | |
---|
98 | if vnatt gt 0 and not keyword_set(noattributes) then begin |
---|
99 | vanames=strarr(vnatt) |
---|
100 | for iatt=0,vnatt-1 do begin |
---|
101 | aname=ncdf_attname(id,ivar,iatt) |
---|
102 | ainq=ncdf_attinq(id,ivar,aname) |
---|
103 | ncdf_attget,id,ivar,aname,aval |
---|
104 | if (ainq.datatype eq 'CHAR') then aval=string(aval) |
---|
105 | if iatt eq 0 then begin |
---|
106 | atts=create_struct(aname,aval) |
---|
107 | endif else begin |
---|
108 | atts=create_struct(atts,aname,aval) |
---|
109 | endelse |
---|
110 | vanames[iatt]=aname |
---|
111 | endfor |
---|
112 | v=create_struct(v,'atts',atts,'attnames',anames) |
---|
113 | endif |
---|
114 | |
---|
115 | vdnames=dnames[v.dim] |
---|
116 | vdsizes=dsizes[v.dim] |
---|
117 | v=create_struct(v,'dimnames',vdnames,'dimsizes',vdsizes) |
---|
118 | |
---|
119 | if not keyword_set(nodata) then begin |
---|
120 | ncdf_varget,id,ivar,vdata |
---|
121 | v=create_struct(v,'data',ptr_new(vdata), $ |
---|
122 | 'dimdata',replicate(ptr_new(),vndim)) |
---|
123 | endif |
---|
124 | |
---|
125 | if ivar eq 0 then begin |
---|
126 | vars=create_struct(vname,v) |
---|
127 | endif else begin |
---|
128 | vars=create_struct(vars,vname,v) |
---|
129 | endelse |
---|
130 | |
---|
131 | vnames[ivar]=vname |
---|
132 | endfor |
---|
133 | endif |
---|
134 | |
---|
135 | if ndim gt 0 then begin |
---|
136 | for idim=0,ndim-1 do begin |
---|
137 | dname=dnames[idim] |
---|
138 | d={name:dname,size:dsizes[idim]} |
---|
139 | |
---|
140 | if not keyword_set(nodata) and nvar gt 0 then begin |
---|
141 | matchvar=-1 |
---|
142 | for ivar=0,nvar-1 do begin |
---|
143 | if vnames[ivar] eq dname then matchvar=ivar |
---|
144 | endfor |
---|
145 | if matchvar ne -1 then $ |
---|
146 | d=create_struct(d,'data',vars.(matchvar).data) |
---|
147 | endif |
---|
148 | |
---|
149 | if idim eq 0 then begin |
---|
150 | dims=create_struct(dname,d) |
---|
151 | endif else begin |
---|
152 | dims=create_struct(dims,dname,d) |
---|
153 | endelse |
---|
154 | |
---|
155 | endfor |
---|
156 | g=create_struct(g,'dims',dims,'dimnames',dnames,'dimsizes',dsizes) |
---|
157 | endif |
---|
158 | |
---|
159 | if nvar gt 0 then begin |
---|
160 | if not keyword_set(nodata) then begin |
---|
161 | for ivar=0,nvar-1 do begin |
---|
162 | for idim=0,vars.(ivar).ndims-1 do begin |
---|
163 | vars.(ivar).dimdata[idim]=dims.(vars.(ivar).dim[idim]).data |
---|
164 | endfor |
---|
165 | endfor |
---|
166 | endif |
---|
167 | g=create_struct(g,'vars',vars,'varnames',vnames) |
---|
168 | endif |
---|
169 | |
---|
170 | |
---|
171 | ncdf_close,id |
---|
172 | |
---|
173 | return,g |
---|
174 | |
---|
175 | end |
---|