Пример #1
0
    def nearestSampleFile(self, inFile, tyme, onlyVars=None):
        """
        Sample some or all variables of inFile. This simply returns the
        values of the gridbox the pixel fall into (nearest neighbor).
        """
        from gfio import GFIOctl

        if self.varTime:
          # tyme is hour mark, and scanTime displaces from this in seconds
          tymes = np.array([tyme+timedelta(seconds=int(s)) for s in self.scanTime])
        else:
          # constant time
          tymes = np.repeat(tyme,self.nobs)

        # Instiate grads and open file
        # ----------------------------
        f = GFIOctl(inFile)
        if self.sample == None:
            self.sample = GCS03Handle(inFile)
        if onlyVars is None:
            onlyVars = f.vname

        # Loop over variables on file
        # ---------------------------
        for v in onlyVars:
            if self.verb:
                print " <> Nearest sampling ", v
            var = f.sample(v,self.lon,self.lat,tymes,
                           algorithm='nearest',Verbose=self.verb)
            if var.ndim == 1:
                self.sample.__dict__[v] = var
            elif var.ndim == 2:
                self.sample.__dict__[v] = var.T   # -> (nobs,nz)
            else:
                raise IndexError, 'variable <%s> has rank = %d' % var.ndim
Пример #2
0
    def getICAindx(self, inFile):
        """
        Get gridded related indices for ICA calculations.  It calculates
        grid-box indices useful to implement Independent COlumn
        Approximation (ICA) type of algorithms.
        """
        from gfio import GFIOctl

        # Instiate grads and open file
        # ----------------------------
        f = GFIOctl(inFile)
        if self.ica == None:
            self.ica = GOCIHandle(inFile)

        # Handle grid indices for ICA algorithms
        # --------------------------------------
        nt, nr = self.lon.shape
        if self.verb:
            print " <> Performing ICA index generation"
        iCoord, jCoord = f.coordNN(self.lon.ravel(), self.lat.ravel())
        iS, jS = iCoord.astype('S5'), jCoord.astype('S5'),
        keys = [ii + ',' + jj for ii, jj in zip(iS, jS)]
        Indices = dict()
        for n in range(iCoord.size):
            Indices[keys[n]] = (iCoord[n], jCoord[n])

        # save his for later
        # ------------------
        self.ica.iCoord = iCoord.reshape((nt, nr))
        self.ica.jCoord = jCoord.reshape((nt, nr))
        self.ica.Indices = Indices
Пример #3
0
    def linearSampleFile(self, inFile, tyme, onlyVars=None):
        """
        Interpolates some or all variables of inFile.
        """
        from gfio import GFIOctl

        if self.varTime:
          # tyme is hour mark, and scanTime displaces from this in seconds
          tymes = np.array([tyme+timedelta(seconds=int(s)) for s in self.scanTime])
        else:
          # constant time
          tymes = np.repeat(tyme,self.nobs)
             
        # Open file
        # ---------
        f = GFIOctl(inFile)
        if self.sample == None:
            self.sample = GCS03Handle(inFile)
        if onlyVars is None:
            onlyVars = f.vname

        # Loop over variables on file
        # ---------------------------
        for v in onlyVars:
            if self.verb:
                print " <> Linear sampling ", v
            var = f.sample(v,self.lon,self.lat,tymes,Verbose=self.verb)
            if var.ndim == 1:
                self.sample.__dict__[v] = var
            elif var.ndim == 2:
                self.sample.__dict__[v] = var.T   # -> (nobs,nz)
            else:
                raise IndexError, 'variable <%s> has rank = %d' % var.ndim
Пример #4
0
    def getICAindx(self,inFile):
        """
        Get grid related indices for ICA calculations. Calculates grid-box
        indices useful to implement Independent Column Approximation (ICA)
        type of algorithms.
        """
        from gfio import GFIOctl

        # Instiate grads and open file
        # ----------------------------
        f = GFIOctl(inFile)
        if self.ica == None:
            self.ica = GCS03Handle(inFile)

        # Handle grid indices for ICA algorithms
        # --------------------------------------
        if self.verb:
            print " <> Performing ICA index generation"
        # GEOS-5 coordinates of each pixel
        iCoord, jCoord = f.coordNN(self.lon,self.lat)
        # dictionary containing pixels for each gridcolumn
        Indices = {}
        for n in range(iCoord.size):
          Indices.setdefault((iCoord[n],jCoord[n]),[]).append(n)

        # save for later
        # --------------
        self.ica.iCoord = iCoord
        self.ica.jCoord = jCoord
        self.ica.Indices = Indices
Пример #5
0
    def linearSampleFile(self, inFile, tyme, onlyVars=None):
        """
        Interpolates some or all variables of inFile.
        """
        from gfio import GFIOctl

        # constant time
        tymes = np.repeat(tyme, self.nobs)

        # Open file
        # ---------
        f = GFIOctl(inFile)
        if self.sample == None:
            self.sample = SEV03Handle(inFile)
        if onlyVars is None:
            onlyVars = f.vname

        # Loop over variables on file
        # ---------------------------
        for v in onlyVars:
            if self.verb:
                print " <> Linear sampling ", v
            var = f.sample(v, self.lon, self.lat, tymes, Verbose=self.verb)
            if var.ndim == 1:
                self.sample.__dict__[v] = var
            elif var.ndim == 2:
                self.sample.__dict__[v] = var.T  # -> (nobs,nz)
            else:
                raise IndexError, 'variable <%s> has rank = %d' % var.ndim
