示例#1
0
    def fill_between(self, x, y1, y2=0, where=None, **kwargs):
        """
        Make filled polygons between two curves (y1 and y2) where ``where==True``.
        
        :param x: (*array_like*) An N-length array of the x data.
        :param y1: (*array_like*) An N-length array (or scalar) of the y data.
        :param y2: (*array_like*) An N-length array (or scalar) of the y data.
        :param where: (*array_like*) If None, default to fill between everywhere. If not None, it is an 
            N-length boolean array and the fill will only happen over the regions where ``where==True``.
        """
        #Get dataset
        global gca

        #Add data series
        label = kwargs.pop('label', 'S_0')
        dn = len(x)
        xdata = plotutil.getplotdata(x)
        if isinstance(y1, (int, long, float)):
            yy = []
            for i in range(dn):
                yy.append(y1)
            y1 = minum.array(yy).array
        else:
            y1 = plotutil.getplotdata(y1)
        if isinstance(y2, (int, long, float)):
            yy = []
            for i in range(dn):
                yy.append(y2)
            y2 = minum.array(yy).array
        else:
            y2 = plotutil.getplotdata(y2)
        if not where is None:
            if isinstance(where, (tuple, list)):
                where = minum.array(where)
            where = where.asarray()

        #Set plot data styles
        if not 'fill' in kwargs:
            kwargs['fill'] = True
        if not 'edge' in kwargs:
            kwargs['edge'] = False
        pb, isunique = plotutil.getlegendbreak('polygon', **kwargs)
        pb.setCaption(label)

        #Create graphics
        offset = kwargs.pop('offset', 0)
        zdir = kwargs.pop('zdir', 'z')
        if zdir == 'xy':
            y = kwargs.pop('y', x)
            ydata = plotutil.getplotdata(y)
            graphics = GraphicFactory.createFillBetweenPolygons(xdata, ydata, y1, y2, where, pb, \
                offset, zdir)
        else:
            graphics = GraphicFactory.createFillBetweenPolygons(xdata, y1, y2, where, pb, \
                offset, zdir)

        visible = kwargs.pop('visible', True)
        if visible:
            self.add_graphic(graphics)
        return graphics
示例#2
0
 def fill_between(self, x, y1, y2=0, where=None, **kwargs):
     """
     Make filled polygons between two curves (y1 and y2) where ``where==True``.
     
     :param x: (*array_like*) An N-length array of the x data.
     :param y1: (*array_like*) An N-length array (or scalar) of the y data.
     :param y2: (*array_like*) An N-length array (or scalar) of the y data.
     :param where: (*array_like*) If None, default to fill between everywhere. If not None, it is an 
         N-length boolean array and the fill will only happen over the regions where ``where==True``.
     """
     #Get dataset
     global gca   
     
     #Add data series
     label = kwargs.pop('label', 'S_0')
     dn = len(x)
     xdata = plotutil.getplotdata(x)
     if isinstance(y1, (int, long, float)):
         yy = []
         for i in range(dn):
             yy.append(y1)
         y1 = minum.array(yy).array
     else:
         y1 = plotutil.getplotdata(y1)
     if isinstance(y2, (int, long, float)):
         yy = []
         for i in range(dn):
             yy.append(y2)
         y2 = minum.array(yy).array
     else:
         y2 = plotutil.getplotdata(y2)
     if not where is None:
         if isinstance(where, (tuple, list)):
             where = minum.array(where)
         where = where.asarray()
     
     #Set plot data styles
     if not 'fill' in kwargs:
         kwargs['fill'] = True
     if not 'edge' in kwargs:
         kwargs['edge'] = False
     pb, isunique = plotutil.getlegendbreak('polygon', **kwargs)
     pb.setCaption(label)
     
     #Create graphics
     offset = kwargs.pop('offset', 0)
     zdir = kwargs.pop('zdir', 'z')
     if zdir == 'xy':
         y = kwargs.pop('y', x)
         ydata = plotutil.getplotdata(y)
         graphics = GraphicFactory.createFillBetweenPolygons(xdata, ydata, y1, y2, where, pb, \
             offset, zdir) 
     else:
         graphics = GraphicFactory.createFillBetweenPolygons(xdata, y1, y2, where, pb, \
             offset, zdir) 
         
     visible = kwargs.pop('visible', True)
     if visible:
         self.add_graphic(graphics)
     return graphics
示例#3
0
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 = minum.array(x)
    if isinstance(y, (list, tuple)):
        y = minum.array(y)
    if isinstance(polygon, tuple):
        x_p = polygon[0]
        y_p = polygon[1]
        if isinstance(x_p, (list, tuple)):
            x_p = minum.array(x_p)
        if isinstance(y_p, (list, tuple)):
            y_p = minum.array(y_p)
        return MIArray(ArrayMath.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 MIArray(ArrayMath.inPolygon(x.array, y.array, polygon))
示例#4
0
    def __init__(self, data=None, index=None, columns=None, dataframe=None):
        if dataframe is None:
            if not data is None:
                if isinstance(data, dict):
                    columns = data.keys()
                    dlist = []
                    n = 1
                    for v in data.values():
                        if isinstance(v, (list, tuple)):
                            n = len(v)
                            v = minum.array(v)
                        elif isinstance(v, MIArray):
                            n = len(v)
                        dlist.append(v)
                    for i in range(len(dlist)):
                        d = dlist[i]
                        if not isinstance(d, MIArray):
                            d = [d] * n
                            d = minum.array(d)
                            dlist[i] = d
                    data = dlist

                if isinstance(data, MIArray):
                    n = len(data)
                    data = data.array
                else:
                    dlist = []
                    n = len(data[0])
                    for dd in data:
                        dlist.append(dd.array)
                    data = dlist

                if index is None:
                    index = range(0, n)
                else:
                    if n != len(index):
                        raise ValueError('Wrong length of index!')

            if isinstance(index, (MIArray, DimArray)):
                index = index.tolist()

            if isinstance(index, Index):
                self._index = index
            else:
                self._index = Index.factory(index)
            if data is None:
                self._dataframe = MIDataFrame(self._index._index)
            else:
                self._dataframe = MIDataFrame(data, self._index._index,
                                              columns)
        else:
            self._dataframe = dataframe
            self._index = Index.factory(index=self._dataframe.getIndex())
示例#5
0
 def __init__(self, data=None, index=None, columns=None, dataframe=None):                             
     if dataframe is None:
         if not data is None:
             if isinstance(data, dict):
                 columns = data.keys()
                 dlist = []
                 n = 1
                 for v in data.values():
                     if isinstance(v, (list, tuple)):
                         n = len(v)
                         v = minum.array(v)                    
                     elif isinstance(v, MIArray):
                         n = len(v)
                     dlist.append(v)
                 for i in range(len(dlist)):
                     d = dlist[i]
                     if not isinstance(d, MIArray):
                         d = [d] * n
                         d = minum.array(d)
                         dlist[i] = d
                 data = dlist
                 
             if isinstance(data, MIArray):
                 n = len(data)
                 data = data.array
             else:
                 dlist = []
                 n = len(data[0])
                 for dd in data:
                     dlist.append(dd.array)
                 data = dlist
                     
             if index is None:
                 index = range(0, n)
             else:
                 if n != len(index):
                     raise ValueError('Wrong length of index!')
                     
         if isinstance(index, (MIArray, DimArray)):
             index = index.tolist()
             
         if isinstance(index, Index):
             self._index = index
         else:
             self._index = Index.factory(index)
         if data is None:
             self._dataframe = MIDataFrame(self._index._index)
         else:
             self._dataframe = MIDataFrame(data, self._index._index, columns)
     else:
         self._dataframe = dataframe
         self._index = Index.factory(index=self._dataframe.getIndex())
示例#6
0
def getplotdata(data):
    if isinstance(data, (MIArray, DimArray)):
        return data.asarray()
    elif isinstance(data, (list, tuple)):
        if isinstance(data[0], datetime.datetime):
            dd = []
            for d in data:
                v = miutil.date2num(d)
                dd.append(v)
            return minum.array(dd).array
        else:
            return minum.array(data).array
    else:
        return minum.array([data]).array
示例#7
0
 def __init__(self, data=None, index=None, name=None, series=None):
     '''
     One-dimensional array with axis labels (including time series).
     
     :param data: (*array_like*) One-dimensional array data.
     :param index: (*list*) Data index list. Values must be unique and hashable, same length as data.
     :param name: (*string*) Series name.
     '''
     if series is None:
         if isinstance(data, (list, tuple)):
             data = minum.array(data)
         if index is None:
             index = range(0, len(data))
         else:
             if len(data) != len(index):
                 raise ValueError('Wrong length of index!')
         if isinstance(index, (MIArray, DimArray)):
             index = index.tolist()
         if isinstance(index, Index):
             self._index = index
         else:
             self._index = Index.factory(index)
         self._data = data
         self._series = MISeries(data.array, self._index._index, name)
     else:
         self._series = series
         self._data = MIArray(self._series.getData())
         self._index = Index.factory(index=self._series.getIndex())
示例#8
0
 def __init__(self, data=None, index=None, name=None, series=None):
     '''
     One-dimensional array with axis labels (including time series).
     
     :param data: (*array_like*) One-dimensional array data.
     :param index: (*list*) Data index list. Values must be unique and hashable, same length as data.
     :param name: (*string*) Series name.
     '''
     if series is None:
         if isinstance(data, (list, tuple)):
             data = minum.array(data)
         if index is None:
             index = range(0, len(data))
         else:
             if len(data) != len(index):
                 raise ValueError('Wrong length of index!')
         if isinstance(index, (MIArray, DimArray)):
             index = index.tolist()
         if isinstance(index, Index):
             self._index = index
         else:
             self._index = Index.factory(index)
         self._data = data
         self._series = MISeries(data.array, self._index._index, name)
     else:
         self._series = series
         self._data = MIArray(self._series.getData())
         self._index = Index.factory(index=self._series.getIndex())
示例#9
0
 def insert(self, loc, column, value):
     '''
     Insert column into DataFrame at specified location.
     
     :param loc: (*int*) Insertation index.
     :param column: (*string*) Label of inserted column.
     :param value: (*array_like*) Column values.
     '''
     if isinstance(value, datetime.datetime):
         value = miutil.jdatetime(value)
     if isinstance(value, (list, tuple)):
         if isinstance(value[0], datetime.datetime):
             value = miutil.jdatetime(value)
         value = minum.array(value)
     if isinstance(value, MIArray):
         value = value.array 
     self._dataframe.addColumn(loc, column, value)
 def cdf(self, x, *args, **kwargs):
     '''
     Cumulative distribution function of the given RV.
     
     :param x: (*array_like*) quantiles.
     :param loc: (*float*) location parameter (default=0).
     :param scale: (*float*) scale parameter (default=1).
     
     :returns: Cumulative distribution function.
     '''
     dist = self._create_distribution(*args)
     if isinstance(x, (list, tuple)):
         x = minum.array(x)
     if isinstance(x, MIArray):
         x = x.array
     r = DistributionUtil.cdf(dist, x)
     return MIArray(r)
 def ppf(self, x, *args, **kwargs):
     '''
     Percent point function (inverse of cdf) at q of the given RV.
     
     :param q: (*array_like*) lower tail probability.
     :param loc: (*float*) location parameter (default=0).
     :param scale: (*float*) scale parameter (default=1).
     
     :returns: Quantile corresponding to the lower tail probability q.
     '''
     dist = self._create_distribution(*args)
     if isinstance(x, (list, tuple)):
         x = minum.array(x)
     if isinstance(x, MIArray):
         x = x.array
     r = DistributionUtil.ppf(dist, x)
     return MIArray(r)
示例#12
0
 def pmf(self, x, *args, **kwargs):
     '''
     Probability mass function (PMF) of the given RV.
     
     :param x: (*array_like*) quantiles.
     :param loc: (*float*) location parameter (default=0).
     :param scale: (*float*) scale parameter (default=1).
     
     :returns: Probability mas function.
     '''
     dist = self._create_distribution(*args)
     if isinstance(x, (list, tuple)):
         x = minum.array(x)
     if isinstance(x, MIArray):
         x = x.array
     r = DistributionUtil.pmf(dist, x)
     return MIArray(r)
 def logpdf(self, x, *args, **kwargs):
     '''
     Log of the probability density function at x of the given RV.
     
     :param x: (*array_like*) quantiles.
     :param loc: (*float*) location parameter (default=0).
     :param scale: (*float*) scale parameter (default=1).
     
     :returns: Log of the probability density function.
     '''
     dist = self._create_distribution(*args)
     if isinstance(x, (list, tuple)):
         x = minum.array(x)
     if isinstance(x, MIArray):
         x = x.array
     r = DistributionUtil.logpdf(dist, x)
     return MIArray(r)
 def cdf(self, x, *args, **kwargs):
     '''
     Cumulative distribution function of the given RV.
     
     :param x: (*array_like*) quantiles.
     :param loc: (*float*) location parameter (default=0).
     :param scale: (*float*) scale parameter (default=1).
     
     :returns: Cumulative distribution function.
     '''
     dist = self._create_distribution(*args)
     if isinstance(x, (list, tuple)):
         x = minum.array(x)
     if isinstance(x, NDArray):
         x = x.array
     r = DistributionUtil.cdf(dist, x)
     return NDArray(r)
 def logpdf(self, x, *args, **kwargs):
     '''
     Log of the probability density function at x of the given RV.
     
     :param x: (*array_like*) quantiles.
     :param loc: (*float*) location parameter (default=0).
     :param scale: (*float*) scale parameter (default=1).
     
     :returns: Log of the probability density function.
     '''
     dist = self._create_distribution(*args)
     if isinstance(x, (list, tuple)):
         x = minum.array(x)
     if isinstance(x, NDArray):
         x = x.array
     r = DistributionUtil.logpdf(dist, x)
     return NDArray(r)
 def ppf(self, x, *args, **kwargs):
     '''
     Percent point function (inverse of cdf) at q of the given RV.
     
     :param q: (*array_like*) lower tail probability.
     :param loc: (*float*) location parameter (default=0).
     :param scale: (*float*) scale parameter (default=1).
     
     :returns: Quantile corresponding to the lower tail probability q.
     '''
     dist = self._create_distribution(*args)
     if isinstance(x, (list, tuple)):
         x = minum.array(x)
     if isinstance(x, NDArray):
         x = x.array
     r = DistributionUtil.ppf(dist, x)
     return NDArray(r)
示例#17
0
 def insert(self, loc, column, value):
     '''
     Insert column into DataFrame at specified location.
     
     :param loc: (*int*) Insertation index.
     :param column: (*string*) Label of inserted column.
     :param value: (*array_like*) Column values.
     '''
     if isinstance(value, datetime.datetime):
         value = miutil.jdatetime(value)
     if isinstance(value, (list, tuple)):
         if isinstance(value[0], datetime.datetime):
             value = miutil.jdatetime(value)
         value = minum.array(value)
     if isinstance(value, MIArray):
         value = value.array
     self._dataframe.addColumn(loc, column, value)
示例#18
0
    def __setitem__(self, key, value):
        if isinstance(value, datetime.datetime):
            value = miutil.jdatetime(value)
        if isinstance(value, (list, tuple)):
            if isinstance(value[0], datetime.datetime):
                value = miutil.jdatetime(value)
            value = minum.array(value)
        if isinstance(value, MIArray):
            value = value.array

        if isinstance(key, basestring):
            if isinstance(value, series.Series):
                value = value.values.array
            self._dataframe.setColumn(key, value)
            return

        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
                self._dataframe.setValue(ridx, cidx, value)
                return
            elif isinstance(ridx, int) and isinstance(cidx, basestring):
                if ridx < 0:
                    ridx = self.shape[0] + ridx
                self._dataframe.setValue(ridx, cidx, value)
                return
        else:
            key = (key, slice(None))
            hascolkey = False

        k = key[0]
        if isinstance(k, int):
            if k < 0:
                k = self.shape[0] + k
            rowkey = k
        elif isinstance(k, basestring):
            sidx = self._index.index(k)
            if sidx < 0:
                return None
            eidx = sidx
            step = 1
            rowkey = Range(sidx, eidx, step)
        elif isinstance(k, slice):
            if isinstance(k.start, basestring):
                sidx = self._index.index(k.start)
                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):
                eidx = self._index.index(k.stop)
                if eidx < 0:
                    eidx = self.shape[0] + eidx
            else:
                eidx = self.shape[0] - 1 if k.stop is None else k.stop - 1
                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], int):
                rowkey = k
            else:
                tlist = []
                for tstr in k:
                    idx = self._index.index(tstr)
                    if idx >= 0:
                        tlist.append(idx)
                rowkey = tlist
        else:
            return

        if not hascolkey:
            colkey = Range(0, self.shape[1] - 1, 1)
        else:
            k = key[1]
            if isinstance(k, int):
                sidx = k
                if sidx < 0:
                    sidx = self.shape[1] + sidx
                eidx = sidx
                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] - 1 if k.stop is None else k.stop - 1
                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], int):
                    colkey = k
                else:
                    colkey = self.columns.indexOfName(k)
            elif isinstance(k, basestring):
                col = self.columns.indexOf(k)
                colkey = Range(col, col + 1, 1)
            else:
                return

        self._dataframe.setValues(rowkey, colkey, value)
示例#19
0
 def set_data(self, value):
     value = minum.array(value)
     self._dataframe.setData(value.array)
示例#20
0
def ncwrite(fn,
            data,
            varname,
            dims=None,
            attrs=None,
            gattrs=None,
            largefile=False):
    """
    Write a netCDF data file from an array.
    
    :param: fn: (*string*) netCDF data file path.
    :param data: (*array_like*) A numeric array variable of any dimensionality.
    :param varname: (*string*) Variable name.
    :param dims: (*list of dimensions*) Dimension list.
    :param attrs: (*dict*) Variable attributes.
    :param gattrs: (*dict*) Global attributes.
    :param largefile: (*boolean*) Create netCDF as large file or not.
    """
    if dims is None:
        if isinstance(data, MIArray):
            dims = []
            for s in data.shape:
                dimvalue = minum.arange(s)
                dimname = 'dim' + str(len(dims))
                dims.append(dimension(dimvalue, dimname))
        else:
            dims = data.dims
    #New netCDF file
    ncfile = addfile(fn, 'c', largefile=largefile)
    #Add dimensions
    ncdims = []
    for dim in dims:
        ncdims.append(ncfile.adddim(dim.getShortName(), dim.getLength()))
    #Add global attributes
    ncfile.addgroupattr('Conventions', 'CF-1.6')
    ncfile.addgroupattr('Tools', 'Created using MeteoInfo')
    if not gattrs is None:
        for key in gattrs:
            ncfile.addgroupattr(key, gattrs[key])
    #Add dimension variables
    dimvars = []
    wdims = []
    for dim, midim in zip(ncdims, dims):
        dimtype = midim.getDimType()
        dimname = dim.getShortName()
        if dimtype == DimensionType.T:
            var = ncfile.addvar(dimname, 'int', [dim])
            var.addattr('units', 'hours since 1900-01-01 00:00:0.0')
            var.addattr('long_name', 'Time')
            var.addattr('standard_name', 'time')
            var.addattr('axis', 'T')
            tvar = var
        elif dimtype == DimensionType.Z:
            var = ncfile.addvar(dimname, 'float', [dim])
            var.addattr('axis', 'Z')
        elif dimtype == DimensionType.Y:
            var = ncfile.addvar(dimname, 'float', [dim])
            var.addattr('axis', 'Y')
        elif dimtype == DimensionType.X:
            var = ncfile.addvar(dimname, 'float', [dim])
            var.addattr('axis', 'X')
        else:
            var = None
        if not var is None:
            dimvars.append(var)
            wdims.append(midim)
    #Add variable
    var = ncfile.addvar(varname, data.dtype, ncdims)
    if attrs is None:
        var.addattr('name', varname)
    else:
        for key in attrs:
            var.addattr(key, attrs[key])
    #Create netCDF file
    ncfile.create()
    #Write variable data
    for dimvar, dim in zip(dimvars, wdims):
        if dim.getDimType() == DimensionType.T:
            sst = datetime.datetime(1900, 1, 1)
            tt = miutil.nums2dates(dim.getDimValue())
            hours = []
            for t in tt:
                hours.append((t - sst).total_seconds() // 3600)
            ncfile.write(dimvar, minum.array(hours))
        else:
            ncfile.write(dimvar, minum.array(dim.getDimValue()))
    ncfile.write(var, data)
    #Close netCDF file
    ncfile.close()
示例#21
0
 def set_values(self, value):
     self._data = minum.array(value)
     self._series.setData(self._data.array)
示例#22
0
    def plot(self, x, y, z, *args, **kwargs):
        """
        Plot 3D lines and/or markers to the axes. *args* is a variable length argument, allowing
        for multiple *x, y* pairs with an optional format string.
        
        :param x: (*array_like*) Input x data.
        :param y: (*array_like*) Input y data.
        :param z: (*array_like*) Input z data.
        :param style: (*string*) Line style for plot.
        
        :returns: Legend breaks of the lines.
        
        The following format string characters are accepted to control the line style or marker:
        
          =========  ===========
          Character  Description
          =========  ===========
          '-'         solid line style
          '--'        dashed line style
          '-.'        dash-dot line style
          ':'         dotted line style
          '.'         point marker
          ','         pixel marker
          'o'         circle marker
          'v'         triangle_down marker
          '^'         triangle_up marker
          '<'         triangle_left marker
          '>'         triangle_right marker
          's'         square marker
          'p'         pentagon marker
          '*'         star marker
          'x'         x marker
          'D'         diamond marker
          =========  ===========
          
        The following color abbreviations are supported:
          
          =========  =====
          Character  Color  
          =========  =====
          'b'        blue
          'g'        green
          'r'        red
          'c'        cyan
          'm'        magenta
          'y'        yellow
          'k'        black
          =========  =====
        """      
        xdata = plotutil.getplotdata(x)
        ydata = plotutil.getplotdata(y)
        zdata = plotutil.getplotdata(z)  
        style = None
        if len(args) > 0:
            style = args[0]
        
        #Set plot data styles
        label = kwargs.pop('label', 'S_1')
        mvalues = kwargs.pop('mvalues', None)
        if mvalues is None:
            if style is None:
                line = plotutil.getlegendbreak('line', **kwargs)[0]
                line.setCaption(label)
            else:
                line = plotutil.getplotstyle(style, label, **kwargs)
            colors = kwargs.pop('colors', None)
            if not colors is None:
                colors = plotutil.getcolors(colors)
                cbs = []
                for color in colors:
                    cb = line.clone()
                    cb.setColor(color)
                    cbs.append(cb)
        else:
            ls = kwargs.pop('symbolspec', None)
            if ls is None:        
                if isinstance(mvalues, (list, tuple)):
                    mvalues = minum.array(mvalues)
                levels = kwargs.pop('levs', None)
                if levels is None:
                    levels = kwargs.pop('levels', None)
                if levels is None:
                    cnum = kwargs.pop('cnum', None)
                    if cnum is None:
                        ls = plotutil.getlegendscheme([], mvalues.min(), mvalues.max(), **kwargs)
                    else:
                        ls = plotutil.getlegendscheme([cnum], mvalues.min(), mvalues.max(), **kwargs)
                else:
                    ls = plotutil.getlegendscheme([levels], mvalues.min(), mvalues.max(), **kwargs)
                ls = plotutil.setlegendscheme_line(ls, **kwargs)

        #Add graphics
        if mvalues is None:
            if colors is None:
                graphics = GraphicFactory.createLineString3D(xdata, ydata, zdata, line)
            else:
                graphics = GraphicFactory.createLineString3D(xdata, ydata, zdata, cbs)
        else:
            mdata = plotutil.getplotdata(mvalues)
            graphics = GraphicFactory.createLineString3D(xdata, ydata, zdata, mdata, ls)
        visible = kwargs.pop('visible', True)
        if visible:
            self.add_graphic(graphics)
        return graphics
示例#23
0
    def scatter(self,
                x,
                y,
                z,
                s=8,
                c='b',
                marker='o',
                alpha=None,
                linewidth=None,
                verts=None,
                **kwargs):
        """
        Make a 3D scatter plot of x, y and z, where x, y and z are sequence like objects of the same lengths.
        
        :param x: (*array_like*) Input x data.
        :param y: (*array_like*) Input y data.
        :param z: (*array_like*) Input z data.
        :param s: (*int*) Size of points.
        :param c: (*Color*) Color of the points. Or z vlaues.
        :param alpha: (*int*) The alpha blending value, between 0 (transparent) and 1 (opaque).
        :param marker: (*string*) Marker of the points.
        :param label: (*string*) Label of the points series.
        :param levs: (*array_like*) Optional. A list of floating point numbers indicating the level 
            points to draw, in increasing order.
        
        :returns: Points legend break.
        """
        #Add data series
        label = kwargs.pop('label', 'S_0')
        xdata = plotutil.getplotdata(x)
        ydata = plotutil.getplotdata(y)
        zdata = plotutil.getplotdata(z)

        #Set plot data styles
        pb, isunique = plotutil.getlegendbreak('point', **kwargs)
        pb.setCaption(label)
        pstyle = plotutil.getpointstyle(marker)
        pb.setStyle(pstyle)
        isvalue = False
        if len(c) > 1:
            if isinstance(c, (MIArray, DimArray)):
                isvalue = True
            elif isinstance(c[0], (int, long, float)):
                isvalue = True
        if isvalue:
            ls = kwargs.pop('symbolspec', None)
            if ls is None:
                if isinstance(c, (list, tuple)):
                    c = minum.array(c)
                levels = kwargs.pop('levs', None)
                if levels is None:
                    levels = kwargs.pop('levels', None)
                if levels is None:
                    cnum = kwargs.pop('cnum', None)
                    if cnum is None:
                        ls = plotutil.getlegendscheme([], c.min(), c.max(),
                                                      **kwargs)
                    else:
                        ls = plotutil.getlegendscheme([cnum], c.min(), c.max(),
                                                      **kwargs)
                else:
                    ls = plotutil.getlegendscheme([levels], c.min(), c.max(),
                                                  **kwargs)
                ls = plotutil.setlegendscheme_point(ls, **kwargs)
                if isinstance(s, int):
                    for lb in ls.getLegendBreaks():
                        lb.setSize(s)
                else:
                    n = len(s)
                    for i in range(0, n):
                        ls.getLegendBreaks()[i].setSize(s[i])
            #Create graphics
            graphics = GraphicFactory.createPoints3D(xdata, ydata, zdata,
                                                     c.asarray(), ls)
        else:
            colors = plotutil.getcolors(c, alpha)
            pbs = []
            if isinstance(s, int):
                pb.setSize(s)
                if len(colors) == 1:
                    pb.setColor(colors[0])
                    pbs.append(pb)
                else:
                    n = len(colors)
                    for i in range(0, n):
                        npb = pb.clone()
                        npb.setColor(colors[i])
                        pbs.append(npb)
            else:
                n = len(s)
                if len(colors) == 1:
                    pb.setColor(colors[0])
                    for i in range(0, n):
                        npb = pb.clone()
                        npb.setSize(s[i])
                        pbs.append(npb)
                else:
                    for i in range(0, n):
                        npb = pb.clone()
                        npb.setSize(s[i])
                        npb.setColor(colors[i])
                        pbs.append(npb)
            #Create graphics
            graphics = GraphicFactory.createPoints3D(xdata, ydata, zdata, pbs)

        visible = kwargs.pop('visible', True)
        if visible:
            self.add_graphic(graphics)
        return graphics
示例#24
0
def grads2nc(infn, outfn, big_endian=None, largefile=False):
    """
    Convert GrADS data file to netCDF data file.
    
    :param infn: (*string*) Input GrADS data file name.
    :param outfn: (*string*) Output netCDF data file name.
    :param big_endian: (*boolean*) Is GrADS data big_endian or not.
    :param largefile: (*boolean*) Create netCDF as large file or not.
    """
    #Open GrADS file
    f = midata.addfile_grads(infn)
    if not big_endian is None:
        f.bigendian(big_endian)

    #New netCDF file
    ncfile = midata.addfile(outfn, 'c')

    #Add dimensions
    dims = []
    for dim in f.dimensions():
        dims.append(ncfile.adddim(dim.getShortName(), dim.getLength()))
    xdim = f.finddim('X')
    ydim = f.finddim('Y')
    tdim = f.finddim('T')
    xnum = xdim.getLength()
    ynum = ydim.getLength()
    tnum = tdim.getLength()

    #Add global attributes
    ncfile.addgroupattr('Conventions', 'CF-1.6')
    for attr in f.attributes():
        ncfile.addgroupattr(attr.getName(), attr.getValues())

    #Add dimension variables
    dimvars = []
    for dim in dims:
        dname = dim.getShortName()
        if dname == 'T':
            var = ncfile.addvar('time', 'int', [dim])
            var.addattr('units', 'hours since 1900-01-01 00:00:0.0')
            var.addattr('long_name', 'Time')
            var.addattr('standard_name', 'time')
            var.addattr('axis', dname)
            tvar = var
        elif dname == 'Z':
            var = ncfile.addvar('level', 'float', [dim])
            var.addattr('axis', dname)
        else:
            var = ncfile.addvar(dim.getShortName(), 'float', [dim])
            if 'Z' in dname:
                var.addattr('axis', 'Z')
            else:
                var.addattr('axis', dname)
        dimvars.append(var)

    #Add variables
    variables = []
    for var in f.variables():    
        print 'Variable: ' + var.getShortName()
        vdims = []
        for vdim in var.getDimensions():
            for dim in dims:
                if vdim.getShortName() == dim.getShortName():
                    vdims.append(dim)
        #print vdims
        nvar = ncfile.addvar(var.getShortName(), var.getDataType(), vdims)
        nvar.addattr('fill_value', -9999.0)
        for attr in var.getAttributes():
            nvar.addattr(attr.getName(), attr.getValues())
        variables.append(nvar)

    #Create netCDF file
    ncfile.create()

    #Write variable data
    for dimvar, dim in zip(dimvars, f.dimensions()):
        if dim.getShortName() != 'T':
            ncfile.write(dimvar, minum.array(dim.getDimValue()))

    sst = datetime.datetime(1900,1,1)
    for t in range(0, tnum):
        st = f.gettime(t)
        print st.strftime('%Y-%m-%d %H:00')
        hours = (st - sst).total_seconds() // 3600
        origin = [t]
        ncfile.write(tvar, minum.array([hours]), origin=origin)
        for var in variables:
            print 'Variable: ' + var.name
            if var.ndim == 3:
                data = f[str(var.name)][t,:,:]    
                data[data==minum.nan] = -9999.0        
                origin = [t, 0, 0]
                shape = [1, ynum, xnum]
                data = data.reshape(shape)
                ncfile.write(var, data, origin=origin)
            else:
                znum = var.dims[1].getLength()
                for z in range(0, znum):
                    data = f[str(var.name)][t,z,:,:]
                    data[data==minum.nan] = -9999.0
                    origin = [t, z, 0, 0]
                    shape = [1, 1, ynum, xnum]
                    data = data.reshape(shape)
                    ncfile.write(var, data, origin=origin)

    #Close netCDF file
    ncfile.close()
    print 'Convert finished!'
示例#25
0
 def __setitem__(self, key, value):
     if isinstance(value, datetime.datetime):
         value = miutil.jdatetime(value)
     if isinstance(value, (list, tuple)):
         if isinstance(value[0], datetime.datetime):
             value = miutil.jdatetime(value)
         value = minum.array(value)
     if isinstance(value, MIArray):
         value = value.array            
         
     if isinstance(key, basestring):
         if isinstance(value, series.Series):
             value = value.values.array
         self._dataframe.setColumn(key, value)
         return
         
     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
             self._dataframe.setValue(ridx, cidx, value)
             return
         elif isinstance(ridx, int) and isinstance(cidx, basestring):
             if ridx < 0:
                 ridx = self.shape[0] + ridx
             self._dataframe.setValue(ridx, cidx, value)
             return
     else:
         key = (key, slice(None))
         hascolkey = False
         
     k = key[0]
     if isinstance(k, int):
         if k < 0:
             k = self.shape[0] + k
         rowkey = k
     elif isinstance(k, basestring):
         sidx = self._index.index(k)
         if sidx < 0:
             return None
         eidx = sidx
         step = 1
         rowkey = Range(sidx, eidx, step)
     elif isinstance(k, slice):
         if isinstance(k.start, basestring):
             sidx = self._index.index(k.start)
             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):
             eidx = self._index.index(k.stop)
             if eidx < 0:
                 eidx = self.shape[0] + eidx
         else:
             eidx = self.shape[0] - 1 if k.stop is None else k.stop - 1
             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], int):
             rowkey = k
         else:
             tlist = []
             for tstr in k:
                 idx = self._index.index(tstr)
                 if idx >= 0:
                     tlist.append(idx)
             rowkey = tlist
     else:
         return
         
     if not hascolkey:
         colkey = Range(0, self.shape[1] - 1, 1)
     else:
         k = key[1]
         if isinstance(k, int):
             sidx = k
             if sidx < 0:
                 sidx = self.shape[1] + sidx
             eidx = sidx
             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] - 1 if k.stop is None else k.stop - 1
             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], int):
                 colkey = k
             else:
                 colkey = self.columns.indexOfName(k)               
         elif isinstance(k, basestring):
             col = self.columns.indexOf(k)
             colkey = Range(col, col + 1, 1)
         else:
             return
     
     self._dataframe.setValues(rowkey, colkey, value)
