I am working with a dataset that is gridded, with time, latitude and longitude as indices, and monthly temperature as the actual variable. I first load in the data and extract the variable I want.
data=xr.open_dataset('E:/Riskpulse_HD/Jon climate study/adaptor.mars.internal-1583855532.1432714-8122-5-ace27afd-90c0-4a7d-b9ca-f3d5528c5ea1.nc')
temp = data['t2m'][:,0,:,:]-273
I create arrays with the values at the specific points. There is one value for each month starting at 1990-01-01, then 1991-02-01 and so forth to present. Now I create a dictionary of several cities across the world at various lat,lon points.
Now I create the dictionary for the city and for the decades.
cities = {
"Shanghai": {"coords": [31.25, 121.5]},
"Singapore": {"coords": [1.25, 103.75]},
"Shenzhen": {"coords": [22.5, 114]},
"Ningbo-Zhoushan": {"coords": [29.75, 121.5]},
"Guangzhou": {"coords": [23.0, 113.5]}...}
decades = {
"1990s": {"start": dt.datetime(1990,1,1), "end": dt.datetime(1999,12,31)},
"2000s": {"start": dt.datetime(2000,1,1), "end": dt.datetime(2009,12,31)},
"2010s": {"start": dt.datetime(2010,1,1), "end": dt.datetime(2019,12,31)}}
Now I loop through and create new dictionary entries in the form of cities['City name']['Decade'] like so.
for city, location in cities.items():
data = temp.sel(latitude=location['coords'][0], longitude=location['coords'][1])
for decade, dates in decades.items():
cities[city][decade] = data.sel(time=(slice(dates['start'], dates['end'])))
The data looks like this for cities['Shanghai']['1990s'] and cities['Singapore']['1990s'] :
-->array([ 5.1273193, 6.3297424, 11.221802 , 14.664612 , 19.932983 ,...)]
-->array([26.064209, 26.56131 , 27.427673, 27.856506, 28.208313, 28.161072,...)]
Where the first value corresponds to the monthly average temp at Shanghai in 1990-01-01 and the second is the monthly average temp for Shanghai in 1990-02-01.
Now what I would like to do is create a simple loop which takes the 5 cities I listed and adds together the average temps for each month through each decade.
I tried something like this
for i in cities.keys():
total=np.sum(cities[i]['1990s'])
-->total array(1272.0103, dtype=float32)
But this just adds every single temperature data point together. I want the operation to add each array element, while keeping all of the elements the same according to the month in which they are added. For example, for Shanghai and Singapore, it would look like this:
array([31.191528, 32.891052, 38.649475, 42.52112 , 48.141296,...)]