Katie
Katie

Reputation: 65

How to sum together the outputs of a loop in python

I am trying to calculate the average salinity of the North Sea. The code below shows the calculation of the average salinity for each grid cell (96 of them). I then need to add these together and divide by 96, but this is where I am running into issues.

Below is the code and the loop output.

import xarray as xa

# =============================================================================
#  Step 1: Get data
# =============================================================================
    
data_obs = xa.open_dataset("sal_obs_NS_all_new.nc",decode_times = False)

data_extract = data_obs[["salt"]]

# =============================================================================
# Get the longitude data as -180 to 180, rather than 0 to 360
# =============================================================================

long1 = data_extract.lon.values
long1[long1>180]-=360
data_extract["lon"] = long1
data_sorted = data_extract.sortby("lon")

# =============================================================================
# Extract salt data for the North Sea only, and time average
# =============================================================================

sal = data_sorted.salt

salt = sal.sel(lon=slice(-1.8,1.8),
               lat=slice(54.75,60.25))
salt = salt.mean("time")

# =============================================================================
# Calculate the average salinity across the depth for each grid cell
# =============================================================================

arr_id = 0

# Loop latitude
for lat_id in range(12):
    # Loop longitude
    for lon_id in range(8):
        # For each grid cell, reset the depth_sum and depth_count
        depth_sum = 0
        depth_count = 0
        # For each layer of this grid cell
        for layer_id in range(13):
            # Try and access the depth values, if the script fails because the value does not exist
            # then simply ignore the error. If the value does exist, add it to the current depth_sum
            # and increment the depth_count.
            try:
                val = salt[layer_id][lat_id][lon_id].values
                # Make sure we only add valid values
                if val > 0:
                    depth_sum += val
                    depth_count += 1
            except:
                # Ignoring any access errors to depth values
                pass
       
        # May encounter a divide by zero error, so catch and set the depth_avg to the depth_sum.
        try:
            depth_avg = depth_sum / depth_count
        except:
            depth_avg = depth_sum

        print(f'depth_sum({arr_id}):{depth_avg}')
        arr_id += 1 
            

depth_sum(0):0

depth_sum(1):34.83010196685791

depth_sum(2):34.82737890879313 ...

...depth_sum(93):35.23942011052912

depth_sum(94):35.22928237915039

depth_sum(95):35.23519724065607

# =============================================================================
# Total average salinity for the North Sea
# =============================================================================

NS_total = sum(map(float(f'depth_sum({arr_id}):{depth_avg}')))

This gives the error ValueError: could not convert string to float: 'depth_sum(96):35.23519724065607'

Any help greatly appreciated.

Upvotes: 0

Views: 159

Answers (1)

CutePoison
CutePoison

Reputation: 5355

Your last line NS_total = sum(map(float(f'depth_sum({arr_id}):{depth_avg}'))) seems to be the issue.

The string you are generating cannot be converted; f'depth_sum({arr_id}):{depth_avg}' is a string like "depth_sum(95):35.123" which (clearly) cannot be converted to one number using float.

EDIT:

If understand it correctly you can do it as following:

.
.
.
DEPTH_AVG = 0 #Place holder for the total depth_avg
# Loop latitude
for lat_id in range(12):
    # Loop longitude
    for lon_id in range(8):
        # For each grid cell, reset the depth_sum and depth_count
        depth_sum = 0
        depth_count = 0
        # For each layer of this grid cell
        for layer_id in range(13):
            # Try and access the depth values, if the script fails because the value does not exist
            # then simply ignore the error. If the value does exist, add it to the current depth_sum
            # and increment the depth_count.
            try:
                val = salt[layer_id][lat_id][lon_id].values
                # Make sure we only add valid values
                if val > 0:
                    depth_sum += val
                    depth_count += 1
            except:
                # Ignoring any access errors to depth values
                pass
       
        # May encounter a divide by zero error, so catch and set the depth_avg to the depth_sum.
        try:
            depth_avg = depth_sum / depth_count
            DEPTH_AVG += depth_avg #Add the current depth_avg to the sum of all previous depth_avg's
        except:
            depth_avg = depth_sum

        print(f'depth_sum({arr_id}):{depth_avg}')
        arr_id += 1 
            
print(f"Total depth_avgs: {DEPTH_AVG/96}")

Upvotes: 1

Related Questions