Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #293

Closed
wants to merge 127 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
127 commits
Select commit Hold shift + click to select a range
64b6aa2
Revert "revert"
BaptisteVandecrux Jun 4, 2024
df5a9c5
correcting typo
BaptisteVandecrux Jun 4, 2024
8b4d430
aws process handling modified
PennyHow Jun 7, 2024
f8de75c
Level 2 processing capabilities added to get_l3
PennyHow Jun 7, 2024
17245f7
get_l3 renamed to l2_to_l3
PennyHow Jun 7, 2024
cc6bfb2
l2_to_l3 functionality from pypromice
PennyHow Jun 7, 2024
09f12a0
minor de-bug for process test to run
PennyHow Jun 7, 2024
17b647c
Update .gitignore
BaptisteVandecrux Jun 10, 2024
5927dbb
L2 split from L3 CLI processing
PennyHow Jun 10, 2024
b9a2e70
unit tests moved to separate module
PennyHow Jun 10, 2024
bfb1603
file writing functions moved to separate module
PennyHow Jun 10, 2024
eb58371
Loading functions moved to separate module
PennyHow Jun 10, 2024
f536e74
Handling and reformating functions moved
PennyHow Jun 10, 2024
9b2d8f4
resampling functions moved
PennyHow Jun 10, 2024
2e98bfd
aws module updated with structure changes
PennyHow Jun 10, 2024
526f0f0
Logging package typo
PennyHow Jun 10, 2024
de7e5d8
de-bugging
PennyHow Jun 10, 2024
e9bcc8c
get_l2 and l2_to_l3 process test added
PennyHow Jun 10, 2024
496ef03
data prep and write function moved out of AWS class
PennyHow Jun 10, 2024
facf0ee
actions de-bug
PennyHow Jun 10, 2024
bd8d8a5
stations for testing changed
PennyHow Jun 10, 2024
679aa4f
l2 action de-bug
PennyHow Jun 10, 2024
e5db5d6
loop de-bug in l2 test action
PennyHow Jun 10, 2024
a6911f0
out filepath changed
PennyHow Jun 10, 2024
ba1a424
Path alteration 2
PennyHow Jun 10, 2024
0ae13cd
poutput directory alteration 3
PennyHow Jun 10, 2024
6a9b66b
action stages merged
PennyHow Jun 10, 2024
eaa49fd
get_l2tol3 action test skipped
PennyHow Jun 10, 2024
e0d93b5
typo fix in join_levels.py
BaptisteVandecrux Jun 11, 2024
a6e8807
further debugging
BaptisteVandecrux Jun 11, 2024
1a406f4
creating folder before writing files, writing hourly daily monthly fi…
BaptisteVandecrux Jun 11, 2024
697fa60
typo fix
BaptisteVandecrux Jun 12, 2024
b9fb365
fixed daily output file name
BaptisteVandecrux Jun 12, 2024
0607d31
update get_l3 to add historical data
BaptisteVandecrux Jun 4, 2024
fe7149c
update get_l3 to add historical data
BaptisteVandecrux Jun 4, 2024
9d0cb14
resampling frequency specified
PennyHow Jun 12, 2024
8f9e69d
Create get_l3_new.py
BaptisteVandecrux Jun 12, 2024
59cfbf0
modified file filter added
PennyHow Jun 12, 2024
fdb6b02
reverts changes on get_l2.py to write to file both 10min and hourly data
BaptisteVandecrux Jun 12, 2024
3bd0435
renamed join_levels to join_l2 because join_l3 will have different me…
BaptisteVandecrux Jun 12, 2024
2cebff4
skipping resample after join_l2, fixed setup.py for join_l2
BaptisteVandecrux Jun 12, 2024
cb1f6a3
fixing test
BaptisteVandecrux Jun 13, 2024
d1d46b7
fixed function names
BaptisteVandecrux Jun 13, 2024
e914403
update get_l3 to add historical data
BaptisteVandecrux Jun 4, 2024
a8b37ec
update get_l3 to add historical data
BaptisteVandecrux Jun 4, 2024
e276064
Create get_l3_new.py
BaptisteVandecrux Jun 12, 2024
50ef93e
Merge branch 'add-historical-data-to-l3' of https://github.com/GEUS-G…
BaptisteVandecrux Jun 13, 2024
ef4ae44
further work on join_l3, varible_aliases in ressource folder
BaptisteVandecrux Jun 13, 2024
b0ceb91
cleaning up debug code in join_l3
BaptisteVandecrux Jun 13, 2024
a2fa829
small fix in join_l3
BaptisteVandecrux Jun 13, 2024
4533b7f
working verion
BaptisteVandecrux Jun 13, 2024
320ab52
delete encoding info after reading netcdf, debug of getColNames
BaptisteVandecrux Jun 14, 2024
1499e5d
delete get_l3.py
BaptisteVandecrux Jun 14, 2024
efbe35f
removing variables and output files metadata
BaptisteVandecrux Jun 14, 2024
28160e0
new variable description files
BaptisteVandecrux Jun 14, 2024
a847958
added back ressource files, use level attributes for output definition
BaptisteVandecrux Jun 14, 2024
569b8bd
make list of sites from station_config, switched print to logger.info
BaptisteVandecrux Jun 15, 2024
22b88dd
removing get_l3, remove inst. values from averaged files, fixes on lo…
BaptisteVandecrux Jun 15, 2024
a42c814
Updates to numpy dependency version and pandas deprecation warnings (…
PennyHow Jun 17, 2024
96e6674
small changes following review, restored variable.csv history
BaptisteVandecrux Jun 17, 2024
34643df
buiding a station list instead of a station_dict
BaptisteVandecrux Jun 18, 2024
35ea174
renamed l3m to l3_merged, reintroduced getVars and getMeta
BaptisteVandecrux Jun 18, 2024
ce802a7
moving gcnet_postprocessing as part of readNead
BaptisteVandecrux Jun 18, 2024
5e7673b
sorting out the station_list in reverse chronological order
BaptisteVandecrux Jun 18, 2024
1272a7b
using tilde notation in setup.py
BaptisteVandecrux Jun 18, 2024
d445a45
better initialisation of station_attributes attribute
BaptisteVandecrux Jun 18, 2024
7756f30
moved addMeta, addVars, roundValues, reformatTime, reformatLon to wri…
BaptisteVandecrux Jun 19, 2024
48817e6
Inline comment describing enocding attribute removal when reading a n…
BaptisteVandecrux Jun 19, 2024
a49de43
loading toml file as dictionary within join_l3
BaptisteVandecrux Jun 19, 2024
48f6823
ressources renamed to resources (#261)
PennyHow Jun 19, 2024
79a5972
using project attribute of a station locate AWS file and specify whet…
BaptisteVandecrux Jun 19, 2024
ba8cd10
Merge branch 'add-historical-data-to-l3' of https://github.com/GEUS-G…
BaptisteVandecrux Jun 19, 2024
a184277
update test after moving addVars and addMeta
BaptisteVandecrux Jun 19, 2024
a2fe858
fixed logger message in resample
BaptisteVandecrux Jun 19, 2024
bd1983c
better definition of monthly sample rates in addMeta
BaptisteVandecrux Jun 19, 2024
036ba78
dummy datasaet built in unit test now has 'level' attribute
BaptisteVandecrux Jun 19, 2024
9d56d84
not storing timestamp_max for each station but pulling the info direc…
BaptisteVandecrux Jun 19, 2024
1263d7f
removing unecessary import of addMeta, roundValues
BaptisteVandecrux Jun 19, 2024
8ca65a8
make CLI scripts usable within python
BaptisteVandecrux Jun 24, 2024
d25d8cb
return result in join_l2 and join_l3
BaptisteVandecrux Jun 24, 2024
b65d0cc
removing args from join_l2 function
BaptisteVandecrux Jun 24, 2024
c7ddb4e
Merge pull request #255 from GEUS-Glaciology-and-Climate/250-make-a-f…
BaptisteVandecrux Jun 26, 2024
b82b586
Merge pull request #252 from GEUS-Glaciology-and-Climate/add-historic…
BaptisteVandecrux Jun 26, 2024
0dcac0c
proper removal of encoding info when reading netcdf
BaptisteVandecrux Jun 27, 2024
f79edbf
Merge pull request #262 from GEUS-Glaciology-and-Climate/make-python-…
BaptisteVandecrux Jun 28, 2024
cd42bb3
Refactored and Organized Test Modules
ladsmund Jun 25, 2024
76ffa55
Limited the ci tests to only run e2e
ladsmund Jun 28, 2024
44f0d3c
naming conventions changed
PennyHow Jun 28, 2024
2eb7f79
Feature/smoothing and extrapolating gps coordinates (#268)
BaptisteVandecrux Jul 5, 2024
63961be
Updated persistence.py to use explicit variable thresholds
ladsmund Aug 8, 2024
9db5b44
Fixed bug in persistence QC where initial repetitions were ignored
ladsmund Aug 8, 2024
37527b1
Updated python version in unittest
ladsmund Aug 8, 2024
37d671f
Fixed bug in get_bufr
ladsmund Jun 21, 2024
25d41db
Added __all__ to get_bufr.py
ladsmund Jun 11, 2024
8952dd1
Applied black code formatting
ladsmund Jun 21, 2024
d93f185
Made bufr_to_csv as cli script in setup.py
ladsmund Jun 11, 2024
8f371d0
Added script to recreate bufr files
ladsmund Jun 11, 2024
b6281b3
Updated tests parameters
ladsmund Jun 11, 2024
95c0d0c
Added test for missing data in get_bufr
ladsmund Jun 21, 2024
e6c9023
Updated get_bufr to support static GPS heights.
ladsmund Jun 21, 2024
46bf574
Updated github/workflow to run unittests
ladsmund Jun 28, 2024
1bc04bb
Updated get_bufr to support station config files in folder
ladsmund Jul 8, 2024
485cfa8
Updated BUFRVariables with scales and descriptions
ladsmund Jul 9, 2024
06ec9f3
Increased the real_time_utilities rounding precisions
ladsmund Jul 9, 2024
35c0662
Updated get_bufr to separate station position from bufr
ladsmund Jul 18, 2024
18dde7f
Minor cleanup
ladsmund Aug 7, 2024
891a408
Updated StationConfiguration IO to handle unknown attributes from input
ladsmund Aug 7, 2024
09950de
Updated docstring in create_bufr_files.py
ladsmund Aug 12, 2024
cba8fd9
Renamed e2e unittest methods
ladsmund Aug 15, 2024
777c5e8
Feature/surface heights and thermistor depths (#278)
BaptisteVandecrux Aug 15, 2024
232da0f
better adjustment of surface height in join_l3, also adjusting z_ice_…
BaptisteVandecrux Aug 19, 2024
17c5946
different decoding of GPS data if "L" is in GPS string (#288)
BaptisteVandecrux Aug 19, 2024
6977336
Updated pressure field for BUFR output files
ladsmund Aug 14, 2024
f92f1cc
Updated get_l2 to use aws.vars and aws.meta
ladsmund Aug 16, 2024
eb0472e
Removed static processing level attribute from file_attributes
ladsmund Aug 16, 2024
41b7cc2
Run black on write.py
ladsmund Aug 16, 2024
c187642
Implemented alternative helper functions for reading variables and me…
ladsmund Aug 16, 2024
6473cb7
Select format from multiple L0 input files
ladsmund Aug 16, 2024
d0959e9
Updated attribute metadata
ladsmund Aug 16, 2024
5b9601a
Updated test_process with full pipeline test
ladsmund Aug 17, 2024
314d63b
Removed station configuration generation
ladsmund Aug 19, 2024
314a902
Renamed folder name in temporaty test directory
ladsmund Aug 19, 2024
3824c67
Added data issues repository path as an explicit parameter to AWS
ladsmund Aug 19, 2024
1e19ace
Applied black on join_l3
ladsmund Aug 19, 2024
11a9704
Updated join_l3 to generate source attribute for sites
ladsmund Aug 19, 2024
3357e62
job name changed
PennyHow Aug 19, 2024
b29958a
Bugfix/passing adj dir to l3 processing plus attribute fix (#292)
BaptisteVandecrux Aug 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions .github/workflows/process_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: "3.8"
python-version: "3.10"
- name: Checkout repo
uses: actions/checkout@v3
with:
Expand All @@ -31,14 +31,26 @@ jobs:
run: |
cd $GITHUB_WORKSPACE
git clone --depth 1 https://oauth2:${{ env.GITLAB_TOKEN }}@geusgitlab.geus.dk/glaciology-and-climate/promice/aws-l0.git
- name: Run data processing
- name: Run L0 to L2 processing
env:
TEST_STATION: KPC_U CEN2 JAR
TEST_STATION: KAN_U HUM
shell: bash
run: |
mkdir $GITHUB_WORKSPACE/out/
mkdir $GITHUB_WORKSPACE/out/L0toL2/
mkdir $GITHUB_WORKSPACE/data_issues
for i in $(echo ${{ env.TEST_STATION }} | tr ' ' '\n'); do
python3 $GITHUB_WORKSPACE/main/src/pypromice/process/get_l3.py -v $GITHUB_WORKSPACE/main/src/pypromice/process/variables.csv -m $GITHUB_WORKSPACE/main/src/pypromice/process/metadata.csv -c $GITHUB_WORKSPACE/aws-l0/raw/config/$i.toml -i $GITHUB_WORKSPACE/aws-l0/raw -o $GITHUB_WORKSPACE/out/
python3 $GITHUB_WORKSPACE/main/src/pypromice/process/get_l2.py -c $GITHUB_WORKSPACE/aws-l0/tx/config/$i.toml -i $GITHUB_WORKSPACE/aws-l0/tx --issues $GITHUB_WORKSPACE/data_issues -o $GITHUB_WORKSPACE/out/L0toL2/ --data_issues_path $GITHUB_WORKSPACE/data_issues
done
- name: Run L2 to L3 processing
env:
TEST_STATION: KAN_U HUM
shell: bash
run: |
mkdir $GITHUB_WORKSPACE/out/L2toL3/
for i in $(echo ${{ env.TEST_STATION }} | tr ' ' '\n'); do
echo ${i}_hour.nc
python3 $GITHUB_WORKSPACE/main/src/pypromice/process/get_l2tol3.py -c $GITHUB_WORKSPACE/aws-l0/metadata/station_configurations/ -i $GITHUB_WORKSPACE/out/L0toL2/${i}/${i}_hour.nc -o $GITHUB_WORKSPACE/out/L2toL3/ --data_issues_path $GITHUB_WORKSPACE/data_issues
done
- name: Upload test output
uses: actions/upload-artifact@v3
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ on:
workflow_dispatch:

jobs:
build:
test:
name: unit_test
runs-on: ubuntu-latest
strategy:
matrix:
python_version: ['3.8','3.9','3.10']
python_version: ['3.10', '3.11']
steps:
- name: Install Python
uses: actions/setup-python@v4
Expand All @@ -19,6 +19,9 @@ jobs:
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Install eccodes
run : |
sudo apt-get install -y libeccodes-dev
- name: Install dependencies
shell: bash
run: |
Expand All @@ -30,4 +33,4 @@ jobs:
- name: Run unit tests
shell: bash
run: |
python3 -m unittest discover pypromice
python3 -m unittest discover tests
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include src/pypromice/test/*
include src/pypromice/resources/*
16 changes: 10 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pypromice",
version="1.3.6",
version="1.4.0",
author="GEUS Glaciology and Climate",
description="PROMICE/GC-Net data processing toolbox",
long_description=long_description,
Expand All @@ -31,21 +31,25 @@
packages=setuptools.find_packages(where="src"),
python_requires=">=3.8",
package_data={
"pypromice.process": ["metadata.csv", "variables.csv"],
"pypromice.tx": ["payload_formats.csv", "payload_types.csv"],
"pypromice.qc.percentiles": ["thresholds.csv"],
"pypromice.postprocess": ["station_configurations.toml", "positions_seed.csv"],
"pypromice.postprocess": ["positions_seed.csv"],
},
install_requires=['numpy>=1.23.0', 'pandas>=1.5.0', 'xarray>=2022.6.0', 'toml', 'scipy>=1.9.0', 'Bottleneck', 'netcdf4', 'pyDataverse', 'eccodes','scikit-learn>=1.1.0'],
install_requires=['numpy~=1.23', 'pandas>=1.5.0', 'xarray>=2022.6.0', 'toml', 'scipy>=1.9.0', 'Bottleneck', 'netcdf4', 'pyDataverse==0.3.1', 'eccodes', 'scikit-learn>=1.1.0'],
# extras_require={'postprocess': ['eccodes','scikit-learn>=1.1.0']},
entry_points={
'console_scripts': [
'get_promice_data = pypromice.get.get_promice_data:get_promice_data',
'get_l0tx = pypromice.tx.get_l0tx:get_l0tx',
'get_l3 = pypromice.process.get_l3:get_l3',
'join_l3 = pypromice.process.join_l3:join_l3',
'join_l2 = pypromice.process.join_l2:main',
'join_l3 = pypromice.process.join_l3:main',
'get_l2 = pypromice.process.get_l2:main',
'get_l2tol3 = pypromice.process.get_l2tol3:main',
'make_metadata_csv = pypromice.postprocess.make_metadata_csv:main',
'get_watsontx = pypromice.tx.get_watsontx:get_watsontx',
'get_bufr = pypromice.postprocess.get_bufr:main',
'create_bufr_files = pypromice.postprocess.create_bufr_files:main',
'bufr_to_csv = pypromice.postprocess.bufr_to_csv:main',
'get_msg = pypromice.tx.get_msg:get_msg'
],
},
Expand Down
7 changes: 6 additions & 1 deletion src/pypromice/postprocess/bufr_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

from pypromice.postprocess.bufr_utilities import read_bufr_file

if __name__ == "__main__":

def main():
parser = argparse.ArgumentParser("BUFR to CSV converter")
parser.add_argument("path", type=Path)
args = parser.parse_args()

print(read_bufr_file(args.path).to_csv())


if __name__ == "__main__":
main()
109 changes: 91 additions & 18 deletions src/pypromice/postprocess/bufr_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def round(value: float):

return round


# Enforce precision
# Note the sensor accuracies listed here:
# https://essd.copernicus.org/articles/13/3819/2021/#section8
Expand All @@ -64,28 +65,82 @@ class BUFRVariables:
* heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformWSPD: Corresponds to "#7#heightOfSensorAboveLocalGroundOrDeckOfMarinePlatform" which is height if anemometer relative to ground or deck of marine platform.

"""
wmo_id: str

# Station type: "mobile" or "land"
# ===============================
# Fixed land station schema: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_D/307080
# Mobile station schema: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_D/307090

station_type: str

# WMO station identifier
# Land stations: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_D/301090
# Mobile stations: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_D/301092
# ======================================================================================================
wmo_id: str
timestamp: datetime.datetime
relativeHumidity: float = attrs.field(converter=round_converter(0))
airTemperature: float = attrs.field(converter=round_converter(1))
pressure: float = attrs.field(converter=round_converter(1))
windDirection: float = attrs.field(converter=round_converter(0))
windSpeed: float = attrs.field(converter=round_converter(1))
latitude: float = attrs.field(converter=round_converter(6))
longitude: float = attrs.field(converter=round_converter(6))

# https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/005001
# Scale: 5, unit: degrees
# TODO: Test if eccodes does the rounding as well. The rounding is was 6 which is larger that the scale.
latitude: float = attrs.field(converter=round_converter(5))
# https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/006001
# Scale: 5, unit: degrees
longitude: float = attrs.field(converter=round_converter(5))

# https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/007030
# Scale: 1, unit: m
heightOfStationGroundAboveMeanSeaLevel: float = attrs.field(
converter=round_converter(2)
converter=round_converter(1)
)
#
# https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/007031
# Scale: 1, unit: m
heightOfBarometerAboveMeanSeaLevel: float = attrs.field(
converter=round_converter(2),
converter=round_converter(1),
)

# Pressure information
# ====================
# Definition table: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_D/302031
# https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/010004
# Scale: -1, unit: Pa
nonCoordinatePressure: float = attrs.field(converter=round_converter(-1))
# There are two other pressure variables in the template: 007004 - pressure and 010062 24-hour pressure change

# Basic synoptic "instantaneous" data
# ===================================
# Definition table: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_D/302035
# This section only include the temperature and humidity data (302032).
# Precipitation and cloud data are currently ignored.
# https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/007032
# Scale: 2, unit: m
# This is the first appearance of this variable id.
heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformTempRH: float = attrs.field(
converter=round_converter(4),
converter=round_converter(2),
)
# https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/012101
# Scale: 2, unit: K
airTemperature: float = attrs.field(converter=round_converter(2))
# There is also a Dewpoint temperature in this template: 012103 which is currently unused.
# https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/012103
# Scale: 0, unit: %
relativeHumidity: float = attrs.field(converter=round_converter(0))

# Basic synoptic "period" data
# ============================
# Definition table: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_D/302043
# Wind data: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_D/302042
# Wind direction: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/011001
# Scale: 0, unit: degrees
windDirection: float = attrs.field(converter=round_converter(0))
# Wind speed: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/011002
# Scale: 1, unit: m/s
windSpeed: float = attrs.field(converter=round_converter(1))
# https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/007032
# Scale: 2, unit: m
# This is the 7th appearance of this variable id.
heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformWSPD: float = attrs.field(
converter=round_converter(4)
converter=round_converter(2)
)

def as_series(self) -> pd.Series:
Expand Down Expand Up @@ -129,6 +184,7 @@ def __eq__(self, other: "BUFRVariables"):

BUFR_TEMPLATES = {
"mobile": {
# Template definition: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_D/307090
"unexpandedDescriptors": (307090), # message template, "synopMobil"
"edition": 4, # latest edition
"masterTableNumber": 0,
Expand All @@ -144,6 +200,7 @@ def __eq__(self, other: "BUFRVariables"):
"compressedData": 0,
},
"land": {
# Template definition: https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_D/307080
"unexpandedDescriptors": (307080), # message template, "synopLand"
"edition": 4, # latest edition
"masterTableNumber": 0,
Expand Down Expand Up @@ -246,6 +303,11 @@ def set_station(ibufr, station_type: str, wmo_id: str):
elif station_type == "land":
# StationNumber for land stations are integeres
wmo_id_int = int(wmo_id)
if wmo_id_int >= 1024:
raise ValueError(
f"Invalid WMO ID {wmo_id}. Land station number must be less than 1024."
"See https://vocabulary-manager.eumetsat.int/vocabularies/BUFR/WMO/32/TABLE_B/001002"
)
station_config = dict(stationNumber=wmo_id_int)
else:
raise Exception(f"Unsupported station station type {station_type}")
Expand Down Expand Up @@ -280,7 +342,7 @@ def set_AWS_variables(

set_bufr_value(ibufr, "relativeHumidity", variables.relativeHumidity)
set_bufr_value(ibufr, "airTemperature", variables.airTemperature)
set_bufr_value(ibufr, "pressure", variables.pressure)
set_bufr_value(ibufr, "nonCoordinatePressure", variables.nonCoordinatePressure)
set_bufr_value(ibufr, "windDirection", variables.windDirection)
set_bufr_value(ibufr, "windSpeed", variables.windSpeed)

Expand Down Expand Up @@ -372,7 +434,7 @@ def get_bufr_value(msgid: int, key: str) -> float:
raise ValueError(f"Unsupported BUFR value type {type(value)} for key {key}")


def read_bufr_message(fp: BinaryIO) -> Optional[BUFRVariables]:
def read_bufr_message(fp: BinaryIO, backwards_compatible: bool = False) -> Optional[BUFRVariables]:
"""
Read and parse BUFR message from binary IO stream.

Expand All @@ -383,6 +445,8 @@ def read_bufr_message(fp: BinaryIO) -> Optional[BUFRVariables]:
----------
fp
Readable binary io stream
backwards_compatible
Use legacy pressure if nonCoordinatePressure is nan

Returns
-------
Expand Down Expand Up @@ -435,11 +499,19 @@ def read_bufr_message(fp: BinaryIO) -> Optional[BUFRVariables]:
f"Unknown BUFR template unexpandedDescriptors: {unexpanded_descriptors}"
)

nonCoordinatePressure = get_bufr_value(ibufr, "nonCoordinatePressure")
if math.isnan(nonCoordinatePressure) and backwards_compatible:
nonCoordinatePressure = get_bufr_value(ibufr, "pressure")
if not math.isnan(nonCoordinatePressure):
logger.warning(
f"nonCoordinatePressure is nan, using legacy pressure instead"
)

variables = BUFRVariables(
timestamp=timestamp,
relativeHumidity=get_bufr_value(ibufr, "relativeHumidity"),
airTemperature=get_bufr_value(ibufr, "airTemperature"),
pressure=get_bufr_value(ibufr, "pressure"),
nonCoordinatePressure=nonCoordinatePressure,
windDirection=get_bufr_value(ibufr, "windDirection"),
windSpeed=get_bufr_value(ibufr, "windSpeed"),
latitude=get_bufr_value(ibufr, "latitude"),
Expand Down Expand Up @@ -485,5 +557,6 @@ def read_bufr_file(path: PathLike) -> pd.DataFrame:
message_vars = read_bufr_message(fp)
if message_vars is None:
break
lines.append(message_vars)
return pd.DataFrame(lines).rename_axis("message_index")
lines.append(message_vars.as_series())
data_frame = pd.DataFrame(lines).set_index("wmo_id")
return data_frame
Loading
Loading