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

Units Errors when dealing with larger arrays - Pint #1064

Closed
16dreese opened this issue Jun 18, 2019 · 7 comments
Closed

Units Errors when dealing with larger arrays - Pint #1064

16dreese opened this issue Jun 18, 2019 · 7 comments
Labels
Area: Calc Pertains to calculations Area: Units Pertains to unit information Status: Duplicate Duplicates an existing issue Type: Bug Something is not working like it should

Comments

@16dreese
Copy link

Getting two separate but related issues, both of which seem related to the Pint issues documented in previous posts - notably #1032 and #998.

The first issue occurs when representing units through parentheses instead of units.meter etc.

`for k in range(0,6):
for i in range(latli, latui):
for j in range(lonli, lonui):
u = f.variables['u'][ k , : , i , j ] * units.meter / units.second # * units('m/s') will cause an error
v = f.variables['v'][ k , : , i , j ] * units.meter / units.second # * units('m/s')
u = u[::-1]
v = v[::-1]

        ws = np.sqrt(u*u + v*v)
        z = f.variables['z'][ k , : , i  , j ]  #latli:latui , lonli:lonui ]
        z = z[::-1]
        z1 = z/(9.81) * units.meter
        #z = z1*10 * units.meters
        CAPE = f2.variables['cape'][ k + 6, i , j ] * units.joule / units.kilogram
        temp = f.variables['temp'][ k , ::-1 , i , j ] * units.kelvin
        rh = f.variables['rh'][ k , ::-1 , i , j ] / 100. * units.dimensionless #removing this will cause units error 
        dp = calc.dewpoint_rh(temp,rh)`

Different errors will show up for different units, but most of them are fairly similar. Error message below:

runfile('C:/Users/Daniel/Documents/Research/ERA5scripts/dp_test.py', wdir='C:/Users/Daniel/Documents/Research/ERA5scripts')
Traceback (most recent call last):

File "", line 1, in
runfile('C:/Users/Daniel/Documents/Research/ERA5scripts/dp_test.py', wdir='C:/Users/Daniel/Documents/Research/ERA5scripts')

File "C:\Users\Daniel\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)

File "C:\Users\Daniel\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Daniel/Documents/Research/ERA5scripts/dp_test.py", line 54, in
dp = calc.dewpoint_rh(temp,rh)

File "C:\Users\Daniel\Anaconda3\lib\site-packages\metpy\xarray.py", line 436, in wrapper
return func(*args, **kwargs)

File "C:\Users\Daniel\Anaconda3\lib\site-packages\metpy\units.py", line 305, in wrapper
return func(*args, **kwargs)

File "C:\Users\Daniel\Anaconda3\lib\site-packages\metpy\calc\thermo.py", line 700, in dewpoint_rh
return dewpoint(rh * saturation_vapor_pressure(temperature))

File "C:\Users\Daniel\Anaconda3\lib\site-packages\metpy\xarray.py", line 436, in wrapper
return func(*args, **kwargs)

File "C:\Users\Daniel\Anaconda3\lib\site-packages\metpy\units.py", line 304, in wrapper
raise ValueError(msg)

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

The first error was solved by just reverting to the other format for units, and adding units.dimensionless for RH.

The second error happened when I put the above code back in my main script, causing the below error.

TypeError: tuple indices must be integers or slices, not tuple

Downgrading from Pint 0.9 to 0.8.1 fixed this error, but not the first one.

For the full code that I've been using, see attached text file. And sorry if anything here is unclear, first time making a GitHub post.
dp_test.txt

@zbruick
Copy link
Contributor

zbruick commented Jun 18, 2019

Thanks for documenting this for us. As you stated, the second error is due to pint 0.9. We'll be including a fix for that in 0.11. The first error I'll need to dig into more - can you provide an example data file you're using here (or just the arrays of temperature and RH)? Thanks!

@16dreese
Copy link
Author

Not sure how I can upload the file itself, but it's a netcdf of ERA5 data that I trimmed down into a manageable (though still large) file that just covers the CONUS.

Temp and RH arrays -

temp Out[8]: <Quantity([284.87885 282.95456 282.18484 281.1144 279.42355 277.74533 275.7811 273.80005 271.94937 270.34683 268.94412 266.55927 263.5267 259.28906 254.55934 249.57092 244.26706 238.42271 232.35335 224.82867 220.4144 222.13469 222.32185 222.36813 219.73723 217.82767 217.45122], 'kelvin')>

rh Out[9]: masked_array(data=[0.8405978679656982, 0.8792358636856079, 0.46825772523880005, 0.4216090440750122, 0.43887585401535034, 0.4787876009941101, 0.5364474058151245, 0.5903424620628357, 0.5765007138252258, 0.528153657913208, 0.5404669046401978, 0.5042915344238281, 0.4757588505744934, 0.5893517136573792, 0.7777296304702759, 1.0260884761810303, 1.0042927265167236, 0.979326605796814, 0.8878408670425415, 0.9936779141426086, 0.3830276429653168, 0.12606371939182281, 0.07910368591547012, 0.036785878241062164, 0.03590838611125946, 0.03551209717988968, 0.02407637983560562], mask=[False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False], fill_value=1e+20, dtype=float32)

@zbruick
Copy link
Contributor

zbruick commented Jun 18, 2019

Thanks - that'll do!

@akrherz
Copy link
Contributor

akrherz commented Jun 18, 2019

I could be wrong here, but since RH is a masked array, you have to use MetPy's helper to keep the units from getting dropped

from metpy.units import masked_array
rh = masked_array(f.variables['rh'][ k , ::-1 , i , j ] / 100., units.dimensionless)

@zbruick
Copy link
Contributor

zbruick commented Jun 18, 2019

Yup, that's exactly it. If the array is unmasked and doesn't have units assigned, dewpoint_rh() will work just fine. But with the masked array, you'll have to keep the units explicitly assigned.

@zbruick
Copy link
Contributor

zbruick commented Jun 18, 2019

Since the pint 0.9 problem is already documented in #997, I'm going to close this issue.

@zbruick zbruick closed this as completed Jun 18, 2019
@dopplershift
Copy link
Member

I hate masked arrays. 😞

@dopplershift dopplershift added Area: Calc Pertains to calculations Area: Units Pertains to unit information Status: Duplicate Duplicates an existing issue Type: Bug Something is not working like it should labels Sep 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Calc Pertains to calculations Area: Units Pertains to unit information Status: Duplicate Duplicates an existing issue Type: Bug Something is not working like it should
Projects
None yet
Development

No branches or pull requests

4 participants