对 netCDF 文件的变量中的每个值执行计算

Performing a calculation on every value in a variable of a netCDF file

我目前正在处理大约 40 个 NetCDF 文件。我一直在尝试对每个文件的特定变量中的每个值执行计算,但是当我去绘制新的计算变量时,none 个值已经改变。我用 xarray 打开了文件,然后选择了变量。我如何获得新的计算值。我也尝试了多个 return 语句。这是我目前所拥有的:

def calculation_1(文件):

    # Open Global attributes
    with xr.open_dataset(file) as s5p_variables:
        print(colored('Global attributes of product:\n', 'blue'), s5p_variables)

    raw_no2 = s5p_variables['tropospheric_NO2_column_number_density'][0]
    for x in range(raw_no2.shape[0]):
        for y in range(raw_no2.shape[1]):
            y_intercept = (2*(raw_no2[x][y]))/100  
            slope = -y_intercept/100  
            raw_no2[x][y] = slope*10 + y_intercept

请帮忙

很难从您的代码片段中判断出您做错了什么。您可以阅读 how to create a minimal, reproducible example here。我试图通过下面的示例重现您的问题,该示例演示了值应该更改。您还可以避免遍历每个值,因为 xarray 可以 broadcast 您对每个 grid-point 的计算,这比使用 for 循环要快得多。

data = xr.DataArray(np.random.randn(2, 3), dims=("x", "y"), coords={"x": [10, 20]})
>>data
<xarray.DataArray (x: 2, y: 3)>
array([[ 0.27623202, -1.08740069, -0.67368971],
      [ 0.11364841, -1.47842655,  0.52498767]])
Coordinates:
* x        (x) int64 10 20
Dimensions without coordinates: y

y_intercept = (2*(raw_no2[x][y]))/100  
slope = -y_intercept/100  
raw_no2[x][y] = slope*10 + y_intercept
y_intercept = 2*data/100
slope = - y_intercept/100
data= slope*10 + y_intercept

>>data
<xarray.DataArray (x: 2y: 3)>
array([[-0.01121879,  0.02355238, -0.01290332],
       [ 0.01947954,  0.00112604, -0.02490705]])
Coordinates:
* x      (x) int64    10 20