NetCDF:使用 MSWEP 再分析数据集计算沿所有坐标的年降水量总和

问题描述 投票:0回答:1

我正在使用通过合并每月 nc 文件创建的 nc 文件。 实际文件('precip_combined.nc')包含 1979-2020 年的全球月降水量数据(大小:3.79 GB):

<xarray.Dataset>
Dimensions:        (lon: 3600, lat: 1800, time: 504)
Coordinates:
  * lon            (lon) float32 -179.9 -179.8 -179.8 ... 179.8 179.9 179.9
  * lat            (lat) float32 89.95 89.85 89.75 ... -89.75 -89.85 -89.95
  * time           (time) datetime64[ns] 1979-01-01 1979-02-01 ... 2020-12-01
Data variables:
    precipitation  (time, lat, lon) float32 ...
Attributes:
    history:          Created on 2022-10-09 13:23
    input_data_hash:  1ff7f07166074240030172bae541afd3ecbc27ef941e50a65b04a26...type here

目标是:

  1. 根据水文(“水”)年计算每个给定坐标(纬度、经度)的年降水量,水文年通常定义为从n 年 10 月到 n+1 年 9 月
  2. 将结果存储在 nc 文件中,并使用每年和每个坐标的新变量 precipitation_sums

我使用了这个代码:

import xarray as xr

# Open the netCDF file
data = xr.open_dataset('.../precip_combined.nc')

# Define the start and end months of the hydrological year
start_month = 10
end_month = 9

# Initialize an empty list to store the sums of precipitation
precipitation_sums = []

# Loop through each year from 1979 to 2020
for year in range(1979, 2021):
    # Define the start and end dates of the hydrological year
    start_date = f'{year}-{start_month:02d}-01'
    end_date = f'{year+1}-{end_month:02d}-30'
    
    # Select the data for the hydrological year
    hydro_year_data = data.sel(time=slice(start_date, end_date))
    
    # Calculate the sum of precipitation for each lon and lat coordinate
    precipitation_sum = hydro_year_data['precipitation'].sum(dim=['lon', 'lat'])
    
    # Append the sum to the list
    precipitation_sums.append(precipitation_sum)

# Convert the list of sums to a new xarray dataset
precipitation_sums_dataset = xr.concat(precipitation_sums, dim='time')

# Add the precipitation sums dataset to the original dataset
data['precipitation_sums'] = precipitation_sums_dataset

# Save the updated dataset as a new netCDF file
data.to_netcdf('.../precip_annual_sums.nc')

并得到具有以下形状的 nc 文件作为输出:

<xarray.Dataset>
Dimensions:             (lon: 3600, lat: 1800, time: 504)
Coordinates:
  * lon                 (lon) float32 -179.9 -179.8 -179.8 ... 179.8 179.9 179.9
  * lat                 (lat) float32 89.95 89.85 89.75 ... -89.75 -89.85 -89.95
  * time                (time) datetime64[ns] 1979-01-01 ... 2020-12-01
Data variables:
    precipitation       (time, lat, lon) float32 ...
    precipitation_sums  (time) float32 ...
Attributes:
    history:          Created on 2022-10-09 13:23
    input_data_hash:  1ff7f07166074240030172bae541afd3ecbc27ef941e50a65b04a26...type here

如您所见,数据变量 precipitation_sums 不包含任何坐标。 例如,如果我读取创建的 nc 文件并提取伦敦的数据,则不会得到任何输出。

请注意,我需要全局数据集,而不是此处询问的特定位置https://stackoverflow.com/questions/65072907/calculate-monthly-average-and-annual-sum-of-precipitation-from-netcdf-与-cdo

我将不胜感激任何帮助。 GitHub 等上没有提供易于理解的示例,可能这个任务对于专业人士来说太简单了。

python netcdf cdo-climate weatherdata
1个回答
0
投票

当您用

cdo
标记问题时,我认为
cdo
答案对您来说也是可以接受的,在这种情况下,您可以将时间索引向后移动 9 个月,所以 10 月现在是 1 月,全年总和,然后移动返回,以便时间戳正确:

cdo shifttime,9months -yearsum -shifttime,-9months in.nc out.nc

注:

  1. 您可以使用系统命令或 cdo 包从 python 中调用它
  2. 您还可以使用选项
    --timestat_date
    与选项
    last
    first
    来选择定义时间戳的位置(默认为中部,因此数据为四月)
© www.soinside.com 2019 - 2024. All rights reserved.