R 使用wux进行集合气候数据

R 使用wux进行集合气候数据,r,csv,raster,netcdf,R,Csv,Raster,Netcdf,我对R很陌生,我正在处理新英格兰的气候数据。我目前正试图使用WUX软件包来寻找所有29个气候模型中每年平均、最低和最高温度的集合平均值。例如,最后,我想要一个模型平均值的光栅堆栈,每年一个堆栈。最终目标是获得一个显示可变性的图表。我曾尝试在线阅读WUX.pdf,但因为我是如此的新,因为它是如此的全面概述,我觉得我迷路了。我需要帮助开发一个简单的框架来运行模型。到目前为止,我的剧本大致概括了我认为我需要的东西。如果我错了,请纠正我,但我想我希望使用'models2wux'函数。请记住,我的脚本在这

我对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 
#此脚本将:
#计算每年各模块的总体平均值、最小值和最大值。
#例如,脚本将找到所有模块的平均温度
#1980年。它在所有年份都将如此。
#每个场景将有一个单独的集合平均值、最大值和最小值
图书馆(光栅)
图书馆(拉斯特维斯)
图书馆(utils)
图书馆(wux)
图书馆(格子)
#wux信息
#https://cran.r-project.org/web/packages/wux/wux.pdf
路径