示例#1
0
def importColumns(filepath_inn):
    """Reads observed ice columns from file. Input file contains multiple observations.
    Method returns a list of ice coluns.

    :param filepath_inn:    path to the .csv file with observations
    :return:                a list of iceColumn objects [iceColum, iceColum, iceColum, ...]

    Example of inputfile:

    ##################
    #
    #  Observed icecolumns on Otrøvannet winter 2011-2012
    #  Values given as [value; type].
    #  Valid values are: 'date', 'water_line' and all the ice types: 'black_ice', 'slush_ice', 'slush', 'snow'.
    #  Hashtags are omitted and ' ', \n and such are removed when reading the file.
    #  An observation with only date is of "no ice".
    #
    ##################

    2011-12-08; date

    ##################

    2012-01-16; date
    0.32; snow
    0.01; slush
    0.08; slush_ice
    0.22; black_ice
    0.29; water_line

    ##################

    """

    # Read file
    innfile = open(filepath_inn)
    inndata = innfile.readlines()
    innfile.close()

    # fileseperator
    separator = ';'

    # This column is only for initiation and is removed from columns before returned
    column = ice.IceColumn(dt.datetime(1, 1, 1), 0)
    columns = []

    for row in inndata:
        row = row.strip()  # get rid of ' ' and '\n' and such on the row
        if len(row) != 0 and row[0] != '#':
            row = row.split(
                separator)  # splits line into list of elements in the line
            row[0] = row[0].strip(
            )  # get rid of ' ' and '\n' and such on the separate elements
            row[1] = row[1].strip()
            if row[1] == 'date':
                columns.append(copy.deepcopy(column))
                date = dt.datetime.strptime(row[0], "%Y-%m-%d")
                column = ice.IceColumn(date, 0)
            elif row[1] == 'water_line':
                column.water_line = float(row[0])
            else:
                column.add_layer_at_index(-1,
                                          ice.IceLayer(float(row[0]), row[1]))

    # the last object is not added in the loop
    columns.append(column)

    # this was the inital object. If data is the first element of the inputfile this object is empty.
    # If date was not sett before rows with icelayers the icelayer data is lost.
    columns.remove(columns[0])

    # This must be set on each object because Ive not been able to find a way to initialize it properly yet
    for column in columns:
        if column.water_line == -1:
            column.update_water_line()

    return columns
示例#2
0
def calculate_ice_cover_eb(utm33_x,
                           utm33_y,
                           date,
                           temp_atm,
                           prec,
                           prec_snow,
                           cloud_cover,
                           wind,
                           rel_hum,
                           pressure_atm,
                           inn_column=None):
    """

    :param utm33_x:
    :param utm33_y:
    :param date:
    :param temp_atm:
    :param prec:
    :param prec_snow:
    :param cloud_cover:
    :param wind:
    :param inn_column:
    :return:
    """

    if inn_column is None:
        inn_column = ice.IceColumn(date[0], [])

    icecover = []
    time_span_in_sec = 60 * 60 * 24  # fixed timestep of 24hrs given in seconds
    inn_column.remove_metadata()
    inn_column.remove_time()
    icecover.append(copy.deepcopy(inn_column))
    energy_balance = []

    age_factor_tau = 0.
    albedo_prim = const.alfa_black_ice

    for i in range(0, len(date), 1):
        print("{0}".format(date[i]))
        if date[i] < inn_column.date:
            i = i + 1
        else:
            out_column, eb = get_ice_thickness_from_energy_balance(
                utm33_x=utm33_x,
                utm33_y=utm33_y,
                ice_column=inn_column,
                temp_atm=temp_atm[i],
                prec=prec[i],
                prec_snow=prec_snow[i],
                time_span_in_sec=time_span_in_sec,
                albedo_prim=albedo_prim,
                age_factor_tau=age_factor_tau,
                wind=wind[i],
                cloud_cover=cloud_cover[i],
                rel_hum=rel_hum[i],
                pressure_atm=pressure_atm[i])

            icecover.append(out_column)
            energy_balance.append(eb)
            inn_column = copy.deepcopy(out_column)

            if eb.EB is None:
                age_factor_tau = 0.
                albedo_prim = const.alfa_black_ice
            else:
                age_factor_tau = eb.age_factor_tau
                albedo_prim = eb.albedo_prim

    return icecover, energy_balance
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
示例#5
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)