ecohydro / maize-Toff

Repo for modeling analysis of of maize yield variability and tradeoffs between yield and crop failure.
4 stars 2 forks source link

Why do we need planting date in two places in `model.py`? #49

Closed Ntkrell closed 4 years ago

Ntkrell commented 4 years ago

Currently planting_date is set in two places in model.py. I played around with removing it from init but that didn't seem right. Additionally, as-is model.py has some problems:

Here's the code I was playing with:

Start by importing packages and objects


# import packages and set working directory
import numpy as np
import matplotlib.pyplot as plt
import os
from math import exp
import pandas as pd
import sys

# We need to add the module path to our system path so 
# so that our notebook can find our local objects and code:
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
    sys.path.append(module_path)

# import objects
from farm import Climate
from farm import Soil
from farm import Crop
from farm import CropModel
from farm.make_climate_parameters import make_climate_parameters

Example 1

alpha_r, lambda_r = make_climate_parameters()

climate = Climate(alpha_r, lambda_r)
soil = Soil('loam')
crop = Crop(soil=soil)
soil.set_nZr(crop)  
model = CropModel(crop=crop,soil=soil,climate=climate)

model.run(planting_date=75)

o = model.output()
o['kc'].plot()
o

Here the DOY starts on Julian day 54. This makes sense given the calculation: planting date (75) - t_before (21). The model is ending on day 208 (zero-indexed) whereas it should end on day 262 (calculated by doy_end = 75 + 180 + 7). image

Example 2

alpha_r, lambda_r = make_climate_parameters()

climate = Climate(alpha_r, lambda_r)
soil = Soil('loam')
crop = Crop(soil=soil)
soil.set_nZr(crop)  
model = CropModel(crop=crop,soil=soil,climate=climate)

model.run(planting_date=2)

Get the error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-2-9a2956d9a135> in <module>
      7 model = CropModel(crop=crop,soil=soil,climate=climate)
      8 
----> 9 model.run(planting_date=2)
     10 
     11 o = model.output()

~/Box Sync/waves/maize-Toff/farm/model.py in run(self, s0, planting_date, t_before, t_after)
    123 
    124         for t in range(self.n_days):
--> 125             self.R[t] = self.climate.rainfall[doy[t]-1]
    126 
    127         self.doy = doy

IndexError: index 0 is out of bounds for axis 0 with size 0

Example 3

alpha_r, lambda_r = make_climate_parameters()

climate = Climate(alpha_r, lambda_r)
soil = Soil('loam')
crop = Crop(soil=soil)
soil.set_nZr(crop)  
model = CropModel(crop=crop,soil=soil,climate=climate)

model.run(planting_date=200)

o = model.output()
o['kc'].plot()
o

Here it looks like the plant never starts to grow. dos never switches to "1" and we can see that the crop coefficient is always 0.3. image