コード例 #1
0
def main():

    fname_in = sys.argv[1]

    din = GetData(fname_in, 'a')
    time = ap.num2date(getattr(din, T_NAME)[:])
    lon = din.lon[:]
    lat = din.lat[:]
    sat = din.satname[:]
    nrows = len(time)
    nt, ny, nx =  getattr(din, H_NAME).shape    # i,j,k = t,y,x

    RR = np.empty((nt,ny,nx), 'f8') * np.nan
    SS = np.empty((nt,ny,nx), 'f8') * np.nan

    print 'processing time series:'
    isfirst = True

    # iterate over every grid cell (all times): i,j = y,x
    #-----------------------------------------------------------------

    for i in xrange(ny):
        for j in xrange(nx):
            print 'time series of grid-cell:', i, j

            dh = getattr(din, H_NAME)[:nrows,i,j]
            dg = getattr(din, G_NAME)[:nrows,i,j]
            dh_cor = np.zeros_like(dh)

            if np.alltrue(np.isnan(dh)): continue

            #---------------------------------------------------------

            # pull and correct a chunk of the array at a time
            for s in np.unique(sat):
                k = np.where(sat == s)
                dh_cor[k], R, S = ap.backscatter_corr(dh[k], dg[k], 
                                  diff=DIFF, robust=True)
                RR[k,i,j] = R
                SS[k,i,j] = S

            #---------------------------------------------------------

            if PLOT_TS:
                dh_cor = ap.referenced(dh_cor, to='first')
                dh = ap.referenced(dh, to='first')
                dg = ap.referenced(dg, to='first')
                fig = plot_rs(time, RR[:,i,j], SS[:,i,j])
                for s in np.unique(sat):
                    k = np.where(sat == s)
                    r, s = np.mean(RR[k,i,j]), np.mean(SS[k,i,j])
                    try:
                        fig = plot_ts(time[k], lon[j], lat[i], dh_cor[k], 
                                      dh[k], dg[k], r, s, diff=DIFF)
                    except:
                        print 'something wrong with ploting!'
                        print 'dh:', dh
                        print 'dg:', dg
                if fig is None: continue
                plt.show()

            # save one TS per grid cell at a time
            #---------------------------------------------------------

            if not SAVE_TO_FILE: continue

            if isfirst:
                # open or create output file
                isfirst = False
                atom = tb.Atom.from_type('float64', dflt=np.nan)
                filters = tb.Filters(complib='zlib', complevel=9)
                try:
                    c1 = din.file.create_carray('/', SAVE_AS_NAME, atom, 
                                               (nt,ny,nx), '', filters)
                except:
                    c1 = din.file.getNode('/', SAVE_AS_NAME)
                c2 = din.file.create_carray('/', R_NAME, atom, 
                                           (nt,ny,nx), '', filters)
                c3 = din.file.create_carray('/', S_NAME, atom, 
                                           (nt,ny,nx), '', filters)
            c1[:,i,j] = dh_cor

    if SAVE_TO_FILE:
        c2[:] = RR
        c3[:] = SS

    if PLOT_MAP:
        RR = RR[0]  # change accordingly
        SS = SS[0]
        plot_map(lon, lat, np.abs(RR), BBOX, MASK_FILE, mres=1, vmin=0, vmax=1)
        plt.title('Correlation Coefficient, R')
        plt.savefig('map_r.png')
        plot_map(lon, lat, SS, BBOX, MASK_FILE, mres=1, vmin=-0.2, vmax=0.7)
        plt.title('Correlation Gradient, S')
        plt.savefig('map_s.png')
        plt.show()

    din.file.close()

    if SAVE_TO_FILE:
        print 'out file -->', fname_in
コード例 #2
0
ファイル: backscatter2.py プロジェクト: fspaolo/code
def main():

    fname_in = sys.argv[1]

    din = GetData(fname_in, 'a')
    time = ap.num2date(getattr(din, T_NAME)[:])
    lon = din.lon[:]
    lat = din.lat[:]
    nrows = len(time)
    nt, ny, nx =  getattr(din, H_NAME).shape    # i,j,k = t,y,x

    RR = np.empty((nt,ny,nx), 'f8') * np.nan
    SS = np.empty((nt,ny,nx), 'f8') * np.nan

    if TINT:
        intervals = [ap.year2date(tt) for tt in INTERVALS]

    if TINT:
        print 'using time-interval correlation'
    elif TVAR:
        print 'using time-variable correlation'
    else:
        print 'using constant correlation'
    print 'processing time series:'
    isfirst = True

    # iterate over every grid cell (all times): i,j = y,x
    #-----------------------------------------------------------------

    for i in xrange(ny):
        for j in xrange(nx):
            print 'time series of grid-cell:', i, j

            dh = getattr(din, H_NAME)[:nrows,i,j]
            dg = getattr(din, G_NAME)[:nrows,i,j]

            if np.alltrue(np.isnan(dh)): continue

            #---------------------------------------------------------

            if TINT:
                # satellite-dependent R and S
                dh_cor, RR[:,i,j], SS[:,i,j] = \
                    ap.backscatter_corr3(dh, dg, time, intervals, 
                                         diff=DIFF, robust=True)
            elif TVAR:
                # time-varying R and S
                dh_cor, RR[:,i,j], SS[:,i,j] = \
                    ap.backscatter_corr2(dh, dg, diff=DIFF, 
                                         robust=True, npts=NPTS)
            else:
                # constant R and S
                dh_cor, RR[:,i,j], SS[:,i,j] = \
                    ap.backscatter_corr(dh, dg, diff=DIFF, robust=True)

            #---------------------------------------------------------

            # plot figures
            if PLOT_TS:
                dh_cor = ap.referenced(dh_cor, to='first')
                dh = ap.referenced(dh, to='first')
                dg = ap.referenced(dg, to='first')
                k, = np.where(~np.isnan(RR[:,i,j]))
                r = np.mean(RR[k,i,j])
                s = np.mean(SS[k,i,j])
                fig = plot_rs(time, RR[:,i,j], SS[:,i,j])
                fig = plot_ts(time, lon[j], lat[i], dh_cor, dh, dg, 
                              r, s, diff=DIFF)
                if fig is None: continue
                plt.show()

            # save one TS per grid cell at a time
            #---------------------------------------------------------

            if not SAVE_TO_FILE: continue

            if isfirst:
                # open or create output file
                isfirst = False
                atom = tb.Atom.from_type('float64', dflt=np.nan)
                filters = tb.Filters(complib='zlib', complevel=9)
                c1 = din.file.createCArray('/', SAVE_AS_NAME, atom, 
                                           (nt,ny,nx), '', filters)
                c2 = din.file.createCArray('/', R_NAME, atom, 
                                           (nt,ny,nx), '', filters)
                c3 = din.file.createCArray('/', S_NAME, atom, 
                                           (nt,ny,nx), '', filters)
            c1[:,i,j] = dh_cor

    if SAVE_TO_FILE:
        c2[:] = RR
        c3[:] = SS

    if PLOT_MAP:
        if TVAR:
            # 3D -> 2D
            RR = np.mean(RR[~np.isnan(RR)], axis=0)
            SS = np.mean(SS[~np.isnan(SS)], axis=0)
        plot_map(lon, lat, np.abs(RR), BBOX, MFILE, mres=1, vmin=0, vmax=1)
        plt.title('Correlation Coefficient, R')
        plt.savefig('map_r.png')
        plot_map(lon, lat, SS, BBOX, MFILE, mres=1, vmin=-0.2, vmax=0.7)
        plt.title('Correlation Gradient, S')
        plt.savefig('map_s.png')
        plt.show()

    din.file.close()

    if SAVE_TO_FILE:
        print 'out file -->', fname_in
