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

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

miscellaneous modifications according to cerbere.lodyc.jussieu.fr: /usr/home/smasson/SAXO_RD/

  • Property svn:executable set to *
File size: 3.6 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
89gatts=replicate({name:'', $
90                 datatype:'', $
91                 length:0L, $
92                 values:ptr_new()}, $
93                info.ngatts)
94for iatt=0,info.ngatts-1 do begin
95    name=ncdf_attname(id,iatt,/global)
96    gatts[iatt].name=name
97    att=ncdf_attinq(id,name,/global)
98    gatts[iatt].length=att.length
99    gatts[iatt].datatype=att.datatype
100    ncdf_attget,id,name,vals,/global
101    gatts[iatt].values=ptr_new(vals)
102endfor
103
104
105
106;; variable attributes
107
108vatts=replicate(ptr_new(),info.nvars)
109for ivar=0,info.nvars-1 do begin
110    vatts[ivar]=ptr_new(replicate({name:'', $
111                                      datatype:'', $
112                                      length:0L, $
113                                      values:ptr_new()}, $
114                                      vinfo[ivar].natts))
115    for iatt=0,vinfo[ivar].natts-1 do begin
116        name=ncdf_attname(id,ivar,iatt)
117        (*vatts[ivar])[iatt].name=name
118        att=ncdf_attinq(id,ivar,name)
119        (*vatts[ivar])[iatt].length=att.length
120        (*vatts[ivar])[iatt].datatype=att.datatype
121        ncdf_attget,id,ivar,name,vals
122        (*vatts[ivar])[iatt].values=ptr_new(vals)
123    endfor
124endfor
125
126
127;; data
128
129data=replicate(ptr_new(),info.nvars)
130for ivar=0,info.nvars-1 do begin
131    ncdf_varget,id,ivar,val
132    data[ivar]=ptr_new(val)
133endfor
134
135
136end
Note: See TracBrowser for help on using the repository browser.