Graphics of Grid1 of Spectra in Bertolina

require(rgdal)
require(raster)

source("ABSpect_rowcol.R")
source("ABSpect_prepggplot.R")
source("ggfgrid.R")

Data handling for ggplot

Grids

BertGrid1_20130601_metav2 <- ABSpect_rowcol(tmetad = BertGrid1_20130601_meta, 
    metadpx = BertGrid1_20130601_px)

Note: should we be saving “_meta2” df as shapefiles instead of “_meta”?
Note: I was incorrectely using the name “BertGrid1_20130601_wide”, while this is precisely “long” format

BertGrid1_20130601_long <- ABSpect_prepggplot(tmetad = BertGrid1_20130601_metav2@data, 
    tgrid = BertGrid1_20130601_spect@data)
## [1] "Dimensions of tmetad"
## [1] 60 18
## [1] "Dimensions of tgrid"
## [1]  60 259
## [1] "Matching WP?"
## [1] TRUE
## [1] "First row:"
##      obsid WPNUM site row col x y replicate   Lon  Lat Height Heading
## WP01  WP01     1    1   4   1 1 1         1 1.661 42.1     16       0
##      TimeWP DesPosAcc Flags CamTilt            Time    IT  X338.3  X340.2
## WP01      4         2    55      90 20130601_101328 8e+05 0.02103 0.02783
##         X342
## WP01 0.01278
## [1] "Dimension of merged data.frame (wide format):"
## [1]  60 274
## [1] 274
## [1] "Dimension of merged data.frame (long format):"
## [1] 15360    20
BertGrid21_20130601_metav2 <- ABSpect_rowcol(tmetad = BertGrid21_20130601_meta, 
    metadpx = BertGrid21_20130601_px)
BertGrid21_20130601_long <- ABSpect_prepggplot(tmetad = BertGrid21_20130601_metav2@data, 
    tgrid = BertGrid21_20130601_spect@data)
## [1] "Dimensions of tmetad"
## [1] 60 18
## [1] "Dimensions of tgrid"
## [1]  60 259
## [1] "Matching WP?"
## [1] TRUE
## [1] "First row:"
##      obsid WPNUM site row col x y replicate  Lon  Lat Height Heading
## WP01  WP01     1    1   4   1 1 1         1 1.66 42.1     16       0
##      TimeWP DesPosAcc Flags CamTilt            Time    IT  X338.3  X340.2
## WP01      4         2    55      90 20130601_104745 6e+05 0.02615 0.01752
##         X342
## WP01 0.01469
## [1] "Dimension of merged data.frame (wide format):"
## [1]  60 274
## [1] 274
## [1] "Dimension of merged data.frame (long format):"
## [1] 15360    20
BertGrid22_20130601_metav2 <- ABSpect_rowcol(tmetad = BertGrid22_20130601_meta, 
    metadpx = BertGrid22_20130601_px)
BertGrid22_20130601_long <- ABSpect_prepggplot(tmetad = BertGrid22_20130601_metav2@data, 
    tgrid = BertGrid22_20130601_spect@data)
## [1] "Dimensions of tmetad"
## [1] 60 18
## [1] "Dimensions of tgrid"
## [1]  60 259
## [1] "Matching WP?"
## [1] TRUE
## [1] "First row:"
##      obsid WPNUM site row col x y replicate  Lon  Lat Height Heading
## WP01  WP01     1    1   4   1 1 1         1 1.66 42.1     16       0
##      TimeWP DesPosAcc Flags CamTilt            Time     IT  X338.3  X340.2
## WP01      4         2    55      90 20130601_113313 120000 0.01614 0.01047
##         X342
## WP01 0.02159
## [1] "Dimension of merged data.frame (wide format):"
## [1]  60 274
## [1] 274
## [1] "Dimension of merged data.frame (long format):"
## [1] 15360    20
BertGrid3_20130601_metav2 <- ABSpect_rowcol(tmetad = BertGrid3_20130601_meta, 
    metadpx = BertGrid3_20130601_px)
BertGrid3_20130601_long <- ABSpect_prepggplot(tmetad = BertGrid3_20130601_metav2@data, 
    tgrid = BertGrid3_20130601_spect@data)
