Changes between Version 4 and Version 5 of udg/ecoms/RPackage/biascorrection


Ignore:
Timestamp:
Mar 30, 2015 9:24:36 AM (7 years ago)
Author:
gutierjm
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • udg/ecoms/RPackage/biascorrection

    v4 v5  
    88The `ecomsUDG.Raccess` package relies on the `rJava` package as an interface to the powerful capabilities of the [http://www.unidata.ucar.edu/downloads/netcdf/netcdf-java-4/index.jsp Unidata's netCDF Java library].
    99
     10
     11
    1012{{{#!comment
    1113  * [wiki:./Functions Defined Functions]
     
    1517||= R code =||= Output =||
    1618{{{#!td
     19  {{{#!text/R
     20# Packages and loging
     21library(downscaleR)
     22library(ecomsUDG.Raccess)
     23library(ncdf4) # Only needed to write NetCDF
     24loginECOMS_UDG(username = "username", password = "password")
     25  }}}
    1726   {{{#!text/R
     27# Loading data (two members)
    1828obs <- loadECOMS(dataset = "WFDEI",
    19                   var = "tasmin",
    20                   season = c(12,1,2))
     29                  var = "tas",
     30                  season = c(12,1,2,3,4,5),
     31                  lonLim = c(-15,35)
     32                  latLim = c(32, 75)
     33                  years =- c(2001:2010))
    2134prd <- loadECOMS(dataset = "System4_seasonal_15",
    22                   var = "tasmin",
    23                   season = c(12,1,2),
     35                  var = "tas",
     36                  season = c(12,1,2,3,4,5),
    2437                  members = 1:2,
    25                   leadMonth = 1)
     38                  leadMonth = 1,
     39                  lonLim = c(-15,35)
     40                  latLim = c(32, 75)
     41                  years = c(2001:2010))
     42prd <- interpGridData(prd, new.grid = getGrid(obs),
     43                  method = "nearest");
    2644  }}}
    2745  {{{#!text/R
    28 obsr <- interpGridData(gridData = obs,
    29                   new.grid = getGrid(prd),
    30                   method = "bilinear")
    31 bias <- getBias(obsr,prd)
    32 plotMeanField(bias, multi.member = TRUE)
     46# Bias correction and plotting
     47prd.bc <- biasCorrection(obs, prd, prd,
     48                  method = "qqmap",
     49                  multi.member=TRUE,
     50                  window = 30)
     51plotMeanField(obs)
     52plotMeanField(prd, multi.member = FALSE)
     53plotMeanField(prd.bc, multi.member = FALSE)
     54  }}}
     55 {{{#!text/R
     56# Exporting to netcdf4
     57fileName <- "tas_qqmap_System4_WFDEI_2001_2010.nc4"
     58grid2NetCDF(prd.bc, NetCDFOutFile = fileName,
     59                  missval = 1e20,
     60                  prec = "float")
    3361  }}}
    3462}}}
     
    3765}}}
    3866
     67# Creating a nc-file with the bias-corrected data:
     68# The function grid2NetCDF builds a nc4-file using a downscaleR object, in this case the surface pressure bias corrected with the qq-map method. The resulting NetCDF is available in the link:
     69# http://www.meteo.unican.es/work/datasets/psAdjust_System4_1_2_12_6_qqmap_WFDEI_2001_2010.nc4
    3970
    4071
     
    5081# Seasonal forecast parameters:
    5182dataset <- "System4_seasonal_15"
    52 season <- c(12,1:6)
    53 leadMonth <- 0
     83season <- c(12,1:5)
     84leadMonth <- 1
    5485members <- 1:2
    5586lonLim <- c(-15,35)
     
    6899It is important to note that some variables are derived from others (e.g. wss is estimated from the two wind components). For this reason, in order to reduce the memory requirements of the loading process, we have defined an "advisable order", which is used in this example. It is also advisable to remove non-necesary variables.
    69100
     101qqmap (with wet/dry frequency adjustment for precipitation)
     102
    70103{{{
    71104#!text/R
    72105# Advisable Order: taking into account the nature (raw/derived) of the target variables we suggest the following order.
    73106variables <- c("ps","wss","huss","tas","tasmax","tasmin","tp","rsds","rlds")
    74 # Surface air pressure
    75 obs <- loadECOMS(dataset = "WFDEI", var = "ps", lonLim = lonLim, latLim = latLim, season = season, years = years)
    76 ps <- loadECOMS(dataset = dataset, var = "ps", members = members, lonLim = lonLim, latLim = latLim, season = season, years = years, leadMonth = leadMonth, time = time)
    77 
    78 # Interpolating: System4 -> WFDEI grid
    79 ps <- interpGridData(ps, new.grid = getGrid(obs), method = interpolationMethod);
    80 
    81 # Bias Correction
    82 ps <- biasCorrection(obs, ps, ps, method = method, multi.member = multi.member, window = window)
    83 
    84 # Creating a nc-file with the bias-corrected data:
    85 # The function grid2NetCDF builds a nc4-file using a downscaleR object, in this case the surface pressure bias corrected with the qq-map method. The resulting NetCDF is available in the link:
    86 # http://www.meteo.unican.es/work/datasets/psAdjust_System4_1_2_12_6_qqmap_WFDEI_2001_2010.nc4
    87 
    88 grid2NetCDF(ps, NetCDFOutFile = "psAdjust_System4_1_2_12_6_qqmap_WFDEI_2001_2010.nc4", missval = 1e20, prec = "float", compression=4, shuffle = TRUE)
    89 
    90 # Cleaning the workspace:
    91 obs <- NULL
    92 
    93 # Wind speed (at 10m)
    94 obs <- loadECOMS(dataset = "WFDEI", var = "wss", lonLim = lonLim, latLim = latLim, season = season, years = years)
    95 wss <- loadECOMS(dataset = dataset, var = "wss", members = members, lonLim = lonLim, latLim = latLim, season = season, years = years, leadMonth = leadMonth, time = time)
    96 
    97 # Interpolating: System4 -> WFDEI grid
    98 wss <- interpGridData(wss, new.grid = getGrid(obs), method = interpolationMethod);
    99 
    100 # Bias Correction
    101 wss <- biasCorrection(obs, wss, wss, method = method, multi.member = multi.member, window = window)
    102 
    103 # Cleaning the workspace:
    104 obs <- NULL
    105 
    106 # Surface (2m) specific humidity
    107 obs <- loadECOMS(dataset = "WFDEI", var = "huss", lonLim = lonLim, latLim = latLim, season = season, years = years)
    108 huss <- loadECOMS(dataset = dataset, var = "huss", members = members, lonLim = lonLim, latLim = latLim, season = season, years = years, leadMonth = leadMonth, time = time)
    109 
    110 # Interpolating: System4 -> WFDEI grid
    111 huss <- interpGridData(huss, new.grid = getGrid(obs), method = interpolationMethod);
    112 
    113 # Bias Correction
    114 huss <- biasCorrection(obs, huss, huss, method = method, multi.member = multi.member, window = window)
    115 
    116 # Cleaning the workspace:
    117 obs <- NULL
    118 
    119 # Near-Surface air temperature
    120 obs <- loadECOMS(dataset = "WFDEI", var = "tas", lonLim = lonLim, latLim = latLim, season = season, years = years)
    121 tas <- loadECOMS(dataset = dataset, var = "tas", members = members, lonLim = lonLim, latLim = latLim, season = season, years = years, leadMonth = leadMonth, time = time)
    122 
    123 # Interpolating: System4 -> WFDEI grid
    124 tas <- interpGridData(tas, new.grid = getGrid(obs), method = interpolationMethod);
    125 
    126 # Bias Correction
    127 tas <- biasCorrection(obs, tas, tas, method = method, multi.member = multi.member, window = window)
    128 
    129 # Cleaning the workspace:
    130 obs <- NULL
    131 
    132 # Daily Maximum Near-Surface Air Temperature
    133 obs <- loadECOMS(dataset = "WFDEI", var = "tasmax", lonLim = lonLim, latLim = latLim, season = season, years = years)
    134 tasmax <- loadECOMS(dataset = dataset, var = "tasmax", members = members, lonLim = lonLim, latLim = latLim, season = season, years = years, leadMonth = leadMonth, time = time)
    135 
    136 # Interpolating: System4 -> WFDEI grid
    137 tasmax <- interpGridData(tasmax, new.grid = getGrid(obs), method = interpolationMethod);
    138 
    139 # Bias Correction
    140 tasmax <- biasCorrection(obs, tasmax, tasmax, method = method, multi.member = multi.member, window = window)
    141 
    142 # Cleaning the workspace:
    143 obs <- NULL
    144 
    145 # Daily Minimum Near-Surface Air Temperature
    146 obs <- loadECOMS(dataset = "WFDEI", var = "tasmin", lonLim = lonLim, latLim = latLim, season = season, years = years)
    147 tasmin <- loadECOMS(dataset = dataset, var = "tasmin", members = members, lonLim = lonLim, latLim = latLim, season = season, years = years, leadMonth = leadMonth, time = time)
    148 
    149 # Interpolating: System4 -> WFDEI grid
    150 tasmin <- interpGridData(tasmin, new.grid = getGrid(obs), method = interpolationMethod);
    151 
    152 # Bias Correction
    153 tasmin <- biasCorrection(obs, tasmin, tasmin, method = method, multi.member = multi.member, window = window)
    154 
    155 # Cleaning the workspace:
    156 obs <- NULL
    157 
    158 # Total precipitation amount
    159 obs <- loadECOMS(dataset = "WFDEI", var = "pr", lonLim = lonLim, latLim = latLim, season = season, years = years)
    160 pr <- loadECOMS(dataset = dataset, var = "pr", members = members, lonLim = lonLim, latLim = latLim, season = season, years = years, leadMonth = leadMonth, time = time)
    161 
    162 # Interpolating: System4 -> WFDEI grid
    163 pr <- interpGridData(pr, new.grid = getGrid(obs), method = interpolationMethod);
    164 
    165 # Bias Correction
    166 pr <- biasCorrection(obs, pr, pr, method = method, multi.member = multi.member, window = window, pr.threshold = pr.threshold)
    167 
    168 # Cleaning the workspace:
    169 obs <- NULL
    170 
    171 # Surface Downwelling Shortwave Radiation
    172 obs <- loadECOMS(dataset = "WFDEI", var = "rsds", lonLim = lonLim, latLim = latLim, season = season, years = years)
    173 rsds <- loadECOMS(dataset = dataset, var = "rsds", members = members, lonLim = lonLim, latLim = latLim, season = season, years = years, leadMonth = leadMonth, time = time)
    174 
    175 # Interpolating: System4 -> WFDEI grid
    176 rsds <- interpGridData(rsds, new.grid = getGrid(obs), method = interpolationMethod);
    177 
    178 # Bias Correction
    179 rsds <- biasCorrection(obs, rsds, rsds, method = method, multi.member = multi.member, window = window)
    180 
    181 # Cleaning the workspace:
    182 obs <- NULL
    183 
    184 # Net Longwave Surface Radiation
    185 obs <- loadECOMS(dataset = "WFDEI", var = "rlds", lonLim = lonLim, latLim = latLim, season = season, years = years)
    186 rlds <- loadECOMS(dataset = dataset, var = "rlds", members = members, lonLim = lonLim, latLim = latLim, season = season, years = years, leadMonth = leadMonth, time = time)
    187 
    188 # Interpolating: System4 -> WFDEI grid
    189 rlds <- interpGridData(rlds, new.grid = getGrid(obs), method = interpolationMethod);
    190 
    191 # Bias Correction
    192 rlds <- biasCorrection(obs, rlds, rlds, method = method, multi.member = multi.member, window = window)
    193 
    194 # Cleaning the workspace:
    195 obs <- NULL
     107for (v in 1:length(variables)){
     108   obs <- NULL
     109   prd <- NULL
     110   obs <- loadECOMS(dataset = "WFDEI", var = variables[v],
     111        lonLim = lonLim, latLim = latLim, season = season, years = years[1])
     112   
     113   prd <- loadECOMS(dataset = dataset, var = variables[v], time = time, members = members,
     114        lonLim = lonLim, latLim = latLim, season = season, years = years, leadMonth = leadMonth)
     115   
     116   prd <- interpGridData(prd, new.grid = getGrid(obs), method = interpolationMethod);
     117   
     118   if (any(grepl(obs$Variable$varName,c("tp")))){
     119        prd <- biasCorrection(obs, prd, prd, pr.threshold = pr.threshold,
     120             method = method, multi.member = multi.member, window = window)
     121   }else{
     122        prd <- biasCorrection(obs, prd, prd,
     123             method = method, multi.member = multi.member, window = window)
     124   }
     125   # Exporting to netcdf4
     126   fileName <- paste(var[v],"System4_WFDEI.nc4",sep = "_")
     127   grid2NetCDF(prd.bc, NetCDFOutFile = fileName,
     128                  missval = 1e20,
     129                  prec = "float")
     130}
    196131}}}