Welcome to traDSSAT!¶
TraDSSAT aims to reinvent the wheel, once and for all, for DSSAT file management. Whether for preparing input files for automated DSSAT runs or for analysing outputs, traDSSAT’s simple Python interface and modular structure will (we hope!) dramatically simplify your code.
Contents¶
Quick start¶
Installation¶
- traDSSAT requires
numpy
andchardet
to run. You can install it from pip with: - pip install tradssat
- You can also install the most cutting-edge version directly from GitHub with:
- pip install git+git://github.com/julienmalard/tradssat.git@master
You will need a local installation of DSSAT to use the high-level interface’s automatic file managers (since these will need to find soil, weather, and other files in the DSSAT installation directory). TraDSSAT should find your DSSAT installation by itself, but if it needs help you can specify it with:
from tradssat import set_dssat_dir
set_dssat_dir('C:/My/odd/path/to/DSSAT47')
A quick example¶
from tradssat import SoilFile, WTHFile, ExpFile, set_dssat_dir
from tradssat import GeneticMgr, CULFile, ECOFile
# Read, edit and write soil files
soil = SoilFile('path/to/my/file.SOL')
# Read and write weather files as well
wth = WTHFile('path/to/my/WTHR0001.WTH')
# ...and experiment files!
exp = ExpFile('path/to/my/experiment.EXP')
# Access genetic coefficients by cultivar file or ecotype file
cul = CULFile('path/to/my/MZIXM.CUL')
eco = ECOFile('path/to/my/MZIXM.ECO')
cul.get_value('P1') # returns array of all varieties' P1
eco.get_value('TOPT') # returns array of all ecotypes' TOPT
# ...or automagically!
set_dssat_dir('C:/DSSAT47')
gen = GeneticMgr(crop='MZIXM', cult='PC0001')
gen.get_value('P1') # Returns P1 for MZIXM cultivar PC0001
gen.get_value('TOPT') # Returns ecotype variable TOPT for cultivar PC001
Input files¶
TraDSSAT has specific classes to read and edit DSSAT input files. Each class defines its own allowed variables and variable information. All files have the same general structure:
- Section (named; header line starts with
*
or$
in DSSAT files) - Section header variables (optional)
- Subsection (numbered; header line starts with
@
followed by variable names in DSSAT files) - Subsection variables
Specific classes used to read DSSAT input files are:
As all input files inherit from InpFile
, the same interface to reading, editing and writing
holds for all DSSAT input files.
Reading files¶
Input files are instantiated with the path to the file to be read.
from tradssat import WTHFile
wth = WTHFile('path/to/my/WTHR0001.WTH')
Values of all variables can then be read directly.
# Get solar radiation data time series
wth.get_value('SRAD')
# Conditions can also be set
# Only get data from 2 Jan, 2013.
wth.get_value('SRAD', cond={'DATE': '13002'})
# Only get data from dry days
wth.get_value('SRAD', cond={'RAIN': 0})
Getting a list of all allowed variable names for the file type is also easy.
wth.variables()
Editing files¶
Variable values can be changed, either for the whole file or for specific sections and/or subsections, as well as by condition.
from tradssat import SoilFile
soil = SoilFile('path/to/my/file.SOL')
# Set all soils' SALB to 0.15
soil.set_value('SALB', 0.15)
# Only set soil IB00000002's SALB to 0.2
soil.set_value('SALB', 0.20, sect='IB00000002')
# Increase clay, but only for the first 5 cm of soil IB00000002
soil.set_value('SLCL', 0.50, sect='IB00000002', cond={'SLB': 5})
You can also add rows to specific subsections of a file, or remove existing rows. Subsection variables not included
in vals
will be set to missing (usually -99).
# Add new soil layer
soil.add_row(
sect='IB00000002', subsect=2, vals={'SLB': 180, 'SLLL': 0.260}
)
You can save the data to json format, or else write a DSSAT-format file back to disk.
# Convert to dict...
json_d = soil.to_dict()
# ...or save to disk
wth.write('path/to/my/new/SOIL.SOL')
Output files¶
Output files can only be read from, not written to. The following classes are available:
PlantGroOut
(PlantGro.OUT)SoilNiOut
(SoilNi.OUT)SoilTempOut
(SoilTemp.OUT)SummaryOut
(Summary.OUT)
These classes can all be used to read the corresponding output file. Note that it is generally more straightforward
and pleasant to use a DSSATResults
object instead of accessing individual output files
directly.
from tradssat.out import PlantGroOut
plant = PlantGroOut('path/to/my/PlantGro.OUT')
# Get Leaf Area Index time series
plant.get_value('LAID')
High-level interface¶
The high-level interface simplifies many tasks while working with DSSAT input and output files.
Input files¶
Each of these managers creates a more friendly interface to DSSAT files than that of the base input file reader.
Experiment file manager¶
DSSATRun
allows you to read, edit and write experiments, with automatic access to referenced
external soil, genetic and weather files. TraDSSAT will also automagically manage the link between DSSAT treatments
and associated factor levels (if you don’t know what this means, now would be a great time to stop reading this and
read the DSSAT docs instead).
from tradssat import DSSATRun
run = DSSATRun('path/to/my/experiment.EXP')
# Get cultivar for treatment 1
run.get_trt_val('INGENO', trt=1)
# Change level of treatment factor
run.set_trt_factor_level(trt=1, factor='CULTIVARS', level=2)
# Change value of a factor level (in this case cultivar type)
run.set_factor_level_val('INGENO', 'IB0067', level=1)
# Access soil variable SLLL for treatment 2
run.get_trt_val('SLLL', trt=2)
Genetic file manager¶
DSSAT’s crop modules generally split coefficients between cultivar, ecotype and species files. TraDSSAT provides a
GeneticMgr
class to automagically manage all genetic coefficients for a particular crop
and cultivar type.
from tradssat import GeneticMgr
gen = GeneticMgr(crop='MZIXM', cult='PC0001')
# Returns P1 for MZIXM cultivar PC0001
gen.get_value('P1')
# Returns ecotype variable TOPT for cultivar PC001
gen.get_value('TOPT')
Note
It is currently not possible to access species coefficients with traDSSAT, because these are in practice model constants and should not be written or changed (and, if they were, would also by default affect all future DSSAT simulations run on your DSSAT installation). More practically, they also come in a variety of formats and would be a pain to parse.
Soil file manager¶
Soil files can be hard to manage, since they can contain data for many different soils in the same file. With
SoilMgr
, simply pass the soil code and traDSSAT will find the correct file and file section.
from tradssat.mgrs import SoilMgr
soil_mgr = SoilMgr('IB00000005')
soil_mgr.get_value('SLU1')
Weather file manager¶
Don’t feel like finding your weather file yourself? Just give the station code to
WeatherFileMgr
and let it find it for you.
from tradssat.mgrs import WeatherFileMgr
wth_mgr = WeatherFileMgr('ACNM')
wth_mgr.get_value('RAIN')
Output files¶
You can access output from a run using a DSSATResults
object instantiated with the output
directory.
from tradssat import DSSATResults
out = DSSATResults('path/to/my/output/dir')
# Get FWAD results for treatment 1 (as a time series)
out.get_value('FWAD', trt=1)
# Get results at specific time
# Get result at 13 days after start
out.get_value('FWAD', trt=1, t=13, at='DAS')
# Get result at 13 days after planting
out.get_value('FWAD', trt=1, t=13, at='DAP')
# Get result at 123th day of year 1989
out.get_value('FWAD', trt=1, t='1989 123', at='DOY')
API Reference¶
TraDSSAT allows users to access files directly or to manage them through the high-level interface.
File managers¶
All DSSAT file reading and editing happens through subclasses of these parent classes.
High-level API¶
The high-level API simplifies work with more complex DSSAT files, espcially ones that reference each other.
Run input managers¶
-
class
tradssat.mgrs.
DSSATRun
(file, model=None)[source]¶ General manager for DSSAT run input files.
-
add_factor_level
(factor, vals)[source]¶ Adds a factor level to the experiment.
Parameters: - factor (str) – Factor code or name.
- vals (dict) – Dictionnary of variable values for that factor level’s variables. Missing variables will be assigned the corresponding missing code (usually -99).
-
add_treatment
(name, ops=None, factors=None)[source]¶ Adds a treatment level to the experiment.
Parameters: - name (str) – Name of the new treatment.
- ops (dict) – Dictionnary of values for the R, O, and C treatment level options (optional).
- factors (dict) – Dictionnary of values for the treatment’s factor levels (optional). Missing factors will be assigned level 0.
-
get_factor_level_val
(var, level)[source]¶ Obtain the variable value for a specific factor level.
Parameters: - var (str) – The variable of interest.
- level (int) – The factor level corresponding to the specified variable.
Returns: The variable value at the factor level.
Return type: np.ndarray
-
get_general_val
(var)[source]¶ Obtain a variable value from the GENERAL section of the EXP file.
Parameters: var (str) – Variable name Returns: Variable value. Return type: np.ndarray
-
get_trt_factor_level
(trt, factor)[source]¶ Obtain the factor level for a specific treatment.
Parameters: - trt (str | int) – Treatment name or number.
- factor (str) – Factor name or code.
Returns: The factor level corresponding to the given treatment.
Return type: int
-
get_trt_name
(n)[source]¶ Returns the treatment name corresponding to a treatment number.
Parameters: n (int) – The treatment number. Returns: The treatment name. Return type: str
-
get_trt_num
(trt)[source]¶ Returns the treatment number corresponding to a specified treatment name.
Parameters: trt (str) – The treatment name. Returns: The corresponding treatment number. Return type: int
-
get_trt_val
(var, trt)[source]¶ Returns the value of a treatment’s variable.
Parameters: - var (str) – The variable of interest.
- trt (int | str) – The treatment name or number.
Returns: The variable value for the treatment.
Return type: np.ndarray
-
remove_treatment
(trt)[source]¶ Removes a treatment from the experiment.
Parameters: trt (str | int) – Treatment name or number.
-
set_general_val
(var, val)[source]¶ Sets a variable value in the GENERAL section of the EXP file.
Parameters: - var (str) – Variable name.
- val (str | float | int | np.ndarray) – New value for the variable.
-
Run output manager¶
-
class
tradssat.mgrs.
DSSATResults
(folder)[source]¶ Facilitates the reading of DSSAT results. Instead of having to read each output file individually, you can simply point this class to a DSSAT run output folder containing the output files and it will find the desired variables for you.
-
get_final_value
(var, trt)[source]¶ Returns a variable’s final value. Faster than
get_value()
if the variable is available in Summary.OUT.Parameters: - var (str) – The variable name.
- trt (int) – The treatment number of interest.
Returns: The variable’s final value.
Return type: str | float | int
-
get_value
(var, trt, t=None, at='YEAR DOY')[source]¶ Returns the value (point or time-series) of a variable from a DSSAT run.
Parameters: - var (str) – The variable name.
- trt (int) – The treatment number of interest.
- t (str | int) – The time at which one wants the results. If
None
, results will be given for all time steps. - at (str) – Must be one of
DAS
(days after start),DAP
(days after planting), orYEAR DOY
(year, day of year; default). Only used ift
is notNone
.
Returns: The value of the variable.
Return type: np.ndarray
-
traDSSAT internals¶
This references traDSSAT’s (mostly) internal API and is really only useful if you are planning on contributing to or changing a few things in the source code itself.
Variables¶
Variable types used to specify DSSAT file variables.
-
class
tradssat.tmpl.var.
CharacterVar
(name, size, spc=1, sect=None, header_fill=' ', miss='-99', info='')[source]¶ Character (string) variable.
-
type_
¶ alias of
builtins.str
-
-
class
tradssat.tmpl.var.
FloatVar
(name, size, dec, lims=None, spc=1, sect=None, header_fill=' ', miss='-99', info='')[source]¶ Floating point (decimal) variable.
-
type_
¶ alias of
builtins.float
-
-
class
tradssat.tmpl.var.
HeaderVariableSet
(d_vars)[source]¶ Organiser for the allowed header variables of a DSSAT file type.
-
class
tradssat.tmpl.var.
IntegerVar
(name, size, lims=None, spc=1, sect=None, header_fill=' ', miss='-99', info='')[source]¶ Integer (whole number) variable.
-
type_
¶ alias of
builtins.int
-
-
class
tradssat.tmpl.var.
NumericVar
(name, size, lims, spc, header_fill, miss, sect, info)[source]¶ Parent class for numeric variable types.
Values¶
Internal structure to organise DSSAT file data and variable values.
-
class
tradssat.tmpl.vals.
FileValueSet
[source]¶ Represents the set of values in a DSSAT file.
-
add_row
(sect, subsect=None, vals=None)[source]¶ Adds a row to the file.
Parameters: - sect (str) – Name of section.
- subsect (int) – Subsection number. If
None
, will add row to all subsections. - vals (dict) – Dictionnary of new row variable values.
-
add_section
(name)[source]¶ Adds a section to the file.
Parameters: name (str) – Name of the new section.
-
-
class
tradssat.tmpl.vals.
HeaderValues
[source]¶ Represents DSSAT file header variables and their values.
-
changed
()[source]¶ Checks if the header variables values have been changed.
Returns: Return type: bool
-
get_value
(var)[source]¶ Obtain the value of a header variable.
Parameters: var (str) – The variable of interest. Returns: The value of the variable. Return type: np.ndarray
-
set_vars
(subsect)[source]¶ Sets the variables of the header.
Parameters: subsect (ValueSubSection) – The subsection with variables and their values already specified.
-
-
class
tradssat.tmpl.vals.
ValueSection
(name)[source]¶ Represents the structure and variable values in a DSSAT file section.
-
add_subsection
(subsect)[source]¶ Add a subsection to the section.
Parameters: subsect (ValueSubSection) – The new subsection.
-
get_header_var
(var)[source]¶ Obtain the value of a header variable.
Parameters: var (str) – The name of the variable Returns: The value of the header variable. Return type: np.ndarray
-
set_header_vars
(h_vars)[source]¶ Sets the section’s header variables.
Parameters: h_vars (ValueSubSection) – The subsection representing the header variables.
-
-
class
tradssat.tmpl.vals.
ValueSubSection
(l_vars, l_vals)[source]¶ Represents the variables and values in a DSSAT file subsection.
-
add_row
(vals=None)[source]¶ Adds a row to the subsection.
Parameters: vals (dict) – The values for the new row. Any missing value will be assigned the corresponding missing code for that variable (usually -99).
-
changed
()[source]¶ Checks whether any variable in the subsection has been changed.
Returns: Return type: bool
-
check_dims
()[source]¶ Checks that all variables in the subsection have the same size. (If not, the subsection cannot write to disk.)
Raises: ValueError
– If not all variables have the same size.
-
-
class
tradssat.tmpl.vals.
VariableValue
(var, val)[source]¶ Represents a DSSAT file variable.
-
add_value
(val)[source]¶ Adds a value to the variable’s matrix.
Parameters: val (int | float) – The new value.
-
remove_value
(i)[source]¶ Removes a value from the variable’s matrix.
Parameters: i (np.ndarray) – The indices of the value(s) to remove. May be a list of indices, or else a boolean mask of the same size as the variable.
-
Input and output files¶
All file objects, whether input or output, inherit from the abstract class File
and all of its methods.
-
class
tradssat.tmpl.
InpFile
(file)[source]¶ Parent class for all input files, as well as for Summary.OUT.
File template¶
All file objects, whether input or output, inherit from the abstract class File
and all of its methods.
-
class
tradssat.tmpl.
File
(file)[source]¶ Parent class for all file objects.
-
get_value
(var, sect=None, subsect=None, cond=None)[source]¶ Parameters: - var –
- sect –
- subsect –
- cond –
Returns: Return type: np.ndarray
-
get_var_size
(var, sect=None)[source]¶ Returns the size of a variable.
Parameters: - var (str) – The name of the variable.
- sect (str) – The name of the section in which this variable appears (optional; for ambiguous cases where a file has several variables with the same code).
Returns: The size of the variable.
Return type: int
-
Contributing¶
There are a few ways to contribute to traDSSAT…