## [1] "Dimensions of tmetad"
## [1] 60 18
## [1] "Dimensions of tgrid"
## [1]  60 259
## [1] "Matching WP?"
## [1] TRUE
## [1] "First row:"
##      obsid WPNUM site row col x y replicate   Lon  Lat Height Heading
## WP01  WP01     1    1   4   1 1 1         1 1.662 42.1     16       0
##      TimeWP DesPosAcc Flags CamTilt            Time    IT X338.3 X340.2
## WP01      4         2    55      90 20130601_105956 5e+05  0.018 0.0158
##         X342
## WP01 0.01619
## [1] "Dimension of merged data.frame (wide format):"
## [1]  60 274
## [1] 274
## [1] "Dimension of merged data.frame (long format):"
## [1] 15360    20
# Forest: meta includes the water point, which is outside the grid. we
# discard it and keep on different files
delme <- BertGridForest_20130601_meta
delme <- delme[delme@data$site != 17, ]
BertGridForest_20130601_metav2 <- ABSpect_rowcol(tmetad = delme, metadpx = BertGridForest_20130601_px)
BertGridForest_20130601_meta[BertGridForest_20130601_meta$site == 17, 1:5]
##        coordinates obsid WPNUM site replicate   Lon
## WP49 (1.661, 42.1)  WP49    49   17         1 1.661
## WP50 (1.661, 42.1)  WP50    50   17         2 1.661
## WP51 (1.661, 42.1)  WP51    51   17         3 1.661
delme <- BertGridForest_20130601_spect@data
delme <- delme[BertGridForest_20130601_meta@data$site != 17, ]
BertGridForest_20130601_long <- ABSpect_prepggplot(tmetad = BertGridForest_20130601_metav2@data, 
    tgrid = delme)
## [1] "Dimensions of tmetad"
## [1] 48 18
## [1] "Dimensions of tgrid"
## [1]  48 259
## [1] "Matching WP?"
## [1] TRUE
## [1] "First row:"
##      obsid WPNUM site row col x y replicate   Lon  Lat Height Heading
## WP01  WP01     1    1   4   1 1 1         1 1.662 42.1     25       0
##      TimeWP DesPosAcc Flags CamTilt            Time    IT  X338.3  X340.2
## WP01      4         3    55      90 20130601_142454 1e+05 0.01061 0.02317
##         X342
## WP01 0.01929
## [1] "Dimension of merged data.frame (wide format):"
## [1]  48 274
## [1] 274
## [1] "Dimension of merged data.frame (long format):"
## [1] 12288    20
rm(delme)

Pond (1 single site outside the grid)

We select the pond

BertPond_20130601_meta <- BertGridForest_20130601_meta[BertGridForest_20130601_meta@data$site == 
    17, ]

We have to create metav2 in the same way as ABSpect_rowcol.R does:

BertPond_20130601_metav2 <- BertPond_20130601_meta
BertPond_20130601_metav2@data <- cbind(BertPond_20130601_meta@data[, 1:3], row = 1, 
    col = 1, x = 1, y = 1, BertPond_20130601_meta@data[, -(1:3)])
BertPond_20130601_metav2@data <- BertPond_20130601_metav2@data[, -17]  #discard 'Name' field
BertPond_20130601_spect <- BertGridForest_20130601_spect[BertGridForest_20130601_meta@data$site == 
    17, ]
BertPond_20130601_long <- ABSpect_prepggplot(tmetad = BertPond_20130601_metav2@data, 
    tgrid = BertPond_20130601_spect@data)
## [1] "Dimensions of tmetad"
## [1]  3 18
## [1] "Dimensions of tgrid"
## [1]   3 259
## [1] "Matching WP?"
## [1] TRUE
## [1] "First row:"
##      obsid WPNUM site row col x y replicate   Lon  Lat Height Heading
## WP49  WP49    49   17   1   1 1 1         1 1.661 42.1     20       0
##      TimeWP DesPosAcc Flags CamTilt            Time    IT  X338.3  X340.2
## WP49      4         3    55      90 20130601_143215 1e+05 0.02152 0.01797
##         X342
## WP49 0.02159
## [1] "Dimension of merged data.frame (wide format):"
## [1]   3 274
## [1] 274
## [1] "Dimension of merged data.frame (long format):"
## [1] 768  20

ggplots

Grid 1: test several graphics

# I omit this after the first run spin('ABSpect_Bertgrid1_ggplot_log.R')

All Grids

By now we just run the gridded ggplot Note: Function for the grid ggplot (Grid of Spectra matching geometry in the field) ggfgrid() saved as independent ggfgrid.R

ggfgrid(ABSpectm = BertGrid1_20130601_long, svgname = "grid1ggField.svg", pdfname = "grid1ggField.pdf", 
    view = TRUE, title = "Spectra of Grid 1")
## Saving 21 x 15 in image
## Saving 21 x 15 in image

plot of chunk unnamed-chunk-7

ggfgrid(ABSpectm = BertGrid21_20130601_long, svgname = "grid21ggField.svg", 
    pdfname = "grid21ggField.pdf", view = TRUE, title = "Spectra of Grid 2.1")
## Saving 21 x 15 in image
## Saving 21 x 15 in image

plot of chunk unnamed-chunk-7

ggfgrid(ABSpectm = BertGrid22_20130601_long, svgname = "grid22ggField.svg", 
    pdfname = "grid22ggField.pdf", view = TRUE, title = "Spectra of Grid 2.2")
## Saving 21 x 15 in image
## Saving 21 x 15 in image

plot of chunk unnamed-chunk-7

