HWRF  trunk@4391
Public Member Functions | List of all members
hwrf.regrib.RegribMany Class Reference

Runs regrib operations on many input times, sending output to an hwrf.gribtask.GRIBTask. More...

Detailed Description

Runs regrib operations on many input times, sending output to an hwrf.gribtask.GRIBTask.

This class keeps track of a set of named regribbing operations and target grids. Each operation may connect to another by name, allowing the output of one operation to be the input to one or more other operations. The RegribMany.make can then produce the output of an operation, on the condition that its inputs from earlier operations are ready.

The RegribMany is not intended to be used alone. Instead, the GRIBTask class in hwrf.gribtask should be used to execute RegribMany tasks. You can run multiple GRIBTasks at the same time, on the same RegribMany outputs, and the GRIBTasks will automatically work together via file locking and a database to produce the outputs in parallel, as fast as possible.

Here is a simple example of the use of a RegribMany object:

1 # This code assumes a PostManyWRF object is available as "mypost",
2 # and HWRFConfig as "conf", and two WRFDomain are available as
3 # "d01" and "d02". We also assume the post has already been run.
4 
5 r = RegribMany()
6 getd01 = igrb1(mypost,domain=d01)
7 getd02 = igrb1(mypost,domain=d02)
8 r.add('cgrid',clatlon(getd02,res=[0.05,0.05],size=[30.,30.],
9  scan=136,n=[600,600]))
10 r.add('cGRIB1',getd01*r.grid('cgrid')+getd02*r.grid('cgrid'))
11 r.add('cGRIB2',r.GRIB('cGRIB1') * GRIB2)
12 r.to_intercom('output_f{fahr:03d}.grib2','cGRIB2')
13 gribber = GribTask(conf.datastore,conf,'mygrib',r,conf.cycle,
14  126*3600, 6*3600)
15 gribber.run()

Now lets step through that and explain what it does.

1 r = RegribMany() - creates a RegribMany.

First, we create the RegribMany object. We're actually doing this in a sloppy way: it will search the $PATH for cnvgrib, wgrib, and other programs. It is better to specify the exact location. That can be done by passing arguments to the RegribMany constructor. See the RegribBase instructor for a list.

1 getd01 = igrb1(mypost,domain=d01)
2 getd02 = igrb1(mypost,domain=d02)

This creates a pair of GRIB1Selectors, operations recognized by RegribMany. A GRIB1Selector takes a Task, in this case mypost, and calls its "products" function to get a Product as input to the next step in a regribbing operation. The domain=d01 is passed into products, as is the time=X for each time X the RegribMany processes.

1 r.add('cgrid',clatlon(getd02,res=[0.05,0.05],size=[30.,30.],
2  scan=136,n=[600,600]))

This instructs the RegribMany to add an operation to its dictionary. The operation is called "cgrid", and consists of a GRIBGridCompute created by the clatlon function, defined later in this module. The clatlon takes our getd02, and creates a 30x30 degree, 600x600 gridpoint, 0.05 degree grid, centered on that domain at each time. If domain d02 moves, this clatlon output will be a moving domain.

1 r.add('cGRIB1',getd01*r.grid('cgrid')+getd02*r.grid('cgrid'))

This adds another operation to the RegribMany's dictionary, this time a grib output operation called cGRIB1. The r.grid('cgrid') looks up the cgrid operation we just inserted, connecting that to the cGRIB1. Recall that getd01 and getd02 are also operations, and produce a GRIB1 file. When we multiply that by a grid, it creates a new operation that will use copygb to output a new GRIB1 file on that grid. Hence, we have created two temporary files:

1 getd01*r.grid('cgrid') = domain 1's post output on the cgrid
2 getd02*r.grid('cgrid') = domain 2's post output on the cgrid

When we add those two together, it pastes the second one on top of the first via a copygb merge operation. That result is then stored as cGRIB1.

1 r.add('cGRIB2',r.GRIB('cGRIB1') * GRIB2)

