Exemple #1
0
def run_semsvann_eb(startDate, endDate):
    # TODO: get coordinates from the ObsLocation in regObs
    location_name = 'Semsvannet v/Lo 145 moh'

    wsTemp = gws.getMetData(19710, 'TAM', startDate, endDate, 0, 'list')
    wsSno = gws.getMetData(19710, 'SA', startDate, endDate, 0, 'list')
    wsPrec = gws.getMetData(19710, 'RR', startDate, endDate, 0, 'list')
    wsWind = gws.getMetData(18700, 'FFM', startDate, endDate, 0, 'list')
    wsCC = gws.getMetData(18700, 'NNM', startDate, endDate, 0, 'list')

    utm33_y = 6644410
    utm33_x = 243940

    temp, date = we.strip_metadata(wsTemp, get_date_times=True)
    sno_tot = we.strip_metadata(wsSno)
    prec_snow = dp.delta_snow_from_total_snow(sno_tot)
    prec = we.strip_metadata(wsPrec)
    wind = we.strip_metadata(wsWind)
    cloud_cover = we.strip_metadata(wsCC)
    rel_hum = [const.rel_hum_air] * len(date)
    pressure_atm = [const.pressure_atm] * len(date)

    observed_ice = gro.get_all_season_ice_on_location(location_name, startDate,
                                                      endDate)

    ice_cover, energy_balance = it.calculate_ice_cover_eb(
        utm33_x,
        utm33_y,
        date,
        temp,
        prec,
        prec_snow,
        cloud_cover=cloud_cover,
        wind=wind,
        rel_hum=rel_hum,
        pressure_atm=pressure_atm,
        inn_column=copy.deepcopy(observed_ice[0]))

    # Need datetime objects from now on
    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    plot_filename = '{0}Semsvann EB {1}-{2}.png'.format(
        se.plot_folder, from_date.year, to_date.year)
    # pts.plot_ice_cover(ice_cover, observed_ice, date, temp, sno_tot, plot_filename)
    plot_filename = '{0}Semsvann MET with EB {1}-{2}.png'.format(
        se.plot_folder, from_date.year, to_date.year)
    pts.plot_ice_cover_eb(ice_cover,
                          energy_balance,
                          observed_ice,
                          date,
                          temp,
                          sno_tot,
                          plot_filename,
                          prec=prec,
                          wind=wind,
                          clouds=cloud_cover)
Exemple #2
0
def run_otrovann_eb(startDate, endDate):

    location_name = 'Otrøvatnet v/Nystuen 971 moh'
    wsTemp = gws.getMetData(54710, 'TAM', startDate, endDate, 0, 'list')
    wsSno = gws.getMetData(54710, 'SA', startDate, endDate, 0, 'list')
    wsPrec = gws.getMetData(54710, 'RR', startDate, endDate, 0, 'list')

    utm33_y = 6802070
    utm33_x = 130513

    temp, date = we.strip_metadata(wsTemp, get_date_times=True)
    sno_tot = we.strip_metadata(wsSno)
    prec_snow = dp.delta_snow_from_total_snow(sno_tot)
    prec = we.strip_metadata(wsPrec)
    cloud_cover = dp.clouds_from_precipitation(prec)
    wind = [const.avg_wind_const] * len(date)
    rel_hum = [const.rel_hum_air] * len(date)
    pressure_atm = [const.pressure_atm] * len(date)

    # available_elements = gws.getElementsFromTimeserieTypeStation(54710, 0, 'csv')
    observed_ice = gro.get_all_season_ice_on_location(location_name, startDate,
                                                      endDate)

    ice_cover, energy_balance = it.calculate_ice_cover_eb(
        utm33_x,
        utm33_y,
        date,
        temp,
        prec,
        prec_snow,
        cloud_cover,
        wind,
        rel_hum=rel_hum,
        pressure_atm=pressure_atm,
        inn_column=copy.deepcopy(observed_ice[0]))

    # Need datetime objects from now on
    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    plot_filename = '{0}Ortovann MET EB {1}-{2}.png'.format(
        se.plot_folder, from_date.year, to_date.year)
    # pts.plot_ice_cover(ice_cover, observed_ice, date, temp, sno_tot, plot_filename)
    plot_filename = '{0}Ortovann MET with EB {1}-{2}.png'.format(
        se.plot_folder, from_date.year, to_date.year)
    pts.plot_ice_cover_eb(ice_cover,
                          energy_balance,
                          observed_ice,
                          date,
                          temp,
                          sno_tot,
                          plot_filename,
                          prec=prec,
                          wind=wind,
                          clouds=cloud_cover)
def calculate_and_plot9d_regid(regid,
                               plot_folder=se.plot_folder,
                               observed_ice=None):
    """For an ice thickness on a given regObs RegID, a plot of will be made of the following 9 days development.
    If observed_ice is not given, it is looked up on regObs api by using its RegID. If observation is older
    than 11 days, no plot is made. Weather data is from GTS.

    1.1 If observed ice is none, get some on this regid
    1.2 Else use what is provided, but it has to be a list.
    2.  Get weather data.
    3.  Plot file if it is missing or it is newer than 11 days.

    :param regid:           [Int]           RegID as defined in regObs.
    :param plot_folder:     [string]        Path of folder for plots.
    :param observed_ice:    [ice.IceColumn] Optional. If not given, one will be looked up.
    """

    # if no observed ice is given, get it. Also, observed ice in the plotting routine is a list, so make it so.
    if not observed_ice:
        observed_ice = [gro.get_ice_thickness_on_regid(regid)]
    else:
        observed_ice = [observed_ice]

    x, y = observed_ice[0].metadata['UTMEast'], observed_ice[0].metadata[
        'UTMNorth']

    from_date = observed_ice[0].date.date()
    to_date = from_date + dt.timedelta(days=9)

    # Get weather and snow data
    gridTemp = gts.getgts(x, y, 'tm', from_date, to_date)
    gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
    gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

    temp, date_times = we.strip_metadata(gridTemp, get_date_times=True)
    dates = [d.date() for d in date_times]
    sno = we.strip_metadata(gridSno)
    snotot = we.strip_metadata(gridSnoTot)
    cc = dp.clouds_from_precipitation(sno)

    # Define file name and tests for modelling and plotting
    plot_filename = '{0}{1}.png'.format(plot_folder, regid)

    try:
        icecover = it.calculate_ice_cover_air_temp(observed_ice[0], date_times,
                                                   temp, sno, cc)
        pts.plot_ice_cover_9dogn(icecover, observed_ice[0], dates, temp, sno,
                                 snotot, plot_filename)
    except:
        # raise
        error_msg = sys.exc_info()[0]
        ml.log_and_print(
            "[Error] calculateandplot.py -> calculate_and_plot9d_regid: {}. Could not plot {}."
            .format(error_msg, regid))
