예제 #1
0
def plotDist(tables,
             variables,
             startDate,
             endDate,
             lat1,
             lat2,
             lon1,
             lon2,
             depth1,
             depth2,
             fname,
             exportDataFlag,
             marker='-',
             msize=30,
             clr='purple'):
    p = []
    lw = 2
    w = 800
    h = 400
    TOOLS = 'pan,wheel_zoom,zoom_in,zoom_out,box_zoom, undo,redo,reset,tap,save,box_select,poly_select,lasso_select'
    for i in tqdm(range(len(tables)), desc='overall'):
        y = subset.spaceTime(tables[i], variables[i], startDate, endDate, lat1,
                             lat2, lon1, lon2, depth1, depth2)
        if len(y) < 1:
            com.printTQDM(
                '%d: No matching entry found: Table: %s, Variable: %s ' %
                (i + 1, tables[i], variables[i]),
                err=True)
            continue
        com.printTQDM('%d: %s retrieved (%s).' %
                      (i + 1, variables[i], tables[i]),
                      err=False)
        y = y[variables[i]]
        if exportDataFlag:
            exportData(y, tables[i], variables[i], startDate, endDate, lat1,
                       lat2, lon1, lon2, depth1, depth2)
        try:
            y = y[~np.isnan(y)]  # remove nans
        except Exception as e:
            continue
        hist, edges = np.histogram(y, density=True, bins=50)
        p1 = figure(tools=TOOLS,
                    toolbar_location="above",
                    plot_width=w,
                    plot_height=h)
        p1.yaxis.axis_label = 'Density'
        p1.xaxis.axis_label = variables[i] + ' [' + db.getVar(
            tables[i], variables[i]).iloc[0]['Unit'] + ']'
        leg = variables[i]
        fill_alpha = 0.4
        cr = p1.quad(top=hist,
                     bottom=0,
                     left=edges[:-1],
                     right=edges[1:],
                     fill_color="dodgerblue",
                     line_color=None,
                     hover_fill_color="firebrick",
                     fill_alpha=fill_alpha,
                     hover_alpha=0.7,
                     hover_line_color="white",
                     legend=leg)
        p1.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='mouse'))
        p.append(p1)
    dirPath = 'embed/'
    if not os.path.exists(dirPath):
        os.makedirs(dirPath)
    if not inline:  ## if jupyter is not the caller
        output_file(dirPath + fname + ".html", title="Histogram")
    show(column(p))
    return
예제 #2
0
def plotMonthly(tables,
                variables,
                lat1,
                lat2,
                lon1,
                lon2,
                extV,
                extVV,
                extV2,
                extVV2,
                marker='-',
                msize=30,
                clr='purple'):
    p = []
    lw = 2
    w = 800
    h = 400
    months = range(1, 13)
    TOOLS = "hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,tap,save,box_select,poly_select,lasso_select,"
    TOOLS = 'pan,wheel_zoom,zoom_in,zoom_out,box_zoom, undo,redo,reset,tap,save,box_select,poly_select,lasso_select'
    for i in range(len(tables)):
        monthly = np.array([])
        monthly_std = np.array([])
        for mon in months:
            mon = int(mon)
            args = [
                tables[i], variables[i], mon, lat1, lat2, lon1, lon2, extV[i],
                extVV[i]
            ]
            query = 'EXEC uspMonthly ?, ?, ?, ?, ?, ?, ?, ?, ?'
            df = db.dbFetchStoredProc(query, args)
            df = pd.DataFrame.from_records(
                df, columns=['lat', 'lon', variables[i]])
            ############# removing outlier values ################
            vals = df[variables[i]]
            vals = vals[abs(vals) < 1e30]  # remove outliers
            ######################################################
            monthly = np.append(monthly, np.nanmean(vals))
            monthly_std = np.append(monthly_std, np.nanstd(vals))

        p1 = figure(tools=TOOLS,
                    toolbar_location="above",
                    plot_width=w,
                    plot_height=h)
        #p1.xaxis.axis_label = 'Month'
        p1.yaxis.axis_label = variables[i] + ' [' + db.getVar(
            tables[i], variables[i]).iloc[0]['Unit'] + ']'
        leg = variables[i]
        if extV[i] != None:
            leg = leg + '   ' + extV[i] + ': ' + ('%d' % float(extVV[i]))
            if tables[i].find('Pisces') != -1:
                leg = leg + ' ' + 'm'

        fill_alpha = 0.07
        if tables[i].find('Pisces') != -1:
            fill_alpha = 0.3
        cr = p1.circle(months,
                       monthly,
                       fill_color="grey",
                       hover_fill_color="firebrick",
                       fill_alpha=fill_alpha,
                       hover_alpha=0.3,
                       line_color=None,
                       hover_line_color="white",
                       legend=leg,
                       size=msize)
        p1.line(months, monthly, line_color=clr, line_width=lw, legend=leg)
        p1.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='hline'))
        #p1.xaxis.major_label_orientation = pi/4
        #p1.xaxis.visible = False
        p.append(p1)

    output_file("embed/" + fname + ".html", title="Monthly Trend")
    show(column(p))
    '''
    p1_script, p1_div = components(p1)
    embedComponents('embed/scriptMon1.js', p1_script)
    embedComponents('embed/divMon1.js', p1_div)

    p2_script, p2_div = components(p2)
    embedComponents('embed/scriptMon2.js', p1_script)
    embedComponents('embed/divMon2.js', p1_div)

    p3_script, p3_div = components(p3)
    embedComponents('embed/scriptMon3.js', p1_script)
    embedComponents('embed/divMon3.js', p1_div)
    '''
    return
