Esempio n. 1
0
def createGenericGrid(latArray,
                      lonArray,
                      latBounds=None,
                      lonBounds=None,
                      order="yx",
                      mask=None):
    lat = createAxis(latArray, latBounds)
    lat.units = "degrees_north"
    lon = createAxis(lonArray, lonBounds)
    lon.units = "degrees_east"
    return createRectGrid(lat, lon, order, "generic", mask)
Esempio n. 2
0
File: grid.py Progetto: AZed/uvcdat
def createZonalGrid(grid):
    inlat = grid.getLatitude()
    outlatBounds, inlonBounds = grid.getBounds()
    outlat = createAxis(inlat[:],outlatBounds)
    outlat.units = inlat.units

    inlon = grid.getLongitude()
    outlonArray = numpy.array([(inlon[0] + inlon[-1])/2.0])
    outlonBounds = numpy.array([[inlonBounds[0,0], inlonBounds[-1,1]]])
    outlon = createAxis(outlonArray, outlonBounds)
    outlon.units = inlon.units

    return createRectGrid(outlat,outlon,grid.getOrder())
Esempio n. 3
0
def createZonalGrid(grid):
    inlat = grid.getLatitude()
    outlatBounds, inlonBounds = grid.getBounds()
    outlat = createAxis(inlat[:], outlatBounds)
    outlat.units = inlat.units

    inlon = grid.getLongitude()
    outlonArray = numpy.array([(inlon[0] + inlon[-1]) / 2.0])
    outlonBounds = numpy.array([[inlonBounds[0, 0], inlonBounds[-1, 1]]])
    outlon = createAxis(outlonArray, outlonBounds)
    outlon.units = inlon.units

    return createRectGrid(outlat, outlon, grid.getOrder())
Esempio n. 4
0
def fromJSON(jsn):
    """ Recreate a TV from a dumped jsn object"""
    D = json.loads(jsn)

    ## First recreates the axes
    axes = []
    for a in D["_axes"]:
        ax = createAxis(numpy.array(a["_values"], dtype=a["_dtype"]),
                        id=a["id"])
        for k, v in a.iteritems():
            if not k in ["_values", "id", "_dtype"]:
                setattr(ax, k, v)
        axes.append(ax)
    ## Now prep the variable
    V = createVariable(D["_values"], id=D["id"], typecode=D["_dtype"])
    V.setAxisList(axes)
    for k, v in D.iteritems():
        if not k in [
                "id",
                "_values",
                "_axes",
                "_grid",
                "_fill_value",
                "_dtype",
        ]:
            setattr(V, k, v)
    V.set_fill_value(D["_fill_value"])
    return V
Esempio n. 5
0
 def getAxis (self, n):
     if n < 0: n = n + self.rank()
     if self.__domain[n] is None:
         length = numpy.ma.size(self, n)
         # axis = createAxis(numpy.ma.arange(numpy.ma.size(self, n), typecode=numpy.Float))
         axis = createAxis(numpy.ma.arange(numpy.ma.size(self, n), dtype=numpy.float_))
         axis.id = "axis_" + str(n)
         self.__domain[n] = (axis, 0, length, length)
     return self.__domain[n][0]
Esempio n. 6
0
 def getAxis (self, n):
     if n < 0: n = n + self.rank()
     if self.__domain[n] is None:
         length = numpy.ma.size(self, n)
         # axis = createAxis(numpy.ma.arange(numpy.ma.size(self, n), typecode=numpy.Float))
         axis = createAxis(numpy.ma.arange(numpy.ma.size(self, n), dtype=numpy.float_))
         axis.id = "axis_" + str(n)
         self.__domain[n] = (axis, 0, length, length)
     return self.__domain[n][0]
Esempio n. 7
0
 def copyAxis (self, n, axis):
     """Set n axis of self to a copy of axis. (0-based index)
        Invalidates grid.
     """
     if n < 0: n = n + self.rank()
     if not isinstance(axis, AbstractAxis):
         raise CDMSError,"copydimension, other not an axis."
     b = axis.getBounds()
     mycopy = createAxis(axis[:], b)
     mycopy.id = axis.id
     for k, v in axis.attributes.items():
        setattr(mycopy, k, v)
     self.setAxis (n, mycopy)
Esempio n. 8
0
 def copyAxis(self, n, axis):
     """Set n axis of self to a copy of axis. (0-based index)
        Invalidates grid.
     """
     if n < 0: n = n + self.rank()
     if not isinstance(axis, AbstractAxis):
         raise CDMSError, "copydimension, other not an axis."
     b = axis.getBounds()
     mycopy = createAxis(axis[:], b)
     mycopy.id = axis.id
     for k, v in axis.attributes.items():
         setattr(mycopy, k, v)
     self.setAxis(n, mycopy)
