Skip to content

Commit

Permalink
up-to-date
Browse files Browse the repository at this point in the history
  • Loading branch information
mammatus95 committed Apr 21, 2024
1 parent e9dc14b commit f0a44cf
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 198 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# ignore ALL .log files

.ipynb_checkpoints/
*/.ipynb_checkpoints/
**__pycache__

**.ipynb_checkpoints/

*/*.png
*/*/*.png
Expand All @@ -17,4 +18,4 @@
*/*/*.pyc

src/invariant
src/iconnest
src/modeldata
4 changes: 2 additions & 2 deletions src/download_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ source /etc/profile

conda activate HodographMaps

store_path=$(pwd)/iconnest
store_path=$(pwd)/modeldata

# create nwp directory and if not there a output images directory
mkdir -p ${store_path}
Expand Down Expand Up @@ -65,4 +65,4 @@ do
done

# remove nwp files
rm -rf ${store_path}
# rm -rf ${store_path}
97 changes: 55 additions & 42 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,28 @@
import utilitylib as ut
import plotlib
import modelinfolib as model
model = model.icon_nest

# ---------------------------------------------------------------------------------------------------------------------


def run(program_mode, fieldname, rundate, model_run, fp):
def run(model_obj, program_mode, fieldname, fp):
config = ut.load_yaml('config.yml')

if program_mode == "Test":
cape_fld, lats, lons = ut.open_gribfile_single(fieldname, rundate, model_run, fp, path="./iconnest/")
assert cape_fld.shape == (model.getnlat, model.getnlon), "Shape inconsistency"
plotlib.test_plot(cape_fld, lats, lons, fp, model_run, titel='CAPE')
cape_fld, lats, lons = ut.open_gribfile_single(model_obj, fieldname, fp, path="./modeldata/")
assert cape_fld.shape == (model_obj.getnlat(), model_obj.getnlon()), "Shape inconsistency"
plotlib.test_plot(model_obj, cape_fld, lats, lons, fp)

elif program_mode == "Sounding":
cape_fld, lats, lons = ut.open_gribfile_single(fieldname, rundate, model_run, fp, path="./iconnest/")
cape_fld, lats, lons = ut.open_gribfile_single(model_obj, fieldname, fp, path="./modeldata/")

steps = config["steps"]
nlvl = int(model.getnlev()/steps)
t_fld = np.empty(nlvl*model.getpoints()).reshape((nlvl, model.getnlat(), model.getnlon()))
nlvl = int(model_obj.getnlev()/steps)
t_fld = np.empty(nlvl*model_obj.getpoints()).reshape((nlvl, model_obj.getnlat(), model_obj.getnlon()))
t_fld.fill(np.nan)
q_fld = np.empty(nlvl*model.getpoints()).reshape((nlvl, model.getnlat(), model.getnlon()))
q_fld = np.empty(nlvl*model_obj.getpoints()).reshape((nlvl, model_obj.getnlat(), model_obj.getnlon()))
q_fld.fill(np.nan)
p_fld = np.empty(nlvl*model.getpoints()).reshape((nlvl, model.getnlat(), model.getnlon()))
p_fld = np.empty(nlvl*model_obj.getpoints()).reshape((nlvl, model_obj.getnlat(), model_obj.getnlon()))
p_fld.fill(np.nan)

if config["levels"][0] > config["levels"][1]:
Expand All @@ -44,24 +43,24 @@ def run(program_mode, fieldname, rundate, model_run, fp):

lvl_idx = 0
for level in range(config["levels"][0], config["levels"][1], steps):
t_fld[lvl_idx, :, :] = ut.open_gribfile_multi("T", level, rundate, model_run, fp, path="./iconnest/")
q_fld[lvl_idx, :, :] = ut.open_gribfile_multi("QV", level, rundate, model_run, fp, path="./iconnest/")
p_fld[lvl_idx, :, :] = ut.open_gribfile_multi("P", level, rundate, model_run, fp, path="./iconnest/")
t_fld[lvl_idx, :, :] = ut.open_gribfile_multi(model_obj, "T", level, fp, path="./modeldata/")
q_fld[lvl_idx, :, :] = ut.open_gribfile_multi(model_obj, "QV", level, fp, path="./modeldata/")
p_fld[lvl_idx, :, :] = ut.open_gribfile_multi(model_obj, "P", level, fp, path="./modeldata/")

lvl_idx += 1
if lvl_idx >= nlvl:
break

print(np.nanmean(t_fld, axis=(1, 2))-273.15)
plotlib.sounding_plot(cape_fld, t_fld, q_fld, p_fld, lats, lons, fp, model_run, titel='CAPE')
plotlib.sounding_plot(model_obj, cape_fld, t_fld, q_fld, p_fld, lats, lons, fp)
elif program_mode == "Basic":
cape_fld, lats, lons = ut.open_gribfile_single(fieldname, rundate, model_run, fp, path="./iconnest/")
cape_fld, lats, lons = ut.open_gribfile_single(model_obj, fieldname, fp, path="./modeldata/")

steps = config["steps"]
nlvl = int(model.getnlev()/steps)
u_fld = np.empty(nlvl*model.getpoints()).reshape((nlvl, model.getnlat(), model.getnlon()))
nlvl = int(model_obj.getnlev()/steps)
u_fld = np.empty(nlvl*model_obj.getpoints()).reshape((nlvl, model_obj.getnlat(), model_obj.getnlon()))
u_fld.fill(np.nan)
v_fld = np.empty(nlvl*model.getpoints()).reshape((nlvl, model.getnlat(), model.getnlon()))
v_fld = np.empty(nlvl*model_obj.getpoints()).reshape((nlvl, model_obj.getnlat(), model_obj.getnlon()))
v_fld.fill(np.nan)

if config["levels"][0] > config["levels"][1]:
Expand All @@ -72,30 +71,30 @@ def run(program_mode, fieldname, rundate, model_run, fp):

lvl_idx = 0
for level in range(config["levels"][0], config["levels"][1], steps):
u_fld[lvl_idx, :, :] = ut.open_gribfile_multi("U", level, rundate, model_run, fp, path="./iconnest/")
v_fld[lvl_idx, :, :] = ut.open_gribfile_multi("V", level, rundate, model_run, fp, path="./iconnest/")
u_fld[lvl_idx, :, :] = ut.open_gribfile_multi(model_obj, "U", level, fp, path="./modeldata/")
v_fld[lvl_idx, :, :] = ut.open_gribfile_multi(model_obj, "V", level, fp, path="./modeldata/")

lvl_idx += 1
if lvl_idx >= nlvl:
break

print(np.nanmean(u_fld, axis=(1, 2)))
plotlib.basic_plot(cape_fld, u_fld, v_fld, lats, lons, fp, model_run,
titel='CAPE', threshold=config["threshold"])
plotlib.basic_plot_custarea(cape_fld, u_fld, v_fld, lats, lons, fp, model_run,
titel='CAPE', threshold=config["threshold"])
plotlib.basic_plot(model_obj, cape_fld, u_fld, v_fld, lats, lons, fp,
threshold=config["threshold"])
plotlib.basic_plot_custarea(model_obj, cape_fld, u_fld, v_fld, lats, lons, fp,
threshold=config["threshold"])
elif program_mode == "Nixon":
cape_fld, lats, lons = ut.open_gribfile_single(fieldname, rundate, model_run, fp, path="./iconnest/")
cape_fld, lats, lons = ut.open_gribfile_single(model_obj, fieldname, fp, path="./modeldata/")

steps = config["steps"]
nlvl = int(model.getnlev()/steps)
u_fld = np.empty(nlvl*model.getpoints()).reshape((nlvl, model.getnlat(), model.getnlon()))
nlvl = int(model_obj.getnlev()/steps)
u_fld = np.empty(nlvl*model_obj.getpoints()).reshape((nlvl, model_obj.getnlat(), model_obj.getnlon()))
u_fld.fill(np.nan)
v_fld = np.empty(nlvl*model.getpoints()).reshape((nlvl, model.getnlat(), model.getnlon()))
v_fld = np.empty(nlvl*model_obj.getpoints()).reshape((nlvl, model_obj.getnlat(), model_obj.getnlon()))
v_fld.fill(np.nan)
h_fld = np.empty(nlvl*model.getpoints()).reshape((nlvl, model.getnlat(), model.getnlon()))
h_fld = np.empty(nlvl*model_obj.getpoints()).reshape((nlvl, model_obj.getnlat(), model_obj.getnlon()))
h_fld.fill(np.nan)
p_fld = np.empty(nlvl*model.getpoints()).reshape((nlvl, model.getnlat(), model.getnlon()))
p_fld = np.empty(nlvl*model_obj.getpoints()).reshape((nlvl, model_obj.getnlat(), model_obj.getnlon()))
p_fld.fill(np.nan)

if config["levels"][0] > config["levels"][1]:
Expand All @@ -106,10 +105,10 @@ def run(program_mode, fieldname, rundate, model_run, fp):

lvl_idx = 0
for level in range(config["levels"][0], config["levels"][1], steps):
u_fld[lvl_idx, :, :] = ut.open_gribfile_multi("U", level, rundate, model_run, fp, path="./iconnest/")
v_fld[lvl_idx, :, :] = ut.open_gribfile_multi("V", level, rundate, model_run, fp, path="./iconnest/")
h_fld[lvl_idx, :, :] = ut.open_gribfile_multi("H", level, rundate, model_run, fp, path="./iconnest/")
p_fld[lvl_idx, :, :] = ut.open_gribfile_multi("P", level, rundate, model_run, fp, path="./iconnest/")
u_fld[lvl_idx, :, :] = ut.open_gribfile_multi(model_obj, "U", level, fp, path="./modeldata/")
v_fld[lvl_idx, :, :] = ut.open_gribfile_multi(model_obj, "V", level, fp, path="./modeldata/")
h_fld[lvl_idx, :, :] = ut.open_gribfile_multi(model_obj, "H", level, fp, path="./modeldata/")
p_fld[lvl_idx, :, :] = ut.open_gribfile_multi(model_obj, "P", level, fp, path="./modeldata/")

lvl_idx += 1
if lvl_idx >= nlvl:
Expand All @@ -120,7 +119,7 @@ def run(program_mode, fieldname, rundate, model_run, fp):
du = np.subtract(u_fld[30, :, :], u_fld[0, :, :])
dv = np.subtract(v_fld[30, :, :], v_fld[0, :, :])
dls_fld = np.sqrt(np.add(np.square(du), np.square(dv)))
plotlib.nixon_proj(cape_fld, dls_fld, u_fld, v_fld, p_fld, h_fld, lats, lons, fp, model_run, imfmt="png")
plotlib.nixon_proj(model_obj, cape_fld, dls_fld, u_fld, v_fld, p_fld, h_fld, lats, lons, fp, imfmt="png")
else:
print("Wrong command line argument")
exit(-1)
Expand Down Expand Up @@ -151,6 +150,9 @@ def main():
parser.add_argument('-r', '--run', type=str,
help='Run (0,6,12,18,.. .etc)')

parser.add_argument('-m', '--model', type=str,
help='--model: ICON, IFS, or GFS')

# Parse the command line arguments
args = parser.parse_args()

Expand All @@ -161,20 +163,32 @@ def main():
print("Unknown field")
exit(-1)

if args.model is None:
model_obj = model.MODELIFNO("ICON EU", 1377, 657, 0.0625, 54, "pres")
elif "ICON" in args.model:
model_obj = model.icon_nest
# elif args.model == "IFS":
# model_obj = model.ifs
# elif args.model == "GFS":
# model_obj = model.gfs
else:
print("Unkown model! Exit.")
exit(0)

if args.date is None:
rundate = datetime.strptime(run_config["default_date"], "%Y-%m-%d")
model_obj.setrundate(datetime.strptime(run_config["default_date"], "%Y-%m-%d"))
else:
rundate = datetime.strptime(args.date, "%Y-%m-%d")
model_obj.setrundate(datetime.strptime(args.date, "%Y-%m-%d"))

if args.fp is None:
fp = run_config["fp"]
else:
fp = args.fp

if args.run is None:
model_run = run_config["run"]
model_obj.setrun(run_config["run"])
else:
model_run = args.run
model_obj.setrun(args.run)

if args.mode != "Test" and args.mode != "Sounding" and args.mode != "Basic" and args.mode != "Nixon":
print("Unknown Mode. Exit program.")
Expand All @@ -185,9 +199,8 @@ def main():
# replace space with underscores
fieldname = args.field.replace(" ", "_")

print(f"\nDate: {rundate}\n Arguments: {args} \nConfig-File: {config}\n\n")

run(program_mode, fieldname, rundate, model_run, fp)
print(f"\nArguments: {args}\n{model_obj}")
run(model_obj, program_mode, fieldname, fp)

# ---------------------------------------------------------------------------------------------------------------------

Expand Down
89 changes: 40 additions & 49 deletions src/modelinfolib.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,22 @@
#!/usr/bin/python3
"""
# ICON Nest
points = 904689
nlon = 1377
nlat = 657
nlev = 51
lonmin = -23.5
lonmax = 45.0
latmin = 29.5
latmax = 70.5
d_grad = 0.0625
# IFS
points = 405900
nlon = 900
nlat = 451
nlev =
lonmin = 0
lonmax = 359
latmin = -90
latmax = 90
d_grad = 0.4
# GFS
points = 1038240
nlon = 1440
nlat = 721
nlev =
lonmin = 0
lonmax = 359
latmin = -90
latmax = 90
d_grad = 0.25
"""
from datetime import datetime, date


class MODELIFNO:

def __init__(self, nlon, nlat, d_grad, nlev, levtyp):
def __init__(self, modelname, nlon, nlat, d_grad, nlev, levtyp):
self.modelname = modelname
self.points = nlon*nlat
self.nlon = nlon
self.nlat = nlat
self.nlev = nlev
self.d_grad = d_grad
self.levtyp = levtyp

"""
def __init__(self, nlon, nlat, nlev, lonmin, lonmax, latmin, latmax, d_grad):
self.points = nlon*nlat
self.nlon = nlon
self.nlat = nlat
self.nlev = nlev
self.d_grad = d_grad
print(lonmin, lonmax, latmin, latmax)
"""

self.run = -99
self.rundate = date.today()
self.hodo_interval_lat = range(272, 415, 12)
self.hodo_interval_lon = range(420, 670, 15)

def __str__(self):
return (
f"Model Information:\nPoints: {self.points}\n"
Expand All @@ -64,6 +25,33 @@ def __str__(self):
f"Number of Levels: {self.nlev}\tLeveltyps: {self.levtyp}\n"
)

def setrun(self, run):
if isinstance(run, int):
self.run = run
else:
self.run = int(run)

def setrundate(self, rundate):
if isinstance(rundate, datetime):
self.rundate = rundate
else:
self.rundate = datetime.strptime(rundate, "%Y-%m-%d")

def getrun(self):
return self.run

def getrundate(self):
return self.rundate

def getrundate_as_str(self, fmt="%Y%m%d"):
return self.rundate.strftime(fmt)

def getname(self):
return self.modelname

def getParamter(self):
return self.parlist

def getpoints(self):
return self.points

Expand All @@ -74,14 +62,17 @@ def getnlat(self):
return self.nlat

def getnlev(self):
return self.nlev
return int(self.nlev)

def getlevtyp(self):
return self.d_grad

def getd_grad(self):
return self.d_grad

def create_plottitle(self):
return f"Hodographmap of {self.modelname}"


# Example usage:
icon_nest = MODELIFNO(1377, 657, 0.0625, 54, "model") # lowest 74 and we download till 20
icon_nest = MODELIFNO("ICON Nest", 1377, 657, 0.0625, 54, "model") # lowest 74 and we download till 20
Loading

0 comments on commit f0a44cf

Please sign in to comment.