def calculate_and_plot9d_regid(regid, plot_folder=se.plot_folder, observed_ice=None):
    """For an ice thickness on a given regObs RegID, a plot of will be made of the following 9 days development.
    If observed_ice is not given, it is looked up on regObs api by using its RegID. If observation is older
    than 11 days, no plot is made. Weather data is from GTS.

    1.1 If observed ice is none, get some on this regid
    1.2 Else use what is provided, but it has to be a list.
    2.  Get weather data.
    3.  Plot file if it is missing or it is newer than 11 days.

    :param regid:           [Int]           RegID as defined in regObs.
    :param plot_folder:     [string]        Path of folder for plots.
    :param observed_ice:    [ice.IceColumn] Optional. If not given, one will be looked up.
    """

    # if no observed ice is given, get it. Also, observed ice in the plotting routine is a list, so make it so.
    if not observed_ice:
        observed_ice = [gro.get_ice_thickness_on_regid(regid)]
    else:
        observed_ice = [observed_ice]

    x, y = observed_ice[0].metadata['UTMEast'], observed_ice[0].metadata['UTMNorth']

    from_date = observed_ice[0].date.date()
    to_date = from_date + dt.timedelta(days=9)

    # Get weather and snow data
    gridTemp = gts.getgts(x, y, 'tm', from_date, to_date)
    gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
    gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

    temp, date_times = we.strip_metadata(gridTemp, get_date_times=True)
    dates = [d.date() for d in date_times]
    sno = we.strip_metadata(gridSno)
    snotot = we.strip_metadata(gridSnoTot)
    cc = dp.clouds_from_precipitation(sno)

    # Define file name and tests for modelling and plotting
    plot_filename = '{0}{1}.png'.format(plot_folder, regid)

    try:
        icecover = it.calculate_ice_cover_air_temp(observed_ice[0], date_times, temp, sno, cc)
        pts.plot_ice_cover_9dogn(icecover, observed_ice[0], dates, temp, sno, snotot, plot_filename)
    except:
        # raise
        error_msg = sys.exc_info()[0]
        ml.log_and_print("[Error] calculateandplot.py -> calculate_and_plot9d_regid: {}. Could not plot {}.".format(error_msg, regid))
def __test_clouds_from_short_wave():

    date_list = [dt.date.today() - dt.timedelta(days=x) for x in range(0, 365)]
    date_list = [
        dt.datetime.combine(d, dt.datetime.min.time()) for d in date_list
    ]
    date_list.reverse()

    # test Nordnesfjellet i Troms
    station_id = 91500
    short_wave_id = 'QSI'
    long_wave_id = 'QLI'
    temperature_id = 'TA'
    timeseries_type = 2
    utm_e = 711075
    utm_n = 7727719

    short_wave = gws.getMetData(station_id, short_wave_id, date_list[0],
                                date_list[-1], timeseries_type)
    long_wave = gws.getMetData(station_id, long_wave_id, date_list[0],
                               date_list[-1], timeseries_type)
    temperature = gws.getMetData(station_id, temperature_id, date_list[0],
                                 date_list[-1], timeseries_type)

    short_wave = we.fix_data_quick(short_wave)
    long_wave = we.fix_data_quick(long_wave)
    temperature_daily = we.fix_data_quick(temperature)

    short_wave_daily = we.make_daily_average(short_wave)
    short_wave_daily = we.multiply_constant(
        short_wave_daily,
        24 * 3600 / 1000)  # Wh/m2 * 3600 s/h * kJ/1000J (energy) over 24hrs
    long_wave_daily = we.make_daily_average(long_wave)
    long_wave_daily = we.multiply_constant(long_wave_daily, 24 * 3600 / 1000)
    temperature_daily = we.make_daily_average(temperature)

    Short_wave_list = we.strip_metadata(short_wave_daily)
    I_clear_sky_list = [
        irradiance_clear_sky(utm_e, utm_n, d) for d in date_list
    ]
    Cloud_cover = clouds_from_short_wave(utm_e, utm_n, Short_wave_list,
                                         date_list)

    import matplotlib.pyplot as plt

    plt.plot(date_list, Short_wave_list)
    plt.plot(date_list, I_clear_sky_list)
    plt.plot(date_list, Cloud_cover)
    plt.ylim(0, 50000)
    plt.show()
def run_semsvann_eb(startDate, endDate):
    # TODO: get coordinates from the ObsLocation in regObs
    location_name = 'Semsvannet v/Lo 145 moh'

    wsTemp = gws.getMetData(19710, 'TAM', startDate, endDate, 0, 'list')
    wsSno = gws.getMetData(19710, 'SA', startDate, endDate, 0, 'list')
    wsPrec = gws.getMetData(19710, 'RR', startDate, endDate, 0, 'list')
    wsWind = gws.getMetData(18700, 'FFM', startDate, endDate, 0, 'list')
    wsCC = gws.getMetData(18700, 'NNM', startDate, endDate, 0, 'list')

    utm33_y = 6644410
    utm33_x = 243940

    temp, date = we.strip_metadata(wsTemp, get_date_times=True)
    sno_tot = we.strip_metadata(wsSno)
    prec_snow = dp.delta_snow_from_total_snow(sno_tot)
    prec = we.strip_metadata(wsPrec)
    wind = we.strip_metadata(wsWind)
    cloud_cover = we.strip_metadata(wsCC)
    rel_hum = [const.rel_hum_air] * len(date)
    pressure_atm = [const.pressure_atm] * len(date)

    observed_ice = gro.get_all_season_ice_on_location(location_name, startDate, endDate)

    ice_cover, energy_balance = it.calculate_ice_cover_eb(
        utm33_x, utm33_y, date,
        temp, prec, prec_snow, cloud_cover=cloud_cover, wind=wind, rel_hum=rel_hum, pressure_atm=pressure_atm,
        inn_column=copy.deepcopy(observed_ice[0]))

    # Need datetime objects from now on
    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    plot_filename = '{0}Semsvann EB {1}-{2}.png'.format(se.plot_folder, from_date.year, to_date.year)
    # pts.plot_ice_cover(ice_cover, observed_ice, date, temp, sno_tot, plot_filename)
    plot_filename = '{0}Semsvann MET with EB {1}-{2}.png'.format(se.plot_folder, from_date.year, to_date.year)
    pts.plot_ice_cover_eb(ice_cover, energy_balance, observed_ice, date, temp, sno_tot, plot_filename, prec=prec, wind=wind, clouds=cloud_cover)
def run_otrovann_eb(startDate, endDate):

    location_name = 'Otrøvatnet v/Nystuen 971 moh'
    wsTemp = gws.getMetData(54710, 'TAM', startDate, endDate, 0, 'list')
    wsSno  = gws.getMetData(54710, 'SA',  startDate, endDate, 0, 'list')
    wsPrec = gws.getMetData(54710, 'RR',  startDate, endDate, 0, 'list')

    utm33_y = 6802070
    utm33_x = 130513

    temp, date = we.strip_metadata(wsTemp, get_date_times=True)
    sno_tot = we.strip_metadata(wsSno)
    prec_snow = dp.delta_snow_from_total_snow(sno_tot)
    prec = we.strip_metadata(wsPrec)
    cloud_cover = dp.clouds_from_precipitation(prec)
    wind = [const.avg_wind_const] * len(date)
    rel_hum = [const.rel_hum_air] * len(date)
    pressure_atm = [const.pressure_atm] * len(date)


    # available_elements = gws.getElementsFromTimeserieTypeStation(54710, 0, 'csv')
    observed_ice = gro.get_all_season_ice_on_location(location_name, startDate, endDate)

    ice_cover, energy_balance = it.calculate_ice_cover_eb(
        utm33_x, utm33_y, date, temp, prec, prec_snow, cloud_cover, wind, rel_hum=rel_hum, pressure_atm=pressure_atm,
        inn_column=copy.deepcopy(observed_ice[0]))

    # Need datetime objects from now on
    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    plot_filename = '{0}Ortovann MET EB {1}-{2}.png'.format(se.plot_folder, from_date.year, to_date.year)
    # pts.plot_ice_cover(ice_cover, observed_ice, date, temp, sno_tot, plot_filename)
    plot_filename = '{0}Ortovann MET with EB {1}-{2}.png'.format(se.plot_folder, from_date.year, to_date.year)
    pts.plot_ice_cover_eb(ice_cover, energy_balance, observed_ice, date, temp, sno_tot, plot_filename,
                       prec=prec, wind=wind, clouds=cloud_cover)
