将NETCDF文件变成平均值的数据框

发布于 2025-01-17 20:24:14 字数 1860 浏览 2 评论 0原文

我对NetCDF并不熟悉,但这是我的文件的设置方式。

这是一个在经度(-15至-10)和纬度(10至6.5)上的网格,其中包含常规的小网格盒,大小为0.25度x 0.25度。网格存在一个小时,然后下一个小时是一个相同的网格,并在该小时内具有数据,因此在24小时内将有24个相同的LAT/长网格。

我有多年的数据。我要做的是为每个0.25x0.25框找到每年的总平均值,并将该值添加到数据框架中:

”

这就是我到目前为止尝试的:

precip_full1 = xr.open_dataset('era_yr1979.nc')
precip_full2 = xr.open_dataset('era_yr1980.nc')
precip_full3 = xr.open_dataset('era_yr1981.nc')

precip_full = xr.concat([precip_full1,precip_full2,precip_full3],dim='time')



output = []

for x in np.arange(6.5,10,0.25):
    for y in np.arange(-15,-10,0.25):
        precip = precip_full.where((precip_full.latitude==int(x))&(precip_full.longitude==int(y)),drop=True)
        roll = precip.rolling(time=1,center=False).sum()
    

        annual = roll.groupby('time.year').max()

        tab = annual.to_dataframe().rename(columns={'tp':1})

    # Keep track of the output by appending it to the output list
        output.append(tab)
    

output = pd.concat(output,1)

print(output)

mean = output.mean()


data_mean = pd.DataFrame(mean, columns=['mean'])

这是我得到的输出:

        mean     x      y
1        NaN  9.75 -10.25
1        NaN  9.75 -10.25
1        NaN  9.75 -10.25
1        NaN  9.75 -10.25
1        NaN  9.75 -10.25
..       ...   ...    ...
1   0.015662  9.75 -10.25
1   0.015662  9.75 -10.25
1   0.013323  9.75 -10.25
1   0.013323  9.75 -10.25
1   0.013323  9.75 -10.25

[280 rows x 3 columns]

以前有人遇到过NETCDF的这种问题,并且知道为什么我会得到NAN,还是我写了错误的东西?

I'm not very familiar with NetCDF, but this is how my file is set up.

It's a grid over a longitude (-15 to -10) and latitude (10 to 6.5), containing regular small grid boxes which are 0.25 degrees x 0.25 degrees in size. The grid exists for a single hour, then the next hour is an identical grid with the data for that hour, so in 24 hours there will be 24 identical lat/long grids.

enter image description here

I have numerous years of data. What I'm trying to do is find the overall mean value of every year for each 0.25x0.25 box, and add that value to a data frame:

enter image description here

Here's what I've tried so far:

precip_full1 = xr.open_dataset('era_yr1979.nc')
precip_full2 = xr.open_dataset('era_yr1980.nc')
precip_full3 = xr.open_dataset('era_yr1981.nc')

precip_full = xr.concat([precip_full1,precip_full2,precip_full3],dim='time')



output = []

for x in np.arange(6.5,10,0.25):
    for y in np.arange(-15,-10,0.25):
        precip = precip_full.where((precip_full.latitude==int(x))&(precip_full.longitude==int(y)),drop=True)
        roll = precip.rolling(time=1,center=False).sum()
    

        annual = roll.groupby('time.year').max()

        tab = annual.to_dataframe().rename(columns={'tp':1})

    # Keep track of the output by appending it to the output list
        output.append(tab)
    

output = pd.concat(output,1)

print(output)

mean = output.mean()


data_mean = pd.DataFrame(mean, columns=['mean'])

Here's the output I get:

        mean     x      y
1        NaN  9.75 -10.25
1        NaN  9.75 -10.25
1        NaN  9.75 -10.25
1        NaN  9.75 -10.25
1        NaN  9.75 -10.25
..       ...   ...    ...
1   0.015662  9.75 -10.25
1   0.015662  9.75 -10.25
1   0.013323  9.75 -10.25
1   0.013323  9.75 -10.25
1   0.013323  9.75 -10.25

[280 rows x 3 columns]

Has anyone come across this sort of problem with NetCDF before and know why I'm getting NaN, or am I writing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末的微笑 2025-01-24 20:24:14

我不太确定您的最终目标是什么,但是假设您的数据具有相同的网格,您可以通过每年的平均值来计算pandas.dataframe

df = xr.open_mfdataset('path/*era_yr*', concat_dim='time')
mean_df = df.groupby('time.year').mean('time').to_dataframe()

I'm not quite sure what your end goal is but assuming your data has the same grids, you can calculate for annual mean per gid point by and convert that into a pandas.DataFrame:

df = xr.open_mfdataset('path/*era_yr*', concat_dim='time')
mean_df = df.groupby('time.year').mean('time').to_dataframe()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文