PEcAn: Generating met drivers for the MAAT model using AmerifluxLBL tower observations

Overview

This is a demonstration of the PEcAn utilities for downloading met data, converting it to the PEcAn-CF format (which is based on the Climate Forecasting conventions and similar to MsTMIP), gapfilling the CF driver data, and converting to model-specific drivers for the MAAT model. These variables are defined in the PEcAn documentation.

In this example we will download 6 years of met data from the Willow Creek Eddy Covariance tower site (US-WCr). It has an Ameriflux SITE_ID of US-WCr

The PEcAn.data.atmosphere source code is in modules/data.atmosphere and the documentation can be found with either package?PEcAn.data.atmosphere or in the data.atmosphere package documentation.

First make sure you have the nescessary PEcAn package for processing met data

options(repos = c(getOption("repos"), PEcAn = "pecanproject.r-universe.dev"))
install.packages("PEcAn.data.atmosphere")
library(PEcAn.data.atmosphere)

Connect to BETY database and retrieve appropriate format for AmerifluxLBL datasets [TODO: Update to work with R-OpenSci traits BETY]

con <- PEcAn.DB::db.open(
   list(user='bety', password='bety', host='localhost',
        dbname='bety', driver='PostgreSQL',write=TRUE))
format.id <- 5000000002
format <- PEcAn.DB::query.format.vars(format.id=format.id,bety = con)
format$time_zone <- "America/Chicago"

(Note: To avoid needing a database connection at vignette build time, we use code that does not display in the compiled vignette to construct a static version of the format object. When running this document interactively, with a live database connention, you can use the result from query.format.vars directly.)