This adds a third operation to the RegribMany, called cGRIB2. The r.GRIB('cGRIB1') looks up the operation that creates cGRIB1, connecting that to the new cGRIB2. When we multiply by the special constant GRIB2, it creates a new operation to convert that file to GRIB2.

1 r.to_com('output_0p05_degree_f{fahr:03d}.grib2','cGRIB2')

This sets the delivery location for the cGRIB2 outputs. It will create files with names like:

1 output_0p05_degree_048.grib2 # 48hr output file

Without that command, the output will go to automatically-generated locations somewhere in intercom, which is likely not what you want.

1 gribber = GRIBTask(conf.datastore,conf,'mygrib',r,conf.cycle,
2  126*3600, 6*3600)

This creates a GRIBTask that will execute the regribber we just made.

The GRIBTask is configured to be able to run our RegribMany for all forecast times starting at the analysis time (conf.cycle) and running to 126 hours afterwards (126*3600), every six hours (6*3600). The GRIBTask will set the time= keyword argument to mypost.products, allowing each RegribMany operation to run on the correct input time.

1 gribber.run()

This tells the gribber to run to completion, generating our output files.

Definition at line 311 of file regrib.py.

Inheritance diagram for hwrf.regrib.RegribMany:
hwrf.regrib.RegribBase

Public Member Functions

def __init__ (self, _copy=None, workprefix=None, kwargs)
 Creats a new RegribMany. More...
 
def __enter__ (self)
 Has no effect. More...
 
def __exit__ (self, etype, evalue, traceback)
 Has no effect. More...
 
def has_name (self, name)
 Returns True if an operation by the given name exists. More...
 
def reset (self)
 Erases all cached results. More...
 
def copy (self)
 Returns a copy of self with no cached results. More...
 
def GRIB (self, name)
 Returns a GRIB1Fetcher object that will grab the GRIB1 data with the given name. More...
 
def nonGRIBOps (self)
 Iterates over all non-GRIBOp operations yielding tuples containing the name and operation. More...
 
def GRIBOps (self)
 Iterates over all GRIBOp operations yielding tuples containing the name and operation. More...
 
def is_grid (self, name)
 Returns True if the name corresponds to a grid. More...
 
def grid (self, name)
 Returns a GRIBGridFetcher object that will grab the grid structure with the given name. More...
 
def subset (self, name)
 Returns a GRIBSubsetterFetcher object that will grab the GRIB subsetter with the given name. More...
 
def add (self, name, arg, kwargs)
 Adds the given generator "arg" under the specified name (in "name") to this object. More...
 
def to_com
 Requests delivery to the com directory in file "location" of operation "name". More...
 
def to_intercom
 Requests delivery to the intercom directory in file "location" of operation "name". More...
 
def deliveries
 Iterates over all deliveries yielding a six element tuple. More...
 
def has_deliveries (self)
 Returns True if there are any requested deliveries and False otherwise. More...
 
def has_data (self, name, kwargs)
 Returns True if there is a cached result for the specified operation. More...
 
def is_ready (self, name, kwargs)
 Determines if the specified operation is ready. More...
 
def input_valid (self, name, kwargs)
 Determines if it is possible to run the specified operation with the given keyword arguments. More...
 
def make (self, name, kwargs)
 Executes the operation previously stored in self.add(), passing it the given keyword arguments, returning the result. More...
 
def names (self)
 Iterates over the names provided to self.add(name,...) in the order they were added. More...
 
- Public Member Functions inherited from hwrf.regrib.RegribBase
def __init__
 Creates a RegribBase. More...
 
def getcopygb (self)
 Returns the copygb ImmutableRunner.
 
def getcnvgrib_g12 (self)
 Returns the cnvgrib -p32 -g12 command, which should be an ImmutableRunner.
 
def getcnvgrib_g21 (self)
 Returns the cnvgrib -g21 command, which should be an ImmutableRunner. More...
 
def getwgrib (self)
 Returns the wgrib command, which should be an ImmutableRunner. More...
 
def getlog (self)
 Returns the logger.Logging object to use for logging messages, or None if no logger was provided to the constructor. More...
 