ggfgrid(ABSpectm = BertGrid3_20130601_long, svgname = "grid3ggField.svg", pdfname = "grid3ggField.pdf", 
    view = TRUE, title = "Spectra of Grid 3")
## Saving 21 x 15 in image
## Saving 21 x 15 in image

plot of chunk unnamed-chunk-7

ggfgrid(ABSpectm = BertGridForest_20130601_long, svgname = "gridForestggField.svg", 
    pdfname = "gridForestggField.pdf", view = TRUE, title = "Spectra of Grid Forest")
## Warning: Removed 1 rows containing missing values (geom_path).
## Saving 21 x 15 in image
## Warning: Removed 1 rows containing missing values (geom_path).
## Saving 21 x 15 in image
## Warning: Removed 1 rows containing missing values (geom_path).

plot of chunk unnamed-chunk-7

ggfgrid(ABSpectm = BertPond_20130601_long, svgname = "PondggField.svg", pdfname = "PondggField.pdf", 
    view = TRUE, title = "Spectra of Pond")
## Saving 21 x 15 in image
## Saving 21 x 15 in image

plot of chunk unnamed-chunk-7

ABSpectm <- BertPond_20130601_long
ggplot() + geom_line(data = ABSpectm, aes(x = Wavelength, y = Reflectance, group = obsid, 
    col = replicate)) + ylim(c(0, 0.4)) + ggtitle("Spectra of Pond")

plot of chunk unnamed-chunk-7

rm(ABSpectm)

Save data for midvatten

meta

No hace falta el csv para BertGrid1_20130601_metav2. Se usa el shapefile BertGrid1_20130601_meta pero hay que tener el campo id como obsid (o poner otra columna al escribir el shape)

Spectra

Bertgrid1mvspec <- BertGrid1_20130601_long[, c(1, 19, 20)]
names(Bertgrid1mvspec) <- c("obsid", "wavelength", "reflectance")
write.csv(Bertgrid1mvspec, file = "Bertgrid1mvspec.csv", row.names = FALSE)

Create db sqlite

External tool (i.e. con Firefox) including table *spec

Create sqlite db from within R:

based on R-bloggers article

require(sqlutils)
require(RSQLite)
# install_github('retention','jbryer')

m <- dbDriver("SQLite")
sqlname <- file.path("./sqlite", "Bertgrid1mvspec.sqlite")
file.create(sqlname)
## [1] TRUE
conn <- dbConnect(m, dbname = sqlname)
dbWriteTable(conn, "Bertgrid1mvspec", Bertgrid1mvspec)
## [1] TRUE

Bertgrid21mvspec <- BertGrid21_20130601_long[, c(1, 19, 20)]
names(Bertgrid21mvspec) <- c("obsid", "wavelength", "reflectance")
sqlname <- file.path("./sqlite", "Bertgrid21mvspec.sqlite")
file.create(sqlname)
## [1] TRUE
conn <- dbConnect(m, dbname = sqlname)
dbWriteTable(conn, "Bertgrid21mvspec", Bertgrid21mvspec)
## [1] TRUE

Bertgrid22mvspec <- BertGrid22_20130601_long[, c(1, 19, 20)]
names(Bertgrid22mvspec) <- c("obsid", "wavelength", "reflectance")
sqlname <- file.path("./sqlite", "Bertgrid22mvspec.sqlite")
file.create(sqlname)
## [1] TRUE
conn <- dbConnect(m, dbname = sqlname)
dbWriteTable(conn, "Bertgrid22mvspec", Bertgrid22mvspec)
## [1] TRUE

Bertgrid3mvspec <- BertGrid3_20130601_long[, c(1, 19, 20)]
names(Bertgrid3mvspec) <- c("obsid", "wavelength", "reflectance")
sqlname <- file.path("./sqlite", "Bertgrid3mvspec.sqlite")
file.create(sqlname)
## [1] TRUE
conn <- dbConnect(m, dbname = sqlname)
dbWriteTable(conn, "Bertgrid3mvspec", Bertgrid3mvspec)
## [1] TRUE

BertgridForestmvspec <- BertGridForest_20130601_long[, c(1, 19, 20)]
names(BertgridForestmvspec) <- c("obsid", "wavelength", "reflectance")
sqlname <- file.path("./sqlite", "BertgridForestmvspec.sqlite")
file.create(sqlname)
## [1] TRUE
conn <- dbConnect(m, dbname = sqlname)
dbWriteTable(conn, "BertgridForestmvspec", BertgridForestmvspec)
## [1] TRUE

BertPondmvspec <- BertPond_20130601_long[, c(1, 19, 20)]
names(BertPondmvspec) <- c("obsid", "wavelength", "reflectance")
sqlname <- file.path("./sqlite", "BertPondmvspec.sqlite")
file.create(sqlname)
## [1] TRUE
conn <- dbConnect(m, dbname = sqlname)
dbWriteTable(conn, "BertPondmvspec", BertPondmvspec)
## [1] TRUE