def arrayinpolygon(a, polygon, x=None, y=None): ''' Set array element value as 1 if inside a polygon or set value as -1. :param a: (*array_like*) The array. :param polygon: (*PolygonShape*) The polygon. :param x: (*float*) X coordinate of the point. Default is ``None``, for DimArray :param y: (*float*) Y coordinate of the point. Default is ``None``, for DimArray :returns: (*array_like*) Result array. ''' if isinstance(a, DimArray): if x is None or y is None: x = self.dimvalue(1) y = self.dimvalue(0) if not x is None and not y is None: if isinstance(polygon, tuple): x_p = polygon[0] y_p = polygon[1] if isinstance(x_p, NDArray): x_p = x_p.aslist() if isinstance(y_p, NDArray): y_p = y_p.aslist() return NDArray(GeometryUtil.inPolygon(a.asarray(), x.aslist(), y.aslist(), x_p, y_p)) else: if isinstance(polygon, MILayer): polygon = polygon.layer return NDArray(GeometryUtil.inPolygon(a.asarray(), x.aslist(), y.aslist(), polygon)) else: return None
def project(x, y, fromproj=KnownCoordinateSystems.geographic.world.WGS1984, toproj=KnownCoordinateSystems.geographic.world.WGS1984): """ Project geographic coordinates from one projection to another. :param x: (*array_like*) X coordinate values for projection. :param y: (*array_like*) Y coordinate values for projection. :param fromproj: (*ProjectionInfo*) From projection. Default is longlat projection. :param toproj: (*ProjectionInfo*) To projection. Default is longlat projection. :returns: (*array_like*, *array_like*) Projected geographic coordinates. """ if isinstance(fromproj, str): fromproj = ProjectionInfo.factory(fromproj) if isinstance(toproj, str): toproj = ProjectionInfo.factory(toproj) if isinstance(x, (tuple, list)): x = np.array(x) if isinstance(y, (tuple, list)): y = np.array(y) if isinstance(x, NDArray): outxy = Reproject.reproject(x.asarray(), y.asarray(), fromproj, toproj) return NDArray(outxy[0]), NDArray(outxy[1]) else: inpt = PointD(x, y) outpt = Reproject.reprojectPoint(inpt, fromproj, toproj) return outpt.X, outpt.Y
def inpolygon(x, y, polygon): ''' Check if x/y points are inside a polygon or not. :param x: (*array_like*) X coordinate of the points. :param y: (*array_like*) Y coordinate of the points. :param polygon: (*PolygonShape list*) The polygon list. :returns: (*boolean array*) Inside or not. ''' if isinstance(x, numbers.Number): return GeoComputation.pointInPolygon(polygon, x, y) if isinstance(x, (list, tuple)): x = np.array(x) if isinstance(y, (list, tuple)): y = np.array(y) if isinstance(polygon, tuple): x_p = polygon[0] y_p = polygon[1] if isinstance(x_p, (list, tuple)): x_p = np.array(x_p) if isinstance(y_p, (list, tuple)): y_p = np.array(y_p) return NDArray(GeometryUtil.inPolygon(x._array, y._array, x_p._array, y_p._array)) else: if isinstance(polygon, MILayer): polygon = polygon.shapes() elif isinstance(polygon, PolygonShape): polygon = [polygon] return NDArray(GeometryUtil.inPolygon(x._array, y._array, polygon))
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 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 rmaskin(data, x, y, mask): """ Maskin data by polygons - the elements inside polygons will be removed :param data: (*array_like*) Array data for maskin. :param x: (*array_like*) X coordinate array. :param y: (*array_like*) Y coordinate array. :param mask: (*list*) Polygon list as mask borders. :returns: (*list*) Masked data, x and y array list. """ if not isinstance(mask, (list, ArrayList)): mask = [mask] r = GeometryUtil.maskin_Remove(data._array, x._array, y._array, mask) return NDArray(r[0]), NDArray(r[1]), NDArray(r[2])
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 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 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 reproject(a, x=None, y=None, fromproj=None, xp=None, yp=None, toproj=None, method='bilinear'): """ Project array :param a: (*array_like*) Input array. :param x: (*array_like*) Input x coordinates. :param y: (*array_like*) Input y coordinates. :param fromproj: (*ProjectionInfo*) Input projection. :param xp: (*array_like*) Projected x coordinates. :param yp: (*array_like*) Projected y coordinates. :param toproj: (*ProjectionInfo*) Output projection. :param method: Interpolation method: ``bilinear`` or ``neareast`` . :returns: (*NDArray*) Projected array """ if x is None or y is None: if isinstance(a, DimArray): y = a.dimvalue(a.ndim - 2) x = a.dimvalue(a.ndim - 1) else: raise ValueError('Input x/y coordinates are None') if fromproj is None: if isinstance(a, DimArray): fromproj = a.proj else: fromproj = KnownCoordinateSystems.geographic.world.WGS1984 if toproj is None: toproj = KnownCoordinateSystems.geographic.world.WGS1984 if method == 'bilinear': method = ResampleMethods.Bilinear else: method = ResampleMethods.NearestNeighbor if xp is None or yp is None: pr = Reproject.reproject(a.asarray(), x.aslist(), y.aslist(), fromproj, toproj, method) return NDArray(pr[0]), NDArray(pr[1]), NDArray(pr[2]) if isinstance(xp, (list, tuple)): xp = NDArray(xp) if isinstance(yp, (list, tuple)): yp = NDArray(yp) xp, yp = ArrayUtil.meshgrid(xp.asarray(), yp.asarray()) r = Reproject.reproject(a.asarray(), x.aslist(), y.aslist(), xp, yp, fromproj, toproj, method) return NDArray(r)
def maximum_filter(a, size): ''' Calculate a multi-dimensional maximum filter. :param a: (*array*) Input array :param size: (*int*) Window size :return: Filtered array. Has the same shape as input array. ''' r = ImageUtil.maximumFilter(a._array, size) return NDArray(r)
def count(a, size): ''' Count none-zero points with window size :param a: (*array_like*) 2-D array. :param size: (*int*) Window size. :returns: (*array_like*) Count result. ''' r = ImageUtil.count(a.asarray(), size) return NDArray(r)
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 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 gaussian_filter(a, sigma, size=3): ''' Calculate a multi-dimensional gaussian filter. :param a: (*array*) Input array :param sigma: (*float*) Standard deviation for Gaussian kernel. :param size: (*int*) Window size :return: Filtered array. Has the same shape as input array. ''' r = ImageUtil.gaussianFilter(a._array, size, sigma) return NDArray(r)
def convert_array(a): ''' Convert netcdf Array to NDArray or conversely. :param a: (*netcdf Array or NDArray*) Input array. :returns: (*NDArray or netcdf Array) Output array. ''' if isinstance(a, Array): return NDArray(NCUtil.convertArray(a)) else: return NCUtil.convertArray(a._array)
def mean(a, size, positive=True): ''' Calculate mean value with window size :param a: (*array_like*) 2-D array. :param size: (*int*) Window size. :param positive: (*boolean*) Only calculate the positive value or not. :returns: (*array_like*) Mean result. ''' r = ImageUtil.mean(a.asarray(), size, positive) return NDArray(r)
def imread(fname): ''' Read RGB(A) data array from image file. :param fname: (*String*) Image file name. :returns: (*array*) RGB(A) data array. ''' if not os.path.exists(fname): raise IOError(fname) r = ImageUtil.imageRead(fname) return NDArray(r)
def geotiffread(filename): ''' Return data array from a GeoTiff data file. :param filename: (*string*) The GeoTiff file name. :returns: (*NDArray*) Readed data array. ''' geotiff = GeoTiff(filename) geotiff.read() r = geotiff.readArray() return NDArray(r)
def __getreturn(src, dst): if isinstance(src, Graphic): src.getShape().setImage(dst) return src elif isinstance(src, MILayer): src.layer.setImage(dst) return src elif isinstance(src, NDArray): r = ImageUtil.imageRead(dst) return NDArray(r) else: return dst
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 gifread(gif, frame=0): ''' Read RGB(A) data array from a gif image file or a gif decoder object. :param gif: (*string or GifDecoder*) Gif image file or gif decoder object. :param frame: (*int*) Image frame index. :returns: (*array*) RGB(A) data array. ''' if isinstance(gif, basestring): gif = gifopen(gif) im = gif.getFrame(frame) r = ImageUtil.imageRead(im) 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 binread(fn, dim, datatype=None, skip=0, byteorder='little_endian'): """ Read data array from a binary file. :param fn: (*string*) The binary file name for data reading. :param dim: (*list*) Dimensions. :param datatype: (*string*) Data type string [byte | short | int | float | double]. :param skip: (*int*) Skip bytes number. :param byteorder: (*string*) Byte order. ``little_endian`` or ``big_endian``. :returns: (*NDArray*) Data array """ if not os.path.exists(fn): raise IOError('No such file: ' + fn) r = ArrayUtil.readBinFile(fn, dim, datatype, skip, byteorder); return NDArray(r)
def categorical(obs, fcst, min=None, max=None, values=None): """ Categoraical calculation :param obs: (*array_like*) Observation data. :param fcst: (*array_like*) Forecast data. :param min: (*Number*) Minimum value of data range. :param max: (*Number*) Maximum value of data range. :param values: (*list*) Values of data range - not using min/max. :returns: (*array_like*) Categoraical result data. """ if values is None: drange = DataRange(min, max) else: drange = DataRange(values) return NDArray(VerifyStat.categorical(obs.asarray(), fcst.asarray(), drange))
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)