Пример #6
0
 def addVar(self, ctlfile, vname, **kwds):
     """
     Interpolates variable <vname> in <ctlfile> to pixel locations
     and saves it as an attribute named <vname>.
     """
     from gfio import GFIOctl
     f = GFIOctl(ctlfile)
     self.__dict__[vname] = f.interpXY(vname, self.lon, self.lat, **kwds)
     f.close()
Пример #7
0
    def sampleFile(self, inFile, npzFile=None, onlyVars=None, Verbose=True):
        """
        Interpolates all variables of inFile and optionally
        save them to file *npzFile*. On input, I is an optional filtering
        index.
        """
        from gfio import GFIOctl, GFIOHandle

        # Instiate grads and open file
        # ----------------------------
        f = GFIOctl(inFile)

        # Check if all variables in file are upper case
        # ---------------------------------------------
        lc = False
        for v in f.vname:
            if v != v.upper(): lc = True

        self.sample = GFIOHandle(inFile)
        if onlyVars is None:
            onlyVars = f.vname

        # Flatten coordinates
        # -------------------
        nt, nr = self.lon.shape
        tyme = tile(self.time, (nr, 1)).T
        lons = self.lon.ravel()
        lats = self.lat.ravel()
        tymes = tyme.ravel()

        # Loop over variables on file
        # ---------------------------
        for v in onlyVars:
            if Verbose:
                print "<> Sampling ", v
            if not lc:
                v_ = v.upper()
            else:
                v_ = v
            var = f.sample(v_, lons, lats, tymes, Verbose=Verbose)
            if len(var.shape) == 1:
                self.sample.__dict__[v] = var.reshape((nt, nr))
            elif len(var.shape) == 2:
                var = var.T  # shape should be (nobs,nz)
                self.sample.__dict__[v] = var.reshape((nt, nr, -1))
            else:
                raise IndexError, 'variable <%s> has rannk = %d' % len(
                    var.shape)

        if npzFile is not None:
            savez(npzFile, **self.sample.__dict__)
Пример #8
0
    def nearestSampleFile(self, inFile, onlyVars=None):
        """
        Sample some or all variables of inFile. This simply returns the
        values of the gridbox the pixel fall into (nearest neighbor).
        """
        from gfio import GFIOctl

        # Instiate grads and open file
        # ----------------------------
        f = GFIOctl(inFile)
        if self.sample == None:
            self.sample = GOCIHandle(inFile)
        if onlyVars is None:
            onlyVars = f.vname

        # Flatten coordinates
        # -------------------
        nt, nr = self.lon.shape
        #tyme = tile(self.tyme,(nr,1)).T
        lons = self.lon.ravel()
        lats = self.lat.ravel()
        tymes = self.tyme.ravel()
        #tymes[:] = self.gtime # use mean granule time

        tymes[isnan(self.Observation_time_minute.ravel())] = self.gtime

        # Loop over variables on file
        # ---------------------------
        for v in onlyVars:
            if self.verb:
                print " <> Nearest sampling ", v
            var = f.sample(v,
                           lons,
                           lats,
                           tymes,
                           algorithm='nearest',
                           Verbose=self.verb)
            self.var = var
            if len(var.shape) == 1:
                self.sample.__dict__[v] = var.reshape((nt, nr))
            elif len(var.shape) == 2:
                var = var.T  # shape should be (nobs,nz)
                self.sample.__dict__[v] = var.reshape((nt, nr, -1))
            else:
                raise IndexError, 'variable <%s> has rannk = %d' % len(
                    var.shape)
Пример #9
0
 def sampleVar(self, ctlfile, vname, aname=None, I=None, **kwds):
     """
     Interpolates variable *vname* from a gridded GFIO collection to obs location/time.
     On input, *aname* is the attribute name to be defined; if not specified it defaults
     to *vname*.
     """
     from gfio import GFIOctl
     f = GFIOctl(ctlfile)
     if aname == None:
         aname = vname
     if I is None:
         self.__dict__[aname] = f.sample(vname, self.lon, self.lat,
                                         self.time, **kwds)
     else:
         self.__dict__[aname][I] = MISSING * ones(len(self.lon))
         self.__dict__[aname][I] = f.sample(vname, self.lon[I], self.lat[I],
                                            self.time[I], **kwds)
