;--------------------------------------------------------------------------------! ; This file is part of PALM. ; ; PALM is free software: you can redistribute it and/or modify it under the terms ; of the GNU General Public License as published by the Free Software Foundation, ; either version 3 of the License, or (at your option) any later version. ; ; PALM is distributed in the hope that it will be useful, but WITHOUT ANY ; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR ; A PARTICULAR PURPOSE. See the GNU General Public License for more details. ; ; You should have received a copy of the GNU General Public License along with ; PALM. If not, see . ; ; Copyright 1997-2012 Leibniz University Hannover ;------------------------------------------------------------------------------! ; ; Current revisions: ; ----------------- ; ; Former revisions: ; ----------------- ; $Id: nc2vdf.ncl 1047 2012-11-09 15:32:58Z hoffmann $ ; ; 1046 2012-11-09 14:38:45Z maronga ; Initial revision ; ; Description: ; ------------ ; This NCL script coverts PALM NetCDF data to vdf, VAPOR's own data format. In ; order to run this script, NCL version 5.2.0 or higher is required. ; Default setting will be loaded from .nc2vdf.config. ; Note that on HLRN, vapor is only available on hicegate2, bicegate2 and the UV ; sytem. ;------------------------------------------------------------------------------! load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl" outputfile = "default.vdf" if(isfilepresent(inputfile)) then print("*** Loading " + inputfile) loadscript(inputfile) else print("+++ Input file does not exist. Exiting...") exit end if begin ; print loaded data from input file print("*** loading " + dimsizes(files) + ":") do i=0,dimsizes(files)-1 print(" " + i + ") " + files(i)) end do print("*** will create dataset " + outputfile) workpath = systemfunc("echo $PWD") + "/" path = "" ; hannes stuff below ;-) ;****************************************************** x_min = new(1,double) x_max = new(1,double) x_min = 99.0 x_max = -99.0 y_min = new(1,double) y_max = new(1,double) y_min = 99.0 y_max = -99.0 z_min = new(1,double) z_max = new(1,double) z_min = 99.0 z_max = -99.0 vars3d = "" ;print("You may need to run 'vapor-setup.sh' in order to set all enviroment variables.") ;/muksoft/packages/vapor/2.1.0/bin/vapor-setup.sh ;**** Get names of variables wanted to appear in the VDF file, then get their dimensions with min/max x,y,z print(" ") print("Choose the *.nc files containing 3D data by typing their indices (seperated by ','). Start with the first files of the timeseries.") print(" ") files_avail = systemfunc("cd "+workpath+"; ls *.nc") print(" "+files_avail) files_in_ary = toint(str_split(systemfunc("read file; echo $file"), ",")) nofiles = dimsizes(files_in_ary) file_in = new(nofiles,string) do i=0,nofiles-1 file_in(i) = files_avail(files_in_ary(i)) end do if (nofiles .ge. 2) then nvars = 1 varString = new((/nofiles,nvars/),string) dimNames = new((/nofiles,nvars,4/),string) end if do i=0,nofiles-1 f = addfile(workpath+file_in(i),"r") varNames = getfilevarnames(f) print("Loading file...") print(" ") print("The following variables were found in "+file_in(i)+":") print(" ") print(" "+varNames) print(" ") print("Type the index of the VARIABLES (not dimensions) you want to visualize in Vapor (seperated by ',')") vars = toint(str_split(systemfunc("read vars; echo $vars"), ",")) nvars = dimsizes(vars) if (nofiles .eq. 1) then varString = new((/nofiles,nvars/),string) dimNames = new((/nofiles,nvars,4/),string) end if do n=0,nvars-1 varString(i,n) = varNames(vars(n)) if (vars3d .ne. "") then vars3d = vars3d + ":" + varNames(vars(n)) else vars3d = varNames(vars(n)) end if dims = getfilevardimsizes(f, varString(i,n)) dimNames(i,n,:) = getfilevardims(f, varString(i,n)) time = f->$dimNames(i,n,0)$ t_max = max(time) z = f->$dimNames(i,n,1)$ y = f->$dimNames(i,n,2)$ x = f->$dimNames(i,n,3)$ if (z_min .ge. min(z)) then z_min = min(z) end if if (z_max .le. max(z)) then z_max = max(z) end if if (y_min .ge. min(y)) then y_min = min(y) end if if (y_max .le. max(y)) then y_max = max(y) end if if (x_min .ge. min(x)) then x_min = min(x) end if if (x_max .le. max(x)) then x_max = max(x) end if delete(z) delete(y) delete(x) end do end do print(varString) delete(files_avail) print(" ") periodic = systemfunc("read -p 'Periodic lateral boundaries? (y/n) ' RESP; echo $RESP") if (periodic .eq. "y") then px = 1 py = 1 else px = 0 py = 0 end if print(" ") print(" ") grid = systemfunc("read -p 'Is grid stretching used? (y/n) YES will cut of heights above dz_stretch_level? ' RESP; echo $RESP") if (grid .eq. "y") then stretch_level = stringtointeger(systemfunc("read -p 'Please type height of dz_stretch_level (in meters) ' RESP; echo $RESP")) z_max = stretch_level ;**** limit z_max end if print(" ") ;******************************************************************************* if (any(varNames .EQ. "zwwi")) then var2d = " -vars2dxy HGT:topo" topo = True print(" ") print("Found topography - will add 'zwwi' (w-grid) topography to *.vdf file.") print(" ") else var2d = " " topo = False end if print(" ") print(dims(0)+" time steps found in "+file_in) ts = systemfunc("read -p 'Include all time steps in *.vdf file? (y/n) ' RESP; echo $RESP") if (ts .eq. "y") then t_start = 0 t_end = dimsizes(time)-1 else t_start = stringtointeger(systemfunc("read -p 'Type index of first time step: ' ts; echo $ts")) t_end = stringtointeger(systemfunc("read -p 'Type index of last time step: ' ts; echo $ts")) end if print(" ") askreflevel = systemfunc("read -p 'Specify a maximum refinement level? (y/n) No will set the default value. ' RESP; echo $RESP") if (askreflevel .eq. "y") then reflevel = systemfunc("read -p 'Please type maximum refinement level ' RESP; echo $RESP") reflevel_str = "-level "+ reflevel else reflevel_str = "" end if print(" ") ;******************************************************************************* ;******************************************************************************* ;******************************************************************************* print(" ") print("Creating *.vdf file") print(" ") ;vars3d = str_join(varString,":") 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) delete(a) print("Done.") ;******************************************************************************* print("Populating *.vdf file") do i=0,nofiles-1 do n=0,nvars-1 dimnamesstring = str_join(dimNames(i,n,::-1),":") print(" ") print("Variable: "+varString(i,n)+", number "+(n+1+i)+" of "+(nvars+nofiles-1) ) print(" ") do t=t_start,t_end print("Time step "+(t-t_start+1)+" of "+(t_end-t_start+1) ) 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)) delete(a) end do end do end do if topo then print(" ") print("Adding topography (1)...") print(" ") do t=t_start,t_end print("Time step "+(t-t_start+1)+" of "+(t_end-t_start+1) ) 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)) delete(a) end do print(" ") print("Adding topography (2)...") print(" ") do t=t_start,t_end print("Time step "+(t-t_start+1)+" of "+(t_end-t_start+1) ) 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)) delete(a) end do end if ;******************************************************************************* ;**** Read more files and add them to VDF/VDC addmore = "y" do while (addmore .eq. "y") addmore = systemfunc("read -p 'Add time steps or 2D cross sections from additional *.nc files? (2d/time/n) ' RESP; echo $RESP") ;**** Add time steps from additional 3D files if (addmore .eq. "time") then delete(f) do i=0,nofiles-1 print(" ") print("Choose the *.nc file that contains 3D data for "+varString(i,0)+" by typing it´s index.") print(" ") files_avail = systemfunc("cd "+workpath+"; ls *.nc") print(" "+files_avail) print(" ") file_in(i) = files_avail(toint(systemfunc("read file; echo $file"))) end do f = addfile(workpath+file_in(0),"r") time = f->time a = systemfunc(path+"vdfedit -addts "+dimsizes(time)+" "+workpath+outputfile) delete(a) do i=0,nofiles-1 do n=0,nvars-1 dimnamesstring = str_join(dimNames(i,n,::-1),":") print(" ") print("Variable: "+varString(i,n)+", number "+(n+1+i)+" of "+(nvars+nofiles-1) ) print(" ") do t=t_end,dimsizes(time)-1 print("Time step "+(t-t_end+1)+" of "+(dimsizes(time)-t_end) ) 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)) delete(a) end do end do end do if topo then print(" ") print("Adding topography (1)...") print(" ") do t=t_end,dimsizes(time)-1 print("Time step "+(t-t_end+1)+" of "+(dimsizes(time)-t_end) ) 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)) delete(a) end do print(" ") print("Adding topography (2)...") print(" ") do t=t_end,dimsizes(time)-1 print("Time step "+(t-t_end+1)+" of "+(dimsizes(time)-t_end) ) 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)) delete(a) end do end if t_end = dimsizes(time)-1 end if ;**** Add 2D cross sections if (addmore .eq. "2d") then delete(f) delete(file_in) print(" ") print("Choose additional file to add 2D cross sections. Type index.") print(" ") files_avail = systemfunc("cd "+workpath+"; ls *.nc") print(" "+files_avail) file_in = files_avail(toint(systemfunc("read file; echo $file"))) delete(files_avail) f = addfile(workpath+file_in,"r") varNames2d = getfilevarnames(f) crosstype = systemfunc("read -p 'Please specify type of cross section (xy/xz/yz) ' RESP; echo $RESP") print(" ") print("The following variables were found in the file:") print(" ") print(" "+varNames2d) print(" ") print("Type the index of the variables you want to add to the *.vdf file (seperated by ',')") vars2d = toint(str_split(systemfunc("read vars; echo $vars"), ",")) nvars2d = dimsizes(vars2d) varString2d = new(dimsizes(vars2d),string) dimNames2d = new((/dimsizes(vars2d),4/),string) do n=0,nvars2d-1 varString2d(n) = varNames2d(vars2d(n)) dims2d = getfilevardimsizes(f,varString2d(n)) dimNames2d(n,:) = getfilevardims(f,varString2d(n)) end do vars2dstr = str_join(varString2d,":") a = systemfunc(path+"vdfedit -addvars2d"+crosstype+" "+vars2dstr+" "+workpath+outputfile) delete(a) do n=0,nvars2d-1 dimnamesstring = str_join(dimNames2d(n,::-1),":") do t=0,dims2d(0)-1 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) delete(a) end do end do delete(varNames2d) end if addmore = systemfunc("read -p 'Add additional *.nc files? (y/n) ' RESP; echo $RESP") end do ;******************************************************************************* print("Done.") ;************* end