示例#26
0
 def set_data(self, value):
     value = minum.array(value)
     self._dataframe.setData(value.array)
示例#27
0
def convert2nc(infn, outfn, version='netcdf3', writedimvar=False, largefile=False):
    """
    Convert data file (Grib, HDF...) to netCDF data file.
    
    :param infn: (*string or DimDataFile*) Input data file (or file name).
    :param outfn: (*string*) Output netCDF data file name.
    :param writedimvar: (*boolean*) Write dimension variables or not.
    :param largefile: (*boolean*) Create netCDF as large file or not.
    """
    if isinstance(infn, DimDataFile):
        f = infn
    else:
        #Open input data file
        f = midata.addfile(infn)
        
    #New netCDF file
    ncfile = midata.addfile(outfn, 'c', version=version, largefile=largefile)
    
    #Add dimensions
    dims = []
    for dim in f.dimensions():
        dims.append(ncfile.adddim(dim.getShortName(), dim.getLength()))
        
    #Add global attributes
    for attr in f.attributes():
        ncfile.addgroupattr(attr.getName(), attr.getValues())
        
    #Add dimension variables
    tvar = None
    if writedimvar:
        dimvars = []
        for i in range(len(f.dimensions())):
            dim = f.dimensions()[i]
            dname = dim.getShortName()
            if dim.getDimType() == DimensionType.T:
                var = ncfile.addvar(dname, 'int', [dims[i]])
                var.addattr('units', 'hours since 1900-01-01 00:00:0.0')
                var.addattr('long_name', 'Time')
                var.addattr('standard_name', 'time')
                var.addattr('axis', 'T')
                tvar = var
            elif dim.getDimType() == DimensionType.Z:
                var = ncfile.addvar(dname, 'float', [dims[i]])
                var.addattr('long_name', 'Level')
                var.addattr('axis', 'Z')
            elif dim.getDimType() == DimensionType.Y:
                var = ncfile.addvar(dname, 'float', [dims[i]])
                var.addattr('long_name', dname)
                var.addattr('axis', 'Y')
            elif dim.getDimType() == DimensionType.X:
                var = ncfile.addvar(dname, 'float', [dims[i]])
                var.addattr('long_name', dname)
                var.addattr('axis', 'X')
            else:
                var = ncfile.addvar(dname, 'float', [dims[i]])
                var.addattr('long_name', dname)
                var.addattr('axis', dname)
            dimvars.append(var)
        
    #Add variables
    variables = []
    for var in f.variables():    
        #print 'Variable: ' + var.getShortName()
        if var.hasNullDimension():
            continue
        vdims = []
        missdim = False
        for vdim in var.getDimensions():
            isvalid = False
            for dim in dims:
                if dim.getShortName() == vdim.getShortName():
                    vdims.append(dim)
                    isvalid = True
                    break
            if not isvalid:
                missdim = True
                break
        if missdim:
            continue
        nvar = ncfile.addvar(var.getShortName(), var.getDataType(), vdims)
        for attr in var.getAttributes():
            nvar.addattr(attr.getName(), attr.getValues())
        variables.append(nvar)
        
    #Create netCDF file
    ncfile.create()
    
    #Write dimension variable data
    if writedimvar:
        for dimvar, dim in zip(dimvars, f.dimensions()):
            if dim.getDimType() != DimensionType.T:
                ncfile.write(dimvar, minum.array(dim.getDimValue()))
    
    #Write time dimension variable data
    if writedimvar and not tvar is None:
        sst = datetime.datetime(1900,1,1)
        tnum = f.timenum()
        hours = []
        for t in range(0, tnum):
            st = f.gettime(t)
            hs = (st - sst).total_seconds() // 3600
            hours.append(hs)
        ncfile.write(tvar, minum.array(hours))
    
    #Write variable data
    for var in variables:
        print 'Variable: ' + var.name
        data = f[str(var.name)].read()
        ncfile.write(var, data)    
        
    #Close netCDF file
    ncfile.close()
    print 'Convert finished!'