コード例 #3
0
ファイル: backscatter2.py プロジェクト: sjl421/code-2
def main():

    fname_in = sys.argv[1]

    din = GetData(fname_in, 'a')
    time = ap.num2date(getattr(din, T_NAME)[:])
    lon = din.lon[:]
    lat = din.lat[:]
    nrows = len(time)
    nt, ny, nx = getattr(din, H_NAME).shape  # i,j,k = t,y,x

    RR = np.empty((nt, ny, nx), 'f8') * np.nan
    SS = np.empty((nt, ny, nx), 'f8') * np.nan

    if TINT:
        intervals = [ap.year2date(tt) for tt in INTERVALS]

    if TINT:
        print 'using time-interval correlation'
    elif TVAR:
        print 'using time-variable correlation'
    else:
        print 'using constant correlation'
    print 'processing time series:'
    isfirst = True

    # iterate over every grid cell (all times): i,j = y,x
    #-----------------------------------------------------------------

    for i in xrange(ny):
        for j in xrange(nx):
            print 'time series of grid-cell:', i, j

            dh = getattr(din, H_NAME)[:nrows, i, j]
            dg = getattr(din, G_NAME)[:nrows, i, j]

            if np.alltrue(np.isnan(dh)): continue

            #---------------------------------------------------------

            if TINT:
                # satellite-dependent R and S
                dh_cor, RR[:,i,j], SS[:,i,j] = \
                    ap.backscatter_corr3(dh, dg, time, intervals,
                                         diff=DIFF, robust=True)
            elif TVAR:
                # time-varying R and S
                dh_cor, RR[:,i,j], SS[:,i,j] = \
                    ap.backscatter_corr2(dh, dg, diff=DIFF,
                                         robust=True, npts=NPTS)
            else:
                # constant R and S
                dh_cor, RR[:,i,j], SS[:,i,j] = \
                    ap.backscatter_corr(dh, dg, diff=DIFF, robust=True)

            #---------------------------------------------------------

            # plot figures
            if PLOT_TS:
                dh_cor = ap.referenced(dh_cor, to='first')
                dh = ap.referenced(dh, to='first')
                dg = ap.referenced(dg, to='first')
                k, = np.where(~np.isnan(RR[:, i, j]))
                r = np.mean(RR[k, i, j])
                s = np.mean(SS[k, i, j])
                fig = plot_rs(time, RR[:, i, j], SS[:, i, j])
                fig = plot_ts(time,
                              lon[j],
                              lat[i],
                              dh_cor,
                              dh,
                              dg,
                              r,
                              s,
                              diff=DIFF)
                if fig is None: continue
                plt.show()

            # save one TS per grid cell at a time
            #---------------------------------------------------------

            if not SAVE_TO_FILE: continue

            if isfirst:
                # open or create output file
                isfirst = False
                atom = tb.Atom.from_type('float64', dflt=np.nan)
                filters = tb.Filters(complib='zlib', complevel=9)
                c1 = din.file.createCArray('/', SAVE_AS_NAME, atom,
                                           (nt, ny, nx), '', filters)
                c2 = din.file.createCArray('/', R_NAME, atom, (nt, ny, nx), '',
                                           filters)
                c3 = din.file.createCArray('/', S_NAME, atom, (nt, ny, nx), '',
                                           filters)
            c1[:, i, j] = dh_cor

    if SAVE_TO_FILE:
        c2[:] = RR
        c3[:] = SS

    if PLOT_MAP:
        if TVAR:
            # 3D -> 2D
            RR = np.mean(RR[~np.isnan(RR)], axis=0)
            SS = np.mean(SS[~np.isnan(SS)], axis=0)
        plot_map(lon, lat, np.abs(RR), BBOX, MFILE, mres=1, vmin=0, vmax=1)
        plt.title('Correlation Coefficient, R')
        plt.savefig('map_r.png')
        plot_map(lon, lat, SS, BBOX, MFILE, mres=1, vmin=-0.2, vmax=0.7)
        plt.title('Correlation Gradient, S')
        plt.savefig('map_s.png')
        plt.show()

    din.file.close()

    if SAVE_TO_FILE:
        print 'out file -->', fname_in