mon_mean_areaavg <- function(var, lat_var, latmin, latmax, lonmin, lonmax) {

#read in daily mean data, calculate monthly mean
#read in lat and lon range, calculate area mean
#'var' is the variable read from netCDF
var_sub <- var[lonmin:lonmax, latmin:latmax, ]
lat_sub <- lat_var[latmin:latmax]
var_lon_mean <- apply(var_sub, c(2,3), mean)  #average along longitude

cos_lat <- abs(cos(lat_sub))/sum(abs(cos(lat_sub)))
weight <- matrix(rep(cos_lat,each=dim(var)[3]),ncol=dim(var)[3], byrow=TRUE) #create same matrix size as var

areamean <- weighted.mean(var_lon_mean, weight)
monmean <- mean(areamean)
#monmean <- apply(as.matrix(var_onept), 3, mean)

#Outputs
return(monmean)

}