예제 #3
0
def colocalize(ftleTable,
               ftleField,
               ftleValue,
               tables,
               variables,
               startDate,
               endDate,
               lat1,
               lat2,
               lon1,
               lon2,
               depth1,
               depth2,
               spMargin,
               exportDataFlag,
               fname,
               bkgComparison,
               marker='-'):
    fmt = '%Y-%m-%d'
    dt = 24 * 60
    msize = 10
    p = []
    lw = 2
    w = 800
    h = 400
    TOOLS = 'pan,wheel_zoom,zoom_in,zoom_out,box_zoom, undo,redo,reset,tap,save,box_select,poly_select,lasso_select'
    for i in tqdm(range(len(tables))):
        df = match(ftleTable, tables[i], startDate, endDate, lat1, lat2, lon1,
                   lon2, depth1, depth2, ftleField, ftleValue, variables[i],
                   spMargin, '0')
        if len(df) < 1:
            continue

        if i == 0:
            loadedFTLE = pd.DataFrame(df)
        ts, ys, y_stds = df[df.columns[0]], df[variables[i]], ''
        if i > 0:
            loadedFTLE = appendVar(loadedFTLE, ts, ys, y_stds, variables[i])
        #plot_single_hist(ys, clr='m', labelx='', labely='', leg='', yscale='linear', store_path='', bincount=50)

        ys = ys[~np.isnan(ys)]  # remove nans
        hist, edges = np.histogram(ys, density=False, bins=50)
        if bkgComparison:
            ########## get backgrounf distribution (not matched with fronts)
            dfBkg = match(ftleTable, tables[i], startDate, endDate, lat1, lat2,
                          lon1, lon2, depth1, depth2, ftleField, ftleValue,
                          variables[i], spMargin, '1')
            ysBkg = dfBkg[variables[i]]
            ysBkg = ysBkg[~np.isnan(ysBkg)]  # remove nans
            histBkg, edgesBkg = np.histogram(ysBkg, density=False, bins=50)
            ################################################################
        p1 = figure(tools=TOOLS,
                    toolbar_location="above",
                    plot_width=w,
                    plot_height=h)
        p1.yaxis.axis_label = 'Density'
        p1.xaxis.axis_label = variables[i] + ' [' + db.getVar(
            tables[i], variables[i]).iloc[0]['Unit'] + ']'
        leg = variables[i]
        fill_alpha = 0.4
        cr = p1.quad(top=hist,
                     bottom=0,
                     left=edges[:-1],
                     right=edges[1:],
                     fill_color="dodgerblue",
                     line_color=None,
                     hover_fill_color="firebrick",
                     fill_alpha=fill_alpha,
                     hover_alpha=0.7,
                     hover_line_color="white",
                     legend=leg + ' on fronts')
        if bkgComparison:
            cr = p1.quad(top=histBkg,
                         bottom=0,
                         left=edgesBkg[:-1],
                         right=edgesBkg[1:],
                         fill_color="purple",
                         line_color=None,
                         hover_fill_color="firebrick",
                         fill_alpha=fill_alpha,
                         hover_alpha=0.7,
                         hover_line_color="white",
                         legend='background')
        p1.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='mouse'))
        p.append(p1)

    dirPath = 'embed/'
    if not os.path.exists(dirPath):
        os.makedirs(dirPath)
    if not inline:  ## if jupyter is not the caller
        output_file(dirPath + fname + ".html", title="Front")
    show(column(p))
    if exportDataFlag:
        exportData(loadedFTLE, ts, ys, y_stds, tables[i], variables[i],
                   spMargin)
    print('')
    return df
