May-22-2018, 08:15 PM (This post was last modified: May-22-2018, 08:16 PM by Lightning1800. Edited 3 times in total.)

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?

Many thanks, and I would GREATLY appreciate any feedback!

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!