Ejemplo n.º 1
0
    def attach(self, filename, Vars=None, Cache=False):
        """
        Attach variables from gridded meteorological files, interpolating
        from the gridded values to the (lat,lon) of the fire. If the variables
        are 3D, the whole curtain is included. If *Vars* is not specified,
        all non-coordinate variables on file are included. 

        When *Cache* is true, the variables are saved locally to directory
        "__cache__". When the input file name is "__cache__", the variables are read
        from cache.

        """

        from grads import GrADS

        #       Expects GrADS v2
        #       ----------------
        ga = GrADS(Bin='grads', Window=False, Echo=False)

        #       Open the file
        #       -------------
        fh = ga.open(filename)
        ga('set lon -180 180')  # good for interpolation

        #       Either all varibles on file or user subset
        #       ------------------------------------------
        if Vars == None:
            vinfo = fh.var_info
        else:
            vinfo = []
            for v, k, l in fh.var_info:
                if v in Vars:
                    vinfo.append((v, k, l))
            if len(vinfo) == 0:
                print "IndexError: requested variables - ", Vars
                raise IndexError, "cannot find any matchig variable in file %f"\
                    %filename

#       For each observation, find the correspondng time on file
#       --------------------------------------------------------
        utimes = unique(self.tga)  # unique obs times in grads format
        self.tgaf = self.tga.copy()  # will hold times on file for each ob
        for tga in utimes:
            ga('set time %s' % tga, Quiet=True)
            qh = ga('query time', Quiet=True)
            self.tgaf[self.tga == tga] = ga.rword(1, 3)

#       Loop over each desired variable and interpolate it to
#        fire location
#       -----------------------------------------------------
        self.met = {}
        n = self.lon.size
        levs = 1000. * zeros(fh.nz)
        for v, nlevs, l in vinfo:
            if nlevs == 0:
                nlevs = 1
                y_f = zeros(n)
            else:
                y_f = zeros((n, nlevs))
            for tgaf in unique(self.tgaf):
                ga('set time %s' % tgaf, Quiet=True)
                ga('set z %d %d' % (1, nlevs), Quiet=True)
                m = (self.tgaf == tgaf)  # gather obs for this time
                lon_, lat_ = (self.lon[m], self.lat[m])
                print "- Interpolating %5d %s obs at %s" % (lon_.size, v, tgaf)
                y_f[m], levs = ga.interp(v, lon_, lat_)  # interp & scatter
            self.met[v] = y_f
        self.met['levs'] = levs  # record vertical levels
Ejemplo n.º 2
0
class TestGrb2File(unittest.TestCase):
    def tearDown(self):
        del self.ga

    def _GenericSetUp(self, bin):
        global GrADSTestFiles
        global GrADSBinaryFiles
        self.ga = GrADS(Bin=GrADSBinaryFiles[bin], Echo=False, Window=False)
        self.fh = self.ga.open(GrADSTestFiles['grb2'])

    def test_01_Open(self):
        """
        Check whether file was opened correctly.
        """

        type = 'Gridded'
        vars = [
            'apcpsfc', 'hgtprs', 'prmslmsl', 'rhprs', 'tmpprs', 'ugrdprs',
            'vgrdprs'
        ]
        var_levs = [0, 3, 0, 3, 3, 3, 3]
        nx, ny, nz, nt = (144, 73, 3, 4)  # no E size yet

        fh = self.fh
        self.assertEqual(type, fh.type)
        self.assertEqual(nx, fh.nx)
        self.assertEqual(ny, fh.ny)
        self.assertEqual(nz, fh.nz)
        self.assertEqual(nt, fh.nt)

        vars2 = fh.vars[:]
        var_levs2 = fh.var_levs[:]
        self.assertEqual(vars.sort(), vars2.sort())
        self.assertEqual(var_levs.sort(), var_levs2.sort())

    def test_03_Display(self):
        """
        Displays several variables and checks contour intervals
        """
        self._CheckCint('apcpsfc', 0, 140, 10, z=1, t=2)
        self._CheckCint('0.01*prmslmsl', 950, 1050, 10, z=1, t=1)
        self._CheckCint('hgtprs', 900, 1600, 100, z=1, t=1)
        self._CheckCint('rhprs', 0, 100, 10, z=1, t=1)
        self._CheckCint('tmpprs', 235, 300, 5, z=1, t=1)
        self._CheckCint('ugrdprs', -30, 30, 10, z=1, t=1)
        self._CheckCint('vgrdprs', -25, 35, 5, z=1, t=1)

        self._CheckCint('apcpsfc', 0, 80, 10, z=1, t=4)
        self._CheckCint('0.01*prmslmsl', 950, 1040, 10, z=1, t=4)
        self._CheckCint('hgtprs', 900, 1600, 100, z=1, t=4)
        self._CheckCint('rhprs', 0, 100, 10, z=1, t=4)
        self._CheckCint('tmpprs', 240, 300, 5, z=1, t=4)
        self._CheckCint('ugrdprs', -25, 30, 5, z=1, t=4)
        self._CheckCint('vgrdprs', -25, 30, 5, z=1, t=4)

        self._CheckCint('hgtprs', 10800, 12400, 200, z=3, t=4)
        self._CheckCint('rhprs', 0, 100, 10, z=3, t=4)
        self._CheckCint('tmpprs', 200, 235, 5, z=3, t=4)
        self._CheckCint('ugrdprs', -40, 100, 10, z=3, t=4)
        self._CheckCint('vgrdprs', -50, 50, 10, z=3, t=4)

    def _CheckCint(self, name, cmin, cmax, cint, z=1, t=1):
        """
        Check contour intervals during display.
        """
        self.ga('clear')
        self.ga('display %s(z=%d,t=%d)' % (name, z, t))
        self.assertEqual(cmin, int(self.ga.rword(1, 2)))
        self.assertEqual(cmax, int(self.ga.rword(1, 4)))
        self.assertEqual(cint, int(self.ga.rword(1, 6)))

    def _GenericSetUp(self, bin):
        global GrADSTestFiles
        global GrADSBinaryFiles
        self.ga = GrADS(Bin=GrADSBinaryFiles[bin], Echo=False, Window=False)
        self.fh = self.ga.open(GrADSTestFiles['grb2'])