ex1_create.R 2.86 KB
Newer Older
#*******************************************************************************
# Title: Example script to create 's2dv_cube' objects
# Author: Eva Rifà Rovira 
#*******************************************************************************
# This example shows how to create an 's2dv_cube' object. 
# There are two ways of creating an 's2dv_cube' object. The first way is 
# to use the function s2dv_cube(): create an 's2dv_cube' from scratch with any 
# data. In the second example we see the other method with the function 
# as.s2dv_cube(). This function is to create an 's2dv_cube' from a 
# 'startR_array' or 'load' object.
#-------------------------------------------------------------------------------
library(CSTools)

################################################################################
#-----------------------------------------------------
# Example 1: Function s2dv_cube() from defined data
#-----------------------------------------------------
# (1.1) Minimal use case, with s2dv_cube function
# We define the array with named dimensions:
dat <- array(1:100, dim = c(time = 10, lat = 4, lon = 10))
# We define the coordinates as a list of vectors:
coords <- list(time = 1:10, lat = 43:40, lon = 0:9)
# The metadata:
metadata <- list(tas = list(level = '2m'),
                 lon = list(cdo_grid_name = 'r360x181'),
                 lat = list(cdo_grid_name = 'r360x181'))
# The creation of Dates array. First the initial date:
ini_date <- as.POSIXct('2010-01-01', format = '%Y-%m-%d')
# The sequence of dates
dates <- seq(ini_date, by = 'days', length.out = 10)
# We define the dates dimensions
dim(dates) <- c(time = 10)
dat_cube <- s2dv_cube(data = dat, coords = coords,
                      varName = 'tas', metadata = metadata,
                      Dates = dates,
                      when = "2019-10-23 19:15:29 CET", 
                      source_files = c("/path/to/file1.nc", "/path/to/file2.nc"),
                      Datasets = 'test_dataset')  
# We print the result:
# > dat_cube
# 's2dv_cube'
# Data          [ 1, 2, 3, 4, 5, 6, 7, 8 ... ] 
# Dimensions    ( time = 10, lat = 4, lon = 10 ) 
# Coordinates  
#  * time : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 
#  * lat : 43, 42, 41, 40 
#  * lon : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 
# Attributes   
#    Dates  : 2010-01-01 2010-01-02 2010-01-03 2010-01-04 2010-01-05 ... 
#    varName  : tas 
#    metadata :  
#       tas 
#         other : level 
#       lon 
#         other : cdo_grid_name 
#       lat 
#         other : cdo_grid_name 
#    Datasets  : test_dataset 
#    when  : 2019-10-23 19:15:29 CET 
#    source_files  : /path/to/file1.nc ... 

#-----------------------------------------------------
# Example 2: Function as.s2dv_cube() from 'startR_array'
#-----------------------------------------------------


################################################################################