Пример #10
0
    def linearSampleFile(self, inFile, onlyVars=None):
        """
        Interpolates some or all variables of inFile.
        """
        from gfio import GFIOctl

        # Open file
        # ---------
        f = GFIOctl(inFile)
        if self.sample == None:
            self.sample = GOCIHandle(inFile)
        if onlyVars is None:
            onlyVars = f.vname

        # Flatten coordinates
        # -------------------
        nt, nr = self.lon.shape
        #tyme = tile(self.tyme,(nr,1)).T
        lons = self.lon.ravel()
        lats = self.lat.ravel()
        tymes = self.tyme.ravel()
        #tymes[:] = self.gtime # use mean granule time

        tymes[isnan(self.Observation_time_minute.ravel())] = self.gtime

        # Loop over variables on file
        # ---------------------------
        for v in onlyVars:
            if self.verb:
                print " <> Linear sampling ", v
            var = f.sample(v, lons, lats, tymes, Verbose=self.verb)
            if len(var.shape) == 1:
                self.sample.__dict__[v] = var.reshape((nt, nr))
            elif len(var.shape) == 2:
                var = var.T  # shape should be (nobs,nz)
                self.sample.__dict__[v] = var.reshape((nt, nr, -1))
            else:
                raise IndexError, 'variable <%s> has rannk = %d' % len(
                    var.shape)
Пример #11
0
    def sampleFile(self, inFile, npzFile=None, onlyVars=None, Verbose=False):
        """
        Interpolates all variables of inFile and optionally
        save them to file *npzFile*
        """
        from gfio import GFIO, GFIOctl, GFIOHandle

        # Instantiate grads and open file
        # -------------------------------
        name, ext = os.path.splitext(inFile)
        if ext in ('.nc4', '.nc', '.hdf'):
            fh = GFIO(inFile)  # open single file
            if fh.lm == 1:
                timeInterp = False  # no time interpolation in this case
            else:
                raise ValueError, "cannot handle files with more tha 1 time, use ctl instead"
        else:
            fh = GFIOctl(inFile)  # open timeseries
            timeInterp = True  # perform time interpolation

        if self.sample == None:
            self.sample = GFIOHandle(inFile)
        if onlyVars is None:
            onlyVars = fh.vname

        nt = self.lon.shape
        tymes = self.time
        lons = self.lon
        lats = self.lat

        # Loop over variables on file
        # ---------------------------
        for v in onlyVars:
            if Verbose:
                print "<> Sampling ", v
            if timeInterp:
                var = fh.sample(v, lons, lats, tymes, Verbose=Verbose)
            else:
                var = fh.interp(v, lons, lats)
            if len(var.shape) == 1:
                self.sample.__dict__[v] = var
            elif len(var.shape) == 2:
                var = var.T  # shape should be (nobs,nz)
                self.sample.__dict__[v] = var
            else:
                raise IndexError, 'variable <%s> has rank = %d' % (
                    v, len(var.shape))

        if npzFile is not None:
            savez(npzFile, **self.sample.__dict__)
Пример #12
0
    def sampleFile(self, inFile, npzFile=None, onlyVars=None, Verbose=True):
        """
        Interpolates all variables of inFile and optionally
        save them to file *npzFile*
        """
        from gfio import GFIOctl, GFIOHandle

        # Instiate grads and open file
        # ----------------------------
        fh = GFIOctl(inFile)
        self.sample = GFIOHandle(inFile)
        if onlyVars is None:
            onlyVars = fh.vname

        nt = self.lon.shape
        nz = self.nz
        tymes = self.tyme
        lons = self.lon
        lats = self.lat

        # Loop over variables on file
        # ---------------------------
        for v in onlyVars:
            if Verbose:
                print "<> Sampling ", v
            var = fh.sample(v,lons,lats,tymes,Verbose=Verbose)
            if len(var.shape) == 1:
                self.sample.__dict__[v] = var
            elif len(var.shape) == 2:
                var = var.T # shape should be (nobs,nz)
                self.sample.__dict__[v] = var
            else:
                raise IndexError, 'variable <%s> has rannk = %d'%len(var.shape)

        if npzFile is not None:
            savez(npzFile,**self.sample.__dict__)            
Пример #13
0
    interpXY = NC4ctl.interpXY_LatLon

if __name__ == "__main__":

    from time import time as now
    from gfio import GFIOctl

    kbeg = 36
    kount = 72 - kbeg + 1

    filename = '/home/adasilva/iesa/MERRAero/inst3d_prog_v.ddf'

    # interp using NC4ctl
    # -------------------
    print "------ NC4ctl ----------"
    f = NC4ctl_(filename)
    lons = linspace(-45.,-30.,100)
    lats = linspace(30.,50.,100)
    dt = f.dt/10
    t0 = datetime(2008,6,29,12)
    times = array([ t0 + i * dt for i in range(100)])
    slp = f.sample('SLP',lons,lats,times,Verbose=True)
    t = f.sample('T',lons,lats,times,kbeg=kbeg,kount=kount,Verbose=True)

    # Redo with GFIOctl for testing
    # -----------------------------
    print "\n------ GFIOctl ----------"
    g = GFIOctl(filename)
    SLP = g.sample('SLP',lons,lats,times,Verbose=True)
    T = g.sample('T',lons,lats,times,Verbose=True,kbeg=kbeg,kount=kount)