def qair2rh(qair, temp, press=1013.25): """ Specific humidity to relative humidity qair: DimArray or NDArray or number Specific humidity - dimensionless (e.g. kg/kg) ratio of water mass / total air mass temp: DimArray or NDArray or number Temperature - degree c press: DimArray or NDArray or number Pressure - hPa (mb) return: DimArray or NDArray or number Relative humidity - % """ if isinstance(press, NDArray) or isinstance(press, DimArray): p = press.asarray() else: p = press if isinstance(qair, NDArray): r = NDArray(MeteoMath.qair2rh(qair.asarray(), temp.asarray(), p)) if isinstance(qair, DimArray): r = DimArray(r, qair.dims, qair.fill_value, qair.proj) return r else: return MeteoMath.qair2rh(qair, temp, press)
def get_slp(wrfin, timeidx=0, units='hPa'): ''' Return the sea level pressure in the specified units. This function extracts the necessary variables from the NetCDF file object in order to perform the calculation. :param wrfin: (*DimDataFile*) Data file. :param timeidx: (*int*) Time index. :param units: (*string*) The desired units. :returns: (*array*) Sea level pressure. ''' t = wrfin['T'][timeidx, :, :, :] p = wrfin['P'][timeidx, :, :, :] pb = wrfin['PB'][timeidx, :, :, :] qvapor = wrfin['QVAPOR'][timeidx, :, :, :] ph = wrfin['PH'][timeidx, :, :, :] phb = wrfin['PHB'][timeidx, :, :, :] full_t = t + constants.T_BASE full_p = p + pb qvapor[qvapor < 0] = 0. full_ph = (ph + phb) / constants.g destag_ph = destagger(full_ph, -3) tk = meteo.temperature_from_potential_temperature(full_p * 0.01, full_t) slp = MeteoMath.calSeaPrs(destag_ph._array, tk._array, full_p._array, qvapor._array) return DimArray(slp, dims=t.dims[1:])
def vorticity(u, v, x=None, y=None): """ Calculates the vertical component of the curl (ie, vorticity). The data should be lon/lat projection. :param u: (*array*) U component array (2D). :param v: (*array*) V component array (2D). :param x: (*array*) X coordinate array (1D). :param y: (*array*) Y coordinate array (1D). :returns: Array of the vertical component of the curl. """ if x is None or y is None: if isinstance(u, DimArray) and isinstance(v, DimArray): x = u.dimvalue(1) y = u.dimvalue(0) else: raise ValueError("Need x, y coordinates") if isinstance(x, (list, tuple)): x = np.array(x) if isinstance(y, (list, tuple)): y = np.array(y) r = MeteoMath.hcurl(u.asarray(), v.asarray(), x.asarray(), y.asarray()) return DimArray(NDArray(r), u.dims, u.fill_value, u.proj)
def divergence(u, v, x=None, y=None): ''' Calculates the horizontal divergence using finite differencing. The data should be lon/lat projection. :param u: (*array*) U component array. :param v: (*array*) V component array. :param x: (*array*) X coordinate. :param y: (*array*) Y coordinate. :returns: Array of the horizontal divergence. ''' ny = u.shape[-2] nx = u.shape[-1] if x is None: if isinstance(u, DimArray): x = u.dimvalue(-1) else: x = np.arange(nx) elif isinstance(x, (list, tuple)): x = np.array(x) if y is None: if isinstance(v, DimArray): y = u.dimvalue(-2) else: y = np.arange(ny) elif isinstance(y, (list, tuple)): y = np.array(y) r = MeteoMath.divergence(u.asarray(), v.asarray(), x.asarray(), y.asarray()) if isinstance(u, DimArray): return DimArray(NDArray(r), u.dims, u.fill_value, u.proj) else: return NDArray(r)
def vorticity(u, v, x=None, y=None): """ Calculates the vertical component of the curl (ie, vorticity). The data should be lon/lat projection. :param u: (*array*) U component array (2D). :param v: (*array*) V component array (2D). :param x: (*array*) X coordinate array (1D). :param y: (*array*) Y coordinate array (1D). :returns: Array of the vertical component of the curl. """ ny = u.shape[-2] nx = u.shape[-1] if x is None: if isinstance(u, DimArray): x = u.dimvalue(-1) else: x = np.arange(nx) elif isinstance(x, (list, tuple)): x = np.array(x) if y is None: if isinstance(v, DimArray): y = u.dimvalue(-2) else: y = np.arange(ny) elif isinstance(y, (list, tuple)): y = np.array(y) r = MeteoMath.vorticity(u.asarray(), v.asarray(), x.asarray(), y.asarray()) return DimArray(NDArray(r), u.dims, u.fill_value, u.proj)
def ds2uv(d, s): ''' Calculate U/V from wind direction and wind speed. :param d: (*array_like*) Wind direction. :param s: (*array_like*) Wind speed. :returns: Wind U/V. ''' if isinstance(d, NDArray): r = MeteoMath.ds2uv(d.asarray(), s.asarray()) u = NDArray(r[0]) v = NDArray(r[1]) if isinstance(d, DimArray) and isinstance(s, DimArray): u = DimArray(u, d.dims, d.fill_value, d.proj) v = DimArray(v, d.dims, d.fill_value, d.proj) return u, v else: r = MeteoMath.ds2uv(d, s) return r[0], r[1]
def uv2ds(u, v): ''' Calculate wind direction and wind speed from U/V. :param u: (*array_like*) U component of wind field. :param v: (*array_like*) V component of wind field. :returns: Wind direction and wind speed. ''' if isinstance(u, NDArray): r = MeteoMath.uv2ds(u.asarray(), v.asarray()) d = NDArray(r[0]) s = NDArray(r[1]) if isinstance(u, DimArray) and isinstance(v, DimArray): d = DimArray(d, u.dims, u.fill_value, u.proj) s = DimArray(s, u.dims, u.fill_value, u.proj) return d, s else: r = MeteoMath.uv2ds(u, v) return r[0], r[1]
def maskout(data, mask, x=None, y=None): """ Maskout data by polygons - NaN values of elements outside polygons. :param data: (*array_like*) Array data for maskout. :param mask: (*list*) Polygon list as maskout borders. :param x: (*array_like*) X coordinate array. :param y: (*array_like*) Y coordinate array. :returns: (*array_like*) Maskouted data array. """ if mask is None: return data elif isinstance(mask, (NDArray, DimArray)): r = ArrayMath.maskout(data.asarray(), mask.asarray()) if isinstance(data, DimArray): return DimArray(r, data.dims, data.fill_value, data.proj) else: return NDArray(r) if x is None or y is None: if isinstance(data, DimArray): x = data.dimvalue(data.ndim - 1) y = data.dimvalue(data.ndim - 2) else: return None if not isinstance(mask, (list, ArrayList)): mask = [mask] if data.ndim == 2 and x.ndim == 1 and y.ndim == 1: x, y = np.meshgrid(x, y) r = GeometryUtil.maskout(data._array, x._array, y._array, mask) if isinstance(data, DimArray): return DimArray(r, data.dims, data.fill_value, data.proj) else: return NDArray(r)
def cdiff(a, dimidx): ''' Performs a centered difference operation on a array in a specific direction :param a: (*array*) The input array. :param dimidx: (*int*) Demension index of the specific direction. :returns: Result array. ''' r = MeteoMath.cdiff(a.asarray(), dimidx) if isinstance(a, DimArray): return DimArray(NDArray(r), a.dims, a.fill_value, a.proj) else: return NDArray(r)
def p2h(press): """ Pressure to height :param press: (*float*) Pressure - hPa. :returns: (*float*) Height - meter. """ if isinstance(press, NDArray): r = NDArray(MeteoMath.press2Height(press.asarray())) if isinstance(press, DimArray): r = DimArray(r, press.dims, press.fill_value, press.proj) return r else: return MeteoMath.press2Height(press)
def h2p(height): """ Height to pressure :param height: (*float*) Height - meter. :returns: (*float*) Pressure - hPa. """ if isinstance(height, NDArray): r = NDArray(MeteoMath.height2Press(height.asarray())) if isinstance(height, DimArray): r = DimArray(r, height.dims, height.fill_value, height.proj) return r else: return MeteoMath.height2Press(height)
def tc2tf(tc): """ Celsius temperature to Fahrenheit temperature tc: DimArray or NDArray or number Celsius temperature - degree c return: DimArray or NDArray or number Fahrenheit temperature - degree f """ if isinstance(tc, NDArray): r = NDArray(MeteoMath.tc2tf(tc.asarray())) if isinstance(tc, DimArray): r = DimArray(r, tc.dims, tc.fill_value, tc.proj) return r else: return MeteoMath.tc2tf(tc)
def tf2tc(tf): """ Fahrenheit temperature to Celsius temperature tf: DimArray or NDArray or number Fahrenheit temperature - degree f return: DimArray or NDArray or number Celsius temperature - degree c """ if isinstance(tf, NDArray): r = NDArray(MeteoMath.tf2tc(tf.asarray())) if isinstance(tf, DimArray): r = DimArray(r, tf.dims, tf.fill_value, tf.proj) return r else: return MeteoMath.tf2tc(tf)
def rh2dewpoint(rh, temp): """ Calculate dewpoint from relative humidity and temperature rh: DimArray or NDArray or number Relative humidity - % temp: DimArray or NDArray or number Temperature - degree c return: DimArray or NDArray or number Relative humidity - % """ if isinstance(rh, NDArray): r = NDArray(MeteoMath.rh2dewpoint(rh.asarray(), temp.asarray())) if isinstance(rh, DimArray): r = DimArray(r, rh.dims, rh.fill_value, rh.proj) return r else: return MeteoMath.rh2dewpoint(rh, temp)
def dewpoint2rh(dewpoint, temp): """ Dew point to relative humidity dewpoint: DimArray or NDArray or number Dew point - degree c temp: DimArray or NDArray or number Temperature - degree c return: DimArray or NDArray or number Relative humidity - % """ if isinstance(dewpoint, NDArray): r = NDArray(MeteoMath.dewpoint2rh(dewpoint.asarray(), temp.asarray())) if isinstance(dewpoint, DimArray): r = DimArray(r, dewpoint.dims, dewpoint.fill_value, dewpoint.proj) return r else: return MeteoMath.dewpoint2rh(temp, dewpoint)