extract_copernicus: Extract and process COPERNICUS h5 files

Description Usage Arguments Value Author(s) Examples

Description

Un-zip, extract the given layers of COPERNICUS h5 files to tif files. The function can also crop, project and apply gain and offset values

Usage

1
2
extract_copernicus(fnames,extent,extend,convertDN = TRUE,outProj,pixelSize,resamplingType,
                   outPath,job,gdalPath,zip = TRUE,layers = 1, allowParallel = FALSE,...)

Arguments

fnames

character vector of the file names to transform (with a zip or h5 extension). If fnames is a list of vector of file names, then files are automatically mosaicked for each element of the list

extent

A Raster-class, Extent or SpatialPolygons-class giving the subwindow to crop the image to. If this is an Extent object, it is assumed to be in geographical coordinates. Else, the projection info of the object is used to project the image to the same projection.

extend

numeric vector of length 1 or 2 (nrow,ncol). Extend the spatial extent of the subwindows (extent argument) by a given number of (rows,cols)

convertDN

logical value indicating whether DN values should be converted to physical values (eg vegetation index). Default to TRUE

outProj

character string giving the coordinate projection system of the output in the PROJ.4 format. Can also be a CRS-class object. Default is '+init=epsg:32662' (Plate Carree, WGS84), i.e. no re-projection is done. See copernicus_options('outProj') to change default value. if the extent argument is set, with an object of class Raster-class or SpatialPolygons-class, their projection info is used instead, unless their projection is 'NA'.

pixelSize

output pixel size (c(xres,yres)). Default is 'asIn' (same as the input image). Can be set via copernicus_options('pixelSize'). If a Raster-class object is provided as extent argument, the resolution of the object override this argument.

resamplingType

Resampling method in case of reprojection or change in pixel resolution. Should be one of the following: 'near','bilinear','cubic','cubicspline'

outPath

Path where processed files should be stored. Default set via copernicus_options('outPath')

job

Path to append to outPath

gdalPath

Path to the gdal binaries. Default set via copernicus_options('gdalPath')

zip

logical value indicating whether file(s) should be un-zipped before h5 extraction

layers

numeric vector indicating the index/indices of the layer(s) to extract from the h5 file(s). Default set to 1 See eg http://land.copernicus.eu/global/products/ndvi for more details.

allowParallel

Logical. If a foreach parallel backend is loaded and available, should the function use it? Default is FALSE.

...

arguments passed to writeRaster

Value

Return invisibly the list of extracted h5 files. If files are mosaicked, tiles numbers are dropped from the names

Author(s)

Antoine Stevens

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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
66
67
68
69
70
71
72
73
74
75
76
## Not run: 
# Don't forget to provide in copernicus_options() your user and password details
# for COPERNICUS data portal before running this
# e.g. : copernicus_options(user = "Smith", password = "hello")
# First, get data: NDVI_1km_V1, for JAN 2009
fn <- download_copernicus(product = 'NDVI_1km_V1', begin = '2009-01-01', end = '2009-01-31',
                     tileH = 19, tileV = 4)
fn # downloaded file names
# extract layers 1,2,3 of the downladed file. Store images in the 'H19V4' folder
extract_copernicus(fnames = fn,job = 'H19V4',layers = c(1:3))
# Now, crop the tile to a given extent (extent argument),
# and add 1 row and col at each side of the data (extend argument)
# This is useful to add a buffer zone, in case of spatial smoothing
# tile location is:
get_tile_copernicus(tileH = 19, tileV = 4)
# create extent object
library(raster)
# extent corresponding to the Delta of the Po
e <- extent(c(10.5,12.5,44,46)) # xmin,xmax,ymin,ymax
f <- extract_copernicus(fnames = fn,extent = e,job = 'H19V4',extend = 1, layers = 2)
f # name of the h5 file(s)
# for each layer, the function append its name to the saved file name
# so we can change the name of the file(s) accordingly
f <- sub('\\.h5','_NDVI.tif',f)
# this is the resulting raster stack
s <- stack(f);s
plot(s)
# extent argument could also be a Raster* object,
# its projection, and resolution are then used to crop/project
# first let's create a raster object using the extent object
r <- raster(e)
projection(r) <- '+init=epsg:4326' # LatLon WGS84
# project to UTM 32N
r <- projectExtent(r,CRS('+init=epsg:32632'))
res(r) <- 1000 # set resolution to 1 km
# now extract COPERNICUS data and match these topology
f <- extract_copernicus(fnames = fn,extent = r,job = 'H19V4',extend = 1, layers = 2)
s <- stack(sub('\\.h5','_NDVI.tif',f));s
plot(s)
# This works with a SpatialPolygons* object too
# resolution can be set through pixelSize (in target geometry)
sPol <- as(e,'SpatialPolygons')
proj4string(sPol) <- CRS('+init=epsg:4326')
sPol <- spTransform(sPol,CRS('+init=epsg:32632')) # project to UTM 32N
f <- extract_copernicus(fnames = fn,extent = sPol,pixelSize = c(2000,2000),
                        job = 'H19V4',extend = 1, layers = 2)
s <- stack(sub('\\.h5','_NDVI.tif',f));s
plot(s)
# One can also simply reproject using the outProj argument
# and change resolution with the pixelSize argument
f <- extract_copernicus(fnames = fn,outProj = '+init=epsg:32632', pixelSize = c(1000,1000),
                        job = 'H19V4',layers = 2)
s <- stack(sub('\\.h5','_NDVI.tif',f));s
plot(s)
# extract_copernicus allows also to mosaic files, by grouping file names within a list
# first, download data spanning neighbouring tiles
e <- extent(c(-1,1,49,51)) # the English Channel
# the groupByDate argument allows to return a list of files, grouped by date
fn <- download_copernicus(product, begin = '2009-01-01', end = '2009-01-31',
                         extent = e,groupByDate = TRUE)
# now extract
f <- extract_copernicus(fnames = fn,extent = e, layers = 2)
f <- sub('\\.h5','_NDVI.tif',f)
s <-stack(f);s
plot(s,1)
# Let's add the COPERNICUS tiling system, projected to Plate Carree
sPol <- spTransform(gen_tile_copernicus(poly = T),CRS("+init=epsg:32662"))
plot(sPol,add=T)
# mosaicking works also with projection
f <- extract_copernicus(fnames = fn,extent = e, outProj = '+init=epsg:32632', layers = 2,
                       resamplingType = "bilinear")
f <- sub('\\.h5','_NDVI.tif',f)
s <-stack(f);s
plot(s)

## End(Not run)

antoinestevens/copernicus documentation built on May 10, 2019, 12:23 p.m.