예제 #1
0
def plot_bar4date_rms(inps):
    inps.figName = os.path.splitext(inps.rmsFile)[0] + '.pdf'
    if ut.update_file(inps.figName,
                      [inps.exDateFile, inps.refDateFile, inps.template_file],
                      check_readable=False):
        if inps.fig_size:
            fig = plt.figure(figsize=inps.fig_size)
        else:
            fig = plt.figure()
        ax = fig.add_subplot(111)
        font_size = 12

        dates, datevector = ptime.date_list2vector(inps.dateList)
        try:
            bar_width = ut.most_common(np.diff(dates).tolist()) * 3 / 4
        except:
            bar_width = np.min(np.diff(dates).tolist()) * 3 / 4
        x_list = [i - bar_width / 2 for i in dates]

        inps.rmsList = [i * 1000. for i in inps.rmsList]
        min_rms = inps.min_rms * 1000.
        # Plot all dates
        ax.bar(x_list, inps.rmsList, bar_width.days)

        # Plot reference date
        ax.bar(x_list[inps.refDateIndex],
               inps.rmsList[inps.refDateIndex],
               bar_width.days,
               label='Reference date')

        # Plot exclude dates
        if inps.exIdxList:
            ex_x_list = [x_list[i] for i in inps.exIdxList]
            inps.exRmsList = [inps.rmsList[i] for i in inps.exIdxList]
            ax.bar(ex_x_list,
                   inps.exRmsList,
                   bar_width.days,
                   color='darkgray',
                   label='Exclude date(s)')

        # Plot min_rms line
        ax, xmin, xmax = pp.auto_adjust_xaxis_date(
            ax, datevector, font_size, every_year=inps.tick_year_num)
        ax.plot(np.array([xmin, xmax]), np.array([min_rms, min_rms]), '--k')

        # axis format
        ax = pp.auto_adjust_yaxis(ax,
                                  inps.rmsList + [min_rms],
                                  font_size,
                                  ymin=0.0)
        ax.set_xlabel('Time [years]', fontsize=font_size)
        ax.set_ylabel('Root Mean Square [mm]', fontsize=font_size)
        ax.yaxis.set_ticks_position('both')
        ax.tick_params(labelsize=font_size)
        plt.legend(fontsize=font_size)

        # save figure
        fig.savefig(inps.figName, bbox_inches='tight', transparent=True)
        print('save figure to file: ' + inps.figName)
    return inps
예제 #2
0
def check_exist_grib_file(gfile_list, print_msg=True):
    """Check input list of grib files, and return the existing ones with right size."""
    gfile_exist = ut.get_file_list(gfile_list)
    if gfile_exist:
        file_sizes = [
            os.path.getsize(i) for i in gfile_exist
            if os.path.getsize(i) > 10e6
        ]
        if file_sizes:
            comm_size = ut.most_common([i for i in file_sizes])
            if print_msg:
                print('common file size: {} bytes'.format(comm_size))
                print('number of grib files existed    : {}'.format(
                    len(gfile_exist)))

            gfile_corrupt = []
            for gfile in gfile_exist:
                if os.path.getsize(gfile) < comm_size * 0.9:
                    gfile_corrupt.append(gfile)
        else:
            gfile_corrupt = gfile_exist

        if gfile_corrupt:
            if print_msg:
                print(
                    '------------------------------------------------------------------------------'
                )
                print(
                    'corrupted grib files detected! Delete them and re-download...'
                )
                print('number of grib files corrupted  : {}'.format(
                    len(gfile_corrupt)))
            for i in gfile_corrupt:
                rmCmd = 'rm ' + i
                print(rmCmd)
                os.system(rmCmd)
                gfile_exist.remove(i)
            if print_msg:
                print(
                    '------------------------------------------------------------------------------'
                )
    return gfile_exist
