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

Last change on this file since 70 was 70, checked in by pinsard, 18 years ago

update according to idl-NetCDF-v04.tar.gz available via http://www.ittvis.com/codebank/search.asp?FID=418; side effects are not tested

  • 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
49;; open file
50
51id=ncdf_open(filename)
52
53
54;; info
55
56info=ncdf_inquire(id)
57
58
59;; dimension info
60
61dinfo=replicate({name:"",size:0L},info.ndims)
62for idim=0,info.ndims-1 do begin $
63  ncdf_diminq,id,idim,name,size
64  dinfo[idim].name=name
65  dinfo[idim].size=size
66endfor
67
68
69;; variable info
70
71vinfo=replicate({ name:"", $
72                  datatype:"", $
73                  ndims:0l, natts:0l, $
74                  dim:lonarr(info.ndims) $
75                },info.nvars)
76
77for ivar=0,info.nvars-1 do begin
78    var=ncdf_varinq(id,ivar)
79    vinfo[ivar].name=var.name
80    vinfo[ivar].datatype=var.datatype
81    vinfo[ivar].ndims=var.ndims
82    vinfo[ivar].natts=var.natts
83    vinfo[ivar].dim=var.dim
84endfor
85
86
87;; global attributes
88
89if info.ngatts gt 0 then begin
90    gatts=replicate({name:'', $
91                     datatype:'', $
92                     length:0L, $
93                     values:ptr_new()}, $
94                    info.ngatts)
95    for iatt=0,info.ngatts-1 do begin
96        name=ncdf_attname(id,iatt,/global)
97        gatts[iatt].name=name
98        att=ncdf_attinq(id,name,/global)
99        gatts[iatt].length=att.length
100        gatts[iatt].datatype=att.datatype
101        ncdf_attget,id,name,vals,/global
102        gatts[iatt].values=ptr_new(vals)
103    endfor
104endif else begin
105    ;; arbitary scalar value
106    ;; an empty list would be sensible but IDL doesn't support it
107    gatts=-1
108endelse
109
110
111
112;; variable attributes
113
114vatts=replicate(ptr_new(),info.nvars)
115for ivar=0,info.nvars-1 do begin
116    if vinfo[ivar].natts gt 0 then begin
117        vatts[ivar]=ptr_new(replicate({name:'', $
118                                       datatype:'', $
119                                       length:0L, $
120                                       values:ptr_new()}, $
121                                      vinfo[ivar].natts))
122        for iatt=0,vinfo[ivar].natts-1 do begin
123            name=ncdf_attname(id,ivar,iatt)
124            (*vatts[ivar])[iatt].name=name
125            att=ncdf_attinq(id,ivar,name)
126            (*vatts[ivar])[iatt].length=att.length
127            (*vatts[ivar])[iatt].datatype=att.datatype
128            ncdf_attget,id,ivar,name,vals
129            (*vatts[ivar])[iatt].values=ptr_new(vals)
130        endfor
131    endif else begin
132        vatts[ivar]=ptr_new(-1)
133        ;; Pointer to arbitrary scalar -- analogous to case of lack of
134        ;; global attributes above.  We could put a <NullPointer> here
135        ;; instead, but try to be friendlier to code that might try
136        ;; to dereference it.
137    endelse
138endfor
139
140
141;; data
142
143data=replicate(ptr_new(),info.nvars)
144for ivar=0,info.nvars-1 do begin
145    ncdf_varget,id,ivar,val
146    data[ivar]=ptr_new(val)
147endfor
148
149
150end
Note: See TracBrowser for help on using the repository browser.