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
#11
Hi buran,

My apologies for that.

What I am trying to do is this: I have a 3D matrix, called "Quantity". Quantity is composed of three elements:

[Days, Latitude, Longitude]

The element of Days is a unit of time and is composed of 51,100 days, from Day 0 to Day 50,099. There are 64 lines of latitude and 128 lines of longitude (latitude and longitude form "grid cells").

My goal is to compute maximum annual cumulative values for 5-consecutive day periods for each grid cell. The first five consecutive day would be Day 0+Day 1+Day 2+Day 3+Day 4 (so, Day 0 to Day 4). The second period would be Day 1+Day 2+Day 3+Day 4+Day 5 (Day 1 to Day 5), the third would be Day 2+Day 3+Day 4+Day 5+Day 6 (so, Day 2 to Day 6)....etc. The idea is to get the loop to produce the maximum cumulative 5-consecutive day values achieved every year for each grid cell. For example, adhering to the structuring of my 5-consecutive days, there would be 361 such periods in year 0. And then another 361 periods in year 1, and another 361 in year 2.....etc....all the way to year 139. The idea is to derive which of those 361 periods have the highest value for individuals years, and for each grid cell. Ultimately, I am deriving 140 maximums for each year for each grid cell.

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?

I tried the above above code, but it led to the previously mentioned error, and I just wanted to know of a way to re-structure the line where the error is occurring in order to avoid it. Conceptually, what I have in that first loop is good, but I don't know how to get rid of that error to allow the code to run. Is there another way to simply have the loop account for Day 2 to Day 50099?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through values and compare edroche3rd 6 628 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,962 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,430 Jul-27-2022, 08:50 PM
Last Post: rob101
  How do loop over curl and 'put' different values in API call? onenessboy 0 1,197 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,839 Mar-27-2022, 07:58 PM
Last Post: deanhystad
  How to add for loop values in variable paulo79 1 1,411 Mar-09-2022, 07:20 PM
Last Post: deanhystad
Exclamation Compare values in a for loop. penahuse 1 2,336 Feb-22-2021, 07:01 AM
Last Post: buran
  Calculate column with cumulative return tgottsc1 1 1,815 Jan-25-2021, 12:52 PM
Last Post: buran
  returning values in for loop Nickd12 4 11,854 Dec-17-2020, 03:51 AM
Last Post: snippsat
  Finding Max and Min Values Associated with Unique Identifiers in Python ubk046 1 2,011 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