繁体   English   中英

如何使用从 xarray 到 select 日期范围的 isel 方法?

[英]How can I use the isel method from xarray to select a range of dates?

我正在根据一系列日期对 NASA DAYMET 数据进行子集化 - 具体来说,我正在尝试自动对价值 20 年的每日气候数据进行子集化,但我只对每年的夏季月份感兴趣。 我已经设法让我的代码部分工作,但每年更新仍然有问题。 即如果指定四年数据,则同一年的每日数据价值为 output 四个不同的时间,仅更新了 plot 标题中的年份。 结果图在标题中更新了年份,但数据本身仅来自一年。

该循环当前从夏季(六月至八月)每天输出,但 output 数据未正确更新年份。 我一直在使用 xarray 的 isel function,因为它有一个附加组件,可以增加处理栅格数据的能力。

我倾向于认为问题在于第二个嵌套循环中的这一行:
prcp_1day_export = prcp_clip.isel(time=spec_day)

代码的最终结果应该是一个 matplotlib plot 和一个 .tif 文件,其中包含已剪辑到我研究区域的原始数据。

for spec_year in range(2000, 2003):
        print("year = " + str(spec_year))
        start_date = dt.datetime(spec_year, 6, 1) # specify your own start date
        end_date = dt.datetime(spec_year, 7, 1)  # specify your end start date
        beg_day = (start_date - dt.datetime(spec_year, 1, 1)).days
        end_day = beg_day + 92 
        print(start_date)
        print(end_date)
        print(beg_day)
        print(end_day)
        
        for spec_day in range(beg_day, end_day):
            # rename and export
#             print(str(spec_day)) #spec_day will be the value to use to specify what time equals a particular day
            # The line below is the likely result of the inaccurate specification of year when automating the subset.
            prcp_1day_export = prcp_clip.isel(time=spec_day)
            print(str(prcp_1day_export))
            prcp_1day_export.rio.to_raster("prcp_1day_export" + "_" + str(spec_day) + "_" + str(spec_year) + ".tif")
            fig, ax = plt.subplots(figsize = (20,10))
            prcp_1day_export.plot(ax=ax, robust=True, cbar_kwargs={'label': 'mm/day'})
            ga_shape_lcc.plot(ax = ax, color = 'none', edgecolor = 'red')
            fig.savefig("prcp_1day_export" + "_" + str(spec_day) + "_" + str(spec_year) + ".png")
            

print("Finished")

任何关于如何解决这个问题的想法都非常感谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM