合并年nc数据并统计生长季数据
主要针对多个年lai的nc数据进行数据的整体合并和多年生长季统计。为后续数据处理做铺垫。
数据格式
数据格式如图,nc文件内有time坐标,是日数据组成。
最终可得到两个文件,一个是lai生长季的多年均值,一个是lai生长季的每年值。
代码
def Search_File(dirname,suffix):
'''
This function can search all files with the specified suffix in this dir.
:param dirname: string, the path need to be searched
:param suffix: string, the specified suffix need to be seached
:return:
filter_list: list, the path list need to be searched.
'''
filter = [suffix] # 设置过滤后的文件类型 当然可以设置多个类型
filter_list = []
for maindir, subdir, file_name_list in os.walk(dirname):
#print(maindir) #当前主目录
for filename in file_name_list:
apath = os.path.join(maindir, filename)#合并成一个完整路径
portion = os.path.splitext(apath)
ext = portion[1] # 获取文件后缀 [0]获取的是除了文件名以外的内容
if ext in filter:
newname = portion[0] + suffix
filter_list.append((newname,portion[0].split("\\")[-1]))
# print(filter_list)
return filter_list
def lai_statistics(combine_path):
second_path_list = Search_File(combine_path, ".nc")
combine_data = [xr.open_dataset(path) for path,name in second_path_list]
full_time_data = xr.combine_nested(combine_data,concat_dim="time")
month = full_time_data.resample(time='M').mean()
# 时间序列用来拿月份
creat_time = pd.DataFrame({'time': pd.to_datetime(month["time"].values)})
creat_time["month"] = pd.DatetimeIndex(creat_time['time']).month
select = creat_time[(creat_time["month"] >= 3) & (creat_time["month"] < 10)]["time"].to_numpy()
every_year_lai = month.sel(time=select).resample(time='Y').mean()
#多年均值先做每月的多年均值,然后提取生长季月份,最后再做均值
mutil_year_avg = month.groupby("time.month").mean().sel(month=[3, 4, 5, 6, 7, 8, 9]).mean(dim='month')
every_year_lai.to_netcdf(r"F:\small_basin\result\every_year_lai.nc")
mutil_year_avg.to_netcdf(r"F:\small_basin\result\mutil_year_lai_avg.nc")
data_combine_and_statistics(r"F:\small_basin\lai_stand")
需要帮忙处理数据的可以加我,jia5678912,wx