Python Forum
Need help for finding cumulative values in a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help for finding cumulative values in a loop
#1
Greetings,

I would like to compute all annual cumulative 5-consecutive day maximum values in a 3D matrix using a for loop. To be more specific, the variable "Quantity" is a 3D matrix that is composed of 3 elements: the first dimension being time (in days), the second being latitude (64 lines of latitude), and the third being longitude (128 degrees of longitude). The total number of days is 51,100, making up 140 years. Previously, I converted the number of days into years (by dividing by 365, shown in the code below) in order to simply derive the maximum value every year for every grid cell.

Ultimately, the goal is now to still similarly obtain maximum values at an annual time scale, as before (which the below code will do already), but now I want to try to specifically obtain the maximum cumulative values for every year for every grid cell based on 5 consecutive day periods. So, basically, 140 maximum values (because there are 140 years) for each grid cell, but the maximums would be based on 5-consecutive day periods, rather than a single day (as I did already as my first task).

The way I define the 5-consecutive days is as follows. For example, the first set of 5 consecutive days is as follows: Day 0+Day 1+Day 2+Day 3+Day 4. For my particular case, what I was envisioning would be to begin deriving the cumulative value over Day 0 to Day 4 (so, the first 5 days in year 0), and then move to the next group of 5 consecutive days, from Day 1 to Day 5 (i.e. Day 1+Day 2+Day 3+Day 4+Day 5), and then Day 2 to Day 6 (i.e. Day 2+Day 3+Day 4+Day 5+Day 6), and then Day 3 to Day 7, and then Day 4 to Day 8.....etc....all the way to the end of the 51,100 day period (140 years). In this way, there would be about 50,000-51,000 5-consecutive day periods over the 140 years, but the existing np.max function (in the code below) would allow Python to return the maximum cumulative values for each year for each grid cell. In this case, would you use the np.sum function to derive cumulative values, and then have the previously generated code (see below) to produce the annual maximum cumulative values?

Finally, the "time-2" and "time+2" in the below code gives a range of days relative to a specified day. For example, at Day 2, the range would be Day 0 to Day 4 (because 2-2=0 and 2+2=4, so Day 0 to Day 4, relative to Day 2). Using that approach, though, I cannot use Day 0 and Day 1, as well as the final two days in the period, since that would lead to a range that contains days that do not exist. For instance, if you subtract 2 from Day 0 or Day 1, you will end up with a negative day (-2 and -1, respectively). Likewise, you will end up with days that exceed 51100 for the final two days due to time+2 (Day 51,101 and Day 51,102, which don't exist). Thus, some kind of condition would need to be used to tell the loop to ignore those particular days (the very first two days and the very last two days), but I'm just uncertain how to specify the conditions in the for loop and wanted to know of a way to use them for this case?

Quantity=Q
Quantity2=np.zeros(Quantity.shape)
Year=np.arange(Quantity.shape[0])//365
n_year = max(Year)+1
onedaymax=np.empty((n_year, )+Quantity.shape[1:3])
fivedaymax=np.empty((n_year, )+Quantity2.shape[1:3])
for time in range(Quantity.shape[0]):
                Quantity2[time,...]
                fiveconsecutivedays=np.sum(Quantity[time-2:time+2,...], axis=0) 
                        if time in Quantity.shape[0]>1 and <50,099
                                continue
                                        for y in range(n_year):
                                                onedaymax[y, ...]=np.max(Quantity[Year==y, ...], axis=0)
                                                fivedaymax[y, ...]=np.max(Quantity2[Year==y, ...], axis=0)
The problem is within the first for loop, and I'm not sure that was is specified within it is the right approach. The "if time" portion is intended to try to omit the first two days and last two days of the period, but I'm unsure if that it specified correctly for my purposes, or if it is even relevant. What would you modify there?

Many thanks, and I would GREATLY appreciate any feedback!
Reply


Messages In This Thread
Need help for finding cumulative values in a loop - by Lightning1800 - May-22-2018, 08:15 PM
Looping issue - please help!! - by Lightning1800 - May-23-2018, 07:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through values and compare edroche3rd 6 684 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,097 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,482 Jul-27-2022, 08:50 PM
Last Post: rob101
  How do loop over curl and 'put' different values in API call? onenessboy 0 1,219 Jun-05-2022, 05:24 AM
Last Post: onenessboy
  Loop through values in dictrionary and find the same as in previous row Paqqno 5 1,901 Mar-27-2022, 07:58 PM
Last Post: deanhystad
  How to add for loop values in variable paulo79 1 1,441 Mar-09-2022, 07:20 PM
Last Post: deanhystad
Exclamation Compare values in a for loop. penahuse 1 2,366 Feb-22-2021, 07:01 AM
Last Post: buran
  Calculate column with cumulative return tgottsc1 1 1,855 Jan-25-2021, 12:52 PM
Last Post: buran
  returning values in for loop Nickd12 4 12,165 Dec-17-2020, 03:51 AM
Last Post: snippsat
  Finding Max and Min Values Associated with Unique Identifiers in Python ubk046 1 2,044 May-08-2020, 12:04 PM
Last Post: anbu23

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020