Skip to content

Commit

Permalink
apply Scott Paine fixes through March 29, 2021
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpus committed Feb 5, 2022
1 parent 4f25387 commit b9f6035
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
58 changes: 32 additions & 26 deletions eht_met_forecast/am.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
header_amc = '''
#
# This header is prepended to the atmospheric layers generated by
# gfs15_to_am10.py to generate a complete am configuration file.
# gfs16_to_am10.py to generate a complete am configuration file.
#
f 225 GHz 225 GHz 1 GHz
output f GHz tau Tb K
Expand Down Expand Up @@ -104,14 +104,12 @@ def grib2_to_am_layers(gribname, lat, lon, alt):
try:
x = (grid_interp(grbindx.select(
name="Relative humidity", level=lev)[0].values, u, v))
if (lev >= RH_TOP_PLEVEL):
RH.append(x)
else:
RH.append(0.0)
# Greg: this used to have 'if (lev >= RH_TOP_PLEVEL)' in gfs15_to_am
# now it throws the exception
RH.append(x)
except:
print('point 4', file=sys.stderr)
raise # XXX debug these bad gribs
RH.append(0.0)
print('point 4', file=sys.stderr)
try:
x = (grid_interp(grbindx.select(
name="Cloud mixing ratio", level=lev)[0].values, u, v))
Expand Down Expand Up @@ -163,21 +161,23 @@ def print_am_layers(alt, Pbase, z, T, o3_vmr, RH, cloud_lmr, cloud_imr):
T_mid = T[i]
if (o3_vmr_mid > 0.0):
print("column o3 vmr {0:.3e}".format(o3_vmr_mid))
if (RH_mid > 0.0):
if (T_mid > H2O_SUPERCOOL_LIMIT):
print("column h2o RH {0:.2f}%".format(RH_mid))
else:
if (Pbase[i] > RH_TOP_PLEVEL):
if (T_mid < H2O_SUPERCOOL_LIMIT):
print("column h2o RHi {0:.2f}%".format(RH_mid))
else:
print("column h2o RH {0:.2f}%".format(RH_mid))
else:
print("column h2o vmr {0:.3e}".format(STRAT_H2O_VMR))
if (cloud_lmr_mid > 0.0):
#
# Convert cloud liquid water mixing ratio [kg / kg] to
# cloud total liquid water across the layer [kg / m^2].
# Below the supercooling limit, assume any liquid water
# is really ice. (GFS 15 occasionally has numerically
# is really ice. (GFS 15 occasionally had numerically
# negligible amounts of liquid water at unphysically
# low temperature.)
#
dP = PASCAL_ON_MBAR * (Pbase[i] - Pbase[i-1])
dP = PASCAL_ON_MBAR * (Pbase[0] if i == 0 else Pbase[i] - Pbase[i-1])
m = dP / G_STD
ctw = m * cloud_lmr_mid
if (T_mid < H2O_SUPERCOOL_LIMIT):
Expand All @@ -189,7 +189,7 @@ def print_am_layers(alt, Pbase, z, T, o3_vmr, RH, cloud_lmr, cloud_imr):
# Convert cloud ice mixing ratio [kg / kg] to cloud total
# ice across the layer [kg / m^2].
#
dP = PASCAL_ON_MBAR * (Pbase[i] - Pbase[i-1])
dP = PASCAL_ON_MBAR * (Pbase[0] if i == 0 else Pbase[i] - Pbase[i-1])
m = dP / G_STD
cti = m * cloud_imr_mid
print("column iwp_abs_Rayleigh {0:.3e} kg*m^-2".format(cti))
Expand All @@ -198,18 +198,19 @@ def print_am_layers(alt, Pbase, z, T, o3_vmr, RH, cloud_lmr, cloud_imr):
if (z[i] == alt):
return

u = (alt - z[i-1]) / (z[i] - z[i-1])
u = (alt - z[i-1]) / (z[i] - z[i-1])
logP_s = u * math.log(Pbase[i]) + (1.0 - u) * math.log(Pbase[i-1])
P_s = math.exp(logP_s)
T_s = u * T[i] + (1.0 - u) * T[i-1]
P_s = math.exp(logP_s)
T_s = u * T[i] + (1.0 - u) * T[i-1]
T_mid = 0.5 * (T_s + T[i-1])

#
# Other variables are interpolated or extrapolated linearly in P
# to the base level and clamped at zero.
#
u = (P_s - Pbase[i-1]) / (Pbase[i] - Pbase[i-1])
o3_vmr_s = u * o3_vmr[i] + (1.0 - u) * o3_vmr[i-1]
RH_s = u * RH[i] + (1.0 - u) * RH[i-1]
o3_vmr_s = u * o3_vmr[i] + (1.0 - u) * o3_vmr[i-1]
RH_s = u * RH[i] + (1.0 - u) * RH[i-1]
cloud_lmr_s = u * cloud_lmr[i] + (1.0 - u) * cloud_lmr[i-1]
cloud_imr_s = u * cloud_imr[i] + (1.0 - u) * cloud_imr[i-1]
if (o3_vmr_s < 0.0):
Expand All @@ -230,18 +231,23 @@ def print_am_layers(alt, Pbase, z, T, o3_vmr, RH, cloud_lmr, cloud_imr):
print("column dry_air vmr")
if (o3_vmr_mid > 0.0):
print("column o3 vmr {0:.3e}".format(o3_vmr_mid))
if (RH_mid > 0.0):
if (T_mid > H2O_SUPERCOOL_LIMIT):
print("column h2o RH {0:.2f}%".format(RH_mid))
else:
if (P_s > RH_TOP_PLEVEL):
if (T_mid < H2O_SUPERCOOL_LIMIT):
print("column h2o RHi {0:.2f}%".format(RH_mid))
else:
print("column h2o RH {0:.2f}%".format(RH_mid))
else:
print("column h2o vmr {0:.3e}".format(STRAT_H2O_VMR))
if (cloud_lmr_mid > 0.0):
dP = PASCAL_ON_MBAR * (Pbase[i] - Pbase[i-1])
dP = PASCAL_ON_MBAR * (Pbase[0] if i == 0 else Pbase[i] - Pbase[i-1])
m = dP / G_STD
ctw = m * cloud_lmr_mid
print("column lwp_abs_Rayleigh {0:.3e} kg*m^-2".format(ctw))
if (T_mid < H2O_SUPERCOOL_LIMIT):
print("column iwp_abs_Rayleigh {0:.3e} kg*m^-2".format(ctw))
else:
print("column lwp_abs_Rayleigh {0:.3e} kg*m^-2".format(ctw))
if (cloud_imr_mid > 0.0):
dP = PASCAL_ON_MBAR * (Pbase[i] - Pbase[i-1])
dP = PASCAL_ON_MBAR * (Pbase[0] if i == 0 else Pbase[i] - Pbase[i-1])
m = dP / G_STD
cti = m * cloud_imr_mid
print("column iwp_abs_Rayleigh {0:.3e} kg*m^-2".format(cti))
Expand Down
15 changes: 12 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,23 @@ def content_403_second(request, context):


def test_cli(capsys):

# AM 11.0 gfs15_to_am
#stdout = '00:00 7.6246e-02 2.3999e+01 1.4787e+00 0.0000e+00 0.0000e+00 2.7655e+02\n'
# AM 11.0 gfs16_to_am and bugfixes 2/5/22
stdout = '00:00 7.6247e-02 2.3999e+01 1.4794e+00 0.0000e+00 0.0000e+00 2.7655e+02\n'
# AM 12.0 gfs16 is the same

stdout = '20200316+18:' + stdout

tests = [
[
{'contentf': 'test.grb', 'args': ['--vex', 'Mm', '--one', '--stdout']},
{'stdout': '20200316_18:00:00 7.6246e-02 2.3999e+01 1.4787e+00 0.0000e+00 0.0000e+00 2.7655e+02\n'}
{'stdout': stdout}
],
[
{'contentf': 'test.grb', 'args': ['--vex', 'Mm', '--one', '--stdout', '--log', 'LOG.pytest']},
{'stdout': '20200316_18:00:00 7.6246e-02 2.3999e+01 1.4787e+00 0.0000e+00 0.0000e+00 2.7655e+02\n', 'ofile': 'LOG.pytest'}
{'stdout': stdout, 'ofile': 'LOG.pytest'}
],
[
{'status_code': 500, 'whack_timeouts': True, 'exception': SystemExit,
Expand All @@ -50,7 +59,7 @@ def test_cli(capsys):
[
{'contentc': content_403_second, 'status_code': 403, 'whack_timeouts': True,
'args': ['--vex', 'Mm', '--one', '--stdout']},
{'stdout': '20200316_18:00:00 7.6246e-02 2.3999e+01 1.4787e+00 0.0000e+00 0.0000e+00 2.7655e+02\n'},
{'stdout': stdout},
],
]

Expand Down

0 comments on commit b9f6035

Please sign in to comment.