-1

Is there a way to average values in a NetCDF file over a given list of lat/lon coordinates using nco before using R to convert it into a Raster Brick? This list of lat/lon coordinates represents a non-rectangular region.

Right now, I have an .nc file with monthly temperature data for a single year for the entire ocean. I have a list of lat/lon values that describe my study region. I would like to extract a single average value for temperature for each month across all coordinates in my lat/lon list. Thus far, I've been trying to perform this operation after the .nc file has been converted to a Raster Brick in R, but it requires too much memory to perform the operation.

Therefore, I'm wondering if there is a way I can do this in bash using nco instead.

Thanks!

zoek
  • 141
  • 1
  • 7
  • You say that, you *have been been trying to perform this operation after the .nc file has been converted to a Raster Brick in R, but it requires too much memory to perform the operation.* That suggest that you are doing something wrong in R. Can you show some code? – Robert Hijmans Aug 27 '19 at 20:02

2 Answers2

2

I'm not sure I understand the intent of your question. Either of these commands should give a single temperature for the whole year for each point:

ncwa -a time in.nc out.nc

or

ncra in.nc out.nc

To extract the specific points on the list from the global ocean (and store them in a single file without using _FillValues for the other points) is more problematic, and would be most concise if you used ncremap to first convert your global ocean data from a (presumably) rectangular latitude-longitude format to an unstructured (i.e., 1D) array, then used ncks -X to extract the list of points into a single compact file. The User Guide gives more info on each step.

Charlie Zender
  • 5,929
  • 14
  • 19
0

If the lat lon points are a rectangle region (i.e. lon1-lon2 and lat1-lat2), then you can extract them and then take the spatial average in this way with CDO:

cdo fldmean -sellonlatbox,lon1,lon2,lat1,lat2 in.nc out.nc 

If the region in question is not a simple "box", then you will need to extract the nearest grid point into a file and then use ensemble averaging:

for lonlat in $lonlatlist ; do
   lable=`echo $lonlat | sed  's/\//_/g' # replace / in output name
   cdo remapbil,$lonlat in.nc point_${lonlat}.nc
done 
cdo ensmean point_*.nc out.nc 

where lonlat list is a list of array indexes in strings "lon=X/lat=Y" (or you could use an alternative imitation 2D array in bash (How to declare 2D array in bash)

This method uses bilinear interpolation to get the value for each given lat-lon (and they need to be regularly spaced to get a sensible answer, otherwise you are weighting the average more to one region where your points are more closely space)

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86