Exemplo n.º 1
0
    def __init__(self, station, data ):
        self.station = station
        self.data    = data
        self.ymd     = data[:,daydata.YYYYMMDD]
        self.date_s  = utils.f_to_s(self.ymd[ 0])  # First day data
        self.date_e  = utils.f_to_s(self.ymd[-1])  # Last day data
        self.period  = f'{self.date_s}-{self.date_e}'

        self.tg_gem = stats.average(data,'TG') # Avergae TG
        self.tx_max_sort = stats.sort( data, 'TX')
        self.tx_max      = self.tx_max_sort[0, daydata.ndx_ent('TX')] # Get max is 1st in list
        self.tg_max_sort = stats.sort( data, 'TG')
        self.tg_max      = self.tg_max_sort[0, daydata.ndx_ent('TG')]
        self.tn_max_sort = stats.sort( data, 'TN')
        self.tn_max      = self.tn_max_sort[0, daydata.ndx_ent('TN')]

        self.sq_tot = stats.sum(data,'SQ') # Total sunshine hours
        self.sq_sort = stats.sort(data, 'SQ')
        self.rh_tot = stats.sum(data,'RH') # Total rain
        self.rh_sort = stats.sort( data, 'RH')

        self.days_tx_gte_20 = stats.terms_days(data,'TX','≥',20) # Warm days
        self.days_tx_gte_25 = stats.terms_days(data,'TX','≥',25) # Summer days
        self.days_tx_gte_30 = stats.terms_days(data,'TX','≥',30) # Tropical days
        self.days_tx_gte_35 = stats.terms_days(data,'TX','≥',35) # Tropical days
        self.days_tx_gte_40 = stats.terms_days(data,'TX','≥',40) # Extreme Tropical days
        self.days_tg_gte_18 = stats.terms_days(data,'TG','≥',18) # Warmte getal dag
        self.days_tg_gte_20 = stats.terms_days(data,'TG','≥',20) # Warme gemiddelde
        self.days_tn_gte_20 = stats.terms_days(data,'TN','≥',20) # Tropical nights
        self.days_sq_gte_10 = stats.terms_days(data,'SQ','≥',10) # Sunny days > 10 hours
        self.days_rh_gte_10 = stats.terms_days(data,'RH','≥',10) # Rainy days > 10mm

        self.heat_ndx = stats.heat_ndx(data)
        self.days_heat_ndx = self.days_tg_gte_18
Exemplo n.º 2
0
def month_only( data, per, month ):
    '''Function select a day in  a year with a start and end date'''
    data = period( data, per )
    lymd = data[:, YYYYMMDD]    # Get the dates array
    iys = int(utils.f_to_s(lymd[ 0])[:4])  # Get start year
    iye = int(utils.f_to_s(lymd[-1])[:4])  # Get end year
    sel = np.array( [] )  # Keys array
    while iys <= iye:
        mmmm = f'{iys}{month}**' # Get the whole month
        k   = sel_keys_days( lymd, mmmm )[0]
        sel = np.concatenate( (sel, k) ) if np.size( sel ) != 0 else k
        iys += 1

    return data[sel]
Exemplo n.º 3
0
def day_only( data, per, day ):
    '''Function select a day in  a year with a start and end date'''
    data = period( data, per )
    lymd = data[:, YYYYMMDD]    # Get the dates array
    iys = int(utils.f_to_s(lymd[ 0])[:4])
    iye = int(utils.f_to_s(lymd[-1])[:4])
    sel = np.array( [] )
    while iys <= iye:
        symd = f'{iys}{day}'
        k   = sel_keys_days( lymd, symd )[0]
        sel = np.concatenate( (sel, k) ) if np.size( sel ) != 0 else k
        iys += 1

    return data[sel]
