Seyed Omid Nabavi
Seyed Omid Nabavi

Reputation: 497

how to spatialy aggregate netcdf fields in python, CDO, or NCO?

I want to regrid population data from 0.05 degrees to 0.1 degrees. Because it is population, I should aggregate (sum) population values for resampling data to a coarser resolution. Although I thought that there going to be a simple answer to this question, I did find any yet. I think my question does not need sample data, but you may find the population data from Gridded Population of the World (GPW), v4.

Thanks for your help.

Upvotes: 1

Views: 974

Answers (3)

ClimateUnboxed
ClimateUnboxed

Reputation: 8107

You could also duplicate Charlie's conversion approach using cdo, but because your gridbox sizes (before/after) are an integral multiple of each other (i.e. you want to aggregate over a 2x2 pixel square), then it is simpler to directly use the cdo function gridboxsum

 cdo gridboxsum,2,2 in.nc out.nc

Upvotes: 2

Charlie Zender
Charlie Zender

Reputation: 6352

With NCO you would first convert the population data to an intensive field (population per unit area), then "regrid", then, if you want, convert back to an extensive variable (population), e.g.,

ncap2 -s 'pop_per_area=pop/area' in.nc pop_area.nc
ncremap -G latlon=1800,3600 -g dest_grid.nc
ncremap -d dest_grid.nc pop_area.nc out.nc
ncap2 -s 'pop=pop_per_area*area' out.nc pop.nc

All commands documented here. Good luck!

Upvotes: 2

Jeremy
Jeremy

Reputation: 876

What about using xarray packages to read the netcdf files, convert the ND xarray data arrays to pandas dataframes. Then use pandas to achieve what you need. Then you can convert pandas dataframes back to xarray data arrays and save out as netCDF files. Everything should be straightforward using xarray. My only concern is that xarray may not work for some netCDF files, which have sub data groups. In those cases, you may need other Python packages to read the netcdf files, like netCDF.

Or there is a Python regridding tool called xESMF, but I have not tried it yet.

Upvotes: 0

Related Questions