├── data
├── obsWheat.rda
└── wheat-opt-ex.rda
├── inst
└── extdata
│ ├── ISUAG
│ └── SSURGO
│ │ ├── ISUAG_SSURGO_Mapunits.dbf
│ │ ├── ISUAG_SSURGO_Mapunits.shp
│ │ ├── ISUAG_SSURGO_Mapunits.shx
│ │ ├── ISUAG_SSURGO_Mapunits.prj
│ │ └── ISUAG_SSURGO_mapunit.csv
│ └── Maize.csv
├── man
├── print.met.Rd
├── wop.Rd
├── mcmc.apsim.env.Rd
├── mcmc.apsimx.env.Rd
├── wop.h.Rd
├── auto_detect_apsimx_examples.Rd
├── amp_apsim_met.Rd
├── auto_detect_apsim_examples.Rd
├── napad_apsim_met.Rd
├── tav_apsim_met.Rd
├── grep_json_list.Rd
├── soilorganicmatter_parms.Rd
├── read_apsimx_all.Rd
├── obsWheat.Rd
├── doy2date.Rd
├── check_apsim_met.Rd
├── impute_apsim_met.Rd
├── apsim.options.Rd
├── apsimx_filetype.Rd
├── get_apsimx_json.Rd
├── read_apsim_soils.Rd
├── apsimx.options.Rd
├── view_apsim_xml.Rd
├── apsim_version.Rd
├── get_slga_soil.Rd
├── initialwater_parms.Rd
├── write_apsim_met.Rd
├── view_apsim.Rd
├── view_apsimx.Rd
├── extract_values_apsimx.Rd
├── read_apsim_met.Rd
├── read_apsim.Rd
├── apsim_options.Rd
├── read_apsim_all.Rd
├── apsimx_example.Rd
├── read_apsimx.Rd
├── available_water_content.Rd
├── check_apsimx.Rd
├── get_chirps_apsim_met.Rd
├── inspect_apsim_xml.Rd
├── get_worldmodeler_apsim_met.Rd
├── solutes_parms.Rd
├── get_worldmodeler_soil_profile.Rd
├── apsim_example.Rd
├── get_power_apsim_met.Rd
├── as_apsim_met.Rd
├── carbon_stocks.Rd
├── apsim.Rd
├── insert_replacement_node.Rd
├── get_ssurgo_tables.Rd
├── get_gsod_apsim_met.Rd
├── add_column_apsim_met.Rd
├── edit_apsimx_replace_soil_profile.Rd
├── get_iemre_apsim_met.Rd
├── soilwat_parms.Rd
├── get_iem_apsim_met.Rd
├── get_daymet_apsim_met.Rd
├── get_daymet2_apsim_met.Rd
├── apsimx_options.Rd
├── xargs_apsimx.Rd
├── apsimx.Rd
├── inspect_apsimx_json.Rd
├── swim_parms.Rd
├── unit_conv.Rd
├── plot.met.Rd
├── tt_apsim_met.Rd
├── edit_apsim_replace_soil_profile.Rd
├── sens_apsim.Rd
├── get_ssurgo_soil_profile.Rd
├── edit_apsim_xml.Rd
├── edit_apsimx_batch.Rd
├── summary.met.Rd
├── compare_apsim_soil_profile.Rd
├── compare_apsim_met.Rd
├── get_isric_soil_profile.Rd
└── extract_data_apsimx.Rd
├── tests
├── test_get_gsod_apsim_met.R
├── test_edit_apsimx_json.R
├── test_replacements.R
├── test_edit_apsim_xml.R
├── test_read_apsim.R
├── test_classic_examples.r
├── test_examples.R
├── test_inspect_apsimx_json.R
├── test_apsim_swim.R
├── test_edit_batch.R
├── test_allow_path_spaces.R
├── test_read_apsim_met.R
├── test_grep_json_list.R
├── test_vignette.R
├── test_apsim_soils.R
├── test_initialwater.R
├── test_inspect_apsim_factorial.R
├── test_get_isric_soil_profile.R
├── test_soil_properties.R
├── test_insert_replacement_node.R
├── test_worldmodeler.R
├── test_apsim_met_tt.R
├── test_extract_values_apsimx.R
├── test_compare_apsim.R
├── test_ssurgo2sp.R
├── test_check_apsimx.R
├── test_apsimx_factorial.R
├── test_edit_apsimx_replacement.R
├── test_get_daymet_apsim_met.R
└── test_AK_point_sim.R
├── doc
└── apsimx-scripts.R
├── R
├── data-doc.R
├── apsimx_filetype.R
├── get_chirps_apsim_met.R
├── zzz.R
├── get_power_apsim_met.R
└── apsim_internal.R
├── DESCRIPTION
└── NAMESPACE
/data/obsWheat.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/femiguez/apsimx/HEAD/data/obsWheat.rda
--------------------------------------------------------------------------------
/data/wheat-opt-ex.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/femiguez/apsimx/HEAD/data/wheat-opt-ex.rda
--------------------------------------------------------------------------------
/inst/extdata/ISUAG/SSURGO/ISUAG_SSURGO_Mapunits.dbf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/femiguez/apsimx/HEAD/inst/extdata/ISUAG/SSURGO/ISUAG_SSURGO_Mapunits.dbf
--------------------------------------------------------------------------------
/inst/extdata/ISUAG/SSURGO/ISUAG_SSURGO_Mapunits.shp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/femiguez/apsimx/HEAD/inst/extdata/ISUAG/SSURGO/ISUAG_SSURGO_Mapunits.shp
--------------------------------------------------------------------------------
/inst/extdata/ISUAG/SSURGO/ISUAG_SSURGO_Mapunits.shx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/femiguez/apsimx/HEAD/inst/extdata/ISUAG/SSURGO/ISUAG_SSURGO_Mapunits.shx
--------------------------------------------------------------------------------
/inst/extdata/ISUAG/SSURGO/ISUAG_SSURGO_Mapunits.prj:
--------------------------------------------------------------------------------
1 | GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
--------------------------------------------------------------------------------
/man/print.met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{print.met}
4 | \alias{print.met}
5 | \title{Printer-friendly version of a metfile}
6 | \usage{
7 | \method{print}{met}(x, ...)
8 | }
9 | \arguments{
10 | \item{x}{an R object of class \sQuote{met}}
11 |
12 | \item{...}{additional printing arguments}
13 | }
14 | \value{
15 | It prints to console. Not used to return an R object.
16 | }
17 | \description{
18 | Print a met file in a friendly way
19 | }
20 |
--------------------------------------------------------------------------------
/man/wop.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data-doc.R
3 | \docType{data}
4 | \name{wop}
5 | \alias{wop}
6 | \title{Wheat example optimization results}
7 | \format{
8 | An object of class 'optim_apsim'
9 | \describe{
10 | \item{ wop }{ wheat optimization results }
11 | }
12 | }
13 | \source{
14 | Result of running the examples in Parameter Optimization vignette
15 | }
16 | \usage{
17 | wop
18 | }
19 | \description{
20 | Results from Wheat optimization example
21 | }
22 | \keyword{datasets}
23 |
--------------------------------------------------------------------------------
/man/mcmc.apsim.env.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/optim_apsim.R
3 | \docType{data}
4 | \name{mcmc.apsim.env}
5 | \alias{mcmc.apsim.env}
6 | \title{Environment to store data for apsim MCMC}
7 | \format{
8 | An object of class \code{environment} of length 0.
9 | }
10 | \usage{
11 | mcmc.apsim.env
12 | }
13 | \value{
14 | This is an environment, so nothing to return.
15 | }
16 | \description{
17 | Environment which stores data for MCMC
18 | }
19 | \details{
20 | Create an apsim environment for MCMC
21 | }
22 | \keyword{datasets}
23 |
--------------------------------------------------------------------------------
/man/mcmc.apsimx.env.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/optim_apsimx.R
3 | \docType{data}
4 | \name{mcmc.apsimx.env}
5 | \alias{mcmc.apsimx.env}
6 | \title{Environment to store data for apsimx MCMC}
7 | \format{
8 | An object of class \code{environment} of length 0.
9 | }
10 | \usage{
11 | mcmc.apsimx.env
12 | }
13 | \value{
14 | This is an environment, so nothing to return.
15 | }
16 | \description{
17 | Environment which stores data for MCMC
18 | }
19 | \details{
20 | Create an apsimx environment for MCMC
21 | }
22 | \keyword{datasets}
23 |
--------------------------------------------------------------------------------
/man/wop.h.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data-doc.R
3 | \docType{data}
4 | \name{wop.h}
5 | \alias{wop.h}
6 | \title{Wheat example optimization results plus Hessian}
7 | \format{
8 | An object of class 'optim_apsim'
9 | \describe{
10 | \item{ wop.h }{ wheat optimization results plus Hessian}
11 | }
12 | }
13 | \source{
14 | Result of running the examples in Parameter Optimization vignette with the added Hessian
15 | }
16 | \usage{
17 | wop.h
18 | }
19 | \description{
20 | Results from Wheat optimization example plus the Hessian
21 | }
22 | \keyword{datasets}
23 |
--------------------------------------------------------------------------------
/inst/extdata/ISUAG/SSURGO/ISUAG_SSURGO_mapunit.csv:
--------------------------------------------------------------------------------
1 | musym,muname,mukind,mustatus,muacres,mapunitlfw.l,mapunitlfw.r,mapunitlfw.h,mapunitpfa.l,mapunitpfa.r,mapunitpfa.h,farmlndcl,muhelcl,muwathelcl,muwndhelcl,interpfocus,invesintens,iacornsr,nhiforsoigrp,nhspiagr,vtsepticsyscl,mucertstat,lkey,mukey
2 | L138B,"Clarion loam, Bemis moraine, 2 to 6 percent slopes",Consociation,NA,48216,NA,NA,NA,NA,NA,NA,All areas are prime farmland,NA,NA,NA,NA,Order 2,88,NA,NA,NA,NA,32529,2765522
3 | L507,"Canisteo clay loam, Bemis moraine, 0 to 2 percent slopes",Consociation,NA,56250,NA,NA,NA,NA,NA,NA,Prime farmland if drained,NA,NA,NA,NA,Order 2,87,NA,NA,NA,NA,32529,2922007
4 |
--------------------------------------------------------------------------------
/man/auto_detect_apsimx_examples.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx.R
3 | \name{auto_detect_apsimx_examples}
4 | \alias{auto_detect_apsimx_examples}
5 | \title{Auto detect where apsimx examples are located}
6 | \usage{
7 | auto_detect_apsimx_examples()
8 | }
9 | \value{
10 | will create a directory (character string) pointing to APSIM-X distributed examples
11 | }
12 | \description{
13 | simple function to detect where APSIM-X examples are located
14 | }
15 | \details{
16 | Auto detect where apsimx examples are located
17 | }
18 | \examples{
19 | \dontrun{
20 | ex.dir <- auto_detect_apsimx_examples()
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/man/amp_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{amp_apsim_met}
4 | \alias{amp_apsim_met}
5 | \title{Calculates attribute amp for an object of class \sQuote{met}}
6 | \usage{
7 | amp_apsim_met(met, by.year = TRUE)
8 | }
9 | \arguments{
10 | \item{met}{object of class \sQuote{met}}
11 |
12 | \item{by.year}{whether to perform calculations by year (default is TRUE)}
13 | }
14 | \value{
15 | an object of class \sQuote{met} with a recalculation of annual amplitude in mean monthly temperature
16 | }
17 | \description{
18 | This function can re-calculate annual mean monthly amplitude
19 | for an object of class \sQuote{met}
20 | }
21 |
--------------------------------------------------------------------------------
/man/auto_detect_apsim_examples.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_classic.R
3 | \name{auto_detect_apsim_examples}
4 | \alias{auto_detect_apsim_examples}
5 | \title{Auto detect where apsim examples are located}
6 | \usage{
7 | auto_detect_apsim_examples()
8 | }
9 | \value{
10 | will create a directory pointing to APSIM \sQuote{Classic} distributed examples
11 | }
12 | \description{
13 | simple function to detect where APSIM \sQuote{Classic} examples are located
14 | }
15 | \details{
16 | Auto detect where APSIM (7.x) \sQuote{Classic} examples are located
17 | }
18 | \examples{
19 | \dontrun{
20 | ex.dir <- auto_detect_apsim_examples()
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/man/napad_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{napad_apsim_met}
4 | \alias{napad_apsim_met}
5 | \title{Pad a met file with NAs when there are date discontinuities}
6 | \usage{
7 | napad_apsim_met(met)
8 | }
9 | \arguments{
10 | \item{met}{object of class \sQuote{met}}
11 | }
12 | \value{
13 | It returns an object of class \sQuote{met} with padded NAs.
14 | }
15 | \description{
16 | It will fill in or \sQuote{pad} a met object with NAs
17 | }
18 | \details{
19 | Fill in with missing data date discontinuities in a met file
20 | }
21 | \note{
22 | The purpose of this function is to allow for imputation using \code{\link{impute_apsim_met}}
23 | }
24 |
--------------------------------------------------------------------------------
/tests/test_get_gsod_apsim_met.R:
--------------------------------------------------------------------------------
1 | ## Testing GSOD in general and the radiation fill in feature
2 | ##
3 | require(apsimx)
4 |
5 | ## This is for UGANDA
6 | lonlat <- c(33, 1)
7 | dates <- c("2000-01-01", "2019-01-01")
8 |
9 | if(FALSE){
10 | ## Without filling in missing radiation data
11 | gsd <- get_gsod_apsim_met(lonlat = lonlat, dates = dates)
12 | ## Test fillin.radn feature
13 | gsd2 <- get_gsod_apsim_met(lonlat = lonlat, dates = dates, fillin.radn = TRUE)
14 |
15 | ## Test padding and imputation
16 | gsd.napad <- napad_apsim_met(gsd)
17 | gsd2.napad <- napad_apsim_met(gsd2)
18 |
19 | ## This shows that impute does not work on the edges
20 | gsd2.impt <- impute_apsim_met(gsd2.napad)
21 | }
22 |
23 |
24 |
--------------------------------------------------------------------------------
/man/tav_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{tav_apsim_met}
4 | \alias{tav_apsim_met}
5 | \title{Calculates attribute amp for an object of class \sQuote{met}}
6 | \usage{
7 | tav_apsim_met(met, by.year = TRUE, na.rm = TRUE)
8 | }
9 | \arguments{
10 | \item{met}{object of class \sQuote{met}}
11 |
12 | \item{by.year}{whether to compute tav for each year and then average (default is TRUE)}
13 |
14 | \item{na.rm}{whether to remove missing values (NAs). Default is TRUE.}
15 | }
16 | \value{
17 | an object of class \sQuote{met} with a recalculation of annual mean temperature amplitude
18 | }
19 | \description{
20 | This function can re-calculate annual mean temperature
21 | for an object of class \sQuote{met}
22 | }
23 |
--------------------------------------------------------------------------------
/man/grep_json_list.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/inspect_apsimx_json.R
3 | \name{grep_json_list}
4 | \alias{grep_json_list}
5 | \title{grep but for json list}
6 | \usage{
7 | grep_json_list(
8 | pattern,
9 | x,
10 | ignore.case = FALSE,
11 | search.depth = 10,
12 | how = c("unlist", "replace", "list")
13 | )
14 | }
15 | \arguments{
16 | \item{pattern}{as in grep}
17 |
18 | \item{x}{object (a list)}
19 |
20 | \item{ignore.case}{as in grep}
21 |
22 | \item{search.depth}{search depth for the list (to prevent endless search)}
23 |
24 | \item{how}{argument passed to \link{rapply}}
25 | }
26 | \value{
27 | It returns a list with the found object, the json path and the positions in the list.
28 | }
29 | \description{
30 | recursive grep adapted for a json list
31 | }
32 |
--------------------------------------------------------------------------------
/man/soilorganicmatter_parms.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/edit_apsim_replace_soil_profile.R
3 | \name{soilorganicmatter_parms}
4 | \alias{soilorganicmatter_parms}
5 | \title{Helper function to supply additional Soil Organic Matter parameters}
6 | \usage{
7 | soilorganicmatter_parms(
8 | RootCN = NA,
9 | RootWt = NA,
10 | EnrACoeff = NA,
11 | EnrBCoeff = NA,
12 | OCUnits = NA
13 | )
14 | }
15 | \arguments{
16 | \item{RootCN}{Root Carbon:Nitrogen ratio (see APSIM documentation)}
17 |
18 | \item{RootWt}{Root weight (see APSIM documentation)}
19 |
20 | \item{EnrACoeff}{(see APSIM documentation)}
21 |
22 | \item{EnrBCoeff}{(see APSIM documentation)}
23 |
24 | \item{OCUnits}{Organic Carbon Units}
25 | }
26 | \description{
27 | Creates a list with specific components for the Soil Organic Matter module
28 | }
29 |
--------------------------------------------------------------------------------
/man/read_apsimx_all.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx.R
3 | \name{read_apsimx_all}
4 | \alias{read_apsimx_all}
5 | \title{Read all APSIM-X generated .db files in a directory}
6 | \usage{
7 | read_apsimx_all(src.dir = ".", value = "report")
8 | }
9 | \arguments{
10 | \item{src.dir}{source directory where files are located}
11 |
12 | \item{value}{either \sQuote{report} or \sQuote{all} (only \sQuote{report} implemented at the moment)}
13 | }
14 | \value{
15 | it returns a data frame or a list if \sQuote{value} equals \sQuote{all}.
16 | }
17 | \description{
18 | Like \code{\link{read_apsimx}}, but it reads all .db files in a directory.
19 | }
20 | \details{
21 | Read all APSIM-X generated .db files in a directory
22 | }
23 | \note{
24 | Warning: very simple function at the moment, not optimized for memory or speed.
25 | }
26 |
--------------------------------------------------------------------------------
/man/obsWheat.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data-doc.R
3 | \docType{data}
4 | \name{obsWheat}
5 | \alias{obsWheat}
6 | \title{Observed wheat phenology, LAI and biomass}
7 | \format{
8 | A data frame with 10 rows and 4 variables:
9 | \describe{
10 | \item{Date}{ -date- date starting Oct 1 2016 and ending June 6 2017}
11 | \item{Wheat.Phenology.Stage}{ -numeric- phenology stage of wheat}
12 | \item{Wheat.Leaf.LAI}{ -numeric- Leaf Area Index}
13 | \item{Wheat.AboveGround.Wt}{-numeric- above ground biomass (g/m2)}
14 | }
15 | }
16 | \source{
17 | These are simulated data. For details see the APSIM documentation
18 | }
19 | \usage{
20 | obsWheat
21 | }
22 | \description{
23 | Artificial observed data for Wheat
24 | }
25 | \details{
26 | A dataset containing the Date, phenology stage, LAI and above ground biomass for Wheat
27 | }
28 | \keyword{datasets}
29 |
--------------------------------------------------------------------------------
/man/doy2date.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/unit_conv.R
3 | \name{doy2date}
4 | \alias{doy2date}
5 | \alias{date2doy}
6 | \title{Converts from doy to date}
7 | \usage{
8 | doy2date(x, year = 2001, inverse = FALSE)
9 |
10 | date2doy(x, year = 2001, inverse = FALSE)
11 | }
12 | \arguments{
13 | \item{x}{either an integer 1-366 or a \sQuote{Date}}
14 |
15 | \item{year}{year}
16 |
17 | \item{inverse}{if TRUE it goes from doy to \sQuote{Date}}
18 | }
19 | \value{
20 | an object of class \sQuote{Date} or a numeric if inverse equals TRUE.
21 |
22 | an numeric or an object of class \sQuote{Date} if inverse equals TRUE.
23 | }
24 | \description{
25 | Given a day of the year as julian (1-366) it converts to \sQuote{Date}
26 |
27 | Given a \sQuote{Date} it converts to julian day (1-366) or day of the year
28 | }
29 | \examples{
30 | doy2date(120)
31 | date2doy("04-30")
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/man/check_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{check_apsim_met}
4 | \alias{check_apsim_met}
5 | \title{Check a met file for possible errors}
6 | \usage{
7 | check_apsim_met(met)
8 | }
9 | \arguments{
10 | \item{met}{object of class \sQuote{met}}
11 | }
12 | \value{
13 | does not return anything unless possible errors are found
14 | }
15 | \description{
16 | Takes in an object of class \sQuote{met} and checks for missing/valid/reasonable values
17 | }
18 | \details{
19 | It will only check for missing values and reasonable (within range) values for:
20 | \sQuote{year}: range (1500 to 3000); \cr
21 | \sQuote{day}: range (1 to 366); \cr
22 | \sQuote{maxt}: range (-60 to 60) -- units (C); \cr
23 | \sQuote{mint}: range (-60 to 40) -- units (C); \cr
24 | \sQuote{radn}: range (0 to 40) -- units (MJ/m2/day); \cr
25 | \sQuote{rain}: range (0 to 100) -- units (mm/day)
26 | }
27 |
--------------------------------------------------------------------------------
/man/impute_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{impute_apsim_met}
4 | \alias{impute_apsim_met}
5 | \title{Perform imputation for missing data in a met file}
6 | \usage{
7 | impute_apsim_met(
8 | met,
9 | method = c("approx", "spline", "mean"),
10 | verbose = FALSE,
11 | ...
12 | )
13 | }
14 | \arguments{
15 | \item{met}{object of class \sQuote{met}}
16 |
17 | \item{method}{method for imputation, \sQuote{approx} (\code{\link[stats]{approxfun}}),
18 | \sQuote{spline} (\code{\link[stats]{splinefun}}) or \sQuote{mean} (\code{\link{mean}}).}
19 |
20 | \item{verbose}{whether to print missing data to the console, default = FALSE}
21 |
22 | \item{...}{additional arguments to be passed to imputation method}
23 | }
24 | \value{
25 | an object of class \sQuote{met} with attributes
26 | }
27 | \description{
28 | Takes in an object of class \sQuote{met} and imputes values
29 | }
30 |
--------------------------------------------------------------------------------
/man/apsim.options.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_classic.R
3 | \docType{data}
4 | \name{apsim.options}
5 | \alias{apsim.options}
6 | \title{Environment which stores APSIM options}
7 | \format{
8 | An object of class \code{environment} of length 3.
9 | }
10 | \usage{
11 | apsim.options
12 | }
13 | \value{
14 | This is an environment, so nothing to return.
15 | }
16 | \description{
17 | Environment which can store the path to the executable and where examples are located.
18 | Creating an environment avoids the use of global variables or other similar practices
19 | which would have possible undesriable consequences.
20 | }
21 | \details{
22 | Environment which stores APSIM options
23 | }
24 | \examples{
25 | \dontrun{
26 | names(apsim.options)
27 | apsim_options(exe.path = "some-new-path-to-executable")
28 | apsim.options$exe.path
29 | }
30 |
31 | }
32 | \keyword{datasets}
33 |
--------------------------------------------------------------------------------
/tests/test_edit_apsimx_json.R:
--------------------------------------------------------------------------------
1 | ## The purpose of this test file is to consider the inspecting
2 | ## and editing capabilities of:
3 | ##
4 | ## 1. inspect_apsimx_json
5 | ## 2. edit_apsimx (node == "Other")
6 | ## 3. edit_apsimx_json
7 | ##
8 | ## I need to consider both files with a .apsimx and .json extension
9 | require(apsimx)
10 | apsimx_options(warn.versions = FALSE)
11 |
12 | run.edit.apsimx.json <- get(".run.local.tests", envir = apsimx.options)
13 |
14 | if(run.edit.apsimx.json){
15 |
16 | tmp.dir <- tempdir()
17 | ## Testing with an .apsimx file first
18 | ex.dir <- auto_detect_apsimx_examples()
19 | pp <- inspect_apsimx_json("Barley.apsimx", src.dir = ex.dir, parm = "Version$")
20 | ## Try editing
21 | apsimx:::edit_apsimx_json("Barley.apsimx", src.dir = ex.dir, wrt.dir = tmp.dir,
22 | parm.path = pp[1], value = 200)
23 | inspect_apsimx_json("Barley-edited.apsimx", src.dir = tmp.dir, parm = "Version")
24 |
25 | }
--------------------------------------------------------------------------------
/man/apsimx_filetype.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx_filetype.R
3 | \name{apsimx_filetype}
4 | \alias{apsimx_filetype}
5 | \title{Test file format for .apsimx files}
6 | \usage{
7 | apsimx_filetype(file = "", src.dir = ".")
8 | }
9 | \arguments{
10 | \item{file}{file ending in .apsimx to be tested}
11 |
12 | \item{src.dir}{directory containing the .apsimx file to be tested; defaults to the current working directory}
13 | }
14 | \value{
15 | \sQuote{xml}, \sQuote{json} or \sQuote{unknown}
16 | }
17 | \description{
18 | Test whether an .apsimx file is XML or json
19 | }
20 | \note{
21 | Minimal function which reads only the first line in a file and tries to guess whether it is
22 | an \sQuote{xml} or \sQuote{json} file type.
23 | }
24 | \examples{
25 | \donttest{
26 | extd.dir <- system.file("extdata", package = "apsimx")
27 | apsimx_filetype("Wheat.apsimx", src.dir = extd.dir)
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/man/get_apsimx_json.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/wget_apsimx_json.R
3 | \name{get_apsimx_json}
4 | \alias{get_apsimx_json}
5 | \title{fetches the json file for a specific model from APSIMX github}
6 | \usage{
7 | get_apsimx_json(model = "Wheat", wrt.dir = ".", cleanup = FALSE)
8 | }
9 | \arguments{
10 | \item{model}{a model (e.g. \sQuote{Wheat} or \sQuote{Maize})}
11 |
12 | \item{wrt.dir}{directory to save the JSON file (default is the current directory)}
13 |
14 | \item{cleanup}{whether to delete the JSON file}
15 | }
16 | \value{
17 | a list read through the jsonlite package
18 | }
19 | \description{
20 | Retrieves the json replacement file for a specific model
21 | }
22 | \details{
23 | Get APSIM-X Model Replacement from github
24 | }
25 | \examples{
26 | \donttest{
27 | tmp.dir <- tempdir()
28 | wheat <- get_apsimx_json(model = "Wheat", wrt.dir = tmp.dir)
29 | }
30 |
31 | }
32 | \seealso{
33 | \code{\link{insert_replacement_node}}
34 | }
35 |
--------------------------------------------------------------------------------
/man/read_apsim_soils.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_soils.R
3 | \name{read_apsim_soils}
4 | \alias{read_apsim_soils}
5 | \title{Read in a soils (XML) file into a list of \sQuote{soil_profile} objects}
6 | \usage{
7 | read_apsim_soils(file, src.dir = ".", verbose = TRUE)
8 | }
9 | \arguments{
10 | \item{file}{name of the file (the extension sohuld be .soils)}
11 |
12 | \item{src.dir}{directory containing the .soils file (defaults to the current directory)}
13 |
14 | \item{verbose}{whether to print additional information about the progress of reading
15 | the individual soils in.}
16 | }
17 | \description{
18 | APSIM soils can be stored as XML files (soils) and reading them in
19 | converts them into a list of individual objects of class \sQuote{soil_profile}
20 | }
21 | \examples{
22 | \donttest{
23 | extd.dir <- system.file("extdata", package = "apsimx")
24 |
25 | sls <- read_apsim_soils("Clarion.soils", src.dir = extd.dir)
26 |
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/man/apsimx.options.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx.R
3 | \docType{data}
4 | \name{apsimx.options}
5 | \alias{apsimx.options}
6 | \title{Environment which stores APSIM-X options}
7 | \format{
8 | An object of class \code{environment} of length 9.
9 | }
10 | \usage{
11 | apsimx.options
12 | }
13 | \value{
14 | This is an environment, not a function, so nothing is returned.
15 | }
16 | \description{
17 | Environment which can store the path to the executable, warning settings and
18 | where examples are located.
19 | Creating an environment avoids the use of global variables or other similar practices
20 | which would have possible undesriable consequences.
21 | }
22 | \details{
23 | Environment which stores APSIM-X options
24 | }
25 | \examples{
26 | \donttest{
27 | names(apsimx.options)
28 | apsimx_options(exe.path = "some-new-path-to-executable")
29 | apsimx.options$exe.path
30 | }
31 |
32 | }
33 | \keyword{datasets}
34 |
--------------------------------------------------------------------------------
/tests/test_replacements.R:
--------------------------------------------------------------------------------
1 | ## Testing replacements examples with APSIM-X
2 | ## Run a few tests for the examples
3 | require(apsimx)
4 | apsimx_options(warn.versions = FALSE)
5 |
6 | run.replacements.tests <- get(".run.local.tests", envir = apsimx.options)
7 |
8 | extd.dir <- system.file("extdata", package = "apsimx")
9 |
10 | replacements <- c("MaizeSoybean.apsimx","WheatRye.apsimx","Soybean.apsimx")
11 |
12 | tmp.dir <- tempdir()
13 |
14 | setwd(tmp.dir)
15 |
16 | if(run.replacements.tests){
17 | start <- Sys.time()
18 | for(i in replacements){
19 | rep.tst <- apsimx(i, src.dir = extd.dir, cleanup = TRUE)
20 | cat("Ran: ", i, "\n")
21 | }
22 | end <- Sys.time()
23 | cat("Total time:", end - start, "\n")
24 | if(Sys.info()[["sysname"]] != "Windows"){
25 | cat("APSIM-X version:", apsim_version(which = "inuse", verbose = FALSE)[1],"\n")
26 | }else{
27 | aiu <- apsim_version(which = "inuse", verbose = FALSE)[2]
28 | cat("APSIM-X version:", as.character(aiu[1,1]),"\n")
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/tests/test_edit_apsim_xml.R:
--------------------------------------------------------------------------------
1 | ## Testing the 'edit_apsim_xml' function
2 | require(apsimx)
3 |
4 | ## I guess I can run this non-locally
5 | extd.dir <- system.file("extdata", package = "apsimx")
6 |
7 | tmp.dir <- tempdir()
8 |
9 | values <- paste(rep(1.7, 12), collapse = " ")
10 |
11 | edit_apsim_xml("Maize75.xml",
12 | src.dir = extd.dir,
13 | wrt.dir = tmp.dir,
14 | parm.path = ".//Model/rue",
15 | value = values)
16 |
17 | pp1 <- edit_apsim_xml("Maize75.xml",
18 | src.dir = extd.dir,
19 | wrt.dir = tmp.dir,
20 | parm.path = ".//Model/rue",
21 | value = values,
22 | verbose = FALSE)
23 |
24 | pp2 <- edit_apsim_xml("Maize75.xml",
25 | src.dir = extd.dir,
26 | wrt.dir = tmp.dir,
27 | parm.path = ".//rue",
28 | value = values,
29 | verbose = FALSE)
30 |
--------------------------------------------------------------------------------
/man/view_apsim_xml.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/inspect_apsim_xml.R
3 | \name{view_apsim_xml}
4 | \alias{view_apsim_xml}
5 | \title{View an APSIM Classic auxiliary (XML) file}
6 | \usage{
7 | view_apsim_xml(file, src.dir, viewer = c("json", "react"), ...)
8 | }
9 | \arguments{
10 | \item{file}{file ending in .xml to be viewed.}
11 |
12 | \item{src.dir}{directory containing the .xml file to be viewed; defaults to the current working directory}
13 |
14 | \item{viewer}{either \dQuote{json} or \dQuote{react}.}
15 |
16 | \item{...}{additional arguments passed to either \sQuote{jsonedit} or \sQuote{reactjson}.}
17 | }
18 | \value{
19 | It does not return an object but it produces a tree display of the APSIM file.
20 | }
21 | \description{
22 | view an auxilliary XML apsim file.
23 | }
24 | \details{
25 | view APSIM XML file
26 | }
27 | \examples{
28 |
29 | \donttest{
30 | extd.dir <- system.file("extdata", package = "apsimx")
31 | view_apsim_xml("Maize75.xml", src.dir = extd.dir)
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/man/apsim_version.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_version.R
3 | \name{apsim_version}
4 | \alias{apsim_version}
5 | \title{Display available APSIM \sQuote{Classic} and APSIM-X versions}
6 | \usage{
7 | apsim_version(
8 | which = c("all", "inuse"),
9 | version = c("number", "full"),
10 | verbose = TRUE
11 | )
12 | }
13 | \arguments{
14 | \item{which}{either \sQuote{all} or \sQuote{inuse}}
15 |
16 | \item{version}{either \sQuote{number} for a compact display or \sQuote{full}}
17 |
18 | \item{verbose}{whether to print the information to standard output}
19 | }
20 | \value{
21 | a data frame (all) or a vector (inuse) with APSIM-X and/or APSIM versions
22 | }
23 | \description{
24 | Display available APSIM \sQuote{Classic} and APSIM-X versions
25 | }
26 | \examples{
27 | \dontrun{
28 | ## Check which APSIM versions are available
29 | ava <- apsim_version(verbose = TRUE)
30 |
31 | ## How to get APSIM 'help'
32 | ## apc <- apsimx:::auto_detect_apsimx()
33 | ## system(paste(apc, "--help"))
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/man/get_slga_soil.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_slga_soil_profile.R
3 | \name{get_slga_soil}
4 | \alias{get_slga_soil}
5 | \title{Retrieve soil profile data from SLGA (Soils for Australia)}
6 | \usage{
7 | get_slga_soil(lonlat)
8 | }
9 | \arguments{
10 | \item{lonlat}{Longitude and latitude vector (e.g. c(151.8306, -27.4969))}
11 | }
12 | \value{
13 | a data.frame with elements: depth (midpoint in cm), depths (as character in cm), thickness (cm), clay, sand, silt, wv1500, wv0033, bdod, nitrogen, phh2o, cec, soc
14 | }
15 | \description{
16 | This function gets a soil profile for the Australia extent
17 | }
18 | \details{
19 | The data comes from https://esoil.io/TERNLandscapes/Public/Pages/SLGA/index.html
20 | }
21 | \examples{
22 | \dontrun{
23 | ## retrieve data from longitude and latitude 151.8305805675806 and -27.496873026858598
24 | ## Note: This can take a couple of minutes
25 | slga.soil <- get_slga_soil(lonlat = c(151.8306, -27.4969))
26 |
27 | }
28 | }
29 | \author{
30 | Chloe (Yunru Lai) and Fernando E. Miguez
31 | }
32 |
--------------------------------------------------------------------------------
/tests/test_read_apsim.R:
--------------------------------------------------------------------------------
1 | ## Testing read_apsim_all function
2 | require(apsimx)
3 |
4 | run.examples <- FALSE
5 |
6 | extd.dir <- system.file("extdata", package = "apsimx")
7 |
8 | maize.out <- read_apsim("Maize.out", src.dir = extd.dir)
9 |
10 | millet.out <- read_apsim("Millet.out", src.dir = extd.dir)
11 |
12 | ## Running all examples
13 | os <- .Platform$OS.type
14 |
15 | if(os == "windows" && run.examples){
16 |
17 | apsim_options(warn.versions = FALSE)
18 | ex.classic.dir <- apsimx:::auto_detect_apsim_examples()
19 | apsim.examples <- dir(ex.classic.dir, pattern = "apsim$")
20 |
21 | tmp.dir <- tempdir()
22 | setwd(tmp.dir)
23 |
24 | for(i in apsim.examples){
25 | if(grepl("\\s", i)) next
26 | file.copy(file.path(ex.classic.dir, i), to = file.path(tmp.dir, i))
27 | cat("Example:", i)
28 | sim <- try(apsim(file = i, simplify = FALSE), silent = TRUE)
29 | if(inherits(sim, "try-error")){
30 | cat(" Failed. \n")
31 | next
32 | }else{
33 | cat("; object class:", class(sim), " ")
34 | cat("; Done. \n")
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/man/initialwater_parms.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/edit_apsim_replace_soil_profile.R
3 | \name{initialwater_parms}
4 | \alias{initialwater_parms}
5 | \title{Helper function to supply additional Initial Soil Water parameters}
6 | \usage{
7 | initialwater_parms(
8 | Depth = NA,
9 | Thickness = NA,
10 | InitialValues = NA,
11 | InitialPAWmm = NA,
12 | PercentFull = NA,
13 | RelativeTo = NA,
14 | FilledFromTop = NA,
15 | DepthWetSoil = NA
16 | )
17 | }
18 | \arguments{
19 | \item{Depth}{depth for soil layers (see APSIM documentation)}
20 |
21 | \item{Thickness}{soil thickness for layers (either enter Depth or Thickness, but not both)}
22 |
23 | \item{InitialValues}{initial values of soil water}
24 |
25 | \item{InitialPAWmm}{Initial Plant Available Water in mm}
26 |
27 | \item{PercentFull}{Percent full (0 - 100)}
28 |
29 | \item{RelativeTo}{usually LL15}
30 |
31 | \item{FilledFromTop}{either true or false}
32 |
33 | \item{DepthWetSoil}{depth of wet soil in mm}
34 | }
35 | \description{
36 | Creates a list with specific components for the Initial Soil Water module
37 | }
38 |
--------------------------------------------------------------------------------
/doc/apsimx-scripts.R:
--------------------------------------------------------------------------------
1 | ## ----setup, include=FALSE-----------------------------------------------------
2 | knitr::opts_chunk$set(echo = TRUE)
3 | ##require(apsimx)
4 |
5 | ## ----apsimx-windows, eval = FALSE---------------------------------------------
6 | # run.strng <- "C:/PROGRA~1/APSIM2024.2.7382.0/bin/Models.exe ./Wheat.apsimx"
7 | # shell(cmd = run.strng, translate = TRUE, intern = TRUE)
8 |
9 | ## ----run-apsimx, eval = FALSE-------------------------------------------------
10 | # ## Mac
11 | # run.string.mac <- "mono /Applications/APSIM2021.01.14.6010.app/Contents/Resources/Bin/Models.exe ./Wheat.apsimx"
12 | # system(command = run.string.mac, intern = TRUE)
13 | # ## Debian
14 | # run.string.debian <- "mono /usr/local/lib/apsim/2019.10.04.4236/Bin/Models.exe ./Wheat.apsimx"
15 | # system(command = run.string.debian, intern = TRUE)
16 |
17 | ## ----sisd, eval = FALSE-------------------------------------------------------
18 | # sessionInfo()
19 | # Sys.info()[["sysname"]]
20 | # Sys.info()[["release"]]
21 | # Sys.info()[["version"]]
22 | # if(.Platform$OS.type == "unix") system(command = "mono --version", intern = TRUE)
23 | # R.version
24 |
25 |
--------------------------------------------------------------------------------
/man/write_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{write_apsim_met}
4 | \alias{write_apsim_met}
5 | \title{Write an APSIM met file}
6 | \usage{
7 | write_apsim_met(met, wrt.dir = NULL, filename = NULL)
8 | }
9 | \arguments{
10 | \item{met}{object of class \sQuote{met}}
11 |
12 | \item{wrt.dir}{directory where the file will be written}
13 |
14 | \item{filename}{optional alternative filename}
15 | }
16 | \value{
17 | does not create an R object, it only writes to disk
18 | }
19 | \description{
20 | Write an object of class \sQuote{met} to disk
21 | }
22 | \details{
23 | Write a met file to disk. It takes an object of class \sQuote{met}
24 |
25 | at the moment the read-write cycle will strip comments
26 | }
27 | \examples{
28 | \donttest{
29 | extd.dir <- system.file("extdata", package = "apsimx")
30 | ames.met <- read_apsim_met("Ames.met", src.dir = extd.dir)
31 | ames.met
32 | tmp.dir <- tempdir()
33 | write_apsim_met(ames.met, wrt.dir = tmp.dir, filename = "Ames.met")
34 | ## Here I write to a temporary directory, but change this to where
35 | ## you want to write to
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/man/view_apsim.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/view_apsimx.R
3 | \name{view_apsim}
4 | \alias{view_apsim}
5 | \title{Viewing an APSIM Classic file interactively}
6 | \usage{
7 | view_apsim(file, src.dir, viewer = c("json", "react"), ...)
8 | }
9 | \arguments{
10 | \item{file}{a file ending in .apsim to be inspected (XML)}
11 |
12 | \item{src.dir}{directory containing the .apsim file to be inspected; defaults to the current working directory}
13 |
14 | \item{viewer}{either \dQuote{json} or \dQuote{react}.}
15 |
16 | \item{...}{additional arguments passed to either \sQuote{jsonedit} or \sQuote{reactjson}.
17 | These are functions in package \CRANpkg{listviewer}.}
18 | }
19 | \value{
20 | a display with the APSIM file structure.
21 | }
22 | \description{
23 | Generate an interactive viewer for an APSIM file
24 | }
25 | \note{
26 | I do not know how to edit an APSIM file using this method yet.
27 | }
28 | \examples{
29 | \donttest{
30 | extd.dir <- system.file("extdata", package = "apsimx")
31 | ## View the structure of the APSIM-X simulation file
32 | view_apsim("Millet.apsim", src.dir = extd.dir)
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/man/view_apsimx.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/view_apsimx.R
3 | \name{view_apsimx}
4 | \alias{view_apsimx}
5 | \title{Viewing an APSIM-X file interactively}
6 | \usage{
7 | view_apsimx(file, src.dir, viewer = c("json", "react"), ...)
8 | }
9 | \arguments{
10 | \item{file}{a file ending in .apsimx to be inspected (JSON)}
11 |
12 | \item{src.dir}{directory containing the .apsimx file to be inspected; defaults to the current working directory}
13 |
14 | \item{viewer}{either \dQuote{json} or \dQuote{react}.}
15 |
16 | \item{...}{additional arguments passed to either \sQuote{jsonedit} or \sQuote{reactjson}.
17 | These are functions in package \CRANpkg{listviewer}.}
18 | }
19 | \value{
20 | a display with the APSIM file structure.
21 | }
22 | \description{
23 | Generate an interactive viewer for an APSIM-X file
24 | }
25 | \note{
26 | I do not know how to edit an APSIM-X file using this method yet.
27 | }
28 | \examples{
29 | \donttest{
30 | extd.dir <- system.file("extdata", package = "apsimx")
31 | ## View the structure of the APSIM-X simulation file
32 | view_apsimx("Wheat.apsimx", src.dir = extd.dir)
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/tests/test_classic_examples.r:
--------------------------------------------------------------------------------
1 | ## Write a test for running APSIM only under Windows
2 | require(apsimx)
3 | apsim_options(warn.versions = FALSE)
4 |
5 | ## This needs windows and APSIM Classic
6 | run.classic.examples <- grepl("windows", Sys.info()[["sysname"]], ignore.case = TRUE) && get(".run.local.tests", envir = apsimx.options)
7 |
8 | tmp.dir <- tempdir()
9 | setwd(tmp.dir)
10 |
11 | if(run.classic.examples){
12 |
13 | ade <- auto_detect_apsim_examples()
14 |
15 | ex <- list.files(path = ade, pattern = ".apsim$")
16 | ## Will only run a few
17 | ex.to.run <- c("Canopy","Centro","Millet","Potato","Sugar")
18 |
19 | for(i in ex.to.run){
20 | tmp <- apsim_example(i)
21 | cat("Ran (apsim_example):",i,"\n")
22 | }
23 |
24 | ## Test examples individually
25 | ## Note: this will not work unless tmp.dir is the current directory
26 | ## because of a bug in APSIM
27 | for(i in ex.to.run){
28 | file.copy(paste0(ade,"/",i,".apsim"), tmp.dir)
29 | tmp <- apsim(paste0(i,".apsim"), cleanup = TRUE)
30 | file.remove(paste0(tmp.dir,"/",i,".apsim"))
31 | cat("Ran (apsim):",i,"\n")
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/tests/test_examples.R:
--------------------------------------------------------------------------------
1 | ## Run a few tests for the examples
2 | require(apsimx)
3 | apsimx_options(warn.versions = FALSE)
4 |
5 | run.example.tests <- get(".run.local.tests", envir = apsimx.options)
6 |
7 | examples <- c("AgPasture", "Barley", "Canola", "Chickpea",
8 | "ControlledEnvironment", "Eucalyptus",
9 | "EucalyptusRotation",
10 | "Maize", "Mungbean",
11 | "Oats", "OilPalm", "Peanut", "Pinus", "Potato",
12 | "RedClover",
13 | "Rotation", "Slurp", "Sorghum",
14 | "Soybean", "Sugarcane", "Wheat", "WhiteClover")
15 |
16 | tmp.dir <- tempdir()
17 |
18 | setwd(tmp.dir)
19 |
20 | if(run.example.tests){
21 |
22 | ex.dir <- auto_detect_apsimx_examples()
23 |
24 | start <- Sys.time()
25 | for(i in examples){
26 | ex.tst <- apsimx_example(i)
27 | cat("Ran: ", i, "\n")
28 | }
29 | end <- Sys.time()
30 | cat("Total time:", end - start, "\n")
31 |
32 | if(.Platform$OS.type == "unix"){
33 | cat("APSIM-X version:", apsim_version(which = "inuse"),"\n")
34 | }else{
35 | cat("APSIM-X version:", as.vector(apsim_version(which = "inuse")[[2]]),"\n")
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/man/extract_values_apsimx.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/extract_data_apsimx.R
3 | \name{extract_values_apsimx}
4 | \alias{extract_values_apsimx}
5 | \title{Extract values from a parameter path}
6 | \usage{
7 | extract_values_apsimx(file, src.dir, parm.path)
8 | }
9 | \arguments{
10 | \item{file}{file name to be run (the extension .apsimx is optional)}
11 |
12 | \item{src.dir}{directory containing the .apsimx file to be run (defaults to the current directory)}
13 |
14 | \item{parm.path}{parameter path either use inspect_apsimx or see example below}
15 | }
16 | \value{
17 | a vector with extracted parameter values from an APSIM file.
18 | }
19 | \description{
20 | Extract initial values from a parameter path
21 | }
22 | \examples{
23 | \donttest{
24 | ## Find examples
25 | extd.dir <- system.file("extdata", package = "apsimx")
26 | ## Extract parameter path
27 | pp <- inspect_apsimx("Maize.apsimx", src.dir = extd.dir,
28 | node = "Manager", parm = list("Fert", 1))
29 | ppa <- paste0(pp, ".Amount")
30 | ## Extract value
31 | extract_values_apsimx("Maize.apsimx", src.dir = extd.dir, parm.path = ppa)
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/tests/test_inspect_apsimx_json.R:
--------------------------------------------------------------------------------
1 | ### Testing the spanking new inspect_apsimx_json
2 | require(apsimx)
3 |
4 | run.inspect.apsimx.json <- FALSE
5 |
6 | if(run.inspect.apsimx.json){
7 |
8 | extd.dir <- system.file("extdata", package = "apsimx")
9 |
10 | inspect_apsimx_json("Maize.apsimx", src.dir = extd.dir, parm = "^Version")
11 |
12 | inspect_apsimx_json("Maize.apsimx", src.dir = extd.dir, parm = "Version")
13 |
14 | inspect_apsimx_json("Maize.apsimx", src.dir = extd.dir, parm = "ApsimVersion")
15 |
16 | inspect_apsimx_json("Maize.apsimx", src.dir = extd.dir, parm = "Clock")
17 |
18 | inspect_apsimx_json("Maize.apsimx", src.dir = extd.dir, parm = "Soil")
19 |
20 | inspect_apsimx_json("Maize.apsimx", src.dir = extd.dir, parm = "Soil")
21 |
22 | inspect_apsimx_json("Maize.apsimx", src.dir = extd.dir, parm = "Dekalb")
23 |
24 | inspect_apsimx_json("Maize.apsimx", src.dir = extd.dir, parm = "10-jan")
25 |
26 | ## These parameters do not return a single path
27 | ## Ideally, I would be able to either return multiple paths or use
28 | ## a jsonpath for the search instead of a single keyword
29 | inspect_apsimx_json("Maize.apsimx", src.dir = extd.dir, parm = "Water")
30 | inspect_apsimx_json("Maize.apsimx", src.dir = extd.dir, parm = "Cultivar")
31 | }
--------------------------------------------------------------------------------
/man/read_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{read_apsim_met}
4 | \alias{read_apsim_met}
5 | \title{Read in an APSIM met file}
6 | \usage{
7 | read_apsim_met(file, src.dir = ".", verbose = TRUE)
8 | }
9 | \arguments{
10 | \item{file}{path to met file}
11 |
12 | \item{src.dir}{optional source directory}
13 |
14 | \item{verbose}{whether to suppress all messages and warnings}
15 | }
16 | \value{
17 | an object of class \sQuote{met} with attributes
18 | }
19 | \description{
20 | Read into R a met file and return an object of class \sQuote{met}
21 | }
22 | \details{
23 | Read a met file into R
24 |
25 | This function uses S3 classes and stores the additional information as attributes \cr
26 | I use a more strict format than \acronym{APSIM} and reading and writing will not \cr
27 | preserve all the details. For example, at this moment comments are lost through \cr
28 | the process of read and write unless they are added back in manually. \cr
29 | Also, empty lines are ignored so these will be lost as well in the read and write process.
30 | }
31 | \examples{
32 | \donttest{
33 | extd.dir <- system.file("extdata", package = "apsimx")
34 | ames.met <- read_apsim_met("Ames.met", src.dir = extd.dir)
35 | ames.met
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/man/read_apsim.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_classic.R
3 | \name{read_apsim}
4 | \alias{read_apsim}
5 | \title{Read APSIM generated .out files}
6 | \usage{
7 | read_apsim(
8 | file = "",
9 | src.dir = ".",
10 | value = c("report", "all"),
11 | date.format = "\%d/\%m/\%Y",
12 | silent = FALSE
13 | )
14 | }
15 | \arguments{
16 | \item{file}{file name}
17 |
18 | \item{src.dir}{source directory where file is located}
19 |
20 | \item{value}{either \sQuote{report} (data.frame), \sQuote{user-defined} or \sQuote{all} (list)}
21 |
22 | \item{date.format}{format for adding \sQuote{Date} column}
23 |
24 | \item{silent}{whether to issue warnings or suppress them}
25 | }
26 | \value{
27 | This function returns a data frame with APSIM output or a list if value equals \sQuote{all}
28 | }
29 | \description{
30 | read \sQuote{output} databases created by APSIM runs (.out and .sim). One file at a time.
31 | }
32 | \details{
33 | Read APSIM generated .out files
34 | }
35 | \examples{
36 | \dontrun{
37 | extd.dir <- system.file("extdata", package = "apsimx")
38 | maize.out <- read_apsim("Maize", src.dir = extd.dir, value = "report")
39 | millet.out <- read_apsim("Millet", src.dir = extd.dir, value = "report")
40 | }
41 |
42 | }
43 | \seealso{
44 | \code{\link{read_apsim_all}}
45 | }
46 |
--------------------------------------------------------------------------------
/man/apsim_options.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_classic.R
3 | \name{apsim_options}
4 | \alias{apsim_options}
5 | \title{Setting some options specific to APSIM (7.x) \sQuote{Classic}}
6 | \usage{
7 | apsim_options(exe.path = NA, examples.path = NA, warn.versions = TRUE)
8 | }
9 | \arguments{
10 | \item{exe.path}{path to apsim executable}
11 |
12 | \item{examples.path}{path to apsim examples}
13 |
14 | \item{warn.versions}{logical. warning if multiple versions of APSIM are detected.}
15 | }
16 | \value{
17 | It modifies the \sQuote{apsim.options} environment as a side effect.
18 | }
19 | \description{
20 | Set the path to the APSIM executable, examples and warning suppression.
21 | }
22 | \details{
23 | Set apsim options
24 | }
25 | \note{
26 | It is possible that APSIM 7.x \sQuote{Classic} is installed in some alternative location other than the
27 | defaults ones. Guessing this can be difficult and then the auto_detect functions might
28 | fail. Also, if multiple versions of APSIM are installed apsim will choose the newest
29 | one but it will issue a warning. Suppress the warning by setting warn.versions = FLASE.
30 | }
31 | \examples{
32 | \dontrun{
33 | names(apsim.options)
34 | apsim_options(exe.path = "some-new-path-to-executable")
35 | apsim.options$exe.path
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/man/read_apsim_all.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_classic.R
3 | \name{read_apsim_all}
4 | \alias{read_apsim_all}
5 | \title{Read all APSIM generated .out files in a directory}
6 | \usage{
7 | read_apsim_all(
8 | filenames,
9 | src.dir = ".",
10 | value = "report",
11 | date.format = "\%d/\%m/\%Y",
12 | simplify = TRUE,
13 | silent = FALSE
14 | )
15 | }
16 | \arguments{
17 | \item{filenames}{names of files to be read}
18 |
19 | \item{src.dir}{source directory where files are located}
20 |
21 | \item{value}{either \sQuote{report}, \sQuote{user-defined} or \sQuote{all} (not implemented at the moment)}
22 |
23 | \item{date.format}{format for adding \sQuote{Date} column}
24 |
25 | \item{simplify}{whether to return a single data frame or a list.}
26 |
27 | \item{silent}{whether to issue warnings or suppress them}
28 | }
29 | \value{
30 | returns a data frame or a list depending on the argument \sQuote{simplify} above.
31 | }
32 | \description{
33 | Like \code{\link{read_apsim}}, but it can read many .out files in a directory.
34 | It will read all of them unless these are filtered using a regular expression as an argument
35 | to \sQuote{value}.
36 | }
37 | \details{
38 | Read all APSIM generated .out files in a directory
39 | }
40 | \note{
41 | Warning: very simple function at the moment, not optimized for memory or speed.
42 | }
43 |
--------------------------------------------------------------------------------
/R/data-doc.R:
--------------------------------------------------------------------------------
1 | #' Artificial observed data for Wheat
2 | #'
3 | #' A dataset containing the Date, phenology stage, LAI and above ground biomass for Wheat
4 | #'
5 | #' @title Observed wheat phenology, LAI and biomass
6 | #' @format A data frame with 10 rows and 4 variables:
7 | #' \describe{
8 | #' \item{Date}{ -date- date starting Oct 1 2016 and ending June 6 2017}
9 | #' \item{Wheat.Phenology.Stage}{ -numeric- phenology stage of wheat}
10 | #' \item{Wheat.Leaf.LAI}{ -numeric- Leaf Area Index}
11 | #' \item{Wheat.AboveGround.Wt}{-numeric- above ground biomass (g/m2)}
12 | #' }
13 | #' @source These are simulated data. For details see the APSIM documentation
14 | "obsWheat"
15 |
16 | #' Results from Wheat optimization example
17 | #'
18 | #' @title Wheat example optimization results
19 | #' @format An object of class 'optim_apsim'
20 | #' \describe{
21 | #' \item{ wop }{ wheat optimization results }
22 | #' }
23 | #' @source Result of running the examples in Parameter Optimization vignette
24 | "wop"
25 |
26 | #' Results from Wheat optimization example plus the Hessian
27 | #'
28 | #' @title Wheat example optimization results plus Hessian
29 | #' @format An object of class 'optim_apsim'
30 | #' \describe{
31 | #' \item{ wop.h }{ wheat optimization results plus Hessian}
32 | #' }
33 | #' @source Result of running the examples in Parameter Optimization vignette with the added Hessian
34 | "wop.h"
35 |
--------------------------------------------------------------------------------
/tests/test_apsim_swim.R:
--------------------------------------------------------------------------------
1 | ## Test APSIM SWIM code for 'Classic'
2 | require(apsimx)
3 |
4 | ## This test does not need APSIM Classic to run
5 | run.test.apsim.swim <- TRUE
6 |
7 | ## Only write to a temp directory
8 | tmp.dir <- tempdir()
9 |
10 | extd.dir <- system.file("extdata", package = "apsimx")
11 |
12 | if(run.test.apsim.swim){
13 |
14 | inspect_apsim("maize-swim.apsim", src.dir = extd.dir)
15 | inspect_apsim("maize-swim.apsim", src.dir = extd.dir, node = "Weather")
16 | inspect_apsim("maize-swim.apsim", src.dir = extd.dir,
17 | node = "Soil", soil.child = "Water")
18 | inspect_apsim("maize-swim.apsim", src.dir = extd.dir,
19 | node = "Soil", soil.child = "SWIM")
20 | edit_apsim("maize-swim.apsim",
21 | src.dir = extd.dir, wrt.dir = tmp.dir,
22 | node = "Soil", soil.child = "SWIM",
23 | parm = "Salb", value = 0.10)
24 | inspect_apsim("maize-swim-edited.apsim", src.dir = tmp.dir,
25 | node = "Soil", soil.child = "SWIM")
26 | edit_apsim("maize-swim-edited.apsim",
27 | src.dir = tmp.dir, wrt.dir = tmp.dir,
28 | overwrite = TRUE,
29 | node = "Soil", soil.child = "SWIM",
30 | parm = "WaterTableDepth", value = 1500)
31 | inspect_apsim("maize-swim-edited.apsim", src.dir = tmp.dir,
32 | node = "Soil", soil.child = "SWIM")
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/man/apsimx_example.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx.R
3 | \name{apsimx_example}
4 | \alias{apsimx_example}
5 | \title{Access Example APSIM-X Simulations}
6 | \usage{
7 | apsimx_example(example = "Wheat", silent = FALSE)
8 | }
9 | \arguments{
10 | \item{example}{run an example from built-in APSIM-X. Options are all of the ones included with the APSIM-X distribution, except \sQuote{Graph}.}
11 |
12 | \item{silent}{whether to print standard output from the APSIM-X execution}
13 | }
14 | \value{
15 | It returns a data frame
16 | }
17 | \description{
18 | simple function to run some of the built-in APSIM-X examples
19 | }
20 | \details{
21 | This function creates a temporary copy of the example file distributed with APSIM-X to avoid writing a .db file
22 | to the directory where the \sQuote{Examples} are located. It is not a good practice and there is no guarantee that
23 | the user has read/write permissions in that directory.
24 | }
25 | \note{
26 | This function creates a new column \sQuote{Date} which is in the R \sQuote{Date} format which is convenient for graphics.
27 | }
28 | \examples{
29 | \dontrun{
30 | wheat <- apsimx_example("Wheat")
31 | maize <- apsimx_example("Maize")
32 | barley <- apsimx_example("Barley")
33 | ## The 'Date' column is created by this function, based on apsim output.
34 | require(ggplot2)
35 | ggplot(data = wheat , aes(x = Date, y = Yield)) +
36 | geom_point()
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/tests/test_edit_batch.R:
--------------------------------------------------------------------------------
1 | ## This is my way of doing this now
2 | require(apsimx)
3 | apsimx_options(warn.versions = FALSE)
4 |
5 | extd.dir <- system.file("extdata", package = "apsimx")
6 |
7 | run.test.edit.apsimx.batch <- get(".run.local.tests", envir = apsimx.options)
8 |
9 | tmp.dir <- tempdir()
10 |
11 | if(run.test.edit.apsimx.batch){
12 |
13 | file.copy(paste0(extd.dir, "/", "Wheat.apsimx"), tmp.dir)
14 |
15 | inspect_apsimx("Wheat.apsimx", src.dir = tmp.dir, node = "SurfaceOrganicMatter")
16 |
17 | edit1time <- system.time(edit_apsimx("Wheat.apsimx", src.dir = tmp.dir,
18 | wrt.dir = tmp.dir,
19 | node = "SurfaceOrganicMatter",
20 | parm = "InitialResidueMass", value = 600))
21 |
22 | inspect_apsimx("Wheat-edited.apsimx", src.dir = tmp.dir, node = "SurfaceOrganicMatter")
23 |
24 | ## Using the 'batch' method
25 | ## Let's say I want to edit 'SurfaceOrganicMatter'
26 | ## InitialResidueMass from 500 to 650
27 | ## Understanding the structure of JSON files
28 | parms <- list(".Simulations.Simulation.Field.SurfaceOrganicMatter.InitialResidueMass" = 650)
29 | edit2time <- system.time(edit_apsimx_batch("Wheat.apsimx", src.dir = tmp.dir, parms = parms))
30 | inspect_apsimx("Wheat.apsimx", src.dir = tmp.dir, node = "SurfaceOrganicMatter")
31 |
32 | ## What is the difference in 'elapsed' real time?
33 | edit2time[[3]]/edit1time[[3]]
34 | }
35 |
--------------------------------------------------------------------------------
/man/read_apsimx.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx.R
3 | \name{read_apsimx}
4 | \alias{read_apsimx}
5 | \title{Read APSIM-X generated .db files}
6 | \usage{
7 | read_apsimx(file = "", src.dir = ".", value = "report", simplify = TRUE)
8 | }
9 | \arguments{
10 | \item{file}{file name}
11 |
12 | \item{src.dir}{source directory where file is located}
13 |
14 | \item{value}{either \sQuote{report}, \sQuote{all} (list) or user-defined for a specific report}
15 |
16 | \item{simplify}{if TRUE will attempt to simplify multiple reports into a single data.frame.
17 | If FALSE it will return a list.}
18 | }
19 | \value{
20 | normally it returns a data frame, but it depends on the argument \sQuote{value} above
21 | }
22 | \description{
23 | read SQLite databases created by APSIM-X runs. One file at a time.
24 | }
25 | \details{
26 | Read APSIM-X generated .db files
27 | }
28 | \note{
29 | if there is one single report it will return a data.frame.
30 | If there are multiple reports, it will attempt to merge them into a data frame.
31 | If not possible it will return a list with names corresponding to the
32 | table report names. It is also possible to select a specific report from several
33 | available by selecting \sQuote{value = ReportName}, where \sQuote{ReportName} is the name
34 | of the specific report that should be returned.
35 | If you select \sQuote{all} it will return all the components in the data base also as a list.
36 | }
37 | \seealso{
38 | \code{\link{read_apsimx_all}}
39 | }
40 |
--------------------------------------------------------------------------------
/tests/test_allow_path_spaces.R:
--------------------------------------------------------------------------------
1 |
2 | library(apsimx)
3 | packageVersion("apsimx")
4 | apsimx_options(warn.versions = FALSE, allow.path.spaces = TRUE, warn.find.apsimx = FALSE)
5 |
6 | run.apsimx.path.spaces <- get(".run.local.tests", envir = apsimx.options)
7 |
8 | if(run.apsimx.path.spaces){
9 |
10 | #### Allow all examples to be run with spaces
11 | tmp.dir <- tempdir()
12 | ex.dir <- auto_detect_apsimx_examples()
13 | dir.create(file.path(tmp.dir, "examples from apsim"))
14 |
15 | examples <- c("AgPasture", "Barley", "Canola", "Chickpea",
16 | "ControlledEnvironment", "Eucalyptus",
17 | "EucalyptusRotation",
18 | "Maize", "Mungbean",
19 | "Oats", "OilPalm", "Peanut", "Pinus", "Potato",
20 | "RedClover",
21 | "Rotation", "Sorghum",
22 | "Soybean", "Sugarcane", "Wheat", "WhiteClover")
23 |
24 | for(i in examples){
25 | if(!file.exists(file.path(tmp.dir, "examples from apsim", paste0(i, ".apsimx"))))
26 | file.copy(from = file.path(ex.dir, paste0(i, ".apsimx")),
27 | to = file.path(tmp.dir, "examples from apsim", paste0(i, ".apsimx")))
28 | }
29 | ## dir(include.dirs = TRUE, recursive = TRUE)
30 | for(i in examples){
31 | file.to.run <- paste0(i, ".apsimx")
32 | src.dir <- file.path(tmp.dir, "examples from apsim")
33 | cat("Running:", file.to.run)
34 | atmp <- apsimx(file = file.to.run, src.dir = src.dir, simplify = FALSE)
35 | cat("... Done ... \n")
36 | }
37 | }
--------------------------------------------------------------------------------
/man/available_water_content.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx_soil_profile.R
3 | \name{available_water_content}
4 | \alias{available_water_content}
5 | \title{Calculate available water content}
6 | \usage{
7 | available_water_content(
8 | x,
9 | depth,
10 | area = c("m", "m2", "ha"),
11 | method = c("linear", "constant"),
12 | weights,
13 | ...
14 | )
15 | }
16 | \arguments{
17 | \item{x}{object of class \sQuote{soil_profile}}
18 |
19 | \item{depth}{soil depth (in meters). If missing then the whole soil profile is used.}
20 |
21 | \item{area}{either \sQuote{m} meter, \sQuote{m2} meter squared or \sQuote{ha}.}
22 |
23 | \item{method}{interpolation method. Either \sQuote{linear} or \sQuote{constant}.}
24 |
25 | \item{weights}{optional weights}
26 |
27 | \item{...}{additional arguments passed to internal functions (none used at the moment).}
28 | }
29 | \value{
30 | returns a value with attribute \sQuote{units} and \sQuote{depth}
31 | }
32 | \description{
33 | Calculation of available water content based on an object of class \sQuote{soil_profile}
34 | }
35 | \details{
36 | Function to calculate available water content. The output units depend on the choice of area.
37 | If \sQuote{m} is used, then the output units will be \sQuote{mm}. If the \sQuote{area} is \sQuote{m2},
38 | then the output units will be in \sQuote{m3}. If the \sQuote{area} is \sQuote{ha}, then the output units will be \sQuote{kg/ha}.
39 | }
40 | \examples{
41 | \dontrun{
42 | sp <- apsimx_soil_profile()
43 | available_water_content(sp)
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/man/check_apsimx.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx.R
3 | \name{check_apsimx}
4 | \alias{check_apsimx}
5 | \title{Partial checking of an apsimx file for possible issues.}
6 | \usage{
7 | check_apsimx(
8 | file = "",
9 | src.dir = ".",
10 | node = c("all", "Clock", "Weather", "Soil"),
11 | soil.child = c("all", "Physical", "InitialWater", "SoilWater", "Solute", "Organic"),
12 | check.apsim.met = FALSE,
13 | root = NULL,
14 | verbose = TRUE
15 | )
16 | }
17 | \arguments{
18 | \item{file}{file ending in .apsimx to be edited (JSON)}
19 |
20 | \item{src.dir}{directory containing the .apsimx file to be checked; defaults to the current working directory}
21 |
22 | \item{node}{either \sQuote{all}, \sQuote{Clock}, \sQuote{Weather}, \sQuote{Soil}}
23 |
24 | \item{soil.child}{specific soil component to be checked.}
25 |
26 | \item{check.apsim.met}{whether to check the \sQuote{met} file. Default is FALSE.}
27 |
28 | \item{root}{supply the node position in the case of multiple simulations such as factorials.}
29 |
30 | \item{verbose}{whether to print information}
31 | }
32 | \value{
33 | It does not return an object, but it prints messages useful for diagnosing issues.
34 | }
35 | \description{
36 | Partial checking of an apsimx file for possible issues.
37 | }
38 | \examples{
39 | \donttest{
40 |
41 | ## Check file distributed with the package
42 | extd.dir <- system.file("extdata", package = "apsimx")
43 |
44 | check_apsimx("Wheat.apsimx", src.dir = extd.dir)
45 | ## This throws warnings but it should not produce errors
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/man/get_chirps_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_chirps_apsim_met.R
3 | \name{get_chirps_apsim_met}
4 | \alias{get_chirps_apsim_met}
5 | \title{Get CHIRPS data for an APSIM met file}
6 | \usage{
7 | get_chirps_apsim_met(
8 | lonlat,
9 | dates,
10 | wrt.dir = ".",
11 | filename = NULL,
12 | fillin.radn = TRUE,
13 | silent = FALSE
14 | )
15 | }
16 | \arguments{
17 | \item{lonlat}{Longitude and latitude vector}
18 |
19 | \item{dates}{date ranges}
20 |
21 | \item{wrt.dir}{write directory}
22 |
23 | \item{filename}{file name for writing out to disk}
24 |
25 | \item{fillin.radn}{whether to fill in radiation data using the nasapower pacakge. Default is TRUE.}
26 |
27 | \item{silent}{defaut is FALSE. Changing it will not do anything at the moment. A future feature.}
28 | }
29 | \value{
30 | returns an object of class \sQuote{met} and writes a file to disk when filename is supplied.
31 | }
32 | \description{
33 | Uses \code{\link[chirps]{get_chirps}} from the \CRANpkg{chirps} package to download data to create an APSIM met file.
34 | }
35 | \details{
36 | This function requires the \CRANpkg{chirps} package, version 0.1.4.
37 |
38 | If the filename is not provided it will not write the file to disk,
39 | but it will return an object of class \sQuote{met}. This is useful in case manipulation
40 | is required before writing to disk.
41 | }
42 | \examples{
43 | \dontrun{
44 | require(chirps)
45 | ## This will not write a file to disk
46 | chrp <- get_chirps_apsim_met(lonlat = c(-93,42), dates = c("2012-01-01","2012-12-31"))
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/DESCRIPTION:
--------------------------------------------------------------------------------
1 | Package: apsimx
2 | Title: Inspect, Read, Edit and Run 'APSIM' "Next Generation" and 'APSIM' Classic
3 | Version: 2.8.254
4 | Authors@R: person("Fernando", "Miguez", email = "femiguez@iastate.edu", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-4627-8329"))
5 | Description: The functions in this package inspect, read, edit and run files for 'APSIM' "Next Generation" ('JSON')
6 | and 'APSIM' "Classic" ('XML'). The files with an 'apsim' extension correspond to
7 | 'APSIM' Classic (7.x) - Windows only - and the ones with an 'apsimx' extension correspond to 'APSIM' "Next Generation".
8 | For more information about 'APSIM' see () and for 'APSIM'
9 | next generation ().
10 | Depends: R (>= 4.0.0)
11 | License: GPL-3
12 | Encoding: UTF-8
13 | VignetteBuilder: knitr
14 | BugReports: https://github.com/femiguez/apsimx/issues
15 | Imports:
16 | DBI,
17 | jsonlite,
18 | knitr,
19 | RSQLite,
20 | tools,
21 | utils,
22 | xml2
23 | Suggests:
24 | BayesianTools,
25 | chirps,
26 | datasets,
27 | daymetr,
28 | future,
29 | ggplot2,
30 | GSODR,
31 | listviewer,
32 | maps,
33 | metrica,
34 | mvtnorm,
35 | nasapower,
36 | nloptr,
37 | parallelly,
38 | reactR,
39 | rmarkdown,
40 | sensitivity,
41 | soilDB,
42 | sp,
43 | spData,
44 | sf,
45 | ucminf
46 | LazyData: true
47 | RoxygenNote: 7.3.2
48 |
--------------------------------------------------------------------------------
/man/inspect_apsim_xml.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/inspect_apsim_xml.R
3 | \name{inspect_apsim_xml}
4 | \alias{inspect_apsim_xml}
5 | \title{Inspect an APSIM Classic auxiliary (XML) file}
6 | \usage{
7 | inspect_apsim_xml(
8 | file = "",
9 | src.dir = ".",
10 | parm,
11 | verbose = TRUE,
12 | print.path = TRUE
13 | )
14 | }
15 | \arguments{
16 | \item{file}{file ending in .xml to be inspected.}
17 |
18 | \item{src.dir}{directory containing the .xml file to be inspected; defaults to the current working directory}
19 |
20 | \item{parm}{parameter to inspect.}
21 |
22 | \item{verbose}{Whether to print to standard output}
23 |
24 | \item{print.path}{Whether to print the parameter path}
25 | }
26 | \value{
27 | it returns an absolute parameter path(s)
28 | }
29 | \description{
30 | inspect an auxiliary XML apsim file.
31 | }
32 | \note{
33 | the behavior has changed from previous verions (earlier than 1.977). Before,
34 | if more than match was found it would return an error. Now it returns a list with all
35 | possible matches. This can be useful when trying to find a parameter.
36 | }
37 | \examples{
38 |
39 | \donttest{
40 | extd.dir <- system.file("extdata", package = "apsimx")
41 |
42 | inspect_apsim_xml("Maize75.xml", src.dir = extd.dir,
43 | parm = "leaf_no_rate_change")
44 |
45 | pp <- inspect_apsim_xml("Maize75.xml", src.dir = extd.dir,
46 | parm = "leaf_no_rate_change",
47 | verbose = FALSE,
48 | print.path = FALSE)
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/man/get_worldmodeler_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_worldmodeler_soil_profile.R
3 | \name{get_worldmodeler_apsim_met}
4 | \alias{get_worldmodeler_apsim_met}
5 | \title{Obtain a weather APSIM met from the World Modeler database}
6 | \usage{
7 | get_worldmodeler_apsim_met(
8 | lonlat,
9 | dates,
10 | wrt.dir,
11 | filenames,
12 | check = FALSE,
13 | verbose = FALSE
14 | )
15 | }
16 | \arguments{
17 | \item{lonlat}{Longitude and latitude vector (e.g. c(-93, 42)) or matrix.}
18 |
19 | \item{dates}{date range (see example for format)}
20 |
21 | \item{wrt.dir}{optional directory where to save a file with \sQuote{met} extension.
22 | If missing it will be written to a temporary directory.}
23 |
24 | \item{filenames}{optional name(s) to be used when saving the file. It should be equal to the
25 | number of rows of the input matrix.}
26 |
27 | \item{check}{whether to check the met file}
28 |
29 | \item{verbose}{argument passed to read_apsim_met}
30 | }
31 | \value{
32 | it creates a list with objects of class \sQuote{met}.
33 | If it fails, then the objects will be of class \sQuote{try-error}.
34 | }
35 | \description{
36 | Retrieves met data from the World Modeler global database and (optionally) saves it to a file
37 | }
38 | \examples{
39 | \dontrun{
40 | ## Get soil profile properties for a single point
41 | am1 <- get_worldmodeler_apsim_met(lonlat = c(-93, 42),
42 | dates = c("2010-01-01", "2013-12-31"))
43 | if(inherits(am1, 'met')){
44 | plot(am1[[1]], met.var = "rain", cumulative = TRUE)
45 | }
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/man/solutes_parms.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/edit_apsim_replace_soil_profile.R
3 | \name{solutes_parms}
4 | \alias{solutes_parms}
5 | \title{Helper function to supply additional Solute parameters}
6 | \usage{
7 | solutes_parms(
8 | Depth = NA,
9 | Thickness = NA,
10 | Solutes = NA,
11 | InitialValues = NA,
12 | InitialValuesUnits = NA,
13 | WaterTableConcentration = NA,
14 | D0 = NA,
15 | Exco = NA,
16 | FIP = NA,
17 | DepthConstant = NA,
18 | MaxDepthSoluteAccessible = NA,
19 | RunoffEffectivenessAtMovingSolute = NA,
20 | MaxEffectiveRunoff = NA
21 | )
22 | }
23 | \arguments{
24 | \item{Depth}{depth for soil layers (see APSIM documentation)}
25 |
26 | \item{Thickness}{soil thickness for layers (either enter Depth or Thickness, but not both).
27 | Thickness will be recycled if more than one Solute is passed.}
28 |
29 | \item{Solutes}{Solutes supplied (for now this can be one or more of: \sQuote{NO3}, \sQuote{NH4} or \sQuote{Urea})}
30 |
31 | \item{InitialValues}{initial values of solutes}
32 |
33 | \item{InitialValuesUnits}{passed to Solutes}
34 |
35 | \item{WaterTableConcentration}{passed to Solutes}
36 |
37 | \item{D0}{passed to Solutes}
38 |
39 | \item{Exco}{passed to Solutes}
40 |
41 | \item{FIP}{passed to Solutes}
42 |
43 | \item{DepthConstant}{passed to Solutes}
44 |
45 | \item{MaxDepthSoluteAccessible}{passed to Solutes}
46 |
47 | \item{RunoffEffectivenessAtMovingSolute}{passed to Solutes}
48 |
49 | \item{MaxEffectiveRunoff}{passed to Solutes}
50 | }
51 | \description{
52 | Creates a list with specific components for the Solutes module
53 | }
54 |
--------------------------------------------------------------------------------
/man/get_worldmodeler_soil_profile.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_worldmodeler_soil_profile.R
3 | \name{get_worldmodeler_soil_profile}
4 | \alias{get_worldmodeler_soil_profile}
5 | \title{Obtain a synthetic APSIM soil profile from the World Modeler database}
6 | \usage{
7 | get_worldmodeler_soil_profile(
8 | lonlat,
9 | soil.name,
10 | wrt.dir,
11 | filename,
12 | verbose = FALSE
13 | )
14 | }
15 | \arguments{
16 | \item{lonlat}{Longitude and latitude vector (e.g. c(-93, 42)) or matrix.}
17 |
18 | \item{soil.name}{optional soil name}
19 |
20 | \item{wrt.dir}{optional directory where to save a file with \sQuote{soils} extension.
21 | If missing it will be written to a temporary directory.}
22 |
23 | \item{filename}{optional name to be used when saving the file}
24 |
25 | \item{verbose}{verbose argument passed to \sQuote{read_apsim_soils}}
26 | }
27 | \value{
28 | it returns a list with objects of class \sQuote{soil_profile}. If
29 | \sQuote{filename} is specified it also creates a file with extension \sQuote{soils},
30 | which can be read using function \code{\link{read_apsim_soils}}.
31 | }
32 | \description{
33 | Retrieves soil data from the World Modeler global database and (optionally) saves it to a soils file
34 | }
35 | \examples{
36 | \dontrun{
37 | ## Get soil profile properties for a single point
38 | sp1 <- get_worldmodeler_soil_profile(lonlat = c(-93, 42))
39 |
40 | if(inherits(sp1[[1]], 'soil_profile')){
41 | plot(sp1[[1]], property = "Carbon")
42 | }
43 | }
44 | }
45 | \author{
46 | Brian Collins (University of Southern Queensland) and Fernando Miguez
47 | }
48 |
--------------------------------------------------------------------------------
/man/apsim_example.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_classic.R
3 | \name{apsim_example}
4 | \alias{apsim_example}
5 | \title{Access Example APSIM Simulations}
6 | \usage{
7 | apsim_example(example = "Millet", silent = FALSE, tmp.dir = NULL)
8 | }
9 | \arguments{
10 | \item{example}{run an example from built-in APSIM. Options are all of the ones included with the APSIM distribution, except \sQuote{Graph}.}
11 |
12 | \item{silent}{whether to print standard output from the APSIM execution}
13 |
14 | \item{tmp.dir}{temporary directory where to write files}
15 | }
16 | \value{
17 | This function returns a data frame with APSIM output
18 | }
19 | \description{
20 | simple function to run some of the built-in APSIM examples
21 | }
22 | \details{
23 | This function creates a temporary copy of the example file distributed with APSIM to avoid writing a .out file
24 | to the directory where the \sQuote{Examples} are located. It is not a good practice and there is no guarantee that
25 | the user has read/write permissions in that directory.
26 | }
27 | \note{
28 | This function creates a new column \sQuote{Date} which is in the R \sQuote{Date} format which is convenient for graphics.
29 | }
30 | \examples{
31 | \dontrun{
32 | ## Only run these if you have APSIM 'Classic' installed (Windows only)
33 | millet <- apsim_example("Millet")
34 | potato <- apsim_example("Potato")
35 | sugar <- apsim_example("Sugar")
36 | ## The 'Date' column is created by this function, based on apsim output.
37 | require(ggplot2)
38 | ggplot(data = millet , aes(x = Date, y = millet_biomass)) +
39 | geom_line()
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/man/get_power_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_power_apsim_met.R
3 | \name{get_power_apsim_met}
4 | \alias{get_power_apsim_met}
5 | \title{Get NASA-POWER data for an APSIM met file}
6 | \usage{
7 | get_power_apsim_met(lonlat, dates, wrt.dir = ".", filename = NULL)
8 | }
9 | \arguments{
10 | \item{lonlat}{Longitude and latitude vector}
11 |
12 | \item{dates}{date ranges}
13 |
14 | \item{wrt.dir}{write directory}
15 |
16 | \item{filename}{file name for writing out to disk}
17 | }
18 | \value{
19 | returns an object of class \sQuote{met} and writes a file to disk when filename is supplied.
20 | }
21 | \description{
22 | Uses \code{\link[nasapower]{get_power}} from the \CRANpkg{nasapower} package to download data to create an APSIM met file.
23 | }
24 | \details{
25 | This function requires the \CRANpkg{nasapower} package version 4.0.0.
26 |
27 | It looks like the earliest year you can request data for is 1984.
28 |
29 | If the filename is not provided it will not write the file to disk,
30 | but it will return an object of class \sQuote{met}. This is useful in case manipulation
31 | is required before writing to disk.
32 | }
33 | \examples{
34 | \dontrun{
35 | require(nasapower)
36 | ## This will not write a file to disk
37 | pwr <- get_power_apsim_met(lonlat = c(-93,42), dates = c("2012-01-01","2012-12-31"))
38 | ## Let's insert a missing value
39 | pwr[100, "radn"] <- NA
40 | summary(pwr)
41 | ## Check the met file
42 | check_apsim_met(pwr)
43 | ## Impute using linear interpolation
44 | pwr.imptd <- impute_apsim_met(pwr, verbose = TRUE)
45 | summary(pwr.imptd)
46 | check_apsim_met(pwr.imptd)
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/R/apsimx_filetype.R:
--------------------------------------------------------------------------------
1 |
2 | #' Test whether an .apsimx file is XML or json
3 | #'
4 | #' @title Test file format for .apsimx files
5 | #' @name apsimx_filetype
6 | #' @param file file ending in .apsimx to be tested
7 | #' @param src.dir directory containing the .apsimx file to be tested; defaults to the current working directory
8 | #' @return \sQuote{xml}, \sQuote{json} or \sQuote{unknown}
9 | #' @note Minimal function which reads only the first line in a file and tries to guess whether it is
10 | #' an \sQuote{xml} or \sQuote{json} file type.
11 | #' @export
12 | #' @examples
13 | #' \donttest{
14 | #' extd.dir <- system.file("extdata", package = "apsimx")
15 | #' apsimx_filetype("Wheat.apsimx", src.dir = extd.dir)
16 | #' }
17 | #'
18 |
19 | apsimx_filetype <- function(file = "", src.dir = "."){
20 |
21 |
22 | file.names <- dir(path = src.dir, pattern=".apsimx$", ignore.case=TRUE)
23 | file.names.c <- dir(path = src.dir, pattern=".apsim$", ignore.case=TRUE)
24 | ## print(file.names.c)
25 | # file.names <- c(file.names.x, file.names.c)
26 |
27 | if(length(file.names)==0){
28 | stop("There are no .apsimx files in the specified directory to test.")
29 | }
30 |
31 | file <- match.arg(file, file.names, several.ok=FALSE)
32 |
33 | rfl1 <- utils::read.table(file = file.path(src.dir, file), nrows = 1)[1,1]
34 | ## The next line will work if all XML files contain xml in the first line
35 | if(length(grep("xml", as.character(rfl1))) != 0){
36 | ans <- "xml"
37 | }else{
38 | if(length(grep("{", as.character(rfl1), fixed = TRUE)) == 0){
39 | ans <- "unkown"
40 | }else{
41 | ans <- "json"
42 | }
43 | }
44 | return(ans)
45 | }
46 |
--------------------------------------------------------------------------------
/man/as_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{as_apsim_met}
4 | \alias{as_apsim_met}
5 | \title{Conversion from data frame to met object}
6 | \usage{
7 | as_apsim_met(
8 | x,
9 | filename = "noname.met",
10 | site = "nosite",
11 | latitude = 0,
12 | longitude = 0,
13 | tav = NA,
14 | amp = NA,
15 | colnames = c("year", "day", "radn", "maxt", "mint", "rain"),
16 | units = c("()", "()", "(MJ/m2/day)", "(oC)", "(oC)", "(mm)"),
17 | constants = NA,
18 | comments = NA,
19 | check = TRUE
20 | )
21 | }
22 | \arguments{
23 | \item{x}{object of class \sQuote{data frame}}
24 |
25 | \item{filename}{default \sQuote{noname.met}}
26 |
27 | \item{site}{default \sQuote{nosite}}
28 |
29 | \item{latitude}{default is zero (0)}
30 |
31 | \item{longitude}{default is zero (0)}
32 |
33 | \item{tav}{average temperature (calculated if not supplied)}
34 |
35 | \item{amp}{temperature amplitude (calculated if not supplied)}
36 |
37 | \item{colnames}{default are \dQuote{year}, \dQuote{day}, \dQuote{radn},
38 | \dQuote{maxt}, \dQuote{mint}, \dQuote{rain}}
39 |
40 | \item{units}{default are \dQuote{()}, \dQuote{()}, \dQuote{(MJ/m2/day)},
41 | \dQuote{(oC)}, \dQuote{(oC)}, \dQuote{(mm)}}
42 |
43 | \item{constants}{default is \dQuote{NA}}
44 |
45 | \item{comments}{default is \dQuote{NA}}
46 |
47 | \item{check}{whether to check the resulting met file using \code{\link{check_apsim_met}}.
48 | default is TRUE.}
49 | }
50 | \value{
51 | it returns an object of class \sQuote{met}.
52 | }
53 | \description{
54 | It makes minimum assumptions about the data so it is recommended to change defaults
55 | }
56 | \details{
57 | Simple utility for converting a data frame to an object of class met
58 | }
59 |
--------------------------------------------------------------------------------
/tests/test_read_apsim_met.R:
--------------------------------------------------------------------------------
1 | ## Testing read_apsim_met
2 | require(apsimx)
3 | require(nasapower)
4 | apsimx_options(warn.versions = FALSE)
5 |
6 | ## setwd(tempdir())
7 |
8 | run.test.apsim.met <- get(".run.local.tests", envir = apsimx.options)
9 |
10 | if(run.test.apsim.met){
11 | ex.dir <- auto_detect_apsimx_examples()
12 | extd.dir <- system.file("extdata", package = "apsimx")
13 |
14 | lmf <- list.files(paste0(ex.dir,"/WeatherFiles"))
15 |
16 | lmf2 <- lmf[lmf %in% c("Dalby.met","Goond.met","Ingham.met","lincoln.met","Popondetta.met",
17 | "VCS_Ruakura.met","WaggaWagga.met")]
18 | }
19 |
20 | if(run.test.apsim.met){
21 | for(i in lmf2){
22 | tmp <- read_apsim_met(file = i, src.dir = paste0(ex.dir,"/WeatherFiles"), verbose = FALSE)
23 | cat("Read:",i,"\n")
24 | }
25 | }
26 |
27 | ## Test multiple imputation function
28 |
29 | run.test.impute.apsim.met <- get(".run.local.tests", envir = apsimx.options)
30 |
31 | ## One trick to only test this locally?
32 | username <- Sys.info()[["user"]]
33 |
34 | ## I need to change this test so that it downloads data instead of distributing
35 | ## nasapower met data with the package
36 | if(run.test.impute.apsim.met && username == "fernandomiguez"){
37 |
38 | ## This will not write a file to disk
39 | pwr <- get_power_apsim_met(lonlat = c(-93,42), dates = c("2012-01-01","2012-12-31"))
40 | ## Note that missing data is coded as -99
41 | summary(pwr)
42 | ## Check for reasonable ranges
43 | check_apsim_met(pwr)
44 | ## Impute using linear interpolation
45 | pwr[1, "radn"] <- NA
46 | pwr[nrow(pwr), "radn"] <- NA
47 | check_apsim_met(pwr)
48 | pwr.imptd <- impute_apsim_met(pwr, verbose = TRUE)
49 | summary(pwr.imptd)
50 | check_apsim_met(pwr.imptd)
51 | }
52 |
--------------------------------------------------------------------------------
/man/carbon_stocks.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx_soil_profile.R
3 | \name{carbon_stocks}
4 | \alias{carbon_stocks}
5 | \title{Calculate soil carbon stocks}
6 | \usage{
7 | carbon_stocks(
8 | x,
9 | depth = NULL,
10 | area = c("m2", "ha"),
11 | method = c("linear", "constant"),
12 | value = c("numeric", "carbon.stocks"),
13 | ...
14 | )
15 | }
16 | \arguments{
17 | \item{x}{object of class \sQuote{soil_profile}}
18 |
19 | \item{depth}{soil depth (in meters). If missing then the whole soil profile is used.}
20 |
21 | \item{area}{either \sQuote{m2} meter squared or \sQuote{ha}.}
22 |
23 | \item{method}{interpolation method. Either \sQuote{linear} or \sQuote{constant}.}
24 |
25 | \item{value}{whether to return a \sQuote{numeric} or an object of class \sQuote{carbon.stocks}.}
26 |
27 | \item{...}{additional arguments passed to internal functions (none used at the moment).}
28 | }
29 | \value{
30 | returns a value with attribute \sQuote{units} and \sQuote{depth}
31 | }
32 | \description{
33 | Calculation of carbon stocks based on an object of class \sQuote{soil_profile}
34 | }
35 | \details{
36 | Function to calculate carbon stocks. The output units depend on the choice of area.
37 | If \sQuote{m2} is used, then the output units will be \sQuote{kg/m2}. If the \sQuote{area}
38 | is \sQuote{ha}, then the output units will be \sQuote{Mg/ha}.
39 |
40 | Note that the bulk density (which is needed in the calculation) is
41 | available as part of the \sQuote{soil_profile} object.
42 | }
43 | \examples{
44 | \dontrun{
45 | sp <- apsimx_soil_profile()
46 | carbon_stocks(sp)
47 | carbon_stocks(sp, depth = 0.1)
48 | carbon_stocks(sp, depth = 0.2)
49 | carbon_stocks(sp, depth = 0.3)
50 | carbon_stocks(sp, depth = 0.4)
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/man/apsim.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_classic.R
3 | \name{apsim}
4 | \alias{apsim}
5 | \title{Run an APSIM (7.x) \sQuote{Classic} simulation}
6 | \usage{
7 | apsim(
8 | file = "",
9 | src.dir = ".",
10 | silent = FALSE,
11 | value = "report",
12 | cleanup = FALSE,
13 | simplify = TRUE
14 | )
15 | }
16 | \arguments{
17 | \item{file}{file name to be run (the extension .apsim is optional)}
18 |
19 | \item{src.dir}{directory containing the .apsim file to be run (defaults to the current directory)}
20 |
21 | \item{silent}{whether to print messages from apsim simulation}
22 |
23 | \item{value}{how much output to return: \cr
24 | option \sQuote{report} returns only the \sQuote{main} report component;
25 | option \sQuote{all} returns all components of the simulation; \cr
26 | option \sQuote{none} runs simulation but does not return a data frame; \cr
27 | option \sQuote{user-defined} should be the name of a specific output file.}
28 |
29 | \item{cleanup}{logical. Whether to delete the .out and .sum files generated by APSIM. Default is FALSE.}
30 |
31 | \item{simplify}{whether to return a single data frame when multiple simulations are present. If FALSE it will return a list.}
32 | }
33 | \value{
34 | This function returns a data frame with APSIM output, but it depends on the argument \sQuote{value} above.
35 | }
36 | \description{
37 | Run apsim from R. It's for Windows only. It uses \sQuote{shell}.
38 | }
39 | \details{
40 | Run an APSIM (7.x) \sQuote{Classic} Simulation
41 |
42 | A valid apsim file can be run from within R. The main goal is to make running APSIM-X
43 | simple, especially for large scale simulations or parameter optimization
44 | }
45 | \examples{
46 | \donttest{
47 | ## See function 'apsim_example'
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/man/insert_replacement_node.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/wget_apsimx_json.R
3 | \name{insert_replacement_node}
4 | \alias{insert_replacement_node}
5 | \title{Inserts a replacement node in a simple apsimx simulation file}
6 | \usage{
7 | insert_replacement_node(
8 | file,
9 | src.dir,
10 | wrt.dir,
11 | rep.node,
12 | edit.tag = "-edited",
13 | overwrite = FALSE,
14 | verbose = FALSE
15 | )
16 | }
17 | \arguments{
18 | \item{file}{file ending in .apsimx to be edited (JSON)}
19 |
20 | \item{src.dir}{directory containing the .apsimx file to be edited; defaults to the current working directory}
21 |
22 | \item{wrt.dir}{should be used if the destination directory is different from the src.dir}
23 |
24 | \item{rep.node}{replacement node as obtained by the \code{\link{get_apsimx_json}} function}
25 |
26 | \item{edit.tag}{if the file is edited a different tag from the default \sQuote{-edited} can be used.}
27 |
28 | \item{overwrite}{logical; if \code{TRUE} the old file is overwritten, a new file is written otherwise}
29 |
30 | \item{verbose}{whether to print information about successful edit}
31 | }
32 | \value{
33 | it does not return an R object but it writes an apsimx file to disk
34 | }
35 | \description{
36 | Inserts a replacement node in a simple apsimx simulation file
37 | }
38 | \examples{
39 | \dontrun{
40 | ## It is not trivial to produce a reproducible example
41 | ## because the model and file versions need to align.
42 | ## The steps are:
43 | ## 1. Get model:
44 | ## wheat <- get_apsimx_json(model = "Wheat", wrt.dir = tmp.dir)
45 | ## 2. Create file that matches current model version
46 | ## 3. Edit the file by inserting the 'replacements' node
47 | ## insert_replacement_node("Wheat.apsimx", rep.node = wheat)
48 |
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/man/get_ssurgo_tables.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_ssurgo_tables.R
3 | \name{get_ssurgo_tables}
4 | \alias{get_ssurgo_tables}
5 | \title{Retrieve soil profile data and return a (list) with data frames (tables)}
6 | \usage{
7 | get_ssurgo_tables(lonlat, shift = -1, aoi, verbose = FALSE)
8 | }
9 | \arguments{
10 | \item{lonlat}{Longitude and latitude vector (e.g. c(-93, 42))}
11 |
12 | \item{shift}{simple mechanism for creating an area of interest by displacing the point indicated in
13 | lonlat by some amount of distance (e.g. 300 - in meters)}
14 |
15 | \item{aoi}{area of interest, if supplied the lonlat and shift arguments will be ignored. Should be
16 | of class \sQuote{sp::SpatialPolygons} or \sQuote{sf}.}
17 |
18 | \item{verbose}{whether to print messages and warnings to the console default FALSE}
19 | }
20 | \value{
21 | a list with elements: mapunit, component, chorizon and mapunit.shp
22 | }
23 | \description{
24 | This function does partially what get_ssurgo_soil_profile does, but it
25 | returns a list with tables for mapunit, component, chorizon and mapunit.shp (object of class sf)
26 | }
27 | \details{
28 | Data source is USDA-NRCS Soil Data Access. See package soilDB for more details
29 |
30 | * If a point is requested then an object of class \sQuote{sf} is returned (for mapunit.shp)
31 | with the MUKEY and AREASYMBOL with GEOMETRY type: POINT.
32 |
33 | * If a the request is for a spatial polygon, then an object of class \sQuote{sf} is returned
34 | with gid, mukey and area_ac with GEOMETRY type: POLYGON.
35 | }
36 | \examples{
37 | \dontrun{
38 | require(soilDB)
39 | require(sp)
40 | require(sf)
41 | require(spData)
42 | ## retrieve data from lon -93, lat = 42
43 | stbls <- get_ssurgo_tables(lonlat = c(-93, 42))
44 |
45 | stbls2 <- get_ssurgo_tables(lonlat = c(-93, 42), shift = 200)
46 |
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/man/get_gsod_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_gsod_apsim_met.R
3 | \name{get_gsod_apsim_met}
4 | \alias{get_gsod_apsim_met}
5 | \title{Get GSOD data for an APSIM met file}
6 | \usage{
7 | get_gsod_apsim_met(
8 | lonlat,
9 | dates,
10 | wrt.dir = ".",
11 | filename = NULL,
12 | distance = 100,
13 | station,
14 | fillin.radn = FALSE
15 | )
16 | }
17 | \arguments{
18 | \item{lonlat}{Longitude and latitude vector}
19 |
20 | \item{dates}{date ranges}
21 |
22 | \item{wrt.dir}{write directory}
23 |
24 | \item{filename}{file name for writing out to disk}
25 |
26 | \item{distance}{distance in kilometers for the nearest station}
27 |
28 | \item{station}{choose the station either by index or character}
29 |
30 | \item{fillin.radn}{whether to fill in radiation data using the nasapower pacakge. Default is FALSE.}
31 | }
32 | \value{
33 | returns an object of class \sQuote{met} and writes a file to disk when filename is supplied.
34 | }
35 | \description{
36 | Uses \code{\link[GSODR]{get_GSOD}} from the \CRANpkg{GSODR} package to download data to create an APSIM met file.
37 | }
38 | \details{
39 | This function requires the \CRANpkg{GSODR} package.
40 |
41 | If the filename is not provided it will not write the file to disk,
42 | but it will return an object of class \sQuote{met}. This is useful in case manipulation
43 | is required before writing to disk.
44 | }
45 | \note{
46 | This source of data does not provide solar radiation. If \sQuote{fillin.radn} is
47 | TRUE it fill in radiation data using the nasapower package.
48 | }
49 | \examples{
50 | \dontrun{
51 | require(GSODR)
52 | ## This will not write a file to disk
53 | gsd <- get_gsod_apsim_met(lonlat = c(-93,42), dates = c("2012-01-01","2012-12-31"),
54 | fillin.radn = TRUE)
55 | summary(gsd)
56 | ## Check for reasonable ranges
57 | check_apsim_met(gsd)
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/man/add_column_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{add_column_apsim_met}
4 | \alias{add_column_apsim_met}
5 | \alias{$<-.met}
6 | \alias{[<-.met}
7 | \alias{[[<-.met}
8 | \alias{remove_column_apsim_met}
9 | \title{Add a column to an object of class \sQuote{met}}
10 | \usage{
11 | add_column_apsim_met(met, value, name, units)
12 |
13 | \method{$}{met}(x, name) <- value
14 |
15 | \method{[}{met}(x, i, j) <- value
16 |
17 | \method{[[}{met}(x, i, j) <- value
18 |
19 | remove_column_apsim_met(met, name)
20 | }
21 | \arguments{
22 | \item{met}{object of class \sQuote{met}}
23 |
24 | \item{value}{value for the data.frame. It could be an integer, double or vector of length equal to the number of rows in x.}
25 |
26 | \item{name}{name of the variable to be removed}
27 |
28 | \item{units}{units for the new column (required)}
29 |
30 | \item{x}{object of class \sQuote{met}}
31 | }
32 | \value{
33 | an object of class \sQuote{met} with the additional column
34 |
35 | an object of class \sQuote{met} without the variable (column) in \sQuote{name}
36 | }
37 | \description{
38 | The usual way of adding a column to a data frame might
39 | not work for an object of class \sQuote{met}, so this method is recommended
40 | }
41 | \examples{
42 | \donttest{
43 | extd.dir <- system.file("extdata", package = "apsimx")
44 | ames <- read_apsim_met("Ames.met", src.dir = extd.dir)
45 |
46 | ## The recommended method is
47 | val <- abs(rnorm(nrow(ames), 10))
48 | ames <- add_column_apsim_met(ames, value = val, name = "vp", units = "(hPa)")
49 |
50 | ## This is also possible
51 | vp <- data.frame(vp = abs(rnorm(nrow(ames), 10)))
52 | attr(vp, "units") <- "(hPa)"
53 | ames$vp <- vp$vp
54 |
55 | ## This is needed to ensure that units and related attributes are also removed
56 | ames <- remove_column_apsim_met(ames, "vp")
57 | ## However, ames$vp <- NULL will also work
58 | }
59 |
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/man/edit_apsimx_replace_soil_profile.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/edit_apsimx_replace_soil_profile.R
3 | \name{edit_apsimx_replace_soil_profile}
4 | \alias{edit_apsimx_replace_soil_profile}
5 | \title{Edit APSIM-X file with a replaced soil profile}
6 | \usage{
7 | edit_apsimx_replace_soil_profile(
8 | file = "",
9 | src.dir = ".",
10 | wrt.dir = NULL,
11 | soil.profile = NULL,
12 | edit.tag = "-edited",
13 | overwrite = FALSE,
14 | verbose = TRUE,
15 | root = NULL
16 | )
17 | }
18 | \arguments{
19 | \item{file}{name of the .apsimx file to be edited}
20 |
21 | \item{src.dir}{source directory}
22 |
23 | \item{wrt.dir}{writing directory}
24 |
25 | \item{soil.profile}{a soil profile object with class \sQuote{soil_profile}}
26 |
27 | \item{edit.tag}{default edit tag \sQuote{-edited}}
28 |
29 | \item{overwrite}{default FALSE}
30 |
31 | \item{verbose}{default TRUE and it will print messages to console}
32 |
33 | \item{root}{supply the node position in the case of multiple simulations such as factorials.}
34 | }
35 | \value{
36 | writes a file to disk with the supplied soil profile
37 | }
38 | \description{
39 | Edits an APSIM-X simulation by replacing the soil profile
40 | }
41 | \details{
42 | This function is designed to batch replace the whole soil in an APSIM simulation file.
43 | }
44 | \note{
45 | There is no such thing as a default soil, carefully build the profile for each simulation.
46 | }
47 | \examples{
48 | \donttest{
49 | sp <- apsimx_soil_profile()
50 | extd.dir <- system.file("extdata", package = "apsimx")
51 |
52 | ## I write to a temp directory but replace as needed
53 | tmp.dir <- tempdir()
54 |
55 | edit_apsimx_replace_soil_profile("Maize.apsimx", soil.profile = sp,
56 | src.dir = extd.dir, wrt.dir = tmp.dir)
57 | inspect_apsimx("Maize-edited.apsimx", src.dir = tmp.dir,
58 | node = "Soil")
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/man/get_iemre_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_iemre_apsim_met.R
3 | \name{get_iemre_apsim_met}
4 | \alias{get_iemre_apsim_met}
5 | \title{Get weather data from Iowa Environmental Mesonet Reanalysis}
6 | \usage{
7 | get_iemre_apsim_met(
8 | lonlat,
9 | dates,
10 | wrt.dir = ".",
11 | filename = NULL,
12 | fillin.radn = FALSE
13 | )
14 | }
15 | \arguments{
16 | \item{lonlat}{Longitude and latitude vector}
17 |
18 | \item{dates}{date ranges}
19 |
20 | \item{wrt.dir}{write directory}
21 |
22 | \item{filename}{file name for writing out to disk}
23 |
24 | \item{fillin.radn}{whether to fill in radiation data using the nasapower pacakge. Default is FALSE.}
25 | }
26 | \value{
27 | returns an object of class \sQuote{met} and writes a file to disk when filename is supplied.
28 | }
29 | \description{
30 | Retrieves weather data from Iowa Environmental Mesonet Reanalysis into an APSIM met file
31 | }
32 | \details{
33 | The original data can be obtained from: https://mesonet.agron.iastate.edu/iemre/
34 |
35 | If the filename is not provided it will not write the file to disk,
36 | but it will return an object of class \sQuote{met}. This is useful in case manipulation
37 | is required before writing to disk.
38 | }
39 | \note{
40 | Multi-year query is not supported for this product.
41 | }
42 | \examples{
43 | \dontrun{
44 | ## This will not write a file to disk
45 | iemre <- get_iemre_apsim_met(lonlat = c(-93,42), dates = c("2012-01-01","2012-12-31"))
46 | ## Note that solar radiation is not available, but can be filled in
47 | ## using the nasapower package
48 | iemre2 <- get_iemre_apsim_met(lonlat = c(-93,42),
49 | dates = c("2012-01-01","2012-12-31"),
50 | fillin.radn = TRUE)
51 | summary(iemre)
52 | summary(iemre2)
53 |
54 | ## Still it is important to check this object
55 | check_apsim_met(iemre2)
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/tests/test_grep_json_list.R:
--------------------------------------------------------------------------------
1 | ## Different behaviors of grep_json_list
2 |
3 | require(apsimx)
4 | apsimx_options(warn.versions = FALSE)
5 |
6 | run.grep.test <- get(".run.local.tests", envir = apsimx.options)
7 |
8 | if(run.grep.test){
9 |
10 | file <- "Wheat.apsimx"
11 | src.dir <- auto_detect_apsimx_examples()
12 |
13 | apsimx_json <- jsonlite::read_json(file.path(src.dir, file))
14 |
15 | ## This function sort of works, but it is not quite what we want
16 | ## ans1 <- grep_json_list1("Cultivar", apsimx_json)
17 |
18 | ## A better function?
19 | ans <- grep_json_list("Cultivar", apsimx_json)
20 |
21 | ans <- grep_json_list("Hartog", apsimx_json)
22 |
23 | ## This does not work
24 | ## ans <- grep_json_list("Version", apsimx_json)
25 |
26 | ans <- grep_json_list("Sowing", apsimx_json)
27 |
28 | ## Testing inspect_apsimx_json
29 | pp0 <- inspect_apsimx_json("Wheat.apsimx", src.dir = src.dir, parm = "Version")
30 |
31 | pp1 <- inspect_apsimx_json("Wheat.apsimx", src.dir = src.dir, parm = "Simulations")
32 |
33 | pp2 <- inspect_apsimx_json("Wheat.apsimx", src.dir = src.dir, parm = "Clock")
34 |
35 | ## pp2.1 <- inspect_apsimx_json("Wheat.apsimx", src.dir = src.dir, parm = "Start") ## Need to account for this
36 |
37 | pp3 <- inspect_apsimx_json("Wheat.apsimx", src.dir = src.dir, parm = "Weather")
38 |
39 | ## pp3.1 <- inspect_apsimx_json("Wheat.apsimx", src.dir = src.dir, parm = "FileName") ## Need to account for this
40 |
41 | pp4 <- inspect_apsimx_json("Wheat.apsimx", src.dir = src.dir, parm = "DUL")
42 |
43 | pp6 <- inspect_apsimx_json("Wheat.apsimx", src.dir = src.dir, parm = "SWCON")
44 |
45 | ## pp7 <- inspect_apsimx_json("Wheat.apsimx", src.dir = src.dir, parm = "LL15")
46 |
47 | ## pp8 <- inspect_apsimx_json("Wheat.apsimx", src.dir = src.dir, parm = "Sow using")
48 |
49 | pp9 <- inspect_apsimx_json("Wheat.apsimx", src.dir = src.dir, parm = "Hartog")
50 |
51 | }
--------------------------------------------------------------------------------
/man/soilwat_parms.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/edit_apsim_replace_soil_profile.R
3 | \name{soilwat_parms}
4 | \alias{soilwat_parms}
5 | \title{Helper function to supply SoilWat parameters}
6 | \usage{
7 | soilwat_parms(
8 | SummerCona = NA,
9 | SummerU = NA,
10 | SummerDate = NA,
11 | WinterCona = NA,
12 | WinterU = NA,
13 | WinterDate = NA,
14 | DiffusConst = NA,
15 | DiffusSlope = NA,
16 | Salb = NA,
17 | CN2Bare = NA,
18 | CNRed = NA,
19 | CNCov = NA,
20 | Slope = NA,
21 | DischargeWidth = NA,
22 | CatchmentArea = NA,
23 | MaxPond = NA,
24 | SWCON = NA,
25 | Thickness = NA
26 | )
27 | }
28 | \arguments{
29 | \item{SummerCona}{see APSIM documentation}
30 |
31 | \item{SummerU}{see APSIM documentation}
32 |
33 | \item{SummerDate}{see APSIM documentation}
34 |
35 | \item{WinterCona}{see APSIM documentation}
36 |
37 | \item{WinterU}{see APSIM documentation}
38 |
39 | \item{WinterDate}{see APSIM documentation}
40 |
41 | \item{DiffusConst}{see APSIM documentation}
42 |
43 | \item{DiffusSlope}{see APSIM documentation}
44 |
45 | \item{Salb}{soil albedo (see APSIM documentation)}
46 |
47 | \item{CN2Bare}{see APSIM documentation}
48 |
49 | \item{CNRed}{see APSIM documentation}
50 |
51 | \item{CNCov}{see APSIM documentation}
52 |
53 | \item{Slope}{see APSIM documentation}
54 |
55 | \item{DischargeWidth}{see APSIM documentation}
56 |
57 | \item{CatchmentArea}{see APSIM documentation}
58 |
59 | \item{MaxPond}{see APSIM documentation}
60 |
61 | \item{SWCON}{see APSIM documentation}
62 |
63 | \item{Thickness}{provide the corresponding thickness layer}
64 | }
65 | \value{
66 | a \sQuote{list} with class \sQuote{soilwat_parms}
67 | }
68 | \description{
69 | Creates a list with specific components for the SoilWat model
70 | }
71 | \details{
72 | current documentation for APSIM 7.10 \url{https://www.apsim.info/documentation/model-documentation/soil-modules-documentation/soilwat/}
73 | }
74 |
--------------------------------------------------------------------------------
/man/get_iem_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_iemre_apsim_met.R
3 | \name{get_iem_apsim_met}
4 | \alias{get_iem_apsim_met}
5 | \title{Get weather data from Iowa Environmental Ag Weather Stations}
6 | \usage{
7 | get_iem_apsim_met(lonlat, dates, wrt.dir = ".", state, station, filename)
8 | }
9 | \arguments{
10 | \item{lonlat}{Longitude and latitude vector (optional)}
11 |
12 | \item{dates}{date ranges}
13 |
14 | \item{wrt.dir}{write directory}
15 |
16 | \item{state}{state which you choose climate data from}
17 |
18 | \item{station}{station which you choose cliamte data from}
19 |
20 | \item{filename}{file name for writing out to disk}
21 | }
22 | \value{
23 | returns an object of class \sQuote{met} and writes a file to disk when filename is supplied.
24 | }
25 | \description{
26 | Retrieves weather data from Iowa Environmental Mesonet (AgWeather) into an APSIM met file
27 | }
28 | \details{
29 | The original data can be obtained from: https://mesonet.agron.iastate.edu/request/coop/fe.phtml
30 |
31 | If the filename is not provided it will not write the file to disk,
32 | but it will return an object of class \sQuote{met}. This is useful in case manipulation
33 | is required before writing to disk. For this function either provide the longitude
34 | and latitude or the state and station, but not both. In fact, \sQuote{state} and
35 | \sQuote{station} will be ignored if \sQuote{lonlat} is supplied.
36 | }
37 | \examples{
38 | \dontrun{
39 | ## This will not write a file to disk
40 | iem.met <- get_iem_apsim_met(state = "IA",
41 | station = "IA0200",
42 | dates = c("2012-01-01","2012-12-31"))
43 |
44 | summary(iem.met)
45 |
46 | ## Alternatively, coordinates can be used
47 | ## This should be equivalent to the previous request
48 | iem.met2 <- get_iem_apsim_met(lonlat = c(-93.77, 42.02),
49 | dates = c("2012-01-01","2012-12-31"))
50 |
51 | summary(iem.met2)
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/inst/extdata/Maize.csv:
--------------------------------------------------------------------------------
1 | "CheckpointID","SimulationID","Zone","Clock.Today","Maize.Phenology.CurrentStageName","Maize.AboveGround.Wt","Maize.AboveGround.N","Maize.Grain.Wt","Maize.Grain.Size","Maize.Grain.NumberFunction","Maize.Grain.N","Maize.Total.Wt","Date"
2 | 1,1,"Field","1991-05-28 12:00:00","HarvestRipe",1625.79242047545,15.6645331797079,863.920526481788,0.282471392719674,3058.43546903579,11.359977843759,1750.91178017821,1991-05-28
3 | 1,1,"Field","1992-04-09 12:00:00","HarvestRipe",902.099342677026,10.1375002421492,494.585086266944,0.28187551587384,1754.62237198461,6.56126581062025,979.375358706639,1992-04-09
4 | 1,1,"Field","1993-03-16 12:00:00","HarvestRipe",198.485002364912,2.10288549699734,70.3547288982805,0.302829951335575,232.324208976008,0.94466545779189,221.646034001362,1993-03-16
5 | 1,1,"Field","1994-03-15 12:00:00","HarvestRipe",832.62301673018,8.81930530152225,373.447574667307,0.220476849545734,1693.81762954591,5.23477557801663,909.9516209372,1994-03-15
6 | 1,1,"Field","1995-04-04 12:00:00","HarvestRipe",1571.07178164777,16.9053196115832,812.784228637644,0.280541069092516,2897.20229293632,10.837162881889,1712.93275470988,1995-04-04
7 | 1,1,"Field","1996-03-15 12:00:00","HarvestRipe",1995.85176071568,18.3266494861792,885.178187981663,0.299567237476232,2954.85646374094,11.288845223715,2152.06772539061,1996-03-15
8 | 1,1,"Field","1997-04-05 12:00:00","HarvestRipe",1123.36335586334,11.4205761687996,405.097638746584,0.298877417273355,1355.39728107353,5.36835312241526,1274.59025434334,1997-04-05
9 | 1,1,"Field","1998-03-06 12:00:00","HarvestRipe",883.043430859492,8.815055551191,331.035889804223,0.295592174987998,1119.90748678535,4.49146001083045,1000.25660778222,1998-03-06
10 | 1,1,"Field","1999-04-10 12:00:00","HarvestRipe",1879.58074363833,18.1881485394794,844.08120651574,0.29837026036001,2828.97231613258,11.1603623198647,2034.84836838482,1999-04-10
11 | 1,1,"Field","2000-04-04 12:00:00","HarvestRipe",1603.23745394378,17.1068627904784,777.951640117353,0.28955289217715,2686.7341378217,10.2618710598899,1759.57055507131,2000-04-04
12 |
--------------------------------------------------------------------------------
/man/get_daymet_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_daymet_apsim_met.R
3 | \name{get_daymet_apsim_met}
4 | \alias{get_daymet_apsim_met}
5 | \title{Get DAYMET data for an APSIM met file}
6 | \source{
7 | The data is retrieved using the \CRANpkg{daymetr} package. For the original
8 | source see: https://daymet.ornl.gov/
9 | }
10 | \usage{
11 | get_daymet_apsim_met(lonlat, years, wrt.dir = ".", filename, silent = FALSE)
12 | }
13 | \arguments{
14 | \item{lonlat}{Longitude and latitude vector}
15 |
16 | \item{years}{a numeric vector of years to extract (c(start, end)). For example, f you need 2012 through 2015, use c(2012, 2015).}
17 |
18 | \item{wrt.dir}{write directory (default is the current directory)}
19 |
20 | \item{filename}{file name for writing out to disk}
21 |
22 | \item{silent}{argument passed to \code{\link[daymetr]{download_daymet}}}
23 | }
24 | \value{
25 | It returns an object of class \sQuote{met} and writes a file to disk when filename is supplied.
26 | }
27 | \description{
28 | Uses \code{\link[daymetr]{download_daymet}} from the \CRANpkg{daymetr} package to download data to create an APSIM met file.
29 | }
30 | \details{
31 | This function requires the \CRANpkg{daymetr} package. This function should replace
32 | the \code{\link{get_daymet_apsim_met}} function.
33 |
34 | If the filename is not provided it will not write the file to disk,
35 | but it will return an object of class \sQuote{met}. This is useful in case manipulation
36 | is required before writing to disk. The variable \sQuote{srad} as downloaded from
37 | daymet is average solar radiation, so it is converted to total.
38 | Daily total radiation (MJ/m2/day) can be calculated as
39 | follows: ((srad (W/m2) * dayl (s/day)) / 1,000,000) \cr
40 | Vapor Pressure Deficit (vp) should be in hecto Pascals
41 | }
42 | \examples{
43 | \dontrun{
44 | require(daymetr)
45 | ## I write to a temp directory but replace as needed
46 | dmet12 <- get_daymet_apsim_met(lonlat = c(-93,42), years = 2012)
47 | summary(dmet12)
48 | ## Check for reasonable ranges
49 | check_apsim_met(dmet12)
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/man/get_daymet2_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_daymet_apsim_met.R
3 | \name{get_daymet2_apsim_met}
4 | \alias{get_daymet2_apsim_met}
5 | \title{Get DAYMET data for an APSIM met file}
6 | \source{
7 | The data is retrieved using the \CRANpkg{daymetr} package. For the original
8 | source see: https://daymet.ornl.gov/
9 | }
10 | \usage{
11 | get_daymet2_apsim_met(lonlat, years, wrt.dir = ".", filename, silent = FALSE)
12 | }
13 | \arguments{
14 | \item{lonlat}{Longitude and latitude vector}
15 |
16 | \item{years}{a numeric vector of years to extract (c(start, end)). For example, f you need 2012 through 2015, use c(2012, 2015).}
17 |
18 | \item{wrt.dir}{write directory (default is the current directory)}
19 |
20 | \item{filename}{file name for writing out to disk}
21 |
22 | \item{silent}{argument passed to \code{\link[daymetr]{download_daymet}}}
23 | }
24 | \value{
25 | It returns an object of class \sQuote{met} and writes a file to disk when filename is supplied.
26 | }
27 | \description{
28 | Uses \code{\link[daymetr]{download_daymet}} from the \CRANpkg{daymetr} package to download data to create an APSIM met file.
29 | }
30 | \details{
31 | This function requires the \CRANpkg{daymetr} package. This function should replace
32 | the \code{\link{get_daymet_apsim_met}} function.
33 |
34 | If the filename is not provided it will not write the file to disk,
35 | but it will return an object of class \sQuote{met}. This is useful in case manipulation
36 | is required before writing to disk. The variable \sQuote{srad} as downloaded from
37 | daymet is average solar radiation, so it is converted to total.
38 | Daily total radiation (MJ/m2/day) can be calculated as
39 | follows: ((srad (W/m2) * dayl (s/day)) / 1,000,000) \cr
40 | Vapor Pressure Deficit (vp) should be in hecto Pascals
41 | }
42 | \examples{
43 | \dontrun{
44 | require(daymetr)
45 | ## I write to a temp directory but replace as needed
46 | dmet12 <- get_daymet2_apsim_met(lonlat = c(-93,42), years = 2012)
47 | summary(dmet12)
48 | ## Check for reasonable ranges
49 | check_apsim_met(dmet12)
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/man/apsimx_options.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx.R
3 | \name{apsimx_options}
4 | \alias{apsimx_options}
5 | \title{Setting some options for the package}
6 | \usage{
7 | apsimx_options(
8 | exe.path = NA,
9 | dotnet = FALSE,
10 | mono = FALSE,
11 | examples.path = NA,
12 | warn.versions = TRUE,
13 | warn.find.apsimx = TRUE,
14 | allow.path.spaces = FALSE,
15 | system.intern = FALSE
16 | )
17 | }
18 | \arguments{
19 | \item{exe.path}{path to apsim executable. White spaces are not allowed.}
20 |
21 | \item{dotnet}{logical indicating if APSIM should be run through the dotnet command}
22 |
23 | \item{mono}{logical indicating if the mono command should be used when running APSIM. This is for versions
24 | for Mac/Linux older than Sept 2021.}
25 |
26 | \item{examples.path}{path to apsim examples}
27 |
28 | \item{warn.versions}{logical. warning if multiple versions of APSIM-X are detected.}
29 |
30 | \item{warn.find.apsimx}{logical. By default a warning will be thrown if APSIM-X is not found.
31 | If \sQuote{exe.path} is \sQuote{NA} an error will be thrown instead.}
32 |
33 | \item{allow.path.spaces}{logical. By default spaces are not allowed in paths or in the run command.}
34 |
35 | \item{system.intern}{logical. Default is FALSE. \sQuote{intern} argument passed to \code{\link{system}}.}
36 | }
37 | \value{
38 | as a side effect it modifies the \sQuote{apsimx.options} environment.
39 | }
40 | \description{
41 | Set the path to the APSIM-X executable, examples and warning suppression.
42 | }
43 | \details{
44 | Set apsimx options
45 | }
46 | \note{
47 | It is possible that APSIM-X is installed in some alternative location other than the
48 | defaults ones. Guessing this can be difficult and then the auto_detect functions might
49 | fail. Also, if multiple versions of APSIM-X are installed apsimx will choose the newest
50 | one but it will issue a warning. Suppress the warning by setting warn.versions = FLASE.
51 | }
52 | \examples{
53 | \donttest{
54 | names(apsimx.options)
55 | apsimx_options(exe.path = "some-new-path-to-executable")
56 | apsimx.options$exe.path
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/man/xargs_apsimx.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx.R
3 | \name{xargs_apsimx}
4 | \alias{xargs_apsimx}
5 | \title{Provide extra arguments for APSIM-X}
6 | \usage{
7 | xargs_apsimx(
8 | verbose = FALSE,
9 | csv = FALSE,
10 | merge.db.files = FALSE,
11 | list.simulations = FALSE,
12 | list.referenced.filenames = FALSE,
13 | single.threaded = FALSE,
14 | cpu.count = -1L,
15 | simulation.names = FALSE,
16 | dotnet = FALSE,
17 | mono = FALSE,
18 | exe.path = NA
19 | )
20 | }
21 | \arguments{
22 | \item{verbose}{Write detailed messages to stdout when a simulation starts/finishes.}
23 |
24 | \item{csv}{Export all reports to .csv files.}
25 |
26 | \item{merge.db.files}{Merge multiple .db files into a single .db file.}
27 |
28 | \item{list.simulations}{List simulation names without running them.}
29 |
30 | \item{list.referenced.filenames}{List all files that are referenced by an .apsimx file(s).}
31 |
32 | \item{single.threaded}{Run all simulations sequentially on a single thread.}
33 |
34 | \item{cpu.count}{(Default: -1) Maximum number of threads/processes to spawn for running simulations.}
35 |
36 | \item{simulation.names}{Only run simulations if their names match this regular expression.}
37 |
38 | \item{dotnet}{Logical. There is a global option for this argument, but this will override it. This
39 | can be useful if the goal is to compare an old version of Next Gen (before Sept 2021) with a more
40 | recent version in the same script. This might be needed if you have your own compiled version of APSIM Next Gen.}
41 |
42 | \item{mono}{Logical. Should be set to TRUE if running a version of APSIM Next Gen from Aug 2021 or older on Mac or Linux.}
43 |
44 | \item{exe.path}{executable path. This can be useful for having both a global option through \sQuote{apsimx.options} and
45 | a local option that will override that. This option will take precedence.}
46 | }
47 | \value{
48 | it returns a character vector with the extra arguments.
49 | }
50 | \description{
51 | This provides additinoal command line arguments when running the model
52 | }
53 | \details{
54 | Extra arguments for running APSIM-X
55 | }
56 |
--------------------------------------------------------------------------------
/man/apsimx.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx.R
3 | \name{apsimx}
4 | \alias{apsimx}
5 | \title{Run an APSIM-X simulation}
6 | \usage{
7 | apsimx(
8 | file = "",
9 | src.dir = ".",
10 | silent = FALSE,
11 | intern = FALSE,
12 | value = "report",
13 | cleanup = FALSE,
14 | simplify = TRUE,
15 | xargs
16 | )
17 | }
18 | \arguments{
19 | \item{file}{file name to be run (the extension .apsimx is optional)}
20 |
21 | \item{src.dir}{directory containing the .apsimx file to be run (defaults to the current directory)}
22 |
23 | \item{silent}{whether to print messages from apsim simulation. This is passed to argument \sQuote{ignore.stdout} for
24 | function \code{\link{system}}.}
25 |
26 | \item{intern}{This is passed to argument \sQuote{intern} for function \code{\link{system}}.}
27 |
28 | \item{value}{how much output to return: \cr
29 | option \sQuote{report} returns only the \sQuote{main} report component; \cr
30 | option \sQuote{all} returns all components of the simulation; \cr
31 | option \sQuote{none} does not create a data.frame but it generates the databases; \cr
32 | option \sQuote{user-defined} should be the name of a specific table}
33 |
34 | \item{cleanup}{logical. Whether to delete the .db file generated by APSIM-X. Default is FALSE}
35 |
36 | \item{simplify}{whether to return a single data frame when multiple reports are present. If FALSE it will return a list.}
37 |
38 | \item{xargs}{extra arguments to be passed to the APSIM-X run. Use function xargs_apsimx.}
39 | }
40 | \value{
41 | a data frame with the \sQuote{Report} from the APSIM-X simulation. The return value depends on the argument \sQuote{value} above.
42 | }
43 | \description{
44 | Run apsimx from R. It uses \code{\link{system}} (unix) or \code{\link{shell}} (windows) and it attempts to be platform independent.
45 | }
46 | \details{
47 | Run an APSIM-X Simulation
48 |
49 | A valid apsimx file can be run from within R. The main goal is to make running APSIM-X
50 | simple, especially for large scale simulations or parameter optimization
51 | }
52 | \examples{
53 | \donttest{
54 | ## See function 'apsimx_example' and vignette 'apsimx'
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/tests/test_vignette.R:
--------------------------------------------------------------------------------
1 | ## These are tests that I will run to reproduce the part of the vignette
2 | ## That depends on APSIM-X. In this way the vignette can be build independently
3 | ## of APSIM-X, but the testing side-effect of the vignette is still present
4 | require(apsimx)
5 | apsimx_options(warn.versions = FALSE)
6 |
7 | extd.dir <- system.file("extdata", package = "apsimx")
8 |
9 | run.vignette <- get(".run.local.tests", envir = apsimx.options)
10 |
11 | tmp.dir <- tempdir()
12 |
13 | if(run.vignette){
14 |
15 | ## Copy 'Maize.apsimx' to 'tmp.dir'
16 | ex.dir <- auto_detect_apsimx_examples()
17 | file.copy(from = paste0(ex.dir,"/","Maize.apsimx"),
18 | to = tmp.dir, copy.mode = FALSE)
19 | maize <- apsimx("Maize.apsimx", src.dir = tmp.dir)
20 |
21 | ## Running 'Wheat' example
22 | file.copy(from = paste0(ex.dir,"/","Wheat.apsimx"),
23 | to = tmp.dir, copy.mode = FALSE)
24 | wheat <- apsimx("Wheat.apsimx", src.dir = tmp.dir)
25 |
26 | ## Running 'MaizeSoybean' example
27 | file.copy(from = paste0(extd.dir,"/","Ames.met"),
28 | to = tmp.dir)
29 | file.copy(from = paste0(extd.dir,"/","MaizeSoybean.apsimx"),
30 | to = tmp.dir)
31 | maize.soy <- apsimx("MaizeSoybean.apsimx", src.dir = tmp.dir)
32 |
33 | ## The code below should only be run from my laptop and only when a new
34 | ## version of APSIM-X is released
35 | ## This updates the 'Wheat' and 'Maize' example files in case they have
36 | ## changed
37 |
38 | aiu <- apsim_version(which = "inuse", verbose = FALSE)
39 |
40 | ## Need to change this for new versions of APSIM-X
41 | ## apsimx.new.version <- as.numeric(strsplit(aiu, ".", fixed = TRUE)[[1]][4])
42 |
43 | my.laptop <- grepl("fernandomiguez", Sys.info()[["user"]])
44 | ## This is only for my laptop
45 | if(my.laptop && FALSE){
46 | my.extd <- "~/Dropbox/apsimx/inst/extdata"
47 | file.copy(from = paste0(ex.dir,"/","Maize.apsimx"),
48 | to = my.extd, copy.mode = FALSE,
49 | overwrite = TRUE)
50 | file.copy(from = paste0(ex.dir,"/","Wheat.apsimx"),
51 | to = my.extd, copy.mode = FALSE,
52 | overwrite = TRUE)
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/man/inspect_apsimx_json.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/inspect_apsimx_json.R
3 | \name{inspect_apsimx_json}
4 | \alias{inspect_apsimx_json}
5 | \title{Inspect an .apsimx or .json (JSON) file}
6 | \usage{
7 | inspect_apsimx_json(
8 | file = "",
9 | src.dir = ".",
10 | parm,
11 | search.depth = 15,
12 | print.path = FALSE,
13 | verbose = FALSE
14 | )
15 | }
16 | \arguments{
17 | \item{file}{file ending in .apsimx or .json to be inspected (JSON)}
18 |
19 | \item{src.dir}{directory containing the .apsimx or .json file to be inspected; defaults to the current working directory}
20 |
21 | \item{parm}{string or regular expression for partial matching. It can be two strings separated by a period to search within a node (child).}
22 |
23 | \item{search.depth}{default is 15. How deep should the algorithm explore the structure of the list.}
24 |
25 | \item{print.path}{whether to print the parameter path (default is FALSE)}
26 |
27 | \item{verbose}{whether to print additional information (mostly used for debugging)}
28 | }
29 | \value{
30 | prints a table with inspected parameters and values (and the path when \sQuote{print.path} = TRUE).
31 | }
32 | \description{
33 | inspect an .apsimx or .json (JSON) file. It does not replace the GUI, but it can save time by quickly checking parameters and values.
34 | }
35 | \details{
36 | This function is a work in progress. There are many instances for which it will not work.
37 |
38 | It will probably only find the first instance that matches.
39 | }
40 | \examples{
41 | \donttest{
42 | extd.dir <- system.file("extdata", package = "apsimx")
43 | ## It seems to work for simple search
44 | inspect_apsimx_json("Wheat.apsimx", src.dir = extd.dir, parm = "Version")
45 | inspect_apsimx_json("Wheat.apsimx", src.dir = extd.dir, parm = "Simulations")
46 | inspect_apsimx_json("Wheat.apsimx", src.dir = extd.dir, parm = "Clock")
47 | inspect_apsimx_json("Wheat.apsimx", src.dir = extd.dir, parm = "Weather")
48 | ## Does return soil components
49 | inspect_apsimx_json("Wheat.apsimx", src.dir = extd.dir, parm = "DUL")
50 | ## Or cultivar
51 | inspect_apsimx_json("Wheat.apsimx", src.dir = extd.dir, parm = "Hartog")
52 |
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/man/swim_parms.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/edit_apsim_replace_soil_profile.R
3 | \name{swim_parms}
4 | \alias{swim_parms}
5 | \title{Helper function to supply SWIM parameters}
6 | \usage{
7 | swim_parms(
8 | Salb = NA,
9 | CN2Bare = NA,
10 | CNRed = NA,
11 | CNCov = NA,
12 | KDul = NA,
13 | PSIDul = NA,
14 | VC = NA,
15 | DTmin = NA,
16 | DTmax = NA,
17 | MaxWaterIncrement = NA,
18 | SpaceWeightingFactor = NA,
19 | SoluteSpaceWeightingFactor = NA,
20 | Diagnostics = NA,
21 | SwimWaterTable_WaterTableDepth = NA,
22 | SwimSubsurfaceDrain_DrainDepth = NA,
23 | SwimSubsurfaceDrain_DrainSpacing = NA,
24 | SwimSubsurfaceDrain_DrainRadius = NA,
25 | SwimSubsurfaceDrain_Klat = NA,
26 | SwimSubsurfaceDrain_ImpermDepth = NA
27 | )
28 | }
29 | \arguments{
30 | \item{Salb}{see APSIM documentation}
31 |
32 | \item{CN2Bare}{see APSIM documentation}
33 |
34 | \item{CNRed}{see APSIM documentation}
35 |
36 | \item{CNCov}{see APSIM documentation}
37 |
38 | \item{KDul}{see APSIM documentation}
39 |
40 | \item{PSIDul}{see APSIM documentation}
41 |
42 | \item{VC}{see APSIM documentation}
43 |
44 | \item{DTmin}{see APSIM documentation}
45 |
46 | \item{DTmax}{see APSIM documentation}
47 |
48 | \item{MaxWaterIncrement}{see APSIM documentation}
49 |
50 | \item{SpaceWeightingFactor}{see APSIM documentation}
51 |
52 | \item{SoluteSpaceWeightingFactor}{see APSIM documentation}
53 |
54 | \item{Diagnostics}{see APSIM documentation}
55 |
56 | \item{SwimWaterTable_WaterTableDepth}{see APSIM documentation}
57 |
58 | \item{SwimSubsurfaceDrain_DrainDepth}{see APSIM documentation}
59 |
60 | \item{SwimSubsurfaceDrain_DrainSpacing}{see APSIM documentation}
61 |
62 | \item{SwimSubsurfaceDrain_DrainRadius}{see APSIM documentation}
63 |
64 | \item{SwimSubsurfaceDrain_Klat}{see APSIM documentation}
65 |
66 | \item{SwimSubsurfaceDrain_ImpermDepth}{see APSIM documentation}
67 | }
68 | \value{
69 | a \sQuote{list} with class \sQuote{swim_parms}
70 | }
71 | \description{
72 | Creates a list with specific components for the SWIM model
73 | }
74 | \details{
75 | current documentation for APSIM 7.10 \url{https://www.apsim.info/documentation/model-documentation/soil-modules-documentation/swim3/}
76 | }
77 |
--------------------------------------------------------------------------------
/tests/test_apsim_soils.R:
--------------------------------------------------------------------------------
1 | ## Test reading and writing soils in XML format
2 | ## Author: Fernando E. Miguez
3 | ## Date: 2022-02-16
4 | ##
5 | ## Required: R package apsimx 2.3.2
6 | require(apsimx)
7 | require(ggplot2)
8 |
9 | apsimx_options(warn.versions = FALSE)
10 |
11 | run.apsimx.soils <- get(".run.local.tests", envir = apsimx.options)
12 |
13 | run.apsimx.soils <- FALSE ## It takes 23 minutes to run the code below
14 |
15 | if(run.apsimx.soils){
16 |
17 | extd.dir <- system.file("extdata", package = "apsimx")
18 |
19 | sls <- read_apsim_soils("Clarion.soils", src.dir = extd.dir)
20 |
21 | ## All soils in APSoil
22 | apsoil.dir <- "~/Dropbox/apsimx-other/APSoil"
23 |
24 | ## Reading in all this data takes a while
25 | start <- Sys.time()
26 | asls <- read_apsim_soils("APSRU-Australia-soils.soils", src.dir = apsoil.dir)
27 | end <- Sys.time()
28 |
29 | }
30 |
31 | ### Testing fixing a soil profile
32 |
33 | run.apsimx.soils.fix <- get(".run.local.tests", envir = apsimx.options)
34 |
35 | try.isric <- try(get_isric_soil_profile(lonlat = c(-93, 42), fix = TRUE, verbose = FALSE), silent = TRUE)
36 |
37 | ## If the above fails it is very likely that the server is down
38 | if(inherits(try.isric, 'try-error')) run.apsimx.soils.fix <- FALSE
39 | run.apsimx.soils.fix <- FALSE
40 |
41 | if(run.apsimx.soils.fix){
42 |
43 | tmp.dir <- tempdir()
44 | setwd(tmp.dir)
45 | ex.dir <- auto_detect_apsimx_examples()
46 |
47 | file.copy(file.path(ex.dir, "Wheat.apsimx"), ".")
48 |
49 | lon <- seq(1, 2, length.out = 30)
50 | lat <- seq(48, 49, length.out = 30)
51 |
52 | for(i in seq_along(lon)){
53 |
54 | sp <- try(get_isric_soil_profile(lonlat = c(lon[i], lat[i]), fix = TRUE, verbose = FALSE), silent = TRUE)
55 | if(inherits(sp, "try-error")){
56 | cat("No soil for this location. Lon:", lon[i], "Lat:", lat[i], "\n")
57 | next
58 | }
59 |
60 | edit_apsimx_replace_soil_profile("Wheat.apsimx", soil.profile = sp,
61 | edit.tag = paste0("-", i))
62 | sim1 <- try(apsimx(paste0("Wheat-", i, ".apsimx")), silent = TRUE)
63 |
64 | if(inherits(sim1, "try-error"))
65 | cat("Simulation:", i, "failed\n")
66 | }
67 | }
68 |
69 |
70 |
--------------------------------------------------------------------------------
/tests/test_initialwater.R:
--------------------------------------------------------------------------------
1 |
2 | ## Testing initialwater with a variety of files
3 | require(apsimx)
4 | packageVersion("apsimx")
5 | apsimx_options(warn.versions = FALSE)
6 |
7 | run.apsimx.initialwater <- get(".run.local.tests", envir = apsimx.options)
8 |
9 | #### Testing InitialWater ----
10 | if(run.apsimx.initialwater){
11 |
12 | tmp.dir <- tempdir()
13 | ex.dir <- auto_detect_apsimx_examples()
14 | ex.dir.list <- dir(ex.dir, pattern = "apsimx$")
15 |
16 | #### I will skip files that require 'root'
17 | ##ex.dir.list2 <- ex.dir.list[c(2, 4, 16:19, 23, 24, 29, 30, 34)]
18 | ex.dir.list3 <- c("Barley.apsimx", "Canola.apsimx", "Maize.apsimx", "Mungbean.apsimx",
19 | "Oats.apsimx", "OilPalm.apsimx", "Potato.apsimx", "RedClover.apsimx",
20 | "Sorghum.apsimx", "Soybean.apsimx", "Wheat.apsimx")
21 | for(.i in ex.dir.list3){
22 | if(.i == "Slurp.apsimx") stop("This is bananas", call. = FALSE)
23 | cat("Simulation:", .i, "\n")
24 | #### 1) Inspect ----
25 | cat("Inspecting...\n")
26 | inspect_apsimx(.i, ex.dir, node = "Soil", soil.child = "InitialWater")
27 | pp <- inspect_apsimx(.i, ex.dir, node = "Soil", soil.child = "InitialWater", print.path = TRUE)
28 | #### 2) Extract ----
29 | cat("Extracting...\n")
30 | edf.iw <- extract_data_apsimx(.i, ex.dir, node = "Soil", soil.child = "InitialWater")
31 | #### 3) Edit ----
32 | cat("Editing...\n")
33 | if(!file.exists(file.path(tmp.dir, .i))) file.copy(from = file.path(ex.dir, .i), to = tmp.dir)
34 | if(!is.null(edf.iw$second) && nrow(edf.iw$second) > 0){
35 | initialvalues <- rep(0.3, nrow(edf.iw$second))
36 | edit_apsimx(.i, tmp.dir, node = "Soil",
37 | soil.child = "InitialWater",
38 | parm = "InitialValues",
39 | value = initialvalues)
40 | #### 4) Check ----
41 | cat("Checking...\n")
42 | edited.file <- paste0(tools::file_path_sans_ext(.i), "-edited.apsimx")
43 | inspect_apsimx(edited.file, tmp.dir, node = "Soil", soil.child = "InitialWater")
44 | check_apsimx(edited.file, tmp.dir)
45 | }
46 | if(file.exists(file.path(tmp.dir, edited.file))) file.remove(file.path(tmp.dir, edited.file))
47 | }
48 | }
49 |
50 |
--------------------------------------------------------------------------------
/tests/test_inspect_apsim_factorial.R:
--------------------------------------------------------------------------------
1 | ## Testing the factorial feature for inspect_apsim
2 | require(apsimx)
3 |
4 | run.factorial.apsim <- FALSE
5 |
6 | extd.dir <- system.file("extdata", package = "apsimx")
7 |
8 | tmp.dir <- tempdir()
9 |
10 | if(run.factorial.apsim){
11 |
12 | ## There are two folders in 'maize-factorial'
13 | inspect_apsim("maize-factorial.apsim", src.dir = extd.dir,
14 | node = "Clock", parm = "start_date",
15 | root = "IA-CC_Canisteo_No-Cover",
16 | print.path = TRUE)
17 |
18 | inspect_apsim("maize-factorial.apsim", src.dir = extd.dir,
19 | node = "Clock", parm = "start_date",
20 | root = "IA-CC_Canisteo_Cover",
21 | print.path = TRUE)
22 |
23 | ## Edit them
24 | pp1 <- inspect_apsim("maize-factorial.apsim", src.dir = extd.dir,
25 | node = "Clock", parm = "start_date",
26 | root = "IA-CC_Canisteo_No-Cover",
27 | print.path = TRUE)
28 |
29 | edit_apsim("maize-factorial.apsim",
30 | src.dir = extd.dir,
31 | wrt.dir = tmp.dir,
32 | node = "Other",
33 | parm.path = pp1,
34 | value = "01/02/1999")
35 |
36 | inspect_apsim("maize-factorial-edited.apsim",
37 | src.dir = tmp.dir,
38 | node = "Clock", parm = "start_date",
39 | root = "IA-CC_Canisteo_No-Cover",
40 | print.path = TRUE)
41 |
42 | ## Second example
43 | pp2 <- inspect_apsim("maize-factorial.apsim", src.dir = extd.dir,
44 | node = "Clock", parm = "start_date",
45 | root = "IA-CC_Canisteo_No-Cover",
46 | print.path = TRUE)
47 |
48 | edit_apsim("maize-factorial.apsim",
49 | src.dir = extd.dir,
50 | wrt.dir = tmp.dir,
51 | node = "Other",
52 | parm.path = pp2,
53 | value = "01/02/2009")
54 |
55 | inspect_apsim("maize-factorial-edited.apsim",
56 | src.dir = tmp.dir,
57 | node = "Clock", parm = "start_date",
58 | root = "IA-CC_Canisteo_No-Cover",
59 | print.path = TRUE)
60 |
61 |
62 | }
--------------------------------------------------------------------------------
/tests/test_get_isric_soil_profile.R:
--------------------------------------------------------------------------------
1 | ## Testing downloading soil data from ISRIC
2 | ## In repeated testing it seems that speed slows down when multiple queries are requested
3 |
4 | require(apsimx)
5 |
6 | run.test.isric.workflow <- get(".run.local.tests", envir = apsimx.options)
7 |
8 | if(run.test.isric.workflow){
9 |
10 | trrry <- try(sp1 <- get_isric_soil_profile(lonlat = c(-93, 42)), silent = TRUE)
11 |
12 | if(!inherits(trrry, "try-error")){
13 | # This takes a few seconds
14 | system.time(sp1 <- get_isric_soil_profile(lonlat = c(-93, 42), fix = TRUE)) ## 1.508 seconds
15 | system.time(sp2 <- get_ssurgo_soil_profile(lonlat = c(-93, 42), fix = TRUE)) ## 1.858 seconds
16 |
17 | plot(sp1)
18 |
19 | plot(sp2[[1]])
20 |
21 | ## The code below now should add LL15, KL and XF
22 | sp3 <- get_isric_soil_profile(lonlat = c(-93, 42), fix = TRUE, xargs = list(crops = "Oilpalm"))
23 | ## Only get SSURGO tables
24 | ## stbls <- get_ssurgo_tables(lonlat = c(-93, 42))
25 | }
26 | }
27 |
28 |
29 |
30 | if(run.test.isric.workflow){
31 |
32 | ### Testing the effectiveness of the 'xargs' option
33 |
34 | ### Testing the number of layers
35 | sp4 <- get_isric_soil_profile(lonlat = c(-93, 42), fix = TRUE, xargs = list(nlayers = 5))
36 |
37 | if(length(sp4$soil$Depth) != 5){
38 | stop("Length of sp4$soil$Depth should be equal to 5")
39 | }
40 |
41 | ### Testing the soil.bottom argument
42 | sp5 <- get_isric_soil_profile(lonlat = c(-93, 42), fix = TRUE, xargs = list(soil.bottom = 210))
43 |
44 | if(sum(sp5$soil$Thickness) * 1e-1 != 210){
45 | stop("Bottom of soil profile shuod be equal to 210")
46 | }
47 |
48 | ### Testing soil bottom and number of layers
49 | sp6 <- get_isric_soil_profile(lonlat = c(-93, 42), fix = TRUE, xargs = list(nlayers = 7, soil.bottom = 210))
50 |
51 | if(sum(sp6$soil$Thickness) * 1e-1 != 210){
52 | stop("Bottom of soil profile shuod be equal to 210")
53 | }
54 | if(length(sp6$soil$Depth) != 7){
55 | stop("Length of sp6$soil$Depth should be equal to 7")
56 | }
57 |
58 | ### This still works
59 | sp7 <- get_isric_soil_profile(lonlat = c(-93, 42), fix = TRUE, xargs = list(crops = "Canola"))
60 |
61 | if(sp7$crops != "Canola"){
62 | stop("sp7$crops should be 'Canola'")
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/tests/test_soil_properties.R:
--------------------------------------------------------------------------------
1 | ## Test soil properties in APSIM-X
2 |
3 | require(apsimx)
4 | require(ggplot2)
5 |
6 | apsimx_options(warn.versions = FALSE)
7 |
8 | run.soil <- get(".run.local.tests", envir = apsimx.options)
9 |
10 | tmp.dir <- tempdir()
11 | setwd(tmp.dir)
12 |
13 | if(run.soil){
14 |
15 | ex.dir <- auto_detect_apsimx_examples()
16 |
17 | file.copy(file.path(ex.dir, "Wheat.apsimx"), tmp.dir)
18 |
19 | wheat <- apsimx("Wheat.apsimx")
20 |
21 | inspect_apsimx("Wheat.apsimx",
22 | node = "Soil", soil.child = "Physical")
23 |
24 | duls <- c(0.52, 0.49, 0.488, 0.48, 0.47, 0.457, 0.452) * 1.05
25 |
26 | edit_apsimx("Wheat.apsimx", src.dir = ex.dir,
27 | wrt.dir = tmp.dir, node = "Soil",
28 | soil.child = "Physical", parm = "DUL",
29 | value = duls)
30 |
31 | inspect_apsimx("Wheat-edited.apsimx", src.dir = tmp.dir,
32 | node = "Soil", soil.child = "Physical")
33 |
34 | wheat2 <- apsimx("Wheat-edited.apsimx")
35 |
36 | ggplot() +
37 | geom_line(data = wheat, aes(x = Date, y = Wheat.AboveGround.Wt, color = "Original DUL")) +
38 | geom_line(data = wheat2, aes(x = Date, y = Wheat.AboveGround.Wt, color = "DUL times 1.05"))
39 |
40 | }
41 |
42 | if(run.soil){
43 |
44 | ## For now
45 | extd.dir <- system.file("extdata", package = "apsimx")
46 |
47 | inspect_apsimx("Wheat.apsimx", src.dir = extd.dir,
48 | node = "Soil", soil.child = "Physical")
49 |
50 | inspect_apsimx("Maize.apsimx", src.dir = extd.dir,
51 | node = "Soil", soil.child = "Physical")
52 |
53 | inspect_apsimx("Wheat_soil.apsimx", src.dir = extd.dir,
54 | node = "Soil", soil.child = "Physical")
55 |
56 | ## CanI now just edit the Crop specific soil physical?
57 | edit_apsimx("Wheat_soil.apsimx", src.dir = extd.dir,
58 | wrt.dir = tmp.dir,
59 | node = "Soil", soil.child = "Physical",
60 | parm = "Maize KL", value = rep(0.07, 7))
61 |
62 | edit_apsimx("Wheat_soil-edited.apsimx",
63 | src.dir = tmp.dir,
64 | overwrite = TRUE,
65 | node = "Soil", soil.child = "Physical",
66 | parm = "Wheat KL", value = rep(0.05, 7))
67 |
68 | inspect_apsimx("Wheat_soil-edited.apsimx", src.dir = tmp.dir,
69 | node = "Soil", soil.child = "Physical")
70 |
71 | }
--------------------------------------------------------------------------------
/R/get_chirps_apsim_met.R:
--------------------------------------------------------------------------------
1 | #'
2 | #' This function requires the \CRANpkg{chirps} package, version 0.1.4.
3 | #'
4 | #' @title Get CHIRPS data for an APSIM met file
5 | #' @description Uses \code{\link[chirps]{get_chirps}} from the \CRANpkg{chirps} package to download data to create an APSIM met file.
6 | #' @name get_chirps_apsim_met
7 | #' @param lonlat Longitude and latitude vector
8 | #' @param dates date ranges
9 | #' @param wrt.dir write directory
10 | #' @param filename file name for writing out to disk
11 | #' @param fillin.radn whether to fill in radiation data using the nasapower pacakge. Default is TRUE.
12 | #' @param silent defaut is FALSE. Changing it will not do anything at the moment. A future feature.
13 | #' @return returns an object of class \sQuote{met} and writes a file to disk when filename is supplied.
14 | #' @details If the filename is not provided it will not write the file to disk,
15 | #' but it will return an object of class \sQuote{met}. This is useful in case manipulation
16 | #' is required before writing to disk.
17 | #' @export
18 | #' @examples
19 | #' \dontrun{
20 | #' require(chirps)
21 | #' ## This will not write a file to disk
22 | #' chrp <- get_chirps_apsim_met(lonlat = c(-93,42), dates = c("2012-01-01","2012-12-31"))
23 | #' }
24 | #'
25 |
26 | get_chirps_apsim_met <- function(lonlat, dates, wrt.dir = ".", filename = NULL,
27 | fillin.radn = TRUE, silent = FALSE){
28 |
29 | if(!requireNamespace("chirps", quietly = TRUE)){
30 | warning("The chirps package is required for this function")
31 | return(NULL)
32 | }
33 |
34 | if(missing(filename)) filename <- "noname.met"
35 |
36 | if(!grepl(".met", filename, fixed = TRUE)) stop("filename should end in .met")
37 |
38 | objct <- data.frame(lon = lonlat[1], lat = lonlat[2])
39 |
40 | chrp <- chirps::get_chirps(object = objct, dates = dates, server = "ClimateSERV") ## This is only precipitation
41 |
42 | pwr <- get_power_apsim_met(lonlat = lonlat, dates = dates)
43 |
44 | pwr$rain <- chrp$chirps
45 | pwr[["rh"]] <- NULL
46 | pwr[["windspeed"]] <- NULL
47 |
48 | attr(pwr, "units") <- c("()", "()", "(MJ/m2/day)", "(oC)", "(oC)", "(mm)")
49 | attr(pwr, "colnames") <- c("year", "day", "radn", "maxt", "mint", "rain")
50 |
51 | chrp <- pwr
52 |
53 | if(filename != "noname.met"){
54 | write_apsim_met(chrp, wrt.dir = wrt.dir, filename = filename)
55 | }
56 | return(invisible(chrp))
57 | }
--------------------------------------------------------------------------------
/tests/test_insert_replacement_node.R:
--------------------------------------------------------------------------------
1 | ## Testing whether
2 |
3 | require(apsimx)
4 | apsimx_options(warn.versions = FALSE)
5 |
6 | # apsimx_options(warn.versions = FALSE,
7 | # exe.path = "/Applications/APSIM2022.6.7044.0.app/Contents/Resources/bin/Models",
8 | # examples.path = "/Applications/APSIM2022.6.7044.0.app/Contents/Resources/Examples")
9 |
10 | run.insert <- get(".run.local.tests", envir = apsimx.options)
11 |
12 | #### This does not work at the moment
13 | run.insert <- FALSE
14 |
15 | tmp.dir <- tempdir()
16 | extd.dir <- system.file("extdata", package = "apsimx")
17 |
18 | if(run.insert){
19 |
20 | ex.dir <- auto_detect_apsimx_examples()
21 |
22 | ## tmp.dir <- "~/Desktop/csm"
23 | #### Test for wheat ####
24 | wheat <- get_apsimx_json(model = "Wheat", wrt.dir = tmp.dir)
25 |
26 | inspect_apsimx_json("Wheat.json", src.dir = tmp.dir,
27 | parm = "Version")
28 |
29 | file.copy(file.path(ex.dir, "Wheat.apsimx"), tmp.dir)
30 |
31 | inspect_apsimx("Wheat_7382.apsimx", src.dir = tmp.dir, node = "Other", parm = 2)
32 |
33 | inspect_apsimx_json("Wheat_7382.apsimx", src.dir = tmp.dir,
34 | parm = "Version")
35 |
36 | ## Versions need to match, but they don't right now even with the latest
37 |
38 | insert_replacement_node("Wheat_7382.apsimx",
39 | src.dir = tmp.dir,
40 | wrt.dir = tmp.dir,
41 | rep.node = wheat)
42 |
43 | inspect_apsimx("Wheat_7382-edited.apsimx", src.dir = tmp.dir, node = "Other", parm = 2)
44 |
45 | #### Test for maize ####
46 | maize <- get_apsimx_json(model = "Maize", wrt.dir = tmp.dir)
47 | insert_replacement_node("Wheat_7382-edited.apsimx",
48 | src.dir = tmp.dir,
49 | wrt.dir = tmp.dir,
50 | rep.node = maize,
51 | edit.tag = "-maize")
52 |
53 | #### Test for soybean ####
54 | soybean <- get_apsimx_json(model = "Soybean", wrt.dir = tmp.dir)
55 |
56 | file.copy(file.path(ex.dir, "Soybean.apsimx"), tmp.dir)
57 |
58 | insert_replacement_node("Soybean.apsimx",
59 | src.dir = extd.dir,
60 | wrt.dir = tmp.dir,
61 | rep.node = soybean)
62 |
63 | sim2 <- apsimx("Soybean-edited.apsimx", src.dir = tmp.dir)
64 |
65 | }
--------------------------------------------------------------------------------
/tests/test_worldmodeler.R:
--------------------------------------------------------------------------------
1 | #### Testing get_worldmodeler_soil_profile
2 | require(apsimx)
3 | apsimx_options(warn.versions = FALSE)
4 |
5 | run.test.worldmodeler <- get(".run.local.tests", envir = apsimx.options)
6 |
7 | if(run.test.worldmodeler && Sys.info()[["user"]] == "fernandomiguez"){
8 |
9 | ### Simple example which cleans up
10 | sp0 <- get_worldmodeler_soil_profile(lonlat = c(-93, 42))
11 | plot(sp0[[1]], property = "water")
12 |
13 | tdir <- "~/Dropbox/apsimx-other/worldmodeler"
14 | get_worldmodeler_soil_profile(lonlat = c(-93, 42), wrt.dir = tdir,
15 | filename = "temp_soils.soils")
16 |
17 | lonlat.mat <- rbind(c(-93, 42), c(-92, 41), c(-91, 40))
18 | get_worldmodeler_soil_profile(lonlat = lonlat.mat, wrt.dir = tdir,
19 | filename = "mat_temp_soils.soils")
20 |
21 | sp1 <- read_apsim_soils("temp_soils.soils", src.dir = tdir)
22 |
23 | plot(sp1[[1]], property = "Carbon")
24 |
25 | sps <- read_apsim_soils("mat_temp_soils.soils", src.dir = tdir)
26 |
27 | plot(sps[[2]], property = "Carbon")
28 |
29 | }
30 |
31 | if(run.test.worldmodeler && Sys.info()[["user"]] == "fernandomiguez"){
32 |
33 | am0 <- get_worldmodeler_apsim_met(lonlat = c(-93, 42), dates = c("1990-01-01", "2020-12-31"))
34 |
35 | if(inherits(am0[[1]], 'met')){
36 | plot(am0[[1]], met.var = "rain", cumulative = TRUE, climatology = TRUE)
37 | }
38 |
39 | tdir <- "~/Dropbox/apsimx-other/worldmodeler"
40 |
41 | lonlat.mat <- rbind(c(-93, 42), c(-92, 41), c(-91, 40))
42 |
43 | am1 <- get_worldmodeler_apsim_met(lonlat = lonlat.mat,
44 | dates = c("1990-01-01", "2020-12-31"))
45 |
46 | if(inherits(am1[[1]], 'met')){
47 | plot(am1[[1]], met.var = "rain", cumulative = TRUE, climatology = TRUE)
48 | }
49 |
50 | if(inherits(am1[[2]], 'met')){
51 | plot(am1[[2]], met.var = "rain", cumulative = TRUE, climatology = TRUE)
52 | }
53 |
54 | am2 <- get_worldmodeler_apsim_met(lonlat = lonlat.mat,
55 | dates = c("1990-01-01", "2020-12-31"),
56 | wrt.dir = tdir,
57 | filenames = c("marshall1.met", "marshall2.met", "marshall3.met"))
58 |
59 | sapply(am2, class)
60 |
61 | if(inherits(am2[[1]], 'met')){
62 | plot(am2[[1]], met.var = "rain", cumulative = TRUE, climatology = TRUE)
63 | }
64 | }
--------------------------------------------------------------------------------
/man/unit_conv.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/unit_conv.R
3 | \name{unit_conv}
4 | \alias{unit_conv}
5 | \title{performs common unit conversions}
6 | \usage{
7 | unit_conv(x, from, to, ...)
8 | }
9 | \arguments{
10 | \item{x}{input variable}
11 |
12 | \item{from}{original units}
13 |
14 | \item{to}{target units}
15 |
16 | \item{...}{additional arguments passed to specific conversions}
17 | }
18 | \value{
19 | value of the input variable with new units
20 | }
21 | \description{
22 | This function is slowly getting better. Adding more unit conversions as I need them.
23 | }
24 | \details{
25 | Function which performs common unit conversions
26 |
27 | At the moment possible conversions are:
28 | \itemize{
29 |
30 | \item \sQuote{g/m2} to \sQuote{kg/ha}
31 | \item \sQuote{kg/ha} to \sQuote{g/m2}
32 | \item \sQuote{lb} to \sQuote{kg}
33 | \item \sQuote{kg} to \sQuote{lb}
34 | \item \sQuote{maize bu} to \sQuote{kg}
35 | \item \sQuote{kg} to \sQuote{maize bu}
36 | \item \sQuote{soy bu} to \sQuote{kg}
37 | \item \sQuote{kg} to \sQuote{soy bu}
38 | \item \sQuote{maize bu/ac} to \sQuote{kg/ha}
39 | \item \sQuote{maize bu/ac} to \sQuote{g/m2}
40 | \item \sQuote{kg/ha} to \sQuote{maize bu/ac}
41 | \item \sQuote{g/m2} to \sQuote{maize bu/ac}
42 | \item \sQuote{soy bu/ac} to \sQuote{kg/ha}
43 | \item \sQuote{soy bu/ac} to \sQuote{g/m2}
44 | \item \sQuote{kg/ha} to \sQuote{soy bu/ac}
45 | \item \sQuote{g/m2} to \sQuote{soy bu/ac}
46 | \item \sQuote{mm} to \sQuote{inches}
47 | \item \sQuote{inches} to \sQuote{mm}
48 | \item \sQuote{lb/ac} to \sQuote{kg/ha}
49 | \item \sQuote{kg/ha} to \sQuote{lb/ac}
50 | \item \sQuote{lb/ac} to \sQuote{g/m2}
51 | \item \sQuote{g/m2} to \sQuote{lb/ac}
52 | \item \sQuote{decimal} to \sQuote{degrees}
53 | \item \sQuote{degrees} to \sQuote{decimal}
54 | \item \sQuote{Fahrenheit} to \sQuote{Celsius}
55 | \item \sQuote{Celsius} to \sQuote{Fahrenheit}
56 | }
57 |
58 | This is for metric and Imperial conversions
59 | Source: https://www.extension.iastate.edu/agdm/wholefarm/html/c6-80.html
60 | }
61 | \examples{
62 | \donttest{
63 | grain.yield.gm2 <- 600
64 | grain.yield.kgha <- unit_conv(grain.yield.gm2, from = "g/m2", to = "kg/ha")
65 | grain.yield.kgha
66 | ## Converting coordinates
67 | require(sp)
68 | unit_conv("42d 0' 0\" N", from = "degrees", to = "decimal")
69 | unit_conv(42, from = "decimal", to = "degrees") ## EW by default
70 | unit_conv(42, from = "decimal", to = "degrees", NS = TRUE)
71 | }
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/man/plot.met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{plot.met}
4 | \alias{plot.met}
5 | \title{Plot method for object of class \sQuote{met}}
6 | \usage{
7 | \method{plot}{met}(
8 | x,
9 | ...,
10 | years,
11 | met.var,
12 | plot.type = c("ts", "area", "col", "density", "anomaly"),
13 | cumulative = FALSE,
14 | facet = FALSE,
15 | climatology = FALSE,
16 | summary = FALSE,
17 | by = c("year", "month")
18 | )
19 | }
20 | \arguments{
21 | \item{x}{object of class \sQuote{met}}
22 |
23 | \item{...}{additional arguments. None used at the moment.}
24 |
25 | \item{years}{optional argument to subset years}
26 |
27 | \item{met.var}{optional argument to choose a certain variable. By default,
28 | temperature (min and max) is displayed}
29 |
30 | \item{plot.type}{type of plot, default is \sQuote{ts} or time-series.
31 | The options \sQuote{area} and \sQuote{col} are only available when summary = TRUE.
32 | Option \sQuote{density} produces a simple plot. Option \sQuote{anomaly} ignores
33 | argument cumulative is treated as TRUE regardless.}
34 |
35 | \item{cumulative}{default is FALSE. Especially useful for \sQuote{rain}.}
36 |
37 | \item{facet}{whether to display the years in in different panels (facets). Not implemented yet.}
38 |
39 | \item{climatology}{logical (default FALSE). Whether to display the \sQuote{climatology}
40 | which would be the average of the data.
41 | Ideally, there are at least 20 years in the \sQuote{met} object.}
42 |
43 | \item{summary}{whether to plot \sQuote{summary} data. (default FALSE).}
44 |
45 | \item{by}{whether to perform summary by \sQuote{year} or \sQuote{month}.}
46 | }
47 | \description{
48 | Some plots are similar to APSIM, others are different
49 | and more useful in some respects
50 | }
51 | \examples{
52 | \donttest{
53 | ## Read in and plot a met file
54 | extd.dir <- system.file("extdata", package = "apsimx")
55 | ames <- read_apsim_met("Ames.met", src.dir = extd.dir)
56 | plot(ames, years = 2012:2015)
57 | ## Perhaps more informative
58 | plot(ames, years = 2012:2015, cumulative = TRUE)
59 | ## for rain
60 | plot(ames, met.var = "rain", years = 2012:2015, cumulative = TRUE)
61 | plot(ames, met.var = "rain", years = 2012:2015, cumulative = TRUE, climatology = TRUE)
62 | plot(ames, met.var = "rain", years = 2012:2015, plot.type = "anomaly")
63 | ## It is possible to add ggplot elements
64 | library(ggplot2)
65 | p1 <- plot(ames, met.var = "rain", years = 2012:2015, cumulative = TRUE)
66 | p1 + ggtitle("Cumulative rain for 2012-2015")
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/man/tt_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsim_met.R
3 | \name{tt_apsim_met}
4 | \alias{tt_apsim_met}
5 | \title{Calculates Thermal Time taking a \sQuote{met} object}
6 | \usage{
7 | tt_apsim_met(
8 | met,
9 | dates,
10 | method = c("Classic_TT", "HeatStress_TT", "CropHeatUnit_TT", "APSIM_TT", "CERES_TT",
11 | "all"),
12 | x_temp = c(0, 26, 34),
13 | y_tt = c(0, 26, 0),
14 | base_temp = 0,
15 | max_temp = 30,
16 | dates.format = c("\%d-\%m")
17 | )
18 | }
19 | \arguments{
20 | \item{met}{object of class \sQuote{met}}
21 |
22 | \item{dates}{when the calculation starts and when it ends. At the moment
23 | it needs to be a character vector (e.g. c(\sQuote{01-05}, \sQuote{10-10})). It will
24 | use the same dates every year for multiple years.}
25 |
26 | \item{method}{one of \sQuote{Classic_TT}, \sQuote{HeatStress_TT}, \sQuote{ASPIM_TT},
27 | \sQuote{CERES_TT} and \sQuote{all}}
28 |
29 | \item{x_temp}{cardinal temperatures (base, optimal and maximum)}
30 |
31 | \item{y_tt}{thermal time accumulation for cardinal temperatures}
32 |
33 | \item{base_temp}{base temperature for Classic TT calculation}
34 |
35 | \item{max_temp}{maximum temperature for Classic TT calculation}
36 |
37 | \item{dates.format}{default is \sQuote{\%d-\%m} which means day and month}
38 | }
39 | \value{
40 | it returns an object of class \sQuote{met} with additional columns
41 | \sQuote{Date} and the corresponding TT calculation
42 | }
43 | \description{
44 | Calculates Thermal Time using the \sQuote{Classic} formula,
45 | Heat Stress, Crop Heat Unit and other methods
46 | }
47 | \details{
48 | Calculating Thermal Time using a variety of methods.
49 | The function will fail if the method is not selected.
50 | Also, it does not work if each year does not have at least
51 | 365 days.
52 | }
53 | \examples{
54 | \dontrun{
55 | require(nasapower)
56 | require(ggplot2)
57 |
58 | pwr <- get_power_apsim_met(lonlat = c(-93,42), dates = c("2012-01-01","2015-12-31"))
59 | check_apsim_met(pwr)
60 | pwr <- impute_apsim_met(pwr)
61 |
62 | pwr2 <- tt_apsim_met(pwr, dates = c("01-05", "30-10"), method = c("Classic", "Heat"))
63 |
64 | ggplot(data = pwr2, aes(x = Date, y = Classic_TT)) + geom_point()
65 |
66 | ggplot(data = pwr2, aes(x = Date, y = HeatStress_TT)) + geom_point()
67 |
68 | }
69 | }
70 | \references{
71 | Abendroth, L.J., Miguez, F.E., Castellano, M.J. and Hatfield, J.L. (2019),
72 | Climate Warming Trends in the U.S. Midwest Using Four Thermal Models.
73 | Agron. J., 111: 3230-3243. (doi:10.2134/agronj2019.02.0118)
74 | }
75 |
--------------------------------------------------------------------------------
/man/edit_apsim_replace_soil_profile.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/edit_apsim_replace_soil_profile.R
3 | \name{edit_apsim_replace_soil_profile}
4 | \alias{edit_apsim_replace_soil_profile}
5 | \title{Edit APSIM \sQuote{Classic} file with a replaced soil profile}
6 | \usage{
7 | edit_apsim_replace_soil_profile(
8 | file = "",
9 | src.dir = ".",
10 | wrt.dir = NULL,
11 | soil.profile = NULL,
12 | swim = NULL,
13 | soilwat = NULL,
14 | initialwater = NULL,
15 | edit.tag = "-edited",
16 | overwrite = FALSE,
17 | verbose = TRUE,
18 | root
19 | )
20 | }
21 | \arguments{
22 | \item{file}{name of the .apsim file to be edited}
23 |
24 | \item{src.dir}{source directory}
25 |
26 | \item{wrt.dir}{writing directory}
27 |
28 | \item{soil.profile}{a soil profile object with class \sQuote{soil_profile}}
29 |
30 | \item{swim}{list with SWIM specific parameters}
31 |
32 | \item{soilwat}{list with SoilWat specific parameters}
33 |
34 | \item{initialwater}{list with InitialWater specific parameters}
35 |
36 | \item{edit.tag}{default edit tag \sQuote{-edited}}
37 |
38 | \item{overwrite}{default FALSE}
39 |
40 | \item{verbose}{default TRUE. Will print messages indicating what was done.}
41 |
42 | \item{root}{supply the node postion in the case of multiple simulations such as factorials.}
43 | }
44 | \value{
45 | writes an APSIM file to disk with the supplied soil profile
46 | }
47 | \description{
48 | Edits an APSIM Classic simulation by replacing the soil profile
49 | }
50 | \details{
51 | This function is designed to batch replace the whole soil in an APSIM simulation.
52 | }
53 | \note{
54 | There is no such thing as a default soil, carefully build the profile for each simulation.
55 | This function replaces values and it can grow an XML node, but it cannot edit a property
56 | which is not present in the original file.
57 | }
58 | \examples{
59 | \donttest{
60 | sp <- apsimx_soil_profile(nlayers = 20,
61 | crops = c("Barley", "Chickpea", "Lucerne",
62 | "Maize", "Perennial Grass", "Sorghum",
63 | "Wheat", "Millet"))
64 |
65 | extd.dir <- system.file("extdata", package = "apsimx")
66 |
67 | ## Writing to a temp directory
68 | tmp.dir <- tempdir()
69 | edit_apsim_replace_soil_profile("Millet.apsim", soil.profile = sp,
70 | edit.tag = "-newsoil",
71 | src.dir = extd.dir,
72 | wrt.dir = tmp.dir)
73 |
74 | inspect_apsim("Millet-newsoil.apsim", src.dir = tmp.dir,
75 | node = "Soil", soil.child = "Water")
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/man/sens_apsim.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/sens_apsim.R
3 | \name{sens_apsim}
4 | \alias{sens_apsim}
5 | \title{Sensitivity Analysis for APSIM Next Generation simulation}
6 | \usage{
7 | sens_apsim(
8 | file,
9 | src.dir = ".",
10 | crop.file,
11 | parm.paths,
12 | parm.vector.index,
13 | xml.parm,
14 | grid,
15 | summary = c("mean", "max", "var", "sd", "none"),
16 | root,
17 | verbose = TRUE,
18 | cores = 1L,
19 | save,
20 | ...
21 | )
22 | }
23 | \arguments{
24 | \item{file}{file name to be run (with extension .apsim)}
25 |
26 | \item{src.dir}{directory containing the .apsim file to be run (defaults to the current directory)}
27 |
28 | \item{crop.file}{name of auxiliary xml file where parameters are stored. If this is missing, it is
29 | assumed that the parameters to be edited are in the main simulation file.}
30 |
31 | \item{parm.paths}{absolute or relative paths of the coefficients to be evaluated.
32 | It is recommended that you use \code{\link{inspect_apsim}} for this}
33 |
34 | \item{parm.vector.index}{Index to evaluate a specific element of a parameter vector. At the moment it is
35 | possible to only edit one element at a time. This is because there is a conflict when generating multiple
36 | elements in the candidate vector for the same parameter.}
37 |
38 | \item{xml.parm}{TRUE or FALSE for each parameter. Indicating whether it is part of
39 | an xml file. Its length should be equal to the length or \sQuote{parm.paths}.}
40 |
41 | \item{grid}{grid of parameter values for the evaluation. It can be a data.frame.}
42 |
43 | \item{summary}{function name to use to summarize the output to be a sinlge row (default is the mean).}
44 |
45 | \item{root}{root argument for \code{\link{edit_apsim}}}
46 |
47 | \item{verbose}{whether to print progress in percent and elapsed time.}
48 |
49 | \item{cores}{number of cores to use for parallel evaluation}
50 |
51 | \item{save}{whether to save intermediate results. By default they will be saved as a
52 | \sQuote{csv} file using the name of the apsim file. This will replace \sQuote{apsim} with \sQuote{csv}.
53 | It is also possible to provide the file name here (for example: \sQuote{Some_results.csv}).}
54 |
55 | \item{...}{additional arguments (none used at the moment).}
56 | }
57 | \value{
58 | object of class \sQuote{sens_apsim}, but really just a list with results from the evaluations.
59 | }
60 | \description{
61 | It is a wrapper for running APSIM and evaluating different parameters values
62 | }
63 | \note{
64 | The summary function is stored as an attribute of the data frame \sQuote{grid.sims}.
65 | }
66 | \examples{
67 | \donttest{
68 | ## See the vignette for examples
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/man/get_ssurgo_soil_profile.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_ssurgo_soil_profile.R
3 | \name{get_ssurgo_soil_profile}
4 | \alias{get_ssurgo_soil_profile}
5 | \title{Retrieve soil profile data and convert it to an object of class \sQuote{soil_profile}}
6 | \usage{
7 | get_ssurgo_soil_profile(
8 | lonlat,
9 | shift = -1,
10 | nmapunit = 1,
11 | nsoil = 1,
12 | xout = NULL,
13 | soil.bottom = 200,
14 | method = c("constant", "linear"),
15 | nlayers = 10,
16 | check = TRUE,
17 | fix = FALSE,
18 | verbose = FALSE,
19 | xargs = NULL
20 | )
21 | }
22 | \arguments{
23 | \item{lonlat}{Longitude and latitude vector (e.g. c(-93, 42))}
24 |
25 | \item{shift}{simple mechanism for creating an area of interest by displacing the point indicated in
26 | lonlat by some amount of distance (e.g. 300 - in meters)}
27 |
28 | \item{nmapunit}{number of mapunits to select (see \code{\link{ssurgo2sp}})}
29 |
30 | \item{nsoil}{number of soils to select (see \code{\link{ssurgo2sp}}). If the
31 | number of soils is negative or NA it will fetch all the soils in the mapunit}
32 |
33 | \item{xout}{see \code{\link{ssurgo2sp}}}
34 |
35 | \item{soil.bottom}{see \code{\link{ssurgo2sp}}}
36 |
37 | \item{method}{interpolation method see \code{\link{ssurgo2sp}}}
38 |
39 | \item{nlayers}{number for layer for the new soil profile}
40 |
41 | \item{check}{whether to check for reasonable values using \code{\link{check_apsimx_soil_profile}}.
42 | TRUE by default. If \sQuote{fix} is TRUE, it will be applied only after the fix attempt.}
43 |
44 | \item{fix}{whether to fix compatibility between saturation and bulk density (default is FALSE).}
45 |
46 | \item{verbose}{default FALSE. Whether to print messages.}
47 |
48 | \item{xargs}{additional arguments passed to \code{\link{apsimx_soil_profile}} function.}
49 | }
50 | \value{
51 | this function will always return a list. Each element of the list will
52 | be an object of class \sQuote{soil_profile}
53 | }
54 | \description{
55 | Generate a synthetic soil profile based on the information in SSURGO database
56 | }
57 | \details{
58 | Data source is USDA-NRCS Soil Data Access. See package soilDB for more details
59 | }
60 | \examples{
61 | \dontrun{
62 | require(soilDB)
63 | require(sp)
64 | require(sf)
65 | require(spData)
66 | require(ggplot2)
67 | ## Soil inforation for a single point
68 | sp <- get_ssurgo_soil_profile(lonlat = c(-93, 42))
69 | ## The initial attempt throws warnings, so better to use 'fix'
70 | sp <- get_ssurgo_soil_profile(lonlat = c(-93, 42), fix = TRUE)
71 | plot(sp[[1]])
72 | plot(sp[[1]], property = "water")
73 | ## Add initial water
74 | iwat <- initialwater_parms(Thickness = sp[[1]]$soil$Thickness,
75 | InitialValues = sp[[1]]$soil$DUL * 0.8)
76 | sp[[1]]$initialwater <- iwat
77 | plot(sp[[1]], property = "initialwater")
78 | }
79 |
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/man/edit_apsim_xml.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/edit_apsim_xml.R
3 | \name{edit_apsim_xml}
4 | \alias{edit_apsim_xml}
5 | \title{Edit an APSIM (Classic) Simulation auxiliary xml file}
6 | \usage{
7 | edit_apsim_xml(
8 | file,
9 | src.dir = ".",
10 | wrt.dir = NULL,
11 | parm.path = NULL,
12 | value = NULL,
13 | overwrite = FALSE,
14 | edit.tag = "-edited",
15 | verbose = TRUE
16 | )
17 | }
18 | \arguments{
19 | \item{file}{file ending in .xml to be edited}
20 |
21 | \item{src.dir}{directory containing the .xml file to be edited; defaults to the current working directory}
22 |
23 | \item{wrt.dir}{should be used if the destination directory is different from the src.dir}
24 |
25 | \item{parm.path}{parameter path to be edited (see example)}
26 |
27 | \item{value}{new values for the parameter to be edited}
28 |
29 | \item{overwrite}{logical; if \code{TRUE} the old file is overwritten, a new file is written otherwise}
30 |
31 | \item{edit.tag}{if the file is edited a different tag from the default \sQuote{-edited} can be used.}
32 |
33 | \item{verbose}{whether to print information about successful edit}
34 | }
35 | \value{
36 | (when verbose=TRUE) complete file path to edited .xml file is returned as a character string.
37 | As a side effect this function creates a new XML file.
38 | }
39 | \description{
40 | This function allows editing of an APSIM (Classic) simulation xml file.
41 | }
42 | \details{
43 | The variables specified by \code{parm} within the .apsim file specified by \code{file}
44 | in the source directory \code{src.dir} are edited. The old values are replaced with \code{value}, which
45 | is a list that has the same number of elements as the length of the vector \code{parm}. The current
46 | .xml file will be overwritten if \code{overwrite} is set to \code{TRUE}; otherwise the file
47 | \sQuote{file} \emph{-edited.xml} will be created. If (verbose = TRUE) then the name
48 | of the written file is returned. The function is similar to the edit_sim_file functin in the \sQuote{apsimr}
49 | package, but with the difference that here the xml2 package is used instead.
50 | }
51 | \note{
52 | This function cannot check whether replacement is of the correct length. Also, there is
53 | an inspect equivalent. It is more flexible than \sQuote{edit_apsim} and (perhaps) similar to
54 | \sQuote{apsimr::edit_sim_file}.
55 | }
56 | \examples{
57 | \donttest{
58 | ## This example changes the RUE values
59 |
60 | extd.dir <- system.file("extdata", package = "apsimx")
61 |
62 | values <- paste(rep(1.7, 12), collapse = " ")
63 |
64 | ## Writing to a temp directory, but replace as needed
65 | tmp.dir <- tempdir()
66 |
67 | edit_apsim_xml("Maize75.xml",
68 | src.dir = extd.dir,
69 | wrt.dir = tmp.dir,
70 | parm.path = ".//Model/rue",
71 | value = values)
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/man/edit_apsimx_batch.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/edit_apsimx_batch.R
3 | \name{edit_apsimx_batch}
4 | \alias{edit_apsimx_batch}
5 | \title{Edit an APSIM-X (JSON) Simulation in Batch mode}
6 | \usage{
7 | edit_apsimx_batch(
8 | file,
9 | src.dir = ".",
10 | wrt.dir = NULL,
11 | parms = NULL,
12 | silent = FALSE,
13 | verbose = TRUE
14 | )
15 | }
16 | \arguments{
17 | \item{file}{file ending in .apsimx to be edited (JSON)}
18 |
19 | \item{src.dir}{directory containing the .apsimx file to be edited; defaults to the current working directory}
20 |
21 | \item{wrt.dir}{should be used if the destination directory is different from the src.dir}
22 |
23 | \item{parms}{parameter to be edited in the for of \sQuote{key = value}}
24 |
25 | \item{silent}{controls the output of running APSIM at the command line}
26 |
27 | \item{verbose}{whether to print information about successful edit}
28 | }
29 | \value{
30 | (when verbose=TRUE) complete file path to edited .apsimx file is returned as a character string.
31 | As a side effect this function creates a new (JSON) .apsimx file.
32 | }
33 | \description{
34 | This function allows editing of an APSIM-X (JSON) simulation file in batch mode.
35 | }
36 | \details{
37 | from hol430
38 |
39 | This allows the user to specify an .apsimx file and a config file when running Models.exe. The .apsimx file will not be run but instead, the changes listed in the config file will be applied to the .apsimx file, which will then be written to disk under the same filename.
40 |
41 | The config file should contain lines of the form \sQuote{path = value}
42 |
43 | e.g.
44 |
45 | [Clock].StartDate = 2019-1-20
46 | .Simulations.Sim1.Name = SimulationVariant35
47 | .Simulations.Sim2.Enabled = false
48 | .Simulations.Sim1.Paddock.Soil.Thickness[1] = 50
49 | Notes:
50 |
51 | Command line arguments should look like:
52 | Models.exe file.apsimx /Edit /path/to/config/file.conf
53 |
54 | Relative paths will be resolved to the first match. ie
55 | [Clock].StartDate will match the first clock found in the file.
56 |
57 | Dates can be specified as yyyy-mm-dd or mm/dd/yyyy.
58 |
59 | Strings should not be quoted
60 |
61 | Array indices will be interpted as 1-indexed (mad face). So the first
62 | element in the array should have index 1 in the config file.
63 |
64 | The file will be upgraded to the latest file version as part of this
65 | process.
66 | }
67 | \examples{
68 | \donttest{
69 | ## This example will read one of the examples distributed with APSIM-X
70 | ## but write to a temporary directory
71 |
72 | tmp.dir <- tempdir()
73 |
74 | ## Edit InitialResidueMass
75 | extd.dir <- system.file("extdata", package = "apsimx")
76 | parms <- list(`.Simulations.Simulation.Field.SurfaceOrganicMatter.InitialResidueMass` = 600)
77 | edit_apsimx_batch("Wheat.apsimx", src.dir = extd.dir, wrt.dir = tmp.dir, parms = parms)
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/tests/test_apsim_met_tt.R:
--------------------------------------------------------------------------------
1 | ## Testing the calculation of thermal time in APSIM
2 | require(apsimx)
3 | require(ggplot2)
4 |
5 | run.test.apsim.met.tt <- FALSE
6 |
7 | if(run.test.apsim.met.tt){
8 |
9 | tmp.dir <- tempdir()
10 |
11 | ex.dir <- auto_detect_apsimx_examples()
12 |
13 | file.copy(file.path(ex.dir, "Maize.apsimx"), tmp.dir)
14 |
15 | inspect_apsimx("Maize.apsimx", tmp.dir, node = "Report")
16 |
17 | edit_apsimx("Maize.apsimx", src.dir = tmp.dir,
18 | node = "Report", parm = "EventNames",
19 | value = "[Clock].EndOfDay", overwrite = TRUE)
20 |
21 | edit_apsimx("Maize.apsimx", src.dir = tmp.dir,
22 | node = "Report", parm = "VariableNames",
23 | value = "[Maize].Phenology.AccumulatedTT", overwrite = TRUE)
24 |
25 | inspect_apsimx("Maize.apsimx", src.dir = tmp.dir, node = "Manager",
26 | parm = list("SowingRule", NA))
27 |
28 | inspect_apsimx("Maize.apsimx", src.dir = tmp.dir, node = "Clock")
29 |
30 | sim0 <- apsimx("Maize.apsimx", src.dir = tmp.dir)
31 |
32 | ggplot(data = sim0, aes(x = Date, y = Maize.Phenology.AccumulatedTT)) +
33 | geom_line()
34 |
35 | ## Bring metfile here
36 | inspect_apsimx("Maize.apsimx", src.dir = tmp.dir, node = "Weather")
37 |
38 | file.copy(file.path(ex.dir, "WeatherFiles/Dalby.met"), tmp.dir)
39 |
40 | dalby <- read_apsim_met("Dalby.met", src.dir = tmp.dir)
41 |
42 | ## What is the emergence date for 1990
43 | sim0[sim0$Maize.Phenology.CurrentStageName == "Emergence",]
44 | sim0[sim0$Maize.Phenology.CurrentStageName == "HarvestRipe",]
45 |
46 | dalby.tt <- tt_apsim_met(dalby, dates = c("07-01", "28-05"), x_temp = c(10, 20, 30, 40), y_tt = c(0, 10, 20, 0))
47 |
48 | sim0$year <- as.numeric(format(sim0$Date, "%Y"))
49 | sim1991 <- subset(sim0, year == 1991)
50 |
51 | dalby.tt.1991 <- subset(dalby.tt, year == 1991)
52 |
53 | ggplot() +
54 | geom_line(data = sim1991, aes(x = Date, y = Maize.Phenology.AccumulatedTT, color = "APSIM")) +
55 | geom_line(data = dalby.tt.1991, aes(x = Date, y = APSIM_TT, color = "apsimx"))
56 |
57 |
58 | file.remove(file.path(tmp.dir, "Maize.apsimx"))
59 |
60 |
61 | }
62 |
63 | ## Test for napad and impute
64 | if(FALSE){
65 |
66 | dts <- c("1990-01-01", "2020-12-31")
67 | iem <- get_iem_apsim_met(lonlat = c(-93.77, 42), dates = dts)
68 | pwr <- get_power_apsim_met(lonlat = c(-93.77, 42), dates = dts)
69 |
70 | nrow(iem); nrow(pwr)
71 |
72 | dmt <- get_daymet2_apsim_met(lonlat = c(-93.77, 42), years = c(1990, 2020))
73 | nrow(iem); nrow(pwr); nrow(dmt)
74 |
75 | check_apsim_met(iem)
76 | check_apsim_met(pwr)
77 | check_apsim_met(dmt)
78 |
79 | ## Fix IEM
80 | iem[iem$radn < 0, "radn"] <- NA
81 | ## The problem is that met radn has a lot of NA's
82 | iem2 <- impute_apsim_met(iem)
83 | check_apsim_met(pwr)
84 |
85 | dmt2 <- napad_apsim_met(dmt)
86 | dmt3 <- impute_apsim_met(dmt2)
87 | check_apsim_met(dmt3)
88 | }
89 |
90 |
--------------------------------------------------------------------------------
/tests/test_extract_values_apsimx.R:
--------------------------------------------------------------------------------
1 | ## Testing the function for extracting values from an arbitrary .apsimx file
2 | ## using extract_value_apsimx
3 | require(apsimx)
4 |
5 | run.extract.value.apsimx <- FALSE
6 |
7 | if(run.extract.value.apsimx){
8 |
9 | ## Testing with the Barley.apsimx example
10 | ex.dir <- auto_detect_apsimx_examples()
11 |
12 | pp1 <- inspect_apsimx("Barley.apsimx", src.dir = ex.dir)
13 | ## Extract value of Start
14 | extract_values_apsimx("Barley.apsimx", src.dir = ex.dir,
15 | parm.path = paste0(pp1, ".Start"))
16 | ## Extract value of End
17 | extract_values_apsimx("Barley.apsimx", src.dir = ex.dir,
18 | parm.path = paste0(pp1, ".End"))
19 | ## Weather
20 | pp2 <- inspect_apsimx("Barley.apsimx", src.dir = ex.dir,
21 | node = "Weather")
22 | ## Extract weather file
23 | extract_values_apsimx("Barley.apsimx", src.dir = ex.dir,
24 | parm.path = paste0(pp2, ".FileName"))
25 | ## Manager
26 | pp3 <- inspect_apsimx("Barley.apsimx", src.dir = ex.dir,
27 | node = "Manager", parm = list("Sowing", 1))
28 | ## Extract fertilizer amount
29 | extract_values_apsimx("Barley.apsimx", src.dir = ex.dir,
30 | parm.path = paste0(pp3, ".Amount"))
31 | ## Row Spacing
32 | pp4 <- inspect_apsimx("Barley.apsimx", src.dir = ex.dir,
33 | node = "Manager", parm = list("fixed", 5))
34 | ## Extract the row spacing
35 | ex.rs <- extract_values_apsimx("Barley.apsimx", src.dir = ex.dir,
36 | parm.path = paste0(pp4, ".RowSpacing"))
37 |
38 | ## What about vectors?
39 | pp5 <- inspect_apsimx("Barley.apsimx", src.dir = ex.dir, node = "Soil",
40 | soil.child = "Physical", parm = "DUL")
41 | ## Extract DUL
42 | ex.dul <- extract_values_apsimx("Barley.apsimx", src.dir = ex.dir,
43 | parm.path = paste0(pp5, ".DUL"))
44 |
45 | ## On the replacement example
46 | extd.dir <- system.file("extdata", package = "apsimx")
47 | pp6 <- inspect_apsimx_replacement("MaizeSoybean.apsimx", src.dir = extd.dir,
48 | node = "Soybean",
49 | node.child = "Leaf", node.subchild = "ExtinctionCoeff",
50 | node.subsubchild = "XYPairs", parm = "Y")
51 | ## Extract it
52 | ext.exc <- extract_values_apsimx("MaizeSoybean.apsimx", src.dir = extd.dir,
53 | parm.path = pp6)
54 | ## Try RUE
55 | pp7 <- inspect_apsimx_replacement("MaizeSoybean.apsimx", src.dir = extd.dir,
56 | node = "Soybean",
57 | node.child = "Leaf", node.subchild = "Photosynthesis",
58 | node.subsubchild = "RUE", parm = "FixedValue")
59 | ext.rue <- extract_values_apsimx("MaizeSoybean.apsimx", src.dir = extd.dir, parm.path = pp7)
60 | }
--------------------------------------------------------------------------------
/R/zzz.R:
--------------------------------------------------------------------------------
1 | ## Function to check whether mono (unix only) and APSIM-X are installed
2 |
3 | .onAttach <- function(libname, pkgname){
4 |
5 | find.mono <- NULL
6 |
7 | find.apsim <- NULL
8 | ## For unix
9 | if(.Platform$OS.type == "unix"){
10 | ## Check for mono
11 | mono <- suppressWarnings(system("which mono", intern = TRUE, ignore.stderr = TRUE))
12 | if(length(mono) == 0){
13 | find.mono <- "Mono framework not found (only needed for pre-Sept 2021 versions of APSIM Next Gen) \n"
14 | ## In Ubuntu mono seems to be in /usr/bin/mono
15 | ## In Mac mono seems to be installed in:
16 | ## "/Library/Frameworks/Mono.framework/Versions/Current/Commands/mono"
17 | ## In both of them the command 'mono' seems to work
18 | }
19 | ## Check for dotnet
20 | ## This is not necessary any more
21 | # dotnet <- system("which dotnet", intern = TRUE)
22 | # if(length(dotnet) == 0){
23 | # find.dotnet <- "dotnet core framework not found (needed Sept 2021 -or later- versions of APSIM Next Gen) \n"
24 | # ## In Ubuntu mono seems to be in /usr/bin/mono
25 | # ## In Mac mono seems to be installed in:
26 | # ## "/usr/local/bin/dotnet"
27 | # ## In both of them the command 'mono' seems to work
28 | # }
29 |
30 | ## This is for solaris or any other flavor of unix
31 | laf <- character(0)
32 | ## List of application files in Darwin (Mac)
33 | if(grepl("Darwin", Sys.info()[["sysname"]])){
34 | laf <- list.files("/Applications/")
35 | }
36 | ## Linux (Debian)
37 | if(grepl("Linux", Sys.info()[["sysname"]])){
38 | laf <- list.files("/usr/local/lib")
39 | }
40 | }else{
41 | if(grepl("Windows", Sys.info()[["sysname"]])){
42 | laf <- list.files("C:/PROGRA~1")
43 | laf2 <- list.files("C:/PROGRA~2")
44 | }
45 | }
46 | ## Check whether APSIM-X is detected
47 | if(!grepl("Windows", Sys.info()[["sysname"]])){
48 | find.apsimx <- laf[grepl("APSIM", laf, ignore.case = TRUE)]
49 | }else{
50 | ## This is the location for APSIM-X
51 | find.apsimx <- laf[grepl("APSIM", laf, ignore.case = TRUE)]
52 | ## This is the location for APSIM "Classic"
53 | find.apsim <- laf2[grepl("APSIM", laf2, ignore.case = TRUE)]
54 | }
55 |
56 | if(length(find.apsimx) > 0 || length(find.apsim) > 0){
57 |
58 | fax <- paste0("Found APSIM or APSIM-X")
59 | ## Won't print ApsimX at the moment but might change it in the future
60 | ## packageStartupMessage(fax)
61 |
62 | }else{
63 |
64 | apsim.not.found <- "APSIM(X) not found.
65 | If APSIM(X) is installed in an alternative location,
66 | set paths manually using 'apsimx_options' or 'apsim_options'.
67 | You can still try as the package will look into the registry (under Windows)"
68 |
69 | ## if(length(find.mono) == 0) apsim.not.found <- c(find.mono, apsim.not.found)
70 |
71 | packageStartupMessage(apsim.not.found)
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/man/summary.met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/compare_apsim_met.R
3 | \name{summary.met}
4 | \alias{summary.met}
5 | \title{Summary for an APSIM met file}
6 | \usage{
7 | \method{summary}{met}(
8 | object,
9 | ...,
10 | years,
11 | months,
12 | days,
13 | julian.days,
14 | by = c("year", "month"),
15 | compute.frost = FALSE,
16 | frost.temperature = 0,
17 | anomaly,
18 | anomaly.units = c("percent", "standard.deviation", "absolute"),
19 | check = FALSE,
20 | verbose = FALSE,
21 | na.rm = FALSE,
22 | digits = 2
23 | )
24 | }
25 | \arguments{
26 | \item{object}{object of class \sQuote{met}}
27 |
28 | \item{...}{optional argument (none used at the moment)}
29 |
30 | \item{years}{optional argument to subset years}
31 |
32 | \item{months}{optional argument to subset by months. If an integer, it should
33 | be between 1 and 12. If a character, it can be in the format, for example,
34 | \sQuote{jan} or \sQuote{Jan}.}
35 |
36 | \item{days}{optional argument to subset by days. It should be an integer
37 | between 1 and 31.}
38 |
39 | \item{julian.days}{optional argument to subset by julian days. It
40 | should be a vector of integers between 1 and 365. Either use \sQuote{days} or
41 | \sQuote{julian.days} but not both.}
42 |
43 | \item{by}{by default the summary is performed by \sQuote{year}. This allows
44 | the use of \sQuote{month}.}
45 |
46 | \item{compute.frost}{logical (default FALSE). Whether to compute
47 | frost statistics.}
48 |
49 | \item{frost.temperature}{value to use for the calculation of the frost
50 | period (default is zero).}
51 |
52 | \item{anomaly}{whether to compute the anomaly. Default is FALSE.
53 | It could be TRUE (for all variables) or a character vector for a specific set of variables.}
54 |
55 | \item{anomaly.units}{what units to use for anomaly calculations. Default is \sQuote{percent}.
56 | Other options are \sQuote{standard.devaition} and \sQuote{absolute} - this means the difference,
57 | which will be in the original units.}
58 |
59 | \item{check}{logical (default FALSE). Whether to \sQuote{check} the \sQuote{met} object.}
60 |
61 | \item{verbose}{whether to print additional infomation to the console}
62 |
63 | \item{na.rm}{whether to remove missing values. Passed to \sQuote{aggregate}}
64 |
65 | \item{digits}{digits for rounding (default is 2).}
66 | }
67 | \value{
68 | an object of class \sQuote{data.frame} with attributes
69 | }
70 | \description{
71 | Create a data.frame summarizing an object of class \sQuote{met}
72 | }
73 | \details{
74 | The frost free period is computed by first spliting each year (or year interval)
75 | in two halves. The first and last frosts in the first and second period are found.
76 | For the Northern hemisphere calendar days are used (1-365).
77 | For the Southern hemisphere the year is split in two halfs, but the second half of
78 | the year is used as the first part of the growing season.
79 | If frost is not found a zero is returned.
80 | }
81 | \examples{
82 |
83 | extd.dir <- system.file("extdata", package = "apsimx")
84 | ames <- read_apsim_met("Ames.met", src.dir = extd.dir)
85 |
86 | summary(ames, years = 2014:2016)
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/man/compare_apsim_soil_profile.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/apsimx_soil_profile.R
3 | \name{compare_apsim_soil_profile}
4 | \alias{compare_apsim_soil_profile}
5 | \alias{print.soil_profile_mrg}
6 | \alias{plot.soil_profile_mrg}
7 | \title{Compare two or more soil profiles}
8 | \usage{
9 | compare_apsim_soil_profile(
10 | ...,
11 | soil.var = c("all", "Thickness", "BD", "AirDry", "LL15", "DUL", "SAT", "KS", "Carbon",
12 | "SoilCNRatio", "FOM", "FOM.CN", "FBiom", "FInert", "NO3N", "NH4N", "PH",
13 | "ParticleSizeClay", "ParticleSizeSilt", "ParticleSizeSand"),
14 | property,
15 | labels,
16 | merge.wide = TRUE,
17 | check = FALSE,
18 | verbose = FALSE
19 | )
20 |
21 | \method{print}{soil_profile_mrg}(x, ..., format = c("wide", "long"), digits = 2)
22 |
23 | \method{plot}{soil_profile_mrg}(
24 | x,
25 | ...,
26 | plot.type = c("depth", "vs", "diff", "density"),
27 | pairs = NULL,
28 | soil.var = c("all", "Thickness", "BD", "AirDry", "LL15", "DUL", "SAT", "KS", "Carbon",
29 | "SoilCNRatio", "FOM", "FOM.CN", "FBiom", "FInert", "NO3N", "NH4N", "PH",
30 | "ParticleSizeClay", "ParticleSizeSilt", "ParticleSizeSand"),
31 | property,
32 | span = 0.75
33 | )
34 | }
35 | \arguments{
36 | \item{...}{\sQuote{soil_profile} objects. Should be of class \sQuote{soil_profile}}
37 |
38 | \item{soil.var}{soil variable to plot}
39 |
40 | \item{property}{same as soil.var}
41 |
42 | \item{labels}{labels for plotting and identification of \sQuote{soil_profile} objects.}
43 |
44 | \item{merge.wide}{whether to attempt to merge soils in \sQuote{wide} format.}
45 |
46 | \item{check}{whether to check \sQuote{soil_profile} objects using \sQuote{check_apsimx_soil_profile}.}
47 |
48 | \item{verbose}{whether to print agreement values (default is FALSE).}
49 |
50 | \item{x}{object of class \sQuote{soil_profile_mrg}}
51 |
52 | \item{format}{either \sQuote{wide} or \sQuote{long}, which depends on the merging.}
53 |
54 | \item{digits}{number of digits to print (default is 2)}
55 |
56 | \item{plot.type}{either \sQuote{depth}, \sQuote{vs}, \sQuote{diff} or \sQuote{density}}
57 |
58 | \item{pairs}{pair of objects to compare, defaults to 1 and 2 but others are possible}
59 |
60 | \item{span}{argument to be passed to \sQuote{geom_smooth}}
61 | }
62 | \value{
63 | object of class \sQuote{soil_profile_mrg}, which can be used for further plotting
64 |
65 | a table with indexes for the soil profiles
66 |
67 | it produces a plot
68 | }
69 | \description{
70 | Helper function which allows for a simple comparison among soil_profile objects
71 |
72 | print method for \sQuote{soil_profile_mrg}
73 |
74 | plotting function for compare_apsim_soil_profile, it requires ggplot2
75 | }
76 | \note{
77 | I have only tested this for 2 or 3 objects. The code is set up to be able to
78 | compare more, but I'm not sure that would be all that useful.
79 | }
80 | \examples{
81 | \dontrun{
82 | require(soilDB)
83 | require(sp)
84 | require(sf)
85 | require(spData)
86 | # Get two soil profiles
87 | sp1 <- get_ssurgo_soil_profile(lonlat = c(-93, 42))
88 | sp2 <- get_ssurgo_soil_profile(lonlat = c(-92, 41))
89 | # Compare them
90 | cmp <- compare_apsim_soil_profile(sp1[[1]], sp2[[1]], labels = c("sp1", "sp2"))
91 | # Plot the variables
92 | plot(cmp)
93 | }
94 |
95 | }
96 |
--------------------------------------------------------------------------------
/tests/test_compare_apsim.R:
--------------------------------------------------------------------------------
1 | ## Testing the compare_apsim function
2 | require(apsimx)
3 | packageVersion("apsimx")
4 | require(ggplot2)
5 | apsimx_options(warn.versions = FALSE)
6 |
7 | run.test.compare.apsimx <- get(".run.local.tests", envir = apsimx.options)
8 |
9 | extd.dir <- system.file("extdata", package = "apsimx")
10 |
11 | if(run.test.compare.apsimx){
12 |
13 | data(obsWheat)
14 | sim.opt <- read.csv(file.path(extd.dir, "wheat-sim-opt.csv"))
15 | sim.opt$Date <- as.Date(sim.opt$Date)
16 |
17 | ## Testing compare_apsim
18 | cap <- compare_apsim(obsWheat, sim.opt, labels = c("obs", "sim"))
19 |
20 | compare_apsim(obsWheat, sim.opt, labels = c("obs", "sim"), verbose = TRUE)
21 |
22 | plot(cap)
23 | plot(cap, plot.type = "diff")
24 | plot(cap, plot.type = "ts")
25 |
26 | plot(cap, variable = "AboveGround")
27 | plot(cap, variable = "AboveGround", plot.type = "diff")
28 | plot(cap, variable = "AboveGround", plot.type = "resid")
29 | plot(cap, variable = "AboveGround", plot.type = "ts")
30 |
31 | ## Testing the id functionality ----
32 | plot(cap, variable = "AboveGround", plot.type = "resid", id = 0.05)
33 | plot(cap, variable = "AboveGround", plot.type = "resid", id = 0.05,
34 | id.label = letters[1:10])
35 | plot(cap, variable = "AboveGround", plot.type = "ts", id = 0.05)
36 | plot(cap, variable = "AboveGround", plot.type = "ts", id = 0.05,
37 | id.label = letters[1:10])
38 |
39 | ## Adding the capability when the length of the index is equal to 2
40 | obsPheno <- read.csv("~/Dropbox/apsimx-other/pheno_optim/obsPheno.csv")
41 | simPheno <- read.csv("~/Dropbox/apsimx-other/pheno_optim/simPheno.csv")
42 |
43 | obsPheno$Date <- as.Date(obsPheno$Date)
44 | simPheno$Date <- as.Date(simPheno$Date)
45 |
46 | cmp <- compare_apsim(obsPheno, simPheno, index = c("report", "Date"), labels = c("Obs", "Sim"))
47 |
48 | plot(cmp)
49 | plot(cmp, by = "report")
50 | plot(cmp, by = "report", se = FALSE)
51 | plot(cmp, by = "report", se = FALSE, facet = TRUE)
52 | plot(cmp, by = "report", plot.type = "ts", facet = TRUE, se = FALSE)
53 |
54 | plot(cmp, by = "report", plot.type = "ts", facet = TRUE, se = FALSE) +
55 | ggplot2::theme(legend.position = "top")
56 |
57 | ### Testing id feature ----
58 | plot(cmp, by = "report", se = FALSE, id = 0.05, facet = TRUE,
59 | id.label = obsPheno$Date)
60 |
61 | }
62 |
63 | if(FALSE){
64 |
65 | ### Testing inspired by a more complicated dataset?
66 | sim.wheat <- read_apsim_all(src.dir = "~/Dropbox/apsimx-other/KeLiu/mcmc_test/mcmc_test")
67 | sim.wheat$outfile <- as.factor(sim.wheat$outfile)
68 | ## obs.wheat <- as.data.frame(readxl::read_excel("~/Dropbox/apsimx-other/KeLiu/mcmc_test/mcmc_test/Obs.xlsx"))
69 | obs.wheat$outfile <- as.factor(obs.wheat$outfile)
70 | obs.wheat$Date <- as.Date(obs.wheat$Date)
71 |
72 | cmp1 <- compare_apsim(obs.wheat, sim.wheat,
73 | labels = c("obs", "sim"))
74 |
75 | cmp1 <- compare_apsim(obs.wheat, sim.wheat, index = c("outfile", "Date"),
76 | labels = c("obs", "sim"))
77 |
78 | plot(cmp1)
79 | plot(cmp1, by = "outfile")
80 | plot(cmp1, by = "outfile", se = FALSE)
81 | plot(cmp1, by = "outfile", plot.type = "ts", facet = TRUE, se = FALSE)
82 |
83 | }
--------------------------------------------------------------------------------
/tests/test_ssurgo2sp.R:
--------------------------------------------------------------------------------
1 | ## Testing the essence of the ssurgo workflow
2 | require(apsimx)
3 | require(sf)
4 | apsimx_options(warn.versions = FALSE)
5 |
6 | extd.dir <- system.file("extdata", package = "apsimx")
7 |
8 | run.test.ssurgo.workflow <- get(".run.local.tests", envir = apsimx.options)
9 |
10 | tmp.dir <- tempdir()
11 |
12 | if(run.test.ssurgo.workflow){
13 |
14 | ## Read the main files
15 | chorizon <- read.csv(paste0(extd.dir,"/ISUAG/SSURGO/ISUAG_SSURGO_chorizon.csv"))
16 | component <- read.csv(paste0(extd.dir,"/ISUAG/SSURGO/ISUAG_SSURGO_component.csv"))
17 | mapunit <- read.csv(paste0(extd.dir,"/ISUAG/SSURGO/ISUAG_SSURGO_mapunit.csv"))
18 | mapunit.shp <- sf::st_read(paste0(extd.dir,"/ISUAG/SSURGO/ISUAG_SSURGO_Mapunits.shp"), quiet = TRUE)
19 |
20 | ## Get tables from SSURGO
21 | srgtbls <- get_ssurgo_tables(lonlat = c(-93, 42))
22 | chorizon <- srgtbls$chorizon
23 | component <- srgtbls$component
24 | mapunit <- srgtbls$mapunit
25 | mapunit.shp <- srgtbls$mapunit.shp
26 |
27 | sp <- ssurgo2sp(mapunit = mapunit,
28 | component = component,
29 | chorizon = chorizon,
30 | mapunit.shp = mapunit.shp,
31 | nlayers = 20)
32 |
33 | ## For first soil
34 | metadata <- attributes(sp[[1]])
35 | metadata$names <- NULL; metadata$class <- NULL; metadata$row.names <- NULL;
36 |
37 | asp <- apsimx_soil_profile(nlayers = 20,
38 | Thickness = sp[[1]]$Thickness * 10,
39 | BD = sp[[1]]$BD,
40 | AirDry = sp[[1]]$AirDry,
41 | LL15 = sp[[1]]$LL15,
42 | DUL = sp[[1]]$DUL,
43 | SAT = sp[[1]]$SAT,
44 | KS = sp[[1]]$KS,
45 | Carbon = sp[[1]]$Carbon,
46 | crop.LL = sp[[1]]$LL15,
47 | soil.bottom = 200,
48 | metadata = metadata)
49 |
50 | check_apsimx_soil_profile(asp)
51 |
52 | edit_apsimx_replace_soil_profile("WheatRye.apsimx",
53 | src.dir = extd.dir,
54 | wrt.dir = tmp.dir,
55 | soil.profile = asp,
56 | edit.tag = "-sp")
57 |
58 | inspect_apsimx("WheatRye-sp.apsimx", src.dir = tmp.dir, node = "Soil")
59 |
60 | inspect_apsimx("WheatRye-sp.apsimx", src.dir = tmp.dir,
61 | node = "Soil", soil.child = "Physical")
62 |
63 | ## If I want to actually run the model
64 | file.copy(paste0(extd.dir,"/Ames.met"),tmp.dir)
65 |
66 | inspect_apsimx("WheatRye-sp.apsimx", src.dir = tmp.dir, node = "Clock")
67 |
68 | sim <- apsimx("WheatRye-sp.apsimx", src.dir = tmp.dir, value = "report")
69 |
70 | }
71 |
72 | if(run.test.ssurgo.workflow){
73 |
74 | ## Testing the shift feature
75 | stbls <- get_ssurgo_tables(lonlat = c(-82.97, 40.739), shift = 300)
76 | stbls$mapunit$mukey
77 |
78 | sp0 <- ssurgo2sp(mapunit = stbls$mapunit, component = stbls$component,
79 | chorizon = stbls$chorizon, mapunit.shp = stbls$mapunit.shp)
80 |
81 | stbls.sp <- get_ssurgo_soil_profile(lonlat = c(-82.97, 40.739), shift = 300)
82 |
83 |
84 | }
--------------------------------------------------------------------------------
/man/compare_apsim_met.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/compare_apsim_met.R
3 | \name{compare_apsim_met}
4 | \alias{compare_apsim_met}
5 | \alias{print.met_mrg}
6 | \alias{plot.met_mrg}
7 | \title{Compare two or more \sQuote{met} files}
8 | \usage{
9 | compare_apsim_met(
10 | ...,
11 | met.var = c("all", "radn", "maxt", "mint", "rain", "rh", "wind_speed", "vp"),
12 | labels,
13 | check = FALSE,
14 | verbose = FALSE
15 | )
16 |
17 | \method{print}{met_mrg}(x, ..., digits = 2)
18 |
19 | \method{plot}{met_mrg}(
20 | x,
21 | ...,
22 | plot.type = c("vs", "diff", "ts", "density"),
23 | pairs = c(1, 2),
24 | cumulative = FALSE,
25 | met.var = c("radn", "maxt", "mint", "rain"),
26 | id,
27 | span = 0.75
28 | )
29 | }
30 | \arguments{
31 | \item{...}{met file objects. Should be of class \sQuote{met}}
32 |
33 | \item{met.var}{meteorological variable to plot}
34 |
35 | \item{labels}{labels for plotting and identification of \sQuote{met} objects.}
36 |
37 | \item{check}{whether to check \sQuote{met} objects using \sQuote{check_apsim_met}.}
38 |
39 | \item{verbose}{whether to print agreement stats to console (default is FALSE).}
40 |
41 | \item{x}{object of class \sQuote{met_mrg}}
42 |
43 | \item{digits}{digits to print (default is 2)}
44 |
45 | \item{plot.type}{either \sQuote{vs}, \sQuote{diff}, \sQuote{ts} - for time series or \sQuote{density}}
46 |
47 | \item{pairs}{pair of objects to compare, defaults to 1 and 2 but others are possible}
48 |
49 | \item{cumulative}{whether to plot cumulative values (default FALSE)}
50 |
51 | \item{id}{identification (not implemented yet)}
52 |
53 | \item{span}{argument to be passed to \sQuote{geom_smooth}}
54 | }
55 | \value{
56 | object of class \sQuote{met_mrg}, which can be used for further plotting
57 |
58 | it prints the index.table data.frame
59 |
60 | it produces a plot
61 | }
62 | \description{
63 | Helper function which allows for a simple comparison among \sQuote{met} objects
64 |
65 | print method for \sQuote{met_mrg}
66 |
67 | plotting function for compare_apsim_met, it requires ggplot2
68 | }
69 | \details{
70 | If the \sQuote{met} files do not have the same number of columns or the same
71 | names for the data frame, then an error will be returned. If the years are not
72 | the same a message will be returned with the different years and the years
73 | in common.
74 | }
75 | \note{
76 | I have only tested this for 2 or 3 objects. The code is set up to be able to
77 | compare more, but I'm not sure that would be all that useful.
78 | }
79 | \examples{
80 | \dontrun{
81 | require(nasapower)
82 | ## Specify the location
83 | lonlat <- c(-93, 42)
84 | ## dates
85 | dts <- c("2017-01-01","2017-12-31")
86 | ## Get pwr
87 | pwr <- get_power_apsim_met(lonlat = lonlat, dates = dts)
88 | ## Get data from IEM
89 | iem <- get_iem_apsim_met(lonlat = lonlat, dates = dts)
90 | ## Compare them
91 | cmet <- compare_apsim_met(pwr[,1:6], iem, labels = c("pwr","iem"))
92 | ## Visualize radiation
93 | plot(cmet, met.var = "radn")
94 | plot(cmet, plot.type = "diff")
95 | plot(cmet, plot.type = "ts")
96 | ## Visualize maxt
97 | plot(cmet, met.var = "maxt")
98 | plot(cmet, met.var = "maxt", plot.type = "diff")
99 | plot(cmet, met.var = "maxt", plot.type = "ts")
100 | ## Cumulative rain
101 | plot(cmet, met.var = "rain", plot.type = "ts", cumulative = TRUE)
102 | }
103 |
104 | }
105 |
--------------------------------------------------------------------------------
/tests/test_check_apsimx.R:
--------------------------------------------------------------------------------
1 | ## Testing the check_apsimx function with a variety of files
2 | require(apsimx)
3 | packageVersion("apsimx")
4 | apsimx_options(warn.versions = FALSE, allow.path.spaces = TRUE)
5 |
6 | run.apsimx.check <- get(".run.local.tests", envir = apsimx.options)
7 |
8 | if(run.apsimx.check){
9 |
10 | ex.dir <- auto_detect_apsimx_examples()
11 | ex.dir.list <- dir(ex.dir, pattern = "apsimx$")
12 |
13 | for(.j in seq_along(ex.dir.list)){
14 |
15 | file.to.check <- ex.dir.list[.j]
16 | if(file.to.check == "Slurp.apsimx") next
17 |
18 | if(.j %in% c(2, 4, 5, 16, 17, 18, 24, 29, 30, 34)){
19 | cat("Simulation check:", file.to.check, "\n")
20 | check_apsimx(file.to.check, ex.dir)
21 | }
22 |
23 | if(.j %in% c(10, 11)){
24 | ## Simulation 10 is eucalyptus
25 | cat("Simulation to inspect:", file.to.check, "\n")
26 | capo <- capture.output(inspect_apsimx(file.to.check, ex.dir, node = "Other", parm = 3))
27 | }
28 |
29 | if(.j %in% c(6, 7, 8, 9)){
30 | ## Simulation 6 is chickpea
31 | ## Simulation 7 is chicory
32 | ## Simulation 8 is controlledenvironment
33 | ## Simulation 9 is csvweather
34 | cat("Simulation check:", file.to.check, "\n")
35 | capo <- capture.output(inspect_apsimx(file.to.check, ex.dir, node = "Other", parm = 3))
36 | if(file.to.check == "Chickpea.apsimx"){
37 | inspect_apsimx(file.to.check, ex.dir, root = "Continuous_TOS.Cont_TOS")
38 | inspect_apsimx(file.to.check, ex.dir, node = "Soil",
39 | soil.child = "InitialWater",
40 | root = "Continuous_TOS.Cont_TOS")
41 | check_apsimx(file.to.check, ex.dir, root = "Continuous_TOS.Cont_TOS")
42 | }
43 | if(file.to.check %in% c("Chicory.apsimx", "ControlledEnvironment.apsimx", "CsvWeather.apsimx")){
44 | inspect_apsimx(file.to.check, ex.dir, root = "Simulation")
45 | inspect_apsimx(file.to.check, ex.dir, node = "Soil",
46 | soil.child = "InitialWater",
47 | root = "Simulation")
48 | check_apsimx(file.to.check, ex.dir, root = "Simulation")
49 | }
50 | }
51 |
52 | if(.j %in% c(1, 3)){
53 | if(file.to.check == "BiomassRemovalFromPlant.apsimx"){
54 | check_apsimx(file.to.check, ex.dir, root = "UsingFixedDates")
55 | check_apsimx(file.to.check, ex.dir, root = "SendingDatesFromOpperations")
56 | }
57 | if(file.to.check == "AgPasture.apsimx"){
58 | pps <- inspect_apsimx(file.to.check, src.dir = ex.dir, node = "Other", parm = list(1, 3:7), print.path = TRUE)
59 | for(.jj in pps){
60 | rut <- strsplit(.jj, ".", fixed = TRUE)[[1]][3]
61 | if(rut == "Harvested Pasture by Production Year") next
62 | if(rut == "PastureByWaterAndNitrogen") next
63 | if(rut == "CO2xBaseTemperature") next
64 | if(rut == "CO2xHeatOnset ") next
65 | check_apsimx(file.to.check, ex.dir, root = rut)
66 | }
67 | }
68 | }
69 | }
70 | }
71 |
72 | #### Check files in extd.dir
73 |
74 | if(run.apsimx.check){
75 |
76 | ### Files that make check throw an error
77 | ### 1. MaizeSoybean.apsimx
78 | extd.dir <- system.file("extdata", package = "apsimx")
79 |
80 | apsimx.files.to.check <- dir(extd.dir, pattern = "apsimx")
81 |
82 | # for(.ii in seq_along(apsimx.files.to.check)){
83 | # ### Some of these files need 'root'
84 | # if(apsimx.files.to.check[[.ii]] == "Factorial.apsimx"){
85 | # for(.jj in )
86 | # }
87 | # check_apsimx(apsimx.files.to.check[[.ii]], extd.dir)
88 | # }
89 | }
--------------------------------------------------------------------------------
/tests/test_apsimx_factorial.R:
--------------------------------------------------------------------------------
1 | ## A few steps
2 | require(apsimx)
3 | apsimx_options(warn.versions = FALSE)
4 |
5 | ## Only write to a temp directory
6 | tmp.dir <- tempdir()
7 |
8 | ## These tests can only be run locally
9 | run.factorial.example <- get(".run.local.tests", envir = apsimx.options)
10 |
11 | if(run.factorial.example){
12 |
13 | ex.dir <- auto_detect_apsimx_examples()
14 |
15 | ## Copy 'Factorial' example to current directory
16 | fact.ex <- paste0(ex.dir, "/Factorial.apsimx")
17 | file.exists(fact.ex)
18 | file.copy(from = fact.ex,
19 | to = tmp.dir, copy.mode = FALSE)
20 |
21 | ## I ran these commands at the command line
22 | ## sed -i'.bak' -e 's/\bWeatherFiles/%root%\\\\\\\\Examples\\\\\\\\WeatherFiles/g *
23 |
24 | inspect_apsimx_replacement("Factorial", src.dir = tmp.dir,
25 | root = list("Experiment",1),
26 | node = "Base",
27 | node.child = "Weather")
28 | for(i in 1:5){
29 | edit_apsimx_replacement("Factorial",
30 | src.dir = tmp.dir,
31 | wrt.dir = tmp.dir,
32 | root = list("Experiment",i),
33 | node = "Base",
34 | node.child = "Weather",
35 | parm = "FileName",
36 | overwrite = TRUE,
37 | verbose = TRUE,
38 | value = paste0(ex.dir,"/WeatherFiles/lincoln.met"))
39 | }
40 |
41 | inspect_apsimx_replacement("Factorial", src.dir = tmp.dir,
42 | root = list("Experiment",2),
43 | node = "Base",
44 | node.child = "Weather")
45 |
46 | fact <- apsimx("Factorial.apsimx", src.dir = tmp.dir)
47 | file.remove(paste0(tmp.dir, "/Factorial.apsimx"))
48 | file.remove(paste0(tmp.dir, "/Factorial.db"))
49 | }
50 |
51 | run.factorial.example2.inspect <- get(".run.local.tests", envir = apsimx.options)
52 |
53 | if(run.factorial.example2.inspect){
54 |
55 | ex.dir <- auto_detect_apsimx_examples()
56 |
57 | ## Inspect factorial
58 | inspect_apsimx("Factorial", src.dir = ex.dir,
59 | root = c("RangeExperiment","Base"),
60 | node = "Weather")
61 |
62 | inspect_apsimx("Factorial", src.dir = ex.dir,
63 | root = c("RangeExperiment","Base"),
64 | node = "Clock")
65 |
66 |
67 | }
68 |
69 | run.factorial.example2.edit <- get(".run.local.tests", envir = apsimx.options)
70 |
71 | if(run.factorial.example2.edit){
72 |
73 | ex.dir <- auto_detect_apsimx_examples()
74 | ## Copy 'Factorial' example to current directory
75 | fact.ex <- paste0(ex.dir,"/Factorial.apsimx")
76 | file.exists(fact.ex)
77 | file.copy(from = fact.ex,
78 | to = tmp.dir, copy.mode = FALSE)
79 |
80 | factor.label <- c("PropertyReplacement", "RangeExperiment", "CompositeFactor",
81 | "ModelReplacement", "Permutation")
82 | # bases <- c("Base","Base2","Base3","Base4","Base5")
83 |
84 | for(i in 1:5){
85 | edit_apsimx("Factorial.apsimx",
86 | src.dir = tmp.dir,
87 | wrt.dir = tmp.dir,
88 | node = "Weather",
89 | overwrite = TRUE,
90 | root = c(factor.label[i], "Base"),
91 | value = paste0(ex.dir,"/WeatherFiles/lincoln.met"))
92 | }
93 |
94 | fact2 <- apsimx("Factorial.apsimx", src.dir = tmp.dir)
95 | file.remove(paste0(tmp.dir, "/Factorial.apsimx"))
96 | file.remove(paste0(tmp.dir, "/Factorial.db"))
97 |
98 | }
99 |
--------------------------------------------------------------------------------
/tests/test_edit_apsimx_replacement.R:
--------------------------------------------------------------------------------
1 | ## Testing the edit family of functions
2 | require(apsimx)
3 | extd.dir <- system.file("extdata", package = "apsimx")
4 |
5 | run.test.edit.apsimx.replacement <- get(".run.local.tests", envir = apsimx.options)
6 |
7 | tmp.dir <- tempdir()
8 |
9 | if(run.test.edit.apsimx.replacement){
10 |
11 | ## Inspect, edit, inspect
12 | inspect_apsimx_replacement("MaizeSoybean.apsimx", src.dir = extd.dir,
13 | node = "Soybean",
14 | node.child = "Leaf",
15 | parm = "Gsmax350")
16 |
17 | edit_apsimx_replacement("MaizeSoybean.apsimx", src.dir = extd.dir,
18 | wrt.dir = tmp.dir,
19 | node = "Soybean",
20 | node.child = "Leaf",
21 | parm = "Gsmax350", value = 0.009,
22 | verbose = FALSE)
23 |
24 | inspect_apsimx_replacement("MaizeSoybean-edited.apsimx", src.dir = tmp.dir,
25 | node = "Soybean",
26 | node.child = "Leaf",
27 | parm = "Gsmax350")
28 |
29 | ## Example for RUE
30 | ## Inspect, edit, inspect
31 | inspect_apsimx_replacement("MaizeSoybean.apsimx", src.dir = extd.dir,
32 | node = "Soybean",
33 | node.child = "Leaf",
34 | node.subchild = "Photosynthesis",
35 | node.subsubchild = "RUE",
36 | parm = "FixedValue")
37 |
38 | edit_apsimx_replacement("MaizeSoybean.apsimx",
39 | src.dir = extd.dir, wrt.dir = tmp.dir,
40 | node = "Soybean",
41 | node.child = "Leaf",
42 | node.subchild = "Photosynthesis",
43 | node.subsubchild = "RUE",
44 | parm = "FixedValue", value = 1, verbose = FALSE)
45 |
46 | inspect_apsimx_replacement("MaizeSoybean-edited.apsimx", src.dir = tmp.dir,
47 | node = "Soybean",
48 | node.child = "Leaf",
49 | node.subchild = "Photosynthesis",
50 | node.subsubchild = "RUE",
51 | parm = "FixedValue")
52 |
53 | #### Looking at Soybean
54 | inspect_apsimx_replacement("MaizeSoybean.apsimx", src.dir = extd.dir,
55 | node = "Soybean",
56 | node.child = "Cultivars",
57 | node.subchild = "USA",
58 | node.subsubchild = "IA2008_MG20",
59 | parm = "VegetativePhotoperiod",
60 | print.path = TRUE)
61 | }
62 |
63 | if(run.test.edit.apsimx.replacement){
64 |
65 | inspect_apsimx_replacement("MaizeSoybean.apsimx", src.dir = extd.dir,
66 | node = "Weather",
67 | parm = "FileName",
68 | root = "SimulationMaize")
69 |
70 | inspect_apsimx_replacement("MaizeSoybean.apsimx", src.dir = extd.dir,
71 | node = "Weather",
72 | parm = "FileName",
73 | root = "SimulationSoybean")
74 |
75 | pp <- inspect_apsimx_replacement("MaizeSoybean.apsimx", src.dir = extd.dir,
76 | node = "field",
77 | node.child = "Soil",
78 | node.subchild = "Physical",
79 | parm = "LL15",
80 | root = "SimulationMaize",
81 | print.path = TRUE)
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/man/get_isric_soil_profile.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_isric_soil_profile.R
3 | \name{get_isric_soil_profile}
4 | \alias{get_isric_soil_profile}
5 | \title{Generate a synthetic APSIM soil profile from the ISRIC soil database}
6 | \usage{
7 | get_isric_soil_profile(
8 | lonlat,
9 | statistic = c("mean", "Q0.5"),
10 | soil.profile,
11 | find.location.name = TRUE,
12 | fix = FALSE,
13 | verbose = TRUE,
14 | check = TRUE,
15 | physical = c("default", "SR"),
16 | xargs = NULL
17 | )
18 | }
19 | \arguments{
20 | \item{lonlat}{Longitude and latitude vector (e.g. c(-93, 42)).}
21 |
22 | \item{statistic}{default is the mean}
23 |
24 | \item{soil.profile}{a soil profile to fill in in case the default one is not appropriate}
25 |
26 | \item{find.location.name}{default is TRUE. Use either maps package or photon API to find Country/State.
27 | If you are running this function many times it might be better to set this to FALSE.}
28 |
29 | \item{fix}{whether to fix compatibility between saturation and bulk density (default is FALSE).}
30 |
31 | \item{verbose}{argument passed to the fix function.}
32 |
33 | \item{check}{whether to check the soil profile (default is TRUE)}
34 |
35 | \item{physical}{whether soil physical properties are obtained from the data base or through \sQuote{SR}, Saxton and Rawls pedotransfer functions.}
36 |
37 | \item{xargs}{additional arguments passed to \code{\link{apsimx_soil_profile}} or \sQuote{apsimx:::approx_soil_variable} function. At the moment these are:
38 | \sQuote{soil.bottom}, \sQuote{crops}, and \sQuote{nlayers} for the first function and \sQuote{method} for the second function.}
39 | }
40 | \value{
41 | it generates an object of class \sQuote{soil_profile}.
42 | }
43 | \description{
44 | Retrieves soil data from the ISRIC global database and converts it to an APSIM soil_profile object
45 | }
46 | \details{
47 | Source: https://www.isric.org/ \cr
48 | Details: https://www.isric.org/explore/soilgrids/faq-soilgrids \cr
49 |
50 | Pedotransfer functions: Saxton and Rawls, 2006. Soil Water Characteristic Estimates by Texture and Organic Matter for Hydrologic Solutions.
51 | Soil Sci. Soc. Am. J. 70:1569–1578. \cr
52 |
53 | TODO: need to look into how this is done in APSIM NG
54 | https://github.com/APSIMInitiative/ApsimX/pull/3994/files \cr
55 |
56 | NOTE: Eric Zurcher provided help by sending me an R file originally written by
57 | Andrew Moore. It provides a bit of context for how some of the decisions
58 | were made for constructing the synthetic soil profiles in APSIM. (email from june 3 2021).
59 |
60 | Variable which are directly retrieved and a simple unit conversion is performed: \cr
61 | * Bulk density - bdod \cr
62 | * Carbon - soc \cr
63 | * Clay - clay \cr
64 | * Sand - sand \cr
65 | * PH - phh2o \cr
66 | * Nitrogen - nitrogen \cr
67 | Variables which are optionally estimated using pedotransfer functions: \cr
68 | LL15, DUL, SAT, KS, AirDry \cr
69 | TO-DO: \cr
70 | What do I do with nitrogen? \cr
71 | Can I use CEC? \cr
72 | How can I have a guess at FBiom and Finert? \cr
73 | FBiom does not depend on any soil property at the moment, should it? \cr
74 | }
75 | \examples{
76 | \dontrun{
77 | ## Only run this if rest.isric.org is working
78 | rest.isric.on <- suppressWarnings(try(readLines("http://rest.isric.org",
79 | n = 1, warn = FALSE), silent = TRUE))
80 |
81 | ## Get soil profile properties for a single point
82 | if(!inherits(rest.isric.on, "try-error")){
83 | sp1 <- get_isric_soil_profile(lonlat = c(-93, 42), fix = TRUE, verbose = FALSE)
84 | ## Visualize
85 | plot(sp1)
86 | plot(sp1, property = "water")
87 | }
88 | }
89 | }
90 | \seealso{
91 | \code{\link{apsimx_soil_profile}}, \code{\link{edit_apsim_replace_soil_profile}}, \code{\link{edit_apsimx_replace_soil_profile}}.
92 | }
93 | \author{
94 | Fernando E. Miguez, Eric Zurcher (CSIRO) and Andrew Moore (CSIRO)
95 | }
96 |
--------------------------------------------------------------------------------
/R/get_power_apsim_met.R:
--------------------------------------------------------------------------------
1 | #'
2 | #' This function requires the \CRANpkg{nasapower} package version 4.0.0.
3 | #'
4 | #' It looks like the earliest year you can request data for is 1984.
5 | #'
6 | #' @title Get NASA-POWER data for an APSIM met file
7 | #' @description Uses \code{\link[nasapower]{get_power}} from the \CRANpkg{nasapower} package to download data to create an APSIM met file.
8 | #' @name get_power_apsim_met
9 | #' @param lonlat Longitude and latitude vector
10 | #' @param dates date ranges
11 | #' @param wrt.dir write directory
12 | #' @param filename file name for writing out to disk
13 | #' @return returns an object of class \sQuote{met} and writes a file to disk when filename is supplied.
14 | #' @details If the filename is not provided it will not write the file to disk,
15 | #' but it will return an object of class \sQuote{met}. This is useful in case manipulation
16 | #' is required before writing to disk.
17 | #' @export
18 | #' @examples
19 | #' \dontrun{
20 | #' require(nasapower)
21 | #' ## This will not write a file to disk
22 | #' pwr <- get_power_apsim_met(lonlat = c(-93,42), dates = c("2012-01-01","2012-12-31"))
23 | #' ## Let's insert a missing value
24 | #' pwr[100, "radn"] <- NA
25 | #' summary(pwr)
26 | #' ## Check the met file
27 | #' check_apsim_met(pwr)
28 | #' ## Impute using linear interpolation
29 | #' pwr.imptd <- impute_apsim_met(pwr, verbose = TRUE)
30 | #' summary(pwr.imptd)
31 | #' check_apsim_met(pwr.imptd)
32 | #' }
33 | #'
34 |
35 | get_power_apsim_met <- function(lonlat, dates, wrt.dir = ".", filename = NULL){
36 |
37 | if(!requireNamespace("nasapower", quietly = TRUE)){
38 | warning("The nasapower package is required for this function")
39 | return(NULL)
40 | }
41 |
42 | if(packageVersion("nasapower") <= '3.0.1'){
43 | stop("Please upgrade the 'nasapower' package to the latest version", call. = FALSE)
44 | }
45 |
46 | if(missing(filename)) filename <- "noname.met"
47 |
48 | if(!grepl(".met", filename, fixed = TRUE)) stop("filename should end in .met")
49 |
50 | pwr <- nasapower::get_power(community = "AG",
51 | pars = c("T2M_MAX",
52 | "T2M_MIN",
53 | "ALLSKY_SFC_SW_DWN",
54 | "PRECTOTCORR",
55 | "RH2M",
56 | "WS2M"),
57 | dates = dates,
58 | lonlat = lonlat,
59 | temporal_api = "daily")
60 |
61 | pwr <- subset(as.data.frame(pwr), select = c("YEAR", "DOY",
62 | "ALLSKY_SFC_SW_DWN",
63 | "T2M_MAX", "T2M_MIN",
64 | "PRECTOTCORR", "RH2M", "WS2M"))
65 |
66 | names(pwr) <- c("year", "day", "radn", "maxt", "mint", "rain", "rh", "windspeed")
67 | units <- c("()", "()", "(MJ/m2/day)", "(oC)", "(oC)", "(mm)", "(%)", "(m/s)")
68 |
69 | comments <- paste("!data from nasapower R package. retrieved: ", Sys.time())
70 |
71 | ## Calculating annual amplitude in mean monthly temperature
72 |
73 | attr(pwr, "filename") <- filename
74 | attr(pwr, "site") <- paste("site =", sub(".met", "", filename, fixed = TRUE))
75 | attr(pwr, "latitude") <- paste("latitude =", lonlat[2])
76 | attr(pwr, "longitude") <- paste("longitude =", lonlat[1])
77 | attr(pwr, "colnames") <- names(pwr)
78 | attr(pwr, "units") <- units
79 | attr(pwr, "comments") <- comments
80 | ## No constants
81 | class(pwr) <- c("met", "data.frame")
82 |
83 | pwr <- amp_apsim_met(pwr)
84 | pwr <- tav_apsim_met(pwr)
85 |
86 | if(filename != "noname.met"){
87 | write_apsim_met(pwr, wrt.dir = wrt.dir, filename = filename)
88 | }
89 | return(invisible(pwr))
90 | }
91 |
92 |
93 |
--------------------------------------------------------------------------------
/man/extract_data_apsimx.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/extract_data_apsimx.R
3 | \name{extract_data_apsimx}
4 | \alias{extract_data_apsimx}
5 | \title{Extract data from an .apsimx (JSON) file}
6 | \usage{
7 | extract_data_apsimx(
8 | file = "",
9 | src.dir = ".",
10 | node = c("Clock", "Weather", "Soil", "SurfaceOrganicMatter", "MicroClimate", "Crop",
11 | "Manager", "Report", "Operations", "Other"),
12 | soil.child = c("Metadata", "Water", "InitialWater", "Chemical", "Physical", "Analysis",
13 | "SoilWater", "InitialN", "CERESSoilTemperature", "Sample", "Solute", "NO3", "NH4",
14 | "Urea", "Nutrient", "Organic", "Swim3"),
15 | parm = NULL,
16 | digits = 3,
17 | root = NULL
18 | )
19 | }
20 | \arguments{
21 | \item{file}{file ending in .apsimx to be inspected (JSON)}
22 |
23 | \item{src.dir}{directory containing the .apsimx file to be inspected; defaults to the current working directory}
24 |
25 | \item{node}{specific node to be used either \sQuote{Clock}, \sQuote{Weather},
26 | \sQuote{Soil}, \sQuote{SurfaceOrganicMatter}, \sQuote{MicroClimate}, \sQuote{Crop},
27 | \sQuote{Manager}, \sQuote{Operations} or \sQuote{Other}}
28 |
29 | \item{soil.child}{specific soil component to be inspected. The options vary depending on what is available (see \link{inspect_apsimx})}
30 |
31 | \item{parm}{parameter to refine the extraction of the \sQuote{manager} list(\sQuote{parm},\sQuote{position}), use \sQuote{NA} for all the positions. \sQuote{parm} can be a regular expression for partial matching.}
32 |
33 | \item{digits}{number of decimals to print (default 3). Not used now because everything is a character.}
34 |
35 | \item{root}{root node label. In simulation structures such as factorials there will be multiple possible nodes. This can be specified by supplying an appropriate character.}
36 | }
37 | \value{
38 | a \link{data.frame} or a \link{list}. It does not return a path.
39 | }
40 | \description{
41 | Extract data from a JSON apsimx file.
42 | }
43 | \details{
44 | This function does not print anything (compared to inspect_apsimx). The purpose is to return data
45 | contained in the APSIM simulation as a data.frame. It will return a \sQuote{list} when a data frame
46 | does not naturally accommodate the result. For example, the complete manager node does not naturally
47 | fit into a data frame structure. In some cases, multiple data frames are returned as part of lists.
48 |
49 | Have not written this section yet
50 | }
51 | \examples{
52 | \donttest{
53 | extd.dir <- system.file("extdata", package = "apsimx")
54 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Clock"))
55 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Weather"))
56 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Soil",
57 | soil.child = "Metadata"))
58 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Soil",
59 | soil.child = "Physical"))
60 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Soil",
61 | soil.child = "SoilWater"))
62 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Soil",
63 | soil.child = "Organic"))
64 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Soil",
65 | soil.child = "Chemical"))
66 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Soil",
67 | soil.child = "InitialWater"))
68 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Soil",
69 | soil.child = "InitialN"))
70 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "SurfaceOrganicMatter"))
71 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "MicroClimate"))
72 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Crop"))
73 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Manager"))
74 | (edf <- extract_data_apsimx("Wheat.apsimx", src.dir = extd.dir, node = "Report"))
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/NAMESPACE:
--------------------------------------------------------------------------------
1 | # Generated by roxygen2: do not edit by hand
2 |
3 | S3method("$<-",met)
4 | S3method("[",met)
5 | S3method("[<-",met)
6 | S3method("[[<-",met)
7 | S3method(coef,optim_apsim)
8 | S3method(confint,optim_apsim)
9 | S3method(plot,met)
10 | S3method(plot,met_mrg)
11 | S3method(plot,out_mrg)
12 | S3method(plot,soil_profile)
13 | S3method(plot,soil_profile_mrg)
14 | S3method(print,met)
15 | S3method(print,met_mrg)
16 | S3method(print,optim_apsim)
17 | S3method(print,out_mrg)
18 | S3method(print,sens_apsim)
19 | S3method(print,soil_profile_mrg)
20 | S3method(summary,met)
21 | S3method(summary,sens_apsim)
22 | S3method(vcov,optim_apsim)
23 | export(add_column_apsim_met)
24 | export(amp_apsim_met)
25 | export(apsim)
26 | export(apsim.options)
27 | export(apsim_example)
28 | export(apsim_options)
29 | export(apsim_version)
30 | export(apsimx)
31 | export(apsimx.options)
32 | export(apsimx_example)
33 | export(apsimx_filetype)
34 | export(apsimx_options)
35 | export(apsimx_soil_profile)
36 | export(as_apsim_met)
37 | export(auto_detect_apsim_examples)
38 | export(auto_detect_apsimx_examples)
39 | export(available_water_content)
40 | export(carbon_stocks)
41 | export(check_apsim_met)
42 | export(check_apsimx)
43 | export(check_apsimx_soil_profile)
44 | export(compare_apsim)
45 | export(compare_apsim_met)
46 | export(compare_apsim_soil_profile)
47 | export(date2doy)
48 | export(doy2date)
49 | export(edit_apsim)
50 | export(edit_apsim_replace_soil_profile)
51 | export(edit_apsim_xml)
52 | export(edit_apsimx)
53 | export(edit_apsimx_batch)
54 | export(edit_apsimx_replace_soil_profile)
55 | export(edit_apsimx_replacement)
56 | export(extract_data_apsimx)
57 | export(extract_values_apsimx)
58 | export(get_apsimx_json)
59 | export(get_chirps_apsim_met)
60 | export(get_daymet2_apsim_met)
61 | export(get_daymet_apsim_met)
62 | export(get_gsod_apsim_met)
63 | export(get_iem_apsim_met)
64 | export(get_iemre_apsim_met)
65 | export(get_isric_soil_profile)
66 | export(get_power_apsim_met)
67 | export(get_slga_soil)
68 | export(get_slga_soil_profile)
69 | export(get_ssurgo_soil_profile)
70 | export(get_ssurgo_tables)
71 | export(get_worldmodeler_apsim_met)
72 | export(get_worldmodeler_soil_profile)
73 | export(grep_json_list)
74 | export(impute_apsim_met)
75 | export(initialwater_parms)
76 | export(insert_replacement_node)
77 | export(inspect_apsim)
78 | export(inspect_apsim_xml)
79 | export(inspect_apsimx)
80 | export(inspect_apsimx_json)
81 | export(inspect_apsimx_replacement)
82 | export(mcmc.apsim.env)
83 | export(mcmc.apsimx.env)
84 | export(napad_apsim_met)
85 | export(optim_apsim)
86 | export(optim_apsimx)
87 | export(read_apsim)
88 | export(read_apsim_all)
89 | export(read_apsim_met)
90 | export(read_apsim_soils)
91 | export(read_apsimx)
92 | export(read_apsimx_all)
93 | export(remove_column_apsim_met)
94 | export(sens_apsim)
95 | export(sens_apsimx)
96 | export(soilorganicmatter_parms)
97 | export(soilwat_parms)
98 | export(solutes_parms)
99 | export(ssurgo2sp)
100 | export(swim_parms)
101 | export(tav_apsim_met)
102 | export(tt_apsim_met)
103 | export(unit_conv)
104 | export(view_apsim)
105 | export(view_apsim_xml)
106 | export(view_apsimx)
107 | export(write_apsim_met)
108 | export(xargs_apsimx)
109 | import(DBI)
110 | import(RSQLite)
111 | import(jsonlite)
112 | import(knitr)
113 | import(xml2)
114 | importFrom(stats,aggregate)
115 | importFrom(stats,anova)
116 | importFrom(stats,coef)
117 | importFrom(stats,cor)
118 | importFrom(stats,cov2cor)
119 | importFrom(stats,deviance)
120 | importFrom(stats,lm)
121 | importFrom(stats,optim)
122 | importFrom(stats,pnorm)
123 | importFrom(stats,qt)
124 | importFrom(stats,quantile)
125 | importFrom(stats,reformulate)
126 | importFrom(stats,sd)
127 | importFrom(stats,setNames)
128 | importFrom(stats,sigma)
129 | importFrom(stats,terms)
130 | importFrom(stats,var)
131 | importFrom(tools,file_ext)
132 | importFrom(tools,file_path_sans_ext)
133 | importFrom(utils,head)
134 | importFrom(utils,packageVersion)
135 | importFrom(utils,read.table)
136 | importFrom(utils,write.table)
137 |
--------------------------------------------------------------------------------
/tests/test_get_daymet_apsim_met.R:
--------------------------------------------------------------------------------
1 | ## Testing the get_daymet_apsim_met function
2 | ## Note (2020-02-09)
3 | ## I've ran this test several times now on Mac and Windows
4 | ## I won't run it again until it is needed
5 | ## However, note that the source of weather data can
6 | ## have a large impact on final yield estimates
7 | ## the difference is 171.1547 g per meter squared
8 | ## with higher values for power over daymet
9 | ## Note (2020-06-15): This takes for ever, I'm not running it for a while
10 | require(apsimx)
11 | apsimx_options(warn.versions = FALSE)
12 |
13 | run.test.daymet0 <- FALSE
14 |
15 | run.test.daymet <- get(".run.local.tests", envir = apsimx.options) & run.test.daymet0
16 |
17 | username <- Sys.info()[["user"]]
18 |
19 | tmp.dir <- tempdir()
20 |
21 | if(run.test.daymet && username == "fernandomiguez"){
22 |
23 | lonlat <- c(-93, 42)
24 |
25 | dmet <- get_daymet_apsim_met(lonlat = lonlat,
26 | years = 2015:2016,
27 | width.height = c(1e-5,1e-5),
28 | raw.dir = paste0(tmp.dir,"/RAW/DAYMET"),
29 | extraction.dir = paste0(tmp.dir, "/EXTRACTIONS/CIA/DAYMET"),
30 | label = "CIA",
31 | filename = "cia-daymet.met",
32 | cleanup = FALSE)
33 |
34 | dmet2 <- get_daymet2_apsim_met(lonlat = lonlat, years = 2015:2016)
35 |
36 | cia.daymet <- read_apsim_met("/cia-daymet.met", src.dir = tmp.dir)
37 |
38 | check_apsim_met(cia.daymet)
39 | }
40 |
41 | ## Does the file produced by DayMet work in APSIM?
42 |
43 | run.test.maize.daymet <- get(".run.local.tests", envir = apsimx.options) & run.test.daymet0
44 |
45 | if(run.test.maize.daymet && username == "fernandomiguez"){
46 |
47 | extd.dir <- system.file("extdata", package = "apsimx")
48 |
49 | edit_apsimx("Maize.apsimx",
50 | node = "Clock",
51 | src.dir = extd.dir,
52 | wrt.dir = tmp.dir,
53 | parm = c("Start","End"),
54 | value = c("2015-01-01","2016-12-30"),
55 | overwrite = FALSE)
56 |
57 | inspect_apsimx("Maize-edited.apsimx", src.dir = tmp.dir, node = "Clock")
58 |
59 | edit_apsimx("Maize-edited.apsimx",
60 | src.dir = tmp.dir, wrt.dir = tmp.dir,
61 | node = "Weather",
62 | value = "cia-daymet.met",
63 | overwrite = TRUE)
64 |
65 | inspect_apsimx("Maize-edited.apsimx", src.dir = tmp.dir, node = "Weather")
66 |
67 | maize.daymet <- apsimx("Maize-edited.apsimx", src.dir = tmp.dir, value = "report")
68 |
69 | }
70 |
71 | ## What if I get data from POWER?
72 |
73 | run.test.power <- get(".run.local.tests", envir = apsimx.options) & run.test.daymet0
74 |
75 | if(run.test.power && username == "fernandomiguez"){
76 |
77 | lonlat <- c(-93, 42)
78 |
79 | pwr <- get_power_apsim_met(lonlat = lonlat, wrt.dir = tmp.dir,
80 | dates = c("2015-01-01","2016-12-31"),
81 | filename = "cia-power.met")
82 |
83 | pwr.met <- read_apsim_met("cia-power.met", src.dir = tmp.dir)
84 |
85 | check_apsim_met(pwr.met)
86 |
87 | pwr.met$radn <- ifelse(pwr.met$radn == -99, NA, pwr.met$radn)
88 |
89 | pwr.met2 <- impute_apsim_met(pwr.met, verbose = TRUE)
90 |
91 | check_apsim_met(pwr.met2)
92 |
93 | write_apsim_met(pwr.met2, wrt.dir = tmp.dir, filename = "cia-power2.met")
94 | }
95 |
96 | run.test.maize.power <- get(".run.local.tests", envir = apsimx.options) & run.test.daymet0
97 |
98 | if(run.test.maize.daymet && username == "fernandomiguez"){
99 |
100 | edit_apsimx("Maize-edited.apsimx", node = "Weather",
101 | value = "cia-power2.met", src.dir = tmp.dir,
102 | edit.tag = "-power")
103 |
104 | maize.power <- apsimx("Maize-edited-power.apsimx",
105 | src.dir = tmp.dir, value = "report")
106 |
107 | yld.diff <- maize.power$Maize.Grain.Wt - maize.daymet$Maize.Grain.Wt
108 | }
109 |
110 |
--------------------------------------------------------------------------------
/tests/test_AK_point_sim.R:
--------------------------------------------------------------------------------
1 | ## Example simulation for a point using APSIM
2 | ## Author: Fernando E. Miguez
3 | ## Date: 2021-06-08
4 | ##
5 | ## Required: R package apsimx 1.992
6 | require(apsimx)
7 | require(ggplot2)
8 |
9 | apsimx_options(warn.versions = FALSE)
10 |
11 | run.apsimx.point.sim <- get(".run.local.tests", envir = apsimx.options)
12 |
13 | tmpd <- tempdir()
14 | ## tmpd <- "../tests"
15 | ## setwd(tmpd)
16 |
17 | if(run.apsimx.point.sim && FALSE){
18 |
19 | site.name <- "ARK1"
20 | site.coords <- c(-90.75973, 34.72834)
21 |
22 | ## Copy Maize from Examples
23 | exdir <- auto_detect_apsimx_examples()
24 | file.copy(file.path(exdir, "Maize.apsimx"), tmpd)
25 |
26 | ## Get weather from NASAPOWER
27 | ## Important: this is just an example. I recommend looking into
28 | ## whether this is a good source of weather data for this location.
29 | ## This takes a few seconds
30 | pmet <- get_power_apsim_met(site.coords,
31 | dates = c("2000-01-01", "2020-12-31"))
32 | check_apsim_met(pmet) ## Some missing values for solar radiation
33 | pmet <- impute_apsim_met(pmet) ## Automatically impute
34 | check_apsim_met(pmet) ## Now it is okay
35 | write_apsim_met(pmet, wrt.dir = ".", filename = paste0(site.name, ".met")) ## Write to file
36 |
37 | ## Edit the filename for weather
38 | edit_apsimx("Maize.apsimx", node = "Weather",
39 | value = paste0(site.name, ".met"),
40 | overwrite = TRUE)
41 |
42 | inspect_apsimx("Maize.apsimx", node = "Weather")
43 |
44 | ## Also change the clock
45 | edit_apsimx("Maize.apsimx", node = "Clock",
46 | parm = c("Start", "End"),
47 | value = c("2000-01-01T00:00:00", "2020-12-31T00:00:00"),
48 | overwrite = TRUE)
49 |
50 | inspect_apsimx("Maize.apsimx")
51 |
52 | ## Change planting and harvest date
53 | edit_apsimx("Maize.apsimx", node = "Crop",
54 | parm = "StartDate", value = "1-may",
55 | overwrite = TRUE)
56 |
57 | edit_apsimx("Maize.apsimx", node = "Crop",
58 | parm = "EndDate", value = "1-oct",
59 | overwrite = TRUE)
60 |
61 | inspect_apsimx("Maize.apsimx", node = "Crop")
62 |
63 | sim0 <- apsimx("Maize.apsimx")
64 |
65 | ## Results with default soil
66 | ggplot(sim0, aes(x = Date, y = Maize.AboveGround.Wt)) + geom_point()
67 |
68 | ## Now let's get the soil data from SSURGO
69 | sp0 <- get_ssurgo_soil_profile(site.coords)
70 |
71 | ## Very important: these are just 'template' soil profiles from
72 | ## where to start. These need to be checked and if you have any site
73 | ## specific information you should use it
74 | plot(sp0[[1]]) ## Visualize properties
75 | plot(sp0[[1]], property = "water")
76 | ## You might want to use the APSIM-X GUI in addition
77 |
78 | ## Also, here you can modify the metadata, since it is a list
79 | sp0[[1]]$metadata$NearestTown <- "Marianna"
80 | ## You might want to populate the soilwat or swim options
81 | ## using the helper functions soilwat_parms or swim_parms
82 |
83 | edit_apsimx_replace_soil_profile("Maize.apsimx",
84 | soil.profile = sp0[[1]],
85 | overwrite = TRUE)
86 |
87 | sim1 <- apsimx("Maize.apsimx")
88 |
89 | ## Results with ssurgo soil
90 | ggplot(sim1, aes(x = Date, y = Maize.AboveGround.Wt)) + geom_point()
91 |
92 | cmp <- compare_apsim(sim0, sim1, variable = "Maize.AboveGround.Wt")
93 | plot(cmp, variable = "Maize.AboveGround.Wt") ## similar but not identical
94 |
95 | ## There is no site information
96 | inspect_apsimx("Maize.apsimx", node = "Soil", parm = "Site")
97 | ## However, the nearest town was edited as expected
98 | inspect_apsimx("Maize.apsimx", node = "Soil", parm = "NearestTown")
99 |
100 | ## If we still want to edit the Site
101 | edit_apsimx("Maize.apsimx", node = "Soil",
102 | parm = "Site", value = "AK1",
103 | overwrite = TRUE)
104 |
105 | inspect_apsimx("Maize.apsimx", node = "Soil")
106 | }
107 |
--------------------------------------------------------------------------------
/R/apsim_internal.R:
--------------------------------------------------------------------------------
1 | #' Auxiliary unexported and \sQuote{hidden} functions
2 | #' Find output file names in an APSIM \sQuote{Classic} file
3 | #' @name .find_ouput_names
4 | #' @description In APSIM \sQuote{Classic} the outputfiles are named internally and this
5 | #' function can retrieve this information
6 | #' @param .file file name
7 | #' @param .src.dir source directory
8 | #'
9 | #' @return output file names
10 | #'
11 | #' @noRd
12 | .find_output_names <- function(.file, .src.dir = "."){
13 |
14 | .apsim_xml <- xml2::read_xml(paste0(.src.dir,"/",.file))
15 |
16 | .find.output <- xml2::xml_find_all(.apsim_xml, ".//outputfile/filename")
17 |
18 | .ans <- xml2::xml_text(.find.output)
19 |
20 | return(.ans)
21 | }
22 |
23 | #' Check the name and disallow white spaces
24 | #' These are not allowed as it would not work (easily)
25 | #' at the command line
26 | #' @name .check_apsim_name
27 | #' @description Checks whether an APSIM file has spaces in it
28 | #' @param .file an APSIM file
29 | #' @return It only returns an error if there is a space in the file
30 | #' @noRd
31 | #'
32 | .check_apsim_name <- function(.file){
33 | ## No spaces are allowed, provide informative error
34 | if(missing(.file))
35 | stop("argument .file is missing", call. = FALSE)
36 | if(grepl("\\s", .file))
37 | stop("White spaces are not allowed in file names or directories", call. = FALSE)
38 | }
39 |
40 | #' This is for when a list has only one element
41 | #' But inside that element there is a children with multiple
42 | #' elements, or at least is not null
43 | #' @name .extract_bad_children
44 | #' @description Possibly extract unnamed children more robustlly
45 | #' @param x a list with (potentially) a \sQuote{Children} object
46 | #' @return it returns a subset of a json list
47 | #' @noRd
48 | #'
49 | .extract_bad_children <- function(x){
50 |
51 | if(!is.list(x)) stop("x should be a list")
52 |
53 | lnms <- names(x)
54 | nco <- is.null(x$Children)
55 |
56 | if(length(x) == 1 && lnms == 0 && !nco){
57 | x <- x$Children[[1]]
58 | }
59 | return(x)
60 | }
61 |
62 | #' This function extracts the APSIM-X year, month version
63 | #' This used to be able to extract the date, but this is not
64 | #' part of the name anymore, so it is not available
65 | #' @name .favd
66 | #' @description Extract the year, month version from an APSIM binary name
67 | #' @param x an APISM binary name such as 'APSIM2020.06.5260.app'
68 | #' @return it returns a string for the APSIM year, month, version
69 | #' @noRd
70 | #'
71 | .favd <- function(x){
72 | x1 <- gsub("APSIM","\\1", x) ## Remove 'APSIM'
73 | x2 <- strsplit(x1, ".", fixed = TRUE)[[1]] ## Split by '.'
74 | x3 <- paste(paste(x2[1:2], collapse="-"), " version:", as.numeric(x2[3]))
75 | return(x3)
76 | }
77 |
78 | #' This function extracts the APSIM-X Version number
79 | #' Find Apsim Version number
80 | #' @name .favn
81 | #' @description Extract the number from an APSIM binary name being used
82 | #' @param x an APISM binary name such as 'APSIM2020.06.5260.app'
83 | #' @return it returns a number for the APSIM version
84 | #' @noRd
85 | #'
86 | .favn <- function(x){
87 | x1 <- gsub("APSIM","\\1", x) ## Remove 'APSIM'
88 | x2 <- strsplit(x1, ".", fixed = TRUE)[[1]] ## Split by '.'
89 | # x3 <- as.Date(paste(x2[1:3], collapse="-"), format = "%Y-%m-%d") ## Convert to 'Date'
90 | x3 <- as.numeric(x2[3]) + as.numeric(x2[4])
91 | return(x3)
92 | }
93 |
94 | #' This function converts thickness to depth for soil profiles
95 | #' Thickness is assumed to be in mm and the returned depths would be in
96 | #' cm
97 | #' @name .t2d
98 | #' @description Takes a \sQuote{Thickness} column and returns \sQuote{Depth}
99 | #' @param x a soil profile \sQuote{Thickness} column (numeric)
100 | #' @return it returns a column with strings such as \sQuote{0-20}
101 | #' @noRd
102 | #'
103 | .t2d <- function(x){
104 | x2 <- c(0, x)/10 ## Divide by 10 to go from mm to cm
105 | ans <- character(length(x))
106 | csx2 <- cumsum(x2)
107 | for(i in 2:length(x2)){
108 | ans[i-1] <- paste0(csx2[i-1], "-", csx2[i])
109 | }
110 | ans
111 | }
112 |
--------------------------------------------------------------------------------