Example #1
0
 def _outline(self, X, Y):
     """
     Return x, y arrays of colorbar bounding polygon,
     taking orientation into account.
     """
     N = nx.shape(X)[0]
     ii = [0, 1, N - 2, N - 1, 2 * N - 1, 2 * N - 2, N + 1, N, 0]
     x = nx.take(nx.ravel(nx.transpose(X)), ii)
     y = nx.take(nx.ravel(nx.transpose(Y)), ii)
     if self.orientation == "horizontal":
         return y, x
     return x, y
Example #2
0
 def _outline(self, X, Y):
     '''
     Return x, y arrays of colorbar bounding polygon,
     taking orientation into account.
     '''
     N = nx.shape(X)[0]
     ii = [0, 1, N - 2, N - 1, 2 * N - 1, 2 * N - 2, N + 1, N, 0]
     x = nx.take(nx.ravel(nx.transpose(X)), ii)
     y = nx.take(nx.ravel(nx.transpose(Y)), ii)
     if self.orientation == 'horizontal':
         return y, x
     return x, y
Example #3
0
    def __call__(self, value):

        vmin = self.vmin
        vmax = self.vmax

        if type(value) in [IntType, FloatType]:
            vtype = 'scalar'
            val = array([value])
        else:
            vtype = 'array'
            val = nx.asarray(value)

        # if both vmin is None and vmax is None, we'll automatically
        # norm the data to vmin/vmax of the actual data, so the
        # clipping step won't be needed.
        if vmin is None and vmax is None:
            needs_clipping = False
        else:
            needs_clipping = True

        if vmin is None or vmax is None:
            rval = nx.ravel(val)
            #do this if sentinels (values to ignore in data)
            if self.ignore:
                sortValues = nx.sort(rval)
                if vmin is None:
                    # find the lowest non-sentinel value
                    for thisVal in sortValues:
                        if thisVal not in self.ignore:
                            vmin = thisVal  #vmin is the lowest non-sentinel value
                            break
                    else:
                        vmin = 0.
                if vmax is None:
                    for thisVal in sortValues[::-1]:
                        if thisVal not in self.ignore:
                            vmax = thisVal  #vmax is the greatest non-sentinel value
                            break
                    else:
                        vmax = 0.
            else:
                if vmin is None: vmin = min(rval)
                if vmax is None: vmax = max(rval)
        if vmin > vmax:
            raise ValueError("minvalue must be less than or equal to maxvalue")
        elif vmin == vmax:
            return 0. * value
        else:
            if needs_clipping:
                val = nx.clip(val, vmin, vmax)
            result = (1.0 / (vmax - vmin)) * (val - vmin)

        # replace sentinels with original (non-normalized) values
        for thisIgnore in self.ignore:
            result = nx.where(val == thisIgnore, thisIgnore, result)

        if vtype == 'scalar':
            result = result[0]
        return result
        def __call__(self, value):

                vmin = self.vmin
                vmax = self.vmax

                if type(value) in [IntType, FloatType]:
                        vtype = 'scalar'
                        val = array([value])
                else:
                        vtype = 'array'
                        val = nx.asarray(value)

                # if both vmin is None and vmax is None, we'll automatically
                # norm the data to vmin/vmax of the actual data, so the
                # clipping step won't be needed.
                if vmin is None and vmax is None:
                        needs_clipping = False
                else:
                        needs_clipping = True

                if vmin is None or vmax is None:
                        rval = nx.ravel(val)
                        #do this if sentinels (values to ignore in data)
                        if self.ignore:
                                sortValues=nx.sort(rval)
                                if vmin is None: 
                                        # find the lowest non-sentinel value
                                        for thisVal in sortValues:
                                                if thisVal not in self.ignore:
                                                        vmin=thisVal #vmin is the lowest non-sentinel value
                                                        break
                                        else:
                                                vmin=0.
                                if vmax is None: 
                                        for thisVal in sortValues[::-1]:
                                                if thisVal not in self.ignore:
                                                        vmax=thisVal #vmax is the greatest non-sentinel value
                                                        break
                                        else:
                                                vmax=0.
                        else:
                                if vmin is None: vmin = min(rval)
                                if vmax is None: vmax = max(rval)
                if vmin > vmax:
                        raise ValueError("minvalue must be less than or equal to maxvalue")
                elif vmin==vmax:
                        return 0.*value
                else:
                        if needs_clipping:
                                val = nx.clip(val,vmin, vmax)
                        result = (1.0/(vmax-vmin))*(val-vmin)

                # replace sentinels with original (non-normalized) values
                for thisIgnore in self.ignore:
                        result = nx.where(val==thisIgnore,thisIgnore,result)

                if vtype == 'scalar':
                        result = result[0]
                return result
Example #5
0
 def _parse_args(self, *args):
     X, Y, U, V, C = [None]*5
     args = list(args)
     if len(args) == 3 or len(args) == 5:
         C = nx.ravel(args.pop(-1))
         #print 'in parse_args, C:', C
     V = nx.ma.asarray(args.pop(-1))
     U = nx.ma.asarray(args.pop(-1))
     nn = nx.shape(U)
     nc = nn[0]
     nr = 1
     if len(nn) > 1:
         nr = nn[1]
     if len(args) == 2:
         X, Y = [nx.ravel(a) for a in args]
         if len(X) == nc and len(Y) == nr:
             X, Y = [nx.ravel(a) for a in meshgrid(X, Y)]
     else:
         X, Y = [nx.ravel(a) for a in meshgrid(nx.arange(nc), nx.arange(nr))]
     return X, Y, U, V, C
