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

import metar or synop data fo metpy #3737

Open
fipoucat opened this issue Jan 15, 2025 · 4 comments
Open

import metar or synop data fo metpy #3737

fipoucat opened this issue Jan 15, 2025 · 4 comments

Comments

@fipoucat
Copy link

I am facing an error for metar not in the registry, which means associated with package:

ValueError: File 'metar_20230114_1200.txt' is not in the registry

1)I need help on how to load other imported metar and synops to be handled by the station_plot.py script.

  1. I am looking for the script to compute and display the nice GDI map shown in the related issue section.

THank you

@DWesl
Copy link
Contributor

DWesl commented Jan 17, 2025

What code are you trying that produces the above ValueError? Is it lifted exactly from an example? Did you tweak it to plot a different date?

Which is "the nice GDI map"? What "related issue section" do you mean? I would guess "the nice GDI map" is one of the ones from the example gallery, perhaps this one, but none of them mention a GDI.

@fipoucat
Copy link
Author

Thank you @dwes, I am trying the example station_plot.py which works as it is. Now when I download other metar datasets from the server https://thredds.ucar.edu/thredds/catalog/noaaport/text/metar/catalog.html I am getting this error message. I tried 3 different dates.

For the GDI it relates to the following issue.: THe example related to GDI is : #3121

@DWesl
Copy link
Contributor

DWesl commented Jan 18, 2025

The catalog you linked only displays files for the last month or so: does 2025-01-17T12Z work for you?

There's tests calculating GDI here:

def test_gdi_xarray(index_xarray_data_expanded):
"""Test the GDI calculation with a grid of xarray data."""
pressure = index_xarray_data_expanded.isobaric
temperature = index_xarray_data_expanded.temperature
dewpoint = index_xarray_data_expanded.dewpoint
mixing_ratio = mixing_ratio_from_relative_humidity(
pressure, temperature, relative_humidity_from_dewpoint(temperature, dewpoint))
result = galvez_davison_index(
pressure,
temperature,
mixing_ratio,
pressure[0]
)
assert_array_almost_equal(
result,
np.array([[[189.5890429, 157.4307982, 129.9739099],
[106.6763526, 87.0637477, 70.7202505]]])
)
def test_gdi_arrays(index_xarray_data_expanded):
"""Test GDI on 3-D Quantity arrays with an array of surface pressure."""
ds = index_xarray_data_expanded.isel(time=0).squeeze()
pressure = ds.isobaric.metpy.unit_array[:, None, None]
temperature = ds.temperature.metpy.unit_array
dewpoint = ds.dewpoint.metpy.unit_array
mixing_ratio = mixing_ratio_from_relative_humidity(
pressure, temperature, relative_humidity_from_dewpoint(temperature, dewpoint))
surface_pressure = units.Quantity(
np.broadcast_to(pressure.m, temperature.shape), pressure.units)[0]
result = galvez_davison_index(pressure, temperature, mixing_ratio, surface_pressure)
assert_array_almost_equal(
result,
np.array([[189.5890429, 157.4307982, 129.9739099],
[106.6763526, 87.0637477, 70.7202505]])
)

