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

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1PRO 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
54id=ncdf_open(filename)
55
56
57;; info
58
59info=ncdf_inquire(id)
60
61
62;; dimension info
63
64dinfo=replicate({name:"",size:0L},info.ndims)
65for idim=0,info.ndims-1 do begin $
66  ncdf_diminq,id,idim,name,size
67  dinfo[idim].name=name
68  dinfo[idim].size=size
69endfor
70
71
72;; variable info
73
74vinfo=replicate({ name:"", $
75                  datatype:"", $
76                  ndims:0l, natts:0l, $
77                  dim:lonarr(info.ndims) $
78                },info.nvars)
79
80for 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
87endfor
88
89
90;; global attributes
91
92if 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
107endif else begin
108    ;; arbitary scalar value
109    ;; an empty list would be sensible but IDL doesn't support it
110    gatts=-1
111endelse
112
113
114
115;; variable attributes
116
117vatts=replicate(ptr_new(),info.nvars)
118for 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
141endfor
142
143
144;; data
145
146data=replicate(ptr_new(),info.nvars)
147for ivar=0,info.nvars-1 do begin
148    ncdf_varget,id,ivar,val
149    data[ivar]=ptr_new(val)
150endfor
151
152
153end
Note: See TracBrowser for help on using the repository browser.