예제 #3
0
파일: plot.py 프로젝트: fossabot/PySAR
def plot_coherence_history(ax, date12List, cohList, plot_dict={}):
    """Plot min/max Coherence of all interferograms for each date"""
    # Figure Setting
    if not 'fontsize'    in plot_dict.keys():   plot_dict['fontsize']    = 12
    if not 'linewidth'   in plot_dict.keys():   plot_dict['linewidth']   = 2
    if not 'markercolor' in plot_dict.keys():   plot_dict['markercolor'] = 'orange'
    if not 'markersize'  in plot_dict.keys():   plot_dict['markersize']  = 16
    if not 'disp_title'  in plot_dict.keys():   plot_dict['disp_title']  = True
    if not 'every_year'  in plot_dict.keys():   plot_dict['every_year']  = 1

    # Get date list
    date12List = ptime.yyyymmdd_date12(date12List)
    m_dates = [date12.split('_')[0] for date12 in date12List]
    s_dates = [date12.split('_')[1] for date12 in date12List]
    dateList = sorted(ptime.yyyymmdd(list(set(m_dates + s_dates))))

    dates, datevector = ptime.date_list2vector(dateList)
    bar_width = ut.most_common(np.diff(dates).tolist())*3/4
    x_list = [i-bar_width/2 for i in dates]

    coh_mat = pnet.coherence_matrix(date12List, cohList)

    ax.bar(x_list, np.nanmax(coh_mat, axis=0), bar_width.days, label='Max Coherence')
    ax.bar(x_list, np.nanmin(coh_mat, axis=0), bar_width.days, label='Min Coherence')

    if plot_dict['disp_title']:
        ax.set_title('Coherence History of All Related Interferograms')

    ax = auto_adjust_xaxis_date(ax, datevector, plot_dict['fontsize'],
                                every_year=plot_dict['every_year'])[0]
    ax.set_ylim([0.0, 1.0])

    ax.set_xlabel('Time [years]', fontsize=plot_dict['fontsize'])
    ax.set_ylabel('Coherence', fontsize=plot_dict['fontsize'])
    ax.legend(loc='lower right')

    return ax
예제 #4
0
def plot_rms_bar(ax,
                 date_list,
                 rms,
                 cutoff=3.,
                 font_size=12,
                 tick_year_num=1,
                 legend_loc='best',
                 disp_legend=True,
                 disp_side_plot=True,
                 disp_thres_text=True,
                 ylabel=r'Residual Phase $\hat \phi_{resid}$ RMS [mm]'):
    """ Bar plot Phase Residual RMS
    Parameters: ax : Axes object
                date_list : list of string in YYYYMMDD format
                rms    : 1D np.array of float for RMS value in mm
                cutoff : cutoff value of MAD outlier detection
                tick_year_num : int, number of years per major tick
                legend_loc : 'upper right' or (0.5, 0.5)
    Returns:    ax : Axes object
    """
    dates, datevector = ptime.date_list2vector(date_list)
    try:
        bar_width = min(ut.most_common(np.diff(dates).tolist(), k=2)) * 3 / 4
    except:
        bar_width = np.min(np.diff(dates).tolist()) * 3 / 4
    rms = np.array(rms)

    # Plot all dates
    ax.bar(dates, rms, bar_width.days, color=pp.mplColors[0])

    # Plot reference date
    ref_idx = np.argmin(rms)
    ax.bar(dates[ref_idx],
           rms[ref_idx],
           bar_width.days,
           color=pp.mplColors[1],
           label='Reference date')

    # Plot exclude dates
    rms_threshold = ut.median_abs_deviation_threshold(rms,
                                                      center=0.,
                                                      cutoff=cutoff)
    ex_idx = rms > rms_threshold
    if not np.all(ex_idx == False):
        ax.bar(dates[ex_idx],
               rms[ex_idx],
               bar_width.days,
               color='darkgray',
               label='Exclude date')

    # Plot rms_threshold line
    (ax, xmin, xmax) = pp.auto_adjust_xaxis_date(ax,
                                                 datevector,
                                                 font_size,
                                                 every_year=tick_year_num)
    ax.plot(np.array([xmin, xmax]),
            np.array([rms_threshold, rms_threshold]),
            '--k',
            label='RMS threshold')

    # axis format
    ax = pp.auto_adjust_yaxis(ax,
                              np.append(rms, rms_threshold),
                              font_size,
                              ymin=0.0)
    ax.set_xlabel('Time [years]', fontsize=font_size)
    ax.set_ylabel(ylabel, fontsize=font_size)
    ax.tick_params(which='both',
                   direction='in',
                   labelsize=font_size,
                   bottom=True,
                   top=True,
                   left=True,
                   right=True)

    # 2nd axes for circles
    if disp_side_plot:
        divider = make_axes_locatable(ax)
        ax2 = divider.append_axes("right", "10%", pad="2%")
        ax2.plot(np.ones(rms.shape, np.float32) * 0.5,
                 rms,
                 'o',
                 mfc='none',
                 color=pp.mplColors[0])
        ax2.plot(np.ones(rms.shape, np.float32)[ref_idx] * 0.5,
                 rms[ref_idx],
                 'o',
                 mfc='none',
                 color=pp.mplColors[1])
        if not np.all(ex_idx == False):
            ax2.plot(np.ones(rms.shape, np.float32)[ex_idx] * 0.5,
                     rms[ex_idx],
                     'o',
                     mfc='none',
                     color='darkgray')
        ax2.plot(np.array([0, 1]), np.array([rms_threshold, rms_threshold]),
                 '--k')

        ax2.set_ylim(ax.get_ylim())
        ax2.set_xlim([0, 1])
        ax2.tick_params(which='both',
                        direction='in',
                        labelsize=font_size,
                        bottom=True,
                        top=True,
                        left=True,
                        right=True)
        ax2.get_xaxis().set_ticks([])
        ax2.get_yaxis().set_ticklabels([])

    if disp_legend:
        ax.legend(loc=legend_loc, frameon=False, fontsize=font_size)

    # rms_threshold text
    if disp_thres_text:
        ymin, ymax = ax.get_ylim()
        yoff = (ymax - ymin) * 0.1
        if (rms_threshold - ymin) > 0.5 * (ymax - ymin):
            yoff *= -1.
        ax.annotate('Median Abs Dev * {}'.format(cutoff),
                    xy=(xmin + (xmax - xmin) * 0.05, rms_threshold + yoff),
                    color='k',
                    xycoords='data',
                    fontsize=font_size)
    return ax
