Converting raw met from SoyFACE (collected in 2010) to BioCro input.

See https://github.com/ebimodeling/soy/issues/1

Setup

knitr::opts_chunk$set(message = FALSE, warnings = FALSE,  cache = FALSE, 
                      fig.height= 12, fig.width = 6)
library(PEcAn.data.atmosphere)
library(PEcAn.BIOCRO)
library(BioCro)
library(dplyr)
library(data.table)
library(ggplot2)
library(lubridate)
library(ncdf4)
library(udunits2)
theme_set(theme_bw())

Import Raw data and view

met <- fread("~/dev/soy/data/soyface_met_2010.csv") 

met <- met %>% 
  mutate(datetime = ymd_h(paste(C_Year,C_Month, C_Day, C_Hour))) %>%
  subset(select = c("datetime",
                    "dw_psp [Wm-2]", "temp [Co]", "rh [%]", 
                    "Precip [mm]", "windspd [ms-1]"))
## write comma-delimited file for next step
write.csv(met, "/tmp/soyface_met_2010.csv", row.names = FALSE)

## change variable names for plotting

setnames(met, c("datetime", "dw_psp [Wm-2]", "temp [Co]", "rh [%]", "Precip [mm]", "windspd [ms-1]"),
                 c("date", "dw_psp", "Temp", "RH", "Precip", "Wind"))
gridExtra::grid.arrange(
ggplot(data = met, aes(date, dw_psp)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("dw_psp \n downwelling shortwave (NIR + PAR) W m-2"),
ggplot(data = met, aes(date, Temp)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Temperature (C)"),
ggplot(data = met, aes(date, RH)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Relative Humidity (%)"),
ggplot(data = met, aes(date, Precip)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Precipitation (mm/h)"),
ggplot(data = met, aes(date, Wind)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Wind m/s"),
ncol = 1
)

Transform variables; build PEcAn-CF compliant netCDF file

# run from root directory of soy/

format <- list(orig = c("datetime",
                        "dw_psp [Wm-2]", "temp [Co]", "rh [%]", 
                        "Precip [mm]", "windspd [ms-1]"),
               units = c("ymd_hms",
                         "J m-2 s-1", "celsius", "%", 
                         "mm", "m s-1"),
               bety = c("datetime", 
                        "solar_radiation", 
                        "airT", "relative_humidity",
                        "precipitation_flux", "Wspd"),
               skip = 0, unit.row = FALSE, na.strings = NA)
met2CF.csv(in.path = "/tmp/", in.file = "soyface_met_2010.csv", 
           outfolder = "data/", lat = 40.04, lon = -88.24, format = format)

Load Data in CF format

soyface.nc <- nc_open("data/soyface_met_2010_CF.nc")

cfmet <- load.cfmet(met.nc = soyface.nc, 
                  lat = 40.04, lon = -88.24,
                  start.date = "2010-01-01", 
                  end.date = "2010-12-31")

#narr.nc <- nc_open("~/.pecan/dbfiles/met/champaign.nc")
#narr.cfmet <- load.cfmet(met.nc = narr.nc, 
#                lat = 40.04, lon = -88.24,
#                start.date = "2000-01-01", 
#                end.date = "2000-12-31")

Look at met in CF units

gridExtra::grid.arrange(
ggplot(data = cfmet, aes(date, specific_humidity)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Specific Humidity"),

ggplot(data = cfmet, aes(date, relative_humidity)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Relative Humidity"),
ggplot(data = cfmet, aes(date, precipitation_flux)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Rain (kg / m2 / s"),
ggplot(data = cfmet, aes(date, surface_downwelling_shortwave_flux_in_air)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5)  + ggtitle("SW Down W/m2"),

ggplot(data = cfmet, aes(date, air_temperature -273.15)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Air Temperature degrees C"),

ggplot(data = cfmet, aes(date, wind_speed)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Wind m/s"),
ncol = 1
)

Convert to BioCro met format

biocro.met <- cf2biocro(cfmet)
write.csv(biocro.met, "data/soyface_met_2010_BIOCRO.csv", row.names = FALSE)

And in BioCro units:

biocro.met$date <- strptime(paste(biocro.met$year, biocro.met$doy, biocro.met$hour), format = "%Y%j%H")

gridExtra::grid.arrange(
ggplot(data = biocro.met, aes(date, SolarR)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Direct solar radiation (micro mol per meter squared per second"),
ggplot(data = biocro.met, aes(date, RH)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Relative Humidity (fraction)"),
ggplot(data = biocro.met, aes(date, Temp)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Air Temperature degrees C"),

ggplot(data = biocro.met, aes(date, WS)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Wind m/s"),
ggplot(data = biocro.met, aes(date, precip)) +
    geom_point(alpha = 0.1) +
    geom_line(alpha=0.2, size=0.5) + ggtitle("Rain, mm/h"),
ncol = 1
)

See if it works …

library(BioCro)
output <- willowGro(WetDat = biocro.met)
plot(output)