# copy-pasted from `dput(format)` after evaluating the `bety` chunk above
# using Chris Black's development version of BeTY on 2024-09-16
format <- list(
   file_name = "AMERIFLUX_BASE_HH",
   mimetype = "csv",
   vars = structure(
      list(
         bety_name = c("air_pressure", "airT", "co2atm", "datetime", "FC", "H",
            "LE", "NEE", "PAR", "precipitation_flux", "relative_humidity",
            "soilM", "soilT", "solar_radiation", "specific_humidity",
            "surface_downwelling_longwave_flux_in_air", "TotalResp", "UST",
            "VPD", "wind_direction", "Wspd"),
         variable_id = c(554, 86, 135, 5000000001, 1000000042, 299, 298, 297,
            136, 555, 556, 391, 379, 547, 557, 561, 1000000206, 1000000041, 387,
            560, 390),
         input_name = c("PA", "TA", "CO2_1", "TIMESTAMP_START", "FC", "H", "LE",
            "NEE_PI", "PPFD_IN", "P", "RH", "SWC_1", "TS_1", "SW_IN", "H2O",
            "LW_IN", "RECO_PI", "USTAR", "VPD", "WD", "WS"),
         input_units = c("kPa", "celsius", "umol mol-1", "ymd_hms",
            "umol C m-2 s-1", "W m-2","W m-2", "umol C m-2 s-1", "umol m-2 s-1",
            "kg m-2 (30 minute)-1", "percent", "%", "celsius", "W m-2",
            "g kg-1", "W m-2", "umol C m-2 s-1", "m s-1", "hPa", "degrees",
            "m s-1"),
         storage_type = c("", "", "", "%Y%m%d%H%M", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", ""),
         column_number = c(19L, 4L, 20L, 1L, 8L, 10L, 12L, 7L, 26L, 17L, 18L,
            23L, 15L, 27L, 33L, 31L, NA, 3L, NA, 5L, 6L),
         bety_units = c("Pa", "degrees C", "ppm (= umol mol-1)", "ymd_hms",
            "umol C m-2 s-1", "W m-2", "W m-2", "umol C m-2 s-1",
            "umol m-2 s-1", "Kg/m2/s", "%", "%", "degrees C", "W/m^2", "g g-1",
            "W m-2", "kg C m-2 s-1", "m s-1", "Pa", "degrees", "m s-1"),
         mstmip_name = c("Psurf", "Tair", "CO2air", NA, "FC", "Qh", "Qle",
            "NEE", NA, "Rainf", NA, "SoilMoistFrac", "SoilTemp", "SWdown",
            "Qair", "LWdown", "TotalResp", NA, NA, NA, NA),
         mstmip_units = c("Pa", "K", "micromol mol-1", NA, "kg C m-2 s-1",
            "W m-2", "W m-2", "kg C m-2 s-1", NA, "kg m-2 s-1", NA, "1", "K",
            "W/m^2", "kg kg-1", "W/m2", "kg C m-2 s-1", NA, NA, NA, NA),
         pecan_name = c("Psurf", "Tair", "CO2air", "datetime", "FC", "Qh",
            "Qle", "NEE", "PAR", "Rainf", "relative_humidity", "SoilMoistFrac",
            "SoilTemp", "SWdown", "Qair", "LWdown", "TotalResp", "UST", "VPD",
            "wind_direction", "Wspd"),
         pecan_units = c("Pa", "K", "micromol mol-1", "ymd_hms", "kg C m-2 s-1",
            "W m-2", "W m-2", "kg C m-2 s-1", "umol m-2 s-1", "kg m-2 s-1", "%",
            "1", "K", "W/m^2", "kg kg-1", "W/m2", "kg C m-2 s-1", "m s-1", "Pa",
            "degrees", "m s-1")),
      row.names = c(NA, -21L),
      class = "data.frame"),
   skip = 2,
   header = 1,
   na.strings = c("-9999", "-6999", "9999", "NA"),
   time.row = 4L,
   site = NULL,
   lat = NULL,
   lon = NULL,
   time_zone = "America/Chicago")

Download AmerifluxLBL data for selected site and dates (US-WCr, 2000 to 2005)

m2mdir <- tempfile("met2model_testing")
dir.create(m2mdir)
download.AmerifluxLBL(sitename = "US-WCr", outfolder = m2mdir,
                   start_date = "2000-01-01", end_date = "2005-12-31", overwrite = TRUE, verbose = TRUE)
## Data use guidelines for AmeriFlux CC-BY-4.0 Data Policy:
## 
## (1) Data user is free to Share (copy and redistribute the material in any medium or format) and/or Adapt (remix, transform, and build upon the material) for any purpose.
## (2) Provide a citation to each site data product that includes the data-product DOI and/or recommended publication.
## (3) Acknowledge funding for supporting AmeriFlux data portal: U.S. Department of Energy Office of Science.
##                                                                       file
## 1 /tmp/RtmpL3Kfzp/met2model_testing1d65ac19c30/AMF_US-WCr_BASE_HH_31-5.csv
##           host mimetype        formatname        startdate          enddate
## 1 768260f87aa0 text/csv AMERIFLUX_BASE_HH 1998-01-01 00:00 2024-06-18 23:30
##               dbfile.name
## 1 AMF_US-WCr_BASE_HH_31-5

Convert downloaded AmerifluxLBL data to CF format

cfdir <- file.path(m2mdir, "CF")
met2CF.AmerifluxLBL(in.path = m2mdir, in.prefix = "AMF_US-WCr", outfolder = cfdir,
                 start_date = "2000-01-01", end_date = "2005-12-31",format=format)

Gapfill CF met drivers

gapfilldir <- file.path(cfdir, "gapfill")
metgapfill(in.path = cfdir, in.prefix = "AMF_US-WCr_BASE_HH_14-5",
           outfolder = gapfilldir, start_date = "2000-01-01", end_date = "2005-12-31")

Create MAAT model-specific met drivers

in.path <- gapfilldir
in.prefix <- "AMF_US-WCr_BASE_HH_14-5"
outfolder <- file.path(gapfilldir, "maat_drivers")
start_date <- "2000-01-01"
end_date <- "2005-12-31"
overwrite <- TRUE
verbose <- TRUE
leap_year = TRUE
PEcAn.MAAT::met2model.MAAT(in.path, in.prefix, outfolder, start_date, end_date,
                           overwrite = overwrite, verbose = verbose)
## [1] "internal results"
##                                                                                                                     file
## 1 /tmp/RtmpL3Kfzp/met2model_testing1d65ac19c30/CF/gapfill/maat_drivers/AMF_US-WCr_BASE_HH_14-5.2000-01-01.2005-12-31.csv
##           host mimetype       formatname  startdate    enddate
## 1 768260f87aa0 text/csv MAAT meteorology 2000-01-01 2005-12-31
##                                         dbfile.name
## 1 AMF_US-WCr_BASE_HH_14-5.2000-01-01.2005-12-31.csv
## [1] "Skipping to next year"
## [1] "Skipping to next year"
## [1] "Skipping to next year"
## [1] "Skipping to next year"
## [1] "Skipping to next year"
## [1] "Skipping to next year"
## [1] "NO MET TO OUTPUT"