forked from arulalant/UMRider
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from arulalant/master
Support for EPS Average VSDB Input
- Loading branch information
Showing
12 changed files
with
356 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
bsubScripts/ncumeps_global_post/ncumeps_create_memavg_vsdb_input.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
#!/usr/bin/env python | ||
|
||
## This is call back script which will be executed after created the grib2 files. | ||
## | ||
## After created NCUM_EPS grib2 files (which contain all 45 members in it), lets | ||
## do calculate mean over all 45 members and then convert to 2.5X2.5 degree | ||
## resolution, finally convert to grib1 format for vsdb input purpose. | ||
## | ||
## Arulalan.T | ||
## 25-July-2016. | ||
|
||
import os, subprocess, datetime, getopt, sys, iris, numpy, time | ||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../g2utils'))) | ||
from cubeutils import cubeRealizationAverager | ||
from um2grb2 import tweaked_messages, getCubeData, createDirWhileParallelRacing | ||
|
||
|
||
neededVars = [ | ||
## Pressure Level Variable names & STASH codes | ||
('geopotential_height', 'm01s16i202'), | ||
('x_wind', 'm01s15i243'), | ||
('y_wind', 'm01s15i244'), | ||
('air_temperature', 'm01s16i203'), | ||
('relative_humidity', 'm01s16i256'), | ||
## Non Pressure Level Variable names & STASH codes | ||
('air_pressure_at_sea_level', 'm01s16i222'), | ||
] | ||
|
||
#Define _precipVars_ | ||
# The following vars should contains only precipitation, rainfall, snow | ||
# variables, those whose regrid extrapolate should be only in 'linear' mode | ||
# and not in 'mask' mode, and should not have -ve values. | ||
_precipVars_ = [('precipitation_amount', 'm01s05i226'),] | ||
|
||
g2ctl = "/gpfs2/home/umtid/Softwares/grib2ctl/g2ctl.pl" | ||
grib2ctl = "/gpfs2/home/umtid/Softwares/grib2ctl/grib2ctl.pl" | ||
gribmap = "/gpfs1/home/Libs/GNU/GRADS/grads-2.0.2.oga.1/Contents/gribmap" | ||
cnvgrib = "/gpfs1/home/Libs/INTEL/CNVGRIB/CNVGRIB-1.4.1/cnvgrib-1.4.1/cnvgrib" | ||
wgrib2 = "/gpfs1/home/Libs/GNU/WGRIB2/v2.0.1/wgrib2" | ||
|
||
_targetGridFile_ = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../data/sample_global_2p5X2p5_73X144.grib2')) | ||
_targetGrid_ = iris.load(_targetGridFile_)[0] | ||
|
||
__grib1FilesNameSuffix__ = None | ||
__wgrib2Arguments__ = ' -set_bin_prec 12 -set_grib_type complex2 -grib_out ' | ||
__OVERWRITE__ = True | ||
|
||
def createENSavg_VSDB_Grib1Files(inpath, outpath, today, utc, start_long_fcst_hour, stephr=24): | ||
|
||
|
||
day = int(start_long_fcst_hour) / int(stephr) | ||
pfileday = str(day).zfill(2) | ||
pfilename = 'umeps_prg_1cntl_44ens_24hourly_day' + pfileday | ||
needed_fname = pfilename + '_' + today + '_' + utc + 'Z.grib2' | ||
files = [f for f in os.listdir(inpath) if f == needed_fname] | ||
if not files: return | ||
|
||
tDay = datetime.datetime.strptime(today, "%Y%m%d") | ||
tvDay = tDay.strftime('%d%m%y') | ||
|
||
opath = os.path.join(outpath, today) | ||
createDirWhileParallelRacing(opath) | ||
|
||
g2filepath = os.path.join(opath, 'prg_' + today + pfileday + '.grib2') | ||
if __OVERWRITE__ and os.path.isfile(g2filepath): os.remove(g2filepath) | ||
|
||
wg2filename = 'prg%d%sz%s.grib2' % (day, utc.zfill(2), tvDay) | ||
wg2filepath = os.path.join(opath, wg2filename) | ||
if __OVERWRITE__ and os.path.isfile(wg2filepath): os.remove(wg2filepath) | ||
|
||
ensfpath = os.path.join(inpath, files[0]) | ||
inf = iris.load(ensfpath) | ||
for varName, varSTASH in neededVars: | ||
# define variable name constraint | ||
varConstraint = iris.Constraint(name=varName) | ||
print varName, varSTASH | ||
ensCube = inf.extract(varConstraint) | ||
if not ensCube: continue | ||
print "doing members average for", varName | ||
# do average over ensembles (1 control + 44 ensemble members) | ||
ensAvgCube = cubeRealizationAverager(ensCube[0]) | ||
# make memory free | ||
del ensCube | ||
|
||
if (varName, varSTASH) in _precipVars_: | ||
# DO NOT APPLY iris.analysis.Linear(extrapolation_mode='mask'), | ||
# which writes nan every where for the snowfall_flux, | ||
# rainfall_flux, precipitation_flux. So donot apply that. | ||
exmode = 'linear' | ||
else: | ||
# In general all the other variables should not be | ||
# extrapolated over masked grid points. | ||
exmode = 'mask' | ||
# end of if (...): | ||
|
||
scheme = iris.analysis.Linear(extrapolation_mode=exmode) | ||
try: | ||
# This lienar interpolate will do extra polate over ocean even | ||
# though original data doesnt have values over ocean and wise versa. | ||
# So lets be aware of this. | ||
regdCube = ensAvgCube.regrid(_targetGrid_, scheme) | ||
except Exception as e: | ||
print "ALERT !!! Error while regridding!! %s" % str(e) | ||
print " So skipping this without saving data" | ||
continue | ||
# end of try: | ||
|
||
# make memory free | ||
del ensAvgCube | ||
|
||
if (varName, varSTASH) in _precipVars_: | ||
# Since we are not using 'mask' option for extrapolate while | ||
# doing linear regrid, which bring -ve values after regrid in | ||
# extrapolated grids. So lets make it as 0 as minimum value. | ||
regdCube.data[regdCube.data < 0.0] = 0.0 | ||
# end of if (varName, varSTASH) in _precipVars_: | ||
|
||
if exmode == 'mask': | ||
regdCube.data = numpy.ma.masked_array(regdCube.data, | ||
dtype=numpy.float64, fill_value=9.999e+20) | ||
|
||
print regdCube | ||
# save into grib2 file | ||
iris.fileformats.grib.save_messages(tweaked_messages([regdCube]), | ||
g2filepath, append=True) # save grib2 file | ||
|
||
print "Appending %s to grib2 file" % varName | ||
# make memory free | ||
del regdCube | ||
# end of for varName, varSTASH in neededVars: | ||
|
||
# execute post wgrib2 command compression algorithm | ||
cmd = "%s %s %s %s" % (wgrib2, g2filepath, __wgrib2Arguments__, wg2filepath) | ||
print cmd | ||
subprocess.call(cmd, shell=True) | ||
time.sleep(10) | ||
# remove the grib2 file generated by IRIS | ||
os.remove(g2filepath) | ||
# rename g2filepath as wg2filepath | ||
g2filepath = wg2filepath | ||
print "Created grib2 file using wgrib2 command with compress arguments " | ||
|
||
# Conver grib2 to grib1 | ||
g1filepath = '.'.join(g2filepath.split('.')[:-1]) | ||
g1filepath = g1filepath if g1filepath else g2filepath[:-1] | ||
if __grib1FilesNameSuffix__: g1filepath += str(__grib1FilesNameSuffix__) | ||
|
||
if os.path.isfile(g1filepath): os.remove(g1filepath) | ||
|
||
cmd = [cnvgrib, '-g21', g2filepath, g1filepath] | ||
subprocess.call(cmd, shell=False) | ||
cmd = ['chmod', '644', g1filepath] | ||
subprocess.call(cmd, shell=False) | ||
print "Converted grib2 to grib1 file : -", g1filepath | ||
os.remove(g2filepath) | ||
# end of def createTarBalls(path, today, ...): | ||
|
||
if __name__ == '__main__': | ||
|
||
|
||
date = None | ||
inpath = None | ||
oftype = None | ||
utc = None | ||
outpath = '/gpfs3/home/umeps/EPS/ShortJobs/NCUM_EPS_VSDB_Input' | ||
|
||
helpmsg = './ncumeps_create_memavg_vsdb_input.py --date=20160302 --outpath=path --oftype=forecast --utc=00 --start_long_fcst_hour=24 --end_long_fcst_hour=24 --fcst_step_hour=24' | ||
try: | ||
opts, args = getopt.getopt(sys.argv[1:], "d:o:t:z:s:e:i", ["date=", | ||
"outpath=", "oftype=", "utc=", "start_long_fcst_hour=", "end_long_fcst_hour=", "fcst_step_hour="]) | ||
except getopt.GetoptError: | ||
print helpmsg | ||
sys.exit(2) | ||
for opt, arg in opts: | ||
if opt == '-h': | ||
print helpmsg | ||
sys.exit() | ||
elif opt in ("-d", "--date"): | ||
date = arg | ||
elif opt in ("-o", "--outpath"): | ||
inpath = arg | ||
elif opt in ("-t", "--oftype"): | ||
oftype = arg | ||
elif opt in ("-z", "--utc"): | ||
utc = arg | ||
elif opt in ("-s", "--start_long_fcst_hour"): | ||
start_long_fcst_hour = arg | ||
elif opt in ("-e", "--end_long_fcst_hour"): | ||
end_long_fcst_hour = arg | ||
elif opt in ("-i", "--fcst_step_hour"): | ||
fcst_step_hour = arg | ||
# end of for opt, arg in opts: | ||
|
||
# create tar balls only if forecast & utc is 00, otherwise skip it! | ||
if oftype == 'forecast' and utc == '00' and fcst_step_hour == '24': | ||
# pass the arg to function | ||
createENSavg_VSDB_Grib1Files(inpath, outpath, date, utc, start_long_fcst_hour, stephr=fcst_step_hour) | ||
# end of if oftype == 'forecast' and utc == '00': |
47 changes: 47 additions & 0 deletions
47
bsubScripts/ncumeps_global_post/ncumeps_global_post_anl_00Z.bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/bin/bash | ||
# | ||
#BSUB -a poe # set parallel operating environment | ||
#BSUB -J umeps2g2anl # job name | ||
#BSUB -W 06:00 # wall-clock time (hrs:mins) | ||
#BSUB -n 16 # number of tasks in job | ||
#BSUB -x | ||
#BSUB -R span[ptile=16] | ||
#BSUB -R rusage[mem=61440] | ||
#BSUB -q small # queue | ||
#BSUB -e /gpfs3/home/umeps/UMRiderLogs/post/bsub/um2grb2.anl.00hr.err.%J.%I.hybrid # error file name in which %J is replaced by the job ID | ||
#BSUB -o /gpfs3/home/umeps/UMRiderLogs/post/bsub/um2grb2.anl.00hr.out.%J.%I.hybrid # output file name in which %J is replaced by the job ID | ||
|
||
|
||
# find out the directory of this bash script after submitted to bsub | ||
DIR="$( cd "$( dirname "${BASH_SOURCE[1]}" )" && pwd )" | ||
|
||
# get the absolute path of the local table | ||
localTable_relative_dir="$DIR/../../tables/local/ncmr/v1/" | ||
localTable_absolute_dir="$( cd "$localTable_relative_dir" && pwd )" | ||
localTable=$localTable_absolute_dir/ncmr_grib2_local_table | ||
|
||
# get the absolute path of the script for forecast 00utc | ||
g2scripts_relative_dir="$DIR/../../g2scripts/" | ||
g2scripts_absolute_dir="$( cd "$g2scripts_relative_dir" && pwd )" | ||
g2script=$g2scripts_absolute_dir/umeps2grb2_fcst_00Z.py | ||
|
||
# export the configure paths to needed variables | ||
export UMRIDER_SETUP=$DIR/ncumeps_global_post_um2grb2_24hourly_setup.cfg ## yes, this is correct program. | ||
export UMRIDER_VARS=$DIR/ncumeps_global_post_um2grb2_anl_vars.cfg | ||
export GRIB2TABLE=$localTable | ||
|
||
echo "export UMRIDER_SETUP="$UMRIDER_SETUP | ||
echo "export UMRIDER_VARS="$UMRIDER_VARS | ||
echo "export GRIB2TABLE="$GRIB2TABLE | ||
|
||
# sourcing umtid_bashrc to load module python-uvcdat-iris! | ||
#source "$DIR/../umtid_bashrc" | ||
# execute the script | ||
#srun -n 1 -c 64 python $g2script | ||
|
||
export SHELL=/bin/bash | ||
|
||
hour=0 # 0 will produce 0th hour prognostic data (equivalent to analysis) | ||
echo "hour="${hour} | ||
|
||
python $g2script --start_long_fcst_hour=${hour} --end_long_fcst_hour=${hour} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
bsubScripts/ncumeps_global_post/ncumeps_global_post_um2grb2_anl_vars.cfg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
############################################################################## | ||
## vars configure file: Used for the purpose of um2grb2 conversion of only ## | ||
## needed NCUM model out variables. um2grb2 python parallel scripts will ## | ||
## create analysis and forecast files, by conveting to gri2 file only for ## | ||
## for the following cf_standard_name and varSTASH coded vars. ## | ||
## ## | ||
## Author : Arulalan <arulalan@ncmrwf.gov.in> ## | ||
## Updated : 18-Feb-2016 ## | ||
############################################################################## | ||
|
||
##### BEGIN OF UMRIDER VARS CONFIGURE FOR um2grb2 SCRIPTS ###### | ||
## Pressure Level Variable names & STASH codes | ||
|
||
('geopotential_height', 'm01s16i202') | ||
('relative_humidity', 'm01s16i256') | ||
('x_wind', 'm01s15i243') | ||
('y_wind', 'm01s15i244') | ||
('upward_air_velocity', 'm01s15i242') | ||
('air_temperature', 'm01s16i203') | ||
('air_pressure_at_sea_level', 'm01s16i222') | ||
('surface_air_pressure', 'm01s00i409') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.