vendredi 25 octobre 2019

Extract certain conditions/values inside a loop

I have a dataset, which I developed using a loop (code below).

## A subset of the data
deficit = (E_daily[:,160,100] - P_daily[:,160,100])  # A view of `deficit` is given below
deficit_cum = np.zeros([365])   
start = 0
stop = 0
for i in range(365):
    deficit_cum[i] = deficit[i] + deficit_cum[i-1]
    if deficit_cum[i] >= 0:
        if np.nanmax(deficit_cum) <= deficit_cum[i]:
            stop = i
        else:
            continue
    else:
        deficit_cum[i] = 0
        if np.nanmax(deficit_cum) > deficit_cum[i]:
            continue
        else:
            start = i     #Start is not defined correctly by me

This is how deficit looks, before the loop enter image description here

Now I am also interested in the index of the values from where my loop starts and where it ends. I know it's confusing, here is an example (below is how deficit_cum looks):

enter image description here

In the figure above, greenline defines start (x-axis) and redline defines stop (x-axis) from the code. So basically I want the maximum value to be my stop point. But I want my start to be less than stop and there shouldn't be any negative point between start and stop. So for the diagram below my start should be around 111 and stop should be around 236.

I think I know how to get stop (code above), but I am still having trouble defining start.

Aucun commentaire:

Enregistrer un commentaire