예제 #4
0
def plotAlongTrack(dt,
                   fmt,
                   tables,
                   variables,
                   track,
                   spMargin,
                   depth1,
                   depth2,
                   exportDataFlag,
                   fname,
                   marker='-',
                   msize=30,
                   clr='purple'):
    p = []
    #fmt = '%Y-%m-%d %H:%M:%S'
    dt = dt / 60  # time resolution (minutes)
    loadedTrack = pd.DataFrame(track)
    lw = 2
    w = 800
    h = 400
    TOOLS = 'pan,wheel_zoom,zoom_in,zoom_out,box_zoom, undo,redo,reset,tap,save,box_select,poly_select,lasso_select'
    for i in tqdm(range(len(tables)), desc='overall'):
        ts, ys, y_stds = [], np.array([]), np.array([])
        for j in tqdm(range(len(track)), desc=variables[i]):
            startDate = track.iloc[j]['time'].strftime(fmt)
            endDate = startDate
            lat1 = float(track.iloc[j]['lat']) - spMargin
            lat2 = float(track.iloc[j]['lat']) + spMargin
            lon1 = float(track.iloc[j]['lon']) - spMargin
            lon2 = float(track.iloc[j]['lon']) + spMargin
            t, y, y_std = TS.timeSeries(tables[i],
                                        variables[i],
                                        startDate,
                                        endDate,
                                        lat1,
                                        lat2,
                                        lon1,
                                        lon2,
                                        depth1,
                                        depth2,
                                        fmt=fmt,
                                        dt=dt)
            ts.append(track.iloc[j]['time'])
            ys = np.append(ys, y[0])
            y_stds = np.append(y_stds, y_std[0])
        loadedTrack = appendVar(loadedTrack, ts, ys, y_stds, variables[i])
        p1 = figure(tools=TOOLS,
                    toolbar_location="above",
                    plot_width=w,
                    plot_height=h)
        #p1.xaxis.axis_label = 'Time'
        p1.yaxis.axis_label = variables[i] + ' [' + db.getVar(
            tables[i], variables[i]).iloc[0]['Unit'] + ']'
        leg = 'Along Tracer Track ' + variables[i]
        fill_alpha = 0.3
        cr = p1.circle(ts,
                       ys,
                       fill_color="grey",
                       hover_fill_color="firebrick",
                       fill_alpha=fill_alpha,
                       hover_alpha=0.3,
                       line_color=None,
                       hover_line_color="white",
                       legend=leg,
                       size=msize)
        p1.line(ts, ys, line_color=clr, line_width=lw, legend=leg)
        p1.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='hline'))
        p1.xaxis.formatter = DatetimeTickFormatter(
            hours=["%d %B %Y"],
            days=["%d %B %Y"],
            months=["%d %B %Y"],
            years=["%d %B %Y"],
        )
        p1.xaxis.major_label_orientation = pi / 4
        p.append(p1)

    dirPath = 'embed/'
    if not os.path.exists(dirPath):
        os.makedirs(dirPath)
    if not inline:  ## if jupyter is not the caller
        output_file(dirPath + fname + ".html", title="Lagrangian")
    show(column(p))
    if exportDataFlag:
        exportData(loadedTrack, ts, ys, y_stds, tables[i], variables[i],
                   spMargin)
    print('')
    return
예제 #5
0
def plotXY(tables,
           variables,
           startDate,
           endDate,
           lat1,
           lat2,
           lon1,
           lon2,
           depth1,
           depth2,
           fname,
           exportDataFlag,
           marker='-',
           msize=15,
           clr='green'):
    p = []
    lw = 2
    w = 500
    h = 500
    TOOLS = 'pan,wheel_zoom,zoom_in,zoom_out,box_zoom, undo,redo,reset,tap,save,box_select,poly_select,lasso_select'
    tablePairs = list(itt.combinations(tables, 2))
    variablePairs = list(itt.combinations(variables, 2))
    for i in range(len(tablePairs)):
        t1, y1, y_std1 = TS.timeSeries(tablePairs[i][0], variablePairs[i][0],
                                       startDate, endDate, lat1, lat2, lon1,
                                       lon2, depth1, depth2)
        t2, y2, y_std2 = TS.timeSeries(tablePairs[i][1], variablePairs[i][1],
                                       startDate, endDate, lat1, lat2, lon1,
                                       lon2, depth1, depth2)
        if exportDataFlag:
            exportData(t1, y1, y_std1, t2, y2, y_std2, tablePairs[i][0],
                       variablePairs[i][0], tablePairs[i][1],
                       variablePairs[i][1], lat1, lat2, lon1, lon2, depth1,
                       depth2)

        if len(t1) < 1 or len(y1) < 1 or len(t2) < 1 or len(y2) < 1:
            continue
        if (len(t1) - len(t2) != 0) or (len(y1) - len(y2) != 0):
            continue
        p1 = figure(tools=TOOLS,
                    toolbar_location="above",
                    plot_width=w,
                    plot_height=h)
        p1.xaxis.axis_label = variablePairs[i][0] + ' [' + db.getVar(
            tablePairs[i][0], variablePairs[i][0]).iloc[0]['Unit'] + ']'
        p1.yaxis.axis_label = variablePairs[i][1] + ' [' + db.getVar(
            tablePairs[i][1], variablePairs[i][1]).iloc[0]['Unit'] + ']'
        leg = variablePairs[i][0] + ' / ' + variablePairs[i][1]
        fill_alpha = 0.3
        cr = p1.circle(y1,
                       y2,
                       fill_color="grey",
                       hover_fill_color="firebrick",
                       fill_alpha=fill_alpha,
                       hover_alpha=0.6,
                       line_color=None,
                       hover_line_color="white",
                       legend=leg,
                       size=msize)
        #p1.line(y1, y2, line_color=clr, line_width=lw, legend=leg)
        p1.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='hline'))
        p.append(p1)
    dirPath = 'embed/'
    if not os.path.exists(dirPath):
        os.makedirs(dirPath)
    if not inline:  ## if jupyter is not the caller
        output_file(dirPath + fname + ".html", title="XY")
    show(column(p))
    return
예제 #6
0
def mutualTrends(loadedTrack,
                 tables,
                 variables,
                 extV,
                 extVV,
                 extV2,
                 extVV2,
                 msize=20,
                 fname='MutualTrends'):
    if len(variables) < 2:
        print('Error: at least 2 variables are needed to plot mutual trends.')
        return
    tablePairs = list(itt.combinations(tables, 2))
    variablePairs = list(itt.combinations(variables, 2))
    extVPairs = list(itt.combinations(extV, 2))
    extVVPairs = list(itt.combinations(extVV, 2))
    extV2Pairs = list(itt.combinations(extV2, 2))
    extVV2Pairs = list(itt.combinations(extVV2, 2))

    p = []
    lw = 2
    w = 500
    h = 500
    TOOLS = 'pan,wheel_zoom,zoom_in,zoom_out,box_zoom, undo,redo,reset,tap,save,box_select,poly_select,lasso_select'
    for i in range(len(tablePairs)):
        t1, y1, y_std1 = loadedTrack['time'], loadedTrack[
            variablePairs[i][0]], loadedTrack[variablePairs[i][0] + '_std']
        t2, y2, y_std2 = loadedTrack['time'], loadedTrack[
            variablePairs[i][1]], loadedTrack[variablePairs[i][1] + '_std']

        p1 = figure(tools=TOOLS,
                    toolbar_location="above",
                    plot_width=w,
                    plot_height=h)
        p1.xaxis.axis_label = variablePairs[i][0] + ' [' + db.getVar(
            tablePairs[i][0], variablePairs[i][0]).iloc[0]['Unit'] + ']'
        p1.yaxis.axis_label = variablePairs[i][1] + ' [' + db.getVar(
            tablePairs[i][1], variablePairs[i][1]).iloc[0]['Unit'] + ']'
        leg = variablePairs[i][0] + ' / ' + variablePairs[i][1]
        if extVPairs[i][0] != None:
            leg = leg + '   ' + extVPairs[i][0] + ': ' + (
                '%d' % float(extVVPairs[i][0]))
            if tablePairs[i][0].find('Pisces') != -1:
                leg = leg + ' ' + 'm'
        if extVPairs[i][1] != None:
            leg = leg + '   ' + extVPairs[i][1] + ': ' + (
                '%d' % float(extVVPairs[i][1]))
            if tablePairs[i][1].find('Pisces') != -1:
                leg = leg + ' ' + 'm'
        fill_alpha = 0.6
        #if tablePairs[i][0].find('Pisces') != -1 or tablePairs[i][1].find('Pisces') != -1:
        #    fill_alpha = 0.3
        cr = p1.circle(y1,
                       y2,
                       fill_color="grey",
                       hover_fill_color="firebrick",
                       fill_alpha=fill_alpha,
                       hover_alpha=0.6,
                       line_color=None,
                       hover_line_color="white",
                       legend=leg,
                       size=msize)
        #p1.line(y1, y2, line_color=clr, line_width=lw, legend=leg)
        p1.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='hline'))
        p.append(p1)
    dirPath = 'embed/'
    if not os.path.exists(dirPath):
        os.makedirs(dirPath)
    output_file(dirPath + fname + ".html", title="Mutual Trends")
    show(column(p))
    return
예제 #7
0
def plotAlongTrack(tables,
                   variables,
                   cruiseName,
                   track,
                   spMargin,
                   extV,
                   extVV,
                   extV2,
                   extVV2,
                   exportDataFlag,
                   marker='-',
                   msize=30,
                   clr='purple'):
    p = []
    fmt = '%Y-%m-%d %H:%M:%S'
    dt = resampleToTimeResolution(resampTau)  # time resolution (minutes)
    loadedTrack = pd.DataFrame(track)
    lw = 2
    w = 800
    h = 400
    TOOLS = 'pan,wheel_zoom,zoom_in,zoom_out,box_zoom, undo,redo,reset,tap,save,box_select,poly_select,lasso_select'
    for i in range(len(tables)):
        ts, ys, y_stds = [], np.array([]), np.array([])
        for j in range(len(track)):
            startDate = track.iloc[j]['time'].strftime(fmt)
            endDate = startDate
            lat1 = float(track.iloc[j]['lat']) - spMargin
            lat2 = float(track.iloc[j]['lat']) + spMargin
            lon1 = float(track.iloc[j]['lon']) - spMargin
            lon2 = float(track.iloc[j]['lon']) + spMargin
            t, y, y_std = TS.timeSeries(tables[i],
                                        variables[i],
                                        startDate,
                                        endDate,
                                        lat1,
                                        lat2,
                                        lon1,
                                        lon2,
                                        extV[i],
                                        extVV[i],
                                        extV2[i],
                                        extVV2[i],
                                        fmt=fmt,
                                        dt=dt)
            ts.append(track.iloc[j]['time'])
            ys = np.append(ys, y[0])
            y_stds = np.append(y_stds, y_std[0])
        loadedTrack = appendVar(loadedTrack, ts, ys, y_stds, variables[i],
                                extV[i], extVV[i], extV2[i], extVV2[i])
        p1 = figure(tools=TOOLS,
                    toolbar_location="above",
                    plot_width=w,
                    plot_height=h)
        #p1.xaxis.axis_label = 'Time'
        p1.yaxis.axis_label = variables[i] + ' [' + db.getVar(
            tables[i], variables[i]).iloc[0]['Unit'] + ']'
        leg = 'Along Track (' + cruiseName + ') ' + variables[i]
        if extV[i] != None:
            leg = leg + '   ' + extV[i] + ': ' + ('%d' % float(extVV[i]))
            if tables[i].find('Pisces') != -1:
                leg = leg + ' ' + 'm'
        fill_alpha = 0.07
        if tables[i].find('Pisces') != -1:
            fill_alpha = 0.3
        cr = p1.circle(ts,
                       ys,
                       fill_color="grey",
                       hover_fill_color="firebrick",
                       fill_alpha=fill_alpha,
                       hover_alpha=0.3,
                       line_color=None,
                       hover_line_color="white",
                       legend=leg,
                       size=msize)
        p1.line(ts, ys, line_color=clr, line_width=lw, legend=leg)
        p1.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='hline'))
        p1.xaxis.formatter = DatetimeTickFormatter(
            hours=["%d %B %Y %H:%M:%S"],
            days=["%d %B %Y %H:%M:%S"],
            months=["%d %B %Y %H:%M:%S"],
            years=["%d %B %Y %H:%M:%S"],
        )
        p1.xaxis.major_label_orientation = pi / 4
        p.append(p1)

    dirPath = 'embed/'
    if not os.path.exists(dirPath):
        os.makedirs(dirPath)
    output_file(dirPath + fname + ".html", title="Along Track")
    show(column(p))
    if exportDataFlag:
        exportData(loadedTrack, ts, ys, y_stds, cruiseName, tables[i],
                   variables[i], spMargin, extV[i], extVV[i], extV2[i],
                   extVV2[i])
    return loadedTrack
예제 #8
0
def getUnit(table, variable):
    return ' [' + db.getVar(table, variable).iloc[0]['Unit'] + ']'