[R-sig-Geo] Calculating weighted spatial means globally across grid cells using NetCDF file

r@i@1290 m@iii@g oii @im@com r@i@1290 m@iii@g oii @im@com
Mon Mar 18 16:00:56 CET 2019


Hi there,
I am currently working on a project that involves climate model data stored in a NetCDF file. I am currently trying to calculate "weighted" spatial annual "global" averages for precipitation. I need to do this for each of the 95 years of global precipitation data that I have. The idea would be to somehow apply weights to each grid cell by using the cosine of its latitude (which means latitude grid cells at the equator would have a weight of 1 (i.e. the cosine of 0 degrees is 1), and the poles would have a value of 1 (as the cosine of 90 is 1)). Then, I would be in a position to calculate annual weighted averages based on averaging each grid cell. 
I have an idea how to do this conceptually, but I am not sure where to begin writing a script in R to apply the weights across all grid cells and then average these for each of the 95 years. I would greatly appreciate any help with this, or any resources that may be helpful!!!
At the very least, I have opened the .nc file and read-in the NetCDF variables, as shown below:
ncfname<-"MaxPrecCCCMACanESM2rcp45.nc"
Prec<-raster(ncfname)
print(Prec)
Model<-nc_open(ncfname)
get<-ncvar_get(Model,"onedaymax")longitude<-ncvar_get(Model, "lon")
latitude<-ncvar_get(Model, "lat")
Year<-ncvar_get(Model, "Year")

Additionally, let's say that I wanted to create a time series of these newly derived weighted averaged for a specific location or region, the following code that I previously used to show trends over the 95 years for one-day maximum precipitation works, but I would just need to change it slightly to use the annual weighted means? :
r_brick<-brick(get, xmn=min(latitude), xmx=max(latitude), ymn=min(longitude), ymx=max(longitude), crs=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0"))
r_brick<-flip(t(r_brick), direction='y')
randompointlon<-13.178
randompointlat<--59.548
Hope<-extract(r_brick, SpatialPoints(cbind(randompointlon,randompointlat)),method='simple')
df<-data.frame(year=seq(from=1, to=95, by=1), Precipitation=t(Hope))
ggplot(data=df, aes(x=Year, y=Precipitation, group=1))+geom_line()+ggtitle("One-day maximum precipitation (mm/day) trend for Barbados for CanESM2 RCP4.5")


Also, if it helps, here is what the .nc file contains:

3 variables (excluding dimension variables):
        double onedaymax[lon,lat,time]   (Contiguous storage)  
            units: mm/day
        double fivedaymax[lon,lat,time]   (Contiguous storage)  
            units: mm/day
        short Year[time]   (Contiguous storage)  

     3 dimensions:
        time  Size:95
        lat  Size:64
            units: degree North
        lon  Size:128
            units: degree East
Again, any assistance would be extremely valuable with this! I look forward to your response!

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list