1 | ;; |
---|
2 | ;; HELPER2 |
---|
3 | ;; Constructs the commands which are actually needed to write the NetCDF file. |
---|
4 | ;; |
---|
5 | ;;----------------------------------------------------------------------------- |
---|
6 | |
---|
7 | ; this file contains: STR, ncdf_quickwrite_typename, ncdf_quickwrite_helper2 |
---|
8 | |
---|
9 | compile_opt hidden |
---|
10 | |
---|
11 | ;------------------------------------------------------ |
---|
12 | ;; _STR - like STRING, but with no whitespace. |
---|
13 | ;; |
---|
14 | ;; we use this function enough to give it a short name, but the underscore |
---|
15 | ;; is to make it unlikely to conflict with a user's function. |
---|
16 | ;+ |
---|
17 | ; @file_comments |
---|
18 | ; |
---|
19 | ; |
---|
20 | ; @categories |
---|
21 | ; |
---|
22 | ; |
---|
23 | ; @param STRING |
---|
24 | ; String to be split. Contains text after in, out token on output. |
---|
25 | ; |
---|
26 | ; |
---|
27 | ; @returns |
---|
28 | ; |
---|
29 | ; |
---|
30 | ; @restrictions |
---|
31 | ; |
---|
32 | ; |
---|
33 | ; @examples |
---|
34 | ; |
---|
35 | ; |
---|
36 | ; @history |
---|
37 | ; |
---|
38 | ; |
---|
39 | ; @version |
---|
40 | ; $Id$ |
---|
41 | ; |
---|
42 | ;- |
---|
43 | function _str,string |
---|
44 | return,strcompress(string,/remove_all) |
---|
45 | end |
---|
46 | |
---|
47 | ;------------------------------------------------------ |
---|
48 | ;+ |
---|
49 | ; @file_comments |
---|
50 | ; |
---|
51 | ; |
---|
52 | ; @categories |
---|
53 | ; |
---|
54 | ; |
---|
55 | ; @param NUM |
---|
56 | ; |
---|
57 | ; |
---|
58 | ; @param NAME{type=string} |
---|
59 | ; It is a string giving the name of the file to be opened. If NAME does not contain |
---|
60 | ; the separating character of directories ('/' under unix for example), the file |
---|
61 | ; will be looked for in the current directory. |
---|
62 | ; |
---|
63 | ; |
---|
64 | ; @returns |
---|
65 | ; |
---|
66 | ; |
---|
67 | ; @restrictions |
---|
68 | ; |
---|
69 | ; |
---|
70 | ; @examples |
---|
71 | ; |
---|
72 | ; |
---|
73 | ; @history |
---|
74 | ; |
---|
75 | ; |
---|
76 | ; @version |
---|
77 | ; $Id$ |
---|
78 | ; |
---|
79 | ;- |
---|
80 | function ncdf_quickwrite_typename,num,name |
---|
81 | on_error,2 |
---|
82 | ;; |
---|
83 | ;; translate type number returned from "size" function into name usable by |
---|
84 | ;; ncdf routines |
---|
85 | ;; |
---|
86 | ;; if not valid type, throw an error, and use "name" in informational |
---|
87 | ;; message if set, |
---|
88 | ;; |
---|
89 | |
---|
90 | case num of |
---|
91 | |
---|
92 | ;; usable types |
---|
93 | 1: type='byte' |
---|
94 | 2: type='short' |
---|
95 | 3: type='long' |
---|
96 | 4: type='float' |
---|
97 | 5: type='double' |
---|
98 | |
---|
99 | ;; other types: set to something appropriate. |
---|
100 | 7: type='char' ;; string |
---|
101 | 12: type='long' ;; unsigned |
---|
102 | 13: type='long' ;; unsigned long |
---|
103 | 14: type='float' ;; 64-bit integer |
---|
104 | 15: type='float' ;; 64-bit integer |
---|
105 | |
---|
106 | else: begin |
---|
107 | if num eq 0 then gripe='undefined' $ |
---|
108 | else gripe='not of valid type for a NetCDF file.' |
---|
109 | |
---|
110 | if n_params() eq 1 then name='Data item' |
---|
111 | |
---|
112 | message,name+' is '+gripe,/noname |
---|
113 | |
---|
114 | end |
---|
115 | |
---|
116 | endcase |
---|
117 | |
---|
118 | return,type |
---|
119 | |
---|
120 | end |
---|
121 | ;----------------------------------------------------- |
---|
122 | |
---|
123 | ;+ |
---|
124 | ; @file_comments |
---|
125 | ; |
---|
126 | ; |
---|
127 | ; @categories |
---|
128 | ; |
---|
129 | ; |
---|
130 | ; @param NCFILENAME |
---|
131 | ; |
---|
132 | ; |
---|
133 | ; @param S |
---|
134 | ; The string to be searched |
---|
135 | ; |
---|
136 | ; @param SNAME |
---|
137 | ; |
---|
138 | ; |
---|
139 | ; @returns |
---|
140 | ; |
---|
141 | ; |
---|
142 | ; @restrictions |
---|
143 | ; |
---|
144 | ; |
---|
145 | ; @examples |
---|
146 | ; |
---|
147 | ; |
---|
148 | ; @history |
---|
149 | ; |
---|
150 | ; |
---|
151 | ; @version |
---|
152 | ; $Id$ |
---|
153 | ; |
---|
154 | ;- |
---|
155 | |
---|
156 | pro ncdf_quickwrite_helper2,ncfilename,s,sname |
---|
157 | ;; |
---|
158 | on_error,2 |
---|
159 | compile_opt hidden |
---|
160 | |
---|
161 | ;; NB main structure is called "s" - we use it so much that anything longer |
---|
162 | ;; could get tedious... |
---|
163 | |
---|
164 | |
---|
165 | ;; start with no commands - in fact "-1" is an error condition |
---|
166 | s.ncommands=-1 |
---|
167 | ;; free commands written by helper1 from heap |
---|
168 | ptr_free,s.commands |
---|
169 | |
---|
170 | dimsize=lonarr(s.ndim > 1) ;; (">1" stops error if all fields scalar) |
---|
171 | types=strarr(s.nvar) |
---|
172 | ;; |
---|
173 | ;; first of all, work out dimension sizes. |
---|
174 | ;; |
---|
175 | |
---|
176 | for ivar=0,s.nvar-1 do begin |
---|
177 | |
---|
178 | nvardim=s.nvardims[ivar] |
---|
179 | |
---|
180 | sizeinfo=*(s.varsizes[ivar]) |
---|
181 | |
---|
182 | ntype=sizeinfo[sizeinfo[0]+1] |
---|
183 | |
---|
184 | types[ivar]= $ |
---|
185 | ncdf_quickwrite_typename(ntype,'IDL expression "'+s.varnamesidl[ivar]+ $ |
---|
186 | '" (for NCDF variable "'+s.varnames[ivar]+')') |
---|
187 | |
---|
188 | if nvardim ne sizeinfo[0] then $ |
---|
189 | message,('NCDF variable "'+s.varnames[ivar]+'" is defined with '+ $ |
---|
190 | _str(s.nvardims[ivar])+' dimension(s), '+ $ |
---|
191 | 'but corresponding ' + $ |
---|
192 | 'IDL expression "'+s.varnamesidl[ivar]+'" has '+ $ |
---|
193 | _str(sizeinfo[0])+' dimension(s).'),/noname |
---|
194 | |
---|
195 | if nvardim ne 0 then begin ;; not scalar |
---|
196 | |
---|
197 | for ivardim=0,nvardim-1 do begin |
---|
198 | |
---|
199 | idim=(*(s.vardims[ivar]))[ivardim] |
---|
200 | wanted=sizeinfo[1+ivardim] |
---|
201 | previous=dimsize[idim] |
---|
202 | |
---|
203 | if previous ne 0 and previous ne wanted then $ |
---|
204 | message,('NCDF dimension "'+s.dimnames[idim]+ $ |
---|
205 | '" is multiply used, but with conflicting sizes: '+ $ |
---|
206 | _str(previous)+' and '+_str(wanted)), $ |
---|
207 | /noname |
---|
208 | |
---|
209 | dimsize[idim]=wanted |
---|
210 | |
---|
211 | endfor |
---|
212 | |
---|
213 | endif |
---|
214 | |
---|
215 | endfor |
---|
216 | |
---|
217 | ;; ---- make commands to write the file... ---- |
---|
218 | |
---|
219 | ;; to open the file |
---|
220 | if n_elements(ncfilename) eq 0 then ncfilename='!idl.nc' |
---|
221 | if strmid(ncfilename,0,1) eq '!' then begin |
---|
222 | ncfilename1=strmid(ncfilename,1) |
---|
223 | clobstr=',/clobber' |
---|
224 | endif else begin |
---|
225 | ncfilename1=ncfilename |
---|
226 | clobstr='' |
---|
227 | endelse |
---|
228 | commands=[sname+'.fileid=ncdf_create('''+ncfilename1+''''+clobstr+')'] |
---|
229 | |
---|
230 | ;; to do the dimensions |
---|
231 | for idim=0,s.ndim-1 do begin |
---|
232 | |
---|
233 | if idim eq s.dimunlim then sizestr='/unlimited' $ |
---|
234 | else sizestr=_str(dimsize[idim]) |
---|
235 | |
---|
236 | commands=[commands, $ |
---|
237 | sname+'.dimids['+_str(idim)+']=ncdf_dimdef('+sname+ $ |
---|
238 | '.fileid,'''+s.dimnames[idim]+''','+sizestr+')'] |
---|
239 | endfor |
---|
240 | |
---|
241 | ;; to do the variables |
---|
242 | for ivar=0,s.nvar-1 do begin |
---|
243 | |
---|
244 | if s.nvardims[ivar] eq 0 then dimstr='' $ |
---|
245 | else dimstr=','+sname+'.dimids[['+strjoin(_str(*(s.vardims[ivar])),',')+']]' |
---|
246 | |
---|
247 | commands=[commands, $ |
---|
248 | sname+'.varids['+_str(ivar)+']=ncdf_vardef('+sname+ $ |
---|
249 | '.fileid,'''+s.varnames[ivar]+''''+ $ |
---|
250 | dimstr+',/'+types[ivar]+')'] |
---|
251 | endfor |
---|
252 | |
---|
253 | ;; to do the global attributes |
---|
254 | |
---|
255 | if s.globattflag then begin |
---|
256 | |
---|
257 | tags=tag_names(*s.globatts) |
---|
258 | ntags=n_elements(tags) |
---|
259 | |
---|
260 | for itag=0,ntags-1 do begin |
---|
261 | sizeinfo=size((*s.globatts).(itag)) |
---|
262 | type=ncdf_quickwrite_typename(sizeinfo[sizeinfo[0]+1]) |
---|
263 | |
---|
264 | commands=[commands, $ |
---|
265 | ('ncdf_attput,'+sname+'.fileid,/global,'''+ $ |
---|
266 | strlowcase(tags[itag])+ $ |
---|
267 | ''','+s.globattnameidl+'.'+tags[itag]+',/'+type)] |
---|
268 | endfor |
---|
269 | |
---|
270 | endif |
---|
271 | |
---|
272 | ;; to do the variable attributes |
---|
273 | |
---|
274 | for ivar=0,s.nvar-1 do begin |
---|
275 | if s.varattflags[ivar] then begin |
---|
276 | |
---|
277 | tags=tag_names(*(s.varatts[ivar])) |
---|
278 | ntags=n_elements(tags) |
---|
279 | |
---|
280 | for itag=0,ntags-1 do begin |
---|
281 | sizeinfo=size((*(s.varatts[ivar])).(itag)) |
---|
282 | type=ncdf_quickwrite_typename(sizeinfo[sizeinfo[0]+1]) |
---|
283 | |
---|
284 | commands=[commands, $ |
---|
285 | ('ncdf_attput,'+sname+'.fileid,'+ $ |
---|
286 | sname+'.varids['+_str(ivar)+'],'''+ $ |
---|
287 | strlowcase(tags[itag])+''','+s.varattnamesidl[ivar]+'.'+ $ |
---|
288 | tags[itag]+',/'+type)] |
---|
289 | endfor |
---|
290 | endif |
---|
291 | endfor |
---|
292 | |
---|
293 | ;; to end the definition section |
---|
294 | commands=[commands,'ncdf_control,'+sname+'.fileid,/endef'] |
---|
295 | |
---|
296 | ;; to write the data |
---|
297 | for ivar=0,s.nvar-1 do begin |
---|
298 | commands=[commands, $ |
---|
299 | ('ncdf_varput,'+sname+'.fileid,'+sname+'.varids['+_str(ivar)+'],'+ $ |
---|
300 | s.varnamesidl[ivar]) ] |
---|
301 | endfor |
---|
302 | |
---|
303 | ;; close the file |
---|
304 | commands=[commands,'ncdf_close,'+sname+'.fileid'] |
---|
305 | |
---|
306 | |
---|
307 | ;; make commands available to main level |
---|
308 | s.ncommands=n_elements(commands) |
---|
309 | s.commands=ptr_new(commands) |
---|
310 | |
---|
311 | end |
---|
312 | |
---|