Ejemplo n.º 1
0
def plot_data(lon,
              lat,
              data,
              scalebar,
              title,
              lon_min=0,
              lon_max=359,
              lat_min=-90,
              lat_max=90,
              has_scale_bounds=False,
              scale_min=0,
              scale_max=0,
              figsize_x=20,
              figsize_y=15,
              colormap="coolwarm",
              has_rivers=True,
              file_name=None):
    data = np.asarray(data)

    #make the figure
    fig = plt.figure(figsize=(figsize_x, figsize_y))
    ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
    ax.coastlines()
    ax.add_feature(cartopy.feature.LAKES, alpha=1)
    if has_rivers: ax.add_feature(cartopy.feature.RIVERS, alpha=1)

    #The latitudes break because of how they're set up in the NOAA renalysis data unless you do this
    lat1 = em.find_closest_val(lat_min, lat)
    lat2 = em.find_closest_val(lat_max, lat)
    lon1 = em.find_closest_val(lon_min, lon)
    lon2 = em.find_closest_val(lon_max, lon)
    print(lat1)
    print(lat2)

    #label axes and the graph
    ax.set_extent((lon_min, lon_max, lat_min, lat_max))
    ax.set_xticks(lon[math.ceil(lon1 / 10) * 10:lon2][::10],
                  crs=ccrs.PlateCarree())
    ax.set_yticks(lat[math.ceil(lat1 / 10) * 10:lat2][::10],
                  crs=ccrs.PlateCarree())
    plt.xlabel('lon')
    plt.ylabel('lat')
    plt.title(title)

    #
    if not has_scale_bounds:
        mesh = plt.pcolormesh(lon[lon1:lon2],
                              lat[lat1:lat2],
                              data[lat1:lat2, lon1:lon2],
                              cmap=colormap)
    else:
        mesh = plt.pcolormesh(lon[lon1:lon2],
                              lat[lat1:lat2],
                              data[lat1:lat2, lon1:lon2],
                              cmap=colormap,
                              vmin=scale_min,
                              vmax=scale_max)
    cbar = plt.colorbar(mesh)
    cbar.set_label(scalebar)
    if file_name != None: plt.savefig(file_name)
Ejemplo n.º 2
0
def plot_vector_data(lon,
                     lat,
                     x_data,
                     y_data,
                     title,
                     scale_title,
                     lon_min=0,
                     lon_max=359,
                     lat_min=-90,
                     lat_max=90,
                     detail=3,
                     v_scale=400,
                     figsize_x=20,
                     figsize_y=15,
                     colormap="GnBu"):
    #make the figure
    fig = plt.figure(figsize=(figsize_x, figsize_y))
    ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
    ax.coastlines()
    ax.add_feature(cartopy.feature.LAKES, alpha=1)
    ax.add_feature(cartopy.feature.RIVERS, alpha=1)

    lat1 = em.find_closest_val(lat_min, lat)
    lat2 = em.find_closest_val(lat_max, lat)

    #set labels and axes up
    ax.set_extent((lon_min, lon_max, lat_min, lat_max))
    ax.set_xticks(lon[math.ceil(lon_min / 10) * 10:lon_max][::10],
                  crs=ccrs.PlateCarree())
    ax.set_yticks(lat[math.ceil(lat1 / 10) * 10:lat2][::10],
                  crs=ccrs.PlateCarree())
    plt.xlabel('lon')
    plt.ylabel('lat')
    plt.title(title)

    color = np.hypot(x_data[lat1:lat2, lon_min:lon_max][::detail, ::detail],
                     y_data[lat1:lat2, lon_min:lon_max][::detail, ::detail])
    v_field = plt.quiver(lon[lon_min:lon_max][::detail],
                         lat[lat1:lat2][::detail],
                         x_data[lat1:lat2,
                                lon_min:lon_max][::detail, ::detail],
                         y_data[lat1:lat2,
                                lon_min:lon_max][::detail, ::detail],
                         color,
                         cmap=colormap,
                         scale=v_scale)
    scalebar = plt.colorbar(v_field)
    scalebar.set_label(scale_title)