Example #6
0
    def __call__(self, lon, lat, inverse=False):
        """
 Calling a Proj class instance with the arguments lon, lat will
 convert lon/lat (in degrees) to x/y native map projection 
 coordinates (in meters).  If optional keyword 'inverse' is
 True (default is False), the inverse transformation from x/y
 to lon/lat is performed.

 For cylindrical equidistant projection ('cyl'), this
 does nothing (i.e. x,y == lon,lat).

 lon,lat can be either scalar floats or N arrays.
        """
        if self.projection == 'cyl':  # for cyl x,y == lon,lat
            return lon, lat
        # if inputs are numarray arrays, get shape and typecode.
        try:
            shapein = lon.shape
            lontypein = lon.typecode()
            lattypein = lat.typecode()
        except:
            shapein = False
        # make sure inputs have same shape.
        if shapein and lat.shape != shapein:
            raise ValueError, 'lon, lat must be scalars or numarrays with the same shape'
        if shapein:
            x = N.ravel(lon).tolist()
            y = N.ravel(lat).tolist()
            if inverse:
                outx, outy = self._inv(x, y)
            else:
                outx, outy = self._fwd(x, y)
            outx = N.reshape(N.array(outx, lontypein), shapein)
            outy = N.reshape(N.array(outy, lattypein), shapein)
        else:
            if inverse:
                outx, outy = self._inv(lon, lat)
            else:
                outx, outy = self._fwd(lon, lat)
        return outx, outy
Example #7
0
    def __call__(self,lon,lat,inverse=False):
        """
 Calling a Proj class instance with the arguments lon, lat will
 convert lon/lat (in degrees) to x/y native map projection 
 coordinates (in meters).  If optional keyword 'inverse' is
 True (default is False), the inverse transformation from x/y
 to lon/lat is performed.

 For cylindrical equidistant projection ('cyl'), this
 does nothing (i.e. x,y == lon,lat).

 lon,lat can be either scalar floats or N arrays.
        """
        if self.projection == 'cyl': # for cyl x,y == lon,lat
            return lon,lat
        # if inputs are numarray arrays, get shape and typecode.
        try:
            shapein = lon.shape
            lontypein = lon.typecode()
            lattypein = lat.typecode()
        except:
            shapein = False
        # make sure inputs have same shape.
        if shapein and lat.shape != shapein:
            raise ValueError, 'lon, lat must be scalars or numarrays with the same shape'
        if shapein:
            x = N.ravel(lon).tolist()
            y = N.ravel(lat).tolist()
            if inverse:
                outx, outy = self._inv(x,y)
            else:
                outx, outy = self._fwd(x,y)
            outx = N.reshape(N.array(outx,lontypein),shapein)
            outy = N.reshape(N.array(outy,lattypein),shapein)
        else:
            if inverse:
                outx,outy = self._inv(lon,lat)
            else:
                outx,outy = self._fwd(lon,lat)
        return outx,outy
Example #8
0
 def _make_verts(self, U, V):
     uv = U+V*1j
     uv = nx.ravel(nx.ma.filled(uv, nx.nan))
     a = nx.absolute(uv)
     if self.scale is None:
         sn = max(10, math.sqrt(self.N))
         scale = 1.8 * nx.average(a) * sn # crude auto-scaling
         scale = scale/self.span
         self.scale = scale
     length = a/(self.scale*self.width)
     X, Y = self._h_arrows(length)
     xy = (X+Y*1j) * nx.exp(1j*nx.angle(uv[...,nx.newaxis]))*self.width
     xy = xy[:,:,nx.newaxis]
     XY = nx.concatenate((xy.real, xy.imag), axis=2)
     return XY
Example #9
0
 def set_UVC(self, U, V, C=None):
     self.U = nx.ma.ravel(U)
     self.V = nx.ma.ravel(V)
     if C is not None:
         self.set_array(nx.ravel(C))
     self._new_UV = True
Example #10
0
# find 4 lon/lat corners of AWIPS grid 221.
    llcornerx = 0.; llcornery = 0.
    lrcornerx = dx*(nx-1); lrcornery = 0.
    ulcornerx = 0.; ulcornery = dy*(ny-1)
    urcornerx = dx*(nx-1); urcornery = dy*(ny-1)
    llcornerlon, llcornerlat = awips221(llcornerx, llcornery, inverse=True)
    lrcornerlon, lrcornerlat = awips221(lrcornerx, lrcornery, inverse=True)
    urcornerlon, urcornerlat = awips221(urcornerx, urcornery, inverse=True)
    ulcornerlon, ulcornerlat = awips221(ulcornerx, ulcornery, inverse=True)
    print '4 corners of AWIPS grid 221:'
    print llcornerlon, llcornerlat
    print lrcornerlon, lrcornerlat
    print urcornerlon, urcornerlat
    print ulcornerlon, ulcornerlat
    print 'from GRIB docs'
    print '(see http://www.nco.ncep.noaa.gov/pmb/docs/on388/tableb.html)'
    print '   -145.5  1.0'
    print '   -68.318 0.897'
    print '   -2.566 46.352'
    print '   148.639 46.635'
# compute lons and lats for the whole AWIPS grid 221 (377x249).
    import time; t1 = time.clock()
    lons, lats = awips221.makegrid(nx,ny)
    t2 = time.clock()
    print 'compute lats/lons for all points on AWIPS 221 grid (%sx%s)' %(nx,ny)
    print 'max/min lons'
    print min(NX.ravel(lons)),max(NX.ravel(lons))
    print 'max/min lats'
    print min(NX.ravel(lats)),max(NX.ravel(lats))
    print 'took',t2-t1,'secs'