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