使用wux进行集合气候数据

时间:2017-06-23 14:50:00

标签: r csv raster netcdf

我是R的新手,我正在研究新英格兰气候数据。我目前正在尝试使用WUX包来查找所有29种气候模型中每年平均,最低和最高温度的总体平均值。最后,例如,我希望有一个模型平均值的栅格堆栈,每年一个堆栈。最终目标是获得显示可变性的图表。我试图通过WUX.pdf在线阅读,但因为我是如此新,并且因为它是如此笼统的概述我觉得我迷路了。我需要帮助开发一个简单的框架来运行模型。到目前为止,我的剧本总结了我认为我需要的内容。如果我错了,请纠正我,但我想我想使用' models2wux'功能。请记住,我的脚本在这一点上有点乱。

#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 

0 个答案:

没有答案