[67] | 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 | function _str,string |
---|
| 18 | return,strcompress(string,/remove_all) |
---|
| 19 | end |
---|
| 20 | |
---|
| 21 | ;------------------------------------------------------ |
---|
| 22 | function ncdf_quickwrite_typename,num,name |
---|
| 23 | on_error,2 |
---|
| 24 | ;; |
---|
| 25 | ;; translate type number returned from "size" function into name usable by |
---|
| 26 | ;; ncdf routines |
---|
| 27 | ;; |
---|
| 28 | ;; if not valid type, throw an error, and use "name" in informational |
---|
| 29 | ;; message if set, |
---|
| 30 | ;; |
---|
| 31 | |
---|
| 32 | case num of |
---|
| 33 | |
---|
| 34 | ;; usable types |
---|
| 35 | 1: type='byte' |
---|
| 36 | 2: type='short' |
---|
| 37 | 3: type='long' |
---|
| 38 | 4: type='float' |
---|
| 39 | 5: type='double' |
---|
| 40 | |
---|
| 41 | ;; other types: set to something appropriate. |
---|
| 42 | 7: type='char' ;; string |
---|
| 43 | 12: type='long' ;; unsigned |
---|
| 44 | 13: type='long' ;; unsigned long |
---|
| 45 | 14: type='float' ;; 64-bit integer |
---|
| 46 | 15: type='float' ;; 64-bit integer |
---|
| 47 | |
---|
| 48 | else: begin |
---|
| 49 | if num eq 0 then gripe='undefined' $ |
---|
| 50 | else gripe='not of valid type for a NetCDF file.' |
---|
| 51 | |
---|
| 52 | if n_params() eq 1 then name='Data item' |
---|
| 53 | |
---|
| 54 | message,name+' is '+gripe,/noname |
---|
| 55 | |
---|
| 56 | end |
---|
| 57 | |
---|
| 58 | endcase |
---|
| 59 | |
---|
| 60 | return,type |
---|
| 61 | |
---|
| 62 | end |
---|
| 63 | ;----------------------------------------------------- |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | pro ncdf_quickwrite_helper2,ncfilename,s,sname |
---|
| 67 | ;; |
---|
| 68 | on_error,2 |
---|
| 69 | compile_opt hidden |
---|
| 70 | |
---|
| 71 | ;; NB main structure is called "s" - we use it so much that anything longer |
---|
| 72 | ;; could get tedious... |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | ;; start with no commands - in fact "-1" is an error condition |
---|
| 76 | s.ncommands=-1 |
---|
| 77 | ;; free commands written by helper1 from heap |
---|
| 78 | ptr_free,s.commands |
---|
| 79 | |
---|
| 80 | dimsize=lonarr(s.ndim > 1) ;; (">1" stops error if all fields scalar) |
---|
| 81 | types=strarr(s.nvar) |
---|
| 82 | ;; |
---|
| 83 | ;; first of all, work out dimension sizes. |
---|
| 84 | ;; |
---|
| 85 | |
---|
| 86 | for ivar=0,s.nvar-1 do begin |
---|
| 87 | |
---|
| 88 | nvardim=s.nvardims[ivar] |
---|
| 89 | |
---|
| 90 | sizeinfo=*(s.varsizes[ivar]) |
---|
| 91 | |
---|
| 92 | ntype=sizeinfo[sizeinfo[0]+1] |
---|
| 93 | |
---|
| 94 | types[ivar]= $ |
---|
| 95 | ncdf_quickwrite_typename(ntype,'IDL expression "'+s.varnamesidl[ivar]+ $ |
---|
| 96 | '" (for NCDF variable "'+s.varnames[ivar]+')') |
---|
| 97 | |
---|
| 98 | if nvardim ne sizeinfo[0] then $ |
---|
| 99 | message,('NCDF variable "'+s.varnames[ivar]+'" is defined with '+ $ |
---|
| 100 | _str(s.nvardims[ivar])+' dimension(s), '+ $ |
---|
| 101 | 'but corresponding ' + $ |
---|
| 102 | 'IDL expression "'+s.varnamesidl[ivar]+'" has '+ $ |
---|
| 103 | _str(sizeinfo[0])+' dimension(s).'),/noname |
---|
| 104 | |
---|
| 105 | if nvardim ne 0 then begin ;; not scalar |
---|
| 106 | |
---|
| 107 | for ivardim=0,nvardim-1 do begin |
---|
| 108 | |
---|
| 109 | idim=(*(s.vardims[ivar]))[ivardim] |
---|
| 110 | wanted=sizeinfo[1+ivardim] |
---|
| 111 | previous=dimsize[idim] |
---|
| 112 | |
---|
| 113 | if previous ne 0 and previous ne wanted then $ |
---|
| 114 | message,('NCDF dimension "'+s.dimnames[idim]+ $ |
---|
| 115 | '" is multiply used, but with conflicting sizes: '+ $ |
---|
| 116 | _str(previous)+' and '+_str(wanted)), $ |
---|
| 117 | /noname |
---|
| 118 | |
---|
| 119 | dimsize[idim]=wanted |
---|
| 120 | |
---|
| 121 | endfor |
---|
| 122 | |
---|
| 123 | endif |
---|
| 124 | |
---|
| 125 | endfor |
---|
| 126 | |
---|
| 127 | ;; ---- make commands to write the file... ---- |
---|
| 128 | |
---|
| 129 | ;; to open the file |
---|
| 130 | if n_elements(ncfilename) eq 0 then ncfilename='!idl.nc' |
---|
| 131 | if strmid(ncfilename,0,1) eq '!' then begin |
---|
| 132 | ncfilename1=strmid(ncfilename,1) |
---|
| 133 | clobstr=',/clobber' |
---|
| 134 | endif else begin |
---|
| 135 | ncfilename1=ncfilename |
---|
| 136 | clobstr='' |
---|
| 137 | endelse |
---|
| 138 | commands=[sname+'.fileid=ncdf_create('''+ncfilename1+''''+clobstr+')'] |
---|
| 139 | |
---|
| 140 | ;; to do the dimensions |
---|
| 141 | for idim=0,s.ndim-1 do begin |
---|
| 142 | |
---|
| 143 | if idim eq s.dimunlim then sizestr='/unlimited' $ |
---|
| 144 | else sizestr=_str(dimsize[idim]) |
---|
| 145 | |
---|
| 146 | commands=[commands, $ |
---|
| 147 | sname+'.dimids['+_str(idim)+']=ncdf_dimdef('+sname+ $ |
---|
| 148 | '.fileid,'''+s.dimnames[idim]+''','+sizestr+')'] |
---|
| 149 | endfor |
---|
| 150 | |
---|
| 151 | ;; to do the variables |
---|
| 152 | for ivar=0,s.nvar-1 do begin |
---|
| 153 | |
---|
| 154 | if s.nvardims[ivar] eq 0 then dimstr='' $ |
---|
| 155 | else dimstr=','+sname+'.dimids[['+strjoin(_str(*(s.vardims[ivar])),',')+']]' |
---|
| 156 | |
---|
| 157 | commands=[commands, $ |
---|
| 158 | sname+'.varids['+_str(ivar)+']=ncdf_vardef('+sname+ $ |
---|
| 159 | '.fileid,'''+s.varnames[ivar]+''''+ $ |
---|
| 160 | dimstr+',/'+types[ivar]+')'] |
---|
| 161 | endfor |
---|
| 162 | |
---|
| 163 | ;; to do the global attributes |
---|
| 164 | |
---|
| 165 | if s.globattflag then begin |
---|
| 166 | |
---|
| 167 | tags=tag_names(*s.globatts) |
---|
| 168 | ntags=n_elements(tags) |
---|
| 169 | |
---|
| 170 | for itag=0,ntags-1 do begin |
---|
| 171 | sizeinfo=size((*s.globatts).(itag)) |
---|
| 172 | type=ncdf_quickwrite_typename(sizeinfo[sizeinfo[0]+1]) |
---|
| 173 | |
---|
| 174 | commands=[commands, $ |
---|
| 175 | ('ncdf_attput,'+sname+'.fileid,/global,'''+ $ |
---|
| 176 | strlowcase(tags[itag])+ $ |
---|
| 177 | ''','+s.globattnameidl+'.'+tags[itag]+',/'+type)] |
---|
| 178 | endfor |
---|
| 179 | |
---|
| 180 | endif |
---|
| 181 | |
---|
| 182 | ;; to do the variable attributes |
---|
| 183 | |
---|
| 184 | for ivar=0,s.nvar-1 do begin |
---|
| 185 | if s.varattflags[ivar] then begin |
---|
| 186 | |
---|
| 187 | tags=tag_names(*(s.varatts[ivar])) |
---|
| 188 | ntags=n_elements(tags) |
---|
| 189 | |
---|
| 190 | for itag=0,ntags-1 do begin |
---|
| 191 | sizeinfo=size((*(s.varatts[ivar])).(itag)) |
---|
| 192 | type=ncdf_quickwrite_typename(sizeinfo[sizeinfo[0]+1]) |
---|
| 193 | |
---|
| 194 | commands=[commands, $ |
---|
| 195 | ('ncdf_attput,'+sname+'.fileid,'+ $ |
---|
| 196 | sname+'.varids['+_str(ivar)+'],'''+ $ |
---|
| 197 | strlowcase(tags[itag])+''','+s.varattnamesidl[ivar]+'.'+ $ |
---|
| 198 | tags[itag]+',/'+type)] |
---|
| 199 | endfor |
---|
| 200 | endif |
---|
| 201 | endfor |
---|
| 202 | |
---|
| 203 | ;; to end the definition section |
---|
| 204 | commands=[commands,'ncdf_control,'+sname+'.fileid,/endef'] |
---|
| 205 | |
---|
| 206 | ;; to write the data |
---|
| 207 | for ivar=0,s.nvar-1 do begin |
---|
| 208 | commands=[commands, $ |
---|
| 209 | ('ncdf_varput,'+sname+'.fileid,'+sname+'.varids['+_str(ivar)+'],'+ $ |
---|
| 210 | s.varnamesidl[ivar]) ] |
---|
| 211 | endfor |
---|
| 212 | |
---|
| 213 | ;; close the file |
---|
| 214 | commands=[commands,'ncdf_close,'+sname+'.fileid'] |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | ;; make commands available to main level |
---|
| 218 | s.ncommands=n_elements(commands) |
---|
| 219 | s.commands=ptr_new(commands) |
---|
| 220 | |
---|
| 221 | end |
---|
| 222 | |
---|