source: trunk/SRC/ReadWrite/idl-NetCDF/ncdf_read.pro @ 378

Last change on this file since 378 was 157, checked in by navarro, 18 years ago

header improvements + xxx doc

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