def binwrite(out, data, byteorder='little_endian', append=False, sequential=False): """ Create a binary data file from an array variable. :param out: (*string or EndianDataOutputStream*) File path or data output stream. :param data: (*array_like*) A numeric array variable of any dimensionality. :param byteorder: (*string*) Byte order. ``little_endian`` or ``big_endian``. :param append: (*boolean*) Append to an existing file or not. Only valid when ``out`` is file path. :param sequential: (*boolean*) If write binary data as sequential - Fortran """ if isinstance(out, basestring): ArrayUtil.saveBinFile(out, data.asarray(), byteorder, append, sequential) else: ArrayUtil.writeBinFile(out, data.asarray(), byteorder, sequential)
def binwrite(fn, data, byteorder='little_endian', append=False, sequential=False): """ Create a binary data file from an array variable. :param fn: (*string*) Path needed to locate binary file. :param data: (*array_like*) A numeric array variable of any dimensionality. :param byteorder: (*string*) Byte order. ``little_endian`` or ``big_endian``. :param append: (*boolean*) Append to an existing file or not. :param sequential: (*boolean*) If write binary data as sequential - Fortran """ ArrayUtil.saveBinFile(fn, data.asarray(), byteorder, append, sequential)
def linint2(*args, **kwargs): """ Interpolates from a rectilinear grid to another rectilinear grid using bilinear interpolation. :param x: (*array_like*) X coordinate array of the sample data (one dimension). :param y: (*array_like*) Y coordinate array of the sample data (one dimension). :param z: (*array_like*) Value array of the sample data (muti-dimension, last two dimensions are y and x). :param xq: (*array_like*) X coordinate array of the query data (one dimension). :param yq: (*array_like*) Y coordinate array of the query data (one dimension). :returns: (*array_like*) Interpolated array. """ if len(args) == 3: z = args[0] x = z.dimvalue(z.ndim - 1) y = z.dimvalue(z.ndim - 2) xq = args[1] yq = args[2] else: x = args[0] y = args[1] z = args[2] xq = args[3] yq = args[4] x = np.array(x)._array y = np.array(y)._array z = np.array(z)._array xq = np.array(xq)._array yq = np.array(yq)._array r = ArrayUtil.linint2(z, x, y, xq, yq) return NDArray(r)
def view(self): ''' New view of array with the same data. :return: New view of array with the same data. ''' return NDArray(ArrayUtil.view(self._array))
def astype(self, dtype): ''' Convert to another data type. :param dtype: (*string*) Data type. :returns: (*array*) Converted array. ''' if dtype == 'int' or dtype is int: r = NDArray(ArrayUtil.toInteger(self._array)) elif dtype == 'float' or dtype is float: r = NDArray(ArrayUtil.toFloat(self._array)) elif dtype == 'boolean' or dtype == 'bool' or dtype is bool: r = NDArray(ArrayUtil.toBoolean(self._array)) else: r = self return r
def dimvalue(self, idx, convert=False): ''' Get dimension values. :param idx: (*int*) Dimension index. :param convert: (*boolean*) If convert to real values (i.e. datetime). Default is ``False``. :returns: (*array_like*) Dimension values ''' dim = self.dims[idx] if convert: if dim.getDimType() == DimensionType.T: return miutil.nums2dates(dim.getDimValue()) else: return np.array(ArrayUtil.array(self.dims[idx].getDimValue())) else: return np.array(ArrayUtil.array(self.dims[idx].getDimValue()))
def repeat(self, repeats, axis=None): ''' Repeat elements of an array. :param repeats: (*int or list of ints*) The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis. :param axis: (*int*) The axis along which to repeat values. By default, use the flattened input array, and return a flat output array. :returns: (*array_like*) Repeated array. ''' if isinstance(repeats, int): repeats = [repeats] if axis is None: r = ArrayUtil.repeat(self._array, repeats) else: r = ArrayUtil.repeat(self._array, repeats, axis) return NDArray(r)
def expfit(x, y, func=False): ''' Exponent fitting. :param x: (*array_like*) x data array. :param y: (*array_like*) y data array. :param func: (*boolean*) Return fit function (for predict function) or not. Default is ``False``. :returns: Fitting parameters and function (optional). ''' if isinstance(x, list): x = NDArray(ArrayUtil.array(x)) if isinstance(y, list): y = NDArray(ArrayUtil.array(y)) r = FittingUtil.expFit(x.asarray(), y.asarray()) if func: return r[0], r[1], r[2], r[3] else: return r[0], r[1], r[2]
def astype(self, dtype): ''' Convert to another data type. :param dtype: (*string*) Data type. :returns: (*array*) Converted array. ''' if not isinstance(dtype, _dtype.DataType): dtype = _dtype.DataType(dtype) if dtype.kind == 'i': r = NDArray(ArrayUtil.toInteger(self._array)) elif dtype.kind == 'f': r = NDArray(ArrayUtil.toFloat(self._array)) elif dtype.kind == 'b': r = NDArray(ArrayUtil.toBoolean(self._array)) else: r = self return r
def tojarray(self, dtype=None): ''' Convert to java array. :param dtype: (*string*) Data type ['double','long',None]. :returns: (*java array*) Java array. ''' r = ArrayUtil.copyToNDJavaArray(self._array, dtype) return r
def numasciirow(filename): ''' Returns the number of rows in an ASCII file. :param filename: (*string*) The ASCII file name. :returns: The number of rows in the file. ''' nrow = ArrayUtil.numASCIIRow(filename) return nrow
def __call__(self, x, y): ''' Evaluate the interpolate vlaues. :param x: (*array_like*) X to evaluate the interpolant at. :param y: (*array_like*) Y to evaluate the interpolant at. ''' if isinstance(x, list): x = NDArray(ArrayUtil.array(x)) if isinstance(x, (NDArray, DimArray)): x = x.asarray() if isinstance(y, list): y = NDArray(ArrayUtil.array(y)) if isinstance(y, (NDArray, DimArray)): y = y.asarray() r = InterpUtil.evaluate(self._func, x, y) if isinstance(r, float): return r else: return NDArray(r)
def polyfit(x, y, degree, func=False): ''' Polynomail fitting. :param x: (*array_like*) x data array. :param y: (*array_like*) y data array. :param degree: (*int*) Degree of the fitting polynomial. :param func: (*boolean*) Return fit function (for predict function) or not. Default is ``False``. :returns: Fitting parameters and function (optional). ''' if isinstance(x, list): x = NDArray(ArrayUtil.array(x)) if isinstance(y, list): y = NDArray(ArrayUtil.array(y)) r = FittingUtil.polyFit(x.asarray(), y.asarray(), degree) if func: return r[0], r[1], r[2] else: return r[0], r[1]
def bincreate(fn): """ Create a binary data file. :param fn: (*str*) The file path. :return: (*EndianDataOutputStream*) File data stream. """ r = ArrayUtil.createBinFile(fn) if r is None: raise IOError(fn) else: return r
def get_string(self, encoding='UTF-8'): ''' Get string from a char array. :param encoding: (*string*) Encoding string. :returns: (*string*) String. ''' if self.dtype == _dtype.char: return ArrayUtil.getString(self._array, encoding) else: return None
def to_encoding(self, encoding): ''' Convert char array to encoding from UTF-8 :param encoding: (*string*) Encoding string. :returns: (*array*) Converted array. ''' if self.dtype == _dtype.char: return NDArray(ArrayUtil.convertEncoding(self._array, encoding)) else: return None
def numasciicol(filename, delimiter=None, headerlines=0): ''' Returns the number of columns in an ASCII file. :param filename: (*string*) The ASCII file name. :param delimiter: (*string*) Field delimiter character. Default is ``None``, means space or tab delimiter. :param headerlines: (*int*) Lines to skip at beginning of the file. Default is ``0``. :returns: The number of columns in the file. ''' ncol = ArrayUtil.numASCIICol(filename, delimiter, headerlines) return ncol
def coldata(self, key): ''' Return column data as one dimension array. :param key: (*string*) Column name. :returns: (*NDArray*) Colomn data. ''' if isinstance(key, str): print key values = self.data.getColumnData(key).getDataValues() return NDArray(ArrayUtil.array(values)) return None
def predict(func, x): ''' Predict y value using fitting function and x value. :param func: (*Fitting function object*) Fitting function. :param x: (*float*) x value. :returns: (*float*) y value. ''' if isinstance(x, (int, float, long)): return func.predict(x) if isinstance(x, list): x = NDArray(ArrayUtil.array(x)) return NDArray(FittingUtil.predict(x.asarray(), func))
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 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 __init__(self, array): if not isinstance(array, Array): array = ArrayUtil.array(array, None) self._array = array self.ndim = array.getRank() s = array.getShape() s1 = [] for i in range(len(s)): s1.append(s[i]) self._shape = tuple(s1) self.dtype = _dtype.fromjava(array.getDataType()) self.size = int(self._array.getSize()) self.iterator = array.getIndexIterator() self.base = None if self.ndim > 0: self.sizestr = str(self.shape[0]) if self.ndim > 1: for i in range(1, self.ndim): self.sizestr = self.sizestr + '*%s' % self.shape[i]
def polyval(p, x): """ Evaluate a polynomial at specific values. If p is of length N, this function returns the value: p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1] If x is a sequence, then p(x) is returned for each element of x. If x is another polynomial then the composite polynomial p(x(t)) is returned. :param p: (*array_like*) 1D array of polynomial coefficients (including coefficients equal to zero) from highest degree to the constant term. :param x: (*array_like*) A number, an array of numbers, or an instance of poly1d, at which to evaluate p. :returns: Polynomial value """ if isinstance(x, list): x = NDArray(ArrayUtil.array(x)) return NDArray(ArrayMath.polyVal(p, x.asarray()))
def interpolate_1d(x, xp, *args, **kwargs): ''' Interpolation over a specified axis for arrays of any shape. Parameters ---------- x : array-like 1-D array of desired interpolated values. xp : array-like The x-coordinates of the data points. args : array-like The data to be interpolated. Can be multiple arguments, all must be the same shape as xp. axis : int, optional The axis to interpolate over. Defaults to 0. Returns ------- array-like Interpolated values for each point with coordinates sorted in ascending order. ''' axis = kwargs.pop('axis', 0) if isinstance(x, (list, tuple)): x = np.array(x) if isinstance(x, NDArray): x = x._array vars = args ret = [] for a in vars: r = ArrayUtil.interpolate_1d(x, xp._array, a._array, axis) ret.append(NDArray(r)) if len(ret) == 1: return ret[0] else: return ret
def asciiread(filename, **kwargs): ''' Read data from an ASCII file. :param filename: (*string*) The ASCII file name. :param delimiter: (*string*) Field delimiter character. Default is ``None``, means space or tab delimiter. :param headerlines: (*int*) Lines to skip at beginning of the file. Default is ``0``. :param shape: (*string*) Data array dimension shape. Default is ``None``, the file content will be readed as one dimension array. :param readfirstcol: (*boolean*) Read first column data or not. Default is ``True``. :returns: (*NDArray*) The data array. ''' if not os.path.exists(filename): raise IOError('No such file: ' + filename) delimiter = kwargs.pop('delimiter', None) datatype = kwargs.pop('datatype', None) headerlines = kwargs.pop('headerlines', 0) shape = kwargs.pop('shape', None) rfirstcol = kwargs.pop('readfirstcol', True) a = ArrayUtil.readASCIIFile(filename, delimiter, headerlines, datatype, shape, rfirstcol) return NDArray(a)
def __getitem__(self, indices): if not isinstance(indices, tuple): inds = [] inds.append(indices) indices = inds if len(indices) < self.ndim: if isinstance(indices, tuple): indices = list(indices) for i in range(self.ndim - len(indices)): indices.append(slice(None)) allint = True aindex = self._array.getIndex() i = 0 for ii in indices: if isinstance(ii, int): if ii < 0: ii = self.shape[i] + ii aindex.setDim(i, ii) else: allint = False break i += 1 if allint: return self._array.getObject(aindex) if self.ndim == 0: return self newaxisn = 0 newaxis = False if len(indices) > self.ndim: newaxisn = len(indices) - self.ndim newaxis = True for i in range(newaxisn): if not indices[-i - 1] is None: newaxis = False if newaxis: indices = list(indices) indices = indices[:-newaxisn] if len(indices) != self.ndim: print 'indices must be ' + str(self.ndim) + ' dimensions!' raise IndexError() ranges = [] flips = [] onlyrange = True alllist = True isempty = False nshape = [] for i in range(0, self.ndim): k = indices[i] if isinstance(k, int): if k < 0: k = self._shape[i] + k sidx = k eidx = k step = 1 alllist = False elif isinstance(k, slice): sidx = 0 if k.start is None else k.start if sidx < 0: sidx = self._shape[i] + sidx eidx = self._shape[i] if k.stop is None else k.stop if eidx < 0: eidx = self._shape[i] + eidx eidx -= 1 step = 1 if k.step is None else k.step alllist = False elif isinstance(k, (list, tuple, NDArray)): if isinstance(k, NDArray): k = k.aslist() if isinstance(k[0], bool): kk = [] for i in range(len(k)): if k[i]: kk.append(i) k = kk onlyrange = False ranges.append(k) continue else: print k return None if step < 0: step = abs(step) flips.append(i) if eidx < sidx: tempidx = sidx sidx = eidx + 2 eidx = tempidx if sidx >= self.shape[i]: isempty = True if eidx < sidx: isempty = True else: rr = Range(sidx, eidx, step) ranges.append(rr) nshape.append(eidx - sidx + 1 if eidx - sidx >= 0 else 0) if isempty: r = ArrayUtil.empty([0], self.dtype._dtype) return NDArray(r) if onlyrange: r = ArrayMath.section(self._array, ranges) else: if alllist: r = ArrayMath.takeValues(self._array, ranges) else: r = ArrayMath.take(self._array, ranges) if newaxis: for i in flips: r = r.flip(i) rr = Array.factory(r.getDataType(), r.getShape()) MAMath.copy(rr, r) rr = NDArray(rr) newshape = list(rr.shape) for i in range(newaxisn): newshape.append(1) return rr.reshape(newshape) if r.getSize() == 1: iter = r.getIndexIterator() r = iter.getObjectNext() if isinstance(r, Complex): return complex(r.getReal(), r.getImaginary()) else: return r else: for i in flips: r = r.flip(i) r = NDArray(r) if onlyrange: r.base = self.get_base() return r
def __repr__(self): return ArrayUtil.convertToString(self._array)
def __setitem__(self, indices, value): #print type(indices) if isinstance(indices, NDArray): if isinstance(value, NDArray): value = value.asarray() ArrayMath.setValue(self._array, indices._array, value) return None if not isinstance(indices, tuple): inds = [] inds.append(indices) indices = inds if len(indices) < self.ndim: for i in range(self.ndim - len(indices)): indices.append(slice(None)) if self.ndim == 0: self._array.setObject(0, value) return None if len(indices) != self.ndim: print 'indices must be ' + str(self.ndim) + ' dimensions!' raise IndexError() ranges = [] flips = [] onlyrange = True alllist = True for i in range(0, self.ndim): k = indices[i] if isinstance(k, int): sidx = k if sidx < 0: sidx = self._shape[i] + sidx eidx = sidx step = 1 alllist = False elif isinstance(k, (list, tuple, NDArray)): if isinstance(k, NDArray): k = k.aslist() onlyrange = False ranges.append(k) continue else: sidx = 0 if k.start is None else k.start if sidx < 0: sidx = self._shape[i] + sidx eidx = self._shape[i] if k.stop is None else k.stop if eidx < 0: eidx = self._shape[i] + eidx eidx -= 1 step = 1 if k.step is None else k.step alllist = False if step < 0: step = abs(step) flips.append(i) if eidx < sidx: return rr = Range(sidx, eidx, step) ranges.append(rr) if isinstance(value, (list, tuple)): value = ArrayUtil.array(value) if isinstance(value, NDArray): value = value.asarray() if onlyrange: r = ArrayMath.setSection(self._array, ranges, value) else: if alllist: r = ArrayMath.setSection_List(self._array, ranges, value) else: r = ArrayMath.setSection_Mix(self._array, ranges, value) self._array = r
def __init__(self, x, y, kind='linear'): if isinstance(x, list): x = NDArray(ArrayUtil.array(x)) if isinstance(y, list): y = NDArray(ArrayUtil.array(y)) self._func = InterpUtil.getInterpFunc(x.asarray(), y.asarray(), kind)
def __getitem__(self, key): if isinstance(key, basestring): coldata = self.data.getColumnData(key) if coldata.getDataType().isNumeric(): return NDArray(ArrayUtil.array(coldata.getDataValues())) elif coldata.getDataType() == DataType.DATE: vv = coldata.getData() r = [] cal = Calendar.getInstance() for v in vv: cal.setTime(v) year = cal.get(Calendar.YEAR) month = cal.get(Calendar.MONTH) + 1 day = cal.get(Calendar.DAY_OF_MONTH) hour = cal.get(Calendar.HOUR_OF_DAY) minute = cal.get(Calendar.MINUTE) second = cal.get(Calendar.SECOND) dt = datetime.datetime(year, month, day, hour, minute, second) r.append(dt) return r else: return NDArray(ArrayUtil.array(coldata.getData())) hascolkey = True if isinstance(key, tuple): ridx = key[0] cidx = key[1] if isinstance(ridx, int) and isinstance(cidx, int): if ridx < 0: ridx = self.shape[0] + ridx if cidx < 0: cidx = self.shape[1] + cidx return self.data.getValue(ridx, cidx) elif isinstance(ridx, int) and isinstance(cidx, basestring): if ridx < 0: ridx = self.shape[0] + ridx return self.data.getValue(ridx, cidx) else: key = (key, slice(None)) hascolkey = False k = key[0] if isinstance(k, int): sidx = k if sidx < 0: sidx = self.shape[0] + sidx eidx = sidx + 1 step = 1 rowkey = Range(sidx, eidx, step) elif isinstance(k, slice): if isinstance(k.start, basestring): t = miutil.str2date(k.start) t = miutil.jdate(t) sidx = self.data.getTimeIndex(t) if sidx < 0: sidx = 0 else: sidx = 0 if k.start is None else k.start if sidx < 0: sidx = self.shape[0] + sidx if isinstance(k.stop, basestring): t = miutil.str2date(k.stop) t = miutil.jdate(t) eidx = self.data.getTimeIndex(t) + 1 if eidx < 0: eidx = self.shape[0] else: eidx = self.shape[0] if k.stop is None else k.stop if eidx < 0: eidx = self.shape[0] + eidx step = 1 if k.step is None else k.step rowkey = Range(sidx, eidx, step) elif isinstance(k, list): if isinstance(k[0], basestring): tlist = [] for tstr in k: t = miutil.jdate(miutil.str2date(tstr)) idx = self.data.getTimeIndex_Ex(t) if idx >= 0: tlist.append(idx) rowkey = tlist else: rowkey = k else: return None tcolname = self.data.getTimeColName() if not hascolkey: r = self.data.select(rowkey) if r.findColumn(tcolname) is None: r = TableData(r) else: r = TimeTableData(r, tcolname) return PyTableData(r) k = key[1] if isinstance(k, int): sidx = k if sidx < 0: sidx = self.shape[1] + sidx eidx = sidx + 1 step = 1 colkey = Range(sidx, eidx, step) elif isinstance(k, slice): sidx = 0 if k.start is None else k.start if sidx < 0: sidx = self.shape[1] + sidx eidx = self.shape[1] if k.stop is None else k.stop if eidx < 0: eidx = self.shape[1] + eidx step = 1 if k.step is None else k.step colkey = Range(sidx, eidx, step) elif isinstance(k, list): if isinstance(k[0], basestring): cols = self.data.findColumns(k) else: cols = self.data.findColumns_Index(k) colkey = cols elif isinstance(k, basestring): rows = self.data.getRows(rowkey) coldata = self.data.getColumnData(rows, k) if coldata.getDataType().isNumeric(): return NDArray(ArrayUtil.array(coldata.getDataValues())) else: return NDArray(ArrayUtil.array(coldata.getData())) else: return None r = self.data.select(rowkey, colkey) if r.findColumn(tcolname) is None: r = TableData(r) else: r = TimeTableData(r, tcolname) return PyTableData(r)