0

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?

jw99
  • 41
  • 6
  • Could you also print the metadata? You might also be interested to use `xarray.open_mfdataset` and simply concatenate across time by adding `concat_dim='time'` – Dani56 Apr 01 '22 at 11:53

1 Answers1

1

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()
Dani56
  • 372
  • 1
  • 2
  • 7