예제 #5
0
def dload_grib_pyaps(date_list, hour, trop_model='ECMWF', weather_dir='./'):
    """Download weather re-analysis grib files using PyAPS
    Inputs:
        date_list   : list of string in YYYYMMDD format
        hour        : string in HH:MM or HH format
        trop_model : string, 
        weather_dir : string,
    Output:
        grib_file_list : list of string
    """
    print(
        '*' * 50 +
        '\nDownloading weather model data using PyAPS (Jolivet et al., 2011, GRL) ...'
    )
    # Grib data directory
    grib_dir = weather_dir + '/' + trop_model
    if not os.path.isdir(grib_dir):
        os.makedirs(grib_dir)
        print('making directory: ' + grib_dir)

    # Date list to grib file list
    grib_file_list = date_list2grib_file(date_list, hour, trop_model, grib_dir)

    # Get date list to download (skip already downloaded files)
    grib_file_existed = ut.get_file_list(grib_file_list)
    if grib_file_existed:
        grib_filesize_digit = ut.most_common(
            [len(str(os.path.getsize(i))) for i in grib_file_existed])
        grib_filesize_max2 = ut.most_common(
            [str(os.path.getsize(i))[0:2] for i in grib_file_existed])
        grib_file_corrupted = [
            i for i in grib_file_existed
            if (len(str(os.path.getsize(i))) != grib_filesize_digit
                or str(os.path.getsize(i))[0:2] != grib_filesize_max2)
        ]
        print('file size mode: %se%d bytes' %
              (grib_filesize_max2, grib_filesize_digit - 2))
        print('number of grib files existed    : %d' % len(grib_file_existed))
        if grib_file_corrupted:
            print(
                '------------------------------------------------------------------------------'
            )
            print(
                'corrupted grib files detected! Delete them and re-download...'
            )
            print('number of grib files corrupted  : %d' %
                  len(grib_file_corrupted))
            for i in grib_file_corrupted:
                rmCmd = 'rm ' + i
                print(rmCmd)
                os.system(rmCmd)
                grib_file_existed.remove(i)
            print(
                '------------------------------------------------------------------------------'
            )
    grib_file2download = sorted(
        list(set(grib_file_list) - set(grib_file_existed)))
    date_list2download = [
        str(re.findall('\d{8}', i)[0]) for i in grib_file2download
    ]
    print('number of grib files to download: %d' % len(date_list2download))
    print(
        '------------------------------------------------------------------------------\n'
    )

    # Download grib file using PyAPS
    if trop_model == 'ECMWF': pa.ECMWFdload(date_list2download, hour, grib_dir)
    elif trop_model == 'MERRA':
        pa.MERRAdload(date_list2download, hour, grib_dir)
    elif trop_model == 'NARR':
        pa.NARRdload(date_list2download, hour, grib_dir)
    elif trop_model == 'ERA':
        pa.ERAdload(date_list2download, hour, grib_dir)
    elif trop_model == 'MERRA1':
        pa.MERRA1dload(date_list2download, hour, grib_dir)
    return grib_file_list