I have this toy example where I am using pint and numpy -
import numpy as np
from pint import UnitRegistry
ureg = UnitRegistry()
Q_ = ureg.Quantity
args = []
for k in range(0,17):
uwnd = Q_(np.ones((1,73,144)),'meter / second')
args.append(uwnd)
uwndTot = np.vstack(args)
print(uwndTot.shape)
for element in args:
print(type(element))
The shape of uwndTot is
(17,73,144)
As shown above the type of the element in args is
<class 'pint.quantity.build_quantity_class.<locals>.Quantity'>
But instead if I print out the type of uwndTot in the following way I get
for element in uwndTot:
print(type(element))
I get
<class 'numpy.ndarray'>
So I am unable to extract the units of uwnd from this procedure. Is that correct behavior ? Why does the type change when I issue the call
np.vstack(args)
or
np.concatenate(args,axis = 0)
I want to be able to use vstack or concatenate. Is there another way ?
UPDATE I have a bunch of netCDF files which when I read in are of shape (73,144). I need to concatenate 'n' of them which will give me a numpy array of shape (17,73,144). Those netCDF files have units of meters / second or other units. Those units are required in my calculation.
Here's what we use in MetPy to do this, making sure that all source arrays have compatible dimensionality:
def concatenate(arrs, axis=0):
dest = 'dimensionless'
for a in arrs:
if hasattr(a, 'units'):
dest = a.units
break
data = []
for a in arrs:
if hasattr(a, 'to'):
a = a.to(dest).magnitude
data.append(np.atleast_1d(a))
return units.Quantity(np.concatenate(data, axis=axis), dest)