def setlog (self, val)
 Sets the logger.Logging object to use for logging messages. More...
 
def getprodargs (self)
 Returns the array of arguments to send to the products iterators. More...
 
def getprodkwargs (self)
 Returns a dict of keyword arguments to send to the products iterators. More...
 
def setsync_frequently (self, flag)
 Sets the sync_frequently flag. More...
 
def getsync_frequently (self)
 Returns the value of the sync_frequently flag. More...
 

Additional Inherited Members

- Properties inherited from hwrf.regrib.RegribBase
 sync_frequently
 Boolean flag: should we call sync frequently? More...
 
 logger
 A logging.Logger for log messages. More...
 
 prodargs
 A list of additional positional arguments to send to Task.products. More...
 
 prodkwargs
 A dict of additional keyword arguments to send to Task.products. More...
 
 wgrib
 A produtil.prog.ImmutableRunner for running wgrib, or None if unavailable. More...
 
 copygb
 A produtil.prog.ImmutableRunner for running copygb, or None if unavailable. More...
 
 cnvgrib_g21
 A produtil.prog.ImmutableRunner for running cnvgrib to convert from GRIB2 to GRIB1, or None if unavailable. More...
 
 cnvgrib_g12
 A produtil.prog.ImmutableRunner for running cnvgrib to convert from GRIB1 to GRIB2, or None if unavailable. More...
 

Constructor & Destructor Documentation

def hwrf.regrib.RegribMany.__init__ (   self,
  _copy = None,
  workprefix = None,
  kwargs 
)

Creats a new RegribMany.

Parameters
workprefixThe workprefix, if specified, sets the location in which to run. The _copy is used by the self.copy() for copying.
_copy,kwargsAll other arguments are passed to the superclass RegribBase.__init__ constructor .

Definition at line 452 of file regrib.py.

Member Function Documentation

def hwrf.regrib.RegribMany.__enter__ (   self)

Has no effect.

This is for forward compatibility to a later thread-safe implementation.

Note
This used to be implemented but it turns out that Python locks are VERY slow, so the locking had to be removed. Multiple simultaneous regrib operrations are now done through multiple processes

Definition at line 484 of file regrib.py.

def hwrf.regrib.RegribMany.__exit__ (   self,
  etype,
  evalue,
  traceback 
)

Has no effect.

This is for forward compatibility to a later thread-safe implementation.

Parameters
etype,evalue,tracebackException information.
Note
This used to be implemented but it turns out that Python locks are VERY slow, so the locking had to be removed. Multiple simultaneous regrib operations are now done through multiple processes.

Definition at line 492 of file regrib.py.

def hwrf.regrib.RegribMany.add (   self,
  name,
  arg,
  kwargs 
)

Adds the given generator "arg" under the specified name (in "name") to this object.

Parameters
namethe operation name to query
argA object of a subclass of GRIBBase
kwargsStored for later passing to function calls to arg

Definition at line 548 of file regrib.py.

Referenced by produtil.fileop.FileWaiter.add().

def hwrf.regrib.RegribMany.copy (   self)
def hwrf.regrib.RegribMany.deliveries (   self,
  name = None 
)

Iterates over all deliveries yielding a six element tuple.

Parameters
nameThe name of the operation of interest. The tuple contents:
  1. "com" or "intercom"
  2. operation name
  3. location within target directory
  4. requested category or None
  5. requested prodname or None
  6. keep flag from the to_com or to_intercom

Definition at line 604 of file regrib.py.

def hwrf.regrib.RegribMany.GRIB (   self,
  name 
)

Returns a GRIB1Fetcher object that will grab the GRIB1 data with the given name.

Parameters
namethe operation name to query

Definition at line 514 of file regrib.py.

def hwrf.regrib.RegribMany.GRIBOps (   self)

Iterates over all GRIBOp operations yielding tuples containing the name and operation.

Definition at line 526 of file regrib.py.

def hwrf.regrib.RegribMany.grid (   self,
  name 
)

Returns a GRIBGridFetcher object that will grab the grid structure with the given name.

Parameters
namethe operation name to query

Definition at line 536 of file regrib.py.

def hwrf.regrib.RegribMany.has_data (   self,
  name,
  kwargs 
)

Returns True if there is a cached result for the specified operation.

Parameters
kwargsThe keyword arguments are passed on to the get_data routine for the caching object (usually an hwrf.gribtask.GRIBTask).
nameThe operation name.

Definition at line 625 of file regrib.py.

def hwrf.regrib.RegribMany.has_deliveries (   self)

Returns True if there are any requested deliveries and False otherwise.

Returns
True if to_com() or to_intercom() were called for a known name.

Definition at line 618 of file regrib.py.

def hwrf.regrib.RegribMany.has_name (   self,
  name 
)

Returns True if an operation by the given name exists.

Parameters
namethe operation name to query

Definition at line 501 of file regrib.py.

def hwrf.regrib.RegribMany.input_valid (   self,
  name,
  kwargs 
)

Determines if it is possible to run the specified operation with the given keyword arguments.

Parameters
nameObtains the operation by the specified name.
kwargsThe input_valid function is called on the generator for the named operation, passing these keyword arguments.

Definition at line 656 of file regrib.py.

def hwrf.regrib.RegribMany.is_grid (   self,
  name 
)

Returns True if the name corresponds to a grid.

Parameters
namethe operation name to query

Definition at line 532 of file regrib.py.

def hwrf.regrib.RegribMany.is_ready (   self,
  name,
  kwargs 
)

Determines if the specified operation is ready.

Parameters
nameObtains the operation by the specified name.
kwargsThe is_ready funciton is called on the named operation's generator, passing the given keyword arguments to it.

Definition at line 641 of file regrib.py.

Referenced by hwrf.regrib.RegribMany.has_data().

def hwrf.regrib.RegribMany.make (   self,
  name,
  kwargs 
)

Executes the operation previously stored in self.add(), passing it the given keyword arguments, returning the result.

If a cache is available (usually a GRIBTask), stores the result in that cache.

Parameters
namethe name of the operation of interest
kwargsPassed to the make function in the generator for the named operation

Definition at line 672 of file regrib.py.

def hwrf.regrib.RegribMany.names (   self)

Iterates over the names provided to self.add(name,...) in the order they were added.

Definition at line 703 of file regrib.py.

def hwrf.regrib.RegribMany.nonGRIBOps (   self)

Iterates over all non-GRIBOp operations yielding tuples containing the name and operation.

Definition at line 520 of file regrib.py.

def hwrf.regrib.RegribMany.reset (   self)

Erases all cached results.

Definition at line 506 of file regrib.py.

def hwrf.regrib.RegribMany.subset (   self,
  name 
)

Returns a GRIBSubsetterFetcher object that will grab the GRIB subsetter with the given name.

Parameters
namethe operation name to query

Definition at line 542 of file regrib.py.

def hwrf.regrib.RegribMany.to_com (   self,
  location,
  name,
  category = None,
  prodname = None,
  keep = True 
)

Requests delivery to the com directory in file "location" of operation "name".

Parameters
category,prodnameThe category and prodname are used to create the FileProduct object.
keepSent to the final deliver_file operation. It should be left as True.
namethe operation's name
locationthe destination, which will be sent through hwrf.config.HWRFConfig.conftimestrinterp() by the hwrf.gribtask.GRIBTask

Definition at line 559 of file regrib.py.

def hwrf.regrib.RegribMany.to_intercom (   self,
  location,
  name,
  category = None,
  prodname = None,
  keep = True 
)

Requests delivery to the intercom directory in file "location" of operation "name".

Parameters
category,prodnameThe category and prodname are used to create the FileProduct object.
keepSent to the final deliver_file operation. It should be left as True.
namethe operation's name
locationthe destination, which will be sent through hwrf.config.HWRFConfig.conftimestrinterp() by the hwrf.gribtask.GRIBTask

Definition at line 582 of file regrib.py.


The documentation for this class was generated from the following file: