Structures representing a GADGET area file

gadget_areafile(size, temperature, area = attr(size, 'area'))

Arguments

size

data.frame as produced by mfdb_area_size

temperature

data.frame as produced by mfdb_temperature

area

Optional. mfdb_group that you used to specify area. By default pulls it from annotations on the size object.

Details

Once formed, you can then use gadget_dir_write to write this out to a GADGET areafile.

Value

List of class 'gadget_areafile' that represents the area file contents.

Examples


# Open a temporary database connection
mdb <- mfdb(tempfile(fileext = '.duckdb'))
#> 2022-11-16 12:34:20 INFO:mfdb:Creating schema from scratch
#> 2022-11-16 12:34:20 INFO:mfdb:Taxonomy market_category no updates to make
#> 2022-11-16 12:34:20 INFO:mfdb:Schema up-to-date

# Define 2 areacells of equal size
mfdb_import_area(mdb, data.frame(name=c("divA", "divB"), size=1))

# We want to have 3 area groups, 2 for original cells, one aggregating across the lot
area_group <- mfdb_group(
    divA = c("divA"),
    divB = c("divB"),
    divAB = c("divA", "divB"))

# Make up temperature data
temps <- expand.grid(year=c(1998,2000), month=c(1:12), areacell=c("divA", "divB"))
temps$temperature <- runif(nrow(temps), 5, 10)
mfdb_import_temperature(mdb, temps)

# Create an areafile from 2 mfdb queries
areafile <- gadget_areafile(
    mfdb_area_size(mdb, list(
        area = area_group))[[1]],
    mfdb_temperature(mdb, list(
        year = 1998:2000,
        timestep = mfdb_timestep_quarterly,
        area = area_group))[[1]])
areafile
#> $labels
#> [1] "divA"  "divB"  "divAB"
#> 
#> $areas
#> [1] 1 2 3
#> 
#> $size
#>  divA  divB divAB 
#>     1     1     2 
#> 
#> $temperature
#>    year step area mean
#> 1  1998    1    1  6.6
#> 2  1998    1    3  7.0
#> 3  1998    1    2  7.4
#> 4  1998    2    1  7.1
#> 5  1998    2    3  8.3
#> 6  1998    2    2  9.5
#> 7  1998    3    1  6.5
#> 8  1998    3    3  7.1
#> 9  1998    3    2  7.6
#> 10 1998    4    1  8.0
#> 11 1998    4    3  8.2
#> 12 1998    4    2  8.3
#> 13 2000    1    1  8.7
#> 14 2000    1    3  8.5
#> 15 2000    1    2  8.4
#> 16 2000    2    1  6.2
#> 17 2000    2    3  6.0
#> 18 2000    2    2  5.7
#> 19 2000    3    1  7.9
#> 20 2000    3    3  8.2
#> 21 2000    3    2  8.5
#> 22 2000    4    1  8.5
#> 23 2000    4    3  8.4
#> 24 2000    4    2  8.3
#> 
#> attr(,"class")
#> [1] "gadget_areafile"

# Write this to a gadget_directory
gadget_dir_write(gadget_directory(tempfile()), areafile)

# Check data in file matches input data
stopifnot(identical(
    areafile$size,
    c(divA=1, divB=1, divAB=2)))
stopifnot(all.equal(
    mean(areafile$temperature[areafile$temperature$area == 1, 'mean']),
    mean(temps[temps$areacell == 'divA', 'temperature']),
    tolerance = 1e-2))
stopifnot(all.equal(
    mean(areafile$temperature[areafile$temperature$area == 2, 'mean']),
    mean(temps[temps$areacell == 'divB', 'temperature']),
    tolerance = 1e-2))
stopifnot(all.equal(
    mean(areafile$temperature[areafile$temperature$area == 3, 'mean']),
    mean(temps[,'temperature']),
    tolerance = 1e-2))

mfdb_disconnect(mdb)