Example #1
0
    def _BSBEC_add_met_data(self, data, met_data, years):
        uids = misc.uniq_key(data, formats.UID)

        for uid in uids:
            for year in years:
                fll_reader = FLLReader(Location(Location.BSBEC, year))
                emergence = fll_reader.get_plot_fll(uid)
                max_date = datetime(year + 1, 3, 10)

                subset = [
                    x for x in data
                    if in_season_uid(x, uid, emergence, max_date)
                ]

                met_data_subset = [
                    x for x in met_data if in_season(x, emergence, max_date)
                ]

                accumulator = MetDataAccumulator(emergence, met_data_subset,
                                                 self._t_base)

                for entry in subset:
                    met_entry = accumulator.get_record(entry[formats.DATE])
                    entry[formats.DD] = met_entry[formats.DD]
                    entry[formats.PAR] = met_entry[formats.PAR]
                    entry[formats.RAINFALL] = met_entry[formats.RAINFALL]

        return data
Example #2
0
    def get_fll_date(self, condition):
        """
        This function gives the FLL info to the CDModel class,
        to be used in simulations"""
        geno = condition[formats.GENOTYPE]
        year = condition['year']
        fll_reader = FLLReader(Location(self._location_name, year))

        return fll_reader.get_genotype_fll(geno)
Example #3
0
    def _fix_start_date(self, years, genotype):
        start_dates = []
        for year in years:
            location = Location(self._location.get_name(), year)
            fll_reader = FLLReader(location)
            fll_date = fll_reader.get_genotype_fll(genotype)
            start_dates.append(int(fll_date.strftime("%j")))

        jul = sum(start_dates) / len(start_dates)
        date_string = '%d %s' % (jul, self._location.get_year())
        start_date = datetime.strptime(date_string, "%j %Y")
        return start_date
Example #4
0
    def create_plot(self, plot_n):
        """
		Create plot object and populate it with the LAI and dd measurements
		"""
        plants = [
            plant for plant in self._plants if plant.get_plot() == plot_n
        ]
        genotype = plants[0].get_genotype()
        leaf_conversion = self._location.get_leaf_conversion(genotype)

        fll_date = FLLReader(self._location).get_plot_fll(plot_n)
        pheno_dates = [
            measurement.get_date()
            for measurement in plants[0].get_pheno_measurements()
        ]
        weekly_dd = self.get_weekly_dd(pheno_dates, self._met_data, fll_date)

        plot = Plot(plot_n, genotype)
        #first liguled leaf stage - LAI and dd are 0
        for plant in plants:
            plot.add_measurement(plant.get_uid(), 0.0, 0.0, fll_date)
            for measurement in plant.get_pheno_measurements():
                LAI = self.calc_LAI(measurement, leaf_conversion)
                dd = next(x['dd'] for x in weekly_dd
                          if x['Date'] == measurement.get_date())
                plot.add_measurement(plant.get_uid(), LAI, dd,
                                     measurement.get_date())

        return plot
Example #5
0
    def _inject_fll(self, data, preps=[], additional_phenos=False):
        # inject at FLL
        uids = misc.uniq_key(data, formats.UID)
        years = set([x[formats.DATE].year for x in data])

        for year in years:
            reader = FLLReader(Location(Location.BSBEC, year))
            for uid in uids:
                fll_date = reader.get_plot_fll(uid)
                entry = {
                    formats.UID: uid,
                    formats.DATE: fll_date,
                    formats.GENOTYPE: misc.assign_geno_bsbec(uid),
                    formats.TRANSMISSION: 1.0,
                    formats.DD: 0.0,
                    formats.PAR: 0.0,
                    formats.LEAF_AREA_INDEX: 0.0,
                    formats.STEM_COUNT: 0.0,
                    formats.CANOPY_HEIGHT: 0.0,
                    formats.DW_PLANT: 0.0
                }

                if additional_phenos:
                    if year < 2016:
                        diam = None
                    else:
                        diam = 0

                    entry[formats.BASE_DIAM_1] = diam
                    entry[formats.BASE_DIAM_2] = diam
                    entry[formats.STEM_DIAM] = diam
                    entry[formats.YEAR] = year
                    entry = misc.row_col_BSBEC(entry)
                    entry[formats.DOY] = int(
                        entry[formats.DATE].strftime("%j"))
                    entry[formats.FLOWERING_SCORE] = 0
                    entry[formats.RAINFALL] = 0

                if preps != []:
                    for prep in preps:
                        new_entry = deepcopy(entry)
                        new_entry[formats.PSEUDO_REP] = prep
                        data.append(new_entry)
                else:
                    data.append(entry)

        return data
