def addVar(self, url, Vars=None, Verbose=True, Levels=None): """ Sample variable along DIAL track. """ from grads import GrADS ga = GrADS(Window=False, Echo=False) fh = ga.open(url) if Levels is not None: ga('set lev %s' % Levels) qh = ga.query('dims') if Vars is None: Vars = ga.query('file').vars elif type(Vars) is StringType: Vars = [ Vars, ] for var in Vars: if Verbose: print ' Working on <%s>' % var if fh.Vars[var.lower()].levs == 0: ga('set lev 1') # 2D variable else: ga.setdim(qh) q = ga.sampleXYT(var, self.lon, self.lat, self.tyme, Verbose=Verbose).data self.__dict__[var] = ma.MaskedArray(q, mask=q >= MAPL_UNDEF)
def _estimate(): from grads import GrADS man = MAN() ga = GrADS(Echo=0,Window=False) # fh = ga.open('nnr_001.modo.ddf') # man.addVar(ga,'nnr_001.modo.tau.npz',expr='tau') # man.addVar(ga,'nnr_001.modo.tau_.npz',expr='tau_') ga('reinit') fh = ga.open('nnr_001.mydo.ddf') man.addVar(ga,'nnr_001.mydo.tau.npz',expr='tau') man.addVar(ga,'nnr_001.mydo.tau_.npz',expr='tau_')
def getMERRA(self, npzFile=None): """ Get Met fields from MERRA. """ from grads import GrADS ga = GrADS(Window=False, Echo=False) ga.open( 'http://goldsmr2.sci.gsfc.nasa.gov:80/dods/MAT1NXFLX') # 2D fluxes self.sampleFile(ga, onlyVars=('ustar', 'bstar', 'pblh', 'hflux', 'eflux', 'rhoa', 'tsh')) ga('close 1') ga.open( 'http://goldsmr1.sci.gsfc.nasa.gov:80/dods/MAT3FVCHM') # 3D fields ga('set z 1 72') self.sampleFile(ga, onlyVars=('u', 'v', 't', 'qv', 'delp'), npzFile=npzFile)
def getFP(self, npzFile=None, dir='/home/adasilva/iesa/aerosol/experiments/seac4rs_01'): """ Get Met fields from GEOS-5 Forward Processing. """ from grads import GrADS ga = GrADS(Window=False, Echo=False) ga.open(dir + '/tavg1_2d_flx_Nx.ddf') self.sampleFile(ga, onlyVars=('ustar', 'bstar', 'pblh', 'hflux', 'eflux', 'rhoa', 'tsh')) ga('close 1') ga.open(dir + '/inst3_3d_asm_Nv.ddf') ga('set z 1 72') self.sampleFile(ga, onlyVars=('u', 'v', 't', 'qv', 'o3', 'delp'), npzFile=npzFile) ga('close 1')
def getMERRAero(self, npzFile=None, dir='/home/adasilva/iesa/MERRAero'): """ Get Met fields from MERRA. """ from grads import GrADS ga = GrADS(Window=False, Echo=False) ga.open(dir + '/geosgcm_surf.ddf') self.sampleFile(ga, onlyVars=('ustar', 'bstar', 'pblh', 'shfx', 'lhfx', 'rhos', 'ts')) ga('close 1') ga.open(dir + '/inst3d_prog_v.ddf') ga('set z 1 72') self.sampleFile(ga, onlyVars=('u', 'v', 't', 'qv')) ga('close 1') ga.open(dir + '/inst3d_aer_v.ddf') ga('set z 1 72') self.sampleFile(ga, onlyVars=('delp', ), npzFile=npzFile)
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
#........................................................................... if __name__ == "__main__": from time import time as now from grads import GrADS f = GFIOctl('/nobackup/ARCTAS/opendap/arctas.ddf') 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)]) ga = GrADS(Window=False, Echo=0) fh = ga.open('/nobackup/ARCTAS/opendap/arctas.ddf') # Interpolating ps # ---------------- t_ = now() ps = f.sample('ps', lons, lats, times, Verbose=True) print "---> Interpolating <ps> with GFIO took ", now() - t_ t_ = now() ps_ = ga.sampleXYT('ps', lons, lats, tyme=times, Verbose=True) print "---> Interpolating <ps> with PyGrADS took ", now() - t_ # Interpolating RH # ---------------- print "" t_ = now()
#!/usr/bin/env python #Martin Iglesias Github SudestadaARG import numpy as np import os import argparse import gc from datetime import datetime from datetime import timedelta from grads import GrADS from mpl_toolkits.basemap import interp from lectura_gefs_ctl import read_ctl ga = GrADS(Bin='grads', Echo=False, Port=True, Window=False) path_in = '/data/miglesias/GEFS_FCST12/GEFS_prono12h' path_out = '/data/miglesias/verificacion_doc/variables_GEFS/' # Defino argumentos para indicarle la fecha y el miembro por linea de comando: parser = argparse.ArgumentParser(description='Year Month Day Hour Member FCST') parser.add_argument('Year', type=int) parser.add_argument('Month', type=int) parser.add_argument('Day', type=int) parser.add_argument('Hour', type=int) parser.add_argument('Member', type=int) parser.add_argument('FCST', type=str) args = parser.parse_args() Y = args.Year M = args.Month D = args.Day H = args.Hour
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 _GenericSetUp(self, bin, url): self.ga = GrADS(Bin=GrADSBinaryFiles[bin], Echo=False, Window=False) self.fh = self.ga('open ' + url)
#!/usr/bin/env python # # Simple script for computing percentiles for each horizontal # gridpoint. # from pylab import * from numpy import float32 from grads import GrADS, GaField # Start GrADS and open the data file # ---------------------------------- ga = GrADS(Bin='gradsnc',Echo=False,Port=True) ga.open('../data/slp_djf.nc') # Extract a timeseries # -------------------- ga('set t 1 41') x = ga.exp('djfslp/100') g = x.grid # Transpose spatial/temporal dimensions # ------------------------------------- (nt,ny,nx) = x.shape; x = transpose(x.reshape((nt,nx*ny))) # Compute percentiles using Matlab compatible prctile function # ------------------------------------------------------------ p = ( 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 ) y = zeros((nx*ny,10),dtype=float32) for i in range(nx*ny):