def __test_clouds_from_short_wave():

    date_list = [dt.date.today() - dt.timedelta(days=x) for x in range(0, 365)]
    date_list = [dt.datetime.combine(d, dt.datetime.min.time()) for d in date_list]
    date_list.reverse()

    # test Nordnesfjellet i Troms
    station_id = 91500
    short_wave_id = 'QSI'
    long_wave_id = 'QLI'
    temperature_id = 'TA'
    timeseries_type = 2
    utm_e = 711075
    utm_n = 7727719

    short_wave = gws.getMetData(station_id, short_wave_id, date_list[0], date_list[-1], timeseries_type)
    long_wave = gws.getMetData(station_id, long_wave_id, date_list[0], date_list[-1], timeseries_type)
    temperature = gws.getMetData(station_id, temperature_id, date_list[0], date_list[-1], timeseries_type)

    short_wave = we.fix_data_quick(short_wave)
    long_wave = we.fix_data_quick(long_wave)
    temperature_daily = we.fix_data_quick(temperature)

    short_wave_daily = we.make_daily_average(short_wave)
    short_wave_daily = we.multiply_constant(short_wave_daily, 24 * 3600 / 1000)  # Wh/m2 * 3600 s/h * kJ/1000J (energy) over 24hrs
    long_wave_daily = we.make_daily_average(long_wave)
    long_wave_daily = we.multiply_constant(long_wave_daily, 24 * 3600 / 1000)
    temperature_daily = we.make_daily_average(temperature)

    Short_wave_list = we.strip_metadata(short_wave_daily)
    I_clear_sky_list = [irradiance_clear_sky(utm_e, utm_n, d) for d in date_list]
    Cloud_cover = clouds_from_short_wave(utm_e, utm_n, Short_wave_list, date_list)

    import matplotlib.pyplot as plt

    plt.plot(date_list, Short_wave_list)
    plt.plot(date_list, I_clear_sky_list)
    plt.plot(date_list, Cloud_cover)
    plt.ylim(0, 50000)
    plt.show()
def calculate_and_plot_location(location_name,
                                from_date,
                                to_date,
                                sub_plot_folder='',
                                make_plots=True,
                                return_values=False):
    """ due to get_all_season_ice returns data grouped be location_id
    For a given LocationName in regObs calculate the ice cover between two dates. Optional, make plots
    and/or return the calculations and observations for this location. Different sources for weather data
    may be given, chartserver grid is default.

    :param location_name:
    :param from_date:               [String] 'yyyy-mm-dd'
    :param to_date:                 [String] 'yyyy-mm-dd'
    :param sub_plot_folder:
    :param make_plots:
    :param return_values:           [bool]  If true the calculated and observed data is returned
    """

    loc = slp.get_for_location(location_name)
    year = '{0}-{1}'.format(from_date[0:4], to_date[2:4])
    lake_file_name = '{0} {1}'.format(
        fe.make_standard_file_name(loc.file_name), year)
    observed_ice = gro.get_observations_on_location_id(loc.regobs_location_id,
                                                       year)

    # Change dates to datetime. Some of the getdata modules require datetime
    from_date = dt.datetime.strptime(from_date, '%Y-%m-%d')
    to_date = dt.datetime.strptime(to_date, '%Y-%m-%d')

    # special rule for this season.
    if year == '2018-19':
        from_date = dt.datetime(2017, 10, 15)

    # if to_date forward in time, make sure it doesnt go to far..
    if to_date > dt.datetime.now():
        to_date = dt.datetime.now() + dt.timedelta(days=7)

    if loc.weather_data_source == 'eKlima':
        wsTemp = gws.getMetData(loc.eklima_TAM, 'TAM', from_date, to_date, 0,
                                'list')
        temp, date = we.strip_metadata(wsTemp, True)

        wsSno = gws.getMetData(loc.eklima_SA, 'SA', from_date, to_date, 0,
                               'list')
        snotot = we.strip_metadata(wsSno)
        sno = dp.delta_snow_from_total_snow(snotot)

        # Clouds. If not from met.no it is parametrised from precipitation.
        if loc.eklima_NNM:
            wsCC = gws.getMetData(loc.eklima_NNM, 'NNM', from_date, to_date, 0,
                                  'list')
            cc = we.strip_metadata(wsCC)
        else:
            cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}{1} eklima.png'.format(
            se.plot_folder + sub_plot_folder, lake_file_name)

    elif loc.weather_data_source == 'grid':
        x, y = loc.utm_east, loc.utm_north

        gridTemp = gts.getgts(x, y, 'tm', from_date, to_date)
        gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
        gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

        temp, date = we.strip_metadata(gridTemp, get_date_times=True)
        sno = we.strip_metadata(gridSno, False)
        snotot = we.strip_metadata(gridSnoTot, False)

        if loc.eklima_NNM:
            wsCC = gws.getMetData(loc.eklima_NNM, 'NNM', from_date, to_date, 0,
                                  'list')
            cc = we.strip_metadata(wsCC)
        else:
            cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}{1} grid.png'.format(
            se.plot_folder + sub_plot_folder, lake_file_name)

    elif loc.weather_data_source == 'nve':
        x, y = loc.utm_east, loc.utm_north

        # Temp from NVE station or grid if not.
        if loc.nve_temp:
            temp_obj = gcsd.getStationdata(loc.nve_temp,
                                           '17.1',
                                           from_date,
                                           to_date,
                                           timeseries_type=0)
        else:
            temp_obj = gcsd.getGriddata(x, y, 'tm', from_date, to_date)
        temp, date = we.strip_metadata(temp_obj, get_date_times=True)

        # Snow from NVE station or grid if not.
        if loc.nve_snow:
            snotot_obj = gcsd.getStationdata(loc.nve_snow,
                                             '2002.1',
                                             from_date,
                                             to_date,
                                             timeseries_type=0)
            snotot = we.strip_metadata(snotot_obj)
            sno = dp.delta_snow_from_total_snow(snotot_obj)
        else:
            snotot_obj = gcsd.getGriddata(x,
                                          y,
                                          'sd',
                                          from_date,
                                          to_date,
                                          timeseries_type=0)
            sno_obj = gcsd.getGriddata(x,
                                       y,
                                       'fsw',
                                       from_date,
                                       to_date,
                                       timeseries_type=0)
            snotot = we.strip_metadata(snotot_obj)
            sno = we.strip_metadata(sno_obj)

        # Clouds. If not from met.no it is parametrised from precipitation.
        if loc.eklima_NNM:
            cc_obj = gws.getMetData(18700, 'NNM', from_date, to_date, 0,
                                    'list')
        else:
            cc_obj = dp.clouds_from_precipitation(sno)
        cc = we.strip_metadata(cc_obj)

        plot_filename = '{0}{1} nve.png'.format(
            se.plot_folder + sub_plot_folder, lake_file_name)

    elif loc.weather_data_source == 'file':
        date, temp, sno, snotot = gfd.read_weather(from_date, to_date,
                                                   loc.input_file)
        cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}{1} file.png'.format(
            se.plot_folder + sub_plot_folder, lake_file_name)

    else:
        ml.log_and_print(
            "[Error] runicethickness -> calculate_and_plot_location: Invalid scource for weather data."
        )
        return

    try:
        if len(observed_ice) == 0:
            calculated_ice = it.calculate_ice_cover_air_temp(
                ice.IceColumn(date[0], []), date, temp, sno, cc)
        else:
            calculated_ice = it.calculate_ice_cover_air_temp(
                copy.deepcopy(observed_ice[0]), date, temp, sno, cc)

        if make_plots:
            pts.plot_ice_cover(calculated_ice, observed_ice, date, temp, sno,
                               snotot, plot_filename)

    except:
        error_msg = sys.exc_info()[0]
        ml.log_and_print(
            "[Error] calculateandplot.py -> calculate_and_plot_location: {}. Could not plot {}."
            .format(error_msg, location_name))
        calculated_ice = None

    if return_values:
        return calculated_ice, observed_ice
def _plot_season(location_id,
                 from_date,
                 to_date,
                 observed_ice,
                 make_plots=True,
                 plot_folder=se.plot_folder):
    """Given a location id, a time period and some observations on this location id and this method
    calculates and optionally plots the ice evolution that season. Weather data from GTS.

    It is a sub method of plot_season_for_location_id and plot_season_for_all_regobs_locations.

    :param location_id:
    :param from_date:
    :param to_date:
    :param observed_ice:
    :param make_plots:
    :param plot_folder:     [string]        Path of folder for plots.

    :return calculated_ice, observed_ice:   [list of Ice.IceColumn] observed_ice is returned as given inn.

    TODO: should accept observerd_ice=None and then query for the observations. If still missing, set icecover on to start date.
    """

    year = '{0}-{1}'.format(from_date[0:4], to_date[2:4])

    # Change dates to datetime. Some of the get data modules require datetime
    from_date = dt.datetime.strptime(from_date, '%Y-%m-%d')
    to_date = dt.datetime.strptime(to_date, '%Y-%m-%d')

    # special rule for this season.
    if year == '2018-19':
        from_date = dt.datetime(2018, 9, 1)

    # if to_date forward in time, make sure it doesnt go to far..
    if to_date > dt.datetime.now():
        to_date = dt.datetime.now() + dt.timedelta(days=7)

    x, y = observed_ice[0].metadata['UTMEast'], observed_ice[0].metadata[
        'UTMNorth']

    # get weather data
    gridTemp = gts.getgts(x, y, 'tm', from_date, to_date)
    gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
    gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

    # adjust grid temperature (at grid elevation) to lake elevation.
    lake_altitude = gm.get_masl_from_utm33(x, y)
    gridTempNewElevation = we.adjust_temperature_to_new_altitude(
        gridTemp, lake_altitude)

    # strip metadata
    temp, date = we.strip_metadata(gridTempNewElevation, get_date_times=True)
    sno = we.strip_metadata(gridSno, False)
    snotot = we.strip_metadata(gridSnoTot, False)
    cc = dp.clouds_from_precipitation(sno)

    plot_filename = '{0}_{1}.png'.format(location_id, year)
    plot_path_and_filename = '{0}{1}'.format(plot_folder, plot_filename)

    try:
        if len(observed_ice) == 0:
            calculated_ice = it.calculate_ice_cover_air_temp(
                ice.IceColumn(date[0], []), date, temp, sno, cc)
        else:
            calculated_ice = it.calculate_ice_cover_air_temp(
                copy.deepcopy(observed_ice[0]), date, temp, sno, cc)

        if make_plots:
            pts.plot_ice_cover(calculated_ice, observed_ice, date, temp, sno,
                               snotot, plot_path_and_filename)

    except:
        # raise
        error_msg = sys.exc_info()[0]
        ml.log_and_print(
            "[Error] calculateandplot.py -> _plot_season: {}. Could not plot {}."
            .format(error_msg, location_id))
        calculated_ice = None

    return calculated_ice, observed_ice, plot_filename
def calculate_and_plot_location(location_name, from_date, to_date, sub_plot_folder='', make_plots=True, return_values=False):
    """ due to get_all_season_ice returns data grouped be location_id
    For a given LocationName in regObs calculate the ice cover between two dates. Optional, make plots
    and/or return the calculations and observations for this location. Different sources for weather data
    may be given, chartserver grid is default.

    :param location_name:
    :param from_date:               [String] 'yyyy-mm-dd'
    :param to_date:                 [String] 'yyyy-mm-dd'
    :param sub_plot_folder:
    :param make_plots:
    :param return_values:           [bool]  If true the calculated and observed data is returned
    """

    loc = slp.get_for_location(location_name)
    year = '{0}-{1}'.format(from_date[0:4], to_date[2:4])
    lake_file_name = '{0} {1}'.format(fe.make_standard_file_name(loc.file_name), year)
    observed_ice = gro.get_observations_on_location_id(loc.regobs_location_id, year)

    # Change dates to datetime. Some of the getdata modules require datetime
    from_date = dt.datetime.strptime(from_date, '%Y-%m-%d')
    to_date = dt.datetime.strptime(to_date, '%Y-%m-%d')

    # special rule for this season.
    if year == '2018-19':
        from_date = dt.datetime(2017, 10, 15)

    # if to_date forward in time, make sure it doesnt go to far..
    if to_date > dt.datetime.now():
        to_date = dt.datetime.now() + dt.timedelta(days=7)

    if loc.weather_data_source == 'eKlima':
        wsTemp = gws.getMetData(loc.eklima_TAM, 'TAM', from_date, to_date, 0, 'list')
        temp, date = we.strip_metadata(wsTemp, True)

        wsSno = gws.getMetData(loc.eklima_SA, 'SA', from_date, to_date, 0, 'list')
        snotot = we.strip_metadata(wsSno)
        sno = dp.delta_snow_from_total_snow(snotot)

        # Clouds. If not from met.no it is parametrised from precipitation.
        if loc.eklima_NNM:
            wsCC = gws.getMetData(loc.eklima_NNM, 'NNM', from_date, to_date, 0, 'list')
            cc = we.strip_metadata(wsCC)
        else:
            cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}{1} eklima.png'.format(se.plot_folder + sub_plot_folder, lake_file_name)

    elif loc.weather_data_source == 'grid':
        x, y = loc.utm_east, loc.utm_north

        gridTemp = gts.getgts(x, y, 'tm', from_date, to_date)
        gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
        gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

        temp, date = we.strip_metadata(gridTemp, get_date_times=True)
        sno = we.strip_metadata(gridSno, False)
        snotot = we.strip_metadata(gridSnoTot, False)

        if loc.eklima_NNM:
            wsCC = gws.getMetData(loc.eklima_NNM, 'NNM', from_date, to_date, 0, 'list')
            cc = we.strip_metadata(wsCC)
        else:
            cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}{1} grid.png'.format(se.plot_folder + sub_plot_folder, lake_file_name)

    elif loc.weather_data_source == 'nve':
        x, y = loc.utm_east, loc.utm_north

        # Temp from NVE station or grid if not.
        if loc.nve_temp:
            temp_obj = gcsd.getStationdata(loc.nve_temp, '17.1', from_date, to_date, timeseries_type=0)
        else:
            temp_obj = gcsd.getGriddata(x, y, 'tm', from_date, to_date)
        temp, date = we.strip_metadata(temp_obj, get_date_times=True)

        # Snow from NVE station or grid if not.
        if loc.nve_snow:
            snotot_obj = gcsd.getStationdata(loc.nve_snow, '2002.1', from_date, to_date, timeseries_type=0)
            snotot = we.strip_metadata(snotot_obj)
            sno = dp.delta_snow_from_total_snow(snotot_obj)
        else:
            snotot_obj = gcsd.getGriddata(x, y, 'sd', from_date, to_date, timeseries_type=0)
            sno_obj = gcsd.getGriddata(x, y, 'fsw', from_date, to_date, timeseries_type=0)
            snotot = we.strip_metadata(snotot_obj)
            sno = we.strip_metadata(sno_obj)

        # Clouds. If not from met.no it is parametrised from precipitation.
        if loc.eklima_NNM:
            cc_obj = gws.getMetData(18700, 'NNM', from_date, to_date, 0, 'list')
        else:
            cc_obj = dp.clouds_from_precipitation(sno)
        cc = we.strip_metadata(cc_obj)

        plot_filename = '{0}{1} nve.png'.format(se.plot_folder + sub_plot_folder, lake_file_name)

    elif loc.weather_data_source == 'file':
        date, temp, sno, snotot = gfd.read_weather(from_date, to_date, loc.input_file)
        cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}{1} file.png'.format(se.plot_folder + sub_plot_folder, lake_file_name)

    else:
        ml.log_and_print("[Error] runicethickness -> calculate_and_plot_location: Invalid scource for weather data.")
        return

    try:
        if len(observed_ice) == 0:
            calculated_ice = it.calculate_ice_cover_air_temp(ice.IceColumn(date[0], []), date, temp, sno, cc)
        else:
            calculated_ice = it.calculate_ice_cover_air_temp(copy.deepcopy(observed_ice[0]), date, temp, sno, cc)

        if make_plots:
            pts.plot_ice_cover(calculated_ice, observed_ice, date, temp, sno, snotot, plot_filename)

    except:
        error_msg = sys.exc_info()[0]
        ml.log_and_print("[Error] calculateandplot.py -> calculate_and_plot_location: {}. Could not plot {}.".format(error_msg, location_name))
        calculated_ice = None

    if return_values:
        return calculated_ice, observed_ice
