Example #1
0
 def setUpClass(cls):
     cls.setUpData()
     calculate_soil_water(
         theta_s=0.5,
         theta_fc=0.4,
         theta_wp=0.1,
         zr=0.95,
         zr_factor=1000,
         p=0.5,
         draintime=28.6,
         timeseries=cls.df,
         theta_init=0.4,
         refill_factor=0.5,
     )
Example #2
0
 def setUp(self):
     data = {
         "effective_precipitation": [0, 4.8],
         "actual_net_irrigation": [0, 0],
         "crop_evapotranspiration": [3.871, 3.990],
     }
     self.df = pd.DataFrame(data, index=pd.date_range("2019-03-07", periods=2))
     calculate_soil_water(
         theta_s=0.425,
         theta_fc=0.287,
         theta_wp=0.14,
         zr=0.5,
         zr_factor=1000,
         p=0.5,
         draintime=16.2,
         timeseries=self.df,
         theta_init=0.252784,
         mif=None,
     )
Example #3
0
 def setUp(self):
     data = {
         "effective_precipitation": [0, 5.544, 2.664],
         "actual_net_irrigation": [0, 0, 0],
         "crop_evapotranspiration": [1.043, 0.952, 0.868],
     }
     self.df = pd.DataFrame(data, index=pd.date_range("2016-03-10", periods=3))
     calculate_soil_water(
         theta_s=0.425,
         theta_fc=0.287,
         theta_wp=0.14,
         zr=0.5,
         zr_factor=1000,
         p=0.5,
         draintime=16.3,
         timeseries=self.df,
         theta_init=0.284732,
         mif=1.0,
     )
Example #4
0
 def setUp(self):
     data = {
         "effective_precipitation": [0, 0, 0],
         "actual_net_irrigation": [True, True, True],
         "crop_evapotranspiration": [3.647, 3.822, 3.885],
     }
     self.df = pd.DataFrame(data, index=pd.date_range("2019-03-07", periods=3))
     calculate_soil_water(
         theta_s=0.425,
         theta_fc=0.287,
         theta_wp=0.14,
         zr=0.5,
         zr_factor=1000,
         p=0.5,
         draintime=16.2,
         timeseries=self.df,
         theta_init=0.223646,
         mif=1.0,
     )
Example #5
0
 def run_swb_model(self):
     return calculate_soil_water(
         timeseries=self.timeseries,
         theta_s=float(self.theta_s),
         theta_fc=self.field_capacity,
         theta_wp=self.wilting_point,
         zr=self.root_depth,
         zr_factor=1000,
         p=float(self.p),
         draintime=self.draintime,
         theta_init=self.field_capacity,
         mif=self.irrigation_optimizer,
     )
Example #6
0
 def setUp(self):
     data = {
         "effective_precipitation": [0, 80],
         "actual_net_irrigation": [0, 0],
         "crop_evapotranspiration": [50, 0.1],
     }
     self.df = pd.DataFrame(data, index=pd.date_range("2016-03-10", periods=2))
     result = calculate_soil_water(
         theta_s=0.425,
         theta_fc=0.287,
         theta_wp=0.14,
         zr=0.5,
         zr_factor=1000,
         p=0.5,
         draintime=16.3,
         timeseries=self.df,
         theta_init=0.15,
         mif=1.0,
     )
     self.taw = result["taw"]
Example #7
0
def calculate_performance_chart(agrifield):
    results = Results()

    # Verify that the agrifield is in study area
    if not agripoint_in_raster(agrifield):
        return results

    # Extract data from files withe pd.DataFrame and end, start dates
    dTimeseries = extractSWBTimeseries(agrifield)

    # Agrifield parameters
    theta_fc = agrifield.get_field_capacity
    zr = (float(agrifield.get_root_depth_min) +
          float(agrifield.get_root_depth_max)) / 2
    zr_factor = 1000
    irr_eff = float(agrifield.get_efficiency)
    a = dTimeseries["draintime_A"]
    b = dTimeseries["draintime_B"]

    theta_init = theta_fc

    # We want the model to run ignoring the actual net irrigation, and instead assume
    # that the actual irrigation equals recommended irrigation.
    saved_actual_net_irrigation = dTimeseries["timeseries"][
        "actual_net_irrigation"]
    dTimeseries["timeseries"]["actual_net_irrigation"] = True

    model_params = dict(
        theta_s=float(agrifield.get_thetaS),
        theta_fc=theta_fc,
        theta_wp=agrifield.get_wilting_point,
        zr=zr,
        zr_factor=zr_factor,
        p=float(agrifield.get_mad),
        draintime=round(a * zr**b),
        theta_init=theta_init,
        mif=agrifield.get_irrigation_optimizer,
        timeseries=dTimeseries["timeseries"],
    )

    dresults = calculate_soil_water(**model_params)
    df = dresults["timeseries"]
    df["ifinal"] = df.apply(lambda x: x.recommended_net_irrigation / irr_eff,
                            axis=1)

    # We restore the actual net irrigation, which we had ignored for the moment,
    # so that the chart can show it.
    df["actual_net_irrigation"] = saved_actual_net_irrigation

    results.chart_dates = [date.date() for date, row in df.iterrows()]
    results.chart_ifinal = [row.ifinal for date, row in df.iterrows()]
    results.chart_peff = [
        row.effective_precipitation for date, row in df.iterrows()
    ]
    results.chart_irr_period_peff_cumulative = sum(results.chart_peff)

    results.applied_water = []
    results.applied_water = [
        row.actual_net_irrigation if row.actual_net_irrigation else 0
        for date, row in df.iterrows()
    ]
    cache.set("performance_chart_{}".format(agrifield.id), results, None)