with data defined here:
@pytest.fixture()
def index_xarray_data_expanded():
"""Create expanded data for testing that index calculations work with xarray data.
Specifically for Galvez Davison Index calculation, which requires 950hPa pressure
"""
pressure = xr.DataArray(
[950., 850., 700., 500.], dims=('isobaric',), attrs={'units': 'hPa'}
)
temp = xr.DataArray([[[[306., 305., 304.], [303., 302., 301.]],
[[296., 295., 294.], [293., 292., 291.]],
[[286., 285., 284.], [283., 282., 281.]],
[[276., 275., 274.], [273., 272., 271.]]]] * units.K,
dims=('time', 'isobaric', 'y', 'x'))
profile = xr.DataArray([[[[299., 298., 297.], [296., 295., 294.]],
[[289., 288., 287.], [286., 285., 284.]],
[[279., 278., 277.], [276., 275., 274.]],
[[269., 268., 267.], [266., 265., 264.]]]] * units.K,
dims=('time', 'isobaric', 'y', 'x'))
dewp = xr.DataArray([[[[304., 303., 302.], [301., 300., 299.]],
[[294., 293., 292.], [291., 290., 289.]],
[[284., 283., 282.], [281., 280., 279.]],
[[274., 273., 272.], [271., 270., 269.]]]] * units.K,
dims=('time', 'isobaric', 'y', 'x'))
dirw = xr.DataArray([[[[135., 135., 135.], [135., 135., 135.]],
[[180., 180., 180.], [180., 180., 180.]],
[[225., 225., 225.], [225., 225., 225.]],
[[270., 270., 270.], [270., 270., 270.]]]] * units.degree,
dims=('time', 'isobaric', 'y', 'x'))
speed = xr.DataArray([[[[15., 15., 15.], [15., 15., 15.]],
[[20., 20., 20.], [20., 20., 20.]],
[[25., 25., 25.], [25., 25., 25.]],
[[50., 50., 50.], [50., 50., 50.]]]] * units.knots,
dims=('time', 'isobaric', 'y', 'x'))
return xr.Dataset({'temperature': temp, 'profile': profile, 'dewpoint': dewp,
'wind_direction': dirw, 'wind_speed': speed},
coords={'isobaric': pressure, 'time': ['2023-01-01T00:00Z']})

Loading the data required for the calculation from a file (downloaded from, say, NCEP for the US models or Copernicus for ERA5 data) and feeding it to the function would produce the GDI to plot.
The plots shown there look like a contourf plot with a contour interval of 5 from -100 to 100 and a RdBu_r colormap on a Plate-Caree/equirectangular projection with state borders and ocean and lake coastlines. Possibly a levels argument of np.arange(0, 101, 5) with a colormap of Reds would also work.

@DWesl
Copy link
Contributor

DWesl commented Jan 18, 2025

Here's the implementation of metpy.cbook.get_test_data:

MetPy/src/metpy/cbook.py

Lines 14 to 40 in 1a00500

pooch_kwargs = {'path': pooch.os_cache('metpy'), 'version': 'v' + __version__,
'base_url': 'https://github.com/Unidata/MetPy/raw/{version}/staticdata/'}
# Check if we have the data available directly from a git checkout, either from the
# TEST_DATA_DIR variable, or looking relative to the path of this module's file. Use this
# to override Pooch's path and disable downloading from GitHub.
dev_data_path = os.environ.get('TEST_DATA_DIR', Path(__file__).parents[2] / 'staticdata')
if Path(dev_data_path).exists():
pooch_kwargs['path'] = dev_data_path
pooch_kwargs['version'] = None
pooch_kwargs['base_url'] = pooch_kwargs['base_url'].format(version='main')
if pooch.__version__ >= 'v1.6.0':
pooch_kwargs['allow_updates'] = False
POOCH = pooch.create(version_dev='main', **pooch_kwargs)
POOCH.load_registry(Path(__file__).parent / 'static-data-manifest.txt')
def get_test_data(fname, as_file_obj=True, mode='rb'):
"""Access a file from MetPy's collection of test data."""
path = POOCH.fetch(fname)
# If we want a file object, open it, trying to guess whether this should be binary mode
# or not
if as_file_obj:
return open(path, mode) # noqa: SIM115
return path

as the name implies, it's only designed for the MetPy test data, not arbitrary dates.

I don't remember if Siphon will download metar data for you, but you could download the files manually with wget and pass the path to parse_metar_file, or download with response = requests.get(url); fid = io.StringIO(response.text), and pass fid to parse_metar_file. Alternately, you could re-work the example above to use the THREDDS catalog you posted earlier instead of MetPy's test data repository and use that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants