source: palm/trunk/SCRIPTS/nc2vdf.ncl @ 1062

Last change on this file since 1062 was 1062, checked in by maronga, 11 years ago

updates in nc2vdf

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 16.2 KB
RevLine 
[1046]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; -----------------
[1062]20; full batch mode support, loading configuration from an external file
21; fallback to interactive mode if no configuration present
22; batch mode does not support loading more than one variable per file if there
23; is more than one file specified (but there is a workaround)
24; you cannot add time steps to a variable from an additional file in batch mode
[1046]25;
26; Former revisions:
27; -----------------
28; $Id: nc2vdf.ncl 1062 2012-11-21 15:57:18Z maronga $
29;
[1047]30; 1046 2012-11-09 14:38:45Z maronga
31; Initial revision
32;
[1046]33; Description:
34; ------------
35; This NCL script coverts PALM NetCDF data to vdf, VAPOR's own data format. In
36; order to run this script, NCL version 5.2.0 or higher is required.
37; Default setting will be loaded from .nc2vdf.config.
38; Note that on HLRN, vapor is only available on hicegate2, bicegate2 and the UV
39; sytem.
40;------------------------------------------------------------------------------!
41
42load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
43load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
44load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
45load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
46
[1062]47alt_varnames = False
48man = True
49path = ""
[1046]50
51if(isfilepresent(inputfile)) then
52   print("*** Loading " + inputfile)
53   loadscript(inputfile)
[1062]54   man = False
[1046]55else
[1062]56    print("+++ Input file does not exist.")
57    man_resp = systemfunc("read -p 'Enter manual mode? (y/n) ' RESP; echo $RESP")
58    if (man_resp .eq. "y") then
59       man = True
60    else
61       print("+++ Exiting...")
62       exit
63    end if
[1046]64end if
65
66
67begin
68
[1062]69if (.not. man) then
[1046]70;   print loaded data from input file
71    print("*** loading " + dimsizes(files) + ":")
72    do i=0,dimsizes(files)-1
73       print("    " + i + ") " + files(i)) 
74    end do
75
76    print("*** will create dataset " + outputfile)
[1062]77end if
[1046]78
[1062]79;******************************************************
80;give path to the directory where NetCDF files are stored:
81    print("*** Checking hostname...")
82    sys        = "unknown"
83    query      = systemfunc("echo $HOSTNAME")
84    check_muk  = isStrSubset(query,"muk.uni-hannover.de")
85    check_hice = isStrSubset(query,"hicegate2")
86    check_bice = isStrSubset(query,"bicegate2")
87    check_huv  = isStrSubset(query,"huv")
88    check_buv  = isStrSubset(query,"buv")
89    check_WS   = isStrSubset(query,"kookaburra") ;*** insert name of your workstation here
[1046]90
91
92
[1062]93    if ( check_muk .eq. True ) then
94       print("*** nc2vdf will execute at IMUK")
95       sys  = "IMUK"
96    else
97       if ( check_hice .eq. True .or. check_bice .eq. True ) then
98          print("*** nc2vdf will execute on hicegate2/bicegate2")
99          sys = "HLRN"
100       else
101          if ( check_huv .eq. True .or. check_buv .eq. True ) then
102             print("*** nc2vdf will execute on UV")
103             sys = "HLRN"
104             else
105                if ( check_WS .eq. True) then
106                print("*** nc2vdf will execute on a workstation PC/Mac")
107                sys = "workstation"
108                path     = "/Applications/VAPOR.app/Contents/MacOS/" ;*** costomize path for WS
109          end if
110          end if
111       end if
112    end if
113    if ( sys .eq. "unknown" ) then
114       print("+++ unknown system. Exiting...")
115       exit
116    end if
[1046]117
[1062]118   
119workpath = systemfunc("echo $PWD") + "/"
120
121
[1046]122;******************************************************
[1062]123;******************************************************
124if man then
125;specify name of output file:
126outputfile      = "building.vdf" ;<<<<------------- !!!
127end if
128;******************************************************
129;******************************************************
[1046]130
131x_min = new(1,double)
132x_max = new(1,double)
133x_min = 99.0
134x_max = -99.0
135y_min = new(1,double)
136y_max = new(1,double)
137y_min = 99.0
138y_max = -99.0
139z_min = new(1,double)
140z_max = new(1,double)
141z_min = 99.0
142z_max = -99.0
143
144vars3d      = ""
145
146
147
[1062]148if man then
149    ;**** Get names of variables wanted to appear in the VDF file, then get their dimensions with min/max x,y,z
150    print(" ")
151    print("Choose the *.nc files containing 3D data by typing their indices (seperated by ','). Start with the first files of the timeseries.")
152    print(" ")
153    files_avail   = systemfunc("cd "+workpath+"; ls *.nc")
154    print(" "+files_avail)
[1046]155
156
[1062]157    files_in_ary  = toint(str_split(systemfunc("read file; echo $file"), ","))
158    nofiles       = dimsizes(files_in_ary)
159    file_in       = new(nofiles,string)
[1046]160
[1062]161    do i=0,nofiles-1
162        file_in(i)    = files_avail(files_in_ary(i))
163    end do
164else
165    file_in = files
166    nofiles = dimsizes(file_in)
167end if
[1046]168
169
170
171
172
173if (nofiles .ge. 2) then
174nvars   = 1
[1062]175if man then
[1046]176varString  = new((/nofiles,nvars/),string)
[1062]177end if; man
[1046]178dimNames   = new((/nofiles,nvars,4/),string)
179end if
180
181
182do i=0,nofiles-1
[1062]183    print("Loading file...")
[1046]184    f             = addfile(workpath+file_in(i),"r")
185
186    varNames      = getfilevarnames(f)
187   
[1062]188   
189    if man then
[1046]190    print(" ")
191    print("The following variables were found in "+file_in(i)+":")
192    print(" ")
193    print(" "+varNames)
194    print(" ")
195    print("Type the index of the VARIABLES (not dimensions) you want to visualize in Vapor (seperated by ',')")
196
197    vars       = toint(str_split(systemfunc("read vars; echo $vars"), ","))
198
199
200    nvars = dimsizes(vars)
[1062]201    end if; man
202   
[1046]203    if (nofiles .eq. 1) then
[1062]204    if man then
[1046]205    varString  = new((/nofiles,nvars/),string)
[1062]206    end if; man
[1046]207    dimNames   = new((/nofiles,nvars,4/),string)
208    end if
209
210
211
212    do n=0,nvars-1
[1062]213       if man then
[1046]214       varString(i,n) = varNames(vars(n))
[1062]215       end if; man
[1046]216       
[1062]217       if alt_varnames then
218            varString(i,n) = varNames(vars(i,n))
219       end if
220       
[1046]221       if (vars3d .ne. "") then
[1062]222       vars3d       = vars3d + ":" + varString(i,n)
[1046]223       else
[1062]224       vars3d       = varString(i,n)
[1046]225       end if
226
227
228       dims          = getfilevardimsizes(f, varString(i,n))
229       dimNames(i,n,:) = getfilevardims(f, varString(i,n))
230
231       
232
233       time     = f->$dimNames(i,n,0)$
234       t_max    = max(time)
235       z        = f->$dimNames(i,n,1)$
236       y        = f->$dimNames(i,n,2)$
[1062]237       x            = f->$dimNames(i,n,3)$
[1046]238
239       if (z_min .ge. min(z)) then
240          z_min = min(z)
241       end if
242
243       if (z_max .le. max(z)) then
244          z_max = max(z)
245       end if
246
247       if (y_min .ge. min(y)) then
248          y_min = min(y)
249       end if
250
251       if (y_max .le. max(y)) then
252          y_max = max(y)
253       end if
254
255       if (x_min .ge. min(x)) then
256          x_min = min(x)
257       end if
258
259       if (x_max .le. max(x)) then
260          x_max = max(x)
261       end if
262
263       delete(z)
264       delete(y)
265       delete(x)
266    end do
267
268end do
269
[1062]270;print(varString)
[1046]271
[1062]272if man then
[1046]273delete(files_avail)
[1062]274end if
[1046]275
[1062]276if man then
277    print(" ")
278    periodic_resp              = systemfunc("read -p 'Periodic lateral boundaries? (y/n) ' RESP; echo $RESP")
279    if (periodic_resp .eq. "y") then
280        periodic = True
281    else
282        periodic = False
283    end if
284end if
[1046]285
[1062]286if (periodic) then
[1046]287   px           = 1
288   py           = 1
289else
290   px           = 0
291   py           = 0
292end if
293print(" ")
294print(" ")
295
[1062]296if man then
297    grid_resp              = systemfunc("read -p 'Is grid stretching used? (y/n) YES will cut of heights above dz_stretch_level? ' RESP; echo $RESP")
298    if (grid_resp .eq. "y") then
299        grid_stretch = True
300    else
301        grid_stretch = False
302    end if
[1046]303end if
304
[1062]305if (grid_stretch) then
306    if man then
307       stretch_level    = stringtointeger(systemfunc("read -p 'Please type height of dz_stretch_level (in meters) ' RESP; echo $RESP"))
308    end if; man
309    z_max = stretch_level ;**** limit z_max
310end if
311
[1046]312print(" ")
313
314;*******************************************************************************
315
316
317
318
319if (any(varNames .EQ. "zwwi")) then
320   var2d     = " -vars2dxy HGT:topo"
321   topo      = True
322   print(" ")
323   print("Found topography - will add 'zwwi' (w-grid) topography to *.vdf file.")
324   print(" ")
325else
326   var2d     = " "
327   topo      = False
328end if
329
[1062]330if man then
[1046]331print(" ")
332
333print(dims(0)+" time steps found in "+file_in)
334ts       = systemfunc("read -p 'Include all time steps in *.vdf file? (y/n) ' RESP; echo $RESP")
335
336if (ts .eq. "y") then
337   t_start      = 0
338   t_end        = dimsizes(time)-1
339else
340   t_start      = stringtointeger(systemfunc("read -p 'Type index of first time step: ' ts; echo $ts"))
341   t_end        = stringtointeger(systemfunc("read -p 'Type index of last time step: ' ts; echo $ts"))
342end if
[1062]343end if
[1046]344
[1062]345if (.not. man) then
346    if (.not. spec_ts) then
347           t_start      = 0
348           t_end        = dimsizes(time)-1
349    end if
350end if
351
352if man then
353    print(" ")
354    askreflevel              = systemfunc("read -p 'Specify a maximum refinement level? (y/n) No will set the default value.  ' RESP; echo $RESP")
355    if (askreflevel .eq. "y") then
356       reflevel    = systemfunc("read -p 'Please type maximum refinement level ' RESP; echo $RESP")
357       reflevel_str = "-level "+ reflevel
358    else
359       reflevel_str = ""
360    end if
361    print(" ")
[1046]362else
[1062]363    if spec_reflevel then
364        reflevel_str = "-level "+ reflevel
365    else
366       reflevel_str = ""
367    end if
[1046]368end if
369
[1062]370       
[1046]371
372;*******************************************************************************
373;*******************************************************************************
374;*******************************************************************************
375print(" ")
376print("Creating *.vdf file")
377print(" ")
378;vars3d    = str_join(varString,":")
379
380a = 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)
381delete(a)
382print("Done.")
383
384
385;*******************************************************************************
386print("Populating *.vdf file")
387
388do i=0,nofiles-1
389    do n=0,nvars-1
390       dimnamesstring = str_join(dimNames(i,n,::-1),":")
391       print(" ")
392       print("Variable: "+varString(i,n)+", number "+(n+1+i)+" of "+(nvars+nofiles-1) )
393       print(" ")
394       do t=t_start,t_end
395          print("Time step "+(t-t_start+1)+" of "+(t_end-t_start+1) )
396          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))
397          delete(a)
398       end do
399    end do
400end do
401
402if topo then
403   print(" ")
404   print("Adding topography (1)...")
405   print(" ")
406   do t=t_start,t_end
407      print("Time step "+(t-t_start+1)+" of "+(t_end-t_start+1) )
408      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))
409      delete(a)
410   end do
411   print(" ")
412   print("Adding topography (2)...")
413   print(" ")
414   do t=t_start,t_end
415      print("Time step "+(t-t_start+1)+" of "+(t_end-t_start+1) )
416      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))
417      delete(a)
418   end do
419end if
420
421
422;*******************************************************************************
[1062]423if man then
[1046]424;**** Read more files and add them to VDF/VDC
425addmore = "y"
426do while (addmore .eq. "y")
427
428   addmore       = systemfunc("read -p 'Add time steps or 2D cross sections from additional *.nc files? (2d/time/n) ' RESP; echo $RESP")
429
430;**** Add time steps from additional 3D files
431    if (addmore .eq. "time") then
432
433    delete(f)
434         
435    do i=0,nofiles-1
436
437        print(" ")
438        print("Choose the *.nc file that contains 3D data for "+varString(i,0)+" by typing itÂŽs index.")
439        print(" ")
440        files_avail   = systemfunc("cd "+workpath+"; ls *.nc")
441        print(" "+files_avail)
442        print(" ")
443        file_in(i)    = files_avail(toint(systemfunc("read file; echo $file")))
444       
445    end do
446
447    f        = addfile(workpath+file_in(0),"r")
448
449    time     = f->time
450
451    a = systemfunc(path+"vdfedit -addts "+dimsizes(time)+" "+workpath+outputfile)
452    delete(a)
453
454    do i=0,nofiles-1
455        do n=0,nvars-1
456           dimnamesstring = str_join(dimNames(i,n,::-1),":")
457           print(" ")
458           print("Variable: "+varString(i,n)+", number "+(n+1+i)+" of "+(nvars+nofiles-1) )
459           print(" ")
460           do t=t_end,dimsizes(time)-1
461              print("Time step "+(t-t_end+1)+" of "+(dimsizes(time)-t_end) )
462              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))
463              delete(a)
464           end do
465        end do
466    end do
467
468    if topo then
469       print(" ")
470       print("Adding topography (1)...")
471       print(" ")
472       do t=t_end,dimsizes(time)-1
473          print("Time step "+(t-t_end+1)+" of "+(dimsizes(time)-t_end) )
474          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))
475          delete(a)
476       end do
477       print(" ")
478       print("Adding topography (2)...")
479       print(" ")
480       do t=t_end,dimsizes(time)-1
481          print("Time step "+(t-t_end+1)+" of "+(dimsizes(time)-t_end) )
482          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))
483          delete(a)
484       end do
485    end if
486           
487           
488           
489    t_end       = dimsizes(time)-1
490
491
492   
493    end if
494
495
496;**** Add 2D cross sections
497   if (addmore .eq. "2d") then
498      delete(f)
499      delete(file_in)
500
501      print(" ")
502      print("Choose additional file to add 2D cross sections. Type index.")
503      print(" ")
504      files_avail   = systemfunc("cd "+workpath+"; ls *.nc")
505      print(" "+files_avail)
506
507      file_in       = files_avail(toint(systemfunc("read file; echo $file")))
508      delete(files_avail)
509
510      f             = addfile(workpath+file_in,"r")
511
512      varNames2d      = getfilevarnames(f)
513
514      crosstype       = systemfunc("read -p 'Please specify type of cross section (xy/xz/yz) ' RESP; echo $RESP")
515      print(" ")
516      print("The following variables were found in the file:")
517      print(" ")
518      print(" "+varNames2d)
519      print(" ")
520      print("Type the index of the variables you want to add to the *.vdf file (seperated by ',')")
521
522      vars2d       = toint(str_split(systemfunc("read vars; echo $vars"), ","))
523
524
525
526      nvars2d = dimsizes(vars2d)
527
528      varString2d  = new(dimsizes(vars2d),string)
529      dimNames2d   = new((/dimsizes(vars2d),4/),string)
530
531      do n=0,nvars2d-1
532         varString2d(n) = varNames2d(vars2d(n))
533
534         dims2d     = getfilevardimsizes(f,varString2d(n))
535         dimNames2d(n,:) = getfilevardims(f,varString2d(n))
536         
537
538      end do
539
540      vars2dstr    = str_join(varString2d,":")
541
542      a = systemfunc(path+"vdfedit -addvars2d"+crosstype+" "+vars2dstr+" "+workpath+outputfile)
543      delete(a)
544
545      do n=0,nvars2d-1
546         dimnamesstring = str_join(dimNames2d(n,::-1),":")
547
548         do t=0,dims2d(0)-1
549            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)
550            delete(a)
551         end do
552      end do
553      delete(varNames2d)     
554   end if
555
556   addmore       = systemfunc("read -p 'Add additional *.nc files? (y/n) ' RESP; echo $RESP")
557end do
[1062]558end if; man
[1046]559;*******************************************************************************
560print("Done.")
561;*************
562end
Note: See TracBrowser for help on using the repository browser.