Class used for {sen2rts} time series.

s2ts(value, date, id = NA, qa, orbit, sensor, rawval, ...)

as.s2ts(x, ...)

Arguments

value

(mandatory) Vector with the values ("y") of the time series.

date

(mandatory) Vector (of the same length of value) with the dates of each value.

id

(optional) Vector (of the same length of value or of length 1, in which case the passed value is replicated for each element of value) with the feature IDs of each value. If missing, all the values are considered as extracted from the same feature.

qa

(optional) Vector (of the same length of value) with the quality assessment values (range 0-1) associated to each value. If missing, all the values are considered as equally weighted.

orbit

(optional) Vector (of the same length of valueor of length 1, in which case the passed value is replicated for each element of value) with the Sentinel-2 orbits of each value.

sensor

(optional) Vector (of the same length of value or of length 1, in which case the passed value is replicated for each element of value) with the Sentinel-2 sensors ("2A" or "2B") of each value.

rawval

(optional) Vector (of the same length of value) with the non-smoothed values (this is generally created by outputs of smooth_s2ts()).

...

Additional vectors to be passed. Each additional argument is threated as an additional element of the output list in case it is of the same length of value, otherwise it is threated as an output attribute.

x

Input element to be converted to s2ts.

Value

The output time series in s2ts format. This is a format derived from data.table, containing the mandatory columns id, date, value, and additional ones among which qa, orbit, sensor and rawval (see the arguments in this help for their meanings). The optional attribute gen_by provides information about the function which generated the object (if provided).

Details

Some specific methods for this class are defined.

  • <s2ts_obj>$value returns a data.table with the values for each date, in wide format.

  • <s2ts_obj>$date returns a vector with the dates.

  • <s2ts_obj>$id returns a vector with the unique ID values.

  • <s2ts_obj>$qa returns a data.table with the quality assessment values for each date, in wide format.

  • <s2ts_obj>$rawval returns a data.table with the rawval values for each date, in wide format.

  • <s2ts_obj>[[<idname>]] returns a subsampled s2ts containing only the values of the provided ID.

Notice that since $ and [[ methods are defined, the syntaxes <s2ts_obj>$<fieldname> and <s2ts_obj>[["<fieldname>"]] can not be used (use instead <s2ts_obj>[,<fieldname>]).

Author

Luigi Ranghetti, PhD (2020) luigi@ranghetti.info

Examples

## Create a s2ts object from raw
library(sen2r)
#> Welcome to sen2r. To use the package from a GUI, launch
#>  > sen2r()
#> Documentation: https://sen2r.ranghetti.info
sample_dt <- sen2r::s2_dop(
  timewindow = c("2020-01-01", "2020-01-31"), 
  s2_orbit = 22
)
sample_dt$ndvi <- runif(nrow(sample_dt), 0, 1)
sample_dt$qa <- sqrt(sample_dt$ndvi)

# Minimum example (including dates and values)
s2ts(value = sample_dt$ndvi, date = sample_dt$date, id = "1")
#> A s2ts time series with 6 dates and 1 IDs.
#>          Date          1  
#> 1: 2020-01-04 0.24241698  
#> 2: 2020-01-09 0.73568978  
#> 3: 2020-01-14 0.37475223  
#> 4: 2020-01-19 0.23795985  
#> 5: 2020-01-24 0.07836545  
#> 6: 2020-01-29 0.80696536  

# Example with additional information (including quality flags)
sample_ts <- s2ts(
  value = sample_dt$ndvi, 
  date = sample_dt$date, 
  id = "1",
  qa = sample_dt$qa, 
  sensor = sample_dt$mission
)
sample_ts
#> A s2ts time series with 6 dates and 1 IDs.
#>          Date Sensor          1   
#> 1: 2020-01-04     2B 0.24241698 ○ 
#> 2: 2020-01-09     2A 0.73568978 ◑ 
#> 3: 2020-01-14     2B 0.37475223 ◔ 
#> 4: 2020-01-19     2A 0.23795985 ○ 
#> 5: 2020-01-24     2B 0.07836545 ○ 
#> 6: 2020-01-29     2A 0.80696536 ◑ 
#> 
#> Quality flags:  ● [1]  ◕ [0.9,1)  ◑ [0.75,0.9)  ◔ [0.5,0.75)  ○ [0,0.5)

# Extract s2ts content
as.list(sample_dt) # convert in list (unclass)
#> $date
#> [1] "2020-01-04" "2020-01-09" "2020-01-14" "2020-01-19" "2020-01-24"
#> [6] "2020-01-29"
#> 
#> $mission
#> [1] "2B" "2A" "2B" "2A" "2B" "2A"
#> 
#> $orbit
#> [1] "022" "022" "022" "022" "022" "022"
#> 
#> $ndvi
#> [1] 0.24241698 0.73568978 0.37475223 0.23795985 0.07836545 0.80696536
#> 
#> $qa
#> [1] 0.4923586 0.8577236 0.6121701 0.4878113 0.2799383 0.8983125
#> 
sample_ts$date # extract dates
#> [1] "2020-01-04" "2020-01-09" "2020-01-14" "2020-01-19" "2020-01-24"
#> [6] "2020-01-29"
sample_ts$id # extract IDs
#> [1] "1" "1" "1" "1" "1" "1"
sample_ts$value # extract values
#> [1] 0.24241698 0.73568978 0.37475223 0.23795985 0.07836545 0.80696536
sample_ts$qa # extract quality values
#> [1] 0.4923586 0.8577236 0.6121701 0.4878113 0.2799383 0.8983125
sample_ts$rawval # extract raw values (for smooth_s2ts() and fill_s2ts() outputs)
#> NULL

# Load sample data generated by sen2rts functions
data(ts_raw)      # generated by extract_s2ts()
data(ts_smoothed) # generated by smooth_s2ts()
data(ts_filled)   # generated by fill_s2ts()

# Visualise data
print(ts_raw, topn = 5)
#> A raw s2ts time series with 60 dates and 2 IDs.
#>           Date Orbit Sensor            1               2   
#>  1: 2020-01-04   022     2B  0.116133333 ○   0.173306250 ○ 
#>  2: 2020-01-09   022     2A  0.020170833 ○   0.022352083 ○ 
#>  3: 2020-01-14   022     2B -0.004372917 ○   0.015077083 ○ 
#>  4: 2020-01-19   022     2A  0.116368750 ○   0.096704167 ○ 
#>  5: 2020-01-24   022     2B  0.123387500 ○   0.165727083 ○ 
#> ---                                                        
#> 56: 2020-10-10   022     2B  0.057233333 ○   0.104959233 ○ 
#> 57: 2020-10-15   022     2A -0.008637500 ○  -0.009322917 ○ 
#> 58: 2020-10-20   022     2B  0.057181250 ○   0.098320833 ○ 
#> 59: 2020-10-25   022     2A -0.038556250 ○  -0.067797917 ○ 
#> 60: 2020-10-30   022     2B  0.067635417 ○   0.078327083 ○ 
#> 
#> Quality flags:  ● [1]  ◕ [0.9,1)  ◑ [0.75,0.9)  ◔ [0.5,0.75)  ○ [0,0.5)
print(ts_smoothed, topn = 5)
#> A smoothed s2ts time series with 60 dates and 2 IDs.
#>           Date Orbit Sensor          1            2   
#>  1: 2020-01-04   022     2B 0.11606015 ○  0.1740709 ○ 
#>  2: 2020-01-09   022     2A         NA ○         NA ○ 
#>  3: 2020-01-14   022     2B         NA ○         NA ○ 
#>  4: 2020-01-19   022     2A         NA ○         NA ○ 
#>  5: 2020-01-24   022     2B 0.12343408 ○  0.2060596 ○ 
#> ---                                                   
#> 56: 2020-10-10   022     2B 0.05760022 ○  0.1021135 ○ 
#> 57: 2020-10-15   022     2A         NA ○         NA ○ 
#> 58: 2020-10-20   022     2B 0.05767390 ○  0.1016156 ○ 
#> 59: 2020-10-25   022     2A         NA ○         NA ○ 
#> 60: 2020-10-30   022     2B         NA ○         NA ○ 
#> 
#> Quality flags:  ● [1]  ◕ [0.9,1)  ◑ [0.75,0.9)  ◔ [0.5,0.75)  ○ [0,0.5)
print(ts_raw, topn = 5)
#> A raw s2ts time series with 60 dates and 2 IDs.
#>           Date Orbit Sensor            1               2   
#>  1: 2020-01-04   022     2B  0.116133333 ○   0.173306250 ○ 
#>  2: 2020-01-09   022     2A  0.020170833 ○   0.022352083 ○ 
#>  3: 2020-01-14   022     2B -0.004372917 ○   0.015077083 ○ 
#>  4: 2020-01-19   022     2A  0.116368750 ○   0.096704167 ○ 
#>  5: 2020-01-24   022     2B  0.123387500 ○   0.165727083 ○ 
#> ---                                                        
#> 56: 2020-10-10   022     2B  0.057233333 ○   0.104959233 ○ 
#> 57: 2020-10-15   022     2A -0.008637500 ○  -0.009322917 ○ 
#> 58: 2020-10-20   022     2B  0.057181250 ○   0.098320833 ○ 
#> 59: 2020-10-25   022     2A -0.038556250 ○  -0.067797917 ○ 
#> 60: 2020-10-30   022     2B  0.067635417 ○   0.078327083 ○ 
#> 
#> Quality flags:  ● [1]  ◕ [0.9,1)  ◑ [0.75,0.9)  ◔ [0.5,0.75)  ○ [0,0.5)

# See which function generated a s2ts object
attr(ts_raw, "gen_by")
#> [1] "extract_s2ts"
attr(ts_smoothed, "gen_by")
#> [1] "smooth_s2ts"
attr(ts_filled, "gen_by")
#> [1] "fill_s2ts"

# Convert in data frames or data tables
df_raw <- as.data.frame(ts_raw)
head(df_raw)
#>   id       date orbit sensor        value   qa
#> 1  1 2020-01-04   022     2B  0.116133333 0.33
#> 2  1 2020-01-09   022     2A  0.020170833 0.00
#> 3  1 2020-01-14   022     2B -0.004372917 0.00
#> 4  1 2020-01-19   022     2A  0.116368750 0.00
#> 5  1 2020-01-24   022     2B  0.123387500 0.33
#> 6  1 2020-01-29   022     2A  0.114379167 0.33
df_smoothed <- as.data.frame(ts_smoothed)
head(df_smoothed)
#>   id       date orbit sensor     value   qa       rawval
#> 1  1 2020-01-04   022     2B 0.1160602 0.33  0.116133333
#> 2  1 2020-01-09   022     2A        NA 0.00  0.020170833
#> 3  1 2020-01-14   022     2B        NA 0.00 -0.004372917
#> 4  1 2020-01-19   022     2A        NA 0.00  0.116368750
#> 5  1 2020-01-24   022     2B 0.1234341 0.33  0.123387500
#> 6  1 2020-01-29   022     2A 0.1137065 0.33  0.114379167
df_filled <- as.data.frame(ts_filled)
head(df_filled)
#>   id       date orbit sensor     value   qa     rawval interpolated
#> 1  1 2020-01-04   022     2B 0.1160602 0.33 0.11613333        FALSE
#> 2  1 2020-01-05  <NA>   <NA> 0.1207222   NA         NA         TRUE
#> 3  1 2020-01-06  <NA>   <NA> 0.1247380   NA         NA         TRUE
#> 4  1 2020-01-07  <NA>   <NA> 0.1281397   NA         NA         TRUE
#> 5  1 2020-01-08  <NA>   <NA> 0.1309599   NA         NA         TRUE
#> 6  1 2020-01-09   022     2A 0.1332310 0.00 0.02017083         TRUE