Home > Mobile >  How to compute 2, 3 and 5-days accumulated rainfall from daily rainfall data in netcdf using Python
How to compute 2, 3 and 5-days accumulated rainfall from daily rainfall data in netcdf using Python

Time:08-22

I'm trying to compute 2, 3 and 5-days accumulated rainfall from grided daily rainfall data (GPCC precisely) in order to look for the largest event in space. When I applied .rolling(time=2).sum() it gives nan in all timesteps. I'm new to python because I've been using ferret long before now. Below is how I've been doing it but am sensing that am complete off the line and I don't know how to proceed or continue.

import numpy as np
import pandas as pd
import xarray as xr 
path_gpcc = 'path_to_my_file/GPCC_daily_1982-2020.nc'
# Create Xarray Dataset
gpcc = xr.open_dataset(path_gpcc)
gpcc

#Dimensions: time: 14245lon: 360lat: 180

gpcc_2d = gpcc.rolling(time=2).sum()
gpcc_5d = gpcc.rolling(time=5).sum()
gpcc_7d = gpcc.rolling(time=7).sum()

I aslo tried this approach but it's not working

lat = gpcc.lat[:]
lon = gpcc.lon[:]
gpcc_2d = []
for i in range(len(lat)):
    for j in range(len(lat)):
        gpcc_ser = pd.Series(gpcc[0,i,j].values)
        gpcc_2d  = [gpcc_ser.rolling(2).sum()]

this is the description of my imput data (global daily rainfall)

I expect the output (says 2-days accumulated rainfall amount) to be like input as described above that contain accumulated rainfall amount at every gridpoints of my data coverage. Since it's going to be accumulated daily rainfall amount for different length (i.e., 2, 3 and 5-days), I expected 'nan' prior to these first valid length

CodePudding user response:

The xarray code you are have should be giving you the correct result. Note that xarray output is right-aligned, so the first time step in gpcc_2d will have nothing but missing values.

If all time steps do have missing values, you might want to try my package nctoolkit. You could check if it works with this file as follows:

import nctoolkit as nc 
path_gpcc = 'path_to_my_file/GPCC_daily_1982-2020.nc'
# Create nctoolkit DataSet
gpcc = nc.open_data(path_gpcc)
# calculate the rolling mean
gpcc.rolling_sum(2)
#plot the result
gpcc.plot()

If neither approach works you probably want to check if the file is CF-compliant https://pumatest.nerc.ac.uk/cgi-bin/cf-checker.pl. Some times xarray or other packages will fail because netCDF files are poorly defined and do not follow expected conventions.

  • Related