def plot_all_models(T,P):
    models = ["susong1999","piecewise_susong1999","marks2017"]
    fig, axarray = plt.subplots(1,len(models), sharex='col', sharey='row')

    for  z,f in enumerate(models):
        ax = axarray[z]
        density,perc_snow = calc_phase_and_density(T,PP,nasde_model = f)
        plot_snow_model(PP,T,density,ax)
    plt.show()
Exemple #2
0
    def distribute_for_susong1999(self, data, ppt_temp, time, mask=None):
        """
        Docs for susong1999
        """

        if data.sum() > 0:

            # distribute data and set the min/max
            self._distribute(data)
            # see if the mass threshold has been passed
            self.precip = utils.set_min_max(self.precip, self.min, self.max)

            # determine the precip phase and den
            snow_den, perc_snow = snow.calc_phase_and_density(
                ppt_temp, self.precip, nasde_model=self.nasde_model)

            # determine the time since last storm
            stormDays, stormPrecip = storms.time_since_storm(
                self.precip,
                perc_snow,
                time_step=self.time_step / 60 / 24,
                mass=self.ppt_threshold,
                time=self.time_to_end_storm,
                stormDays=self.storm_days,
                stormPrecip=self.storm_total)

            # save the model state
            self.percent_snow = perc_snow
            self.snow_density = snow_den
            self.storm_days = stormDays
            self.storm_total = stormPrecip

        else:

            self.storm_days += self.time_step / 60 / 24

            # make everything else zeros
            self.precip = np.zeros(self.storm_days.shape)
            self.percent_snow = np.zeros(self.storm_days.shape)
            self.snow_density = np.zeros(self.storm_days.shape)

        # day of last storm, this will be used in albedo
        self.last_storm_day = utils.water_day(data.name)[0] - \
            self.storm_days - 0.001

        # get the time since most recent storm
        if mask is not None:
            self.last_storm_day_basin = np.max(mask * self.last_storm_day)
        else:
            self.last_storm_day_basin = np.max(self.last_storm_day)
def plot_compare_curve_fit(PP,T,model_name,curve):
        models = [model_name,"curve_fit","residual"]
        fig, axarray = plt.subplots(1,len(models), sharex='col', sharey='row')

        for  z,f in enumerate(models):
            ax = axarray[z]
            if f == model_name:
                density,perc_snow = calc_phase_and_density(T,PP,nasde_model = f)
                density_1 = density
            elif f=="curve_fit":
                density = np.zeros(T.shape)

                for (i,j),t in np.ndenumerate(T):
                    y = t
                    x = PP[i][j]
                    density[i][j] = curve.evalf()
                density_2 = density
                plot_snow_model(PP,T,density,ax)

            else:
                residual = abs(density_2- density_1)
                pc = ax.pcolormesh(PP,T,residual)
def main():
    tmax = 5
    tmin = -15
    pmax = 1000
    pmin = 0
    t_delta = 0.01
    pp_delta = 1

    model = 'marks2017'

    #Create input/output arrays and generate  a mesh
    T_range = np.arange(tmin,tmax,t_delta)
    PP_range = np.arange(pmin,pmax,pp_delta)
    PP,T = np.meshgrid(PP_range,T_range)
    order = 2
    density,perc_snow = calc_phase_and_density(T,PP,nasde_model = model)
    coef,residual = curve_fit(PP,T,density,order = order)
    print "Coefficients: {0}".format(coef)
    print "Residual = {0}".format(residual)

    equation = generate_equation(coef,order = order)
    plot_compare_curve_fit(PP,T,model,equation)
Exemple #5
0
    def distribute_for_marks2017(self, data, precip_temp, ta, time, mask=None):
        """
        Specialized distribute function for working with the new accumulated
        snow density model Marks2017 requires storm total and a corrected
        precipitation as to avoid precip between storms.
        """
        #self.corrected_precip # = data.mul(self.storm_correction)

        if data.sum() > 0.0:
            # Check for time in every storm
            for i, s in self.storms.iterrows():
                storm_start = s['start']
                storm_end = s['end']

                if time >= storm_start and time <= storm_end:
                    # establish storm info
                    self.storm_id = i
                    storm = self.storms.iloc[self.storm_id]
                    self.storming = True
                    break
                else:
                    self.storming = False

            self._logger.debug("Storming? {0}".format(self.storming))
            self._logger.debug("Current Storm ID = {0}".format(self.storm_id))

            # distribute data and set the min/max
            self._distribute(data, zeros=None)
            self.precip = utils.set_min_max(self.precip, self.min, self.max)

            if time == storm_start:
                # Entered into a new storm period distribute the storm total
                self._logger.debug('{0} Entering storm #{1}'.format(
                    data.name, self.storm_id + 1))
                if precip_temp.min() < 2.0:
                    self._logger.debug('''Distributing Total Precip
                                        for Storm #{0}'''.format(
                        self.storm_id + 1))
                    self._distribute(storm[self.stations].astype(float),
                                     other_attribute='storm_total')
                    self.storm_total = utils.set_min_max(
                        self.storm_total, self.min, self.max)

            if self.storming and precip_temp.min() < 2.0:
                self._logger.debug('''Calculating new snow density for
                                    storm #{0}'''.format(self.storm_id + 1))
                # determine the precip phase and den
                snow_den, perc_snow = snow.calc_phase_and_density(
                    precip_temp, self.precip, nasde_model=self.nasde_model)

            else:
                snow_den = np.zeros(self.precip.shape)
                perc_snow = np.zeros(self.precip.shape)

            # calculate decimal days since last storm
            self.storm_days = storms.time_since_storm_pixel(
                self.precip,
                precip_temp,
                perc_snow,
                storming=self.storming,
                time_step=self.time_step / 60.0 / 24.0,
                stormDays=self.storm_days,
                mass=self.ppt_threshold)

        else:
            self.storm_days += self.time_step / 60.0 / 24.0
            self.precip = np.zeros(self.storm_days.shape)
            perc_snow = np.zeros(self.storm_days.shape)
            snow_den = np.zeros(self.storm_days.shape)

        # save the model state
        self.percent_snow = perc_snow
        self.snow_density = snow_den

        # day of last storm, this will be used in albedo
        self.last_storm_day = utils.water_day(data.name)[0] - \
            self.storm_days - 0.001

        # get the time since most recent storm
        if mask is not None:
            self.last_storm_day_basin = np.max(mask * self.last_storm_day)
        else:
            self.last_storm_day_basin = np.max(self.last_storm_day)