source: trunk/SRC/ReadWrite/idl-NetCDF/ncdf_quickwrite/ncdf_quickwrite_helper2.pro @ 163

Last change on this file since 163 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
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
9compile_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;-
43function _str,string
44return,strcompress(string,/remove_all)
45end
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;-
80function ncdf_quickwrite_typename,num,name
81on_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
90case 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   
116endcase
117
118return,type
119
120end
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
156pro ncdf_quickwrite_helper2,ncfilename,s,sname
157;;
158on_error,2
159compile_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
166s.ncommands=-1
167;; free commands written by helper1 from heap
168ptr_free,s.commands
169
170dimsize=lonarr(s.ndim > 1) ;; (">1" stops error if all fields scalar)
171types=strarr(s.nvar)
172;;
173;; first of all, work out dimension sizes.
174;;
175
176for 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       
215endfor
216
217;; ---- make commands to write the file... ----
218
219;; to open the file
220if n_elements(ncfilename) eq 0 then ncfilename='!idl.nc'
221if strmid(ncfilename,0,1) eq '!' then begin
222    ncfilename1=strmid(ncfilename,1)
223    clobstr=',/clobber'
224endif else begin
225    ncfilename1=ncfilename
226    clobstr=''
227endelse
228commands=[sname+'.fileid=ncdf_create('''+ncfilename1+''''+clobstr+')']
229
230;; to do the dimensions
231for 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+')']
239endfor
240
241;; to do the variables
242for 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]+')']
251endfor
252
253;; to do the global attributes
254
255if 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   
270endif     
271
272;; to do the variable attributes
273
274for 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     
291endfor
292
293;; to end the definition section
294commands=[commands,'ncdf_control,'+sname+'.fileid,/endef']
295
296;; to write the data
297for ivar=0,s.nvar-1 do begin
298    commands=[commands, $
299              ('ncdf_varput,'+sname+'.fileid,'+sname+'.varids['+_str(ivar)+'],'+ $
300               s.varnamesidl[ivar]) ]
301endfor
302
303;; close the file
304commands=[commands,'ncdf_close,'+sname+'.fileid']
305
306
307;; make commands available to main level
308s.ncommands=n_elements(commands)
309s.commands=ptr_new(commands)
310
311end
312
Note: See TracBrowser for help on using the repository browser.