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

Fix truncated text and decode SPCL obs #2658

Merged
merged 2 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .codespellignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ Klystron Warmup Integer*2 N/A 0 to 1 1 0=Normal, 1=Preheat 146
# GFS, NAM, RAP, or other gridded dataset (e.g., NARR).
# This attribute can be set to False if the vector components are grid relative (e.g., for NAM
components that are earth-relative. The primary exception is NAM output with wind
col_head.SELV,
row_head.SELV,
263 changes: 204 additions & 59 deletions src/metpy/io/gempak.py

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/metpy/static-data-manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ gem_sigw_pres_unmrg_man_bgl.csv 4033e22cb1159a9a7ab384103f2ee041b2e87b88da99d7d1
gem_sigw_pres_unmrg_man_bgl.snd ae1e2950d825e25021ee6d7f4b142f550c9c797828cb5f6b3294f2489161861b
gem_std.csv 0e8c0801f54f2680790ac8c400078b510ef8011911ea11fbde1ac8f5dd55d244
gem_std.sfc eb858a190b293347253c7136e058bc9e5c8568f04b1867d5d7f07481adf36118
gem_surface_with_text.csv 4335cdf117cfb683e02a7a386bb9737de8d62df3b88cfc44889d4b72a317c4d2
gem_surface_with_text.sfc 6dd101a1b8496b5a2397abf4efbf87ed29769bb133765fa3dc67052eb2534977
gem_unmerged_with_text.csv b1e5b42297dcdde44cf340397f7e47c2fc9e447875940f969a43fc74dd33cadb
gem_unmerged_with_text.snd 903308e18d6665a0e75502bae7825953401791ecc215f9523644b46b93d6761d
gfs_output.nc d25cc505204118d8e352b5bae6ff24a50c5e97a554e4f40cc1f0b6f0f84a9755
interpolation_test_grid.npz 6549bf00a8b77a50cd58f07419d1f6a4c629580ad1ef43cecd04eb70b9bd4020
interpolation_test_points.npz 17825f01377a39528a701ecfa04124e5e52af2ef6dc594f74a34449d423e244c
Expand Down
2 changes: 2 additions & 0 deletions staticdata/gem_surface_with_text.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TEXT,SPCL
"KMSN 062353Z 01003KT 10SM CLR 21/10 A2986 RMK AO2 SLP111 T02110100 10261 20211 58011 $","KMSN 071604Z 23009G18KT 2SM R36/3000VP6000FT -TSRA BKN027CB OVC080 22/18 A2966"
Binary file added staticdata/gem_surface_with_text.sfc
Binary file not shown.
2 changes: 2 additions & 0 deletions staticdata/gem_unmerged_with_text.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TXTA,TXTB,TXTC,TXPB
"TTAA 57001 72357 99977 22458 15007 00142 ///// ///// 92818 18056 15518 85535 11834 17514 70148 06271 05504 50582 07599 07047 40751 20598 06036 30957 36187 04025 25081 46375 35531 20226 55172 34047 15406 62574 33036 10649 74363 29022 88105 73963 27521 77999 31313 51108 82302","TTBB 57008 72357 00977 22458 11976 22259 22847 11028 33825 09632 44821 09846 55817 12064 66812 12267 77786 10467 88777 11475 99752 10872 11565 05181 22517 05199 33415 19581 44341 30382 55320 31991 66220 53565 77190 55578 88165 61574 99145 61775 11105 73963 22101 74163 31313 51108 82302 41414 00900 51515 10164 00091 10194 16015 13005","TTCC 57002 72357 70859 73164 28512 50060 66371 28511 30379 51586 22516 20642 50986 29508 88999 77999 31313 51108 82302","PPBB 57008 72357 90012 15007 15007 15018 90345 15518 17016 17512 90678 16012 13509 10005 909// 04007 91012 03003 06505 08005 91345 10509 11010 10515 9168/ 09524 07547 92059 06546 06036 06032 93024 05028 03524 00530 9357/ 36030 34037 94257 34055 33049 33009 9489/ 25505 21512 9504/ 22017 29020"
Binary file added staticdata/gem_unmerged_with_text.snd
Binary file not shown.
60 changes: 57 additions & 3 deletions tests/io/test_gempak.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging

import numpy as np
from numpy.testing import assert_allclose
from numpy.testing import assert_allclose, assert_almost_equal
import pandas as pd
import pytest

Expand Down Expand Up @@ -162,7 +162,7 @@ def test_standard_surface():
def dtparse(string):
return datetime.strptime(string, '%y%m%d/%H%M')

skip = ['text']
skip = ['text', 'spcl']

gsf = GempakSurface(get_test_data('gem_std.sfc'))
gstns = gsf.sfjson()
Expand All @@ -187,14 +187,15 @@ def test_ship_surface():
def dtparse(string):
return datetime.strptime(string, '%y%m%d/%H%M')

skip = ['text']
skip = ['text', 'spcl']

gsf = GempakSurface(get_test_data('gem_ship.sfc'))

gempak = pd.read_csv(get_test_data('gem_ship.csv'),
index_col=['STN', 'YYMMDD/HHMM'],
parse_dates=['YYMMDD/HHMM'],
date_parser=dtparse)
gempak.sort_index(inplace=True)

uidx = gempak.index.unique()

Expand Down Expand Up @@ -258,3 +259,56 @@ def test_date_parsing():
sfc_data = GempakSurface(get_test_data('sfc_obs.gem'))
dat = sfc_data.sfinfo()[0].DATTIM
assert dat == datetime(2000, 1, 2)


@pytest.mark.parametrize('text_type,date_time', [
('text', '202109070000'), ('spcl', '202109071600')
])
def test_surface_text(text_type, date_time):
"""Test text decoding of surface hourly and special observations."""
g = get_test_data('gem_surface_with_text.sfc')
d = get_test_data('gem_surface_with_text.csv')

Check warning

Code scanning / CodeQL

File is not always closed

File is opened but is not closed.

gsf = GempakSurface(g)
text = gsf.nearest_time(date_time, station_id='MSN')[0]['values'][text_type]

gempak = pd.read_csv(d)
gem_text = gempak.loc[:, text_type.upper()][0]

assert text == gem_text


@pytest.mark.parametrize('text_type', ['txta', 'txtb', 'txtc', 'txpb'])
def test_sounding_text(text_type):
"""Test for proper decoding of coded message text."""
g = get_test_data('gem_unmerged_with_text.snd')

Check warning

Code scanning / CodeQL

File is not always closed

File may not be closed if an exception is raised.
d = get_test_data('gem_unmerged_with_text.csv')

Check warning

Code scanning / CodeQL

File is not always closed

File is opened but is not closed.

gso = GempakSounding(g).snxarray(station_id='OUN')[0]
gempak = pd.read_csv(d)

text = gso.attrs['WMO_CODES'][text_type]
gem_text = gempak.loc[:, text_type.upper()][0]

assert text == gem_text


def test_special_surface_observation():
"""Test special surface observation conversion."""
sfc = get_test_data('gem_surface_with_text.sfc')

gsf = GempakSurface(sfc)
stn = gsf.nearest_time('202109071604', station_id='MSN')[0]['values']

assert_almost_equal(stn['pmsl'], 1003.81, 2)
assert stn['alti'] == 29.66
assert stn['tmpc'] == 22
assert stn['dwpc'] == 18
assert stn['sknt'] == 9
assert stn['drct'] == 230
assert stn['gust'] == 18
assert stn['wnum'] == 77
assert stn['chc1'] == 2703
assert stn['chc2'] == 8004
assert stn['chc3'] == -9999
assert stn['vsby'] == 2