source: trunk/SRC/ReadWrite/idl-NetCDF/ncdf_quickwrite/ncdf_quickwrite_helper1.pro

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

header improvements + xxx doc

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