[Thread Prev][Thread Next][Index]

Re: [ferret_users] IF-THEN-ELIF-ELSE logic



Hi -
Sure, there are lots of ways to put the masks together.

Just to add a few general comments to this thread - 
1) In creating masks, we used addition here. Sometimes multiplication can also be useful, if the masks are constructed from 1's, 0's and missing-values.  Say if you wanted to create your latitude mask and then apply it just in the central Pacific,
let yy = y[gy=myvar]
let xx = x[gx=myvar]

let mnorth = if yy gt 23 then 2 else 0
let msouth = if yy lt -23 then 3 else 0
let mtropic = if (yy ge -23 and yy le 23) then 1 else 0
let latmask = mnorth + msouth + mtropic

let pacific = if (xx gt 130 and xx le 240) then 1

let xymask = latmask * pacific
...
This uses the ideas of  "conformability" were the x and y masks are multiplied and this generates a 2D mask. The only thing to be careful of here is to make sure you know the x range of the underlying grid -- if it's defined on -180:180 then the x values in your definition should also be in that range.

2) There's an FAQ about creating masks for computation based on an _expression_ involving the variables (what's the integral of the temperature above the 20 degree isotherm?) See Averaging/integrating over irregular regions in Ferret  http://ferret.pmel.noaa.gov/FERRET_17sep07/FAQ/analysis/avg_integ_irreg.html


David Wang wrote:
Hi Ansley,

I should have thought about it. This is definitely a clear solution and easy to keep track. Mine was convoluted so I won't mention it.

BTW, I'd define the third mask by

let mtropic = if (yy ge -23 and yy le 23) then 1 else 0

which appears more straightforward to me. Of course it's my personal taste.

Thank you a lot!
D.

On Fri, Mar 27, 2009 at 6:24 PM, Ansley Manke <Ansley.B.Manke@xxxxxxxx> wrote:
Hi
Yes, I thought that was probably what you were asking. One way to do this is with three masks that can be added:
let mnorth = if yy gt 23 then 2 else 0
let msouth = if yy lt -23 then 3 else 0

let mtropic = if (msouth + mnorth) EQ 0 then 1 else 0

let mask = msouth + mnorth + mtropic

! as a check on what you've defined:
let xzero = 0*x[gx=xvar]
shade/lev=3 mask + xzero
Which is probably what you're thinking of with intermediate variables. It keeps things reasonably simple at the expense of extra command lines.

Then you can do things like this
let topic_data = if mask eq 1 then mask* myvar
...
Ansley

David Wang wrote:
Hi Ansley,

Thanks for asking. Let's me elaborate it. I'd like to define a few regional masks for an ocean model so that I can keep monitoring regional diagnostics without dumping 3d fields when unnecessary. This is by tagging each region with an unique index (number) on a single horizontal field. For the sake of simplicity, let's say I need three masks: tropical ocean (1), north ocean (2), and south ocean (3). In Ferret, I would have:

let yy = y[gy=myvar]
let mask = if yy gt 23.5 then 2 elif yy lt -23.5 then 3 else 1

where myvar is an arbitrary 2d field on the model grid.

I understand one can definitely break down the above definition into two steps using an intermediate variable. But this becomes increasingly awkward when the number of regions increases.

I could miss something obvious. If so, please enlighten me.

Thanks,
D.

On Fri, Mar 27, 2009 at 12:04 PM, Ansley Manke <Ansley.B.Manke@xxxxxxxx> wrote:
Hi David,
Can you write out some Ferret-like commands that show the syntax you're thinking of?  Are you trying to define a variable using a number of cases?  Often there are reasonable alternatives to the way one first thinks of doing something.

Ansley



David Wang wrote:
Hi Ferreters,

Well, I may sound pretty greedy. But is there a plan to extend the IF-THEN-ELSE logic to some kind of IF-THEN-ELIF-ELSE logic? It would be quite useful for multi-region masking. (Note: I'm not talking about conditional execution, which does have ELIF)

Thanks,
D.

--
life grows, death doesn't. there is no dress rehearsal.



--
life grows, death doesn't. there is no dress rehearsal.



--
life grows, death doesn't. there is no dress rehearsal.

[Thread Prev][Thread Next][Index]

Contact Us
Dept of Commerce / NOAA / OAR / PMEL / TMAP

Privacy Policy | Disclaimer | Accessibility Statement