Esempio n. 9
0
    def setdimattribute(self, dim, field, value):
        "Set the attribute named field from the dim'th dimension."
        if dim < 0 or dim >= self.rank():
            raise CDMSError, "setdimattribute, dim out of bounds."
        d = self.getAxis(dim)
        if field == "name":
            if not type(value) == types.StringType:
               raise CDMSError, "setdimattribute: name not a string"
            d.id = value
            
        elif field == "values":
            # note -- invalidates grid, may break old code.
            a = createAxis(numpy.ma.filled(value[:]))
            if hasattr(d, 'units'):
                a.units = d.units
            a.id = d.id
            self.setAxis(dim, a)

        elif field == "units":
            if not type(value) == types.StringType:
               raise CDMSError, "setdimattribute: units not a string"
            d.units = value

        elif field == "weights":
            # Well, you can't really do this without modifying the grid
            raise CDMSError, "setdimattribute weights not implemented."

        elif field == "bounds":
            if value is None:
               d.setBounds(None)
            else:
               b = numpy.ma.filled(value)
               if numpy.ma.rank(b) == 2:
                   d.setBounds(b)
               elif numpy.ma.rank(b) == 1:
                   b1 = numpy.zeros((len(b)-1,2), b.dtype.char)
                   b1[:,0] = b[:-1]
                   b1[:,1] = b[1:]
                   d.setBounds(b1)
               else:
                   raise CDMSError, \
                   "setdimattribute, bounds improper shape: " + b.shape
        else:
            setattr(d, field, value)
Esempio n. 10
0
    def setdimattribute(self, dim, field, value):
        "Set the attribute named field from the dim'th dimension."
        if dim < 0 or dim >= self.rank():
            raise CDMSError, "setdimattribute, dim out of bounds."
        d = self.getAxis(dim)
        if field == "name":
            if not type(value) == types.StringType:
                raise CDMSError, "setdimattribute: name not a string"
            d.id = value

        elif field == "values":
            # note -- invalidates grid, may break old code.
            a = createAxis(numpy.ma.filled(value[:]))
            if hasattr(d, 'units'):
                a.units = d.units
            a.id = d.id
            self.setAxis(dim, a)

        elif field == "units":
            if not type(value) == types.StringType:
                raise CDMSError, "setdimattribute: units not a string"
            d.units = value

        elif field == "weights":
            # Well, you can't really do this without modifying the grid
            raise CDMSError, "setdimattribute weights not implemented."

        elif field == "bounds":
            if value is None:
                d.setBounds(None)
            else:
                b = numpy.ma.filled(value)
                if numpy.ma.rank(b) == 2:
                    d.setBounds(b)
                elif numpy.ma.rank(b) == 1:
                    b1 = numpy.zeros((len(b) - 1, 2), b.dtype.char)
                    b1[:, 0] = b[:-1]
                    b1[:, 1] = b[1:]
                    d.setBounds(b1)
                else:
                    raise CDMSError, \
                    "setdimattribute, bounds improper shape: " + b.shape
        else:
            setattr(d, field, value)
Esempio n. 11
0
def fromJSON(jsn):
    """ Recreate a TV from a dumped jsn object"""
    D = json.loads(jsn)

    ## First recreates the axes
    axes = []
    for a in D["_axes"]:
        ax = createAxis(numpy.array(a["_values"], dtype=a["_dtype"]), id=a["id"])
        for k, v in a.iteritems():
            if not k in ["_values", "id", "_dtype"]:
                setattr(ax, k, v)
        axes.append(ax)
    ## Now prep the variable
    V = createVariable(D["_values"], id=D["id"], typecode=D["_dtype"])
    V.setAxisList(axes)
    for k, v in D.iteritems():
        if not k in ["id", "_values", "_axes", "_grid", "_fill_value", "_dtype"]:
            setattr(V, k, v)
    V.set_fill_value(D["_fill_value"])
    return V
Esempio n. 12
0
File: grid.py Progetto: AZed/uvcdat
def createGenericGrid(latArray, lonArray, latBounds=None, lonBounds=None, order="yx", mask=None):
    lat = createAxis(latArray,latBounds)
    lat.units = "degrees_north"
    lon = createAxis(lonArray,lonBounds)
    lon.units = "degrees_east"
    return createRectGrid(lat,lon,order,"generic",mask)