|
1 | 1 | """ |
2 | | -File X module. |
3 | | -
|
4 | | -
|
5 | | -
|
6 | | - SimulationOptions and Treatment: |
7 | | - They are only to represent those sections. |
| 2 | +This module implement the sections of the DSSAT FileX as python objects. All |
| 3 | +sections are implemented, excepting the enviromental modifications section. |
| 4 | +Environmental modifications can be easily implemented by modifying the Weather |
| 5 | +component of each experiment. One section object represents a single factor |
| 6 | +level in the experiment. |
| 7 | +
|
| 8 | +Each section is defined using the same parameter names (lowercase) of the DSSAT |
| 9 | +FileX. For example, planting date is defined as follow: |
| 10 | + >>> planting = Planting( |
| 11 | + >>> pdate=date(1980, 6, 17), ppop=18, ppoe=18, |
| 12 | + >>> plme='S', plds='R', plrs=45, plrd=0, pldp=5 |
| 13 | + >>> ) |
| 14 | +
|
| 15 | +Sections that include a schedule or soil profile (i.e. Initial conditions), are |
| 16 | +defined as a list of individual events or soil layers. For example, a fertilizer |
| 17 | +section with two fertilizer events is defined as follows: |
| 18 | + >>> fertilizer = Fertilizer(table=[ |
| 19 | + >>> FertilizerEvent( |
| 20 | + >>> fdate=date(1980, 7, 4), fmcd='FE005', fdep=5, |
| 21 | + >>> famn=80, facd='AP002' |
| 22 | + >>> ), |
| 23 | + >>> FertilizerEvent( |
| 24 | + >>> fdate=date(1980, 8, 7), fmcd='FE005', fdep=5, |
| 25 | + >>> famn=80, facd='AP002' |
| 26 | + >>> ) |
| 27 | + >>> ]) |
| 28 | +Other sections based on events are irrigation, residue, chemical, and tillage. |
| 29 | +
|
| 30 | +Note that the Fertilizer object is initialized by passing a list of FertilizerEvent |
| 31 | +objects in the 'table' parameter. The table parameter also accepts DataFrames, |
| 32 | +only if the column names of that DataFrame match the parameters for the individual |
| 33 | +event or layer object. Next is an example of this for the initial conditions section: |
| 34 | + >>> initial_conditions = InitialConditions( |
| 35 | + >>> pcr='SG', icdat=date(1980, 6, 1), icrt=500, icnd=0, |
| 36 | + >>> icrn=1, icre=1, icres=1300, icren=.5, icrep=0, icrip=100, icrid=10, |
| 37 | + >>> table=pd.DataFrame([ |
| 38 | + >>> (10, .06, 2.5, 1.8), |
| 39 | + >>> (22, .06, 2.5, 1.8), |
| 40 | + >>> (52, .195, 3., 4.5), |
| 41 | + >>> (82, .21, 3.5, 5.0), |
| 42 | + >>> (112, 0.2, 2., 2.0), |
| 43 | + >>> (142, 0.2, 1., 0.7), |
| 44 | + >>> (172, 0.2, 1., 0.6), |
| 45 | + >>> ], columns=['icbl', 'sh2o', 'snh4', 'sno3']) |
| 46 | + >>> ) |
| 47 | +Note that InitialConditions have more parameters besides the table parameter. The |
| 48 | +column names of the passed DataFrame are the same as the parameters for the |
| 49 | +InitialConditionsLayer class. The soil analysis section is the other section based |
| 50 | +on soil layers. |
| 51 | +
|
| 52 | +The field and cultivar sections are the only sections that need other DSSATTools |
| 53 | +objects as input parameters. In this section, the 'wsta' must be a WeatherStation |
| 54 | +object, and the 'id_soil' must be a SoilProfile object. |
| 55 | + >>> field = Field( |
| 56 | + >>> id_field='ITHY0001', wsta=weather_station, flob=0, fldt='DR000', |
| 57 | + >>> fldd=0, flds=0, id_soil=soil |
| 58 | + >>> ) |
| 59 | +'wsta' and 'id_soil' also receives weather station and soil profile ids as strings. |
| 60 | +However, this wouldn't make sense in the context of running DSSAT using this package. |
| 61 | +
|
| 62 | +The cultivar can be defined either using the Cultivar class, or by directly using the |
| 63 | +one of the classes defined in DSSATTools.crop. |
| 64 | + >>> cultivar = Sorghum('IB0026') |
| 65 | + >>> cultivar = Cultivar(cr='SG', ingeno='IB0026', cname='CSH-1') |
| 66 | +These two definitions will yield the same result. However, when the cultivar is |
| 67 | +defined using the crop class it is posible to modify the cultivar and ecotype |
| 68 | +coefficients. More information is found at the DSSATTools.crop module documentation. |
| 69 | + |
| 70 | +Finally, the simulations controls is created by using the SimulationsControls |
| 71 | +class. Each sub-section of the simulation controls sections is created using its |
| 72 | +own class. Next example shows how to create the simulations controls defining only |
| 73 | +the general options. |
| 74 | + >>> simulation_controls = SimulationControls( |
| 75 | + >>> general=SCGeneral(sdate=date(1980, 6, 1)) |
| 76 | + >>> ) |
| 77 | +
|
| 78 | +The sections can be created from an existing FileX using the read_filex function. |
| 79 | +That function will return a dictionary, mapping each treatment to its correspondent |
| 80 | +section definitions. The next example reads an existing FileX, and then assigns |
| 81 | +the first treatment to the treatment variable. In this case, treatment is a |
| 82 | +dictionary mapping each section name to its python object. |
| 83 | + >>> treatments = read_filex("Maize/BRPI0202.MZX") |
| 84 | + >>> treatment = treatments[1] |
| 85 | +
|
| 86 | +The create_filex function returns the string of the FileX for for the passed |
| 87 | +sections defined as their python objects. |
8 | 88 | """ |
9 | 89 | from datetime import date |
10 | 90 | from .base.partypes import ( |
@@ -1709,14 +1789,14 @@ def read_filex(filexpath): |
1709 | 1789 | ) |
1710 | 1790 | return treatments |
1711 | 1791 |
|
1712 | | -def write_filex(field:Field, cultivar:Cultivar, planting:Planting, |
| 1792 | +def create_filex(field:Field, cultivar:Cultivar, planting:Planting, |
1713 | 1793 | simulation_controls:SimulationControls, harvest:Harvest=None, |
1714 | 1794 | initial_conditions:InitialConditions=None, |
1715 | 1795 | fertilizer:Fertilizer=None, soil_analysis:SoilAnalysis=None, |
1716 | 1796 | irrigation:Irrigation=None, residue:Residue=None, |
1717 | 1797 | chemical:Chemical=None, tillage:Tillage=None): |
1718 | 1798 | """ |
1719 | | - Returns the FileX string |
| 1799 | + Returns the FileX as a string |
1720 | 1800 | """ |
1721 | 1801 | experiment_name = field["id_field"][:4] +\ |
1722 | 1802 | simulation_controls["general"]["sdate"].strftime('%y01') + cultivar.code |
|
0 commit comments