Calculate 12 hour rainfall totals

Post any questions, ideas, or topics related to Jython and Python scripting.
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: Calculate 12 hour rainfall totals

Post by tomw »

Hi Sim...

Thank you. Just one question remains: both of the files you sent me, for the variable "Total_precipitation@surface", there is no T+3 hour data. The first time is 2012-02-14 12:00:00Z, and the second one is 2012-02-14 18:00:00Z. After that, the times are, indeed, 3 hours apart until T+144.

I now understand that the grids at each time are the total preciptation since the beginning of the model run, and you would like to have a time sequence of just the 6- or 12- hour precip totals. I'm working on some Jython code to do that for you, but wanted to ask about the "T+3" before going much further.

Thanks.
tom
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: Calculate 12 hour rainfall totals

Post by tomw »

Hi Sim...

While I await your reply (time zone differences!), I thought I would get you started. This is a two-step process.

1. In your Jython User Library, copy and paste this code, and then click "Save":

Code: Select all

def getStepDiffsFromAccum(grid,step):
  # grid is time sequence
  # step is number of hours desired
  b = grid.clone();                 # make a copy of the grids
  ds = b.getDomainSet();        # get the domain set
  times = ds.getDoubles();      # from that, get a list of the times (secs)
  stepsecs = float(step) * 3600.0;   # convert to seconds

  for i in xrange(len(b)):
    # now for each time, get the index of that time minus step
    k = ds.valueToIndex( [[times[0][i]-stepsecs]])[0];
    if k == -1:  k = 0;       # if out-of-range, use first one...or do something else!
    b[i] = grid[i] - grid[k];   # get the difference
  return b;


2. Then in the Data Explorer window, in the Field Selector, create a new formula with an appropriate name, and the "Formula" should be simply:

Code: Select all

getStepDiffsFromAccum(precip, hours[isuser=true])

You might open the "Advanced Settings" and select appropriate Displays. Then save your formula.

When you want to run this, just select one of your files in the Data Source tab of the Explorer, then in the Field Selector, select your formula (and appropriate Display type). When you click "Create Display", a small window will pop up and ask for a value for "hours" -- put in either 6 or 12. (If you want to skip this step, you can change the "formula" above so that the second parameter is a value and not "step[isuser=true]" (which causes the little pop-up window to appear and ask the user to enter a value for "step").

It will then pop up a window where you should select the Precipitation data (all times!). When you click OK in this window, it will compute the differences and then create the display.

The only remaining issue, as far as I know, is what to do about the first few grids. You are free to modify the Jython 'def' file as needed, or I can offer some suggestions. Please let me know if you have questions.

tom

If you are interested: The idea of the "domain set" appears in this function because all data inside McIDAS-V are VisAD Data objects and VisAD represents data like this as a "function" which maps from a "domain" to a "range". In this case, the "domain" are the time steps, and the "range" is the gridded data (which is also a "function" that maps from Longitude,Latitude to values of Precipitation). The "type" of these data is notated in VisAD as:

Code: Select all

(Time -> ((Longitude, Latitude) -> proj0/Total_precipitation[unit:m]))
User avatar
Sim
Posts: 9
Joined: Thu Jun 09, 2011 2:17 am

Re: Calculate 12 hour rainfall totals

Post by Sim »

Dear Tom,

Thank you very much.

I put the grib files, which should consist of the T+3hr forecast, to your ftp location again. They are H2Dnew.grb and H3Dnew.grb.

I have successfully run the Jython program and plotted the 6-hrly and 12-hrly total rainfall. May I have your suggestion to modify the program so that the rainfall at first few forecast hours, such as T+0 and T+3hr, are zero/null value when plotting 6-hrly rainfall, and T+0 to T+9hr are zero/null value when plotting 12-hrly rainfall, etc.

Thank you.

Regards,
Sim
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: Calculate 12 hour rainfall totals

Post by tomw »

Hello Sim...

It is your choice about the first few time steps. If you want zero values, then you could just change the "if" statement to read:

Code: Select all

    if k == -1:  k = i;

This will cause the values to be subtracted from itself, resulting in zero.

If, on the other hand, you want a "null" for the time, then you could do this:

Code: Select all

    if k == -1:
      b[i] = None;
    else:
      b[i] = grid[i] - grid[k];   # get the difference


I suggest you try both and see which one is more useful for you. Also, you might also pick (or create) a color table that uses "transparent" for the zero (low) values. See, for example, the "System -> Precipitation (transparent)" one.

tom
User avatar
Sim
Posts: 9
Joined: Thu Jun 09, 2011 2:17 am

Re: Calculate 12 hour rainfall totals

Post by Sim »

Dear Tom,

Thank you very much.

Regards,
Sim
Post Reply