Sum of corresponding values from different arrays of the same size with Python

I’m rather new to Python so it’s quite possible that my question has already been asked on the net but when I find things that seem relevant, I don’t always know how to use them in my code (especially if it’s a function definition), so I apologise if there’s any redundancy.

I work with daily temperature data from the Copernicus website (https://marine.copernicus.eu/).

As the netCDF files are too large if I want the data for every day of every month for several years, what I’m trying to do is access the data without downloading it so that I can work with it.

The data is in the form of an array for 1 day of a month of a year.
I want to sum the values of all the arrays for each day of a month in a year.

To make things clearer, here’s an example:

Simplified arrays :

array1([1,4,3,9]
       [7,5,2,3])

array2([3,8,6,1]
       [6,4,7,2])

#... etc until day 28,29,30 or 31

The result I want :

array1 + array 2 => ([1+3,4+8,3+6,9+1]
                     [7+6,5+4,2+7,3+2])


array1 + array 2 => ([4,12,9,10]
                     [13,9,9,5])

I first tried to do it without loop with the data for 2 days and it works.

My code :

import os
import xarray as xr
import numpy as np
import netCDF4 as nc
import copernicusmarine

# Access the data
DS = copernicusmarine.open_dataset(dataset_id="cmems_mod_glo_phy_my_0.083deg_P1D-m")

# Get only thetao (temperature) variable for 1 day
subset = DS[['thetao']].sel(time = slice("2014-01-01", "2014-01-01"))

# Obtain only data of a certain depth
target_depth = 0         #surface
subset_T = subset.thetao.isel(depth=target_depth)

# To view my data in array
thetao_depth0 = subset_T.data
thetao_depth0

# Same thing for next day of the same month and year
subset2 = DS[['thetao']].sel(time = slice("2014-01-02", "2014-01-02"))

subset_T2 = subset2.thetao.isel(depth=target_depth)

thetao_depth0_2 = subset_T2.data
thetao_depth0_2

# The sum of my arrays
days_sum = thetao_depth0 + thetao_depth0_2
days_sum

My thetao_depth0 arrays look like this :

For 01/01/2014 :

array([[[        nan,         nan,         nan, ...,         nan,
                 nan,         nan],
        [        nan,         nan,         nan, ...,         nan,
                 nan,         nan],
        [        nan,         nan,         nan, ...,         nan,
                 nan,         nan],
        ...,
        [-1.70870081, -1.70870081, -1.70870081, ..., -1.70870081,
         -1.70870081, -1.70870081],
        [-1.71016569, -1.71016569, -1.71016569, ..., -1.71016569,
         -1.71016569, -1.71016569],
        [        nan,         nan,         nan, ...,         nan,
                 nan,         nan]]])

For 02/01/2014 :

array([[[        nan,         nan,         nan, ...,         nan,
                 nan,         nan],
        [        nan,         nan,         nan, ...,         nan,
                 nan,         nan],
        [        nan,         nan,         nan, ...,         nan,
                 nan,         nan],
        ...,
        [-1.70870081, -1.70870081, -1.70870081, ..., -1.70870081,
         -1.70870081, -1.70870081],
        [-1.71016569, -1.71016569, -1.71016569, ..., -1.71016569,
         -1.71016569, -1.71016569],
        [        nan,         nan,         nan, ...,         nan,
                 nan,         nan]]])

And I get days_sum :

array([[[        nan,         nan,         nan, ...,         nan,
                 nan,         nan],
        [        nan,         nan,         nan, ...,         nan,
                 nan,         nan],
        [        nan,         nan,         nan, ...,         nan,
                 nan,         nan],
        ...,
        [-3.41740161, -3.41740161, -3.41740161, ..., -3.41740161,
         -3.41740161, -3.41740161],
        [-3.42033139, -3.42033139, -3.42033139, ..., -3.42033139,
         -3.42033139, -3.42033139],
        [        nan,         nan,         nan, ...,         nan,
                 nan,         nan]]])

Now here’s where it gets complicated.
I’d like to create a loop that does the same thing with all the arrays for every day of a month in a year (from 01/01/2014 to 31/01/2014 for example).

So far I’ve done this :

day = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']
month = ['01']
year = ['2014']

DS = copernicusmarine.open_dataset(dataset_id="cmems_mod_glo_phy_my_0.083deg_P1D-m")

for y in year:
    for m in month:
        for d in day:
            start_date="%s"%y+"-%s"%m+"-%s"%d
            end_date=start_date
            subset_thetao = DS[['thetao']].sel(time = slice(start_date, end_date))
            target_depth = 0 
            subset_depth = subset_thetao.thetao.isel(depth=target_depth)
            thetao_depth0 = subset_depth.data

But I’m having trouble adding up the arrays for each round of the loop.

I first tried things with np.sum but either it’s not made for what I want to do, or I’m doing it wrong, especially when it comes to storing the array with the sum in a variable.
I’ve added empty_array = np.array([]) before my for loop but I don’t know what to do next in the loop.

This is the first time I’ve handled arrays with python, so maybe I’m doing it wrong.

In the end, what I’d like to do is average the values of my different arrays over a month.

A simplified example with 3 days of a month :

array1([1,4,3,9]
       [7,5,2,3])

array2([3,8,6,1]
       [6,4,7,2])

array3([3,2,6,1]
       [1,4,5,2])

To get :

array([(1+3+3)/3,(4+8+2)/3,...etc]
       [...etc])

array([2.3,4.6,5,3.6]
       [4.6,4.3,4.6,2.3])

2

I’ve added empty_array = np.array([]) before my for loop but I don’t know what to do next in the loop.

Almost right. You need to instantiate an array of the same shape as the arrays you will sum. With Numpy, you can only sum arrays of the same shape. You can inspect shape using arr.shape.

The array should initially be filled with zeros. This way, you end up with the sum after adding all your other arrays.

import numpy as np

# Create a zero-filled array with the same shape as the arrays you need to sum
sum_arr = np.zeros_like(thetao_depth0)

# alternative: sum_arr = np.zeros(thetao_depth0.shape)

for d in day:
    # ...
    # get the array subset_depth.data for the current day
    # ...

    # add the data of this day to the sum
    sum_arr += subset_depth.data

Your snippet

for y in year:
    for m in month:
        for d in day:
            start_date="%s"%y+"-%s"%m+"-%s"%d
            end_date=start_date
            subset_thetao = DS[['thetao']].sel(time = slice(start_date, end_date))
            target_depth = 0 
            subset_depth = subset_thetao.thetao.isel(depth=target_depth)
            thetao_depth0 = subset_depth.data

does not retain thetao_depth0 for subsequent days. Change the last line in the loop to

   thetao_depth0 = thetao_depth0 + subset_depth.data

However, you’ll need to initialize thetao_depth0 to a zero-filled array of the same shape as subset_depth.data. From the snippets you’ve posted it looks like a multi-dimensional array, so you’ll have to be careful with that.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật