6.6. Creation of a runModel function

The next step is to create a runModel function, the purpose of which is to preprocess parameter files, run OSMOSE Java Java and postprocess the outputs.

The runModel function used to calibrate the [HLS+16] configuration is detailed below.

Danger

This runModel.R script is built for the reference configuration and must not be used to run other configurations. It must be adapted by the user.

6.6.1. Function arguments

This function should be adapted by the user. It must take at least two arguments:

  • the array of calibrated parameters (param arguments)
  • the names of the calibrated parameters (names arguments)
runModel  = function(param, names, ...) {

6.6.2. Writes the calibrated parameters in the CSV file

The first step is to write the calibrated parameters in the calibration-parameters.csv file (that will be located in the master directory):

    # set parameter names
    names(param) = names

    # writes the calibrated parameters into a CSV file
    # following Osmose format. The parameters in this file
    # will overwrite the Osmose parameter
    write.table(param, file="calibration-parameters.csv", sep=";", 
                col.names=FALSE, quote=FALSE)

Danger

The file format must be consistent with the expectations of OSMOSE Java.

6.6.3. Run OSMOSE Java

The next step is to run the OSMOSE Java program. This is done by using the osmose::run_osmose function:

    # defines the user directory
    outdir = "output"
    
    # run Osmose Model
    run_osmose(input="calib_config.csv", 
              output=outdir, options=NULL, log="osmose.log", 
              verbose=FALSE, clean=TRUE)   

Here, the input argument is the calibration configuration file used by OSMOSE Java. In this case, it is calib_config.csv, which includes the calibration-parameters.csv and the osm_all-parameters.csv (see Table 6.1).

6.6.4. Process OSMOSE Java outputs

The last step is to process OSMOSE Java outputs, so that they fit the observation time series (see Section 6.2).

The reading of OSMOSE Java outputs is achieved by using the osmose::read_osmose function.

    # read Osmose outputs 
    data = read_osmose(path=outdir)

Since biomass and yields obervations are on yearly time-scales, the monthly simulated biomass and yields must be yearly integrated:

    # extract the biomass and yields variables (monthly data).
    # expectes = TRUE to average over the replicates
    osmose.biomass = getVar(data, "biomass", expected=TRUE)
    osmose.thresholds = getVar(data, "biomass", expected=TRUE)
    osmose.yields = getVar(data, "yield", expected=TRUE)

    # define a year factor for yearly integration of biomass
    # from monthly biomass
    biomassDim = dim(osmose.biomass)   # dims=(time, species, replic)
    ntime = biomassDim[1]    # nyears * 12
    nspecies = biomassDim[2]
    nyears = ntime / 12
    years = factor(rep(1:nyears, each=12))

    # Integration of monthly values into yearly values
    # here, tapply is applied on dimension 1 (i.e. time)
    osmose.biomass = apply(osmose.biomass, 2, tapply, years, sum)
    osmose.yields = apply(osmose.yields, 2, tapply, years, sum)

Finally, the output must be converted into a list, whose elements’ names must be consistent with the observation names (variable column in Table 6.4).

    output = list(# Biomass
                  OctopusVulgaris.biomass       = param["q.sp0"]*osmose.biomass[, "OctopusVulgaris"],
                  MelicertusKerathurus.biomass  = param["q.sp1"]*osmose.biomass[, "MelicertusKerathurus"],
                  MetapenaeusMonoceros.biomass  = param["q.sp2"]*osmose.biomass[, "MetapenaeusMonoceros"],
                  TrachurusTrachurus.biomass    = param["q.sp3"]*osmose.biomass[, "TrachurusTrachurus"],
                  SardinaPilchardus.biomass     = param["q.sp4"]*osmose.biomass[, "SardinaPilchardus"],
                  SardinellaAurita.biomass      = param["q.sp5"]*osmose.biomass[, "SardinellaAurita"],
                  EngraulisEncrasicolus.biomass = param["q.sp6"]*osmose.biomass[, "EngraulisEncrasicolus"],
                  DiplodusAnnularis.biomass     = param["q.sp7"]*osmose.biomass[, "DiplodusAnnularis"],
                  MustelusMustelus.biomass      = param["q.sp8"]*osmose.biomass[, "MustelusMustelus"],
                  MerlucciusMerluccius.biomass  = param["q.sp9"]*osmose.biomass[, "MerlucciusMerluccius"],
                  PagellusErythrinus.biomass    = param["q.sp10"]*osmose.biomass[, "PagellusErythrinus"],
                  # Landings
                  OctopusVulgaris.landings       = osmose.yields[, "OctopusVulgaris"],
                  MelicertusKerathurus.landings  = osmose.yields[, "MelicertusKerathurus"],
                  MetapenaeusMonoceros.landings  = osmose.yields[, "MetapenaeusMonoceros"], 
                  TrachurusTrachurus.landings    = osmose.yields[, "TrachurusTrachurus"], 
                  SardinaPilchardus.landings     = osmose.yields[, "SardinaPilchardus"], 
                  SardinellaAurita.landings      = osmose.yields[, "SardinellaAurita"], 
                  EngraulisEncrasicolus.landings = osmose.yields[, "EngraulisEncrasicolus"], 
                  DiplodusAnnularis.landings     = osmose.yields[, "DiplodusAnnularis"], 
                  MustelusMustelus.landings      = osmose.yields[, "MustelusMustelus"], 
                  MerlucciusMerluccius.landings  = osmose.yields[, "MerlucciusMerluccius"], 
                  PagellusErythrinus.landings    = osmose.yields[, "PagellusErythrinus"], 
                  # Thresholds
                  OctopusVulgaris.thr           = osmose.thresholds[, "OctopusVulgaris"], 
                  MelicertusKerathurus.thr      = osmose.thresholds[, "MelicertusKerathurus"], 
                  MetapenaeusMonoceros.thr      = osmose.thresholds[, "MetapenaeusMonoceros"], 
                  TrachurusTrachurus.thr        = osmose.thresholds[, "TrachurusTrachurus"], 
                  SardinaPilchardus.thr         = osmose.thresholds[, "SardinaPilchardus"], 
                  SardinellaAurita.thr          = osmose.thresholds[, "SardinellaAurita"], 
                  EngraulisEncrasicolus.thr     = osmose.thresholds[, "EngraulisEncrasicolus"], 
                  DiplodusAnnularis.thr         = osmose.thresholds[, "DiplodusAnnularis"], 
                  MustelusMustelus.thr          = osmose.thresholds[, "MustelusMustelus"], 
                  MerlucciusMerluccius.thr      = osmose.thresholds[, "MerlucciusMerluccius"], 
                  PagellusErythrinus.thr        = osmose.thresholds[, "PagellusErythrinus"]
                  )

    return(output)