Example #6
0
    def simulate(self,
                 location,
                 genotypes,
                 DM_repartition=False,
                 harvest_date=None,
                 end_date=None):

        met_data = MetDataReaderCSV(location).get_met_data()
        result = []
        year = location.get_year()
        fll_reader = FLLReader(self._locations[0])
        plants = PhenoReaderCSV(location, "eey9").get_plants()

        for genotype in genotypes:
            geno_simulation = []

            geno_plants = [x for x in plants if x.get_genotype() == genotype]
            if not end_date:
                end_date = LAIPlateauDetector(geno_plants, location,
                                              genotype).get_plateau()

            #set end date as the LAI plateau point
            geno_plants = [x for x in plants if x.get_genotype() == genotype]

            #get genotype specific coefficients
            LER = [x for x in self._LER if x['Genotype'] == genotype]
            k = [x for x in self._k if x['Genotype'] == genotype]
            #take the second year k value
            if len(k) > 1:
                k = k[1]['k']
            else:
                k = k[0]['k']

            RUE = next(x for x in self._RUE
                       if x['Genotype'] == genotype)['RUE']

            #change the fll year to the current year
            current_date = fll_reader.get_genotype_fll(genotype).replace(
                year=year)
            current_date += timedelta(days=1)
            LAI = 0
            dd = 0
            PAR = 0
            DMY = 0
            while current_date <= end_date:
                geno_simulation.append({
                    'Date': current_date,
                    'Genotype': genotype,
                    'dd': dd,
                    'LAI': LAI,
                    'PAR': PAR,
                    'Yield': DMY
                })

                #delta dd
                met_day = next(x for x in met_data
                               if x['Date'] == current_date)

                delta_dd = degree_days(met_day['t_max'], met_day['t_min'], 0.0)

                #delta LAI
                if len(LER) == 1:
                    current_LER = LER[0]['LER']
                else:
                    if dd < LER[0]['psi']:
                        current_LER = LER[0]['LER']
                    else:
                        current_LER = LER[1]['LER']

                delta_LAI = delta_dd * current_LER

                #delta PAR
                pl = 1 - math.exp(-k * LAI)
                delta_PAR = met_day['PAR'] * pl

                #delta DMY
                delta_DMY = delta_PAR * RUE

                dd += delta_dd
                LAI += delta_LAI
                PAR += delta_PAR
                DMY += delta_DMY
                current_date += timedelta(days=1)

            if DM_repartition:
                final_yield = self.repartition_DM(geno_simulation,
                                                  harvest_date)
                geno_simulation.append({
                    'Date': harvest_date,
                    'Genotype': genotype,
                    'dd': -999,
                    'LAI': -999,
                    'PAR': -999,
                    'Yield': final_yield
                })

            result.append(geno_simulation)

        return result
Example #7
0
    def calc_RUE(self, LER_dict, k_dict, location, LAI):
        met_data = MetDataReaderCSV(location).get_met_data()
        fll_reader = FLLReader(location)
        genotypes = set([x['Genotype'] for x in LER_dict])

        destructive_phenos = CSVFileReader(
            location.get_destr_phenos()).get_content()
        for entry in destructive_phenos:
            entry['Date'] = datetime.strptime(entry['Date'],
                                              "%Y-%m-%d %H:%M:%S UTC")

            try:
                entry['fresh'] = float(
                    entry['Fresh weight above ground material(g)'])
                entry['fresh_sub'] = float(
                    entry['Fresh weight above ground  sub-sample(g)'])
                entry['dry_sub'] = float(
                    entry['dry weight above ground sub-sample(g)'])
            except ValueError:
                try:
                    entry['dry_weight'] = float(
                        entry['dry weight above ground sub-sample(g)'])
                except ValueError:
                    pass
                continue

            if entry['fresh_sub'] == 0.0:
                entry['dry_weight'] = entry['dry_sub']
                continue
            entry['dry_weight'] = entry['fresh'] * (entry['dry_sub'] /
                                                    entry['fresh_sub'])

        destructive_phenos = [
            x for x in destructive_phenos if 'dry_weight' in x
        ]

        #run the simulation per genotype
        RUE = []
        for genotype in genotypes:
            geno_sub = [
                x for x in destructive_phenos if x['Genotype'] == genotype
            ]
            dates = list(set([x['Date'] for x in geno_sub]))
            dates.sort()

            #create data point groups by dates that are close
            #to each other or the same
            groups = []
            group_id = 0
            for date in dates:
                for group in groups:
                    delta = group['Dates'][0] - date
                    days = math.fabs(delta.days)
                    if days and days < 20:
                        group['Dates'].append(date)
                        break
                else:
                    #create new group
                    group = {'id': group_id, 'Dates': [date]}
                    groups.append(group)
                    group_id += 1

            #get the mean dry weight per group
            mean_DW = []
            #add entry for fll day
            fll_date = fll_reader.get_genotype_fll(genotype)
            mean_DW.append({'Date': fll_date, 'Yield': 0.0})

            for group in groups:
                group_phenos = [
                    x for x in geno_sub if x['Date'] in group['Dates']
                ]
                total_dw = 0.0
                for entry in group_phenos:
                    total_dw += entry['dry_weight']

                total_dw /= float(len(group_phenos))

                #correct the group date to the first one in the group
                mean_DW.append({
                    'Date': sorted(group['Dates'])[0],
                    'Yield': total_dw
                })

            #obtain genotype specific coefficients
            LER = [x for x in LER_dict if x['Genotype'] == genotype]
            LER.sort(key=lambda x: x['stage'])
            k = [x for x in k_dict if x['Genotype'] == genotype]
            if len(k) > 1:
                k = next(x['k'] for x in k if x['Year'] == location.get_year())
            else:
                k = sorted(k, key=lambda x: x['Year'])[0]['k']

            #simulate PAR and record values for days of destructive harvests
            real_LAI = [x for x in LAI if x['Genotype'] == genotype]
            mean_DW = self.simulate_PAR(k, LER, met_data, fll_date, mean_DW,
                                        real_LAI)

            #finally work out what the RUE is from
            #the real DMY and simulated PAR values
            temp_file = tempfile.mkstemp()[1] + genotype.split("-")[0]
            CSVFileWriter(temp_file, mean_DW)
            robjects.r('''
				calc_RUE_r <- function(fname){
					data <- read.csv(fname)
					data$Yield <- data$Yield * 2
					fit <- lm(Yield ~ PAR + 0, data = data)
					return(summary(fit)$coefficients[1])
				}
				''')
            calc_RUE_r = robjects.r("calc_RUE_r")
            RUE_val = calc_RUE_r(temp_file)[0]
            RUE.append({'Genotype': genotype, 'RUE': RUE_val})

        return RUE