示例#28
0
def ncwrite(fn, data, varname, dims=None, attrs=None, largefile=False):
    """
    Write a netCDF data file.
    
    :param: fn: (*string*) netCDF data file path.
    :param data: (*array_like*) A numeric array variable of any dimensionality.
    :param varname: (*string*) Variable name.
    :param dims: (*list of dimensions*) Dimension list.
    :param attrs: (*list of attributes*) Attribute list.
    :param largefile: (*boolean*) Create netCDF as large file or not.
    """
    if dims is None:
        if isinstance(data, MIArray):
            dims = []
            for s in data.shape:
                dimvalue = minum.arange(s)
                dimname = 'dim' + str(len(dims))
                dims.append(dimension(dimvalue, dimname))
        else:
            dims = data.dims
    #New netCDF file
    ncfile = midata.addfile(fn, 'c')
    #Add dimensions
    ncdims = []
    for dim in dims:    
        ncdims.append(ncfile.adddim(dim.getShortName(), dim.getLength()))
    #Add global attributes
    ncfile.addgroupattr('Conventions', 'CF-1.6')
    ncfile.addgroupattr('Tools', 'Created using MeteoInfo')
    #Add dimension variables
    dimvars = []
    for dim,midim in zip(ncdims,dims):
        dimtype = midim.getDimType()
        dimname = dim.getShortName()
        if dimtype == DimensionType.T:
            var = ncfile.addvar(dimname, 'int', [dim])
            var.addattr('units', 'hours since 1900-01-01 00:00:0.0')
            var.addattr('long_name', 'Time')
            var.addattr('standard_name', 'time')
            var.addattr('axis', 'T')
            tvar = var
        elif dimtype == DimensionType.Z:
            var = ncfile.addvar(dimname, 'float', [dim])
            var.addattr('axis', 'Z')
        elif dimtype == DimensionType.Y:
            var = ncfile.addvar(dimname, 'float', [dim])
            var.addattr('axis', 'Y')
        elif dimtype == DimensionType.X:
            var = ncfile.addvar(dimname, 'float', [dim])
            var.addattr('axis', 'X')
        else:
            var = ncfile.addvar(dim.getShortName(), 'float', [dim])
            var.addattr('axis', 'null')
        dimvars.append(var)
    #Add variable
    var = ncfile.addvar(varname, data.dtype, ncdims)
    if attrs is None:    
        var.addattr('name', varname)
    else:
        for key in attrs:
            var.addattr(key, attr[key])
    #Create netCDF file
    ncfile.create()
    #Write variable data
    for dimvar, dim in zip(dimvars, dims):
        if dim.getDimType() == DimensionType.T:
            sst = datetime.datetime(1900,1,1)
            tt = miutil.nums2dates(dim.getDimValue())
            hours = []
            for t in tt:
                hours.append((t - sst).total_seconds() // 3600)
            ncfile.write(dimvar, minum.array(hours))
        else:
            ncfile.write(dimvar, minum.array(dim.getDimValue()))
    ncfile.write(var, data)
    #Close netCDF file
    ncfile.close()
示例#29
0
 def stem(self, x, y, z, s=8, c='b', marker='o', alpha=None, linewidth=None, 
             verts=None, **kwargs):
     """
     Make a 3D scatter plot of x, y and z, where x, y and z are sequence like objects of the same lengths.
     
     :param x: (*array_like*) Input x data.
     :param y: (*array_like*) Input y data.
     :param z: (*array_like*) Input z data.
     :param s: (*int*) Size of points.
     :param c: (*Color*) Color of the points. Or z vlaues.
     :param alpha: (*int*) The alpha blending value, between 0 (transparent) and 1 (opaque).
     :param marker: (*string*) Marker of the points.
     :param label: (*string*) Label of the points series.
     :param levs: (*array_like*) Optional. A list of floating point numbers indicating the level 
         points to draw, in increasing order.
     
     :returns: Points legend break.
     """        
     #Add data series
     label = kwargs.pop('label', 'S_0')
     xdata = plotutil.getplotdata(x)
     ydata = plotutil.getplotdata(y)
     zdata = plotutil.getplotdata(z)
     
     #Set plot data styles
     pb, isunique = plotutil.getlegendbreak('point', **kwargs)
     pb.setCaption(label)
     pstyle = plotutil.getpointstyle(marker)    
     pb.setStyle(pstyle)
     bottom = kwargs.pop('bottom', 0)   
     samestemcolor = kwargs.pop('samestemcolor', False)
     isvalue = False
     if len(c) > 1:
         if isinstance(c, (MIArray, DimArray)):
             isvalue = True
         elif isinstance(c[0], (int, long, float)):
             isvalue = True            
     if isvalue:
         ls = kwargs.pop('symbolspec', None)
         if ls is None:        
             if isinstance(c, (list, tuple)):
                 c = minum.array(c)
             levels = kwargs.pop('levs', None)
             if levels is None:
                 levels = kwargs.pop('levels', None)
             if levels is None:
                 cnum = kwargs.pop('cnum', None)
                 if cnum is None:
                     ls = plotutil.getlegendscheme([], c.min(), c.max(), **kwargs)
                 else:
                     ls = plotutil.getlegendscheme([cnum], c.min(), c.max(), **kwargs)
             else:
                 ls = plotutil.getlegendscheme([levels], c.min(), c.max(), **kwargs)
             ls = plotutil.setlegendscheme_point(ls, **kwargs)
             if isinstance(s, int):
                 for lb in ls.getLegendBreaks():
                     lb.setSize(s)
             else:
                 n = len(s)
                 for i in range(0, n):
                     ls.getLegendBreaks()[i].setSize(s[i])
         linefmt = kwargs.pop('linefmt', None)
         if linefmt is None:
             linefmt = PolylineBreak()
             linefmt.setColor(Color.black)
         else:
             linefmt = plotutil.getlegendbreak('line', **linefmt)[0]
         #Create graphics
         graphics = GraphicFactory.createStems3D(xdata, ydata, zdata, c.asarray(), \
             ls, linefmt, bottom, samestemcolor)
     else:
         colors = plotutil.getcolors(c, alpha)   
         pbs = []
         if isinstance(s, int):   
             pb.setSize(s)
             if len(colors) == 1:
                 pb.setColor(colors[0])
                 pb.setOutlineColor(colors[0])
                 pbs.append(pb)
             else:
                 n = len(colors)
                 for i in range(0, n):
                     npb = pb.clone()
                     npb.setColor(colors[i])
                     npb.setOutlineColor(colors[i])
                     pbs.append(npb)
         else:
             n = len(s)
             if len(colors) == 1:
                 pb.setColor(colors[0])
                 pb.setOutlineColor(colors[0])
                 for i in range(0, n):
                     npb = pb.clone()
                     npb.setSize(s[i])
                     pbs.append(npb)
             else:
                 for i in range(0, n):
                     npb = pb.clone()
                     npb.setSize(s[i])
                     npb.setColor(colors[i])
                     npb.setOutlineColor(colors[i])
                     pbs.append(npb)
         linefmt = kwargs.pop('linefmt', None)
         if linefmt is None:
             linefmt = PolylineBreak()
             linefmt.setColor(colors[0])
         else:
             linefmt = plotutil.getlegendbreak('line', **linefmt)[0]
         #Create graphics
         graphics = GraphicFactory.createStems3D(xdata, ydata, zdata, pbs, linefmt, \
             bottom, samestemcolor)
     
     visible = kwargs.pop('visible', True)
     if visible:
         self.add_graphic(graphics[0])
         self.add_graphic(graphics[1])
     return graphics[0], graphics[1]
示例#30
0
def convert2nc(infn,
               outfn,
               version='netcdf3',
               writedimvar=False,
               largefile=False):
    """
    Convert data file (Grib, HDF...) to netCDF data file.
    
    :param infn: (*string or DimDataFile*) Input data file (or file name).
    :param outfn: (*string*) Output netCDF data file name.
    :param writedimvar: (*boolean*) Write dimension variables or not.
    :param largefile: (*boolean*) Create netCDF as large file or not.
    """
    if isinstance(infn, DimDataFile):
        f = infn
    else:
        #Open input data file
        f = addfile(infn)

    #New netCDF file
    ncfile = addfile(outfn, 'c', version=version, largefile=largefile)

    #Add dimensions
    dims = []
    for dim in f.dimensions():
        dims.append(ncfile.adddim(dim.getShortName(), dim.getLength()))

    #Add global attributes
    for attr in f.attributes():
        ncfile.addgroupattr(attr.getName(), attr.getValues())

    #Add dimension variables
    tvar = None
    if writedimvar:
        dimvars = []
        for i in range(len(f.dimensions())):
            dim = f.dimensions()[i]
            dname = dim.getShortName()
            if dim.getDimType() == DimensionType.T:
                var = ncfile.addvar(dname, 'int', [dims[i]])
                var.addattr('units', 'hours since 1900-01-01 00:00:0.0')
                var.addattr('long_name', 'Time')
                var.addattr('standard_name', 'time')
                var.addattr('axis', 'T')
                tvar = var
            elif dim.getDimType() == DimensionType.Z:
                var = ncfile.addvar(dname, 'float', [dims[i]])
                var.addattr('long_name', 'Level')
                var.addattr('axis', 'Z')
            elif dim.getDimType() == DimensionType.Y:
                var = ncfile.addvar(dname, 'float', [dims[i]])
                var.addattr('long_name', dname)
                var.addattr('axis', 'Y')
            elif dim.getDimType() == DimensionType.X:
                var = ncfile.addvar(dname, 'float', [dims[i]])
                var.addattr('long_name', dname)
                var.addattr('axis', 'X')
            else:
                var = ncfile.addvar(dname, 'float', [dims[i]])
                var.addattr('long_name', dname)
                var.addattr('axis', dname)
            dimvars.append(var)

    #Add variables
    variables = []
    for var in f.variables():
        #print 'Variable: ' + var.getShortName()
        if var.hasNullDimension():
            continue
        vdims = []
        missdim = False
        for vdim in var.getDimensions():
            isvalid = False
            for dim in dims:
                if dim.getShortName() == vdim.getShortName():
                    vdims.append(dim)
                    isvalid = True
                    break
            if not isvalid:
                missdim = True
                break
        if missdim:
            continue
        nvar = ncfile.addvar(var.getShortName(), var.getDataType(), vdims)
        for attr in var.getAttributes():
            nvar.addattr(attr.getName(), attr.getValues())
        variables.append(nvar)

    #Create netCDF file
    ncfile.create()

    #Write dimension variable data
    if writedimvar:
        for dimvar, dim in zip(dimvars, f.dimensions()):
            if dim.getDimType() != DimensionType.T:
                ncfile.write(dimvar, minum.array(dim.getDimValue()))

    #Write time dimension variable data
    if writedimvar and not tvar is None:
        sst = datetime.datetime(1900, 1, 1)
        tnum = f.timenum()
        hours = []
        for t in range(0, tnum):
            st = f.gettime(t)
            hs = (st - sst).total_seconds() // 3600
            hours.append(hs)
        ncfile.write(tvar, minum.array(hours))

    #Write variable data
    for var in variables:
        print 'Variable: ' + var.name
        data = f[str(var.name)].read()
        ncfile.write(var, data)

    #Close netCDF file
    ncfile.close()
    print 'Convert finished!'
示例#31
0
 def set_values(self, value):
     self._data = minum.array(value)
     self._series.setData(self._data.array)
示例#32
0
def grads2nc(infn, outfn, big_endian=None, largefile=False):
    """
    Convert GrADS data file to netCDF data file.
    
    :param infn: (*string*) Input GrADS data file name.
    :param outfn: (*string*) Output netCDF data file name.
    :param big_endian: (*boolean*) Is GrADS data big_endian or not.
    :param largefile: (*boolean*) Create netCDF as large file or not.
    """
    #Open GrADS file
    f = addfile_grads(infn)
    if not big_endian is None:
        f.bigendian(big_endian)

    #New netCDF file
    ncfile = addfile(outfn, 'c', largefile=largefile)

    #Add dimensions
    dims = []
    for dim in f.dimensions():
        dims.append(ncfile.adddim(dim.getShortName(), dim.getLength()))
    xdim = f.finddim('X')
    ydim = f.finddim('Y')
    tdim = f.finddim('T')
    xnum = xdim.getLength()
    ynum = ydim.getLength()
    tnum = tdim.getLength()

    #Add global attributes
    ncfile.addgroupattr('Conventions', 'CF-1.6')
    for attr in f.attributes():
        ncfile.addgroupattr(attr.getName(), attr.getValues())

    #Add dimension variables
    dimvars = []
    for dim in dims:
        dname = dim.getShortName()
        if dname == 'T':
            var = ncfile.addvar('time', 'int', [dim])
            var.addattr('units', 'hours since 1900-01-01 00:00:0.0')
            var.addattr('long_name', 'Time')
            var.addattr('standard_name', 'time')
            var.addattr('axis', dname)
            tvar = var
        elif dname == 'Z':
            var = ncfile.addvar('level', 'float', [dim])
            var.addattr('axis', dname)
        else:
            var = ncfile.addvar(dim.getShortName(), 'float', [dim])
            if 'Z' in dname:
                var.addattr('axis', 'Z')
            else:
                var.addattr('axis', dname)
        dimvars.append(var)

    #Add variables
    variables = []
    for var in f.variables():
        print 'Variable: ' + var.getShortName()
        vdims = []
        for vdim in var.getDimensions():
            for dim in dims:
                if vdim.getShortName() == dim.getShortName():
                    vdims.append(dim)
        #print vdims
        nvar = ncfile.addvar(var.getShortName(), var.getDataType(), vdims)
        nvar.addattr('fill_value', -9999.0)
        for attr in var.getAttributes():
            nvar.addattr(attr.getName(), attr.getValues())
        variables.append(nvar)

    #Create netCDF file
    ncfile.create()

    #Write variable data
    for dimvar, dim in zip(dimvars, f.dimensions()):
        if dim.getShortName() != 'T':
            ncfile.write(dimvar, minum.array(dim.getDimValue()))

    sst = datetime.datetime(1900, 1, 1)
    for t in range(0, tnum):
        st = f.gettime(t)
        print st.strftime('%Y-%m-%d %H:00')
        hours = (st - sst).total_seconds() // 3600
        origin = [t]
        ncfile.write(tvar, minum.array([hours]), origin=origin)
        for var in variables:
            print 'Variable: ' + var.name
            if var.ndim == 3:
                data = f[str(var.name)][t, :, :]
                data[data == minum.nan] = -9999.0
                origin = [t, 0, 0]
                shape = [1, ynum, xnum]
                data = data.reshape(shape)
                ncfile.write(var, data, origin=origin)
            else:
                znum = var.dims[1].getLength()
                for z in range(0, znum):
                    data = f[str(var.name)][t, z, :, :]
                    data[data == minum.nan] = -9999.0
                    origin = [t, z, 0, 0]
                    shape = [1, 1, ynum, xnum]
                    data = data.reshape(shape)
                    ncfile.write(var, data, origin=origin)

    #Close netCDF file
    ncfile.close()
    print 'Convert finished!'
示例#33
0
    def plot(self, x, y, z, *args, **kwargs):
        """
        Plot 3D lines and/or markers to the axes. *args* is a variable length argument, allowing
        for multiple *x, y* pairs with an optional format string.
        
        :param x: (*array_like*) Input x data.
        :param y: (*array_like*) Input y data.
        :param z: (*array_like*) Input z data.
        :param style: (*string*) Line style for plot.
        
        :returns: Legend breaks of the lines.
        
        The following format string characters are accepted to control the line style or marker:
        
          =========  ===========
          Character  Description
          =========  ===========
          '-'         solid line style
          '--'        dashed line style
          '-.'        dash-dot line style
          ':'         dotted line style
          '.'         point marker
          ','         pixel marker
          'o'         circle marker
          'v'         triangle_down marker
          '^'         triangle_up marker
          '<'         triangle_left marker
          '>'         triangle_right marker
          's'         square marker
          'p'         pentagon marker
          '*'         star marker
          'x'         x marker
          'D'         diamond marker
          =========  ===========
          
        The following color abbreviations are supported:
          
          =========  =====
          Character  Color  
          =========  =====
          'b'        blue
          'g'        green
          'r'        red
          'c'        cyan
          'm'        magenta
          'y'        yellow
          'k'        black
          =========  =====
        """
        xdata = plotutil.getplotdata(x)
        ydata = plotutil.getplotdata(y)
        zdata = plotutil.getplotdata(z)
        style = None
        if len(args) > 0:
            style = args[0]

        #Set plot data styles
        label = kwargs.pop('label', 'S_1')
        mvalues = kwargs.pop('mvalues', None)
        if mvalues is None:
            if style is None:
                line = plotutil.getlegendbreak('line', **kwargs)[0]
                line.setCaption(label)
            else:
                line = plotutil.getplotstyle(style, label, **kwargs)
            colors = kwargs.pop('colors', None)
            if not colors is None:
                colors = plotutil.getcolors(colors)
                cbs = []
                for color in colors:
                    cb = line.clone()
                    cb.setColor(color)
                    cbs.append(cb)
        else:
            ls = kwargs.pop('symbolspec', None)
            if ls is None:
                if isinstance(mvalues, (list, tuple)):
                    mvalues = minum.array(mvalues)
                levels = kwargs.pop('levs', None)
                if levels is None:
                    levels = kwargs.pop('levels', None)
                if levels is None:
                    cnum = kwargs.pop('cnum', None)
                    if cnum is None:
                        ls = plotutil.getlegendscheme([], mvalues.min(),
                                                      mvalues.max(), **kwargs)
                    else:
                        ls = plotutil.getlegendscheme([cnum], mvalues.min(),
                                                      mvalues.max(), **kwargs)
                else:
                    ls = plotutil.getlegendscheme([levels], mvalues.min(),
                                                  mvalues.max(), **kwargs)
                ls = plotutil.setlegendscheme_line(ls, **kwargs)

        #Add graphics
        if mvalues is None:
            if colors is None:
                graphics = GraphicFactory.createLineString3D(
                    xdata, ydata, zdata, line)
            else:
                graphics = GraphicFactory.createLineString3D(
                    xdata, ydata, zdata, cbs)
        else:
            mdata = plotutil.getplotdata(mvalues)
            graphics = GraphicFactory.createLineString3D(
                xdata, ydata, zdata, mdata, ls)
        visible = kwargs.pop('visible', True)
        if visible:
            self.add_graphic(graphics)
        return graphics
示例#34
0
 def scatter(self, *args, **kwargs):
     """
     Make a scatter plot on a map.
     
     :param x: (*array_like*) Input x data.
     :param y: (*array_like*) Input y data.
     :param z: (*array_like*) Input z data.
     :param levs: (*array_like*) Optional. A list of floating point numbers indicating the level curves 
         to draw, in increasing order.
     :param cmap: (*string*) Color map string.
     :param colors: (*list*) If None (default), the colormap specified by cmap will be used. If a 
         string, like ‘r’ or ‘red’, all levels will be plotted in this color. If a tuple, different 
         levels will be plotted in different colors in the order specified.
     :param size: (*int of list*) Marker size.
     :param marker: (*string*) Marker of the points.
     :param fill: (*boolean*) Fill markers or not. Default is True.
     :param edge: (*boolean*) Draw edge of markers or not. Default is True.
     :param facecolor: (*Color*) Fill color of markers. Default is black.
     :param edgecolor: (*Color*) Edge color of markers. Default is black.
     :param proj: (*ProjectionInfo*) Map projection of the data. Default is None.
     :param zorder: (*int*) Z-order of created layer for display.
     
     :returns: (*VectoryLayer*) Point VectoryLayer.
     """
     n = len(args) 
     if n == 1:
         a = args[0]
         y = a.dimvalue(0)
         x = a.dimvalue(1)
         args = args[1:]
     else:
         x = args[0]
         y = args[1]
         if not isinstance(x, MIArray):
             x = minum.array(x)
         if not isinstance(y, MIArray):
             y = minum.array(y)
         if n == 2:
             a = x
             args = args[2:]
         else:
             a = args[2]
             if not isinstance(a, MIArray):
                 a = minum.array(a)
             args = args[3:]
     
     ls = kwargs.pop('symbolspec', None)
     if ls is None:
         isunique = False
         colors = kwargs.get('colors', None) 
         if not colors is None:
             if isinstance(colors, (list, tuple)) and len(colors) == len(x):
                 isunique = True
         size = kwargs.get('size', None)
         if not size is None:
             if isinstance(size, (list, tuple, MIArray)) and len(size) == len(x):
                 isunique = True
         if isunique:
             ls = LegendManage.createUniqValueLegendScheme(len(x), ShapeTypes.Point)
         else:
             ls = plotutil.getlegendscheme(args, a.min(), a.max(), **kwargs)
         ls = plotutil.setlegendscheme_point(ls, **kwargs)
     
     if a.size == ls.getBreakNum() and ls.getLegendType() == LegendType.UniqueValue:
         layer = DrawMeteoData.createSTPointLayer_Unique(a.array, x.array, y.array, ls, 'layer', 'data')
     else:
         layer = DrawMeteoData.createSTPointLayer(a.array, x.array, y.array, ls, 'layer', 'data')
     
     proj = kwargs.pop('proj', None)
     if not proj is None:
         layer.setProjInfo(proj)
     avoidcoll = kwargs.pop('avoidcoll', None)
     if not avoidcoll is None:
         layer.setAvoidCollision(avoidcoll)
     
     # Add layer
     isadd = kwargs.pop('isadd', True)
     if isadd:
         zorder = kwargs.pop('zorder', None)
         select = kwargs.pop('select', True)
         self.add_layer(layer, zorder, select)
         self.axes.setDrawExtent(layer.getExtent().clone())
         self.axes.setExtent(layer.getExtent().clone())
     
     return MILayer(layer)