Exemplo n.º 1
0
def onset_plot(outpath):

    list_of_files = glob.glob(outpath + '/*onset_dates.csv')
    model_names = [f.split('/')[-1].split('_')[3] for f in list_of_files]
    plt.clf()
    plt.figure(figsize=(16, 16))
    for file in list_of_files:
        holder = gent(file, delimiter=',')
        fi = list_of_files.index(file)
        date_file = glob.glob(outpath + '/*years.csv')
        times = gent(date_file[fi], delimiter=',')
        #times = np.arange(holder.shape[0])
        print len(times), len(holder)
        color = get_color(str(fi))
        line_style = get_line_style(str(fi))
        plt.scatter(times,
                    holder,
                    marker=line_style,
                    c=color,
                    label=model_names[fi])
    plt.ylim(145, 260)
    plt.xlim(times[0], times[-1] + 15)
    plt.yticks([150, 180, 210, 241], ['1 June', '1 July', '1 Aug', '1 Sep'])
    #        plt.xticks([0,10,20,30,40,50],['1950','1960','1970','1980','1990','2000'])
    plt.legend(loc='best')
    plt.savefig(outpath + '/SJ_onsets_all_models_scatter_hitorical.png')
    plt.show()
def workshop_contour_example_basic():

    # this is only working when the .csv file is in the same folder as your script
    #If your script can't find the file, check your path!
    mydata = gent(
        '/nfs/see-fs-01_teaching/earv052/metrics_workshop/example_code/TRMM_v7_Marteau_onsets_1998_dry_spell_30.csv',
        delimiter=',')

    print(mydata.shape[:])
    ltd = np.linspace(-20, 20, 160)
    lat = np.linspace(8.0, 16.0, 32)

    fig1 = plt.figure(figsize=(10, 7))  # open figure window

    plt.subplot(3, 1, 1)  # subplot 1
    ax1 = plt.contour(ltd, lat, mydata[:])

    plt.subplot(3, 1, 2)  # subplot 2
    ax2 = plt.contourf(ltd, lat, mydata[:])

    plt.subplot(3, 1, 3)  # subplot 3
    ax3 = plt.pcolor(ltd, lat, mydata[:], vmin=119, vmax=240)
    plt.show()
def workshop_contour_example_basemap():
    # Read the data, adapt this path to your path if necessary
    mydata = gent('TRMM_v7_Marteau_onsets_1998_dry_spell_30.csv',
                  delimiter=',')

    # Shape of the data
    print(mydata.shape)

    ltd = np.linspace(-20, 20, 160)
    lat = np.linspace(8.0, 16.0, 32)
    levs = [119, 129, 139, 149, 159, 169, 179, 189, 200, 210, 220, 230, 240]

    # Open figure window
    ax1 = plt.figure(figsize=(10, 15))

    # Add first subplot
    plt.subplot(3, 1, 1)

    fig1 = plt.contour(ltd, lat, mydata, levs)
    plt.clabel(fig1, inline=1)  #adds numbers to your contours
    m = bm.Basemap(projection='cyl',
                   llcrnrlat=8.0,
                   urcrnrlat=16.0,
                   llcrnrlon=-20.0,
                   urcrnrlon=20.0,
                   resolution='c')  # coarse resolution for grid
    m.drawcoastlines(linewidth=2)
    m.drawcountries(linewidth=1)
    plt.text(-21.5, 15, '(a)')

    # Add second subplot
    plt.subplot(3, 1, 2)
    fig2 = plt.contourf(ltd, lat, mydata[:], levs)
    m = bm.Basemap(projection='cyl',
                   llcrnrlat=8.0,
                   urcrnrlat=16.0,
                   llcrnrlon=-20.0,
                   urcrnrlon=20.0,
                   resolution='l')  # medium resolution for grid
    m.drawcoastlines(linewidth=2)
    m.drawcountries(linewidth=1)
    plt.text(-21.5, 15, '(b)')
    plt2_ax = ax1.gca()
    left0, bottom0, width0, height0 = plt2_ax.get_position().bounds
    cbar_1_ax = ax1.add_axes([left0, bottom0 - 0.01, width0, 0.015])
    cb2 = ax1.colorbar(fig2, cax=cbar_1_ax, orientation='horizontal')
    cb2.set_label('Marteau local onset 1998')

    # Add third subplot
    plt.subplot(3, 1, 3)
    my_pallet = cm.spectral
    my_pallet.set_over('w')
    my_pallet.set_under('w')
    my_pallet.set_bad(alpha=0.0)

    fig3 = plt.pcolor(ltd, lat, mydata[:], vmin=119, vmax=240, cmap=my_pallet)
    m = bm.Basemap(projection='cyl',
                   llcrnrlat=8.0,
                   urcrnrlat=16.0,
                   llcrnrlon=-20.0,
                   urcrnrlon=20.0,
                   resolution='f')  # fine resolution for grid
    m.drawcoastlines(linewidth=2)
    m.drawcountries(linewidth=1)
    plt.text(-21.5, 15, '(c)')
    plt3_ax = ax1.gca()
    left0, bottom0, width0, height0 = plt3_ax.get_position().bounds
    cbar_3_ax = ax1.add_axes([left0, bottom0 - 0.01, width0, 0.015])
    cb3 = ax1.colorbar(fig3, cax=cbar_3_ax, orientation='horizontal')
    cb3.set_label('Marteau local onset 1998')

    plt.subplots_adjust(hspace=0.3)
    # plt.savefig('matplot_contour_with_basemap.png') # save figure
    plt.show()
Exemplo n.º 4
0
def TRMM_CDD(yyyy, total):
    import numpy as np
    import matplotlib.pyplot as plt
    from numpy import genfromtxt as gent
    from netCDF4 import Dataset
    import mpl_toolkits.basemap as bm

    # first bring in the data
    holdr = gent('csvs/TRMM_v7_Lele_Lamb_onsets_' + str(yyyy) + '.csv',
                 delimiter=',')
    my_data = holdr[12:, 10:34]
    # now we also want trmm data
    nc = Dataset('C:/Users/js08rgjf/Canopy/scripts/csvs/v07/' + str(yyyy) +
                 '/3B42_daily.' + str(yyyy) + '.05.01.7.nc')
    trmm = nc.variables['r']
    left = trmm[:, 232:264, -80:]
    right = trmm[:, 232:264, :80]
    holdr = np.concatenate((left, right), axis=2)
    print holdr.shape

    # Ok now that we have the first of May, we do the rest of may

    dys = [
        '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12',
        '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24',
        '25', '26', '27', '28', '29', '30', '31'
    ]

    # First do May

    for t in range(1, 31):
        nc = Dataset('C:/Users/js08rgjf/Canopy/scripts/csvs/v07/' + str(yyyy) +
                     '/3B42_daily.' + str(yyyy) + '.05.' + str(dys[t]) +
                     '.7.nc')
        trmm = nc.variables['r']
        left = trmm[:, 232:264, -80:]
        right = trmm[:, 232:264, :80]
        holdr_temp = np.concatenate((left, right), axis=2)
        holdr = np.concatenate((holdr, holdr_temp), axis=0)
        print holdr.shape

# Now do June

    for t in range(0, 30):
        nc = Dataset('C:/Users/js08rgjf/Canopy/scripts/csvs/v07/' + str(yyyy) +
                     '/3B42_daily.' + str(yyyy) + '.06.' + str(dys[t]) +
                     '.7.nc')
        trmm = nc.variables['r']
        left = trmm[:, 232:264, -80:]
        right = trmm[:, 232:264, :80]
        holdr_temp = np.concatenate((left, right), axis=2)
        holdr = np.concatenate((holdr, holdr_temp), axis=0)
        print holdr.shape

# Now do July

    for t in range(0, 31):
        nc = Dataset('C:/Users/js08rgjf/Canopy/scripts/csvs/v07/' + str(yyyy) +
                     '/3B42_daily.' + str(yyyy) + '.07.' + str(dys[t]) +
                     '.7.nc')
        trmm = nc.variables['r']
        left = trmm[:, 232:264, -80:]
        right = trmm[:, 232:264, :80]
        holdr_temp = np.concatenate((left, right), axis=2)
        holdr = np.concatenate((holdr, holdr_temp), axis=0)
        print holdr.shape

# Finally August

    for t in range(0, 31):
        nc = Dataset('C:/Users/js08rgjf/Canopy/scripts/csvs/v07/' + str(yyyy) +
                     '/3B42_daily.' + str(yyyy) + '.08.' + str(dys[t]) +
                     '.7.nc')
        trmm = nc.variables['r']
        left = trmm[:, 232:264, -80:]
        right = trmm[:, 232:264, :80]
        holdr_temp = np.concatenate((left, right), axis=2)
        holdr = np.concatenate((holdr, holdr_temp), axis=0)
        print holdr.shape

# And September

    for t in range(0, 30):
        nc = Dataset('C:/Users/js08rgjf/Canopy/scripts/csvs/v07/' + str(yyyy) +
                     '/3B42_daily.' + str(yyyy) + '.09.' + str(dys[t]) +
                     '.7.nc')
        trmm = nc.variables['r']
        left = trmm[:, 232:264, -80:]
        right = trmm[:, 232:264, :80]
        holdr_temp = np.concatenate((left, right), axis=2)
        holdr = np.concatenate((holdr, holdr_temp), axis=0)
        print holdr.shape
    my_trmm = holdr[:, 12:, 10:34]
    dtes = []
    duration = []
    pltdtes = np.zeros((my_trmm.shape[1], my_trmm.shape[2]), dtype=int)
    pltdur = np.zeros((my_trmm.shape[1], my_trmm.shape[2]), dtype=int)
    for x in range(0, my_trmm.shape[2]):
        for y in range(0, my_trmm.shape[1]):
            current_max = 0
            current_dte = 0
            dte = 0
            duratn = 0
            if my_data[y, x] - 119 > my_trmm.shape[0] - 10 or my_data[
                    y, x] >= 250:
                continue
            else:
                for t in range(
                        int(my_data[y, x]) - 119, my_trmm.shape[0] - 10):
                    if my_trmm[t, y, x] <= float(total):
                        dte = t
                        print dte
                        for t1 in xrange(t, my_trmm.shape[0] - 10):
                            if my_trmm[t1, y, x] <= float(total):
                                continue
                            else:
                                duratn = t1 - t
                                if duratn > current_max:
                                    current_dte = dte + 119
                                    current_max = duratn
                            break
            dtes.extend([current_dte])
            duration.extend([current_max])
            pltdtes[y, x] = current_dte
            pltdur[y, x] = current_max

#                else:
#                  continue
#                break
    print np.max(duration[:])
    print np.max(dtes[:])
    #   plt.clf()
    #   plt.scatter(duration[:],dtes[:])
    #   plt.show()
    plt.clf()
    levs1 = [119, 129, 139, 150, 160, 170, 180, 190, 200, 211, 221, 231, 242]
    dates = [
        '1/5', '11/5', '21/5', '1/6', '11/6', '21/6', '1/7', '11/7', '21/7',
        '1/8', '11/8', '21/8', '1/9'
    ]
    levs2 = [2, 4, 6, 8, 10, 12, 15, 18, 20]
    ltd = np.linspace(-17.5, -11.5, 24)
    lati = np.linspace(11, 16, 20)
    ax1 = plt.figure(figsize=(12, 12))
    plt.subplot(2, 1, 1)
    m = bm.Basemap(projection='cyl',
                   llcrnrlat=11,
                   urcrnrlat=16,
                   llcrnrlon=-17.5,
                   urcrnrlon=-11.5,
                   resolution='c')
    fig2 = plt.contourf(ltd, lati, pltdtes[:], levs1, extend='max')
    m.drawcoastlines(linewidth=2)
    m.drawcountries(linewidth=1)
    plt2_ax = ax1.gca()
    left0, bottom0, width0, height0 = plt2_ax.get_position().bounds
    cbar_1_ax = ax1.add_axes([left0, bottom0 - 0.05, width0, 0.015])
    cb2 = ax1.colorbar(fig2, cax=cbar_1_ax, orientation='horizontal')
    cb2.set_label('Date of longest dry spell (after onset)')
    cb2.set_ticks(levs1)
    cb2.set_ticklabels(dates)
    plt.subplot(2, 1, 2)
    m = bm.Basemap(projection='cyl',
                   llcrnrlat=11,
                   urcrnrlat=16,
                   llcrnrlon=-17.5,
                   urcrnrlon=-11.5,
                   resolution='c')
    fig2 = plt.contourf(ltd, lati, pltdur[:], levs2, extend='max')
    m.drawcoastlines(linewidth=2)
    m.drawcountries(linewidth=1)
    plt2_ax = ax1.gca()
    left0, bottom0, width0, height0 = plt2_ax.get_position().bounds
    cbar_1_ax = ax1.add_axes([left0, bottom0 - 0.05, width0, 0.015])
    cb2 = ax1.colorbar(fig2, cax=cbar_1_ax, orientation='horizontal')
    cb2.set_label('Duration of first dry spell')
    plt.subplots_adjust(hspace=0.5)
    # plt.savefig('TRMM_CDD_'+str(yyyy)+'_limit'+str(total)+'mm_per_day.png')
    plt.show()
    np.savetxt('CDD_date_' + str(yyyy) + '.csv', pltdtes[:], delimiter=',')
    np.savetxt('CDD_duration_' + str(yyyy) + '.csv', pltdur[:], delimiter=',')