def _plot_season(location_id, from_date, to_date, observed_ice, make_plots=True, plot_folder=se.plot_folder):
    """Given a location id, a time period and some observations on this location id and this method
    calculates and optionally plots the ice evolution that season. Weather data from GTS.

    It is a sub method of plot_season_for_location_id and plot_season_for_all_regobs_locations.

    :param location_id:
    :param from_date:
    :param to_date:
    :param observed_ice:
    :param make_plots:
    :param plot_folder:     [string]        Path of folder for plots.

    :return calculated_ice, observed_ice:   [list of Ice.IceColumn] observed_ice is returned as given inn.

    TODO: should accept observerd_ice=None and then query for the observations. If still missing, set icecover on to start date.
    """

    year = '{0}-{1}'.format(from_date[0:4], to_date[2:4])

    # Change dates to datetime. Some of the get data modules require datetime
    from_date = dt.datetime.strptime(from_date, '%Y-%m-%d')
    to_date = dt.datetime.strptime(to_date, '%Y-%m-%d')

    # special rule for this season.
    if year == '2018-19':
        from_date = dt.datetime(2018, 9, 1)

    # if to_date forward in time, make sure it doesnt go to far..
    if to_date > dt.datetime.now():
        to_date = dt.datetime.now() + dt.timedelta(days=7)

    x, y = observed_ice[0].metadata['UTMEast'], observed_ice[0].metadata['UTMNorth']

    # get weather data
    gridTemp = gts.getgts(x, y, 'tm', from_date, to_date)
    gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
    gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

    # adjust grid temperature (at grid elevation) to lake elevation.
    lake_altitude = gm.get_masl_from_utm33(x, y)
    gridTempNewElevation = we.adjust_temperature_to_new_altitude(gridTemp, lake_altitude)

    # strip metadata
    temp, date = we.strip_metadata(gridTempNewElevation, get_date_times=True)
    sno = we.strip_metadata(gridSno, False)
    snotot = we.strip_metadata(gridSnoTot, False)
    cc = dp.clouds_from_precipitation(sno)

    plot_filename = '{0}_{1}.png'.format(location_id, year)
    plot_path_and_filename = '{0}{1}'.format(plot_folder, plot_filename)

    try:
        if len(observed_ice) == 0:
            calculated_ice = it.calculate_ice_cover_air_temp(ice.IceColumn(date[0], []), date, temp, sno, cc)
        else:
            calculated_ice = it.calculate_ice_cover_air_temp(copy.deepcopy(observed_ice[0]), date, temp, sno, cc)

        if make_plots:
            pts.plot_ice_cover(calculated_ice, observed_ice, date, temp, sno, snotot, plot_path_and_filename)

    except:
        # raise
        error_msg = sys.exc_info()[0]
        ml.log_and_print("[Error] calculateandplot.py -> _plot_season: {}. Could not plot {}.".format(error_msg, location_id))
        calculated_ice = None

    return calculated_ice, observed_ice, plot_filename
Exemple #13
0
def run_mosselva(from_date,
                 to_date,
                 make_plots=True,
                 plot_folder=se.plot_folder,
                 forcing='grid'):
    """

    :param from_date:
    :param to_date:
    :param make_plots:
    :param plot_folder:
    :return:
    """

    location_name = 'Mosselva'
    y = 6595744
    x = 255853
    altitude = 25

    met_stnr = 17150  # Rygge målestasjon (met.no)

    first_ice = ice.IceColumn(dt.datetime(int(from_date[0:4]), 12, 31), [])
    first_ice.add_metadata('LocationName', location_name)  # used when plotting
    observed_ice = [first_ice]

    year = '{0}-{1}'.format(from_date[0:4], to_date[2:4])

    # Change dates to datetime. Some of the getdata modules require datetime
    from_date = dt.datetime.strptime(from_date, '%Y-%m-%d')
    to_date = dt.datetime.strptime(to_date, '%Y-%m-%d')

    # if to_date forward in time, make sure it doesnt go to far..
    if to_date > dt.datetime.now():
        to_date = dt.datetime.now() + dt.timedelta(days=7)

    if forcing == 'eKlima':

        wsTemp = gws.getMetData(met_stnr, 'TAM', from_date, to_date, 0, 'list')
        gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
        gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

        temp, date = we.strip_metadata(wsTemp, get_date_times=True)
        sno = we.strip_metadata(gridSno)
        sno_tot = we.strip_metadata(gridSnoTot)
        cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}_{1}_eklima.png'.format(location_name, year)

    elif forcing == 'grid':

        gridTemp = gts.getgts(x, y, 'tm', from_date, to_date)
        gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
        gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

        gridTempNewElevation = we.adjust_temperature_to_new_altitude(
            gridTemp, altitude)

        temp, date = we.strip_metadata(gridTempNewElevation,
                                       get_date_times=True)
        sno = we.strip_metadata(gridSno)
        sno_tot = we.strip_metadata(gridSnoTot)
        cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}_{1}_grid.png'.format(location_name, year)

    else:
        temp, date = None, None
        sno = None
        sno_tot = None
        cc = None

        plot_filename = '{0}_{1}_no_forcing.png'.format(location_name, year)

    calculated_ice = it.calculate_ice_cover_air_temp(copy.deepcopy(first_ice),
                                                     date,
                                                     temp,
                                                     sno,
                                                     cloud_cover=cc)

    if make_plots:
        plot_path_and_filename = '{0}{1}'.format(plot_folder, plot_filename)
        pts.plot_ice_cover(calculated_ice, observed_ice, date, temp, sno,
                           sno_tot, plot_path_and_filename)
