#'LargeLocalScale
#' 
#'@author Carmen Alvarez-Castro, \email{carmen.alvarez-castro@cmcc.it}
#'
#'@description Select the large and the local scale and interpolate into a smaller-grid-size using cdo and daily data 
#'from a model projection, seasonal model or reanalysis 
#'
#'@param var1            variable 1 name (i.e. SLP)
#'       var2            variable 2 name (i.e. PRATE)
#'       directory       the directory folder of your data
#'       filein.large    name of your imput file at large scale (i.e. SLP.nc)
#'       filein.local    name of your imput file at local scale (i.e. PRATE.nc)
#'       filesmallgrid   file with small gridsize to be used as a reference grid for the interpolation
#'       large.lonlat    large scale longitude and latitude as lon1,lon2,lat1,lat2
#'       local.lonlat    local scale longitude and latitude as lon1,lon2,lat1,lat2
#'
#'@import cdo
#'
#'@return netcdf for the large scale and  netcdf for the local scale
#'        in case you have selected interp=TRUE, netcdf interpolated for the large and local scales
#'        a list with the output path and names
#'      
#'        
#' #'@example 1 selection of large and local scales with interpolation (for predictors, models to be interpolated into a finner grid)
#' largeScale="-80,50,22,70 "
#' #localScale="6,20,35,49 " #lon1,lon2,lat1,lat2 #italy
#' localScale="-11,5,34,46 " #lon1,lon2,lat1,lat2 #spain
#' varname1="slp"
#' varname2="prate"
#' directory="~/Downloads/"
#' filein1="slp_CMCC_1990-2018"
#' filein2="prate_CMCC_1990-2018"
#' filesmallgrid="prate_ERA5_1990-2018.nc"
#' #by default interpolation=TRUE
#' Scales=LargeLocalScale(largeScale,localScale,varname1,varname2,filein1,filein2,directory,interp=TRUE)

#' #'@example 2 just to do an interpolation without selection of regions
#' remapnn(filein1,varname1,filesmallgrid,directory)

#' #'@example 3 selection of large and local scales without interpolation (for predictands grid, ERA5, EOBS,etc..)
#' largeScale="-80,50,22,70 "
#' #localScale="6,20,35,49 " #lon1,lon2,lat1,lat2 #italy
#' localScale="-11,5,34,46 " #lon1,lon2,lat1,lat2 #spain
#' varname1="slp"
#' varname2="prate"
#' directory="~/Downloads/"
#' filein1="slp_ERA5_1990-2018"
#' filein2="prate_ERA5_1990-2018"
#' #by default interpolation=TRUE, here must be changed
#' Scales=LargeLocalScale(largeScale,localScale,varname1,varname2,filein1,filein2,directory,interp=FALSE)

# interpolate into a finnest grid using cdo
remapnn<-function(filein.large,filein.local,filesmallgrid,directory){ 
  fileout.large=paste(filein.large,"Interp",sep="")
  fileout.local=paste(filein.local,"Interp",sep="")
  system(paste("cd ",directory,"; cdo griddes ",filesmallgrid," > smallgrid.txt",sep=("")))
  system(paste("cd ",directory,"; cdo remapnn,smallgrid.txt ",filein.large,".nc ",fileout.large,".nc",sep=("")))
  system(paste("cd ",directory,"; cdo remapnn,smallgrid.txt ",filein.local,".nc ",fileout.local,".nc",sep=("")))
 return(list(paste(directory,fileout.large,sep=""),paste(directory,fileout.local,sep="")))
}
# selecting region in cdo to create an netcdf
LargeLocalScale<-function(large.lonlat,local.lonlat,var1,var2,filein.large,filein.local,directory,interp=TRUE){
    Namefileout.large=paste(var1,"Large",sep="")  
    Namefileout.local=paste(var2,"Local",sep="")
    system(paste("cd ",directory,"; cdo sellonlatbox,",large.lonlat,filein.large,".nc ",Namefileout.large,".nc",sep=("")))
    system(paste("cd ",directory,"; cdo sellonlatbox,",local.lonlat,filein.local,".nc ",Namefileout.local,".nc",sep=("")))
    if(interp==TRUE){
  # interpolate to the nearest neighbor using cdo
  x=remapnn(filein.large=Namefileout.large,filein.local=Namefileout.local,filesmallgrid,directory)
  system(paste("cd ",directory,"; cdo sellonlatbox,",local.lonlat,x[[2]],".nc ",x[[2]],"2.nc",sep=("")))
  return(list(paste(directory,Namefileout.large,"Interp.nc",sep=""),paste(directory,Namefileout.local,"Interp2.nc",sep="")))
  }
    if(interp==FALSE){
    return(list(paste(directory,Namefileout.large,".nc",sep=""),paste(directory,Namefileout.local,".nc",sep="")))
    }
}  

