1

I am very new to R, and I am working with New England climate data. I am currently attempting to use the package WUX to find ensemble averages for each year of the mean, minimum, and maximum temperatures across all 29 climate models. In the end for example, I want to have a raster stack of model averages, one stack for each year. The ultimate goal is to obtain a graph that shows variability. I have attempted to read through the WUX.pdf online, but because I am so new, and because it is such a general overview I feel I am getting lost. I need help developing a simple framework to run the model. My script so far has a general outline of what I think I need. Correct me if I am wrong, but I think I want to be using the 'models2wux' function. Bare in mind that my script is a bit messy at this point.

#This script will:
#Calculate ensemble mean, min, and max across modules within each year.
#For example, the script will find the average temp across all modules 
#for the year 1980. It will do the same for all years. 
#There will be a separate ensemble mean, max, and min for each scenario

library(raster)
library(rasterVis)
library(utils)
library(wux)
library(lattice)
#wux information
#https://cran.r-project.org/web/packages/wux/wux.pdf

path <- "/net/nfs/merrimack/raid/Northeast_US_Downscaling_cmip5/"
vars <- c("tasmin", "tasmax") #, "pr")
mods <- c("ACCESS1-0", "ACCESS1-3", "bcc-csm1-1", "bcc-csm1-1-m")
    #"CanESM2", "CCSM4", "CESM1-BGC", "CESM1-CAM5", "CMCC-CM",
    #"CMCC-CMS", "CNRM-CM5", "CSIRO-Mk3-6-0", "FGOALS-g2", "GFDL-CM3", 
    #"GFDL-ESM2G", "GFDL-ESM2M", "HadGEM2-AO", "HadGEM2-CC", "HadGEM2-ES",
    #"inmcm4", "IPSL-CM5A-LR", "IPSL-CM5A-MR", "MIROC5", "MIROC-ESM-CHEM", 
    #"MIROC-ESM", "MPI-ESM-LR", "MPI-ESM-MR", "MRI-CGCM3", "NorESM1-M")
scns <- c("rcp45", "rcp85") #, "historical")

#A character vector containing the names of the models to be processed
climate.models <- c(mods) 

#ncdf file for important cities we want to look at (with lat/lon)
cities.path <-
"/net/home/cv/marina/Summer2017_Projects/Lat_Lon/NE_Important_Cities.csv"
necity.vars <- c("City", "State", "Population", 
             "Latitude", "Longitude", "Elevation(meters")

# package = wux -- models2wux
#models2wux(userinput, modelinput)
#modelinput information
#Start 4 Loops to envelope netcdf data
for (iv in 1:2){
  for (im in 1:4){
    for (is in 1:2){
      for(i in 2006:2099){
      modelinput <- paste(path, vars[iv], "_day_", mods[im], "_", scns[is], "_r1i1p1_", i, "0101-", i, "1231.16th.nc", sep="")
  print(modelinput)
      }   # end of year loop
    }   # end of scenario loop
  }   # end of model loop
}  # end of variable loop  

# this line will print the full file name
print(full)
#more modelinput information necessary? List of models

# package = wux -- models2wux
#userinput information
parameter.names <- c("tasmin", "tasmax")
reference.period <- "2006-2099"
scenario.period <- "2006-2099"

temporal.aggregation <- #maybe don't need this

subregions <- # will identify key areas we want to look at (important cities?)
  #uses projection file
  #These both read the .csv file (first uses 'utils', second uses 'wux')
  #1
  cities.read <- read.delim(cities.path, header = T, sep = ",")
  #2
  read.table <- read.wux.table(cities.path)
  cities.read <- subset(cities.read, subreg = "City", sep = ",")
  # To read only "Cities", "Latitude", and "Longitude"
  cities.points <- subset(cities.read, select = c(1, 4, 5))
  cities.points <- as.data.frame(cities.points)
  colnames(cities.points)<- c("City", "Latitude", "Longitude" )

  #Set plot coordinates for .csv graph 
  coordinates(cities.points) <- ~ Longitude + Latitude
  proj4string(cities.points) <- c("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
  subregions <- proj4string(cities.points)

  area.fraction <- # reread pdf on this (p.24)
  #Do we want area.fraction = T or F ? (FALSE is default behavior)

spatial.weighting <- FALSE 
#spatial.weighting = TRUE enables cosine weighting of latitudes, whereas 
omitting or setting
#FALSE results in unweighted arithmetic areal mean (default). This option is 
valid only for data on a regular grid.

na.rm =  FALSE #keeps NA values

#plot.subregions refer to pdf pg. 25

#save.as.data saves data to specific file


    # 1. use the brick function to read the full netCDF file.
    # note: the varname argument is not necessary, but if a file has multiple varables, brick will read the first one by default.
    air_t <- brick(full, varname = vars[iv])

    # 2. use the calc function to get average, min, max for each year over the entire set of models
    annualmod_ave_t <- calc(air_t, fun = mean, na.rm = T)
    annualmod_max_t <- calc(air_t, fun = max, na.rm = T)
    annualmod_min_t <- calc(air_t, fun = min, na.rm = T)

    if(i == 2006){
      annual_ave_stack = annualmod_ave_t
    }else if{
      annual_ave_stack <- stack(annual_ave_stack, annualmod_max_t)) 
    }else{
      annual_ave_stack <- stack(annual_ave_stack, annualmod_min_t)
     } # end of if/else 
M. Bow
  • 41
  • 5

0 Answers0