Newer
Older
#*******************************************************************************
Eva Rifà
committed
# Title: Example script to create 's2dv_cube' objects
# Author: Eva Rifà Rovira
Eva Rifà
committed
# Date: 16/01/2024
#*******************************************************************************
Eva Rifà
committed
# 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.
#-------------------------------------------------------------------------------
Eva Rifà
committed
# Needed packages
library(CSTools)
################################################################################
#-----------------------------------------------------
Eva Rifà
committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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'