2

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,...)]
1
  • np.sum() adds every value in an iterable together and returns it.Like @rahlf23 suggested, np.stack() joins arrays along a new axis, in this case instead of reading "horizontally" along the temp/city axis np.stack() will read along the temp/month axis and can then be summed up to get your desired outcome Commented Mar 11, 2020 at 19:23

1 Answer 1

1

In this case, you want to stack your arrays using np.stack() prior to using np.sum() with axis=0:

total = np.sum(np.stack([cities[i]['1990s'] for i in cities.keys()]), axis=0)

Yields:

[31.1915283 32.8910524 38.649475  42.521118  48.141296 ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.