我有很多 netcdf 文件,如何使用 xarray 将所有文件上传到一个 python 笔记本中?
I have a lot of netcdf files, how would I use xarray to upload all the files into one python notebook?
我有 1 个包含 12 个 netcdf 文件的文件夹。我如何使用 xarray 将所有 netcdf 文件组合成一个数据数组?
文件夹代表2015年,12个netcdf文件代表2015年每个月的数据。
我想也许我可以尝试通过 运行 一个 for 循环来操作字符串并更改字符串中文件的每个编号,因为我的文件是按以下方式组织的(2015 年):
EN.4.2.1.f.analysis.g10.201501.nc
EN.4.2.1.f.analysis.g10.201502.nc
EN.4.2.1.f.analysis.g10.201503.nc
....
我正在加载一个 netcdf 文件,例如:
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import netCDF4 as s
import glob
import xarray as xr
from datetime import datetime
inpath='../../Data/EN.4.2.1_analyses/EN.4.2.1.analyses.g10.2015/'
# just loading in the first month (january) of 2015
en4_2015 = xr.open_dataset(inpath+'EN.4.2.1.f.analysis.g10.201501.nc')
我如何将所有 netcdf 文件组合成一个数组而不是 12 个不同的 xarray?
我试过了:
import glob
import xarray as xr
from datetime import datetime
# List all matching files
files = glob.glob(inpath+'*.nc')
# Create list for
individual_files = []
# Loop through each file in the list
for i in files:
# Load a single dataset
timestep_ds = xr.open_dataset(i)
# Create a new variable called 'time' from the `time_coverage_start` field, and
# convert the string to a datetime object so xarray knows it is time data
timestep_ds['time'] = datetime.strptime(timestep_ds.time_coverage_start,
"%Y-%m-%dT%H:%M:%S.%fZ")
# Add the dataset to the list
individual_files.append(timestep_ds)
# Combine individual datasets into a single xarray along the 'time' dimension
modis_ds = xr.concat(individual_files, dim='time')
print(modis_ds)
AttributeError Traceback (most recent call last)
<ipython-input-36-34685efc1691> in <module>
10 # Create a new variable called 'time' from the `time_coverage_start` field, and
11 # convert the string to a datetime object so xarray knows it is time data
---> 12 timestep_ds['time'] = datetime.strptime(timestep_ds.time_coverage_start,
13 "%Y-%m-%dT%H:%M:%S.%fZ")
14
~/miniconda3/envs/py3_std_maps/lib/python3.8/site-packages/xarray/core/common.py in __getattr__(self, name)
226 with suppress(KeyError):
227 return source[name]
--> 228 raise AttributeError(
229 "{!r} object has no attribute {!r}".format(type(self).__name__, name)
230 )
AttributeError: 'Dataset' object has no attribute 'time_coverage_start'
轻松修复!使用:
ds = xr.open_mfdataset(file_path_folder...time??/*nc')
哪里的时间不是说 2015 年,而是 20?获取 21 世纪的所有文件,19 世纪也一样??获取 20 世纪等的所有文件
我有 1 个包含 12 个 netcdf 文件的文件夹。我如何使用 xarray 将所有 netcdf 文件组合成一个数据数组?
文件夹代表2015年,12个netcdf文件代表2015年每个月的数据。
我想也许我可以尝试通过 运行 一个 for 循环来操作字符串并更改字符串中文件的每个编号,因为我的文件是按以下方式组织的(2015 年):
EN.4.2.1.f.analysis.g10.201501.nc
EN.4.2.1.f.analysis.g10.201502.nc
EN.4.2.1.f.analysis.g10.201503.nc
....
我正在加载一个 netcdf 文件,例如:
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import netCDF4 as s
import glob
import xarray as xr
from datetime import datetime
inpath='../../Data/EN.4.2.1_analyses/EN.4.2.1.analyses.g10.2015/'
# just loading in the first month (january) of 2015
en4_2015 = xr.open_dataset(inpath+'EN.4.2.1.f.analysis.g10.201501.nc')
我如何将所有 netcdf 文件组合成一个数组而不是 12 个不同的 xarray?
我试过了:
import glob
import xarray as xr
from datetime import datetime
# List all matching files
files = glob.glob(inpath+'*.nc')
# Create list for
individual_files = []
# Loop through each file in the list
for i in files:
# Load a single dataset
timestep_ds = xr.open_dataset(i)
# Create a new variable called 'time' from the `time_coverage_start` field, and
# convert the string to a datetime object so xarray knows it is time data
timestep_ds['time'] = datetime.strptime(timestep_ds.time_coverage_start,
"%Y-%m-%dT%H:%M:%S.%fZ")
# Add the dataset to the list
individual_files.append(timestep_ds)
# Combine individual datasets into a single xarray along the 'time' dimension
modis_ds = xr.concat(individual_files, dim='time')
print(modis_ds)
AttributeError Traceback (most recent call last)
<ipython-input-36-34685efc1691> in <module>
10 # Create a new variable called 'time' from the `time_coverage_start` field, and
11 # convert the string to a datetime object so xarray knows it is time data
---> 12 timestep_ds['time'] = datetime.strptime(timestep_ds.time_coverage_start,
13 "%Y-%m-%dT%H:%M:%S.%fZ")
14
~/miniconda3/envs/py3_std_maps/lib/python3.8/site-packages/xarray/core/common.py in __getattr__(self, name)
226 with suppress(KeyError):
227 return source[name]
--> 228 raise AttributeError(
229 "{!r} object has no attribute {!r}".format(type(self).__name__, name)
230 )
AttributeError: 'Dataset' object has no attribute 'time_coverage_start'
轻松修复!使用:
ds = xr.open_mfdataset(file_path_folder...time??/*nc')
哪里的时间不是说 2015 年,而是 20?获取 21 世纪的所有文件,19 世纪也一样??获取 20 世纪等的所有文件