1 | ;--------------------------------------------------------------------------------! |
---|
2 | ; This file is part of PALM. |
---|
3 | ; |
---|
4 | ; PALM is free software: you can redistribute it and/or modify it under the terms |
---|
5 | ; of the GNU General Public License as published by the Free Software Foundation, |
---|
6 | ; either version 3 of the License, or (at your option) any later version. |
---|
7 | ; |
---|
8 | ; PALM is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
9 | ; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
---|
10 | ; A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
---|
11 | ; |
---|
12 | ; You should have received a copy of the GNU General Public License along with |
---|
13 | ; PALM. If not, see <http://www.gnu.org/licenses/>. |
---|
14 | ; |
---|
15 | ; Copyright 1997-2012 Leibniz University Hannover |
---|
16 | ;------------------------------------------------------------------------------! |
---|
17 | ; |
---|
18 | ; Current revisions: |
---|
19 | ; ----------------- |
---|
20 | ; |
---|
21 | ; Former revisions: |
---|
22 | ; ----------------- |
---|
23 | ; $Id: nc2vdf.ncl 1047 2012-11-09 15:32:58Z hoffmann $ |
---|
24 | ; |
---|
25 | ; 1046 2012-11-09 14:38:45Z maronga |
---|
26 | ; Initial revision |
---|
27 | ; |
---|
28 | ; Description: |
---|
29 | ; ------------ |
---|
30 | ; This NCL script coverts PALM NetCDF data to vdf, VAPOR's own data format. In |
---|
31 | ; order to run this script, NCL version 5.2.0 or higher is required. |
---|
32 | ; Default setting will be loaded from .nc2vdf.config. |
---|
33 | ; Note that on HLRN, vapor is only available on hicegate2, bicegate2 and the UV |
---|
34 | ; sytem. |
---|
35 | ;------------------------------------------------------------------------------! |
---|
36 | |
---|
37 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" |
---|
38 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" |
---|
39 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" |
---|
40 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl" |
---|
41 | |
---|
42 | outputfile = "default.vdf" |
---|
43 | |
---|
44 | if(isfilepresent(inputfile)) then |
---|
45 | print("*** Loading " + inputfile) |
---|
46 | loadscript(inputfile) |
---|
47 | else |
---|
48 | print("+++ Input file does not exist. Exiting...") |
---|
49 | exit |
---|
50 | end if |
---|
51 | |
---|
52 | |
---|
53 | begin |
---|
54 | |
---|
55 | ; print loaded data from input file |
---|
56 | print("*** loading " + dimsizes(files) + ":") |
---|
57 | do i=0,dimsizes(files)-1 |
---|
58 | print(" " + i + ") " + files(i)) |
---|
59 | end do |
---|
60 | |
---|
61 | print("*** will create dataset " + outputfile) |
---|
62 | |
---|
63 | |
---|
64 | workpath = systemfunc("echo $PWD") + "/" |
---|
65 | path = "" |
---|
66 | |
---|
67 | |
---|
68 | ; hannes stuff below ;-) |
---|
69 | |
---|
70 | ;****************************************************** |
---|
71 | |
---|
72 | x_min = new(1,double) |
---|
73 | x_max = new(1,double) |
---|
74 | x_min = 99.0 |
---|
75 | x_max = -99.0 |
---|
76 | y_min = new(1,double) |
---|
77 | y_max = new(1,double) |
---|
78 | y_min = 99.0 |
---|
79 | y_max = -99.0 |
---|
80 | z_min = new(1,double) |
---|
81 | z_max = new(1,double) |
---|
82 | z_min = 99.0 |
---|
83 | z_max = -99.0 |
---|
84 | |
---|
85 | vars3d = "" |
---|
86 | |
---|
87 | |
---|
88 | ;print("You may need to run 'vapor-setup.sh' in order to set all enviroment variables.") |
---|
89 | ;/muksoft/packages/vapor/2.1.0/bin/vapor-setup.sh |
---|
90 | |
---|
91 | |
---|
92 | ;**** Get names of variables wanted to appear in the VDF file, then get their dimensions with min/max x,y,z |
---|
93 | print(" ") |
---|
94 | print("Choose the *.nc files containing 3D data by typing their indices (seperated by ','). Start with the first files of the timeseries.") |
---|
95 | print(" ") |
---|
96 | files_avail = systemfunc("cd "+workpath+"; ls *.nc") |
---|
97 | print(" "+files_avail) |
---|
98 | |
---|
99 | |
---|
100 | files_in_ary = toint(str_split(systemfunc("read file; echo $file"), ",")) |
---|
101 | nofiles = dimsizes(files_in_ary) |
---|
102 | file_in = new(nofiles,string) |
---|
103 | |
---|
104 | do i=0,nofiles-1 |
---|
105 | file_in(i) = files_avail(files_in_ary(i)) |
---|
106 | end do |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | if (nofiles .ge. 2) then |
---|
114 | nvars = 1 |
---|
115 | varString = new((/nofiles,nvars/),string) |
---|
116 | dimNames = new((/nofiles,nvars,4/),string) |
---|
117 | end if |
---|
118 | |
---|
119 | |
---|
120 | do i=0,nofiles-1 |
---|
121 | f = addfile(workpath+file_in(i),"r") |
---|
122 | |
---|
123 | varNames = getfilevarnames(f) |
---|
124 | print("Loading file...") |
---|
125 | |
---|
126 | |
---|
127 | print(" ") |
---|
128 | print("The following variables were found in "+file_in(i)+":") |
---|
129 | print(" ") |
---|
130 | print(" "+varNames) |
---|
131 | print(" ") |
---|
132 | print("Type the index of the VARIABLES (not dimensions) you want to visualize in Vapor (seperated by ',')") |
---|
133 | |
---|
134 | vars = toint(str_split(systemfunc("read vars; echo $vars"), ",")) |
---|
135 | |
---|
136 | |
---|
137 | nvars = dimsizes(vars) |
---|
138 | |
---|
139 | if (nofiles .eq. 1) then |
---|
140 | varString = new((/nofiles,nvars/),string) |
---|
141 | dimNames = new((/nofiles,nvars,4/),string) |
---|
142 | end if |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | do n=0,nvars-1 |
---|
147 | |
---|
148 | varString(i,n) = varNames(vars(n)) |
---|
149 | |
---|
150 | if (vars3d .ne. "") then |
---|
151 | vars3d = vars3d + ":" + varNames(vars(n)) |
---|
152 | else |
---|
153 | vars3d = varNames(vars(n)) |
---|
154 | end if |
---|
155 | |
---|
156 | |
---|
157 | dims = getfilevardimsizes(f, varString(i,n)) |
---|
158 | dimNames(i,n,:) = getfilevardims(f, varString(i,n)) |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 | time = f->$dimNames(i,n,0)$ |
---|
163 | t_max = max(time) |
---|
164 | z = f->$dimNames(i,n,1)$ |
---|
165 | y = f->$dimNames(i,n,2)$ |
---|
166 | x = f->$dimNames(i,n,3)$ |
---|
167 | |
---|
168 | if (z_min .ge. min(z)) then |
---|
169 | z_min = min(z) |
---|
170 | end if |
---|
171 | |
---|
172 | if (z_max .le. max(z)) then |
---|
173 | z_max = max(z) |
---|
174 | end if |
---|
175 | |
---|
176 | if (y_min .ge. min(y)) then |
---|
177 | y_min = min(y) |
---|
178 | end if |
---|
179 | |
---|
180 | if (y_max .le. max(y)) then |
---|
181 | y_max = max(y) |
---|
182 | end if |
---|
183 | |
---|
184 | if (x_min .ge. min(x)) then |
---|
185 | x_min = min(x) |
---|
186 | end if |
---|
187 | |
---|
188 | if (x_max .le. max(x)) then |
---|
189 | x_max = max(x) |
---|
190 | end if |
---|
191 | |
---|
192 | delete(z) |
---|
193 | delete(y) |
---|
194 | delete(x) |
---|
195 | end do |
---|
196 | |
---|
197 | end do |
---|
198 | |
---|
199 | print(varString) |
---|
200 | |
---|
201 | delete(files_avail) |
---|
202 | |
---|
203 | |
---|
204 | print(" ") |
---|
205 | periodic = systemfunc("read -p 'Periodic lateral boundaries? (y/n) ' RESP; echo $RESP") |
---|
206 | if (periodic .eq. "y") then |
---|
207 | px = 1 |
---|
208 | py = 1 |
---|
209 | else |
---|
210 | px = 0 |
---|
211 | py = 0 |
---|
212 | end if |
---|
213 | print(" ") |
---|
214 | print(" ") |
---|
215 | |
---|
216 | grid = systemfunc("read -p 'Is grid stretching used? (y/n) YES will cut of heights above dz_stretch_level? ' RESP; echo $RESP") |
---|
217 | if (grid .eq. "y") then |
---|
218 | stretch_level = stringtointeger(systemfunc("read -p 'Please type height of dz_stretch_level (in meters) ' RESP; echo $RESP")) |
---|
219 | z_max = stretch_level ;**** limit z_max |
---|
220 | end if |
---|
221 | |
---|
222 | print(" ") |
---|
223 | |
---|
224 | ;******************************************************************************* |
---|
225 | |
---|
226 | |
---|
227 | |
---|
228 | |
---|
229 | if (any(varNames .EQ. "zwwi")) then |
---|
230 | var2d = " -vars2dxy HGT:topo" |
---|
231 | topo = True |
---|
232 | print(" ") |
---|
233 | print("Found topography - will add 'zwwi' (w-grid) topography to *.vdf file.") |
---|
234 | print(" ") |
---|
235 | else |
---|
236 | var2d = " " |
---|
237 | topo = False |
---|
238 | end if |
---|
239 | |
---|
240 | print(" ") |
---|
241 | |
---|
242 | print(dims(0)+" time steps found in "+file_in) |
---|
243 | ts = systemfunc("read -p 'Include all time steps in *.vdf file? (y/n) ' RESP; echo $RESP") |
---|
244 | |
---|
245 | if (ts .eq. "y") then |
---|
246 | t_start = 0 |
---|
247 | t_end = dimsizes(time)-1 |
---|
248 | else |
---|
249 | t_start = stringtointeger(systemfunc("read -p 'Type index of first time step: ' ts; echo $ts")) |
---|
250 | t_end = stringtointeger(systemfunc("read -p 'Type index of last time step: ' ts; echo $ts")) |
---|
251 | end if |
---|
252 | |
---|
253 | print(" ") |
---|
254 | askreflevel = systemfunc("read -p 'Specify a maximum refinement level? (y/n) No will set the default value. ' RESP; echo $RESP") |
---|
255 | if (askreflevel .eq. "y") then |
---|
256 | reflevel = systemfunc("read -p 'Please type maximum refinement level ' RESP; echo $RESP") |
---|
257 | reflevel_str = "-level "+ reflevel |
---|
258 | else |
---|
259 | reflevel_str = "" |
---|
260 | end if |
---|
261 | print(" ") |
---|
262 | |
---|
263 | |
---|
264 | ;******************************************************************************* |
---|
265 | ;******************************************************************************* |
---|
266 | ;******************************************************************************* |
---|
267 | print(" ") |
---|
268 | print("Creating *.vdf file") |
---|
269 | print(" ") |
---|
270 | ;vars3d = str_join(varString,":") |
---|
271 | |
---|
272 | a = systemfunc(path+"vdfcreate -dimension "+dims(3)+"x"+dims(2)+"x"+dims(1)+" -numts " + (t_end - t_start + 1) + var2d + " "+reflevel_str+" -vars3d "+vars3d+" -extents "+x_min+":"+y_min+":"+z_min+":"+x_max+":"+y_max+":"+z_max+" -periodic "+px+":"+py+":0 "+workpath+outputfile) |
---|
273 | delete(a) |
---|
274 | print("Done.") |
---|
275 | |
---|
276 | |
---|
277 | ;******************************************************************************* |
---|
278 | print("Populating *.vdf file") |
---|
279 | |
---|
280 | do i=0,nofiles-1 |
---|
281 | do n=0,nvars-1 |
---|
282 | dimnamesstring = str_join(dimNames(i,n,::-1),":") |
---|
283 | print(" ") |
---|
284 | print("Variable: "+varString(i,n)+", number "+(n+1+i)+" of "+(nvars+nofiles-1) ) |
---|
285 | print(" ") |
---|
286 | do t=t_start,t_end |
---|
287 | print("Time step "+(t-t_start+1)+" of "+(t_end-t_start+1) ) |
---|
288 | a = systemfunc(path+"ncdf2vdf -dimnames "+dimnamesstring+" "+reflevel_str+" -varname "+varString(i,n)+" -ncdfvar "+varString(i,n)+" -cnstnames time -cnstvals "+t+" -ts "+t+" "+workpath+outputfile+" "+workpath+file_in(i)) |
---|
289 | delete(a) |
---|
290 | end do |
---|
291 | end do |
---|
292 | end do |
---|
293 | |
---|
294 | if topo then |
---|
295 | print(" ") |
---|
296 | print("Adding topography (1)...") |
---|
297 | print(" ") |
---|
298 | do t=t_start,t_end |
---|
299 | print("Time step "+(t-t_start+1)+" of "+(t_end-t_start+1) ) |
---|
300 | a = systemfunc(path+"ncdf2vdf -dimnames x:y "+reflevel_str+" -varname HGT -ncdfvar zwwi -cnstnames time -cnstvals "+t+" -ts "+t+" "+workpath+outputfile+" "+workpath+file_in(0)) |
---|
301 | delete(a) |
---|
302 | end do |
---|
303 | print(" ") |
---|
304 | print("Adding topography (2)...") |
---|
305 | print(" ") |
---|
306 | do t=t_start,t_end |
---|
307 | print("Time step "+(t-t_start+1)+" of "+(t_end-t_start+1) ) |
---|
308 | a = systemfunc(path+"ncdf2vdf -dimnames x:y "+reflevel_str+" -varname topo -ncdfvar zwwi -cnstnames time -cnstvals "+t+" -ts "+t+" "+workpath+outputfile+" "+workpath+file_in(0)) |
---|
309 | delete(a) |
---|
310 | end do |
---|
311 | end if |
---|
312 | |
---|
313 | |
---|
314 | ;******************************************************************************* |
---|
315 | |
---|
316 | ;**** Read more files and add them to VDF/VDC |
---|
317 | addmore = "y" |
---|
318 | do while (addmore .eq. "y") |
---|
319 | |
---|
320 | addmore = systemfunc("read -p 'Add time steps or 2D cross sections from additional *.nc files? (2d/time/n) ' RESP; echo $RESP") |
---|
321 | |
---|
322 | ;**** Add time steps from additional 3D files |
---|
323 | if (addmore .eq. "time") then |
---|
324 | |
---|
325 | delete(f) |
---|
326 | |
---|
327 | do i=0,nofiles-1 |
---|
328 | |
---|
329 | print(" ") |
---|
330 | print("Choose the *.nc file that contains 3D data for "+varString(i,0)+" by typing itÂŽs index.") |
---|
331 | print(" ") |
---|
332 | files_avail = systemfunc("cd "+workpath+"; ls *.nc") |
---|
333 | print(" "+files_avail) |
---|
334 | print(" ") |
---|
335 | file_in(i) = files_avail(toint(systemfunc("read file; echo $file"))) |
---|
336 | |
---|
337 | end do |
---|
338 | |
---|
339 | f = addfile(workpath+file_in(0),"r") |
---|
340 | |
---|
341 | time = f->time |
---|
342 | |
---|
343 | a = systemfunc(path+"vdfedit -addts "+dimsizes(time)+" "+workpath+outputfile) |
---|
344 | delete(a) |
---|
345 | |
---|
346 | do i=0,nofiles-1 |
---|
347 | do n=0,nvars-1 |
---|
348 | dimnamesstring = str_join(dimNames(i,n,::-1),":") |
---|
349 | print(" ") |
---|
350 | print("Variable: "+varString(i,n)+", number "+(n+1+i)+" of "+(nvars+nofiles-1) ) |
---|
351 | print(" ") |
---|
352 | do t=t_end,dimsizes(time)-1 |
---|
353 | print("Time step "+(t-t_end+1)+" of "+(dimsizes(time)-t_end) ) |
---|
354 | a = systemfunc(path+"ncdf2vdf -dimnames "+dimnamesstring+" "+reflevel_str+" -varname "+varString(i,n)+" -ncdfvar "+varString(i,n)+" -cnstnames time -cnstvals "+t+" -ts "+t+" "+workpath+outputfile+" "+workpath+file_in(i)) |
---|
355 | delete(a) |
---|
356 | end do |
---|
357 | end do |
---|
358 | end do |
---|
359 | |
---|
360 | if topo then |
---|
361 | print(" ") |
---|
362 | print("Adding topography (1)...") |
---|
363 | print(" ") |
---|
364 | do t=t_end,dimsizes(time)-1 |
---|
365 | print("Time step "+(t-t_end+1)+" of "+(dimsizes(time)-t_end) ) |
---|
366 | a = systemfunc(path+"ncdf2vdf -dimnames x:y "+reflevel_str+" -varname HGT -ncdfvar zwwi -cnstnames time -cnstvals "+t+" -ts "+t+" "+workpath+outputfile+" "+workpath+file_in(0)) |
---|
367 | delete(a) |
---|
368 | end do |
---|
369 | print(" ") |
---|
370 | print("Adding topography (2)...") |
---|
371 | print(" ") |
---|
372 | do t=t_end,dimsizes(time)-1 |
---|
373 | print("Time step "+(t-t_end+1)+" of "+(dimsizes(time)-t_end) ) |
---|
374 | a = systemfunc(path+"ncdf2vdf -dimnames x:y "+reflevel_str+" -varname HGT -ncdfvar zwwi -cnstnames time -cnstvals "+t+" -ts "+t+" "+workpath+outputfile+" "+workpath+file_in(0)) |
---|
375 | delete(a) |
---|
376 | end do |
---|
377 | end if |
---|
378 | |
---|
379 | |
---|
380 | |
---|
381 | t_end = dimsizes(time)-1 |
---|
382 | |
---|
383 | |
---|
384 | |
---|
385 | end if |
---|
386 | |
---|
387 | |
---|
388 | ;**** Add 2D cross sections |
---|
389 | if (addmore .eq. "2d") then |
---|
390 | delete(f) |
---|
391 | delete(file_in) |
---|
392 | |
---|
393 | print(" ") |
---|
394 | print("Choose additional file to add 2D cross sections. Type index.") |
---|
395 | print(" ") |
---|
396 | files_avail = systemfunc("cd "+workpath+"; ls *.nc") |
---|
397 | print(" "+files_avail) |
---|
398 | |
---|
399 | file_in = files_avail(toint(systemfunc("read file; echo $file"))) |
---|
400 | delete(files_avail) |
---|
401 | |
---|
402 | f = addfile(workpath+file_in,"r") |
---|
403 | |
---|
404 | varNames2d = getfilevarnames(f) |
---|
405 | |
---|
406 | crosstype = systemfunc("read -p 'Please specify type of cross section (xy/xz/yz) ' RESP; echo $RESP") |
---|
407 | print(" ") |
---|
408 | print("The following variables were found in the file:") |
---|
409 | print(" ") |
---|
410 | print(" "+varNames2d) |
---|
411 | print(" ") |
---|
412 | print("Type the index of the variables you want to add to the *.vdf file (seperated by ',')") |
---|
413 | |
---|
414 | vars2d = toint(str_split(systemfunc("read vars; echo $vars"), ",")) |
---|
415 | |
---|
416 | |
---|
417 | |
---|
418 | nvars2d = dimsizes(vars2d) |
---|
419 | |
---|
420 | varString2d = new(dimsizes(vars2d),string) |
---|
421 | dimNames2d = new((/dimsizes(vars2d),4/),string) |
---|
422 | |
---|
423 | do n=0,nvars2d-1 |
---|
424 | varString2d(n) = varNames2d(vars2d(n)) |
---|
425 | |
---|
426 | dims2d = getfilevardimsizes(f,varString2d(n)) |
---|
427 | dimNames2d(n,:) = getfilevardims(f,varString2d(n)) |
---|
428 | |
---|
429 | |
---|
430 | end do |
---|
431 | |
---|
432 | vars2dstr = str_join(varString2d,":") |
---|
433 | |
---|
434 | a = systemfunc(path+"vdfedit -addvars2d"+crosstype+" "+vars2dstr+" "+workpath+outputfile) |
---|
435 | delete(a) |
---|
436 | |
---|
437 | do n=0,nvars2d-1 |
---|
438 | dimnamesstring = str_join(dimNames2d(n,::-1),":") |
---|
439 | |
---|
440 | do t=0,dims2d(0)-1 |
---|
441 | a = systemfunc(path+"ncdf2vdf -dimnames "+dimnamesstring+" "+reflevel_str+" -varname "+varNames2d(vars2d(n))+" -ncdfvar "+varNames2d(vars2d(n))+" -cnstnames time -cnstvals "+t+" -ts "+t+" "+workpath+outputfile+" "+workpath+file_in) |
---|
442 | delete(a) |
---|
443 | end do |
---|
444 | end do |
---|
445 | delete(varNames2d) |
---|
446 | end if |
---|
447 | |
---|
448 | addmore = systemfunc("read -p 'Add additional *.nc files? (y/n) ' RESP; echo $RESP") |
---|
449 | end do |
---|
450 | |
---|
451 | ;******************************************************************************* |
---|
452 | print("Done.") |
---|
453 | ;************* |
---|
454 | end |
---|