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

calc.potential_temperature does not recognize temperature units #1504

Closed
ThunderRoad75 opened this issue Sep 14, 2020 · 2 comments
Closed

calc.potential_temperature does not recognize temperature units #1504

ThunderRoad75 opened this issue Sep 14, 2020 · 2 comments
Labels
Area: Units Pertains to unit information Status: Upstream Needs work done in upstream project Type: Bug Something is not working like it should

Comments

@ThunderRoad75
Copy link

Using v 0.12.2 on Windows 10 with Anaconda, Python 3.8

Get a ValueError
ValueError: potential_temperature given arguments with incorrect units: temperature requires "[temperature]" but given "none".
Any variable x can be assigned a unit as follows:
from metpy.units import units
x = x * units.meter / units.second

As you'll see in the sample code below, I clearly assign tmpk units exactly as shown in the docs, yet it still says it does not have units.

import numpy as np
import sys
import os
from datetime import datetime, timedelta
from siphon.catalog import TDSCatalog
#from netCDF4 import Dataset
from metpy.calc import potential_temperature
from metpy.units import units

model_url = "https://www.ncei.noaa.gov/thredds/catalog/model-rap130/202009/20200909/catalog.html?dataset=rap130/202009/20200909/rap_130_20200909_1800_000.grb2"
vtime = datetime.strptime('2020090918','%Y%m%d%H')

# Get the data
model = TDSCatalog(model_url)
ds = model.datasets[0]
ncss = ds.subset()
query = ncss.query()
query.accept('netcdf')
query.time(vtime) # Set to the analysis hour only
query.add_lonlat()
query.variables('Temperature_isobaric')
data = ncss.get_data(query)

tmpk = data.variables['Temperature_isobaric'][0,20,:,:] # 600 hPa is 20th element

thta = potential_temperature(600 * units.mbar, tmpk * units.kelvin)
@ThunderRoad75 ThunderRoad75 added the Type: Bug Something is not working like it should label Sep 14, 2020
@jthielen jthielen added the Status: Upstream Needs work done in upstream project label Sep 14, 2020
@jthielen
Copy link
Collaborator

@ThunderRoad75 It looks like this issue is due to numpy/numpy#15200, since in your example tmpk appears to be a masked array (what netcdf4-python uses), and you are attempting to construct the quantity by multiplying by units on the right, which is currently broken by NumPy.

To resolve this issue, you could instead multiply by units on the left, wrap your masked array by np.asarray to convert to an ndarray (example), or bypass all this entirely by using xarray instead (example).

@ThunderRoad75
Copy link
Author

@ThunderRoad75 It looks like this issue is due to numpy/numpy#15200, since in your example tmpk appears to be a masked array (what netcdf4-python uses), and you are attempting to construct the quantity by multiplying by units on the right, which is currently broken by NumPy.

To resolve this issue, you could instead multiply by units on the left, wrap your masked array by np.asarray to convert to an ndarray (example), or bypass all this entirely by using xarray instead (example).

Changing the order of the units assignment worked. Thanks!

@dopplershift dopplershift added the Area: Units Pertains to unit information label Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Units Pertains to unit information Status: Upstream Needs work done in upstream project Type: Bug Something is not working like it should
Projects
None yet
Development

No branches or pull requests

3 participants