extractDate: Extract Dates from (MODIS) Files

Description Usage Arguments Value Author(s) Examples

View source: R/extractDate.R

Description

This function helps to extract dates from a vector of files.

Usage

1
extractDate(files, pos1, pos2, asDate = FALSE, format = "%Y%j")

Arguments

files

A character vector of filenames from which to extract dates. Alternatively, a Raster* with date information in its layer names.

pos1, pos2

Start and end of date string in files as integer. If missing, positions are tried to be retrieved from a look-up table provided that 'files' comply with the MODIS standard naming convention.

asDate

logical. If TRUE, the result is converted to a Date object.

format

character, date format. Used only if asDate = TRUE. Defaults to MODIS date style (i.e., "%Y%j" for year and julian day). See strptime for modifications.

Value

A list with the following entries: 'inputLayerDates', 'pos1', 'pos2', 'asDate' and, optionally, 'format'. If asDate = FALSE, 'inputLayerDates' are represented as character, else as Date.

Author(s)

Matteo Mattiuzzi

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# example on HDF files
files <- c("MOD13Q1.A2010209.h18v03.005.2010239071130.hdf",
           "MOD13Q1.A2010225.h18v03.005.2010254043849.hdf")
extractDate(files)
extractDate(files, asDate = TRUE)

# on any other file 
files <- c("Myfile_20010101.XXX", "Myfile_20010115.XXX", "Myfile_20010204.XXX")
extractDate(files, pos1 = 8, pos2 = 15)
extractDate(files, pos1 = 8, pos2 = 15, asDate = TRUE, format = "%Y%m%d")
 

Example output

Loading required package: mapdata
Loading required package: maps
Loading required package: raster
Loading required package: sp
Warning message:
In file(con, "r") : cannot open file '/proc/stat': Permission denied
$inputLayerDates
MOD13Q1.A2010209.h18v03.005.2010239071130.hdf 
                                    "2010209" 
MOD13Q1.A2010225.h18v03.005.2010254043849.hdf 
                                    "2010225" 

$pos1
[1] 10 10

$pos2
[1] 16 16

$asDate
[1] FALSE

$inputLayerDates
[1] "2010-07-28" "2010-08-13"

$pos1
[1] 10 10

$pos2
[1] 16 16

$asDate
[1] TRUE

$format
[1] "%Y%j"

$inputLayerDates
Myfile_20010101.XXX Myfile_20010115.XXX Myfile_20010204.XXX 
         "20010101"          "20010115"          "20010204" 

$pos1
[1] 8

$pos2
[1] 15

$asDate
[1] FALSE

$inputLayerDates
[1] "2001-01-01" "2001-01-15" "2001-02-04"

$pos1
[1] 8

$pos2
[1] 15

$asDate
[1] TRUE

$format
[1] "%Y%m%d"

MODIS documentation built on Nov. 12, 2020, 5:06 p.m.