Example #8
0
def execute_model(agrifield):
    """
    Run SoilWaterBalance model on agrifield
    """

    results = Results()

    # Verify that the agrifield is in study area
    if not agripoint_in_raster(agrifield):
        return results

    # Calculate crop evapotranspiration at the agrifield
    dTimeseries = extractSWBTimeseries(agrifield)

    # Set some variables for the template. results.irrigation_log_not_exists is True if
    # there's no irrigation event. Otherwise, results.irrigation_log_outside_time_period
    # is True if, well, what the variaable name says. All this probably shouldn't be
    # here, but in views.
    if not agrifield.irrigationlog_set.exists():
        results.irrigation_log_not_exists = True
    last_irrigation = (agrifield.irrigationlog_set.latest()
                       if agrifield.irrigationlog_set.exists() else None)
    start_date_daily = dTimeseries["start"].date()
    end_date_daily = dTimeseries["end"].date()
    if last_irrigation and ((last_irrigation.time.date() < start_date_daily) or
                            (last_irrigation.time.date() > end_date_daily)):
        last_irrigation = None
        results.irrigation_log_outside_time_period = True
    results.last_irr_date = last_irrigation.time.date(
    ) if last_irrigation else None

    # Run swb model and calculate some simple additional columns
    zr = (float(agrifield.get_root_depth_min) +
          float(agrifield.get_root_depth_max)) / 2
    zr_factor = 1000
    irr_eff = float(agrifield.get_efficiency)
    a = dTimeseries["draintime_A"]
    b = dTimeseries["draintime_B"]
    dresults = calculate_soil_water(
        theta_s=float(agrifield.get_thetaS),
        theta_fc=agrifield.get_field_capacity,
        theta_wp=agrifield.get_wilting_point,
        zr=zr,
        zr_factor=zr_factor,
        p=float(agrifield.get_mad),
        draintime=round(a * zr**b),
        theta_init=agrifield.get_field_capacity,
        mif=agrifield.get_irrigation_optimizer,
        timeseries=dTimeseries["timeseries"],
    )
    df = dresults["timeseries"]
    df["advice"] = df.apply(lambda x: x.dr > dresults["raw"], axis=1)
    df["ifinal"] = df.apply(lambda x: x.recommended_net_irrigation / irr_eff,
                            axis=1)
    df["ifinal_m3"] = df.apply(lambda x: (x.ifinal / 1000) * agrifield.area,
                               axis=1)

    # Set some variables in the "results"; these will be used by the template.
    results.sd = dTimeseries["start"].date()
    results.ed = dTimeseries["end"].date()
    results.sdh = dTimeseries["fstart"].date()
    results.edh = dTimeseries["fend"].date()
    results.adv = any([
        row.advice for date, row in df.iterrows()
        if date >= pd.Timestamp(results.sdh)
    ])
    results.ifinal = df.ix[-1, "ifinal"]
    results.ifinal_m3 = (results.ifinal / 1000) * agrifield.area
    results.adv_sorted = [[date.date(), row.ks, row.ifinal, row.ifinal_m3]
                          for date, row in df.loc[df["advice"]].iterrows()
                          if date >= pd.Timestamp(results.sdh)]
    results.swb_report = [[
        date.date(),
        row.effective_precipitation,
        row.dr,
        row.theta,
        row.advice,
        row.ks,
        row.ifinal,
    ] for date, row in df.iterrows() if date >= pd.Timestamp(results.sdh)]

    # Save results and return them
    cache.set("model_run_{}".format(agrifield.id), results, None)
    return results