合并年nc数据并统计生长季数据

合并年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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值