\name{GenSeries}
\alias{GenSeries}
\title{Generates An AR1 Time Series}
\description{This functions generates AR1 processes containing n data, with alpha as autocorrelation at lag 1, and mean and standard deviation provided by the mean and std arguments.}
\usage{
GenSeries(n, alpha, mean, std)
}
\arguments{
  \item{n}{Length of the timeseries to be generated.}
  \item{alpha}{Autocorrelation at lag 1.}
  \item{mean}{Mean of the data.}
  \item{std}{Standard deviation of the data.}
}
\value{AR1 timeseries}
\examples{
series <- GenSeries(1000, 0.35, 2, 1)
plot(series, type = 'l')
}
\author{
History:\cr
0.1  -  2012-04  (L. Auger, \email{ludovic.auger@meteo.fr})  -  Original code\cr
1.0  -  2012-04  (N. Manubens, \email{nicolau.manubens at ic3.cat})  -  Formatting to CRAN
}
\keyword{datagen}
# This function interpolates data with CDO.
#  - Providing a data array (of up to 4 dimension), and vectors or 
#    arrays of longitudes and latitudes. An array of data and new
#    vectors/arrays of longitudes and latitudes are returned.

metadata is preserved (still missing for lons and lats)
grid can point to a nc file
avoid writes will minimize number of writes but will need permutations
avoid writes false will do more file writes but won't do permutations
irregular grids supported
regions are extended to whole world

CDORemap <- function(data_array = NULL, lons = NULL, lats = NULL, 
                      grid, method, avoid_writes = TRUE, 
                      write_dir = tempdir()) {  #, mask = NULL) {

source('~/s2dverification/R/.Load.R')

lon <- seq(0, 360 - 360/50/2, length.out = 50)
lat <- seq(-90, 90, length.out = 25)
tas2 <- CDORemap(NULL, lon, lat, 't170grid', 'bil', TRUE)

tas <- array(1:50, dim = c(25, 50))
names(dim(tas)) <- c('lat', 'lon')
lon <- seq(0, 360 - 360/50/2, length.out = 50)
lat <- seq(-90, 90, length.out = 25)
tas2 <- CDORemap(tas, lon, lat, 't170grid', 'bil', TRUE)

tas <- array(1:50, dim = c(25, 50))
names(dim(tas)) <- c('lat', 'lon')
lon <- seq(0, 360 - 360/50/2, length.out = 50)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
lat <- seq(-90, 90, length.out = 25)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
metadata <- list(tas = list(dim = list(lat = list(len = 25,
                                                  vals = lat),
                                       lon = list(len = 50,
                                                  vals = lon)
                                      )))
attr(tas, 'variables') <- metadata
tas2 <- CDORemap(tas, lon, lat, 't170grid', 'bil', TRUE)

num_lats <- 25
num_lons <- 50
tas <- array(1:(10*num_lats*10*num_lons*10), dim = c(10, num_lats, 10, num_lons, 10))
names(dim(tas)) <- c('a', 'lat', 'b', 'lon', 'c')
lon <- seq(0, 360 - 360/num_lons/2, length.out = num_lons)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
lat <- seq(-90, 90, length.out = num_lats)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
metadata <- list(tas = list(dim = list(a = list(),
                                       lat = list(len = num_lats,
                                                  vals = lat),
                                       b = list(),
                                       lon = list(len = num_lons,
                                                  vals = lon),
                                       c = list()
                                      )))
attr(tas, 'variables') <- metadata
tas2 <- CDORemap(tas, lon, lat, 't17grid', 'bil', TRUE)
tas2 <- CDORemap(tas, lon, lat, 't17grid', 'bil', FALSE)

num_lats <- 25
num_lons <- 50
tas <- array(1:(10*num_lats*10*num_lons*10), dim = c(10, num_lats, 10, num_lons))
names(dim(tas)) <- c('a', 'lat', 'b', 'lon')
lon <- seq(0, 360 - 360/num_lons/2, length.out = num_lons)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
lat <- seq(-90, 90, length.out = num_lats)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
metadata <- list(tas = list(dim = list(a = list(),
                                       lat = list(len = num_lats,
                                                  vals = lat),
                                       b = list(),
                                       lon = list(len = num_lons,
                                                  vals = lon)
                                      )))
attr(tas, 'variables') <- metadata
tas2 <- CDORemap(tas, lon, lat, 't17grid', 'bil', TRUE)
tas2 <- CDORemap(tas, lon, lat, 't17grid', 'bil', FALSE)

num_lats <- 25
num_lons <- 50
tas <- array(1:(1*num_lats*num_lons), dim = c(num_lats, num_lons))
names(dim(tas)) <- c('y', 'x')
lon <- array(seq(0, 360 - 360/num_lons/2, length.out = num_lons), dim = c(num_lons, num_lats))
metadata <- list(lon = list(units = 'degrees_east'))
names(dim(lon)) <- c('x', 'y')
attr(lon, 'variables') <- metadata
lat <- t(array(seq(-90, 90, length.out = num_lats), dim = c(num_lats, num_lons)))
metadata <- list(lat = list(units = 'degrees_north'))
names(dim(lat)) <- c('x', 'y')
attr(lat, 'variables') <- metadata
tas2 <- CDORemap(tas, lon, lat, 'r100x50', 'bil')

num_lats <- 25
num_lons <- 50
tas <- array(1:(10*num_lats*10*num_lons*10), dim = c(10, num_lats, 10, num_lons))
names(dim(tas)) <- c('a', 'j', 'b', 'i')
lon <- array(seq(0, 360 - 360/num_lons/2, length.out = num_lons), dim = c(num_lons, num_lats))
metadata <- list(lon = list(units = 'degrees_east'))
names(dim(lon)) <- c('i', 'j')
attr(lon, 'variables') <- metadata
lat <- t(array(seq(-90, 90, length.out = num_lats), dim = c(num_lats, num_lons)))
metadata <- list(lat = list(units = 'degrees_north'))
names(dim(lat)) <- c('i', 'j')
attr(lat, 'variables') <- metadata
tas2 <- CDORemap(tas, lon, lat, 't17grid', 'bil')

num_lats <- 25
num_lons <- 50
tas <- array(1:(10*num_lats*10*num_lons), dim = c(10, num_lats, 10, num_lons))
names(dim(tas)) <- c('a', 'j', 'b', 'i')
lon <- array(seq(0, 360 - 360/num_lons/2, length.out = num_lons), dim = c(num_lons, num_lats))
names(dim(lon)) <- c('i', 'j')
lat <- t(array(seq(-90, 90, length.out = num_lats), dim = c(num_lats, num_lons)))
names(dim(lat)) <- c('i', 'j')
tas2 <- CDORemap(tas, lon, lat, 't17grid', 'bil')
tas2 <- CDORemap(tas, lon, lat, 't17grid', 'bil', FALSE)
# Possible to specify another file as grid reference
#tas2 <- CDORemap(tas, lon, lat, 'ICA_IP.nc', 'bil')

tas <- array(1:50, dim = c(25, 50))
names(dim(tas)) <- c('lat', 'lon')
lon <- seq(20, 160 - 360/50/2, length.out = 50)
lat <- seq(-40, 20, length.out = 25)
tas2 <- CDORemap(tas, lon, lat, 't170grid', 'bil', TRUE)