Ejemplo n.º 3
0
def average_plot(lat_start, lat_end, lon_start, lon_end, lat, lon, pres_levels,
                 lev_x, data_package, title_str):

    lat1 = em.find_closest_val(lat_start, lat)
    lat2 = em.find_closest_val(lat_end, lat)
    lon1 = em.find_closest_val(lon_start, lon)
    lon2 = em.find_closest_val(lon_end, lon)

    lev_x = em.find_closest_val(lev_x, pres_levels)

    fig = plt.figure(figsize=(12, 4))
    ax = plt.subplot(projection=ccrs.PlateCarree())

    ax.coastlines()

    ax.set_xticks(np.asarray(lon[::5]) - 180)
    ax.set_yticks(np.asarray(lat[::4]))
    ax.set_xlabel('lon')
    ax.set_ylabel('lat')
    mesh = plt.pcolormesh(lon[lon1:lon2],
                          lat[lat1:lat2],
                          data_package['hgt'][lev_x, lat1:lat2, lon1:lon2],
                          cmap='coolwarm')
    #plt.contour(lon[lon1:lon2], lat[lat1:lat2], data_package['hgt'][lev_x, lat1:lat2, lon1:lon2],
    #            cmap = 'coolwarm')

    cb = plt.colorbar(mesh)
    # ax.clabel(cont, inline=1, fontsize=5, colors = 'black')
    cb.set_label('GPH at ' + str(pres_levels[lev_x]) + 'mb level [m]')
    quiv = plt.quiver(lon[lon1:lon2], lat[lat1:lat2],
                      data_package['uwnd'][lev_x, lat1:lat2, lon1:lon2],
                      data_package['vwnd'][lev_x, lat1:lat2, lon1:lon2])
    ax.quiverkey(quiv,
                 X=0.3,
                 Y=-0.1,
                 U=10,
                 label='Quiver key, length = 10 m/s',
                 labelpos='E')
    ax.set_title(title_str)

    ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
    ax.add_feature(cartopy.feature.LAKES, alpha=0.5)
    ax.add_feature(cartopy.feature.RIVERS)

    return fig
Ejemplo n.º 4
0
def sst_anomaly_plot(lat_start, lat_end, lon_start, lon_end, lat, lon,
                     data_package, title_str, max_at_one):

    lat1 = em.find_closest_val(lat_start, lat)
    lat2 = em.find_closest_val(lat_end, lat)
    lon1 = em.find_closest_val(lon_start, lon)
    lon2 = em.find_closest_val(lon_end, lon)

    fig = plt.figure(figsize=(12, 4))
    ax = plt.subplot(projection=ccrs.PlateCarree())

    ax.coastlines()

    ax.add_feature(cartopy.feature.RIVERS)
    ax.add_feature(cartopy.feature.BORDERS, linestyle=':')

    ax.set_xticks(np.asarray(lon[::5]) - 180)
    ax.set_yticks(np.asarray(lat[::4]))
    ax.set_xlabel('lon')
    ax.set_ylabel('lat')
    if max_at_one:
        mesh = plt.pcolormesh(lon[lon1:lon2],
                              lat[lat1:lat2],
                              data_package['sst'][lat1:lat2, lon1:lon2],
                              cmap='coolwarm',
                              vmin=-1,
                              vmax=1)
    else:
        mesh = plt.pcolormesh(lon[lon1:lon2],
                              lat[lat1:lat2],
                              data_package['sst'][lat1:lat2, lon1:lon2],
                              cmap='coolwarm')
    cb = plt.colorbar(mesh)
    # ax.clabel(cont, inline=1, fontsize=5, colors = 'black')
    cb.set_label('stdev from mean')

    ax.set_title(title_str)

    return fig
Ejemplo n.º 5
0
def height_anomaly_plot(lat_start, lat_end, lon_start, lon_end, lat, lon,
                        pres_levels, lev_x, data_package, title_str):

    lat1 = em.find_closest_val(lat_start, lat)
    lat2 = em.find_closest_val(lat_end, lat)
    lon1 = em.find_closest_val(lon_start, lon)
    lon2 = em.find_closest_val(lon_end, lon)

    lev_x = em.find_closest_val(lev_x, pres_levels)

    fig = plt.figure(figsize=(12, 4))
    ax = plt.subplot(projection=ccrs.PlateCarree())

    ax.coastlines()

    ax.set_xticks(np.asarray(lon[::4]) - 180)
    ax.set_yticks(np.asarray(lat[::3]))
    ax.set_xlabel('lon')
    ax.set_ylabel('lat')
    mesh = plt.pcolormesh(lon[lon1:lon2],
                          lat[lat1:lat2],
                          data_package['hgt'][lev_x, lat1:lat2, lon1:lon2],
                          cmap='coolwarm',
                          vmin=-1,
                          vmax=1)
    #plt.contour(lon[lon1:lon2], lat[lat1:lat2], data_package['hgt'][lev_x, lat1:lat2, lon1:lon2],
    #            cmap = 'coolwarm')

    cb = plt.colorbar(mesh)
    # ax.clabel(cont, inline=1, fontsize=5, colors = 'black')
    cb.set_label('stdev from mean')

    ax.set_title(title_str)

    ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
    ax.add_feature(cartopy.feature.LAKES, alpha=0.5)
    ax.add_feature(cartopy.feature.RIVERS)

    return fig
Ejemplo n.º 6
0
def plot_pcasim_reg(tups, x_bin, y_bin, Y, lat, lon, path, title):
    c = np.empty((len(lat), len(lon)))  #lat/lon frame
    tuple_list = em.generate_tuples(x_bin, y_bin)  #list of binned tuples

    lat = lat.tolist()
    lon = lon.tolist()
    for ind in range(
            Y.shape[0]):  #iterate through every lat/lon pair, in Y-order
        x_val = x_bin[em.find_closest_val(
            Y[ind, 0], x_bin)]  #find the index of the closest val in x_bin
        y_val = y_bin[em.find_closest_val(Y[ind, 1], y_bin)]
        c_x = lat.index(tups[ind][0])
        c_y = lon.index(tups[ind][1])

        c[c_x, c_y] = tuple_list.index((x_val, y_val))

    fig = plt.figure()
    ax3 = plt.subplot(projection=ccrs.PlateCarree())
    plt.title(title)
    ax3.coastlines()
    mesh = plt.pcolormesh(lon, lat, c)
    plt.colorbar(mesh)
    plt.savefig(path)
    plt.close(fig)
Ejemplo n.º 7
0
        frames1 = []  #for air
        frames2 = []  #for pres
        frames3 = []  #for prate

        for d in dates:  #iterate through top 10 dates

            month = d.month
            year = d.year
            fname = str(i) + '_' + str(month) + '_' + str(
                year) + '.png'  #make file name: index_month_year.png
            path = os.path.join(
                folder, fname)  #make file path: location folder + file_name

            #find time index within NCEP-NCAR dataset - convert to days since 1/1/1800, subtract from time_greg
            time_index = em.find_closest_val(
                (d - dt.datetime(1800, 1, 1, 0, 0, 0)).days, time_greg)

            #find the mean of the x months before the date
            frame1 = np.mean(air_sub[time_index - num_months:time_index, :, :],
                             axis=0)
            frame2 = np.mean(pres_sub[time_index -
                                      num_months:time_index, :, :],
                             axis=0)
            frame3 = np.mean(prate_sub[time_index -
                                       num_months:time_index, :, :],
                             axis=0)

            #            if geospatial_subset == len(regions) - 1: #only generate these images for full-world
            #                vm.generate_3_var_img(frame1, frame2, frame3, time[time_index], path, lat_sub, lon_sub)

            #append to means for that location
import numpy as np
import os

start_time = module_time.time()

directory = r'C:\Users\bydd1\Documents\Research\Data\gridded environmental'
file = r'sst.mon.mean.nc'
image_folder = r'C:\Users\bydd1\Documents\Research\SST Images'

time, lat, lon, var = em.get_sst_mon_mean(directory, file, 'sst')

lat_bounds = [29.6, 18]
lon_bounds = [360 -98, 360 - 75.8]
#lat_bounds = [max(lat), min(lat)]
#lon_bounds = [min(lon), max(lon)]
lat_ind = [em.find_closest_val(lat_bounds[0], lat), em.find_closest_val(lat_bounds[1], lat)]
lon_ind = [em.find_closest_val(lon_bounds[0], lon), em.find_closest_val(lon_bounds[1], lon)]

mois, _ = generate_doi.get() #retrieve mois from generate_doi 

print("--- %s seconds ---" % (module_time.time() - start_time))

location_index = 2 #alphabetical, 0 for arkansas city, 1 for helena, 2 for vicksburg

moi = mois[location_index]

months_before = 6

time_ordinal = [dt.date.toordinal(x) for x in time]

for m in moi:
Ejemplo n.º 9
0
def plot_corr(dic, array, title, lat_start, lat_end, lon_start, lon_end, poi,
              flipped_axis):
    lat = dic['lat']
    lon = dic['lon']

    old_lon = lon
    if flipped_axis:
        lon = [180 - x for x in lon]
        temp = lon_start
        lon_start = lon_end
        lon_end = temp

        #print(lon)
        print([lon_start, lon_end])

    fig = plt.figure(figsize=(12, 5))
    fig.patch.set_facecolor('white')

    ax = plt.subplot(projection=ccrs.PlateCarree(central_longitude=0))
    lat1 = em.find_closest_val(lat_start, lat)
    lat2 = em.find_closest_val(lat_end, lat)
    lon1 = em.find_closest_val(lon_start, lon)
    lon2 = em.find_closest_val(lon_end, lon)

    print([lon1, lon2])
    ax.coastlines()
    plt.title(title)
    plt.xlabel('lon')
    plt.ylabel('lat')

    int_lon = int(len(old_lon[lon1:lon2]) / 10)
    int_lat = int(len(lat[lat1:lat2]) / 10)

    ax.set_xticks(np.round(np.asarray(old_lon[lon1:lon2][::int_lon])),
                  crs=ccrs.PlateCarree())
    ax.set_yticks(np.round(np.asarray(lat[lat1:lat2][::int_lat])),
                  crs=ccrs.PlateCarree())

    mesh = plt.pcolormesh(old_lon[lon1:lon2],
                          lat[lat1:lat2],
                          array[lat1:lat2, lon1:lon2],
                          cmap='coolwarm',
                          vmax=1,
                          vmin=-1)
    cbar = plt.colorbar(mesh)

    path1 = r'D:\Shapefiles\msrivs\msrivs.shp'
    shp1 = geopandas.read_file(path1)
    shp1.plot(ax=ax, edgecolor='gray', linewidth=1.3)

    path2 = r'D:\Shapefiles\Miss_RiverBasin\Miss_RiverBasin.shp'
    shp2 = geopandas.read_file(path2)
    shp2 = shp2.to_crs("EPSG:4326")
    shp2.plot(ax=ax, edgecolor='black', linewidth=1.3, facecolor='none')

    ax.scatter(poi[1],
               poi[0],
               color='yellow',
               s=30,
               transform=ccrs.PlateCarree())
    cbar.set_label('correlation coefficient to St. Louis')
    ax.add_feature(cartopy.feature.BORDERS, linestyle=':')

    savepath = os.path.join(
        r'C:\Users\bydd1\OneDrive\Documents\Research\miss-atmo correlation images',
        title + '.png')
    plt.savefig(savepath)
Ejemplo n.º 10
0
            corr[i, j] = np.corrcoef(comp, comp0)[0, 1]
    return corr


# #### run correlation on each dictionary

start_time = time.time()
dics = [pre_dic, air_dic, mois_dic]
name = ['precip', 'air', 'soilm']
c = []

for i in range(len(dics)):
    d = dics[i]

    poi = [
        em.find_closest_val(st_lou[0], d['lat']),
        em.find_closest_val(st_lou[1], d['lon'])
    ]

    #conversion
    if i > 0:
        if st_lou[1] < 0:
            poi = [
                em.find_closest_val(st_lou[0], d['lat']),
                em.find_closest_val(360 + st_lou[1], d['lon'])
            ]
        if st_lou[1] > 0:
            poi = [
                em.find_closest_val(st_lou[0], d['lat']),
                em.find_closest_val(st_lou[1], d['lon'])
            ]
location_index = 2

doi = dois[location_index]

lat_bounds = [48, 27]
lon_bounds = [360 - 100, 360 - 70]

for stop_date in doi:  #now iterate over the dates, generate an image for each date with mean/stdev for temp + gph

    if 1948 <= stop_date.year <= 2019:

        start_date = stop_date - dt.timedelta(days=7)
        air, gph, time, lat, lon = em.get_air_and_gph(start_date, stop_date)

        lat_ind = [
            em.find_closest_val(lat_bounds[0], lat),
            em.find_closest_val(lat_bounds[1], lat)
        ]
        lon_ind = [
            em.find_closest_val(lon_bounds[0], lon),
            em.find_closest_val(lon_bounds[1], lon)
        ]

        lat = lat[lat_ind[0]:lat_ind[1]]
        lon = lon[lon_ind[0]:lon_ind[1]]
        air = air[:, lat_ind[0]:lat_ind[1], lon_ind[0]:lon_ind[1]]
        gph = gph[:, lat_ind[0]:lat_ind[1], lon_ind[0]:lon_ind[1]]
        air_mean = np.nanmean(air, axis=0)
        air_stdev = np.std(air, axis=0)

        gph_mean = np.nanmean(gph, axis=0)