Python Forum
Converting days to years in loop while computing values across grid cells
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting days to years in loop while computing values across grid cells
#1
Greetings,

Using a for loop, I recently tried to compute the maximum values across grid cells (which are composed of 64 degrees latitude and 128 degrees longitude), which are part of a 3D matrix (the variable called "Quantity" below). However, I am not sure what is the best way to do this. This matrix is composed of the elements of time, latitude, longitude. Time is given in units of days, but I would like to use units of years in the for loop. Also, how would you construct the for loop to compute all maximum values for every year for every grid cell?

To give an idea of what I mean, here is how I started my loop, but I am uncertain how to finish it to include every year (***remember that the default time units is in days***). Ultimately, I would like to compute all maximum ***annual*** values (I already imported numpy as np to use the max () function) across every latitude and longitude for every year available (140 years, which is, by default expressed as 51100 days). So, the idea is to go from year 0 to year 139, getting the loop to compute all maximum values across every grid cell.

Here is what I have:

Quantity=Q
 
        for lat in range(np.size(Quantity,axis=1)):
                for lon in range(np.size(Quantity,axis=2)):
                        Quantity[:,lat,lon] 
I think that is the right way to start, but how would you finish that loop to include time (to be expressed in years)?

Any help would be greatly appreciated!!!
Reply
#2
Do you have to use loops for this task? If not I would recommend using numpy without loops.
if you have a huge amount of data this way is much much more efficient than loops in combination with numpy.
if we can say, that your data has the dimensions: timexlatitudexlongitude then you can do following:
import numpy as np
# quantity would be given at this point and is an 3-dimensional numpy array.
new_quantity = np.add.reduceat(quantity, range(0, quantity.shape[0], 365))
here is the documentation of reduceat https://docs.scipy.org/doc/numpy/referen...uceat.html
i havent tested it yet, but it may work :)
Reply
#3
Hi ThiefOfTime,

Thank you for your response.

The loops might be necessary, since I need to compute the maximum values over a multitude of grid cells for every year for 140 years. How would you do that using a for loop, if you had to? Such that all maximum values would be computed over every grid cell. As outlined in my other thread, something like this:

Grid cell #1 for year 0: Max value derived
Grid cell #2 for year 0: Max value derived
Grid cell #3 for year 0: Max value derived
....
Grid cell #1000 for year 0: Max value derived

Then, move on to year 1....

etc....all the way to year 139?

What would be an efficient way to account for all of that in a for loop? What I have above appears to be a good starting point.

Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matplotlib scatter plot in loop with None values ivan_sc 1 2,235 Nov-04-2021, 11:25 PM
Last Post: jefsummers
  Computing the distance between each pair of points Truman 11 4,089 Jun-20-2020, 01:15 PM
Last Post: Truman
  newbie: loop, modify dataframe cells expat_th 5 3,625 Mar-03-2020, 09:05 PM
Last Post: jefsummers
  Learning python specific syntax after using other scripting languages for years Arcs 12 5,941 Dec-13-2017, 07:18 PM
Last Post: Arcs
  Error in computing FFT operation raady07 1 4,177 Jan-18-2017, 08:30 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

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