I have tried to figure this one out but so far without much success. I need to calculate the average of the pixel values within a distance from a certain coordinate. To be able to script this I need to be able to give a coordinate and a radius. Does anyone have any ideas?
Do you just need this for a list of coordinates or do you want a lowpass filtered image? Different solutions I think. If I had to do this in GMT I would
- Convert my image to red, green, blue grids via grdmix
- Filter each component grid with a boxcar filter of desired diameter
- Either convert the filtered grids back to an image via grdmix or sample the three grids at your desired coordinates.
There may be simpler non-GMT solutions depending on the initial question.
I need to do this for a single coordinate and a given radius. The image(es) has just one single band and I need to return the average value for the pixels within the radius for further processing. I can sample one single pixel with grdtrack but instead of a single pixel I need to either get the values for all pixels within the given radius or the average of the pixels within the radius.
I assume we are talking very local calculations here (not average over a 4000 km circle). Maybe this scheme would work:
- Given your lon,lat, radius, use grdcut -Slon/lat/radius+n to extract a grid which only has values inside the circle and NaN outside.
- Use grdinfo -L2 on that grid to report the mean value
May not be fast but may not matter to you.
Yes, the calculations are local 0.5 to 1 km or so. I tried grdcut but it is unusably slow. I have ~100 grd files I need to sample in a specific location but grdcut is taking 10+ minutes per file when using -Slon/lat/radius+n
Probably you can then do a rectangular crude grdcut first to only get a tiny rectangular grid that is ensured to contain the circle, then run the slow part on that small grid. E.g., compute your w/e/s/n for the rectangular subset using Lon and lat +/- radius (in degrees if geographic files) plus 10% padding to be sure.