Exemple #14
0
def run_semsvann(from_date,
                 to_date,
                 make_plots=True,
                 plot_folder=se.plot_folder,
                 forcing='grid'):
    """

    :param from_date:
    :param to_date:
    :param make_plots:
    :param plot_folder:
    :return:
    """

    location_name = 'Semsvann'
    regobs_location_id = 2227

    x = 243655
    y = 6644286
    altitude = 145

    met_stnr = 19710  # Asker (Sem)
    met_stnr_NNM = 18700  # Blindern

    year = '{0}-{1}'.format(from_date[0:4], to_date[2:4])

    observed_ice = gro.get_observations_on_location_id(regobs_location_id,
                                                       year)
    first_ice = observed_ice[0]

    # Change dates to datetime. Some of the getdata modules require datetime
    from_date = dt.datetime.strptime(from_date, '%Y-%m-%d')
    to_date = dt.datetime.strptime(to_date, '%Y-%m-%d')

    # if to_date forward in time, make sure it doesnt go to far..
    if to_date > dt.datetime.now():
        to_date = dt.datetime.now() + dt.timedelta(days=7)

    if forcing == 'eKlima':

        wsTemp = gws.getMetData(met_stnr, 'TAM', from_date, to_date, 0, 'list')
        wsSnoTot = gws.getMetData(met_stnr, 'SA', from_date, to_date, 0,
                                  'list')
        wsCC = gws.getMetData(met_stnr_NNM, 'NNM', from_date, to_date, 0,
                              'list')

        temp, date = we.strip_metadata(wsTemp, get_date_times=True)
        sno_tot = we.strip_metadata(wsSnoTot)
        sno = dp.delta_snow_from_total_snow(sno_tot)
        cc = we.strip_metadata(wsCC)

        plot_filename = '{0}_{1}_eklima.png'.format(location_name, year)

    elif forcing == 'grid':

        gridTemp = gts.getgts(x, y, 'tm', from_date, to_date)
        gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
        gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

        # Grid altitude and lake at same elevations.
        gridTempNewElevation = we.adjust_temperature_to_new_altitude(
            gridTemp, altitude)

        temp, date = we.strip_metadata(gridTempNewElevation,
                                       get_date_times=True)
        sno = we.strip_metadata(gridSno)
        sno_tot = we.strip_metadata(gridSnoTot)
        cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}_{1}_grid.png'.format(location_name, year)

    else:
        temp, date = None, None
        sno = None
        sno_tot = None
        cc = None

        plot_filename = '{0}_{1}_no_forcing.png'.format(location_name, year)

    calculated_ice = it.calculate_ice_cover_air_temp(copy.deepcopy(first_ice),
                                                     date,
                                                     temp,
                                                     sno,
                                                     cloud_cover=cc)

    if make_plots:
        plot_path_and_filename = '{0}{1}'.format(plot_folder, plot_filename)
        pts.plot_ice_cover(calculated_ice, observed_ice, date, temp, sno,
                           sno_tot, plot_path_and_filename)
def run_semsvann(from_date, to_date, make_plots=True, plot_folder=se.plot_folder, forcing='grid'):
    """

    :param from_date:
    :param to_date:
    :param make_plots:
    :param plot_folder:
    :return:
    """

    location_name = 'Semsvann'
    regobs_location_id = 2227

    x = 243655
    y = 6644286
    altitude = 145

    met_stnr = 19710        # Asker (Sem)
    met_stnr_NNM = 18700    # Blindern

    year = '{0}-{1}'.format(from_date[0:4], to_date[2:4])

    observed_ice = gro.get_observations_on_location_id(regobs_location_id, year)
    first_ice = observed_ice[0]

    # Change dates to datetime. Some of the getdata modules require datetime
    from_date = dt.datetime.strptime(from_date, '%Y-%m-%d')
    to_date = dt.datetime.strptime(to_date, '%Y-%m-%d')

    # if to_date forward in time, make sure it doesnt go to far..
    if to_date > dt.datetime.now():
        to_date = dt.datetime.now() + dt.timedelta(days=7)

    if forcing == 'eKlima':

        wsTemp = gws.getMetData(met_stnr, 'TAM', from_date, to_date, 0, 'list')
        wsSnoTot = gws.getMetData(met_stnr, 'SA', from_date, to_date, 0, 'list')
        wsCC = gws.getMetData(met_stnr_NNM, 'NNM', from_date, to_date, 0, 'list')

        temp, date = we.strip_metadata(wsTemp, get_date_times=True)
        sno_tot = we.strip_metadata(wsSnoTot)
        sno = dp.delta_snow_from_total_snow(sno_tot)
        cc = we.strip_metadata(wsCC)

        plot_filename = '{0}_{1}_eklima.png'.format(location_name, year)

    elif forcing == 'grid':

        gridTemp = gts.getgts(x, y, 'tm', from_date, to_date)
        gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
        gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

        # Grid altitude and lake at same elevations.
        gridTempNewElevation = we.adjust_temperature_to_new_altitude(gridTemp, altitude)

        temp, date = we.strip_metadata(gridTempNewElevation, get_date_times=True)
        sno = we.strip_metadata(gridSno)
        sno_tot = we.strip_metadata(gridSnoTot)
        cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}_{1}_grid.png'.format(location_name, year)

    else:
        temp, date = None, None
        sno = None
        sno_tot = None
        cc = None

        plot_filename = '{0}_{1}_no_forcing.png'.format(location_name, year)

    calculated_ice = it.calculate_ice_cover_air_temp(copy.deepcopy(first_ice), date, temp, sno, cloud_cover=cc)

    if make_plots:
        plot_path_and_filename = '{0}{1}'.format(plot_folder, plot_filename)
        pts.plot_ice_cover(calculated_ice, observed_ice, date, temp, sno, sno_tot, plot_path_and_filename)
def calculate_reference_lakes(calculation_date=dt.datetime.now(), make_plots=True, get_new_obs=True):
    """Plot ice thickness for this season for all reference lakes. Create a json-file with information about
    changes in the ice thickness last week and expected changes next week.

    # Always plot
    :param make_plots:       [bool] If true plots of reference lakes are made.
    :param calculation_date: [datetime] Defines the day the plots are made for. Datetime since the ice model uses this.
    :param get_new_obs:      [bool] If true, new observations ar requested from Regobs and added to local storage.

    """

    # Filename including path for input json file with reference lake information
    input_json_file = se.reference_lakes_input_json_file
    # Folder for output json file with ice changes
    output_json_folder = se.reference_lakes_output_json_folder
    # Folder to place the plots
    plot_folder = se.reference_lakes_plot_folder
    # Filename for adjusted position for locations outside the GTS-grid
    adjusted_location_json_file = se.reference_lakes_adjusted_location

    # Calculate season text. Set 1. september as start of season
    fyear = calculation_date.year
    month = calculation_date.month
    if month < 9:
        fyear = fyear - 1
    season = str(fyear) + '-' + str(fyear+1)[2:]

    # Calculate start of plot, use 30. september
    start_season_date = dt.datetime(fyear, 9, 30)
    # Use 10 days before present date for the final plot
    start_plot_date = calculation_date - dt.timedelta(days=11)

    # Get new observations from regobs. This updates the pickle in local storage so we may only get data once.
    gro.get_all_season_ice(season, get_new=get_new_obs)

    # Create json filename, Prognoserte_endringer_date. Save one with todays date, and one copy as overwrite 'latest'
    output_folder = output_json_folder
    if output_folder[-1:] != '/':
        output_folder += '/'
    output_filename = output_folder + f'Endringer_istykkelse-{calculation_date.year}-{calculation_date.month:02}-{calculation_date.day:02}.json'
    output_latest_filename_core = 'Endringer_istykkelse-latest.json'
    output_latest_filename = output_folder + output_latest_filename_core

    # Make sure plot_folder ends with '/'
    if plot_folder[-1:] != '/':
        plot_folder += '/'

    with open(adjusted_location_json_file, encoding='utf-8-sig') as adjusted_location_json:
        adjusted_location_data = json.load(adjusted_location_json)

    # Read json-file
    reference_lakes_jsonfile = input_json_file
    with open(reference_lakes_jsonfile, encoding='utf-8-sig') as reference_lakes_json:
        reference_lakes_data = json.load(reference_lakes_json)

    # Store plot file names for later ftp
    lastregion = ''
    ftp_files = []
    for fcr in reference_lakes_data['forecastRegions']:
        # Message
        if fcr['name'] != lastregion:
            lastregion = fcr['name']
            print("--- ", lastregion, " ---")

        for ls in fcr['lakeSize']:
            for lh in ls['lakeHeight']:
                ref_lake = lh['reference_lake']
                if ref_lake['locationId'] != '':
                    # Create filename 'locationid_season_forecastregionindex_lakesizeindex_lakeheightindex.png'
                    plot_filename = fcr['index'] + '_' + ls['index'] + '_' + lh['index'] + '_' + season + '_' \
                                    + ref_lake['locationId'] + '.png'
                    ftp_files.append(plot_filename)

                    # Calculate ice growth for this location
                    # Use grid data
                    location_name = ref_lake['name']
                    regobs_location_id_text = ref_lake['locationId']
                    regobs_location_id = int(ref_lake['locationId'])

                    # Should we adjust the position for this location to match the GTS weather grid?
                    found_in_list = False
                    for station in adjusted_location_data:
                        if station['locationID'] == regobs_location_id_text:
                            found_in_list = True
                            adjusted33x = int(station['valid33x'])
                            adjusted33y = int(station['valid33y'])
                            break

                    if found_in_list:
                        # Use adjusted values
                        x = adjusted33x
                        y = adjusted33y
                        print('x,y', x, y)
                    else:
                        x = int(ref_lake['utm33x'])
                        y = int(ref_lake['utm33y'])

                    altitude = int(ref_lake['height'])

                    # Message
                    print(str(regobs_location_id) + ' - ' + location_name)

                    # Get start and end date for simulation
                    first_possible_date = dt.datetime(fyear,9,1)
                    if ref_lake['FreezeUpThisYear'] != '':
                        freezeup = ref_lake['FreezeUpThisYear']
                        freezup_dt = dt.datetime(int(freezeup[6:]), int(freezeup[3:5]), int(freezeup[0:2]))
                        if freezup_dt < first_possible_date:
                            # Ignore this date, probably a left over from last season
                            freezeup = ref_lake['FreezeUpNormal']
                    else:
                        freezeup = ref_lake['FreezeUpNormal']

                    # Adjust to correct start year and get from_date in datetime
                    if int(freezeup[3:5]) < 9:
                        # Passed new year
                        from_date = dt.datetime(fyear + 1, int(freezeup[3:5]), int(freezeup[0:2]))
                    else:
                        from_date = dt.datetime(fyear, int(freezeup[3:5]), int(freezeup[0:2]))

                    to_date = calculation_date + dt.timedelta(days=9)

                    # Get regobs-observations, if any
                    observed_ice = gro.get_observations_on_location_id(regobs_location_id, season)
                    if len(observed_ice) > 0:
                        first_ice = observed_ice[0]
                        first_ice.ignore_slush_event_variable = False
                        first_ice.slush_event = False
                    else:
                        first_ice = ice.IceColumn(from_date, [])
                        first_ice.add_metadata('LocationName', location_name)  # used when plotting
                        observed_ice.append(first_ice)

                    # Set from_date equal to when the calculations should start
                    from_date = start_season_date

                    gridTemp = gts.getgts(x, y, 'tm', from_date, to_date)
                    gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
                    gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

                    # Grid altitude and lake at same elevations.
                    gridTempNewElevation = we.adjust_temperature_to_new_altitude(gridTemp, altitude)

                    temp, date = we.strip_metadata(gridTempNewElevation, get_date_times=True)
                    sno = we.strip_metadata(gridSno)
                    sno_tot = we.strip_metadata(gridSnoTot)
                    cc = dp.clouds_from_precipitation(sno)

                    calculated_ice = it.calculate_ice_cover_air_temp(copy.deepcopy(first_ice), date, temp, sno, cloud_cover=cc)

                    # Create output json
                    wanted_output_dates = []
                    # 7 days ago at 00:00
                    wdate = calculation_date - dt.timedelta(days=7)
                    wdate = dt.datetime(wdate.year, wdate.month, wdate.day)
                    wanted_output_dates.append(wdate)
                    # Today, at 00:00
                    wdate = dt.datetime(calculation_date.year, calculation_date.month, calculation_date.day)
                    wanted_output_dates.append(wdate)
                    # + 7 days, at 00:00
                    wdate = calculation_date + dt.timedelta(days=7)
                    wdate = dt.datetime(wdate.year, wdate.month, wdate.day)
                    wanted_output_dates.append(wdate)
                    # Get Ice columns at these dates
                    wanted_snow_thickness = []
                    wanted_slush_thickness = []
                    wanted_ice_thickness = []
                    wanted_thickest_icelayer = []
                    for wanted_date in wanted_output_dates:
                        # Find calculated ice at this date, if any
                        # The method allows to merge slush_ice, black_ice and unknown into one layer
                        # Unknown and slush_ice counts as half the ice thickness to simulate black ice strength
                        found = False
                        tot_snow = 0
                        tot_slush = 0
                        tot_ice = 0
                        thickest_ice_layer = 0
                        for ci in calculated_ice:
                            if ci.date == wanted_date:
                                # Find snow and slush
                                for il in ci.column:
                                    if il.type == 'new snow' or il.type == 'snow' or il.type == 'drained_snow':
                                        tot_snow += il.height
                                    elif il.type == 'slush':
                                        tot_slush += il.height
                                # Total thickness of pure ice layers avoid type = 5 (water intermediate)
                                merged_ice_thickness = 0
                                for il in ci.column:
                                    if il.type == 'slush_ice' or il.type == 'black_ice' or il.type == 'unknown':
                                        tot_ice += il.height
                                        if il.type == 'black_ice':
                                            merged_ice_thickness = il.height + merged_ice_thickness
                                        else:
                                            # Weaker ice than black ice. Use half the height
                                            merged_ice_thickness = il.height * 0.5 + merged_ice_thickness
                                        if merged_ice_thickness > thickest_ice_layer:
                                            thickest_ice_layer = merged_ice_thickness
                                    else:
                                        # Not an ice layer
                                        merged_ice_thickness = 0

                                found = True
                                break
                            elif ci.date > wanted_date:
                                # No ice at this date
                                break

                        # Add the ice information to the array
                        wanted_snow_thickness.append(tot_snow)
                        wanted_slush_thickness.append(tot_slush)
                        wanted_ice_thickness.append(tot_ice)
                        wanted_thickest_icelayer.append(thickest_ice_layer)

                    # Add todays date
                    today_date = f'{calculation_date.day:02}.{calculation_date.month:02}.{calculation_date.year}'
                    ref_lake['Updated'] = today_date
                    # Save present status
                    ref_lake['SnowThickness'] = f'{wanted_snow_thickness[1]:.2f}'
                    ref_lake['SlushThickness'] = f'{wanted_slush_thickness[1]:.2f}'
                    ref_lake['TotalPureIceThickness'] = f'{wanted_ice_thickness[1]:.2f}'
                    ref_lake['ThickestIceLayer'] = f'{wanted_thickest_icelayer[1]:.2f}'
                    # Save status 7 days ago
                    ref_lake['SnowThicknessLast7days'] = f'{wanted_snow_thickness[0]:.2f}'
                    ref_lake['SlushThicknessLast7days'] = f'{wanted_slush_thickness[0]:.2f}'
                    ref_lake['TotalPureIceThicknessLast7days'] = f'{wanted_ice_thickness[0]:.2f}'
                    ref_lake['ThickestIceLayerLast7days'] = f'{wanted_thickest_icelayer[0]:.2f}'
                    # Save status 7 days ahead
                    ref_lake['SnowThicknessNext7days'] = f'{wanted_snow_thickness[2]:.2f}'
                    ref_lake['SlushThicknessNext7days'] = f'{wanted_slush_thickness[2]:.2f}'
                    ref_lake['TotalPureIceThicknessNext7days'] = f'{wanted_ice_thickness[2]:.2f}'
                    ref_lake['ThickestIceLayerNext7days'] = f'{wanted_thickest_icelayer[2]:.2f}'
                    # Calculate differences and put in the dictionary
                    tdiff = wanted_snow_thickness[1] - wanted_snow_thickness[0]
                    ref_lake['SnowThicknessChangeLast7days'] = f'{tdiff:.2f}'
                    tdiff = wanted_slush_thickness[1] - wanted_slush_thickness[0]
                    ref_lake['SlushThicknessChangeLast7days'] = f'{tdiff:.2f}'
                    tdiff = wanted_ice_thickness[1] - wanted_ice_thickness[0]
                    ref_lake['TotalPureIceThicknessChangeLast7days'] = f'{tdiff:.2f}'
                    tdiff = wanted_thickest_icelayer[1] - wanted_thickest_icelayer[0]
                    ref_lake['ThickestIceLayerChangeLast7days'] = f'{tdiff:.2f}'
                    tdiff = wanted_snow_thickness[2] - wanted_snow_thickness[1]
                    ref_lake['SnowThicknessChangeNext7days'] = f'{tdiff:.2f}'
                    tdiff = wanted_slush_thickness[2] - wanted_slush_thickness[1]
                    ref_lake['SlushThicknessChangeNext7days'] = f'{tdiff:.2f}'
                    tdiff = wanted_ice_thickness[2] - wanted_ice_thickness[1]
                    ref_lake['TotalPureIceThicknessChangeNext7days'] = f'{tdiff:.2f}'
                    tdiff = wanted_thickest_icelayer[2] - wanted_thickest_icelayer[1]
                    ref_lake['ThickestIceLayerChangeNext7days'] = f'{tdiff:.2f}'

                    if make_plots:
                        # First remove dates before wanted plot date
                        newdate = []
                        newtemp = []
                        newsno = []
                        newsno_tot = []
                        for i in range(len(date)):
                            if date[i] >= start_plot_date:
                                newdate.append(date[i])
                                newtemp.append(temp[i])
                                newsno.append(sno[i])
                                newsno_tot.append(sno_tot[i])
                        plot_path_and_filename = '{0}{1}'.format(plot_folder, plot_filename)
                        # pts.plot_ice_cover(calculated_ice, observed_ice, newdate, temp, sno, sno_tot, plot_path_and_filename)
                        pts.plot_reference_lake(calculated_ice, observed_ice, newdate, newtemp, newsno, newsno_tot, plot_path_and_filename)

                    # Add todays date
                    today_date = f'{calculation_date.day:02}.{calculation_date.month:02}.{calculation_date.year}'
                    reference_lakes_data['icemodelRunDate'] = today_date

                    # Create pretty string json
                    reference_lakes_output_json = json.dumps(reference_lakes_data, ensure_ascii=False, indent=2)

                    with open(output_filename, 'w', encoding='utf-8-sig') as outfile:
                        outfile.write(reference_lakes_output_json)
                        outfile.close()

    # Save revised json-files
    with open(output_latest_filename, 'w', encoding='utf-8-sig') as outfile_latest:
        outfile_latest.write(reference_lakes_output_json)
        outfile_latest.close()

    return
def run_mosselva(from_date, to_date, make_plots=True, plot_folder=se.plot_folder, forcing='grid'):
    """

    :param from_date:
    :param to_date:
    :param make_plots:
    :param plot_folder:
    :return:
    """

    location_name = 'Mosselva'
    y = 6595744
    x = 255853
    altitude = 25

    met_stnr = 17150    # Rygge målestasjon (met.no)

    first_ice = ice.IceColumn(dt.datetime(int(from_date[0:4]), 12, 31), [])
    first_ice.add_metadata('LocationName', location_name)       # used when plotting
    observed_ice = [first_ice]

    year = '{0}-{1}'.format(from_date[0:4], to_date[2:4])

    # Change dates to datetime. Some of the getdata modules require datetime
    from_date = dt.datetime.strptime(from_date, '%Y-%m-%d')
    to_date = dt.datetime.strptime(to_date, '%Y-%m-%d')

    # if to_date forward in time, make sure it doesnt go to far..
    if to_date > dt.datetime.now():
        to_date = dt.datetime.now() + dt.timedelta(days=7)

    if forcing == 'eKlima':

        wsTemp = gws.getMetData(met_stnr, 'TAM', from_date, to_date, 0, 'list')
        gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
        gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

        temp, date = we.strip_metadata(wsTemp, get_date_times=True)
        sno = we.strip_metadata(gridSno)
        sno_tot = we.strip_metadata(gridSnoTot)
        cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}_{1}_eklima.png'.format(location_name, year)

    elif forcing == 'grid':

        gridTemp = gts.getgts(x, y, 'tm', from_date, to_date)
        gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date)
        gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date)

        gridTempNewElevation = we.adjust_temperature_to_new_altitude(gridTemp, altitude)

        temp, date = we.strip_metadata(gridTempNewElevation, get_date_times=True)
        sno = we.strip_metadata(gridSno)
        sno_tot = we.strip_metadata(gridSnoTot)
        cc = dp.clouds_from_precipitation(sno)

        plot_filename = '{0}_{1}_grid.png'.format(location_name, year)

    else:
        temp, date = None, None
        sno = None
        sno_tot = None
        cc = None

        plot_filename = '{0}_{1}_no_forcing.png'.format(location_name, year)

    calculated_ice = it.calculate_ice_cover_air_temp(copy.deepcopy(first_ice), date, temp, sno, cloud_cover=cc)

    if make_plots:
        plot_path_and_filename = '{0}{1}'.format(plot_folder, plot_filename)
        pts.plot_ice_cover(calculated_ice, observed_ice, date, temp, sno, sno_tot, plot_path_and_filename)