Пример #1
0
def main(argv):
    inps = cmdLineParse()
    if inps.template_file:
        inps = read_template2inps(inps.template_file)

    if inps.ref_date == 'no':
        print 'No reference date input, skip this step.'
        return inps.timeseries_file

    elif inps.ref_date.lower() in ['auto']:
        print '------------------------------------------------------------'
        print 'auto choose reference date based on minimum residual RMS'
        if not inps.resid_file:
            inps.resid_file = os.path.splitext(
                inps.timeseries_file)[0] + 'InvResid.h5'
        rms_list, date_list = ut.get_residual_rms(inps.resid_file,
                                                  inps.mask_file,
                                                  inps.ramp_type)
        ref_idx = np.argmin(rms_list)
        inps.ref_date = date_list[ref_idx]
        print 'date with minimum residual RMS: %s - %.4f' % (inps.ref_date,
                                                             rms_list[ref_idx])
        print '------------------------------------------------------------'

    elif os.path.isfile(inps.ref_date):
        print 'read reference date from file: ' + inps.ref_date
        inps.ref_date = ptime.read_date_list(inps.ref_date)[0]

    # Referencing input file
    inps.outfile = ref_date_file(inps.timeseries_file, inps.ref_date,
                                 inps.outfile)
    return inps.outfile
Пример #2
0
def main(argv):
    inps = cmdLineParse()
    if inps.template_file:
        inps = read_template2inps(inps.template_file)

    ##### calculate timeseries of residual Root Mean Square
    #std_list, date_list = ut.get_residual_std(inps.timeseries_file, inps.mask_file, inps.ramp_type)
    rms_list, date_list = ut.get_residual_rms(inps.timeseries_file,
                                              inps.mask_file, inps.ramp_type)

    ##### reference_date.txt
    print '------------------------------------------------------------'
    ref_idx = np.argmin(rms_list)
    ref_date = date_list[ref_idx]
    print 'date with minimum residual RMS: %s - %.4f' % (ref_date,
                                                         rms_list[ref_idx])

    refTxtFile = 'reference_date.txt'
    if (inps.save_reference_date and \
        ut.update_file(refTxtFile, [inps.timeseries_file, inps.mask_file, inps.template_file],\
                       check_readable=False)):
        f = open(refTxtFile, 'w')
        f.write(ref_date + '\n')
        f.close()
        print 'save date to file: ' + refTxtFile

    ##### exclude_date.txt
    print '------------------------------------------------------------'
    ex_idx_list = [rms_list.index(i) for i in rms_list if i > inps.min_rms]
    print 'date(s) with residual RMS > ' + str(inps.min_rms)
    exTxtFile = 'exclude_date.txt'
    if ex_idx_list:
        if (inps.save_exclude_date and \
            ut.update_file(exTxtFile, [inps.timeseries_file, inps.mask_file, inps.template_file],\
                           check_readable=False)):
            f = open(exTxtFile, 'w')
            for i in ex_idx_list:
                print '%s - %.4f' % (date_list[i], rms_list[i])
                f.write(date_list[i] + '\n')
            f.close()
            print 'save date(s) to file: ' + exTxtFile
    else:
        print 'None.'

    ##### Plot
    fig_name = os.path.dirname(os.path.abspath(inps.timeseries_file))+\
               '/rms_'+os.path.splitext(inps.timeseries_file)[0]
    if inps.ramp_type != 'no':
        fig_name += '_' + inps.ramp_type
    fig_name += '.pdf'

    if ut.update_file(fig_name, [exTxtFile, refTxtFile, 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(date_list)
        try:
            bar_width = ut.mode(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]

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

        # Plot reference date
        #if inps.save_reference_date:
        ax.bar(x_list[ref_idx],
               rms_list[ref_idx],
               bar_width.days,
               label='Reference date')

        # Plot exclude dates
        #if ex_idx_list and inps.save_exclude_date:
        if ex_idx_list:
            ex_x_list = [x_list[i] for i in ex_idx_list]
            ex_rms_list = [rms_list[i] for i in ex_idx_list]
            ax.bar(ex_x_list,
                   ex_rms_list,
                   bar_width.days,
                   color='darkgray',
                   label='Exclude date(s)')

        # Plot min_rms line
        ax, xmin, xmax = ptime.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 = pnet.auto_adjust_yaxis(ax,
                                    rms_list + [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)

        if inps.save_reference_date or inps.save_exclude_date:
            plt.legend(fontsize=font_size)

        # save figure
        fig.savefig(fig_name, bbox_inches='tight', transparent=True)
        print 'save figure to file: ' + fig_name

    return