Ejemplo n.º 1
0
def runStorvannetHammerfest(startDate, endDate):

    LocationNames = ["Storvannet, 7 moh"]
    x = 821340
    y = 7862497

    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    cs_temp = gcsd.getGriddata(x, y, "tm", from_date, to_date)
    cs_sno = gcsd.getGriddata(x, y, "fsw", from_date, to_date)
    cs_snotot = gcsd.getGriddata(x, y, "sd", from_date, to_date)
    wsCC = gws.getMetData(95350, "NNM", startDate, endDate, 0, "list")  # BANAK - østover innerst i fjorden

    temp, date = we.strip_metadata(cs_temp, True)
    sno = we.strip_metadata(cs_sno, False)
    snotot = we.strip_metadata(cs_snotot, False)
    cc = we.strip_metadata(wsCC, False)

    observed_ice = gro.get_all_season_ice(LocationNames, startDate, endDate)

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

    plot_filename = "{0}StorvannetHammerfest {1}-{2}.png".format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(ice_cover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 2
0
def doAREMCAnalyssis(stnr, startDate, endDate):
    """
    Preforms a Ragnar Ekker variant of Monte Carlo (or whatever). This metod runs an analysis of several conbinations
    of gammadistributions for precipitation and temperature affect on cloudcover. Results are written to a sqllite
    database for further study.

    :param stnr:        eKlima station where there is cloudcover for testing method.
    :param startDate:   simulations and referance data from this data
    :param endDate:     simulations and referance data to this data
    :return:
    """

    wsTemp = getMetData(stnr, 'TAM', startDate, endDate, 0, 'list')
    wsPrec = getMetData(stnr, 'RR', startDate, endDate, 0, 'list')
    wsCC = getMetData(stnr, 'NNM', startDate, endDate, 0, 'list')

    temp, date = strip_metadata(wsTemp, get_dates=True)
    prec = strip_metadata(wsPrec, False)
    clouds = strip_metadata(wsCC, False)

    loggdate = datetime.datetime.now()
    period = '{0} to {1}'.format(startDate,endDate)

    cs = [1]        # cloudshift the observed data one day forward

    psh = [2.8, 3.0, 3.3]          # prec gamma shapefactor
    psc = [2.0]                    # prec gamma scalefactor
    pdb = [0.2, 0.3, 0.5, 0.7]     # prec days back
    pap = [0.3, 0.4, 0.5]          # prec amplitude factor

    tsh = [4.3, 4.5, 4.8, 5.0]     # temp gamma shapefactor
    tsc = [1.0]                    # temp gamma scalefactor
    tdb = [4.8, 5.0, 5.5]          # temp days back
    tap = [0.02, 0.03, 0.05]       # temp amplitude factor

    estimateSimulations = len(cs)*len(psh)*len(psc)*len(pdb)*len(pap)*len(tsh)*len(tsc)*len(tdb)*len(tap)
    doneSimulations = 0

    for a in cs:
        cloudsShifted = __shiftClouds(clouds, a)
        for b in psh:
            for c in psc:
                for d in pdb:
                    for e in pap:
                        for f in tsh:
                            for g in tsc:
                                for h in tdb:
                                    for i in tap:

                                        estClouds = dgc.cc_gamma_prec_and_temp_change(prec, temp, [b, c, d, e], [f, g, h, i])

                                        # What is the root mean square of estimatet vs observed clouds?
                                        rms = np.sqrt(((np.array(estClouds) - np.array(cloudsShifted)) ** 2).mean())

                                        __writeRMC2database(database_location, a, b, c, d, e, f, g, h, i, rms, loggdate, stnr, period)

                                        doneSimulations = doneSimulations + 1
                                        print('beregning {0} av {1}'.format(doneSimulations, estimateSimulations))

    return
Ejemplo n.º 3
0
def runGiljastolsvatnet(startDate, endDate):

    LocationNames = ['Giljastølsvatnet 412 moh', 'Giljastølvatnet sør 412 moh']
    x = -1904
    y = 6553573

    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    #cs_temp = gcsd.getStationdata('189.3.0','17.1', from_date, to_date)
    cs_temp = gcsd.getGriddata(x, y, 'tm', from_date, to_date)
    cs_sno = gcsd.getGriddata(x, y, 'fsw', from_date, to_date)
    cs_snotot = gcsd.getGriddata(x, y, 'sd', from_date, to_date)
    wsCC = gws.getMetData(43010, 'NNM', startDate, endDate, 0, 'list')  # Eik - Hove. Ligger lenger sør men er litt inn i landet.
    #wsCC = getMetData(43010, 'NNM', startDate, endDate, 0, 'list') # Sola (44560) er et alternativ

    temp, date = we.strip_metadata(cs_temp, True)
    sno = we.strip_metadata(cs_sno, False)
    snotot = we.strip_metadata(cs_snotot, False)
    cc = we.strip_metadata(wsCC, False)

    observed_ice = gro.get_all_season_ice(LocationNames, startDate, endDate)

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

    plot_filename = '{0}Giljastolsvatnet {1}-{2}.png'.format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(ice_cover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 4
0
def doAREMCAnalyssis(stnr, startDate, endDate):
    """
    Preforms a Ragnar Ekker variant of Monte Carlo (or whatever). This metod runs an analysis of several conbinations
    of gammadistributions for precipitation and temperature affect on cloudcover. Results are written to a sqllite
    database for further study.

    :param stnr:        eKlima station where there is cloudcover for testing method.
    :param startDate:   simulations and referance data from this data
    :param endDate:     simulations and referance data to this data
    :return:
    """

    wsTemp = getMetData(stnr, 'TAM', startDate, endDate, 0, 'list')
    wsPrec = getMetData(stnr, 'RR', startDate, endDate, 0, 'list')
    wsCC = getMetData(stnr, 'NNM', startDate, endDate, 0, 'list')

    temp, date = strip_metadata(wsTemp, get_dates=True)
    prec = strip_metadata(wsPrec, False)
    clouds = strip_metadata(wsCC, False)

    loggdate = datetime.datetime.now()
    period = '{0} to {1}'.format(startDate,endDate)

    cs = [1]        # cloudshift the observed data one day forward

    psh = [2.8, 3.0, 3.3]          # prec gamma shapefactor
    psc = [2.0]                    # prec gamma scalefactor
    pdb = [0.2, 0.3, 0.5, 0.7]     # prec days back
    pap = [0.3, 0.4, 0.5]          # prec amplitude factor

    tsh = [4.3, 4.5, 4.8, 5.0]     # temp gamma shapefactor
    tsc = [1.0]                    # temp gamma scalefactor
    tdb = [4.8, 5.0, 5.5]          # temp days back
    tap = [0.02, 0.03, 0.05]       # temp amplitude factor

    estimateSimulations = len(cs)*len(psh)*len(psc)*len(pdb)*len(pap)*len(tsh)*len(tsc)*len(tdb)*len(tap)
    doneSimulations = 0

    for a in cs:
        cloudsShifted = __shiftClouds(clouds, a)
        for b in psh:
            for c in psc:
                for d in pdb:
                    for e in pap:
                        for f in tsh:
                            for g in tsc:
                                for h in tdb:
                                    for i in tap:

                                        estClouds = dgc.cc_gamma_prec_and_temp_change(prec, temp, [b, c, d, e], [f, g, h, i])

                                        # What is the root mean square of estimatet vs observed clouds?
                                        rms = np.sqrt(((np.array(estClouds) - np.array(cloudsShifted)) ** 2).mean())

                                        __writeRMC2database(database_location, a, b, c, d, e, f, g, h, i, rms, loggdate, stnr, period)

                                        doneSimulations = doneSimulations + 1
                                        print('beregning {0} av {1}'.format(doneSimulations, estimateSimulations))

    return
Ejemplo n.º 5
0
def runSkoddebergvatnet(startDate, endDate):

    LocationName = "Skoddebergvatnet - nord 101 moh"
    # Skoddebergvatnet - sør 101 moh
    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    # cs_temp = gcsd.getGriddata('189.3.0','17.1', from_date, to_date)
    cs_temp = gcsd.getGriddata(593273, 7612469, "tm", from_date, to_date)
    cs_sno = gcsd.getGriddata(593273, 7612469, "fsw", from_date, to_date)
    cs_snotot = gcsd.getGriddata(593273, 7612469, "sd", from_date, to_date)
    wsCC = gws.getMetData(87640, "NNM", startDate, endDate, 0, "list")  # Harstad Stadion

    temp, date = we.strip_metadata(cs_temp, True)
    sno = we.strip_metadata(cs_sno, False)
    snotot = we.strip_metadata(cs_snotot, False)
    cc = we.strip_metadata(wsCC, False)

    observed_ice = gro.get_all_season_ice(LocationName, startDate, endDate)

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

    plot_filename = "{0}Skoddebergvatnet {1}-{2}.png".format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(ice_cover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 6
0
def runHakkloa(startDate, endDate):

    LocationName = "Hakkloa nord 372 moh"
    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    cs_temp = gcsd.getStationdata("6.24.4", "17.1", from_date, to_date, timeseries_type=0)
    cs_sno = gcsd.getGriddata(260150, 6671135, "fsw", from_date, to_date, timeseries_type=0)
    cs_snotot = gcsd.getGriddata(260150, 6671135, "sd", from_date, to_date, timeseries_type=0)
    wsCC = gws.getMetData(18700, "NNM", startDate, endDate, 0, "list")

    temp, date = we.strip_metadata(cs_temp, True)
    sno = we.strip_metadata(cs_sno, False)
    snotot = we.strip_metadata(cs_snotot, False)
    cc = we.strip_metadata(wsCC, False)

    observed_ice = gro.get_all_season_ice(LocationName, startDate, endDate)

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

    plot_filename = "{0}Hakkloa {1}-{2}.png".format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(ice_cover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 7
0
def runBaklidammen(startDate, endDate):

    LocationNames = ["Baklidammen 200 moh"]
    x = 266550
    y = 7040812

    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    cs_temp = gcsd.getGriddata(x, y, "tm", from_date, to_date)
    cs_sno = gcsd.getGriddata(x, y, "fsw", from_date, to_date)
    cs_snotot = gcsd.getGriddata(x, y, "sd", from_date, to_date)
    wsCC = gws.getMetData(68860, "NNM", startDate, endDate, 0, "list")  # TRONDHEIM - VOLL

    temp, date = we.strip_metadata(cs_temp, True)
    sno = we.strip_metadata(cs_sno, False)
    snotot = we.strip_metadata(cs_snotot, False)
    cc = we.strip_metadata(wsCC, False)

    observed_ice = gro.get_all_season_ice(LocationNames, startDate, endDate)

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

    plot_filename = "{0}Baklidammen {1}-{2}.png".format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(ice_cover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 8
0
def runGiljastolsvatnet(startDate, endDate):

    LocationNames = ["Giljastølsvatnet 412 moh", "Giljastølvatnet sør 412 moh"]
    x = -1904
    y = 6553573

    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    # cs_temp = gcsd.getStationdata('189.3.0','17.1', from_date, to_date)
    cs_temp = gcsd.getGriddata(x, y, "tm", from_date, to_date)
    cs_sno = gcsd.getGriddata(x, y, "fsw", from_date, to_date)
    cs_snotot = gcsd.getGriddata(x, y, "sd", from_date, to_date)
    wsCC = gws.getMetData(
        43010, "NNM", startDate, endDate, 0, "list"
    )  # Eik - Hove. Ligger lenger sør men er litt inn i landet.
    # wsCC = getMetData(43010, 'NNM', startDate, endDate, 0, 'list') # Sola (44560) er et alternativ

    temp, date = we.strip_metadata(cs_temp, True)
    sno = we.strip_metadata(cs_sno, False)
    snotot = we.strip_metadata(cs_snotot, False)
    cc = we.strip_metadata(wsCC, False)

    observed_ice = gro.get_all_season_ice(LocationNames, startDate, endDate)

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

    plot_filename = "{0}Giljastolsvatnet {1}-{2}.png".format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(ice_cover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 9
0
def runBaklidammen(startDate, endDate):

    LocationNames = ['Baklidammen 200 moh']
    x = 266550
    y = 7040812

    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    cs_temp = gcsd.getGriddata(x, y, 'tm', from_date, to_date)
    cs_sno = gcsd.getGriddata(x, y, 'fsw', from_date, to_date)
    cs_snotot = gcsd.getGriddata(x, y, 'sd', from_date, to_date)
    wsCC = gws.getMetData(68860, 'NNM', startDate, endDate, 0, 'list')  # TRONDHEIM - VOLL

    temp, date = we.strip_metadata(cs_temp, True)
    sno = we.strip_metadata(cs_sno, False)
    snotot = we.strip_metadata(cs_snotot, False)
    cc = we.strip_metadata(wsCC, False)

    observed_ice = gro.get_all_season_ice(LocationNames, startDate, endDate)

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

    plot_filename = '{0}Baklidammen {1}-{2}.png'.format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(ice_cover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 10
0
def runStorvannetHammerfest(startDate, endDate):

    LocationNames = ['Storvannet, 7 moh']
    x = 821340
    y = 7862497

    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    cs_temp = gcsd.getGriddata(x, y, 'tm', from_date, to_date)
    cs_sno = gcsd.getGriddata(x, y, 'fsw', from_date, to_date)
    cs_snotot = gcsd.getGriddata(x, y, 'sd', from_date, to_date)
    wsCC = gws.getMetData(95350, 'NNM', startDate, endDate, 0, 'list')  # BANAK - østover innerst i fjorden

    temp, date = we.strip_metadata(cs_temp, True)
    sno = we.strip_metadata(cs_sno, False)
    snotot = we.strip_metadata(cs_snotot, False)
    cc = we.strip_metadata(wsCC, False)

    observed_ice = gro.get_all_season_ice(LocationNames, startDate, endDate)

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

    plot_filename = '{0}StorvannetHammerfest {1}-{2}.png'.format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(ice_cover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 11
0
def runSkoddebergvatnet(startDate, endDate):

    LocationName = 'Skoddebergvatnet - nord 101 moh'
    # Skoddebergvatnet - sør 101 moh
    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    #cs_temp = gcsd.getGriddata('189.3.0','17.1', from_date, to_date)
    cs_temp = gcsd.getGriddata(593273, 7612469, 'tm', from_date, to_date)
    cs_sno = gcsd.getGriddata(593273, 7612469, 'fsw', from_date, to_date)
    cs_snotot = gcsd.getGriddata(593273, 7612469, 'sd', from_date, to_date)
    wsCC = gws.getMetData(87640, 'NNM', startDate, endDate, 0, 'list')  # Harstad Stadion

    temp, date = we.strip_metadata(cs_temp, True)
    sno = we.strip_metadata(cs_sno, False)
    snotot = we.strip_metadata(cs_snotot, False)
    cc = we.strip_metadata(wsCC, False)

    observed_ice = gro.get_all_season_ice(LocationName, startDate, endDate)

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

    plot_filename = '{0}Skoddebergvatnet {1}-{2}.png'.format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(ice_cover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 12
0
def runHakkloa(startDate, endDate):

    LocationName = 'Hakkloa nord 372 moh'
    from_date = dt.datetime.strptime(startDate, "%Y-%m-%d")
    to_date = dt.datetime.strptime(endDate, "%Y-%m-%d")

    cs_temp = gcsd.getStationdata('6.24.4','17.1', from_date, to_date, timeseries_type=0)
    cs_sno = gcsd.getGriddata(260150, 6671135, 'fsw', from_date, to_date, timeseries_type=0)
    cs_snotot = gcsd.getGriddata(260150, 6671135, 'sd', from_date, to_date, timeseries_type=0)
    wsCC = gws.getMetData(18700, 'NNM', startDate, endDate, 0, 'list')

    temp, date = we.strip_metadata(cs_temp, True)
    sno = we.strip_metadata(cs_sno, False)
    snotot = we.strip_metadata(cs_snotot, False)
    cc = we.strip_metadata(wsCC, False)

    observed_ice = gro.get_all_season_ice(LocationName, startDate, endDate)

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

    plot_filename = '{0}Hakkloa {1}-{2}.png'.format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(ice_cover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 13
0
def runSemsvannEB(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_dates=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(location_name, startDate, endDate)

    ice_cover, energy_balance = 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(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(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,
    )
Ejemplo n.º 14
0
def runOrovannEB(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_dates=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(location_name, startDate, endDate)

    ice_cover, energy_balance = 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(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(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,
    )
Ejemplo n.º 15
0
def __test_clouds_from_short_wave():

    import getWSklima as gws
    import weather as we

    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()
Ejemplo n.º 16
0
def runSemsvannEB(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_dates=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(location_name, startDate, endDate)

    ice_cover, energy_balance = 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(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(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)
Ejemplo n.º 17
0
def runSemsvann(startDate, endDate):

    LocationName = "Semsvannet v/Lo 145 moh"

    wsTemp = gws.getMetData(19710, "TAM", startDate, endDate, 0, "list")
    wsSno = gws.getMetData(19710, "SA", startDate, endDate, 0, "list")
    wsCC = gws.getMetData(18700, "NNM", startDate, endDate, 0, "list")

    temp, date = we.strip_metadata(wsTemp, True)
    snotot = we.strip_metadata(wsSno, False)
    cc = we.strip_metadata(wsCC, False)

    sno = dp.delta_snow_from_total_snow(snotot)

    # observed_ice_filename = '{0}Semsvann observasjoner {1}-{2}.csv'.format(data_path, startDate[0:4], endDate[0:4])
    # observed_ice = importColumns(observed_ice_filename)
    observed_ice = gro.get_all_season_ice(LocationName, startDate, endDate)
    if len(observed_ice) == 0:
        icecover = calculate_ice_cover_air_temp(ice.IceColumn(date[0], []), date, temp, sno, cc)
    else:
        icecover = calculate_ice_cover_air_temp(copy.deepcopy(observed_ice[0]), date, temp, sno, cc)

    plot_filename = "{0}Semsvann {1}-{2}.png".format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(icecover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 18
0
def runSemsvann(startDate, endDate):

    LocationName = 'Semsvannet v/Lo 145 moh'

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

    temp, date = we.strip_metadata(wsTemp, True)
    snotot = we.strip_metadata(wsSno, False)
    cc = we.strip_metadata(wsCC, False)

    sno = dp.delta_snow_from_total_snow(snotot)

    #observed_ice_filename = '{0}Semsvann observasjoner {1}-{2}.csv'.format(data_path, startDate[0:4], endDate[0:4])
    #observed_ice = importColumns(observed_ice_filename)
    observed_ice = gro.get_all_season_ice(LocationName, startDate, endDate)
    if len(observed_ice) == 0:
        icecover = calculate_ice_cover_air_temp(ice.IceColumn(date[0], []), date, temp, sno, cc)
    else:
        icecover = calculate_ice_cover_air_temp(copy.deepcopy(observed_ice[0]), date, temp, sno, cc)

    plot_filename = '{0}Semsvann {1}-{2}.png'.format(plot_folder, startDate[0:4], endDate[0:4])
    pts.plot_ice_cover(icecover, observed_ice, date, temp, snotot, plot_filename)
Ejemplo n.º 19
0
def runOrovannEB(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_dates=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(location_name, startDate, endDate)

    ice_cover, energy_balance = 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(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(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)
Ejemplo n.º 20
0
def __test_clouds_from_short_wave():

    import getWSklima as gws
    import weather as we

    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()
Ejemplo n.º 21
0
def makeSomeScatterPlots(stnr, startDate, endDate):

    import numpy as numpy
    import matplotlib.pyplot as plt
    from scipy.stats import gaussian_kde


    wsTemp = getMetData(stnr, 'TAM', startDate, endDate, 0, 'list')
    wsPrec = getMetData(stnr, 'RR', startDate, endDate, 0, 'list')
    wsCC = getMetData(stnr, 'NNM', startDate, endDate, 0, 'list')

    temp, date = strip_metadata(wsTemp, get_dates=True)
    prec = strip_metadata(wsPrec, False)
    clouds = strip_metadata(wsCC, False)

    dTemp = dpz.delta_temperature_from_temperature(temp)
    abs_dTemp = map(abs, dTemp)

    method = 'dTemp_with_limit_vs_clouds'

    if method == 'dTemp_with_limit_vs_clouds':

        # Calibration params
        limit_temperature = [7]

        for limit_temp in limit_temperature:

            # Mask out positions in list where temps are above and below the threshhold temp
            temp_filter_high = []
            temp_filter_low = []
            for t in temp:
                if t >= limit_temp:
                    temp_filter_high.append(1.)
                    temp_filter_low.append(0.)
                else:
                    temp_filter_high.append(0.)
                    temp_filter_low.append(1.)

            # And make lists of dTemp_abs beloning to either high or low bands relative the threshhold temp
            dTemp_low_abs = [dt*tfl for dt,tfl in zip(abs_dTemp, temp_filter_low)]
            dTemp_high_abs = [dt*tfh for dt,tfh in zip(abs_dTemp, temp_filter_high)]

            fsize = (20, 20)
            plt.figure(figsize=fsize)
            plt.clf()

            #### Generate data
            x = [round(dtla,1) for dtla in dTemp_low_abs]
            y = [round(cc,2) for cc in clouds]

            # Calculate the point density
            xy = np.vstack([x,y])
            z = gaussian_kde(xy)(xy)

            plt.subplot(2,2,1).scatter(x, y, c=z, s=100, edgecolor='')
            plt.title("dtemp vs clouds (temp < {3}C) {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7], limit_temp))

            ##### Generate data
            x = [round(dtha, 1) for dtha in dTemp_high_abs]
            y = clouds

            # Calculate the point density
            xy = np.vstack([x,y])
            z = gaussian_kde(xy)(xy)

            plt.subplot(2,2,2).scatter(x, y, c=z, s=100, edgecolor='')
            plt.title("dtemp vs clouds (temp >= {3}C) {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7], limit_temp))


            plt.subplot(2,2,3).hist(z)


            fileName = "scatter dtemp vs clouds temp_limit{3} {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7], limit_temp)
            plt.savefig("{0}{1}".format(plot_folder, fileName))

    if method == 'dTemp_density':

        # Generate  data
        x = abs_dTemp
        y = clouds

        # Calculate the point density
        xy = np.vstack([x,y])
        z = gaussian_kde(xy)(xy)

        fig, ax = plt.subplots()
        ax.scatter(x, y, c=z, s=100, edgecolor='')

        plt.title("scatter dtemp_density clouds {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7]))
        fileName = "scatter dtemp_density {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7])

        plt.savefig("{0}{1}".format(plot_folder, fileName))

    if method == 'prec':

        fsize = (10, 10)
        plt.figure(figsize=fsize)
        plt.clf()

        plt.title("scatter prec clouds {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7]))
        fileName = "scatter prec clouds {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7])

       # Generate data
        x = prec
        y = clouds

        # Calculate the point density
        xy = np.vstack([x,y])
        z = gaussian_kde(xy)(xy)

        plt.scatter(x, y, c=z, s=100, edgecolor='')

        # calc the trendline
        z = numpy.polyfit(prec, clouds, 1)
        p = numpy.poly1d(z)
        plt.plot(prec, p(prec) )

        # the line equation:
        line_equation = "clouds = %.6f*prec + %.6f"%(z[0],z[1])
        plt.text(0.0, -0.1, 'Line equation: {0}'.format(line_equation))

        plt.savefig("{0}{1}".format(plot_folder, fileName))





    return
Ejemplo n.º 22
0
def correlateCloudsAndTemp(stnr, startDate, endDate):
    """
    DEAD END

    This was an attempt to check different functions of temperature and how well they correlate with an observed
    cloudcover. It turned out all different functions represent different signals so a cross correlation test and
    finding wich has the highest number whont work because the correlation number wil be different from function to
    function.

    :param stnr:
    :param startDate:
    :param endDate:
    :return:

    SELECT * FROM corrCCandTemp
    where loggdate = '2015-02-09 15:57:44.287000'
    order by crossCorr desc

    """


    wsTemp = getMetData(stnr, 'TAM', startDate, endDate, 0, 'list')
    wsCC = getMetData(stnr, 'NNM', startDate, endDate, 0, 'list')

    temp = strip_metadata(wsTemp, False)
    clouds = strip_metadata(wsCC, False)

    oneMinusClouds = [1-cc for cc in clouds]
    dTemp = dpz.delta_temperature_from_temperature(temp)
    abs_dTemp = map(abs, dTemp)
    #sum = [cc + mcc for cc, mcc in zip(clouds,oneMinusClouds)]

    loggdate = datetime.datetime.now()
    period = '{0} to {1}'.format(startDate,endDate)

    tempOffset = [0] #range(-5,6,1)
    dTempOffset = [0] #range(-3,4,1)
    ccShift = range(-4,5,1)
    sign_temp_dependant = [False]
    sign_dTemp_dependant = [False]
    useOneMinusClouds = [False]  # one minus cloudcover

    estimateSimulations = len(tempOffset)*len(dTempOffset) * len(ccShift) * len(sign_temp_dependant) * len(sign_dTemp_dependant) * len(useOneMinusClouds)
    doneSimulations = 0

    for cs in ccShift:
        for to in tempOffset:
            for dto in dTempOffset:
                for depdT in sign_dTemp_dependant:
                    for depT in sign_temp_dependant:
                        for omc in useOneMinusClouds:

                            sign_dTemp = 0
                            sign_temp = 0
                            ccwithoffset = 0

                            # if dTempExpression is not dependant of the signs of temp and dTemp the expression is reduced
                            # to only the abolute value of dTemp
                            if depdT == True:
                                sign_dTemp = __getSign(dTemp, dto)
                            else:
                                sign_dTemp = [1.]*len(temp)

                            if depT == True:
                                sign_temp = __getSign(temp, to)
                            else:
                                sign_temp = [1.]*len(temp)

                            # are we looking at cloud cover or clear skies? Does it matter?
                            if omc == True: # this option would be fraction og clear skies
                                ccwithoffset = __shiftClouds(oneMinusClouds, cs)
                            else:
                                ccwithoffset = __shiftClouds(clouds, cs)

                            dTempExpression = [sdt*adt*st for sdt,adt,st in zip(sign_dTemp,abs_dTemp,sign_temp)]
                            crossCorr = np.correlate(dTempExpression, ccwithoffset)

                            a = 1
                            __writeCorrelation2database(database_location,  cs, to, dto, depdT, depT, omc, crossCorr[0], loggdate, period, stnr)

                            doneSimulations = doneSimulations + 1
                            print('beregning {0} av {1}'.format(doneSimulations, estimateSimulations))

    return
Ejemplo n.º 23
0
def testCloudMaker(stnr, startDate, endDate, method):
    """Gets data from a eKlima station and smooths the data depending on which method we wish to test.
    We find the nash-sutcliffe to observations of clouds. And we plot and save the result to Plots folder.

    :param stnr:        eKlima station where there is cloud cover for testing method.
    :param startDate:   simulations and reference data from this data
    :param endDate:     simulations and reference data to this data
    :param method:      specify which method to be tested
    :return:

    Available methods to test:
    cc_from_prec
    ccFromAveragePrec
    ccFromAverageObsCc
    ccGammaPrec
    ccGammaTemp
    ccGammaPrecAndTemp"""

    wsTemp = getMetData(stnr, 'TAM', startDate, endDate, 0, 'list')
    wsPrec = getMetData(stnr, 'RR', startDate, endDate, 0, 'list')
    wsCC = getMetData(stnr, 'NNM', startDate, endDate, 0, 'list')

    temp, date = strip_metadata(wsTemp, get_dates=True)
    prec = strip_metadata(wsPrec)
    clouds = strip_metadata(wsCC)

    dayShift = 1
    clouds = __shiftClouds(clouds, dayShift)


    if method == 'ccFromRandomThomas':
        estClouds = dpz.clouds_from_precipitation(prec, method='Random Thomas')
        gammaFigtext = 'No gamma smoothing and dayshift = {0}'.format(dayShift)
    elif method == 'ccFromPrec':
        estClouds = dpz.clouds_from_precipitation(prec, method='Binary')
        gammaFigtext = 'No gamma smoothing and dayshift = {0}'.format(dayShift)
    elif method == 'ccFromAveragePrec':
        estClouds = dpz.clouds_from_precipitation(prec, method='Average')
        gammaFigtext = 'No gamma smoothing and dayshift = {0}'.format(dayShift)
    elif method == 'ccFromPrecAndAveragePrec':
        estClouds = dpz.clouds_from_precipitation(prec, method='Binary and average')
        gammaFigtext = ''.format()
    elif method == 'ccFromAverageObsCc':
        estClouds = [sum(clouds)/float(len(clouds))] * len(clouds)
        gammaFigtext = ''.format()
    elif method == 'ccGammaPrec':
        gammaPrec = [2.8, 2., 0.3, 0.4]
        estClouds = dpz.clouds_from_precipitation(prec, method='Binary')
        estClouds = dgc.cc_gamma_smoothing(estClouds, gammaPrec)
        gammaFigtext = "gamma smoothing prec = {0} and dayshift = {1}".format(gammaPrec, dayShift)
    elif method == 'ccGammaTempChange':
        gammaTemp = [4.8, 1., 5., 0.03]
        estClouds = dgc.cc_gamma_temp(temp, gammaTemp)
        gammaFigtext = "gamma smoothing temp = {0} and dayshift = {1}".format(gammaTemp, dayShift)
    elif method == 'ccGammaPrecAndTempChange':
        gammaPrec = [2.8, 2., 0.3, 0.4]
        gammaTemp = [4.8, 1., 5., 0.03]
        estClouds = dgc.cc_gamma_prec_and_temp_change(prec, temp, gammaPrec, gammaTemp)
        gammaFigtext = "prec = {0} and temp = {1} and dayshift = {2}".format(gammaPrec, gammaTemp, dayShift)


    fileName = "{3} {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7], method)

    # What is the root mean square of estimatet vs observed clouds?
    # rms = np.sqrt(((np.array(estClouds) - np.array(clouds)) ** 2).mean())

    # Nash–Sutcliffe model efficiency coefficient from
    # https://en.wikipedia.org/wiki/Nash%E2%80%93Sutcliffe_model_efficiency_coefficient
    numerator = 0
    denominator = 0
    mean_clouds = sum(clouds)/float(len(clouds))
    for i in range(0, len(clouds), 1):
        numerator += (clouds[i] - estClouds[i])**2
        denominator += (clouds[i] - mean_clouds)**2
    nash_sutcliffe = 1 - numerator/denominator

    # Figure dimensions
    fsize = (16, 10)
    plt.figure(figsize=fsize)
    plt.clf()

    # plot total snowdepth on land
    plt.bar(date, prec, width=1, color="0.4")

    # plot the estimated cloud cover
    for i in range(0, len(estClouds) - 1, 1):
        if estClouds[i] > 0:
            plt.hlines(max(prec) * 1.2, date[i], date[i + 1], lw=45, color=str(-(estClouds[i] - 1.)))
        elif estClouds[i] == None:
            plt.hlines(max(prec) * 1.2, date[i], date[i + 1], lw=45, color="pink")
        else:
            plt.hlines(max(prec) * 1.2, date[i], date[i + 1], lw=45, color=str(-(estClouds[i] - 1.)))

    # plot cloud cover from met
    for i in range(0, len(clouds) - 1, 1):
        if clouds[i] > 0:
            plt.hlines(max(prec) * 1.1, date[i], date[i + 1], lw=45, color=str(-(clouds[i] - 1.)))
        elif clouds[i] == None:
            plt.hlines(max(prec) * 1.1, date[i], date[i + 1], lw=45, color="pink")
        else:
            plt.hlines(max(prec) * 1.1, date[i], date[i + 1], lw=45, color=str(-(clouds[i] - 1.)))

    # this plots temperature on separate right side axis
    plt.twinx()
    temp_pluss = []
    temp_minus = []
    for i in range(0, len(temp), 1):
        if temp[i] >= 0:
            temp_pluss.append(temp[i])
            temp_minus.append(np.nan)
        else:
            temp_minus.append(temp[i])
            temp_pluss.append(np.nan)
    plt.plot(date, temp, "black")
    plt.plot(date, temp_pluss, "red")
    plt.plot(date, temp_minus, "blue")

    # title and text fields
    plt.title("{3} {0} {1} {2}".format(stnr, startDate[0:7], endDate[0:7], method))
    plt.text(date[len(date)/2], min(temp)*1.2, 'gamma smoothing [shape, scale, days back, amplification]')
    plt.text(date[len(date)/2], min(temp)*1.3, gammaFigtext)

    # this is a scatter plot of modelled and estimated cloud cover
    xfrac = 0.15
    yfrac = (float(fsize[0])/float(fsize[1])) * xfrac
    xpos = 0.95-xfrac
    ypos = 0.42-yfrac
    a = plt.axes([xpos, ypos, xfrac, yfrac])
    a.scatter(clouds, estClouds)
    plt.setp(a, xticks=[0, 0.5, 1], yticks=[0, 0.5, 1])

    plt.text(0.0, 0.1, 'na_su = {0}'.format(round(nash_sutcliffe, 2)), color='yellow', bbox={'facecolor':'black'})

    plt.savefig("{0}{1}".format(plot_folder, fileName))

    return nash_sutcliffe
Ejemplo n.º 24
0
def correlateCloudsAndTemp(stnr, startDate, endDate):
    """
    DEAD END

    This was an attempt to check different functions of temperature and how well they correlate with an observed
    cloudcover. It turned out all different functions represent different signals so a cross correlation test and
    finding wich has the highest number whont work because the correlation number wil be different from function to
    function.

    :param stnr:
    :param startDate:
    :param endDate:
    :return:

    SELECT * FROM corrCCandTemp
    where loggdate = '2015-02-09 15:57:44.287000'
    order by crossCorr desc

    """


    wsTemp = getMetData(stnr, 'TAM', startDate, endDate, 0, 'list')
    wsCC = getMetData(stnr, 'NNM', startDate, endDate, 0, 'list')

    temp = strip_metadata(wsTemp, False)
    clouds = strip_metadata(wsCC, False)

    oneMinusClouds = [1-cc for cc in clouds]
    dTemp = dpz.delta_temperature_from_temperature(temp)
    abs_dTemp = map(abs, dTemp)
    #sum = [cc + mcc for cc, mcc in zip(clouds,oneMinusClouds)]

    loggdate = datetime.datetime.now()
    period = '{0} to {1}'.format(startDate,endDate)

    tempOffset = [0] #range(-5,6,1)
    dTempOffset = [0] #range(-3,4,1)
    ccShift = range(-4,5,1)
    sign_temp_dependant = [False]
    sign_dTemp_dependant = [False]
    useOneMinusClouds = [False]  # one minus cloudcover

    estimateSimulations = len(tempOffset)*len(dTempOffset) * len(ccShift) * len(sign_temp_dependant) * len(sign_dTemp_dependant) * len(useOneMinusClouds)
    doneSimulations = 0

    for cs in ccShift:
        for to in tempOffset:
            for dto in dTempOffset:
                for depdT in sign_dTemp_dependant:
                    for depT in sign_temp_dependant:
                        for omc in useOneMinusClouds:

                            sign_dTemp = 0
                            sign_temp = 0
                            ccwithoffset = 0

                            # if dTempExpression is not dependant of the signs of temp and dTemp the expression is reduced
                            # to only the abolute value of dTemp
                            if depdT == True:
                                sign_dTemp = __getSign(dTemp, dto)
                            else:
                                sign_dTemp = [1.]*len(temp)

                            if depT == True:
                                sign_temp = __getSign(temp, to)
                            else:
                                sign_temp = [1.]*len(temp)

                            # are we looking at cloud cover or clear skies? Does it matter?
                            if omc == True: # this option would be fraction og clear skies
                                ccwithoffset = __shiftClouds(oneMinusClouds, cs)
                            else:
                                ccwithoffset = __shiftClouds(clouds, cs)

                            dTempExpression = [sdt*adt*st for sdt,adt,st in zip(sign_dTemp,abs_dTemp,sign_temp)]
                            crossCorr = np.correlate(dTempExpression, ccwithoffset)

                            a = 1
                            __writeCorrelation2database(database_location,  cs, to, dto, depdT, depT, omc, crossCorr[0], loggdate, period, stnr)

                            doneSimulations = doneSimulations + 1
                            print('beregning {0} av {1}'.format(doneSimulations, estimateSimulations))

    return
Ejemplo n.º 25
0
def makeSomeScatterPlots(stnr, startDate, endDate):

    import numpy as numpy
    import matplotlib.pyplot as plt
    from scipy.stats import gaussian_kde


    wsTemp = getMetData(stnr, 'TAM', startDate, endDate, 0, 'list')
    wsPrec = getMetData(stnr, 'RR', startDate, endDate, 0, 'list')
    wsCC = getMetData(stnr, 'NNM', startDate, endDate, 0, 'list')

    temp, date = strip_metadata(wsTemp, get_dates=True)
    prec = strip_metadata(wsPrec, False)
    clouds = strip_metadata(wsCC, False)

    dTemp = dpz.delta_temperature_from_temperature(temp)
    abs_dTemp = map(abs, dTemp)

    method = 'dTemp_with_limit_vs_clouds'

    if method == 'dTemp_with_limit_vs_clouds':

        # Calibration params
        limit_temperature = [7]

        for limit_temp in limit_temperature:

            # Mask out positions in list where temps are above and below the threshhold temp
            temp_filter_high = []
            temp_filter_low = []
            for t in temp:
                if t >= limit_temp:
                    temp_filter_high.append(1.)
                    temp_filter_low.append(0.)
                else:
                    temp_filter_high.append(0.)
                    temp_filter_low.append(1.)

            # And make lists of dTemp_abs beloning to either high or low bands relative the threshhold temp
            dTemp_low_abs = [dt*tfl for dt,tfl in zip(abs_dTemp, temp_filter_low)]
            dTemp_high_abs = [dt*tfh for dt,tfh in zip(abs_dTemp, temp_filter_high)]

            fsize = (20, 20)
            plt.figure(figsize=fsize)
            plt.clf()

            #### Generate data
            x = [round(dtla,1) for dtla in dTemp_low_abs]
            y = [round(cc,2) for cc in clouds]

            # Calculate the point density
            xy = np.vstack([x,y])
            z = gaussian_kde(xy)(xy)

            plt.subplot(2,2,1).scatter(x, y, c=z, s=100, edgecolor='')
            plt.title("dtemp vs clouds (temp < {3}C) {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7], limit_temp))

            ##### Generate data
            x = [round(dtha, 1) for dtha in dTemp_high_abs]
            y = clouds

            # Calculate the point density
            xy = np.vstack([x,y])
            z = gaussian_kde(xy)(xy)

            plt.subplot(2,2,2).scatter(x, y, c=z, s=100, edgecolor='')
            plt.title("dtemp vs clouds (temp >= {3}C) {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7], limit_temp))


            plt.subplot(2,2,3).hist(z)


            fileName = "scatter dtemp vs clouds temp_limit{3} {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7], limit_temp)
            plt.savefig("{0}{1}".format(plot_folder, fileName))

    if method == 'dTemp_density':

        # Generate  data
        x = abs_dTemp
        y = clouds

        # Calculate the point density
        xy = np.vstack([x,y])
        z = gaussian_kde(xy)(xy)

        fig, ax = plt.subplots()
        ax.scatter(x, y, c=z, s=100, edgecolor='')

        plt.title("scatter dtemp_density clouds {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7]))
        fileName = "scatter dtemp_density {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7])

        plt.savefig("{0}{1}".format(plot_folder, fileName))

    if method == 'prec':

        fsize = (10, 10)
        plt.figure(figsize=fsize)
        plt.clf()

        plt.title("scatter prec clouds {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7]))
        fileName = "scatter prec clouds {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7])

       # Generate data
        x = prec
        y = clouds

        # Calculate the point density
        xy = np.vstack([x,y])
        z = gaussian_kde(xy)(xy)

        plt.scatter(x, y, c=z, s=100, edgecolor='')

        # calc the trendline
        z = numpy.polyfit(prec, clouds, 1)
        p = numpy.poly1d(z)
        plt.plot(prec, p(prec) )

        # the line equation:
        line_equation = "clouds = %.6f*prec + %.6f"%(z[0],z[1])
        plt.text(0.0, -0.1, 'Line equation: {0}'.format(line_equation))

        plt.savefig("{0}{1}".format(plot_folder, fileName))





    return
Ejemplo n.º 26
0
def testCloudMaker(stnr, startDate, endDate, method):
    """Gets data from a eKlima station and smooths the data depending on which method we wish to test.
    We find the nash-sutcliffe to observations of clouds. And we plot and save the result to Plots folder.

    :param stnr:        eKlima station where there is cloud cover for testing method.
    :param startDate:   simulations and reference data from this data
    :param endDate:     simulations and reference data to this data
    :param method:      specify which method to be tested
    :return:

    Available methods to test:
    cc_from_prec
    ccFromAveragePrec
    ccFromAverageObsCc
    ccGammaPrec
    ccGammaTemp
    ccGammaPrecAndTemp"""

    wsTemp = getMetData(stnr, 'TAM', startDate, endDate, 0, 'list')
    wsPrec = getMetData(stnr, 'RR', startDate, endDate, 0, 'list')
    wsCC = getMetData(stnr, 'NNM', startDate, endDate, 0, 'list')

    temp, date = strip_metadata(wsTemp, get_dates=True)
    prec = strip_metadata(wsPrec)
    clouds = strip_metadata(wsCC)

    dayShift = 1
    clouds = __shiftClouds(clouds, dayShift)


    if method == 'ccFromRandomThomas':
        estClouds = dpz.clouds_from_precipitation(prec, method='Random Thomas')
        gammaFigtext = 'No gamma smoothing and dayshift = {0}'.format(dayShift)
    elif method == 'ccFromPrec':
        estClouds = dpz.clouds_from_precipitation(prec, method='Binary')
        gammaFigtext = 'No gamma smoothing and dayshift = {0}'.format(dayShift)
    elif method == 'ccFromAveragePrec':
        estClouds = dpz.clouds_from_precipitation(prec, method='Average')
        gammaFigtext = 'No gamma smoothing and dayshift = {0}'.format(dayShift)
    elif method == 'ccFromPrecAndAveragePrec':
        estClouds = dpz.clouds_from_precipitation(prec, method='Binary and average')
        gammaFigtext = ''.format()
    elif method == 'ccFromAverageObsCc':
        estClouds = [sum(clouds)/float(len(clouds))] * len(clouds)
        gammaFigtext = ''.format()
    elif method == 'ccGammaPrec':
        gammaPrec = [2.8, 2., 0.3, 0.4]
        estClouds = dpz.clouds_from_precipitation(prec, method='Binary')
        estClouds = dgc.cc_gamma_smoothing(estClouds, gammaPrec)
        gammaFigtext = "gamma smoothing prec = {0} and dayshift = {1}".format(gammaPrec, dayShift)
    elif method == 'ccGammaTempChange':
        gammaTemp = [4.8, 1., 5., 0.03]
        estClouds = dgc.cc_gamma_temp(temp, gammaTemp)
        gammaFigtext = "gamma smoothing temp = {0} and dayshift = {1}".format(gammaTemp, dayShift)
    elif method == 'ccGammaPrecAndTempChange':
        gammaPrec = [2.8, 2., 0.3, 0.4]
        gammaTemp = [4.8, 1., 5., 0.03]
        estClouds = dgc.cc_gamma_prec_and_temp_change(prec, temp, gammaPrec, gammaTemp)
        gammaFigtext = "prec = {0} and temp = {1} and dayshift = {2}".format(gammaPrec, gammaTemp, dayShift)


    fileName = "{3} {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7], method)

    # What is the root mean square of estimatet vs observed clouds?
    # rms = np.sqrt(((np.array(estClouds) - np.array(clouds)) ** 2).mean())

    # Nash–Sutcliffe model efficiency coefficient from
    # https://en.wikipedia.org/wiki/Nash%E2%80%93Sutcliffe_model_efficiency_coefficient
    numerator = 0
    denominator = 0
    mean_clouds = sum(clouds)/float(len(clouds))
    for i in range(0, len(clouds), 1):
        numerator += (clouds[i] - estClouds[i])**2
        denominator += (clouds[i] - mean_clouds)**2
    nash_sutcliffe = 1 - numerator/denominator

    # Figure dimensions
    fsize = (16, 10)
    plt.figure(figsize=fsize)
    plt.clf()

    # plot total snowdepth on land
    plt.bar(date, prec, width=1, color="0.4")

    # plot the estimated cloud cover
    for i in range(0, len(estClouds) - 1, 1):
        if estClouds[i] > 0:
            plt.hlines(max(prec) * 1.2, date[i], date[i + 1], lw=45, color=str(-(estClouds[i] - 1.)))
        elif estClouds[i] == None:
            plt.hlines(max(prec) * 1.2, date[i], date[i + 1], lw=45, color="pink")
        else:
            plt.hlines(max(prec) * 1.2, date[i], date[i + 1], lw=45, color=str(-(estClouds[i] - 1.)))

    # plot cloud cover from met
    for i in range(0, len(clouds) - 1, 1):
        if clouds[i] > 0:
            plt.hlines(max(prec) * 1.1, date[i], date[i + 1], lw=45, color=str(-(clouds[i] - 1.)))
        elif clouds[i] == None:
            plt.hlines(max(prec) * 1.1, date[i], date[i + 1], lw=45, color="pink")
        else:
            plt.hlines(max(prec) * 1.1, date[i], date[i + 1], lw=45, color=str(-(clouds[i] - 1.)))

    # this plots temperature on separate right side axis
    plt.twinx()
    temp_pluss = []
    temp_minus = []
    for i in range(0, len(temp), 1):
        if temp[i] >= 0:
            temp_pluss.append(temp[i])
            temp_minus.append(np.nan)
        else:
            temp_minus.append(temp[i])
            temp_pluss.append(np.nan)
    plt.plot(date, temp, "black")
    plt.plot(date, temp_pluss, "red")
    plt.plot(date, temp_minus, "blue")

    # title and text fields
    plt.title("{3} {0} {1} {2}".format(stnr, startDate[0:7], endDate[0:7], method))
    plt.text(date[len(date)/2], min(temp)*1.2, 'gamma smoothing [shape, scale, days back, amplification]')
    plt.text(date[len(date)/2], min(temp)*1.3, gammaFigtext)

    # this is a scatter plot of modelled and estimated cloud cover
    xfrac = 0.15
    yfrac = (float(fsize[0])/float(fsize[1])) * xfrac
    xpos = 0.95-xfrac
    ypos = 0.42-yfrac
    a = plt.axes([xpos, ypos, xfrac, yfrac])
    a.scatter(clouds, estClouds)
    plt.setp(a, xticks=[0, 0.5, 1], yticks=[0, 0.5, 1])

    plt.text(0.0, 0.1, 'na_su = {0}'.format(round(nash_sutcliffe, 2)), color='yellow', bbox={'facecolor':'black'})

    plt.savefig("{0}{1}".format(plot_folder, fileName))

    return nash_sutcliffe