[Thread Prev][Thread Next][Index]

Re: [ferret_users] running sum with restart after a missing value




Hi Thomas,

You can do this by a combination of using the event mask transform @EVN and projecting onto a second dimension.

Here's an example

let orig = IF MOD(L[L=1:100],7) NE 0 THEN L[l=1:100] +0*L[L=1:100]

! Create an event mask

let orig_miss = MISSING(ORIG,-999)
let evn = orig_miss[L=@EVN:-999]

let orig_miss_0 = MISSING(ORIG,0)   ! We need to fill in zeros for running sums.


let num_events=`evn[l=@max]+1`   ! 15
let evnk = IF K EQ evn+1 THEN orig_miss_0 ELSE 0  !project

shade/k=1:`num_events` evnk

let mask_after = IF  K GE EVN+1 THEN EVNK  ! Do not want to contaminate sums but we want zero before the sum starts
shade/k=1:`num_events` mask_after

let  sum_on_2d = mask_after[L=@RSUM]
shade/k=1:`num_events` sum_on_2d

let sum_on_1d=ifv orig then sum_on_2d[k=1:`num_events`@sum]  ! Collect and mask missing places. First value in each segment should be L
plot sum_on_1d


It's a bit convoluted and there may be a nicer way to do this.


Cheers,
Russ

On 04/09/17 16:38, Frölicher Thomas wrote:
Dear Ryo,

thanks for your suggestions. However, I would like to calculate a running sum that starts from zero when a missing value is found. Here is a very simple example:

Data (.. is missing values):
.. 4 2 6 3 5 .. .. .. 5 9 2 4 .. 6 7 3 5 8 .. ..

I would like to have:
.. 4 6 12 15 20 .. .. .. 5 14 16 20 .. 6 13 16 21 29 .. ..

Any suggestions would be very appreciated.

Thomas

 


From: Ryo Furue [furue@xxxxxxxxxx]
Sent: Monday, September 04, 2017 8:27 AM
To: Frölicher Thomas
Cc: ferret users
Subject: Re: [ferret_users] running sum with restart after a missing value

Hi Thomas,

I would like to calculate a running sum over multiple time step, which however restarts after a missing value.

That behavior seems to me to be inconsistent; that is, @SUM skips missing values and gives a sum of valid values, whereas @RSUM stops when it finds a missing value.
 
Is there a simple solutions in ferret?

If you just want to skip the missing values in your running sum, you want to assign zeros to them. This is a contrived, simple example:

    let var = TSEQUENCE({1,2,-1.0e34,4,5,6})
    let var2 = ifv var then var else 0
    plot/line/symbol var2[t=@RSUM], var[t=@RSUM]

Hope this helps.

Ryo



[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement