Skip to content

Commit

Permalink
Update bufr.py for carra2 for local observations (#35)
Browse files Browse the repository at this point in the history
* Added features in bufr.py for carra2 to be able to read local observations

* Again, added features in bufr.py for carra2 to be able to read local observations

* Do black and remove unused import

* Added features in bufr.py for carra2 to be able to read local observations

* Further updates to bufr.py

* Reformatted pysurfex/bufr.py

---------

Co-authored-by: Trygve Aspelien <[email protected]>
  • Loading branch information
PerDahlgren and trygveasp authored Jan 24, 2024
1 parent ce967d9 commit 4919267
Showing 1 changed file with 48 additions and 11 deletions.
59 changes: 48 additions & 11 deletions pysurfex/bufr.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def __init__(
"/heightOfSensorAboveLocalGroundOrDeckOfMarinePlatform=1.5"
"/dewpointTemperature"
)
keys.append(
"/heightOfSensorAboveLocalGroundOrDeckOfMarinePlatform=2"
"/relativeHumidity"
)
elif var == "airTemperatureAt2M":
keys.append("airTemperatureAt2M")
keys.append(
Expand Down Expand Up @@ -171,7 +175,9 @@ def __init__(
stid = "NA"
station_number = -1
block_number = -1
site_name = "NA"
t2m = np.nan
rh2m = np.nan
td2m = np.nan
s_d = np.nan
temp = np.nan
Expand Down Expand Up @@ -223,6 +229,8 @@ def __init__(
station_number = val
if key == "blockNumber":
block_number = val
if key == "stationOrSiteName":
site_name = str(val)
if key == "airTemperatureAt2M":
t2m = val
if (
Expand All @@ -234,6 +242,12 @@ def __init__(
"/airTemperature"
):
temp = val
if (
key
== "/heightOfSensorAboveLocalGroundOrDeckOfMarinePlatform=2"
"/relativeHumidity"
):
rh2m = val
if key == "dewpointTemperatureAt2M":
td2m = val
if (
Expand Down Expand Up @@ -279,24 +293,37 @@ def __init__(
if not exists:
logging.debug("Pos does not exist %s %s", pos, var)
if var == "relativeHumidityAt2M":
if not np.isnan(t2m) and not np.isnan(td2m):
if (
not np.isnan(t2m)
and not np.isnan(td2m)
and np.isnan(rh2m)
):
try:
value = self.td2rh(td2m, t2m)
value = value * 0.01
except Exception:
logging.debug("Got exception for %s:", var)
value = np.nan
elif (
not np.isnan(temp)
and not np.isnan(t_d)
and np.isnan(rh2m)
):
try:
value = self.td2rh(t_d, temp)
value = value * 0.01
except Exception:
logging.debug(
"Got exception for %s",
var,
)
value = np.nan
else:
if not np.isnan(temp) and not np.isnan(t_d):
try:
value = self.td2rh(t_d, temp)
value = value * 0.01
except Exception:
logging.debug(
"Got exception for %s",
var,
)
value = np.nan
value = np.nan

if np.isnan(value) and not np.isnan(rh2m):
value = 0.01 * rh2m

elif var == "airTemperatureAt2M":
if np.isnan(t2m):
if not np.isnan(temp):
Expand All @@ -307,6 +334,8 @@ def __init__(
value = s_d
elif var == "heightOfBaseOfCloud":
value = c_b
elif var == "stationOrSiteName":
site_name = site_name
else:
raise NotImplementedError(
f"Var {var} is not coded! Please do it!"
Expand Down Expand Up @@ -385,6 +414,14 @@ def __init__(
)
if station_number > 0 and block_number > 0:
stid = str((block_number * 1000) + station_number)

if (
stid == "NA"
and site_name != "NA"
and site_name.isnumeric()
):
stid = site_name

observations.append(
Observation(
obs_dtg,
Expand Down

0 comments on commit 4919267

Please sign in to comment.