Python Forum
Converting days to years in loop while computing values across grid cells - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Converting days to years in loop while computing values across grid cells (/thread-10156.html)



Converting days to years in loop while computing values across grid cells - Lightning1800 - May-15-2018

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!!!


RE: Converting days to years in loop while computing values across grid cells - ThiefOfTime - May-15-2018

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/reference/generated/numpy.ufunc.reduceat.html
i havent tested it yet, but it may work :)


RE: Converting days to years in loop while computing values across grid cells - Lightning1800 - May-15-2018

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!