1 | pro ncdf_quickwrite_helper1,ncvarstring,ncdfstruct,structname |
---|
2 | ;; |
---|
3 | ;; Parses the variable string so as to create the main structure. |
---|
4 | ;; |
---|
5 | ;;------------------------------------------------------------ |
---|
6 | |
---|
7 | |
---|
8 | on_error,2 |
---|
9 | compile_opt hidden |
---|
10 | ncdfstruct={ncommands:-1} |
---|
11 | |
---|
12 | ;; split string, to extract IDL global attribute variable name |
---|
13 | bits=strsplit(ncvarstring,'@',/extract) |
---|
14 | case n_elements(bits) of |
---|
15 | 1: begin |
---|
16 | ;; no attributes |
---|
17 | globattflag=0B |
---|
18 | globattnameidl='' |
---|
19 | end |
---|
20 | 2: begin |
---|
21 | globattflag=1B |
---|
22 | globattnameidl=bits[1] |
---|
23 | end |
---|
24 | else: begin |
---|
25 | message,'Parse error: more than one "@" sign in '+ncvarstring, $ |
---|
26 | /noname |
---|
27 | end |
---|
28 | endcase |
---|
29 | allvarspec=bits[0] |
---|
30 | |
---|
31 | |
---|
32 | vars=strsplit(strcompress(allvarspec,/remove_all),';',/extract) |
---|
33 | nvar=n_elements(vars) |
---|
34 | |
---|
35 | varnames=strarr(nvar) |
---|
36 | varnamesidl=strarr(nvar) |
---|
37 | |
---|
38 | nvardims=intarr(nvar) |
---|
39 | vardims=ptrarr(nvar) |
---|
40 | |
---|
41 | varattflags=bytarr(nvar) |
---|
42 | varattnamesidl=strarr(nvar) |
---|
43 | |
---|
44 | |
---|
45 | ;; at start, no dimensions known |
---|
46 | ndim=0 |
---|
47 | dimnames='' |
---|
48 | dimunlim=-1 |
---|
49 | |
---|
50 | for ivar=0,nvar-1 do begin |
---|
51 | |
---|
52 | varandattspec=vars[ivar] |
---|
53 | |
---|
54 | ;; split into IDL attribute variable name and full variable specification |
---|
55 | bits=strsplit(varandattspec,':',/extract) |
---|
56 | case n_elements(bits) of |
---|
57 | 1: ;; no variable attributes |
---|
58 | 2: begin |
---|
59 | varattflags[ivar]=1B |
---|
60 | varattnamesidl[ivar]=bits[1] |
---|
61 | end |
---|
62 | else: begin |
---|
63 | message,'Parse error: more than one ":" sign in '+varandattspec, $ |
---|
64 | /noname |
---|
65 | end |
---|
66 | endcase |
---|
67 | fullvarspec=bits[0] |
---|
68 | |
---|
69 | |
---|
70 | ;; split full variable specification |
---|
71 | ;; into variable specification and IDL variable name |
---|
72 | ;; |
---|
73 | bits=strsplit(fullvarspec,'=',/extract) |
---|
74 | case n_elements(bits) of |
---|
75 | 1: varnameidl='' ;; fill this in later |
---|
76 | 2: varnameidl=bits[1] |
---|
77 | else: begin |
---|
78 | message,'Parse error: more than one "=" sign in '+fullvarspec, $ |
---|
79 | /noname |
---|
80 | end |
---|
81 | endcase |
---|
82 | |
---|
83 | varspec=bits[0] |
---|
84 | |
---|
85 | ;; split variable specification into name and dimension specification |
---|
86 | ;; |
---|
87 | bits=strsplit(varspec,'[',/extract) |
---|
88 | varname=bits[0] |
---|
89 | case n_elements(bits) of |
---|
90 | 1: begin |
---|
91 | ;; scalar |
---|
92 | nvardims[ivar]=0 |
---|
93 | end |
---|
94 | 2: begin |
---|
95 | dimspec=bits[1] |
---|
96 | ;; test for and strip trailing ']' |
---|
97 | len=strlen(dimspec) |
---|
98 | if strmid(dimspec,len-1,1) ne ']' then begin |
---|
99 | message,'Parse error: dimension specification "['+dimspec+ $ |
---|
100 | '" for variable "'+varname+'" should end with "]"', $ |
---|
101 | /noname |
---|
102 | endif |
---|
103 | dimspec=strmid(dimspec,0,len-1) |
---|
104 | |
---|
105 | if dimspec eq '' then begin |
---|
106 | ;; dimensions not specified - assume 1d array with |
---|
107 | ;; same name for dimension as for variable |
---|
108 | vardimnames=[varname] |
---|
109 | endif else if dimspec eq '*' then begin |
---|
110 | ;; dimensions not specified but "*" given - as above, |
---|
111 | ;; again assume same name for dimension as for variable, |
---|
112 | ;; but with * (parsed below as meaning UNLIMITED) |
---|
113 | vardimnames=['*'+varname] |
---|
114 | endif else begin |
---|
115 | vardimnames=strsplit(dimspec,',',/extract) |
---|
116 | endelse |
---|
117 | |
---|
118 | ;; now for each dimension name, see if it already exists, |
---|
119 | ;; and if not then add it as a new name |
---|
120 | |
---|
121 | nvardim=n_elements(vardimnames) |
---|
122 | nvardims[ivar]=nvardim |
---|
123 | |
---|
124 | thisvardims=intarr(nvardim) |
---|
125 | |
---|
126 | for i=0,nvardim-1 do begin |
---|
127 | |
---|
128 | dimname=vardimnames[i] |
---|
129 | |
---|
130 | ;; first see if dimname has leading "*" |
---|
131 | ;; if so, strip it, but record the fact that UNLIMITED |
---|
132 | ;; is wanted |
---|
133 | unlimited = (strmid(dimname,0,1) eq '*') |
---|
134 | if unlimited then dimname=strmid(dimname,1) |
---|
135 | |
---|
136 | if ndim gt 0 then begin |
---|
137 | match=where(dimnames eq dimname,nmatch) |
---|
138 | case nmatch of |
---|
139 | 0: begin |
---|
140 | ;; no match - append to array |
---|
141 | dimnames=[dimnames,dimname] |
---|
142 | vardim=ndim |
---|
143 | ndim=ndim+1 |
---|
144 | end |
---|
145 | 1: begin |
---|
146 | ;; match found - point to it |
---|
147 | vardim=match[0] |
---|
148 | end |
---|
149 | else: stop,'Duplicate match: BUG in NCDF_QUICK_HELPER1' |
---|
150 | endcase |
---|
151 | endif else begin |
---|
152 | ;; no dimensions known - this is the first |
---|
153 | ndim=1 |
---|
154 | dimnames=[dimname] |
---|
155 | vardim=0 ;; (for completeness) |
---|
156 | endelse |
---|
157 | |
---|
158 | if unlimited then begin |
---|
159 | if (dimunlim ge 0 $ |
---|
160 | and dimunlim ne vardim) then begin |
---|
161 | message,('NCDF dimensions "'+dimnames[dimunlim]+ $ |
---|
162 | '" and "'+dimnames[vardim]+ $ |
---|
163 | '" cannot both be of UNLIMITED size.'), $ |
---|
164 | /noname |
---|
165 | endif |
---|
166 | dimunlim=vardim |
---|
167 | endif |
---|
168 | |
---|
169 | thisvardims[i]=vardim |
---|
170 | |
---|
171 | endfor |
---|
172 | vardims[ivar]=ptr_new(thisvardims) |
---|
173 | end |
---|
174 | else: message,('Parse error: variable specification "'+varspec+ $ |
---|
175 | '" has stray "["'),/noname |
---|
176 | endcase |
---|
177 | |
---|
178 | if varnameidl eq '' then varnameidl=varname |
---|
179 | |
---|
180 | varnames[ivar]=varname |
---|
181 | varnamesidl[ivar]=varnameidl |
---|
182 | endfor |
---|
183 | |
---|
184 | ;; --------------------------------------------------- |
---|
185 | ; now construct some commands, which, when executed at the top level, will |
---|
186 | ; put IDL variable size information into the structure. |
---|
187 | |
---|
188 | commands=( structname+'.varsizes['+string(indgen(nvar))+ $ |
---|
189 | ']=ptr_new(size('+varnamesidl+'))' ) |
---|
190 | |
---|
191 | |
---|
192 | ; now some more commands, to tell the main level to copy the attributes |
---|
193 | ; into a heap location where the next helper routine will see them. |
---|
194 | |
---|
195 | if globattflag then $ |
---|
196 | commands=[commands,structname+'.globatts=ptr_new('+globattnameidl+')'] |
---|
197 | |
---|
198 | for ivar=0,nvar-1 do begin |
---|
199 | if varattflags(ivar) then begin |
---|
200 | commands=[commands, $ |
---|
201 | structname+'.varatts['+string(ivar)+ $ |
---|
202 | ']=ptr_new('+varattnamesidl[ivar]+')'] |
---|
203 | endif |
---|
204 | endfor |
---|
205 | |
---|
206 | ;; |
---|
207 | ;; second argument comes back with a structure which contains all the |
---|
208 | ;; information, and also some variables to be used by next helper routine. |
---|
209 | ;; |
---|
210 | ncdfstruct={ncommands: n_elements(commands), $ |
---|
211 | commands: ptr_new(commands) , $ |
---|
212 | nvar: nvar , $ |
---|
213 | varnames: varnames , $ |
---|
214 | varids: intarr(nvar) , $ |
---|
215 | nvardims: nvardims , $ |
---|
216 | vardims: vardims , $ |
---|
217 | varnamesidl: varnamesidl , $ |
---|
218 | varsizes: ptrarr(nvar) , $ |
---|
219 | varatts: ptrarr(1+nvar) , $ |
---|
220 | varattflags: varattflags , $ |
---|
221 | varattnamesidl: varattnamesidl , $ |
---|
222 | globatts: ptr_new() , $ |
---|
223 | globattflag: globattflag , $ |
---|
224 | globattnameidl: globattnameidl , $ |
---|
225 | ndim: ndim , $ |
---|
226 | dimnames: dimnames , $ |
---|
227 | dimids: intarr(ndim>1) , $ |
---|
228 | dimunlim: dimunlim , $ |
---|
229 | fileid: 0} |
---|
230 | |
---|
231 | end |
---|