Exemplo n.º 4
0
def ents( day ):
    stn   = utils.f_to_s( day[daydata.STN] )
    ymd   = utils.f_to_s( day[daydata.YYYYMMDD] )
    ddvec = fix.ent(day[daydata.DDVEC], daydata.entities[daydata.DDVEC])
    fhvec = fix.ent(day[daydata.FHVEC], daydata.entities[daydata.FHVEC])
    fg    = fix.ent(day[daydata.FG],  daydata.entities[daydata.FG])
    fhx   = fix.ent(day[daydata.FHX], daydata.entities[daydata.FHX])
    fhxh  = fix.ent(day[daydata.FHXH], daydata.entities[daydata.FHXH])
    fhn   = fix.ent(day[daydata.FHN], daydata.entities[daydata.FHN])
    fhnh  = fix.ent(day[daydata.FHNH], daydata.entities[daydata.FHNH])
    fxx   = fix.ent(day[daydata.FXX], daydata.entities[daydata.FXX])
    fxxh  = fix.ent(day[daydata.FXXH], daydata.entities[daydata.FXXH])
    tg    = fix.ent(day[daydata.TG], daydata.entities[daydata.TG])
    tn    = fix.ent(day[daydata.TN], daydata.entities[daydata.TN])
    tnh   = fix.ent(day[daydata.TNH], daydata.entities[daydata.TNH])
    tx    = fix.ent(day[daydata.TX], daydata.entities[daydata.TX])
    txh   = fix.ent(day[daydata.TXH], daydata.entities[daydata.TXH])
    t10n  = fix.ent(day[daydata.T10N], daydata.entities[daydata.T10N])
    t10nh = fix.ent(day[daydata.T10NH], daydata.entities[daydata.T10NH])
    sq    = fix.ent(day[daydata.SQ], daydata.entities[daydata.SQ])
    sp    = fix.ent(day[daydata.SP], daydata.entities[daydata.SP])
    q     = fix.ent(day[daydata.Q], daydata.entities[daydata.Q])
    dr    = fix.ent(day[daydata.DR], daydata.entities[daydata.DR])
    rh    = fix.ent(day[daydata.RH], daydata.entities[daydata.RH])
    rhx   = fix.ent(day[daydata.RHX], daydata.entities[daydata.RHX])
    rhxh  = fix.ent(day[daydata.RHXH], daydata.entities[daydata.RHXH])
    pg    = fix.ent(day[daydata.PG], daydata.entities[daydata.PG])
    px    = fix.ent(day[daydata.PX], daydata.entities[daydata.PX])
    pxh   = fix.ent(day[daydata.PXH], daydata.entities[daydata.PXH])
    pn    = fix.ent(day[daydata.PN], daydata.entities[daydata.PN])
    pnh   = fix.ent(day[daydata.PNH], daydata.entities[daydata.PNH])
    vvn   = fix.ent(day[daydata.VVN], daydata.entities[daydata.VVN])
    vvnh  = fix.ent(day[daydata.VVNH], daydata.entities[daydata.VVNH])
    vvx   = fix.ent(day[daydata.VVX], daydata.entities[daydata.VVX])
    vvxh  = fix.ent(day[daydata.VVXH], daydata.entities[daydata.VVXH])
    ng    = fix.ent(day[daydata.NG], daydata.entities[daydata.NG])
    ug    = fix.ent(day[daydata.UG], daydata.entities[daydata.UG])
    ux    = fix.ent(day[daydata.UX], daydata.entities[daydata.UX])
    uxh   = fix.ent(day[daydata.UXH], daydata.entities[daydata.UXH])
    un    = fix.ent(day[daydata.UN], daydata.entities[daydata.UN])
    unh   = fix.ent(day[daydata.UNH], daydata.entities[daydata.UNH])
    ev24  = fix.ent(day[daydata.EV24], daydata.entities[daydata.EV24])

    return ( stn, ymd, ddvec, fhvec, fg, fhx,
             fhxh, fhn, fhnh, fxx, fxxh, tg,
             tn, tnh, tx, txh, t10n, t10nh,
             sq, sp, q, dr, rh, rhx,
             rhxh, pg, px, pxh, pn, pnh,
             vvn, vvnh, vvx, vvxh, ng, ug,
             ux, uxh, un, unh, ev24 )
Exemplo n.º 5
0
    def __init__(self, station, data, year):
        self.station = station
        self.data = data
        self.ymd = data[:, daydata.YYYYMMDD]
        self.year = year
        self.p_start = utils.f_to_s(self.ymd[0])  # First day data
        self.p_end = utils.f_to_s(self.ymd[-1])  # Last day data
        self.period = f'{self.p_start}-{self.p_end}'

        self.tg_max_sort = stats.sort(data, 'TG')
        self.tg_max = cfg.no_data_given if self.tg_max_sort.size == 0 else self.tg_max_sort[
            0, daydata.ndx_ent('TG')]
        self.tg_min = cfg.no_data_given if self.tg_max_sort.size == 0 else self.tg_max_sort[
            -1, daydata.ndx_ent('TG')]

        self.tx_max_sort = stats.sort(data, 'TX')
        self.tx_max = cfg.no_data_given if self.tx_max_sort.size == 0 else self.tx_max_sort[
            0, daydata.ndx_ent('TX')]
        self.tx_min = cfg.no_data_given if self.tx_max_sort.size == 0 else self.tx_max_sort[
            -1, daydata.ndx_ent('TX')]

        self.tn_max_sort = stats.sort(data, 'TN')
        self.tn_max = cfg.no_data_given if self.tn_max_sort.size == 0 else self.tn_max_sort[
            0, daydata.ndx_ent('TN')]
        self.tn_min = cfg.no_data_given if self.tn_max_sort.size == 0 else self.tn_max_sort[
            -1, daydata.ndx_ent('TN')]

        self.tn10_max_sort = stats.sort(data, 'T10N')
        self.tn10_max = cfg.no_data_given if self.tn10_max_sort.size == 0 else self.tn10_max_sort[
            0, daydata.ndx_ent('T10N')]
        self.tn10_min = cfg.no_data_given if self.tn10_max_sort.size == 0 else self.tn10_max_sort[
            -1, daydata.ndx_ent('T10N')]

        self.sq_max_sort = stats.sort(data, 'SQ')
        self.sq_max = cfg.no_data_given if self.sq_max_sort.size == 0 else self.sq_max_sort[
            0, daydata.ndx_ent('SQ')]

        self.rh_max_sort = stats.sort(data, 'RH')
        self.rh_max = cfg.no_data_given if self.rh_max_sort.size == 0 else self.rh_max_sort[
            0, daydata.ndx_ent('RH')]
Exemplo n.º 6
0
    def __init__(self, station, data):
        self.station = station
        self.data = data
        self.ymd = data[:, daydata.YYYYMMDD]
        self.date_s = utils.f_to_s(self.ymd[0])  # First day data
        self.date_e = utils.f_to_s(self.ymd[-1])  # Last day data
        self.period = f'{self.date_s}-{self.date_e}'

        # Average calculations
        self.tg_gem = stats.average(data, 'TG')

        # Min extremes
        self.tx_min_sort = stats.sort(data, 'TX', reverse=True)
        self.tx_min = self.tx_min_sort[0, daydata.ndx_ent('TX')]
        self.tg_min_sort = stats.sort(data, 'TG', reverse=True)
        self.tg_min = self.tg_min_sort[0, daydata.ndx_ent('TG')]
        self.tn_min_sort = stats.sort(data, 'TN', reverse=True)
        self.tn_min = self.tn_min_sort[0, daydata.ndx_ent('TN')]

        self.rh_sort = stats.sort(data, 'RH')
        self.rh_sum = stats.sum(data, 'RH')
        self.sq_sort = stats.sort(data, 'SQ')
        self.sq_sum = stats.sum(data, 'SQ')

        # Days lists
        self.days_tx_lt_0 = stats.terms_days(data, 'TX', '<', 0)
        self.days_tg_lt_0 = stats.terms_days(data, 'TG', '<', 0)
        self.days_tn_lt_0 = stats.terms_days(data, 'TN', '<', 0)
        self.days_tn_lt__5 = stats.terms_days(data, 'TN', '<', -5)
        self.days_tn_lt__10 = stats.terms_days(data, 'TN', '<', -10)
        self.days_tn_lt__15 = stats.terms_days(data, 'TN', '<', -15)
        self.days_tn_lt__20 = stats.terms_days(data, 'TN', '<', -20)

        self.days_hellmann = self.days_tg_lt_0
        self.sum_hellmann = stats.hellmann(data)
        self.frost_sum = stats.frost_sum(data)
        self.frost_sum_data = self.days_tn_lt_0  # All days with a T < 0
        self.ijnsen = stats.ijnsen(data)
Exemplo n.º 7
0
def plot( stations, ent_type_graphs, period, title, ylabel, fname, options ):

    path = utils.mk_path( cfg.dir_period_img, fname + f'.{cfg.plot_image_type}' )

    # Size values are inches. And figure always in front
    plt.figure( figsize=( convert.pixel_to_inch(options['plot_width']),
                          convert.pixel_to_inch(options['plot_height'])
                          ), dpi=options['plot_dpi'] )

    # Color handling
    rnd_col = True if len(stations) > 1 else False
    if rnd_col:
        col_list = utils.shuffle_list(vcol.save_colors, level=2)
        col_ndx, col_cnt = 0, len(col_list) - 1

    min, max = 999999.9, -9999999.9

    period_extremes, clima_means = list(), list()
    min_max_ave_sum_txt = False
    min_max_ave_sum_mean_txt = False
    for station in stations:
        # console.log(f'Calculate weatherdata for {station.place}', True)
        ok, data = daydata.read(station)
        if ok:
            days = daydata.period(data, period)
            ymd = days[:, daydata.ndx_ent('YYYYMMDD')].astype(
                            np.int, copy=False ).astype( np.str, copy=False
                            ).tolist() # Convert to list
            s_ymd, e_ymd = ymd[0], ymd[-1]
            sy, sm, sd  = s_ymd[0:4], s_ymd[4:6], s_ymd[6:8]
            ey, em, ed  = e_ymd[0:4], e_ymd[4:6], e_ymd[6:8]

            clima_mean_sub_txt = ''
            for opts in ent_type_graphs:
                ent         = opts[0].upper()
                graph_type  = opts[1]
                line_width  = opts[2]
                marker_size = opts[3]
                marker_text = opts[4]
                min_max_ave = opts[5]
                climate_ave = opts[6]
                climate_ave_marker_txt = opts[7]
                climate_periode = opts[8]
                cs_ymd, ce_ymd = climate_periode.split('-')
                scy, ecy = cs_ymd[0:4], ce_ymd[0:4]
                console.log(f'Process weatherdata {station.place} for {ent}', True)

                # Get the values needed for the graph
                f_val = days[:, daydata.ndx_ent(ent)]
                # console.log(f'{station.place} {ent} data values: {str(f_val)}')

                if utils.is_yes(min_max_ave):
                    min_max_ave_sum_txt = True
                    # Calculate extremes
                    min_per = stats.min(days, ent) # slow
                    max_per = stats.max(days, ent)
                    ave_per = stats.average(days, ent)
                    # Correct output
                    s_max = f'max {fix.ent(max_per, ent)}'
                    s_min = f'min {fix.ent(min_per, ent)}'
                    s_ave = f'mean {fix.ent(ave_per, ent)}'
                    s_ext = f'{ent} ({sy}{sm}{sd}-{ey}{em}{ed})'

                    if ent in ['SQ', 'RH', 'EV24', 'Q']:
                        sum_per = fix.ent(stats.sum(days, ent), ent)
                        s_ext += f'   {s_max}   sum {sum_per}   {s_ave}'
                    else:
                        s_ext += f'   {s_max}   {s_min}   {s_ave}'

                    period_extremes.append( s_ext )
                    console.log(s_ext)

                # Cumulative sum of values, if chosen
                if utils.is_yes(options['plot_cummul_val']):
                    f_val = np.cumsum( f_val )

                # Min/ max for ranges
                min_act = fix.rounding( np.min(f_val), ent )
                max_act = fix.rounding( np.max(f_val), ent )
                if min_act < min:
                    min = min_act
                if max_act > max:
                    max = max_act

                # Make correct output values
                l_val = [ fix.rounding(v, ent) for v in f_val.tolist() ]
                label = f'{station.place} {vt.ent_to_title(ent)}'
                color = col_list[col_ndx] if rnd_col else vcol.ent_to_color(ent)

                if utils.is_yes( climate_ave ):
                    min_max_ave_sum_mean_txt = True
                    label_clima = f'Day climate {station.place} {vt.ent_to_title(ent)}'
                    clima_ymd = days[:, daydata.YYYYMMDD ].tolist()

                    cli_txt = f'Calculate climate value {ent} for {station.place} (might take a while...)'
                    console.log(cli_txt, True)

                    l_clima = []
                    for d in clima_ymd:
                        ds =  utils.f_to_s(d)
                        mmdd = ds[4:8] # What day it is ?
                        sdat = datetime.strptime(ds, '%Y%m%d').strftime('%B %d').lower()
                        val  = stats.climate_average_for_day( station, mmdd, ent, climate_periode )
                        s_clima = fix.rounding(val, ent)
                        console.log(f'Climate value {ent} for {sdat} is {s_clima}')
                        # Append raw data without correct rounding
                        l_clima.append(val)

                    if utils.is_yes(min_max_ave):
                        # Clima average round correctly based on entity
                        if len(l_clima) > 0:
                            # Calculate average
                            sum = 0.0
                            for el in l_clima:
                                sum += el
                            ave = sum / len(l_clima)
                            s_ave = f'Climate mean ({scy}-{ecy}) {ent} is {fix.ent(ave, ent)}'
                            clima_mean_sub_txt = s_ave
                            console.log( s_ave )

                        else:
                            console.log('List with clima values is empthy.')

                    # Round correctly al climate values based on ent
                    for ndx, val in enumerate(l_clima):
                        l_clima[ndx] = fix.rounding( val, ent )

                    console.log(' ')

                if graph_type == 'line':
                    plt.plot(ymd, l_val,
                             label      = label,
                             color      = color,
                             marker     = cfg.plot_marker_type,
                             linestyle  = cfg.plot_line_style,
                             linewidth  = line_width,
                             markersize = marker_size,
                             alpha      = 0.6 )

                    if utils.is_yes(climate_ave):
                        plt.plot(ymd, l_clima,
                                 label = label_clima,
                                 color      = color,
                                 marker     = cfg.plot_clima_marker_type,
                                 linestyle  = cfg.plot_clima_line_style,
                                 linewidth  = line_width,
                                 markersize = marker_size,
                                 alpha      = 0.6 )

                elif graph_type == 'bar':
                    plt.bar( ymd, l_val, label = label, color = color, alpha = 0.5 )

                    if utils.is_yes(climate_ave):
                        plt.bar(ymd, l_clima, label=label_clima, color=color)

                # Marker texts
                diff = 0.2 if graph_type == 'line' else 0.1
                if utils.is_yes(marker_text):
                    # TODO No negative values for when graph is a bar
                    text = l_val

                    for d, v, t in zip( ymd, l_val, text ):
                        plt.text( d, v+diff, t,
                                  color=cfg.plot_marker_color,
                                  **cfg.plot_marker_font,
                                  horizontalalignment=cfg.plot_marker_horizontalalignment,
                                  alpha=cfg.plot_marker_alpha )

                if  utils.is_yes(climate_ave_marker_txt):
                    for d, v, t in zip( ymd, l_clima, l_clima ):
                        plt.text( d, v+diff, t,
                                  color=cfg.plot_marker_color,
                                  **cfg.plot_marker_font,
                                  horizontalalignment=cfg.plot_marker_horizontalalignment,
                                  alpha=cfg.plot_marker_alpha )

                if rnd_col:
                    col_ndx = 0 if col_ndx == col_cnt else col_ndx + 1

            clima_means.append(clima_mean_sub_txt)

        else:
            console.log('Read not oke in graphs.py -> plot')

    max = round(math.ceil(max), 1)
    min = round(math.floor(min), 1)
    # diff_val = max - min
    add_multi = (len(stations) * len(ent_type_graphs)) * 0.08
    add_space = 1.0 + add_multi # Get legend some space above
    max_tick  = math.ceil( max * add_space )  # upperrange extra
    min_tick  = min  #  % underrange extra unused

    # Update steps
    diff_tick = max_tick - min_tick
    step_calc = math.floor( diff_tick / 10 )
    step_end  = 1 if step_calc < 1 else step_calc  # min step = 1
    pos_txt   = max_tick - 0.3  # Under the edge
    yticks    = np.arange( min_tick, max_tick + step_end, step_end )  # 1-10 % ranges
    xticks    = ymd

    # print('max: '       + str(max))
    # print('min: '       + str(min))
    # print('diff_val: '  + str(diff_val))
    # print('add_multi: ' + str(add_multi))
    # print('!add_space: ' + str(add_space))
    # print('max_tick: '  + str(max_tick))
    # print('min_tick: '  + str(min_tick))
    # print('diff_tick: ' + str(diff_tick))
    # print('step_calc: ' + str(step_calc))
    # print('step_end: '  + str(step_end))
    # print('pos_txt: '   + str(pos_txt))
    # print('xticks: '    + str(xticks))
    # print('yticks: '    + str(yticks))
    # input('')

    if min_max_ave_sum_txt:
        # Add min.max/sum
        t = ''
        for el in period_extremes:
            t += el + '\n'
        # Add clima calculations
        if min_max_ave_sum_mean_txt:
            for el in clima_means:
                t += el + '\n'
        # Add text to plot
        plt.text( ymd[0], pos_txt, # Most left and at the top
                  t, **cfg.plot_add_txt_font, color='#555555',
                  horizontalalignment='left', verticalalignment='top' )

    plt.yticks( yticks, **cfg.plot_yas_font, color=cfg.plot_yas_color )
    plt.xticks( xticks, **cfg.plot_xas_font, color=cfg.plot_xas_color,
                rotation=cfg.plot_xas_rotation )
    plt.title( title, **cfg.plot_title_font, color=cfg.plot_title_color )
    plt.xlabel( cfg.plot_xlabel_text, **cfg.plot_xlabel_font, color=cfg.plot_xlabel_color )
    plt.ylabel( ylabel, **cfg.plot_ylabel_font, color=cfg.plot_ylabel_color )
    plt.legend( loc=cfg.plot_legend_loc,
                prop=cfg.plot_legend_font,
                facecolor=cfg.plot_legend_facecolor,
                shadow=cfg.plot_legend_shadow,
                frameon=cfg.plot_legend_frameon,
                fancybox=cfg.plot_legend_fancybox )

    if cfg.plot_grid_on:
        plt.grid( color=cfg.plot_grid_color,
                  linestyle=cfg.plot_grid_linestyle,
                  linewidth=cfg.plot_grid_linewidth )

    if utils.is_yes(cfg.plot_tight_layout):
        plt.tight_layout()

    plt.savefig( path, dpi=options['plot_dpi'], format=options['plot_image_type'] )

    if utils.is_yes(cfg.plot_show):
        plt.show()

    return path
Exemplo n.º 8
0
    def __init__(self, station, data):
        self.station = station
        self.data = data
        self.ymd = data[:, daydata.YYYYMMDD]
        self.p_start = utils.f_to_s(self.ymd[0])  # First day data
        self.p_end = utils.f_to_s(self.ymd[-1])  # Last day data
        self.period = f'{self.p_start}-{self.p_end}'
        self.tg_ave = stats.average(data, 'TG')
        self.tx_max_sort = stats.sort(data, 'TX')
        self.tx_max = self.tx_max_sort[0, daydata.ndx_ent('TX')]
        self.tg_max_sort = stats.sort(data, 'TG')
        self.tg_max = self.tg_max_sort[0, daydata.ndx_ent('TG')]

        self.tn_max_sort = stats.sort(data, 'TN')
        self.tn_max = self.tn_max_sort[0, daydata.ndx_ent('TN')]
        self.tx_min_sort = stats.sort(data, 'TX', reverse=True)
        self.tx_min = self.tx_min_sort[0, daydata.ndx_ent('TX')]
        self.tg_min_sort = stats.sort(data, 'TG', reverse=True)
        self.tg_min = self.tg_min_sort[0, daydata.ndx_ent('TG')]
        self.tn_min_sort = stats.sort(data, 'TN', reverse=True)
        self.tn_min = self.tn_min_sort[0, daydata.ndx_ent('TN')]

        self.sq_sum = stats.sum(data, 'SQ')  # Total sunshine hours
        self.sq_sort = stats.sort(data, 'SQ')
        self.rh_sum = stats.sum(data, 'RH')  # Total rain
        self.rh_sort = stats.sort(data, 'RH')

        # Days lists
        self.days_tx_lt_0 = stats.terms_days(data, 'TX', '<', 0)
        self.days_tg_lt_0 = stats.terms_days(data, 'TG', '<', 0)
        self.days_tn_lt_0 = stats.terms_days(data, 'TN', '<', 0)
        self.days_tn_lt__5 = stats.terms_days(data, 'TN', '<', -5)
        self.days_tn_lt__10 = stats.terms_days(data, 'TN', '<', -10)
        self.days_tn_lt__15 = stats.terms_days(data, 'TN', '<', -15)
        self.days_tn_lt__20 = stats.terms_days(data, 'TN', '<', -20)
        self.days_tx_gte_20 = stats.terms_days(data, 'TX', '≥',
                                               20)  # Warm days
        self.days_tx_gte_25 = stats.terms_days(data, 'TX', '≥',
                                               25)  # Summer days
        self.days_tx_gte_30 = stats.terms_days(data, 'TX', '≥',
                                               30)  # Tropical days
        self.days_tx_gte_35 = stats.terms_days(data, 'TX', '≥',
                                               35)  # Tropical days
        self.days_tx_gte_40 = stats.terms_days(data, 'TX', '≥',
                                               40)  # Tropical days
        self.days_tg_gte_18 = stats.terms_days(data, 'TG', '≥',
                                               18)  # Warmte getal dag
        self.days_tg_gte_20 = stats.terms_days(data, 'TG', '≥',
                                               20)  # Warme gemiddelde
        self.days_tn_gte_20 = stats.terms_days(data, 'TN', '≥',
                                               20)  # Tropical nights
        self.days_sq_gte_10 = stats.terms_days(data, 'SQ', '≥',
                                               10)  # Sunny days > 10 hours
        self.days_rh_gte_10 = stats.terms_days(data, 'RH', '≥',
                                               10)  # Rainy days > 10mm

        self.heat_ndx = stats.heat_ndx(data)
        self.days_heat_ndx = self.days_tg_gte_18
        self.hellmann = stats.hellmann(data)
        self.days_hellmann = self.days_tg_lt_0
        self.ijnsen = stats.ijnsen(data)
        self.frost_sum = stats.frost_sum(data)
        self.frost_sum_data = self.days_tn_lt_0  # All days with a T < 0
Exemplo n.º 9
0
def calculate(places, period, type, check=False, download=False):
    console.log(f'\nStart make dayvalues...\n', True)
    cnt_places = len(places)
    path = ''
    link = ''

    for place in places:
        console.log(f'Station: {place.wmo} {place.place}', True)

        if download:
            daydata.process_data( stations.from_wmo_to_station(place.wmo) )
            console.log(' ')

        d1 = daydata.read_station_period(place, period)[1]
        dates = d1[:, daydata.YYYYMMDD]
        cnt_dates = len(dates)

        # Base directory
        dir = utils.mk_path(config.dir_dayvalues, type)
        fio.mk_dir(dir)

        # Make path wmo
        w_dir = utils.mk_path(dir, place.wmo)

        for yyyymmdd in dates:
            ymd = utils.f_to_s(yyyymmdd)

            # Get year, month and day
            y, m, d = ymd[:4], ymd[4:6], ymd[6:8]

            y_dir = utils.mk_path(w_dir, y) # Make path year
            m_dir = utils.mk_path(y_dir, m) # Make path month

            # Make path
            path = utils.mk_path(m_dir, f'dayvalues-{place.wmo}-{y}-{m}-{d}.{type}')

            # Make link html only
            link = utils.mk_path(dir, f'dayvalues.html?wmo={place.wmo}&date={y}{m}{d}')

            # if fio.path = Path(fname)  # Python 3.4
            if check:
                if Path(path).exists():  # Check if there is a file
                    console.log(f'File {path} already found and skipped...', True)
                    continue

            console.log(f'A file for station {place.place} for the date {ymd} will be made...', True)

            # Check and make directories
            ok = os.path.isdir(m_dir)
            if not ok: # Check and make necessary maps
                ok = fio.mk_dir(w_dir)
                if ok: # WMO map exists
                    ok = fio.mk_dir(y_dir)
                    if ok: # Year map exists
                        ok = fio.mk_dir(m_dir)

            if ok: # Month map exists
                console.log(f'Map to write {m_dir} is ok!')
            else:
                console.log(f'Error! in map to write map {m_dir}')

            # Get correct day
            day = d1[np.where(dates == yyyymmdd)][0]

            path_to_root = './../../../../../'

            # Make output
            if type == 'html':
                header  = f'<i class="text-info fas fa-home"></i> '
                header += f'{place.wmo} - {place.place} '
                header += f'{place.province} - {utils.ymd_to_txt(ymd)} '

                page = vhtml.Template()
                page.title  = f'{place.wmo} {place.place} {ymd}'
                page.header = header
                page.strip  = True
                page.main   = vhtml.main_ent( day )
                page.footer = vhtml.footer_data_notification(place)
                page.file_path = path
                page.path_to_root = path_to_root
                page.template = utils.mk_path( config.dir_templates_html, 'dayvalues.html' )
                # Styling
                page.css_files = [ f'{path_to_root}dayvalues/css/default.css',
                                   f'{path_to_root}dayvalues/css/dayvalues.css' ]
                page.script_files = [
                                      f'{path_to_root}dayvalues/js/default.js',
                                      f'{path_to_root}static/js/default.js']
                ok = page.save()

            if ok:
                console.log(f'Successfull made {path}', True)
            else:
                console.log(f'Failed to make {path}', True)
            console.log(' ', True)

            # elif type == 'txt':
            #     title = txt_date
            #     main  = view_dayvalues.txt_main( day )
            #     txt   = f'{title}\n{main}'
            #     path  = utils.mk_path( station.dayvalues_dir_txt, fname )


    console.log(f'Link is {link}', True)
    return link
Exemplo n.º 10
0
def sel_keys_days ( ymd, per ):
    # Get the dates array
    y0  = utils.f_to_s(ymd[ 0])[:4]
    yz  = utils.f_to_s(ymd[-1])[:4]
    per = per.replace(' ','')  # No spaces
    now = datetime.now()  # Date now
    yn  = now.strftime('%Y')
    mn  = now.strftime('%m')
    dn  = now.strftime('%d')
    sel = np.array([])  # Array with selected keys
    keys = []  # Empthy keys array to fill with found keys

    # Make start and a end date
    sp, ep = '', ''
    if per.find('-') == -1: #  No '-' in period given
        yp = per[0:4]
        mp = per[4:6]
        dp = per[6:8]

        # OPTION YYYYMMDD   Get only one day in a year
        if per.isdigit():
            sel = np.where( (ymd == float(per)) )
            return sel

        # OPTION ********  All the data
        elif per == '*'*8: # All time
            sp = ymd[ 0]
            ep = ymd[-1]

        # OPTION ****      This actual whole year
        elif per == '****':
            sp = f'{yn}0101'
            ep = f'{yn}{mn}{dn}'

        # OPTION **        This actual whole month
        elif per == '**':
            sp = f'{yn}{mn}01'
            ep = f'{yn}{mn}{dn}'

        # OPTION YYYY****   Get whole year
        elif yp.isdigit() and f'{mp}{dp}' == '****':
            sp = f'{yp}0101'
            ep = f'{yp}1231'

        # OPTION YYYYMM**   Get whole month in year
        elif f'{yp}{mp}'.isdigit() and f'{dp}' == '**':
            dd = calendar.monthrange(int(yp), int(mp))[1]  # num_days
            # Is it the actual month in the actual year? Then actual day
            if mp == mn and yp == yn:
                dd = dn

            sp = f'{yp}{mp}01'
            ep = f'{yp}{mp}{dd}'

        # ADVANCED OPTIONS for more different periods in a given period
        # OPTION ****MM**   Get months for every year
        elif f'{yp}{dp}' == '******' and mp.isdigit():
            years = range(int(y0), int(yz)+1)
            for y in years:
                m_per = f'{y}{mp}**'
                k = sel_keys_days( ymd, m_per )[0]
                if k.size != 0:
                    keys = np.concatenate((keys, k))
            return sel_tuple(keys)

        # OPTION ****MMDD   Get days for every available year
        elif f'{yp}' == '****' and '{mp}{dp}'.isdigit():
            years = range(int(y0), int(yz)+1)
            for y in years:
                k = sel_keys_days(ymd, f'{y}{mp}{dp}')[0]
                if k.size != 0:
                    keys = np.concatenate((keys, k))

            return sel_tuple(keys)

    # Base period with a start and end date is given
    else:
        # Split in start and end-date
        s_per, e_per = per.split('-')

        # OPTION YYYYMMDD-YYYYMMDD  Get keys for the given periode
        if s_per.isdigit() and e_per.isdigit():
            sp = s_per
            ep = e_per
        else:
            # Split up in y , m , d
            ys, ye = s_per[0:4], e_per[0:4]
            ms, me = s_per[4:6], e_per[4:6]
            ds, de = s_per[6:8], e_per[6:8]

            # Correct years for the wildcard option: ****1225-****1225
            if ys == '****':
                ys = y0  # First possible year
            if ye == '****':
                ye = yz  # Last possible year

            # OPTION: YYYY****-YYYY****  A full year from startyear to endyear
            elif f'{ys}{ye}'.isdigit() and f'{ms}{ds}{me}{de}' == '*'*8:
                days = calendar.monthrange(int(ye), int(mn))[1]  # num_days
                mmdd = f'{mn}{days}' if ye == yn else '1231'
                sp = f'{ys}{ms}{ds}'
                ep = f'{ye}{mmdd}'

            # OPTION: YYYYMMDD-YYYYMM**  A full month + next months untill possible end
            elif s_per.isdigit() and f'{ye}{me}'.isdigit() and de == '**':
                days = calendar.monthrange(int(ye), int(me))[1]  # num_days
                dd  = dn if f'{ye}{me}' == f'{yn}{mn}' else days
                sp  = f'{ys}{ms}{ds}'
                ep  = f'{ye}{me}{dd}'

            # OPTION: 20100601-2020****  A full date untill a year or a end date -> now!
            elif s_per.isdigit() and ye.isdigit() and f'{me}{de}' == '*'*4:
                days = calendar.monthrange(int(ye), int(me))[1]  # num_days
                mmdd = f'{mn}{dn}' if ys == ye else f'{me}{days}'
                sp = f'{ys}{ms}{ds}'
                ep = f'{ye}{mmdd}'

            # More simple options later maybe

            # ADVANCED OPTIONS for more different periods in a given period
            # OPTION YYYYMMDD-YYYY*MMDD  A certain day in a year. From startyear to endyear.
            elif s_per.isdigit() and  len(e_per) == 9 and e_per[4] == '*' and \
                 ye.isdigit()    and  f'{ye}{e_per[5:9]}'.isdigit():
                md    = f'{ms}{ds}'
                years = range(int(ys), int(ye)+1)
                for y in years:
                    k = sel_keys_days(ymd, f'{y}{md}')[0]
                    sel = np.concatenate((sel, k)) if np.size(sel) != 0 else k
                # tup = sel_tuple( keys )
                return sel

            # OPTION YYYYMMDD-YYYY*MMDD*  A certain period in a year. From startyear to endyear
            elif s_per.isdigit() and len(e_per) == 10 and \
               e_per[4] == '*' and e_per[9] == '*'  and s_per[5:9].isdigit():

                s_md, e_md = f'{ms}{ds}', f'{e_per[5:9]}'
                years = range(int(ys), int(ye)+1)
                for y in years:
                    keys = sel_keys_days(ymd, f'{y}{s_md}-{y}{e_md}')
                    sel = np.concatenate((sel, keys)) if sel[0].size != 0 else keys

                return sel
            # TODO periods crossover years

            # OPTION YYYYMM**-YYYYMM** A full 1 month in an year. From startyear to endyear
            elif f'{ys}{ms}{ye}{me}'.isdigit() and f'{ds}{de}' == '****' and \
               f'{ys}{ms}' == f'{ye}{me}':
                years = range(int(ys),int(ye)+1)
                for y in years:
                    keys = sel_keys_days(ymd, f'{y}{ms}**')
                    if keys.size != 0:
                        sel = np.concatenate( (sel, keys) ) # Add keys to total list

                return sel

    # Search for wanted date keys
    # print('SP', sp, 'EP', ep)
    sp = float(sp)
    ep = float(ep)
    sel = np.where( (ymd >= sp) & (ymd <= ep) )
    return sel