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

Should the length of this output be 27? #53

Closed Ntkrell closed 4 years ago

Ntkrell commented 4 years ago

@kcaylor Maybe you can see the issue more clearly just by looking at it than I can. Why is the output below only 27 days long? I think this has to do with the average_soil_moisture() calculation which is returning a length of 27.

# Part 1. Set conditions for IC runs
n_sim = 500 # change back to 1000 later
planting_date = 1 # Because we want a date with little rainfall

# Part 2. Initialize model with a climate, soil and crop
climate = Climate(station='OL JOGI FARM')
soil = Soil(texture='loam')
crop = Crop(soil=soil, lgp=180)
soil.set_nZr(crop)

model = CropModel(crop=crop, climate=climate, soil=soil)
model.run()
o = model.output() # The output of this length is 208 which is fine.

# Part 3. Get the mean, SD soil moisture and run the simulations to remove IC
s0_mean, s0_std = average_soil_moisture(model, n_sims=n_sim, doy=planting_date)
models = [CropModel(crop=crop, climate=Climate(), soil=soil) for i in np.arange(n_sim)]

# Part 4. Select the planting date we want. 
planting_date = 100

# Part 5: Run the actual simulations
output = [model.run(s0=s0_mean, do_output=True, planting_date=planting_date) for model in models]
output

     kc   LAI    stress          R         s         E        ET         T  \
 0   0.3  0.75  0.211090   0.000000  0.497572  0.881416  1.530082  0.648666   
 1   0.3  0.75  0.231681   0.000000  0.490787  0.846817  1.469218  0.622401   
 2   0.3  0.75  0.252356   0.000000  0.484271  0.814032  1.411212  0.597180   
 3   0.3  0.75  0.273046   0.000000  0.478013  0.782951  1.355905  0.572955   
 4   0.3  0.75  0.293693   0.000000  0.472000  0.753469  1.303148  0.549679   
 5   0.3  0.75  0.314245  34.984921  0.466221  0.725493  1.252802  0.527309   
 6   0.3  0.75  0.006089   4.667907  0.615810  1.553404  2.659763  1.106360   
 7   0.3  0.75  0.002431   0.000000  0.624715  1.608944  2.749776  1.140832   
 8   0.3  0.75  0.007858   0.000000  0.612521  1.533057  2.626686  1.093629   
 9   0.3  0.75  0.015931   1.421261  0.600872  1.461717  2.510255  1.048539   
 10  0.3  0.75  0.020106   0.000000  0.596043  1.432474  2.462318  1.029845   
 11  0.3  0.75  0.031336   0.000000  0.585124  1.367083  2.354660  0.987576   
 12  0.3  0.75  0.044396   0.000000  0.574682  1.305512  2.252668  0.947156   
 13  0.3  0.75  0.059014   0.000000  0.564692  1.247500  2.155986  0.908486   
 14  0.3  0.75  0.074950   0.000000  0.555131  1.192806  2.064283  0.871476   
 15  0.3  0.75  0.091991   0.026773  0.545977  1.141211  1.977251  0.836040   
 16  0.3  0.75  0.109694   0.000000  0.537328  1.093164  1.895722  0.802558   
 17  0.3  0.75  0.128393   0.000000  0.528921  1.047130  1.817146  0.770016   
 18  0.3  0.75  0.147698   0.000000  0.520862  1.003629  1.742451  0.738823   
 19  0.3  0.75  0.167478   0.000000  0.513135  0.962497  1.671409  0.708911   
 20  0.3  0.75  0.187619   0.000000  0.505723  0.923586  1.603806  0.680220   
 21  0.6  0.75  0.208021   0.000000  0.498611  0.886755  1.539443  0.652688   
 22  0.6  0.75  0.228594   0.000000  0.491784  0.851875  1.478137  0.626262   
 23  0.6  0.75  0.249260   0.000000  0.485229  0.818826  1.419714  0.600888   
 24  0.6  0.75  0.269952   0.000000  0.478934  0.787496  1.364013  0.576517   
 25  0.6  0.75  0.290609   0.000000  0.472885  0.757782  1.310884  0.553102   
 26  0.6  0.75  0.311178   0.000000  0.467072  0.729586  1.260186  0.530600   
 27  0.6  0.75  0.331616   0.000000  0.461483  0.702819  1.211786  0.508967   

       L       dsdt  dos  doy  
 0   0.0  -1.530082  0.0   79  
 1   0.0  -1.469218  0.0   80  
 2   0.0  -1.411212  0.0   81  
 3   0.0  -1.355905  0.0   82  
 4   0.0  -1.303148  0.0   83  
 5   0.0  33.732119  0.0   84  
 6   0.0   2.008144  0.0   85  
 7   0.0  -2.749776  0.0   86  
 8   0.0  -2.626686  0.0   87  
 9   0.0  -1.088995  0.0   88  
 10  0.0  -2.462318  0.0   89  
 11  0.0  -2.354660  0.0   90  
 12  0.0  -2.252668  0.0   91  
 13  0.0  -2.155986  0.0   92  
 14  0.0  -2.064283  0.0   93  
 15  0.0  -1.950478  0.0   94  
 16  0.0  -1.895722  0.0   95  
 17  0.0  -1.817146  0.0   96  
 18  0.0  -1.742451  0.0   97  
 19  0.0  -1.671409  0.0   98  
 20  0.0  -1.603806  0.0   99  
 21  0.0  -1.539443  1.0  100  
 22  0.0  -1.478137  2.0  101  
 23  0.0  -1.419714  3.0  102  
 24  0.0  -1.364013  4.0  103  
 25  0.0  -1.310884  5.0  104  
 26  0.0  -1.260186  6.0  105  
 27  0.0  -1.211786  7.0  106  ,
      kc   LAI    stress          R         s         E        ET         T  \
 0   0.3  0.75  0.211090   1.964480  0.497572  0.881416  1.530082  0.648666   
 1   0.3  0.75  0.205418   0.000000  0.499498  0.891322  1.547445  0.656123   
 2   0.3  0.75  0.225974  12.188593  0.492636  0.856201  1.485761  0.629559   

...etc

Question: I think the problem is in the average_soil_moisture() function: shouldn't it return a year's worth of soil moisture? Currently line 177 in functions.py prevents us from doing so: output = [ models[i].run(do_output=True, planting_date=doy+1, t_before=t_before, t_after=0) for i in np.arange(n_sims) ]

kcaylor commented 4 years ago

This looks like the same problem of overwriting variables in the average_soil_moisture function.

Also, the average soil moisture function is only supposed to return the average soil moisture for a specific day. That's why we pass it a DOY.

kcaylor commented 4 years ago

This is the problem. https://docs.python.org/2/library/copy.html

In the average_soil_moisture function, we need to create a copy of the model.crop object. We are still changing the model.crop.lgp value because we are assigning by reference instead of making a copy of the object. It's a python thing.

Ntkrell commented 4 years ago

Sweet! Thanks for the tips and for giving this time!! Will pick back up a little later.