srv2nc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #!/bin/bash
  2. # PlaSim netcdf postprocessor script
  3. # (c) 2019 Jost von Hardenberg - ISAC-CNR
  4. #set -ex
  5. export LC_NUMERIC="en_US.UTF-8"
  6. function help {
  7. echo "Convert PLASIM service output file to netcdf format"
  8. echo "PlaSIM, ML ocean, ice model and LSG outputs are recognized"
  9. echo "Usage: srv2nc [-m] [-y] [-d code1,code2,...] [-f format] infile.srv outfile.nc"
  10. echo "Options:"
  11. echo " -m perform monthly means"
  12. echo " -y perform yearly means"
  13. echo " -p compute sea-level pressure"
  14. echo " -d codelist remove a comma-separated list of codes from the output"
  15. echo " -f format output format (default is zipped netcdf4 (HDF5))"
  16. echo " Alternatives: nc, nc1, nc4, nc4c, nc5"
  17. echo " -h print this help"
  18. exit
  19. }
  20. fmonth=""
  21. fyear=""
  22. format="nc4 -z zip"
  23. while getopts "d:ypmhf:" OPTION; do
  24. case $OPTION in
  25. d) delcodes=$OPTARG ;;
  26. m) fmonth="-monmean" ;;
  27. y) fyear="-yearmean" ;;
  28. p) psl=1 ;;
  29. f) format=$OPTARG ;;
  30. h) help ;;
  31. esac
  32. done
  33. shift $((OPTIND-1))
  34. if [[ ! "nc nc1 nc2 nc4 nc4c nc5 nc4c -z zip nc4 -z zip" == *$format* ]]
  35. then
  36. echo "Invalid format: " $format
  37. echo "Choose one of: nc, nc1, nc2, nc4, nc4c, nc5, nc4c -z zip, nc4 -z zip"
  38. exit 1
  39. fi
  40. if [ $# -lt 2 ]; then
  41. help
  42. fi
  43. infile=$1
  44. outfile=$2
  45. cdo="cdo -s"
  46. cdonctab="cdo -t ./param$$.tab -f nc -s"
  47. cdonc="cdo -f nc -s"
  48. cdozip="cdo -f $format -s"
  49. cdoziptab="cdo -t ./param$$.tab -f $format -s"
  50. cat > param$$.tab << EOT
  51. 110 mld Mixed Layer Depth [m]
  52. 129 sg Surf. Geopotential Orography [m2/s2]
  53. 130 ta Temperature [K]
  54. 131 ua Zonal Wind [m/s]
  55. 132 va Meridional Wind [m/s]
  56. 133 hus Specific Humidity [kg/kg]
  57. 134 ps Surface Pressure [hPa]
  58. 135 wap Vertical Pressure Velocity [Pa/s]
  59. 137 wa Vertical Wind [m/s]
  60. 138 zeta Vorticity [1/s]
  61. 139 ts Surface Temperature [K]
  62. 140 mrso Soil Wetness [m]
  63. 141 snd Snow Depth [m]
  64. 142 prl Large Scale Precipitation [m/s]
  65. 143 prc Convective Precipitation [m/s]
  66. 144 prsn Snow Fall [m/s]
  67. 145 bld Boundary Layer Dissipation [W/m**2]
  68. 146 hfss Surface Sensible Heat Flux [W/m**2]
  69. 147 hfls Surface Latent Heat Flux [W/m**2]
  70. 148 stf Streamfunction [m**2/s]
  71. 149 psi Velocity Potential [m**2/s]
  72. 151 psl Mean Sea Level Pressure [hPa]
  73. 152 pl Log Surface Pressure [1]
  74. 155 d Divergence [1/s]
  75. 156 zg Geopotential Height [m]
  76. 157 hur Relative Humidity [%]
  77. 158 tps Tendency of Surface Pressure [Pa/s]
  78. 159 u3 ustar **3 [m**3/s**3]
  79. 160 mrro Surface Runoff [m/s]
  80. 161 clw Liquid Water Content [kg/kg]
  81. 162 cl Cloud Cover [0-1]
  82. 163 tcc Total Cloud Cover [0-1]
  83. 164 clt Total Cloud Cover (Mean) [0-1]
  84. 165 uas Eastward Wind 10m [m/s]
  85. 166 vas Northward Wind 10m [m/s]
  86. 167 tas Temperature at 2m [K]
  87. 168 td2m Dew Point Temperature at 2m [K]
  88. 169 tsa Surface Temperature Accumulated [K]
  89. 170 tsod Deep Soil Temperature [K]
  90. 171 dsw Deep Soil Wetness [1]
  91. 172 lsm Land Sea Mask [0-1]
  92. 173 z0 Surface Roughness [m]
  93. 174 alb Surface Albedo [1]
  94. 175 as Surface Albedo [1]
  95. 176 rss Surface Solar Radiation [W/m2]
  96. 177 rls Surface Thermal Radiation [W/m2]
  97. 178 rst Top Solar Radiation [W/m2]
  98. 179 rlut Top Thermal Radiation [W/m2]
  99. 180 tauu U-Stress [Pa]
  100. 181 tauv V-Stress [Pa]
  101. 182 evap Evaporation [m/s]
  102. 183 tso Soil Temperature [K]
  103. 184 wsoi Soil Wetness [1]
  104. 199 vegc Vegetation Cover [0-1]
  105. 201 tasmax Maximum daily 2m temperature [K]
  106. 202 tasmin Minimum daily 2m temperature [K]
  107. 203 rsut Top Solar Radiation Upward [W/m2]
  108. 204 ssru Surface Solar Radiation Upward [W/m2]
  109. 205 stru Surface Therm Radiation Upward [W/m2]
  110. 207 tso2 Soil Temperature Level 2 [K]
  111. 208 tso3 Soil Temperature Level 3 [K]
  112. 209 tso4 Soil Temperature Level 4 [K]
  113. 210 sic Sea Ice Cover [0-1]
  114. 211 sit Sea Ice Thickness [m]
  115. 212 vegf Forest Cover [0-1]
  116. 218 snm Snow Melt [m/s]
  117. 221 sndc Snow Depth Change [m/s]
  118. 229 dwmax Field capacity [1]
  119. 230 prw Vert. Integrated Spec. Hum. [kg/m2]
  120. 232 glac Glacier Cover [0-1]
  121. 238 tsn Snow temperature [K]
  122. 259 spd Wind Speed [m/s]
  123. 260 pr Total Precipitation [m/s]
  124. 261 ntr Net Top Radiation [W/m2]
  125. 262 nbr Net Bottom Radiation [W/m2]
  126. 263 hfns Net Heat Flux [W/m2]
  127. 264 wfn Net Water Flux [m/s]
  128. 273 dpdx d(ps)/dx [Pa/m]
  129. 273 dpdy d(ps)/dy [Pa/m]
  130. 277 hlpr Half level pressure [Pa]
  131. 278 flpr Full level pressure [Pa]
  132. 701 heata Flux from the atmosphere [W/m2]
  133. 702 ofluxa Flux from the ocean [W/m2]
  134. 703 tsfluxa Flux to warm/cool ice/snow [W/m2]
  135. 704 smelta Flux for snow melt [W/m2]
  136. 705 imelta Flux used for ice melt [W/m2]
  137. 706 cfluxa Flux to the ocean [W/m2]
  138. 707 fluxca Cond. heatflux [W/m2]
  139. 708 qmelta Res flux to ice [W/m2]
  140. 709 xflxicea Flux correction [W/m2]
  141. 710 icec Ice cover [0-1]
  142. 711 iced Ice thickness [m]
  143. 712 scflxa Flux from snow -> ice conversion [W/m2]
  144. 713 cfluxra Flux for limiting ice to xmaxd [W/m2]
  145. 714 cfluxna Flux due to neg. ice [W/m2]
  146. 739 ts Surface temperature [K]
  147. 741 zsnow Snow depth [m h2o]
  148. 769 sst Sea surface temperature [K]
  149. 772 ls Land sea mask [0-1]
  150. 790 clicec2 Climatological ice cover [0-1]
  151. 791 cliced2 Climatological ice thickness [m]
  152. 792 icecc Ice cover computed prognostically [frac.]
  153. 794 cpmea Fresh water (p-e) for LSG [m/s]
  154. 795 croffa Runoff for LSG [m/s]
  155. 796 stoia Snow converted to ice [m h2o]
  156. 901 heata Heat flux from atm/ice [W/m2]
  157. 902 ifluxa Heat flux into ice [W/m2]
  158. 903 fssta Flux correction [W/m2]
  159. 904 dssta Vertical diffusion [m2/s]
  160. 905 qhda Horizontal diffusion [m2/s]
  161. 906 fldoa Flux from deep ocean (LSG) [W/m2]
  162. 910 icec Sea ice cover [0-1]
  163. 939 sst SST (ML ocean temperature) [K]
  164. 972 ls Land-sea mask [1]
  165. 990 clsst Climatological SST [K]
  166. EOT
  167. cat > paramlsg$$.tab << EOT
  168. 1 zeta surface elevation [m]
  169. 2 t potential temperature [K]
  170. 3 utot zonal velocity component [m/s]
  171. 4 vtot meridional velocity component [m/s]
  172. 5 s salinity [0/00]
  173. 7 w vertical velocity component [m/s]
  174. 13 sice ice thickness [m]
  175. 18 fluxhea heat flux [W/m2]
  176. 27 psi horizontal barotropic stream function [m3/s]
  177. 37 ub zonal component of barotropic velocity [m/s]
  178. 38 vb meridional component of barotropic velocity [m/s]
  179. 40 wet land-sea mask for scalar points [0-1]
  180. 41 wetvec land-sea mask for vector points [0-1]
  181. 52 taux zonal component of wind stress [Pa]
  182. 53 tauy meridional component of wind stress [Pa]
  183. 62 tc potential temperature [C]
  184. 65 fluwat net fresh water flux (P-E) [m/s]
  185. 66 convadd potential energy dissipation due to convection [mW/m2]
  186. 67 flukwat fresh water flux due to Newtonian coupling [m/s]
  187. 68 flukhea heat flux due to Newtonian coupling [W/m2]
  188. 69 convad convective adjustment events [1]
  189. 80 fldsst sst differences lsg-plasim [K]
  190. 81 fldice ice-differences lsg-plasim [1]
  191. 82 fldpme pme-differences lsg-plasim [m/s]
  192. 83 fldtaux taux-differences lsg-plasim [Pa]
  193. 84 fldtauy tauy-differences lsg-plasim [Pa]
  194. 92 tbound boundary value of t [K]
  195. 97 dQ/dt coupling coefficient [W/m2K]
  196. 98 depp depth in scalar-points [m]
  197. 99 depv depth in vector-points [m]
  198. EOT
  199. ysize=$($cdo -s griddes $infile | grep ysize | head -1 | cut -d= -f2|sed 's/ //g')
  200. case $ysize in
  201. 32) spgrid=t21; gpgrid=n16;;
  202. 48) spgrid=t31; gpgrid=n24;;
  203. 64) spgrid=t42; gpgrid=n32;;
  204. 96) spgrid=t63; gpgrid=n48;;
  205. 128) spgrid=t85; gpgrid=n64;;
  206. 160) spgrid=t106; gpgrid=n80;;
  207. 76) gpgridt=lsggridt$$.txt; gpgridu=lsggridu$$.txt; flsg=1;;
  208. esac
  209. if [ -z "$flsg" ]; then
  210. fsp=$($cdo griddes $infile | grep "gridID 2") # Spectral or not?
  211. $cdo zaxisdes $infile > inzgrid$$.txt
  212. f3d=$(grep "zaxisID 2" inzgrid$$.txt) # Vertical sigma axis or not
  213. else
  214. # vertical axes for LSG
  215. cat > zaxislsgu$$.txt << EOT
  216. zaxistype = depth_below_sea
  217. size = 22
  218. levels = 25 75 125 175 225 275 350 450 550 650 750 850 950 1100 1300 1500 1800 2250 2750 3500 4500 5500
  219. EOT
  220. cat > zaxislsgc$$.txt << EOT
  221. zaxistype = depth_below_sea
  222. size = 21
  223. levels = 75 125 175 225 275 350 450 550 650 750 850 950 1100 1300 1500 1800 2250 2750 3500 4500 5500
  224. EOT
  225. cat > zaxislsgw$$.txt << EOT
  226. zaxistype = depth_below_sea
  227. size = 22
  228. levels = 50 100 150 200 250 312.5 400 500 600 700 800 900 1025 1200 1400 1650 2025 2500 3125 4000 5000 6000
  229. EOT
  230. # Create scalar grid
  231. cat > $gpgridt <<EOT
  232. #
  233. # gridID 1
  234. #
  235. gridtype = curvilinear
  236. gridsize = 5472
  237. xsize = 72
  238. ysize = 76
  239. xname = lon
  240. xdimname = west_east
  241. xlongname = "longitude"
  242. xunits = "degrees_east"
  243. yname = lat
  244. ydimname = south_north
  245. ylongname = "latitude"
  246. yunits = "degrees_north"
  247. xvals =
  248. $(awk "BEGIN {off=-2.5; for(y=93.75; y>=-93.75; y=y-2.5) {for(x=0.; x<=357.5; x=x+5.0) {printf x-off \" \" }; printf \"\n\"; off=2.5+off}}")
  249. yvals =
  250. $(awk "BEGIN {for(y=93.75; y>=-93.75; y=y-2.5) {for(i=1; i<=72; ++i) {printf y \" \" } printf \"\n\"}}")
  251. scanningMode = 64
  252. EOT
  253. # Create vector grid
  254. cat > $gpgridu <<EOT
  255. #
  256. # gridID 1
  257. #
  258. gridtype = curvilinear
  259. gridsize = 5472
  260. xsize = 72
  261. ysize = 76
  262. xname = lon
  263. xdimname = west_east
  264. xlongname = "longitude"
  265. xunits = "degrees_east"
  266. yname = lat
  267. ydimname = south_north
  268. ylongname = "latitude"
  269. yunits = "degrees_north"
  270. xvals =
  271. $(awk "BEGIN {off=-5.; for(y=93.75; y>=-93.75; y=y-2.5) {for(x=0.; x<=357.5; x=x+5.0) {printf x-off \" \" }; printf \"\n\"; off=2.5+off}}")
  272. yvals =
  273. $(awk "BEGIN {for(y=93.75; y>=-93.75; y=y-2.5) {for(i=1; i<=72; ++i) {printf y \" \" } printf \"\n\"}}")
  274. scanningMode = 64
  275. EOT
  276. fi
  277. # If 3D Field, adjust vertical grid
  278. if [ ! -z "$f3d" ]; then
  279. nlev=$( grep size inzgrid$$.txt|tail -1|cut -d= -f2|sed 's/ //g' )
  280. #sigmas=$($cdo outputf,%19.16f -selindexbox,1,$nlev,1,1 -selcode,333 $infile | awk ' BEGIN{p=0.} {s=($1+p)/2.; p=$1} {printf("%19.16f\n", s)}')
  281. sigmas=$($cdo outputf,%19.16f -selindexbox,1,$nlev,1,1 -selcode,333 $infile)
  282. grep -m 1 -B 1000 -A 1 "zaxisID 2" inzgrid$$.txt >zgrid$$.txt
  283. cat >> zgrid$$.txt << EOT
  284. zaxistype = hybrid
  285. size = $nlev
  286. levels = $( seq 1 $nlev )
  287. vctsize = $(( nlev*2 + 2 ))
  288. vct = $( printf "%.s0 " $(seq 1 $((nlev + 1)) ) )
  289. 0 $sigmas
  290. EOT
  291. fixzgrid="-setzaxis,zgrid$$.txt"
  292. delcode333="-delcode,333"
  293. else
  294. fixzgrid=""
  295. delcode333=""
  296. fi
  297. if [ -z "$flsg" ]; then
  298. # If not LSG
  299. # If spectral fix grids
  300. if [ ! -z "$fsp" ]; then
  301. $cdo splitgrid ${infile} grid_$$_
  302. if [ ! -z "$psl" ]; then
  303. $cdonctab $fixzgrid -setgrid,$spgrid grid_$$_02.srv spectral$$.nc
  304. $cdonctab $fixzgrid $delcode333 -setgrid,$gpgrid grid_$$_01.srv gaussian$$.nc
  305. $cdonctab merge spectral$$.nc gaussian$$.nc tempfile0$$.nc
  306. $cdonctab sealevelpressure -sp2gp tempfile0$$.nc press$$.nc
  307. $cdozip $fmonth $fyear -merge tempfile0$$.nc press$$.nc tempfile$$.nc
  308. else
  309. $cdonctab $fmonth $fyear $fixzgrid -setgrid,$spgrid grid_$$_02.srv spectral$$.nc
  310. $cdonctab $fmonth $fyear $fixzgrid $delcode333 -setgrid,$gpgrid grid_$$_01.srv gaussian$$.nc
  311. $cdozip merge spectral$$.nc gaussian$$.nc tempfile$$.nc
  312. fi
  313. else
  314. $cdoziptab setgrid,$gpgrid $fmonth $fyear $infile tempfile$$.nc
  315. fi
  316. else
  317. # If LSG
  318. #$cdonc copy $fmonth $fyear $infile tempinfile$$.nc
  319. $cdonc splitzaxis $infile zaxis$$_
  320. $cdonc setzaxis,zaxislsgu$$.txt -delcode,7 zaxis$$_01.nc fixzaxis$$_01u.nc # code 7 is w
  321. $cdonc setzaxis,zaxislsgw$$.txt -selcode,7 zaxis$$_01.nc fixzaxis$$_01w.nc
  322. $cdonc setzaxis,zaxislsgc$$.txt zaxis$$_03.nc fixzaxis$$_03.nc
  323. $cdonc merge fixzaxis$$_01u.nc fixzaxis$$_01w.nc zaxis$$_02.nc fixzaxis$$_03.nc tempin$$.nc
  324. $cdonc setpartab,paramlsg$$.tab $fmonth $fyear tempin$$.nc tempinpar$$.nc
  325. uvars="taux,tauy,depv,ub,vb,utot,vtot,wetvec"
  326. $cdonc setgrid,$gpgridu $fmonth $fyear -selname,$uvars tempinpar$$.nc ufields$$.nc
  327. $cdonc setgrid,$gpgridt $fmonth $fyear -delname,$uvars tempinpar$$.nc tfields$$.nc
  328. $cdozip merge tfields$$.nc ufields$$.nc tempfile$$.nc
  329. fi
  330. if [ -z "$delcodes" ]
  331. then
  332. mv tempfile$$.nc $outfile
  333. else
  334. $cdozip delcode,$delcodes tempfile$$.nc $outfile
  335. fi
  336. rm -f spectral$$.nc gaussian$$.nc grid_$$_01.srv grid_$$_02.srv inzgrid$$.txt zgrid$$.txt tempfile$$.nc param$$.tab lsggridt$$.txt lsggridu$$.txt ufields$$.nc tfields$$.nc zaxislsgu$$.txt zaxislsgw$$.txt zaxislsgc$$.txt zaxis$$_* fixzaxis$$_* tempin$$.nc tempinpar$$.nc paramlsg$$.tab press$$.nc tempfile0$$.nc