示例#1
0
def sarsim(cfg_file=None):

    print(
        '-------------------------------------------------------------------',
        flush=True)
    print(time.strftime("OCEANSAR SAR Simulator [%Y-%m-%d %H:%M:%S]",
                        time.localtime()),
          flush=True)
    # print('Copyright (c) Gerard Marull Paretas, Paco López-Dekker')
    print(
        '-------------------------------------------------------------------',
        flush=True)
    cfg_file = utils.get_parFile(parfile=cfg_file)
    cfg = osrio.ConfigFile(cfg_file)
    # Create output directory if it doesnt exist already
    os.makedirs(cfg.sim.path, exist_ok=True)
    src_path = os.path.dirname(os.path.abspath(__file__))

    # RAW
    if cfg.sim.raw_run:

        print('Launching SAR RAW Generator...')
        sar_raw(cfg.cfg_file_name,
                os.path.join(cfg.sim.path, cfg.sim.raw_file),
                os.path.join(cfg.sim.path, cfg.sim.ocean_file),
                cfg.sim.ocean_reuse,
                os.path.join(cfg.sim.path, cfg.sim.errors_file),
                cfg.sim.errors_reuse,
                plot_save=True)

    # Processing
    if cfg.sim.proc_run:
        print('Launching SAR RAW Processor...')
        sar_focus(cfg.cfg_file_name,
                  os.path.join(cfg.sim.path, cfg.sim.raw_file),
                  os.path.join(cfg.sim.path, cfg.sim.proc_file))

    if cfg.sim.insar_run:
        print('Launching InSAR L1b Processor...')
        insar_process(cfg.cfg_file_name,
                      os.path.join(cfg.sim.path, cfg.sim.proc_file),
                      os.path.join(cfg.sim.path, cfg.sim.ocean_file),
                      os.path.join(cfg.sim.path, cfg.sim.insar_file))

    if cfg.sim.ati_run:
        print('Launching SAR ATI Processor...')
        ati_process(cfg.cfg_file_name,
                    os.path.join(cfg.sim.path, cfg.sim.insar_file),
                    os.path.join(cfg.sim.path, cfg.sim.ocean_file),
                    os.path.join(cfg.sim.path, cfg.sim.ati_file))

    if cfg.sim.L2_wavespectrum_run:
        print('Launching L2 Wavespectrum Processor...')

    print('----------------------------------', flush=True)
    print(time.strftime("End of tasks [%Y-%m-%d %H:%M:%S]", time.localtime()),
          flush=True)
    print('----------------------------------', flush=True)
示例#2
0
def skimsim(cfg_file=None):

    print('-------------------------------------------------------------------', flush=True)
    print(time.strftime("OCEANSAR SKIM Simulator [%Y-%m-%d %H:%M:%S]", time.localtime()), flush=True)
    # print('Copyright (c) Gerard Marull Paretas, Paco López-Dekker')
    print('-------------------------------------------------------------------', flush=True)
    cfg_file = utils.get_parFile(parfile=cfg_file)
    cfg = osrio.ConfigFile(cfg_file)
    # Create output directory if it doesnt exist already
    os.makedirs(cfg.sim.path, exist_ok=True)
    src_path = os.path.dirname(os.path.abspath(__file__))

    # RAW
    if cfg.sim.raw_run:

        print('Launching SAR RAW Generator...')

        args = [cfg.sim.mpi_exec,
                '-np', str(cfg.sim.mpi_num_proc),
                sys.executable, src_path + os.sep + 'skim_raw.py',
                '-c', cfg.cfg_file_name,
                '-o', cfg.sim.path + os.sep + cfg.sim.raw_file,
                '-oc', cfg.sim.path + os.sep + cfg.sim.ocean_file,
                '-er', cfg.sim.path + os.sep + cfg.sim.errors_file]

        if cfg.sim.ocean_reuse:
            args.append('-ro')
        if cfg.sim.errors_reuse:
            args.append('-re')

        returncode = subprocess.call(args)

        if returncode != 0:
            raise Exception('Something went wrong with SAR RAW Generator (return code %d)...' % returncode)

    # Processing
    if cfg.sim.proc_run:
        print('Launching SAR RAW Processor...')

        returncode = subprocess.call([sys.executable,
                                      src_path + os.sep + 'skim_processor.py',
                                      '-c', cfg.cfg_file_name,
                                      '-r', cfg.sim.path + os.sep + cfg.sim.raw_file])
        #,
        #                              '-o', cfg.sim.path + os.sep + cfg.sim.pp_file])

        if returncode != 0:
            raise Exception('Something went wrong with SAR RAW Processor (return code %d)...' % returncode)



    print('----------------------------------', flush=True)
    print(time.strftime("End of tasks [%Y-%m-%d %H:%M:%S]", time.localtime()), flush=True)
    print('----------------------------------', flush=True)
示例#3
0
def batch_sarsim(template_file):
    cfg_file = utils.get_parFile(parfile=template_file)
    ref_cfg = osrio.ConfigFile(cfg_file)
    step = 0
    sim_path_ref = ref_cfg.sim.path + os.sep + 'sim_U%d_wdir%d_smag%d_sdir%d_inc%d_i%i'
    n_all = (np.size(v_wind_U) * np.size(v_wind_dir) * np.size(v_current_mag) *
             np.size(v_current_dir) * np.size(v_inc_angle) * n_rep)
    for wind_U in v_wind_U:
        for wind_dir in v_wind_dir:
            for current_mag in v_current_mag:
                for current_dir in v_current_dir:
                    for inc_angle in v_inc_angle:
                        for i_rep in np.arange(n_rep, dtype=np.int):
                            step = step + 1
                            print("")
                            print(
                                'CONFIGURATION AND LAUNCH OF SIMULATION %d of %d'
                                % (step, n_all))

                            ### CONFIGURATION FILE ###
                            # Load template
                            cfg = ref_cfg

                            # Modify template, create directory & save
                            cfg.sim.path = sim_path_ref % (
                                wind_U, wind_dir, current_mag, current_dir,
                                inc_angle, i_rep)
                            cfg.ocean.wind_U = wind_U
                            cfg.ocean.wind_dir = wind_dir
                            cfg.ocean.current_mag = current_mag
                            cfg.ocean.current_dir = current_dir
                            cfg.sar.inc_angle = inc_angle

                            if not os.path.exists(cfg.sim.path):
                                os.makedirs(cfg.sim.path)

                            # Save configuration file into an alternate file
                            cfg.save(cfg.sim.path + os.sep + cfg_file_name)

                            ### LAUNCH MACSAR ###
                            subprocess.call([
                                sys.executable, osr_script,
                                cfg.sim.path + os.sep + cfg_file_name
                            ])
示例#4
0
def ati_process(cfg_file, proc_output_file, ocean_file, output_file):

    print('-------------------------------------------------------------------')
    print(time.strftime("- OCEANSAR ATI Processor: [%Y-%m-%d %H:%M:%S]", time.localtime()))
    print('-------------------------------------------------------------------')

    print('Initializing...')

    ## CONFIGURATION FILE
    cfg = tpio.ConfigFile(cfg_file)

    # SAR
    inc_angle = np.deg2rad(cfg.sar.inc_angle)
    f0 = cfg.sar.f0
    prf = cfg.sar.prf
    num_ch = cfg.sar.num_ch
    ant_L = cfg.sar.ant_L
    alt = cfg.sar.alt
    v_ground = cfg.sar.v_ground
    rg_bw = cfg.sar.rg_bw
    over_fs = cfg.sar.over_fs
    pol = cfg.sar.pol
    if pol == 'DP':
        polt = ['hh', 'vv']
    elif pol == 'hh':
        polt = ['hh']
    else:
        polt = ['vv']
    # ATI
    rg_ml = cfg.ati.rg_ml
    az_ml = cfg.ati.az_ml
    ml_win = cfg.ati.ml_win
    plot_save = cfg.ati.plot_save
    plot_path = cfg.ati.plot_path
    plot_format = cfg.ati.plot_format
    plot_tex = cfg.ati.plot_tex
    plot_surface = cfg.ati.plot_surface
    plot_proc_ampl = cfg.ati.plot_proc_ampl
    plot_coh = cfg.ati.plot_coh
    plot_coh_all = cfg.ati.plot_coh_all
    plot_ati_phase = cfg.ati.plot_ati_phase
    plot_ati_phase_all = cfg.ati.plot_ati_phase_all
    plot_vel_hist = cfg.ati.plot_vel_hist
    plot_vel = cfg.ati.plot_vel

    ## CALCULATE PARAMETERS
    if v_ground == 'auto': v_ground = geosar.orbit_to_vel(alt, ground=True)
    k0 = 2.*np.pi*f0/const.c
    rg_sampling = rg_bw*over_fs

    # PROCESSED RAW DATA
    proc_content = tpio.ProcFile(proc_output_file, 'r')
    proc_data = proc_content.get('slc*')
    proc_content.close()

    # OCEAN SURFACE
    surface = OceanSurface()
    surface.load(ocean_file, compute=['D', 'V'])
    surface.t = 0.

    # OUTPUT FILE
    output = open(output_file, 'w')

    # OTHER INITIALIZATIONS
    # Enable TeX
    if plot_tex:
        plt.rc('font', family='serif')
        plt.rc('text', usetex=True)

    # Create plots directory
    plot_path = os.path.dirname(output_file) + os.sep + plot_path
    if plot_save:
        if not os.path.exists(plot_path):
            os.makedirs(plot_path)

    # SURFACE VELOCITIES
    grg_grid_spacing = (const.c/2./rg_sampling/np.sin(inc_angle))
    rg_res_fact = grg_grid_spacing / surface.dx
    az_grid_spacing = (v_ground/prf)
    az_res_fact = az_grid_spacing / surface.dy
    res_fact = np.ceil(np.sqrt(rg_res_fact*az_res_fact))

    # SURFACE RADIAL VELOCITY
    v_radial_surf = surface.Vx*np.sin(inc_angle) - surface.Vz*np.cos(inc_angle)
    v_radial_surf_ml = utils.smooth(utils.smooth(v_radial_surf, res_fact * rg_ml, axis=1), res_fact * az_ml, axis=0)
    v_radial_surf_mean = np.mean(v_radial_surf)
    v_radial_surf_std = np.std(v_radial_surf)
    v_radial_surf_ml_std = np.std(v_radial_surf_ml)

    # SURFACE HORIZONTAL VELOCITY
    v_horizo_surf = surface.Vx
    v_horizo_surf_ml = utils.smooth(utils.smooth(v_horizo_surf, res_fact * rg_ml, axis=1), res_fact * az_ml, axis=0)
    v_horizo_surf_mean = np.mean(v_horizo_surf)
    v_horizo_surf_std = np.std(v_horizo_surf)
    v_horizo_surf_ml_std = np.std(v_horizo_surf_ml)

    # Expected mean azimuth shift
    sr0 = geosar.inc_to_sr(inc_angle, alt)
    avg_az_shift = - v_radial_surf_mean / v_ground * sr0
    std_az_shift = v_radial_surf_std / v_ground * sr0
    ##################
    # ATI PROCESSING #
    ##################

    print('Starting ATI processing...')

    # Get dimensions & calculate region of interest
    rg_span = surface.Lx
    az_span = surface.Ly
    rg_size = proc_data[0].shape[2]
    az_size = proc_data[0].shape[1]

    # Note: RG is projected, so plots are Ground Range
    rg_min = 0
    rg_max = np.int(rg_span/(const.c/2./rg_sampling/np.sin(inc_angle)))
    az_min = np.int(az_size/2. + (-az_span/2. + avg_az_shift)/(v_ground/prf))
    az_max = np.int(az_size/2. + (az_span/2. + avg_az_shift)/(v_ground/prf))
    az_guard = np.int(std_az_shift / (v_ground / prf))
    if (az_max - az_min) < (2 * az_guard - 10):
        print('Not enough edge-effect free image')
        return

    # Adaptive coregistration
    if cfg.sar.L_total:
        ant_L = ant_L/np.float(num_ch)
        dist_chan = ant_L/2
    else:
        if np.float(cfg.sar.Spacing) != 0:
            dist_chan = np.float(cfg.sar.Spacing)/2
        else:
            dist_chan = ant_L/2
    # dist_chan = ant_L/num_ch/2.
    print('ATI Spacing: %f' % dist_chan)
    inter_chan_shift_dist = dist_chan/(v_ground/prf)
    # Subsample shift in azimuth
    for chind in range(proc_data.shape[0]):
        shift_dist = - chind * inter_chan_shift_dist
        shift_arr = np.exp(-2j * np.pi * shift_dist *
                           np.roll(np.arange(az_size) - az_size/2,
                                   int(-az_size / 2)) / az_size)
        shift_arr = shift_arr.reshape((1, az_size, 1))
        proc_data[chind] = np.fft.ifft(np.fft.fft(proc_data[chind], axis=1) *
                                       shift_arr, axis=1)

    # First dimension is number of channels, second is number of pols
    ch_dim = proc_data.shape[0:2]
    npol = ch_dim[1]
    proc_data_rshp = [np.prod(ch_dim), proc_data.shape[2], proc_data.shape[3]]
    # Compute extended covariance matrices...
    proc_data = proc_data.reshape(proc_data_rshp)
    # Intensities
    i_all = []
    for chind in range(proc_data.shape[0]):
        this_i = utils.smooth(utils.smooth(np.abs(proc_data[chind])**2., rg_ml, axis=1, window=ml_win),
                              az_ml, axis=0, window=ml_win)
        i_all.append(this_i[az_min:az_max, rg_min:rg_max])
    i_all = np.array(i_all)
    # .reshape((ch_dim) + (az_max - az_min, rg_max - rg_min))
    interfs = []
    cohs = []
    tind = 0
    coh_lut = np.zeros((proc_data.shape[0], proc_data.shape[0]), dtype=int)
    for chind1 in range(proc_data.shape[0]):
        for chind2 in range(chind1 + 1, proc_data.shape[0]):
            coh_lut[chind1, chind2] = tind
            tind = tind + 1
            t_interf = utils.smooth(utils.smooth(proc_data[chind2] *
                                                 np.conj(proc_data[chind1]),
                                                 rg_ml, axis=1, window=ml_win),
                                    az_ml, axis=0, window=ml_win)
            interfs.append(t_interf[az_min:az_max, rg_min:rg_max])
            cohs.append(t_interf[az_min:az_max, rg_min:rg_max] /
                        np.sqrt(i_all[chind1] * i_all[chind2]))

    print('Generating plots and estimating values...')

    # SURFACE HEIGHT
    if plot_surface:
        plt.figure()
        plt.imshow(surface.Dz, cmap="ocean",
                   extent=[0, surface.Lx, 0, surface.Ly], origin='lower')
        plt.title('Surface Height')
        plt.xlabel('Ground range [m]')
        plt.ylabel('Azimuth [m]')
        cbar = plt.colorbar()
        cbar.ax.set_xlabel('[m]')

        if plot_save:
            plt.savefig(plot_path + os.sep + 'plot_surface.' + plot_format,
                        bbox_inches='tight')
            plt.close()
        else:
            plt.show()


    # PROCESSED AMPLITUDE
    if plot_proc_ampl:
        for pind in range(npol):
            save_path = (plot_path + os.sep + 'amp_dB_' + polt[pind]+
                         '.' + plot_format)
            plt.figure()
            plt.imshow(utils.db(i_all[pind]), aspect='equal',
                       origin='lower',
                       vmin=utils.db(np.max(i_all[pind]))-20,
                       extent=[0., rg_span, 0., az_span], interpolation='nearest',
                       cmap='viridis')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("Amplitude")
            plt.colorbar()
            plt.savefig(save_path)

            save_path = (plot_path + os.sep + 'amp_' + polt[pind]+
                         '.' + plot_format)
            int_img = (i_all[pind])**0.5
            vmin = np.mean(int_img) - 3 * np.std(int_img)
            vmax = np.mean(int_img) + 3 * np.std(int_img)
            plt.figure()
            plt.imshow(int_img, aspect='equal',
                       origin='lower',
                       vmin=vmin, vmax=vmax,
                       extent=[0., rg_span, 0., az_span], interpolation='nearest',
                       cmap='viridis')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("Amplitude")
            plt.colorbar()
            plt.savefig(save_path)

    if plot_coh and ch_dim[0] > 1:
        for pind in range(npol):
            save_path = (plot_path + os.sep + 'ATI_coh_' +
                         polt[pind] + polt[pind] +
                         '.' + plot_format)
            coh_ind = coh_lut[(pind, pind + npol)]
            plt.figure()
            plt.imshow(np.abs(cohs[coh_ind]), aspect='equal',
                       origin='lower',
                       vmin=0, vmax=1,
                       extent=[0., rg_span, 0., az_span],
                       cmap='bone')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("ATI Coherence")
            # plt.colorbar()
            plt.savefig(save_path)

    # ATI PHASE

    tau_ati = dist_chan/v_ground

    ati_phases = []
    # Hack to avoid interferogram computation if there are no interferometric channels
    if num_ch > 1:
        npol_ = npol
    else:
        npol_ = 0
    for pind in range(npol_):
        save_path = (plot_path + os.sep + 'ATI_pha_' +
                     polt[pind] + polt[pind] +
                     '.' + plot_format)
        coh_ind = coh_lut[(pind, pind + npol)]
        ati_phase = uwphase(cohs[coh_ind])
        ati_phases.append(ati_phase)
        v_radial_est = -ati_phase / tau_ati / (k0 * 2.)
        if plot_ati_phase:
            phase_mean = np.mean(ati_phase)
            phase_std = np.std(ati_phase)
            vmin = np.max([-np.abs(phase_mean) - 4*phase_std,
                           -np.abs(ati_phase).max()])
            vmax = np.min([np.abs(phase_mean) + 4*phase_std,
                           np.abs(ati_phase).max()])
            plt.figure()
            plt.imshow(ati_phase, aspect='equal',
                       origin='lower',
                       vmin=vmin, vmax=vmax,
                       extent=[0., rg_span, 0., az_span],
                       cmap='hsv')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("ATI Phase")
            plt.colorbar()
            plt.savefig(save_path)

            save_path = (plot_path + os.sep + 'ATI_rvel_' +
                         polt[pind] + polt[pind] +
                         '.' + plot_format)
            vmin = -np.abs(v_radial_surf_mean) - 4. * v_radial_surf_std
            vmax = np.abs(v_radial_surf_mean) + 4. * v_radial_surf_std
            plt.figure()
            plt.imshow(v_radial_est, aspect='equal',
                       origin='lower',
                       vmin=vmin, vmax=vmax,
                       extent=[0., rg_span, 0., az_span],
                       cmap='bwr')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("Estimated Radial Velocity " + polt[pind])
            plt.colorbar()
            plt.savefig(save_path)

    if npol_ == 4:  # Bypass this for now
        # Cross pol interferogram
        coh_ind = coh_lut[(0, 1)]
        save_path = (plot_path + os.sep + 'POL_coh_' +
                     polt[0] + polt[1] +
                     '.' + plot_format)
        utils.image(np.abs(cohs[coh_ind]), max=1, min=0, aspect='equal',
                    cmap='gray', extent=[0., rg_span, 0., az_span],
                    xlabel='Ground range [m]', ylabel='Azimuth [m]',
                    title='XPOL Coherence',
                    usetex=plot_tex, save=plot_save, save_path=save_path)
        save_path = (plot_path + os.sep + 'POL_pha_' +
                     polt[0] + polt[1] +
                     '.' + plot_format)
        ati_phase = uwphase(cohs[coh_ind])
        phase_mean = np.mean(ati_phase)
        phase_std = np.std(ati_phase)
        vmin = np.max([-np.abs(phase_mean) - 4*phase_std, -np.pi])
        vmax = np.min([np.abs(phase_mean) + 4*phase_std, np.pi])
        utils.image(ati_phase, aspect='equal',
                    min=vmin,  max=vmax,
                    cmap=utils.bwr_cmap, extent=[0., rg_span, 0., az_span],
                    xlabel='Ground range [m]', ylabel='Azimuth [m]',
                    title='XPOL Phase', cbar_xlabel='[rad]',
                    usetex=plot_tex, save=plot_save, save_path=save_path)

    if num_ch > 1:
        ati_phases = np.array(ati_phases)

        output.write('--------------------------------------------\n')
        output.write('SURFACE RADIAL VELOCITY - NO SMOOTHING\n')
        output.write('MEAN(SURF. V) = %.4f\n' % v_radial_surf_mean)
        output.write('STD(SURF. V) = %.4f\n' % v_radial_surf_std)
        output.write('--------------------------------------------\n\n')

        output.write('--------------------------------------------\n')
        output.write('SURFACE RADIAL VELOCITY - SMOOTHING (WIN. SIZE=%dx%d)\n' % (az_ml, rg_ml))
        output.write('MEAN(SURF. V) = %.4f\n' % v_radial_surf_mean)
        output.write('STD(SURF. V) = %.4f\n' % v_radial_surf_ml_std)
        output.write('--------------------------------------------\n\n')

        output.write('--------------------------------------------\n')
        output.write('SURFACE HORIZONTAL VELOCITY - NO SMOOTHING\n')
        output.write('MEAN(SURF. V) = %.4f\n' % v_horizo_surf_mean)
        output.write('STD(SURF. V) = %.4f\n' % v_horizo_surf_std)
        output.write('--------------------------------------------\n\n')

        if plot_vel_hist:
            # PLOT RADIAL VELOCITY
            plt.figure()

            plt.hist(v_radial_surf.flatten(), 200, normed=True, histtype='step')
            #plt.hist(v_radial_surf_ml.flatten(), 500, normed=True, histtype='step')
            plt.grid(True)
            plt.xlim([-np.abs(v_radial_surf_mean) - 4.*v_radial_surf_std, np.abs(v_radial_surf_mean) + 4.* v_radial_surf_std])
            plt.xlabel('Radial velocity [m/s]')
            plt.ylabel('PDF')
            plt.title('Surface velocity')

            if plot_save:
                plt.savefig(plot_path + os.sep + 'TRUE_radial_vel_hist.' + plot_format)
                plt.close()
            else:
                plt.show()

            plt.figure()
            plt.hist(v_radial_surf_ml.flatten(), 200, normed=True, histtype='step')
            #plt.hist(v_radial_surf_ml.flatten(), 500, normed=True, histtype='step')
            plt.grid(True)
            plt.xlim([-np.abs(v_radial_surf_mean) - 4.*v_radial_surf_std, np.abs(v_radial_surf_mean) + 4.* v_radial_surf_std])
            plt.xlabel('Radial velocity [m/s]')
            plt.ylabel('PDF')
            plt.title('Surface velocity (low pass filtered)')

            if plot_save:
                plt.savefig(plot_path + os.sep + 'TRUE_radial_vel_ml_hist.' + plot_format)
                plt.close()
            else:
                plt.show()

        if plot_vel:

            utils.image(v_radial_surf, aspect='equal', cmap=utils.bwr_cmap, extent=[0., rg_span, 0., az_span],
                        xlabel='Ground range [m]', ylabel='Azimuth [m]', title='Surface Radial Velocity', cbar_xlabel='[m/s]',
                        min=-np.abs(v_radial_surf_mean) - 4.*v_radial_surf_std, max=np.abs(v_radial_surf_mean) + 4.*v_radial_surf_std,
                        usetex=plot_tex, save=plot_save, save_path=plot_path + os.sep + 'TRUE_radial_vel.' + plot_format)
            utils.image(v_radial_surf_ml, aspect='equal', cmap=utils.bwr_cmap, extent=[0., rg_span, 0., az_span],
                        xlabel='Ground range [m]', ylabel='Azimuth [m]', title='Surface Radial Velocity', cbar_xlabel='[m/s]',
                        min=-np.abs(v_radial_surf_mean) - 4.*v_radial_surf_std, max=np.abs(v_radial_surf_mean) + 4.*v_radial_surf_std,
                        usetex=plot_tex, save=plot_save, save_path=plot_path + os.sep + 'TRUE_radial_vel_ml.' + plot_format)

        ##  ESTIMATED VELOCITIES

        # Note: plot limits are taken from surface calculations to keep the same ranges

        # ESTIMATE RADIAL VELOCITY
        v_radial_ests = -ati_phases/tau_ati/(k0*2.)

        # ESTIMATE HORIZONTAL VELOCITY
        v_horizo_ests = -ati_phases/tau_ati/(k0*2.)/np.sin(inc_angle)

        #Trim edges
        v_radial_ests = v_radial_ests[:, az_guard:-az_guard, 5:-5]
        v_horizo_ests = v_horizo_ests[:, az_guard:-az_guard, 5:-5]
        output.write('--------------------------------------------\n')
        output.write('ESTIMATED RADIAL VELOCITY - NO SMOOTHING\n')
        for pind in range(npol):
            output.write("%s Polarization\n" % polt[pind])
            output.write('MEAN(EST. V) = %.4f\n' % np.mean(v_radial_ests[pind]))
            output.write('STD(EST. V) = %.4f\n' % np.std(v_radial_ests[pind]))
        output.write('--------------------------------------------\n\n')

        output.write('--------------------------------------------\n')
        output.write('ESTIMATED RADIAL VELOCITY - SMOOTHING (WIN. SIZE=%dx%d)\n' % (az_ml, rg_ml))
        for pind in range(npol):
            output.write("%s Polarization\n" % polt[pind])
            output.write('MEAN(EST. V) = %.4f\n' % np.mean(utils.smooth(utils.smooth(v_radial_ests[pind],
                                                                                     rg_ml, axis=1),
                                                                        az_ml, axis=0)))
            output.write('STD(EST. V) = %.4f\n' % np.std(utils.smooth(utils.smooth(v_radial_ests[pind],
                                                                                   rg_ml, axis=1),
                                                                      az_ml, axis=0)))
        output.write('--------------------------------------------\n\n')

        output.write('--------------------------------------------\n')
        output.write('ESTIMATED HORIZONTAL VELOCITY - NO SMOOTHING\n')
        for pind in range(npol):
            output.write("%s Polarization\n" % polt[pind])
            output.write('MEAN(EST. V) = %.4f\n' % np.mean(v_horizo_ests[pind]))
            output.write('STD(EST. V) = %.4f\n' % np.std(v_horizo_ests[pind]))
        output.write('--------------------------------------------\n\n')

    # Processed NRCS

    NRCS_est_avg = 10*np.log10(np.mean(np.mean(i_all[:, az_guard:-az_guard, 5:-5], axis=-1), axis=-1))
    output.write('--------------------------------------------\n')
    for pind in range(npol):
        output.write("%s Polarization\n" % polt[pind])
        output.write('Estimated mean NRCS = %5.2f\n' % NRCS_est_avg[pind])
    output.write('--------------------------------------------\n\n')

    # Some bookkeeping information
    output.write('--------------------------------------------\n')
    output.write('GROUND RANGE GRID SPACING = %.4f\n' % grg_grid_spacing)
    output.write('AZIMUTH GRID SPACING = %.4f\n' % az_grid_spacing)
    output.write('--------------------------------------------\n\n')

    output.close()

    if plot_vel_hist and num_ch > 1:
        # PLOT RADIAL VELOCITY
        plt.figure()
        plt.hist(v_radial_surf.flatten(), 200, normed=True, histtype='step',
                 label='True')
        for pind in range(npol):
            plt.hist(v_radial_ests[pind].flatten(), 200, normed=True,
                     histtype='step', label=polt[pind])
        plt.grid(True)
        plt.xlim([-np.abs(v_radial_surf_mean) - 4.*v_radial_surf_std,
                  np.abs(v_radial_surf_mean) + 4.*v_radial_surf_std])
        plt.xlabel('Radial velocity [m/s]')
        plt.ylabel('PDF')
        plt.title('Estimated velocity')
        plt.legend()

        if plot_save:
            plt.savefig(plot_path + os.sep + 'ATI_radial_vel_hist.' + plot_format)
            plt.close()
        else:
            plt.show()

    # Save some statistics to npz file
    #
    if num_ch > 1:
        filenpz = os.path.join(os.path.dirname(output_file), 'ati_stats.npz')
        # Mean coh
        cohs = np.array(cohs)[:, az_guard:-az_guard, 5:-5]

        np.savez(filenpz,
                 nrcs=NRCS_est_avg,
                 v_r_dop=np.mean(np.mean(v_radial_ests, axis=-1), axis=-1),
                 v_r_surf = v_radial_surf_mean,
                 v_r_surf_std = v_radial_surf_std,
                 coh_mean= np.mean(np.mean(cohs, axis=-1), axis=-1),
                 abscoh_mean=np.mean(np.mean(np.abs(cohs), axis=-1), axis=-1),
                 coh_lut=coh_lut,
                 pols=polt)
    print('----------------------------------------')
    print(time.strftime("ATI Processing finished [%Y-%m-%d %H:%M:%S]", time.localtime()))
    print('----------------------------------------')
示例#5
0
def delta_k_processing(raw_output_file, cfg_file):
    # heading
    # flush is true is for correctly showing
    print(
        '-------------------------------------------------------------------',
        flush=True)
    print('Delta-k processing begins...')
    # parameters
    cfg = tpio.ConfigFile(cfg_file)
    pp_file = os.path.join(cfg.sim.path, cfg.sim.pp_file)
    n_sar_a = cfg.processing.n_sar
    Azi_img = cfg.processing.Azi_img
    # radar
    f0 = cfg.radar.f0
    prf = cfg.radar.prf
    # num_ch = cfg.radar.num_ch
    alt = cfg.radar.alt
    v_ground = cfg.radar.v_ground
    wei_dop = 0
    m_ind = 0

    # CALCULATE PARAMETERS
    l0 = const.c / f0
    if v_ground == 'auto':
        v_ground = geo.orbit_to_vel(alt, ground=True)

    if Azi_img:  # 2-D unfocusing
        data = np.load(pp_file)
        Doppler_av = np.mean(data['dop_pp_avg'])
        dp_axis = np.linspace(-prf / 2, prf / 2, n_sar_a)
        val_d = np.abs(dp_axis - Doppler_av)
        m_d = np.where(val_d == min(val_d))
        m_ind = np.int(m_d[0])
        f_in = prf / n_sar_a
        beamwide = l0 / cfg.radar.ant_L
        Bw = 2 * v_ground / l0 * beamwide
        Bw_eff = np.abs(Bw * np.sin(cfg.radar.azimuth))
        wei_dop = Bw_eff / f_in

    # Analyse different waves
    sim_path_ref = cfg.sim.path + os.sep + 'wavelength%.1f'
    for inn in range(np.size(cfg.processing.wave_scale)):
        wave_scale = cfg.processing.wave_scale[inn]
        path_p = sim_path_ref % (wave_scale)
        if not (os.path.exists(path_p) | Azi_img):
            os.makedirs(path_p)
        else:
            sim_path_ref = cfg.sim.path + os.sep + 'wavelength%.1f_unfocus'
            path_p = sim_path_ref % (wave_scale)
            if not os.path.exists(path_p):
                os.makedirs(path_p)

        # imaging signs
        plot_pattern = False
        plot_spectrum = False

        # processing parameters and initial parameters
        # radar parameters
        Az_smaples = cfg.radar.n_pulses
        PRF = cfg.radar.prf
        fs = cfg.radar.Fs
        R_samples = cfg.radar.n_rg
        inc = cfg.radar.inc_angle
        n_sar_r = cfg.processing.n_sar_r
        R_n = round(cfg.ocean.Lx * cfg.ocean.dx * np.sin(inc * np.pi / 180) /
                    (const.c / 2 / fs))
        rang_img = cfg.processing.rang_img
        analysis_deltan = np.linspace(0, 500,
                                      501)  # for delta-k spectrum analysis
        r_int_num = cfg.processing.r_int_num
        az_int = cfg.processing.az_int
        Delta_lag = cfg.processing.Delta_lag
        num_az = cfg.processing.num_az
        list = range(1, num_az)
        analyse_num = range(2, Az_smaples - az_int - 1 - num_az)
        Scene_scope = ((R_samples - 1) * const.c / fs / 2) / 2
        k_w = 2 * np.pi / wave_scale
        # initialization parameters
        dk_higha = np.zeros((np.size(analyse_num), r_int_num), dtype=np.float)
        # RCS_power = np.zeros_like(analysis_deltan)

        # get raw data
        raw_data = raw_data_extraction(raw_output_file)
        raw_data = raw_data[0]

        # showing pattern of the ocean waves
        # intensity
        raw_int = raw_data * np.conj(raw_data)
        if plot_pattern:
            plt.figure()
            plt.imshow(np.abs(raw_int))
            plt.xlabel("Range (pixel)")
            plt.ylabel("Azimuth (pixel)")

            plot_path = cfg.sim.path + os.sep + 'delta_k_spectrum_plots'

            if not os.path.exists(plot_path):
                os.makedirs(plot_path)
            plt.savefig(os.path.join(plot_path, 'Pattern.png'))
            plt.close()
        intens_spectrum = np.mean(np.fft.fft(np.abs(raw_int), axis=1), axis=0)
        if plot_spectrum:
            plt.figure()
            plt.plot(
                10 *
                np.log10(np.abs(intens_spectrum[1:np.int(R_samples / 2)])))
            plt.xlabel("Delta_k [1/m]")
            plt.ylabel("Power (dB)")

        # Showing Delta-k spectrum for analysis
        delta_k_spectrum(Scene_scope, r_int_num, inc, raw_data,
                         analysis_deltan, path_p)
        # Calculating the required delta_f value
        delta_f = cal_delta_f(inc, wave_scale)
        delta_k = delta_f / const.c
        ind_N = np.int(round(delta_k * (2 * Scene_scope) * 2))
        # sar processing parameters transfor
        s_r, r_int_num, ind_N, data_rshp, s_a, az_int, analyse_num, n_sar_a = Transform(
            raw_data, R_samples, n_sar_r, r_int_num, Az_smaples, ind_N, az_int,
            n_sar_a, analyse_num, rang_img, Azi_img)
        # initialization parameters
        dk_higha = np.zeros((np.size(analyse_num), r_int_num), dtype=np.float)
        #  = np.zeros_like(analysis_deltan)
        Omiga_p = np.zeros((np.size(analyse_num), np.size(list) + 1),
                           dtype=np.float)
        Omiga_p_z = np.zeros(np.size(analyse_num), dtype=np.float)
        Phase_p = np.zeros((np.size(analyse_num), np.size(list) + 1),
                           dtype=np.float)
        Phase_p_z = np.zeros(np.size(analyse_num), dtype=np.float)

        # Delta-k processing
        if rang_img & Azi_img:  # 2-D unfocusing
            for ind_x in range(s_r):
                r_data = data_rshp[:, ind_x, :]
                spck_f = np.fft.fftshift(np.fft.fft(r_data, axis=1),
                                         axes=(1, ))
                dk_high = spck_f[:, 0:r_int_num] * np.conj(
                    spck_f[:, ind_N:ind_N + r_int_num])
                dk_higha = np.mean(dk_high, axis=1)
                # azimuth sar azimuth processing
                dimsin = dk_higha.shape
                int_unfcs, data_a = unfocused_sar(dk_higha, n_sar_a)
                data_ufcs = np.fft.fftshift(data_a, axes=(1, ))

                for ind in range(np.size(analyse_num)):
                    (Omiga_p_z[ind], Phase_p_z[ind]) = Cal_pha_vel(
                        data_ufcs[analyse_num[ind]:analyse_num[ind] +
                                  az_int, :], data_ufcs[0:az_int, :],
                        analyse_num[ind], PRF, k_w, n_sar_a, m_ind, wei_dop,
                        Azi_img)
                Omiga_p_z[ind] = Omiga_p_z[ind] + Omiga_p_z[ind]
                Phase_p_z[ind] = Phase_p_z[ind] + Phase_p_z[ind]
            Omiga_p_z = Omiga_p_z / s_r
            Phase_p_z = Phase_p_z / s_r

        elif rang_img:  # range unfocusing
            for ind_x in range(s_r):
                r_data = data_rshp[:, ind_x, :]
                spck_f = np.fft.fftshift(np.fft.fft(r_data, axis=1),
                                         axes=(1, ))
                dk_high = spck_f[:, 0:r_int_num] * np.conj(
                    spck_f[:, ind_N:ind_N + r_int_num])
                dk_higha = np.mean(dk_high, axis=1)
                for ind in range(np.size(analyse_num)):
                    (Omiga_p_z[ind], Phase_p_z[ind]) = Cal_pha_vel(
                        dk_higha[analyse_num[ind]:analyse_num[ind] + az_int],
                        dk_higha[0:az_int], analyse_num[ind], PRF, k_w,
                        n_sar_a, m_ind, wei_dop, Azi_img)
                Omiga_p_z[ind] = Omiga_p_z[ind] + Omiga_p_z[ind]
                Phase_p_z[ind] = Phase_p_z[ind] + Phase_p_z[ind]
            Omiga_p_z = Omiga_p_z / s_r
            Phase_p_z = Phase_p_z / s_r
        elif Azi_img:  # azimuth unfocusing
            spck_f = np.fft.fftshift(np.fft.fft(raw_data, axis=1), axes=(1, ))
            dk_high = spck_f[:, 0:r_int_num] * np.conj(
                spck_f[:, ind_N:ind_N + r_int_num])
            dk_higha = np.mean(dk_high, axis=1)
            # azimuth sar azimuth processing
            int_unfcs, data_a = unfocused_sar(dk_higha, n_sar_a)
            data_ufcs = np.fft.fftshift(data_a, axes=(1, ))

            # Estimate phase
            for ind in range(np.size(analyse_num)):
                (Omiga_p_z[ind], Phase_p_z[ind]) = Cal_pha_vel(
                    data_ufcs[analyse_num[ind]:analyse_num[ind] + az_int, :],
                    data_ufcs[0:az_int, :], analyse_num[ind], PRF, k_w,
                    n_sar_a, m_ind, wei_dop, Azi_img)
        # Considering delta-k processing with lags
        elif Delta_lag:
            spck_f = np.fft.fftshift(np.fft.fft(raw_data, axis=1), axes=(1, ))
            for iii in list:
                for ind in range(np.size(analyse_num)):
                    if iii == 0:
                        dk_inda = spck_f[iii:, 0:r_int_num] * np.conj(
                            spck_f[0:, ind_N:ind_N + r_int_num])
                    else:
                        dk_inda = spck_f[iii:, 0:r_int_num] * np.conj(
                            spck_f[0:-iii, ind_N:ind_N + r_int_num])
                    dk_higha = np.mean(dk_inda, axis=1)
                    (Omiga_p[ind, iii], Phase_p[ind, iii]) = Cal_pha_vel(
                        dk_higha[analyse_num[ind]:analyse_num[ind] + az_int],
                        dk_higha[0:az_int], analyse_num[ind], PRF, k_w,
                        n_sar_a, m_ind, wei_dop, Azi_img)

        else:  # none unfocusing
            spck_f = np.fft.fftshift(np.fft.fft(raw_data, axis=1), axes=(1, ))

            # Extracting the information of wave_scale
            dk_high = spck_f[:, 0:r_int_num] * np.conj(
                spck_f[:, ind_N:ind_N + r_int_num])
            dk_higha = np.mean(dk_high, axis=1)
            for ind in range(np.size(analyse_num)):
                (Omiga_p_z[ind], Phase_p_z[ind]) = Cal_pha_vel(
                    dk_higha[analyse_num[ind]:analyse_num[ind] + az_int],
                    dk_higha[0:az_int], analyse_num[ind], PRF, k_w, n_sar_a,
                    m_ind, wei_dop, Azi_img)

        # processing results
        if Azi_img:
            analyse_num = np.array(analyse_num) * n_sar_a
        plt.figure()
        plt.plot(analyse_num, Omiga_p_z)
        plt.xlabel("Azimuth interval [Pixel]")
        plt.ylabel("Angular velocity (rad/s)")

        plot_path = path_p + os.sep + 'delta_k_spectrum_plots'
        if not os.path.exists(path_p):
            os.makedirs(path_p)
        plt.savefig(os.path.join(plot_path, 'Angular_velocity.png'))
        plt.close()

        plt.figure()
        plt.plot(analyse_num, Phase_p_z)
        plt.xlabel("Azimuth interval [Pixel]")
        plt.ylabel("Phase velocity (m/s)")

        plt.savefig(os.path.join(plot_path, 'Phase_velocity.png'))
        plt.close()
        # save the result data
        if Delta_lag:  # lag case
            Omiga_f = Omiga_p[:, 0]
            Phase_f = Phase_p[:, 0]
        else:  # normal case
            Omiga_f = Omiga_p_z
            Phase_f = Phase_p_z
        Omiga_s = Omiga_f
        Phase_s = Phase_f

        np.save(
            os.path.join(plot_path, 'Angular_velocity.npy'),
            [Omiga_s, np.mean(Omiga_s),
             np.size(Omiga_s), analyse_num])

        np.save(
            os.path.join(plot_path, 'Phase_velocity.npy'),
            [Phase_s, np.mean(Phase_s),
             np.size(Phase_s), analyse_num])
示例#6
0
def skim_process(cfg_file, raw_output_file):

    ###################
    # INITIALIZATIONS #
    ###################

    # CONFIGURATION FILE
    cfg = tpio.ConfigFile(cfg_file)
    info = utils.PrInfo(cfg.sim.verbosity, "processor")
    # Say hello
    info.msg(time.strftime("Starting: %Y-%m-%d %H:%M:%S", time.localtime()))
    # PROCESSING
    az_weighting = cfg.processing.az_weighting
    doppler_bw = cfg.processing.doppler_bw
    plot_format = cfg.processing.plot_format
    plot_tex = cfg.processing.plot_tex
    plot_save = cfg.processing.plot_save
    plot_path = cfg.processing.plot_path
    plot_raw = cfg.processing.plot_raw
    plot_rcmc_dopp = cfg.processing.plot_rcmc_dopp
    plot_rcmc_time = cfg.processing.plot_rcmc_time
    plot_image_valid = cfg.processing.plot_image_valid
    doppler_demod = cfg.processing.doppler_demod
    pp_file = os.path.join(cfg.sim.path, cfg.sim.pp_file)

    # radar
    f0 = cfg.radar.f0
    prf = cfg.radar.prf
    # num_ch = cfg.radar.num_ch
    alt = cfg.radar.alt
    v_ground = cfg.radar.v_ground

    # CALCULATE PARAMETERS
    l0 = const.c / f0
    if v_ground == 'auto':
        v_ground = geo.orbit_to_vel(alt, ground=True)
    rg_sampling = cfg.radar.Fs
    # Range freqency axis
    f_axis = np.linspace(0, rg_sampling, cfg.radar.n_rg)
    wavenum_scale = f_axis * 4 * np.pi / const.c * np.sin(
        np.radians(cfg.radar.inc_angle))

    # RAW DATA
    raw_file = tpio.RawFile(raw_output_file, 'r')
    raw_data = raw_file.get('raw_data*')
    info.msg("Raw data max: %f" % (np.max(np.abs(raw_data))))
    dop_ref = raw_file.get('dop_ref')
    sr0 = raw_file.get('sr0')
    azimuth = raw_file.get('azimuth')
    raw_file.close()
    #n_az

    # OTHER INITIALIZATIONS
    # Create plots directory
    plot_path = os.path.dirname(pp_file) + os.sep + plot_path
    if plot_save:
        if not os.path.exists(plot_path):
            os.makedirs(plot_path)

    ########################
    # PROCESSING MAIN LOOP #
    ########################

    # Optimize matrix sizes
    az_size_orig, rg_size_orig = raw_data[0].shape
    optsize = utils.optimize_fftsize(raw_data[0].shape)
    # optsize = [optsize[0], optsize[1]]
    data = np.zeros(optsize, dtype=complex)
    data[0:az_size_orig, 0:rg_size_orig] = raw_data[0]
    # Doppler demodulation according to geometric Doppler
    if doppler_demod:
        info.msg("Doppler demodulation")
        t_vec = (np.arange(optsize[0]) / prf).reshape((optsize[0], 1))
        data[:, 0:rg_size_orig] = (data[:, 0:rg_size_orig] * np.exp(
            (-2j * np.pi) * t_vec * dop_ref.reshape((1, rg_size_orig))))
    # Pulse pair
    info.msg("Range over-sampling")
    data_ovs = range_oversample(data)
    info.msg("Pulse-pair processing")
    dop_pp_avg, dop_pha_avg, coh = pulse_pair(
        data_ovs[0:az_size_orig, 0:2 * rg_size_orig], prf)
    krv, mean_int_profile, int_spe, phase_spec = rar_spectra(
        data_ovs[0:az_size_orig, 0:2 * rg_size_orig],
        2 * rg_sampling,
        rgsmth=8)
    kxv = krv * np.sin(np.radians(cfg.radar.inc_angle))
    range_spectrum(data[0:az_size_orig, 0:rg_size_orig], rg_sampling,
                   plot_path)
    info.msg("Mean DCA (pulse-pair average): %f Hz" % (np.mean(dop_pp_avg)))
    info.msg("Mean DCA (pulse-pair phase average): %f Hz" %
             (np.mean(dop_pha_avg)))
    info.msg("Mean coherence: %f " % (np.mean(coh)))

    # Unfocused SAR
    info.msg("Unfocused SAR")
    int_unfcs, data_ufcs = unfocused_sar(
        data_ovs[0:az_size_orig, 0:2 * rg_size_orig], cfg.processing.n_sar)
    krv2, sar_int_spec = sar_spectra(int_unfcs, 2 * rg_sampling, rgsmth=8)

    # Some delta-k on focused sar data
    info.msg("Unfocused SAR delta-k spectrum")
    dkr, dk_avg, dk_signal = sar_delta_k(data_ufcs, 2 * rg_sampling, dksmoth=8)
    dk_pulse_pairs, dk_omega = sar_delta_k_omega(dk_signal,
                                                 cfg.processing.n_sar / prf,
                                                 dksmoth=16)
    # For verification, comment out
    # dkr_sl, dk_avg_sl, dk_signal_sl = sar_delta_k_slow(data_ufcs, 2 * rg_sampling, dksmoth=8)
    # dk_pulse_pairs_sl, dk_omega_sl = sar_delta_k_omega(dk_signal_sl, cfg.processing.n_sar/prf, dksmoth=32)
    info.msg(
        time.strftime("Processing done [%Y-%m-%d %H:%M:%S]", time.localtime()))

    if plot_raw:
        plt.figure()
        plt.imshow(np.real(raw_data[0]),
                   vmin=-np.max(np.abs(raw_data[0])),
                   vmax=np.max(np.abs(raw_data[0])),
                   origin='lower',
                   aspect=np.float(raw_data[0].shape[1]) /
                   np.float(raw_data[0].shape[0]),
                   cmap='viridis')
        #plt.title()
        plt.xlabel("Range [samples]")
        plt.ylabel("Azimuth [samples")
        plt.savefig(plot_path + os.sep + 'plot_raw_real.%s' % plot_format,
                    dpi=150)
        plt.close()
        plt.figure()
        plt.imshow(np.abs(raw_data[0]),
                   vmin=0,
                   vmax=np.max(np.abs(raw_data[0])),
                   origin='lower',
                   aspect=np.float(raw_data[0].shape[1]) /
                   np.float(raw_data[0].shape[0]),
                   cmap='viridis')
        #plt.title()
        plt.xlabel("Range [samples]")
        plt.ylabel("Azimuth [samples")
        plt.savefig(plot_path + os.sep + 'plot_raw_abs.%s' % plot_format,
                    dpi=150)
        plt.close()

    plt.figure()
    plt.imshow(np.fft.fftshift(int_unfcs, axes=(0, )),
               origin='lower',
               aspect='auto',
               cmap='viridis')
    # plt.title()
    plt.xlabel("Range [samples]")
    plt.ylabel("Doppler [samples")
    plt.savefig(plot_path + os.sep + 'ufcs_int.%s' % plot_format, dpi=150)
    plt.close()

    plt.figure()
    plt.plot(mean_int_profile)
    plt.xlabel("Range samples [Pixels]")
    plt.ylabel("Intensity")
    plt.savefig(plot_path + os.sep + 'mean_int.%s' % plot_format)
    plt.close()

    plt.figure()
    plt.plot(np.fft.fftshift(kxv), np.fft.fftshift(int_spe))
    plt.ylim((int_spe[20:np.int(cfg.radar.n_rg) - 20].min() / 2,
              int_spe[20:np.int(cfg.radar.n_rg) - 20].max() * 1.5))
    plt.xlim((0, kxv.max()))
    plt.xlabel("$k_x$ [rad/m]")
    plt.ylabel("$S_I$")
    plt.savefig(plot_path + os.sep + 'int_spec.%s' % plot_format)
    plt.close()

    plt.figure()
    plt.plot(np.fft.fftshift(kxv), np.fft.fftshift(sar_int_spec))
    plt.ylim((sar_int_spec[20:np.int(cfg.radar.n_rg) - 20].min() / 2,
              sar_int_spec[20:np.int(cfg.radar.n_rg) - 20].max() * 1.5))
    plt.xlim((0, kxv.max()))
    plt.xlabel("$k_x$ [rad/m]")
    plt.ylabel("$S_I$")
    plt.savefig(plot_path + os.sep + 'sar_int_spec.%s' % plot_format)
    plt.close()

    plt.figure()
    plt.plot(np.fft.fftshift(kxv), np.fft.fftshift(phase_spec))
    plt.ylim((phase_spec[20:np.int(cfg.radar.n_rg) - 20].min() / 2,
              phase_spec[20:np.int(cfg.radar.n_rg) - 20].max() * 1.5))
    plt.xlabel("$k_x$ [rad/m]")
    plt.xlim((0, kxv.max()))
    plt.ylabel("$S_{Doppler}$")
    plt.savefig(plot_path + os.sep + 'pp_phase_spec.%s' % plot_format)

    plt.figure()
    dkx = dkr * np.sin(np.radians(cfg.radar.inc_angle))
    plt.plot(dkx, dk_avg)
    plt.ylim((dk_avg[20:dkx.size - 20].min() / 2,
              dk_avg[20:dkx.size - 20].max() * 1.5))
    plt.xlim((0.1, dkx.max()))
    plt.xlabel("$\Delta k_x$ [rad/m]")
    plt.ylabel("$S_I$")
    plt.savefig(plot_path + os.sep + 'sar_delta_k_spec.%s' % plot_format)
    plt.close()

    # plt.figure()
    # dkx_sl = dkr_sl * np.sin(np.radians(cfg.radar.inc_angle))
    # plt.plot(dkx_sl, dk_avg_sl)
    # plt.ylim((dk_avg_sl[20:dkx.size-20].min()/2,
    #           dk_avg_sl[20:dkx.size-20].max()*1.5))
    # plt.xlim((0, dkx.max()))
    # plt.xlabel("$\Delta k_x$ [rad/m]")
    # plt.ylabel("$S_I$")
    # plt.savefig(plot_path + os.sep + 'sar_delta_k_spec_slow.%s' % plot_format)
    # plt.close()

    plt.figure()
    # plt.plot(dkx, dk_omega[0], label="lag=1")
    plt.plot(dkx, dk_omega[1], label="lag=2")
    # plt.plot(dkx, dk_omega_sl[0] + 1, label="slow-lag=1")
    # plt.plot(dkx, dk_omega_sl[1] + 1, label="slow-lag=2")
    plt.plot(dkx, dk_omega[3], label="lag=4")
    # plt.plot(dkx, dk_omega[-1], label="lag=max")
    plt.ylim((-20, 20))
    #plt.ylim((dk_avg[20:dkx.size - 20].min() / 2,
    #          dk_avg[20:dkx.size - 20].max() * 1.5))
    plt.xlim((0.05, 0.8))
    plt.xlabel("$\Delta k_x$ [rad/m]")
    plt.ylabel("$\omega$ [rad/s]")
    plt.legend(loc=0)
    plt.savefig(plot_path + os.sep + 'sar_delta_k_omega.%s' % plot_format)
    plt.close()

    info.msg("Saving output to %s" % pp_file)
    np.savez(pp_file,
             dop_pp_avg=dop_pp_avg,
             dop_pha_avg=dop_pha_avg,
             coh=coh,
             ufcs_intensity=int_unfcs,
             mean_int_profile=mean_int_profile,
             int_spec=int_spe,
             sar_int_spec=sar_int_spec,
             ppphase_spec=phase_spec,
             kx=kxv,
             dk_spec=dk_avg,
             dk_omega=dk_omega,
             dkx=dkx)

    info.msg(time.strftime("All done [%Y-%m-%d %H:%M:%S]", time.localtime()))
示例#7
0
    def __init__(self,
                 cfg_file=None,
                 ntimes=2,
                 t_step=10e-3,
                 pol='DP',
                 winddir=0,
                 U10=None,
                 po_model=None):
        """ This function generates a (short) time series of surface realizations.

                :param scf_file: the full path to the configuration with all OCEANSAR parameters
                :param ntimes: number of time samples generated.
                :param t_step: spacing between time samples. This can be interpreted as the Pulse Repetition Interval
                :param wind:dir: to force wind direction
                :param U10: to force wind force
                :param po_model: one of None, spa (stationary phase approximation, or facet approach)

                :returns: a tuple with the configuration object, the surfaces, the radial velocities for each grid point,
                      and the complex scattering coefficients
        """
        cfg_file = utils.get_parFile(parfile=cfg_file)
        cfg = ocs_io.ConfigFile(cfg_file)
        self.cfg = cfg
        self.use_hmtf = cfg.srg.use_hmtf
        self.scat_spec_enable = cfg.srg.scat_spec_enable
        if po_model is None:
            self.scat_spec_mode = cfg.srg.scat_spec_mode
        else:
            self.scat_spec_mode = po_model
        self.scat_bragg_enable = cfg.srg.scat_bragg_enable
        self.scat_bragg_model = cfg.srg.scat_bragg_model
        self.scat_bragg_d = cfg.srg.scat_bragg_d
        self.scat_bragg_spec = cfg.srg.scat_bragg_spec
        self.scat_bragg_spread = cfg.srg.scat_bragg_spread

        # SAR
        try:
            radcfg = cfg.sar
        except AttributeError:
            radcfg = cfg.radar
        self.inc_angle = np.deg2rad(radcfg.inc_angle)
        self.alt = radcfg.alt
        self.f0 = radcfg.f0
        self.prf = radcfg.prf
        if pol is None:
            self.pol = radcfg.pol
        else:
            self.pol = pol

        l0 = const.c / self.f0
        k0 = 2. * np.pi * self.f0 / const.c
        if self.pol == 'DP':
            self.do_hh = True
            self.do_vv = True
        elif self.pol == 'hh':
            self.do_hh = True
            self.do_vv = False
        else:
            self.do_hh = False
            self.do_vv = True
        # OCEAN / OTHERS
        ocean_dt = cfg.ocean.dt
        self.surface = OceanSurface()
        compute = ['D', 'Diff', 'Diff2', 'V']
        print("Initializating surface")
        if winddir is not None:
            self.wind_dir = np.radians(winddir)
        else:
            self.wind_dir = np.deg2rad(cfg.ocean.wind_dir)
        if U10 is None:
            self.wind_u = cfg.ocean.wind_U
        else:
            self.wind_u = U10
        if self.use_hmtf:
            compute.append('hMTF')
        self.surface.init(cfg.ocean.Lx,
                          cfg.ocean.Ly,
                          cfg.ocean.dx,
                          cfg.ocean.dy,
                          cfg.ocean.cutoff_wl,
                          cfg.ocean.spec_model,
                          cfg.ocean.spread_model,
                          self.wind_dir,
                          cfg.ocean.wind_fetch,
                          self.wind_u,
                          cfg.ocean.current_mag,
                          np.deg2rad(cfg.ocean.current_dir),
                          cfg.ocean.swell_enable,
                          cfg.ocean.swell_ampl,
                          np.deg2rad(cfg.ocean.swell_dir),
                          cfg.ocean.swell_wl,
                          compute,
                          cfg.ocean.opt_res,
                          cfg.ocean.fft_max_prime,
                          choppy_enable=cfg.ocean.choppy_enable)
        # Get a surface realization calculated
        print("Computing surface realizations")
        self.surface.t = 0
        self.ntimes = ntimes
        self.diffx = np.zeros((ntimes, self.surface.Ny, self.surface.Nx))
        self.diffy = np.zeros((ntimes, self.surface.Ny, self.surface.Nx))
        self.diffxx = np.zeros((ntimes, self.surface.Ny, self.surface.Nx))
        self.diffxy = np.zeros((ntimes, self.surface.Ny, self.surface.Nx))
        self.diffyy = np.zeros((ntimes, self.surface.Ny, self.surface.Nx))
        self.dx = np.zeros((ntimes, self.surface.Ny, self.surface.Nx))
        self.dy = np.zeros((ntimes, self.surface.Ny, self.surface.Nx))
        self.dz = np.zeros((ntimes, self.surface.Ny, self.surface.Nx))
        self.diffx[0, :, :] = self.surface.Diffx
        self.diffy[0, :, :] = self.surface.Diffy
        self.diffxx[0, :, :] = self.surface.Diffxx
        self.diffyy[0, :, :] = self.surface.Diffyy
        self.diffxy[0, :, :] = self.surface.Diffxy
        self.dx[0, :, :] = self.surface.Dx
        self.dy[0, :, :] = self.surface.Dy
        self.dz[0, :, :] = self.surface.Dz
        if self.use_hmtf:
            self.h_mtf = np.zeros((ntimes, self.surface.Ny, self.surface.Nx))
            self.h_mtf[0, :, :] = self.surface.hMTF
        self.t_step = t_step
        for az_step in range(1, ntimes):
            t_now = az_step * t_step
            self.surface.t = t_now
            self.diffx[az_step, :, :] = self.surface.Diffx
            self.diffy[az_step, :, :] = self.surface.Diffy
            self.diffxx[az_step, :, :] = self.surface.Diffxx
            self.diffyy[az_step, :, :] = self.surface.Diffyy
            self.diffxy[az_step, :, :] = self.surface.Diffxy
            self.dx[az_step, :, :] = self.surface.Dx
            self.dy[az_step, :, :] = self.surface.Dy
            self.dz[az_step, :, :] = self.surface.Dz
            if self.use_hmtf:
                self.h_mtf[az_step, :, :] = self.surface.hMTF
        self.inc_is_set = False
示例#8
0
def fastraw(cfg_file, output_file, ocean_file, reuse_ocean_file, errors_file, reuse_errors_file,
            plot_save=True):

    ###################
    # INITIALIZATIONS #
    ###################

    ## MPI SETUP
    comm = MPI.COMM_WORLD
    size, rank = comm.Get_size(), comm.Get_rank()

    ## WELCOME
    if rank == 0:
        print('-------------------------------------------------------------------')
        print(time.strftime("- OCEANSAR FAST RAW SAR GENERATOR: %Y-%m-%d %H:%M:%S", time.localtime()))
        print('-------------------------------------------------------------------')

    ## CONFIGURATION FILE
    # Note: variables are 'copied' to reduce code verbosity
    cfg = tpio.ConfigFile(cfg_file)

    # RAW
    wh_tol = cfg.srg.wh_tol
    nesz = cfg.srg.nesz
    use_hmtf = cfg.srg.use_hmtf
    scat_spec_enable = cfg.srg.scat_spec_enable
    scat_spec_mode = cfg.srg.scat_spec_mode
    scat_bragg_enable = cfg.srg.scat_bragg_enable
    scat_bragg_model = cfg.srg.scat_bragg_model
    scat_bragg_d = cfg.srg.scat_bragg_d
    scat_bragg_spec = cfg.srg.scat_bragg_spec
    scat_bragg_spread = cfg.srg.scat_bragg_spread

    # SAR
    inc_angle = np.deg2rad(cfg.sar.inc_angle)
    f0 = cfg.sar.f0
    pol = cfg.sar.pol
    squint_r = np.degrees(cfg.sar.squint)
    if pol == 'DP':
        do_hh = True
        do_vv = True
    elif pol == 'hh':
        do_hh = True
        do_vv = False
    else:
        do_hh = False
        do_vv = True

    prf = cfg.sar.prf
    num_ch = int(cfg.sar.num_ch)
    ant_l = cfg.sar.ant_L
    alt = cfg.sar.alt
    v_ground = cfg.sar.v_ground
    rg_bw = cfg.sar.rg_bw
    over_fs = cfg.sar.over_fs
    sigma_n_tx = cfg.sar.sigma_n_tx
    phase_n_tx = np.deg2rad(cfg.sar.phase_n_tx)
    sigma_beta_tx = cfg.sar.sigma_beta_tx
    phase_beta_tx = np.deg2rad(cfg.sar.phase_beta_tx)
    sigma_n_rx = cfg.sar.sigma_n_rx
    phase_n_rx = np.deg2rad(cfg.sar.phase_n_rx)
    sigma_beta_rx = cfg.sar.sigma_beta_rx
    phase_beta_rx = np.deg2rad(cfg.sar.phase_beta_rx)

    # OCEAN / OTHERS
    ocean_dt = cfg.ocean.dt

    add_point_target = False
    use_numba = True
    n_sinc_samples = 8
    sinc_ovs = 20
    chan_sinc_vec = raw.calc_sinc_vec(n_sinc_samples, sinc_ovs, Fs=over_fs)
    # OCEAN SURFACE

    print('Initializing ocean surface...')

    surface = OceanSurface()
    # Setup compute values
    compute = ['D', 'Diff', 'Diff2']
    if use_hmtf:
        compute.append('hMTF')

    # Try to reuse initialized surface
    if reuse_ocean_file:
        try:
            surface.load(ocean_file, compute)
        except RuntimeError:
            pass

    if (not reuse_ocean_file) or (not surface.initialized):

        if hasattr(cfg.ocean, 'use_buoy_data'):
            if cfg.ocean.use_buoy_data:
                bdataf = cfg.ocean.buoy_data_file
                date = datetime.datetime(np.int(cfg.ocean.year),
                                         np.int(cfg.ocean.month),
                                         np.int(cfg.ocean.day),
                                         np.int(cfg.ocean.hour),
                                         np.int(cfg.ocean.minute), 0)
                date, bdata = tpio.load_buoydata(bdataf, date)
                buoy_spec = tpio.BuoySpectra(bdata, heading=cfg.sar.heading, depth=cfg.ocean.depth)
                dirspectrum_func = buoy_spec.Sk2
                # Since the wind direction is included in the buoy data
                wind_dir = 0
            else:
                dirspectrum_func = None
                wind_dir = np.deg2rad(cfg.ocean.wind_dir)
        else:
            dirspectrum_func = None
            wind_dir = np.deg2rad(cfg.ocean.wind_dir)

        surface.init(cfg.ocean.Lx, cfg.ocean.Ly, cfg.ocean.dx,
                     cfg.ocean.dy, cfg.ocean.cutoff_wl,
                     cfg.ocean.spec_model, cfg.ocean.spread_model,
                     wind_dir,
                     cfg.ocean.wind_fetch, cfg.ocean.wind_U,
                     cfg.ocean.current_mag,
                     np.deg2rad(cfg.ocean.current_dir),
                     cfg.ocean.dir_swell_dir,
                     cfg.ocean.freq_r, cfg.ocean.sigf,
                     cfg.ocean.sigs, cfg.ocean.Hs,
                     cfg.ocean.swell_dir_enable,
                     cfg.ocean.swell_enable, cfg.ocean.swell_ampl,
                     np.deg2rad(cfg.ocean.swell_dir),
                     cfg.ocean.swell_wl,
                     compute, cfg.ocean.opt_res,
                     cfg.ocean.fft_max_prime,
                     choppy_enable=cfg.ocean.choppy_enable,
                     depth=cfg.ocean.depth,
                     dirspectrum_func=dirspectrum_func)

        surface.save(ocean_file)
        # Now we plot the directional spectrum
        # self.wave_dirspec[good_k] = dirspectrum_func(self.kx[good_k], self.ky[good_k])
        plt.figure()
        plt.imshow(np.fft.fftshift(surface.wave_dirspec),
                   extent=[surface.kx.min(), surface.kx.max(),
                           surface.ky.min(), surface.ky.max()],
                   origin='lower',
                   cmap='inferno_r')

        plt.grid(True)
        pltax = plt.gca()
        pltax.set_xlim((-0.1, 0.1))
        pltax.set_ylim((-0.1, 0.1))
        narr_length = 0.08 # np.min([surface_full.kx.max(), surface_full.ky.max()])
        pltax.arrow(0, 0,
                    -narr_length * np.sin(np.radians(cfg.sar.heading)),
                    narr_length * np.cos(np.radians(cfg.sar.heading)),
                    fc="k", ec="k")
        plt.xlabel('$k_x$ [rad/m]')
        plt.ylabel('$k_y$ [rad/m]')
        plt.colorbar()
        #plt.show()
        # Create plots directory
        plot_path = os.path.dirname(output_file) + os.sep + 'raw_plots'
        if plot_save:
            if not os.path.exists(plot_path):
                os.makedirs(plot_path)

        plt.savefig(os.path.join(plot_path, 'input_dirspectrum.png'))
        plt.close()

    # CALCULATE PARAMETERS
    if rank == 0:
        print('Initializing simulation parameters...')

    # SR/GR/INC Matrixes
    sr0 = geosar.inc_to_sr(inc_angle, alt)
    gr0 = geosar.inc_to_gr(inc_angle, alt)
    gr = surface.x + gr0
    sr, inc, _ = geosar.gr_to_geo(gr, alt)
    sr -= np.min(sr)

    inc = inc.reshape(1, inc.size)
    sr = sr.reshape(1, sr.size)
    gr = gr.reshape(1, gr.size)
    sin_inc = np.sin(inc)
    cos_inc = np.cos(inc)

    # lambda, K, resolution, time, etc.
    l0 = const.c/f0
    k0 = 2.*np.pi*f0/const.c
    sr_res = const.c/(2.*rg_bw)
    if cfg.sar.L_total:
        ant_l = ant_l/np.float(num_ch)
        d_chan = ant_l
    else:
        if np.float(cfg.sar.Spacing) != 0:
            d_chan = np.float(cfg.sar.Spacing)
        else:
            d_chan = ant_l

    if v_ground == 'auto':
        v_ground = geosar.orbit_to_vel(alt, ground=True)
    t_step = 1./prf
    t_span = (1.5*(sr0*l0/ant_l) + surface.Ly)/v_ground
    az_steps = np.int(np.floor(t_span/t_step))

    # Number of RG samples
    max_sr = np.max(sr) + wh_tol + (np.max(surface.y) + (t_span/2.)*v_ground)**2./(2.*sr0)
    min_sr = np.min(sr) - wh_tol
    rg_samp_orig = np.int(np.ceil(((max_sr - min_sr)/sr_res)*over_fs))
    rg_samp = np.int(utils.optimize_fftsize(rg_samp_orig))

    # Other initializations
    if do_hh:
        proc_raw_hh = np.zeros([num_ch, az_steps, rg_samp], dtype=np.complex)
    if do_vv:
        proc_raw_vv = np.zeros([num_ch, az_steps, rg_samp], dtype=np.complex)
    t_last_rcs_bragg = -1.
    last_progress = -1
    nrcs_avg_vv = np.zeros(az_steps, dtype=np.float)
    nrcs_avg_hh = np.zeros(az_steps, dtype=np.float)

    ## RCS MODELS
    # Specular
    if scat_spec_enable:
        if scat_spec_mode == 'kodis':
            rcs_spec = rcs.RCSKodis(inc, k0, surface.dx, surface.dy)
        elif scat_spec_mode == 'fa' or scat_spec_mode == 'spa':
            spec_ph0 = np.random.uniform(0., 2.*np.pi,
                                         size=[surface.Ny, surface.Nx])
            rcs_spec = rcs.RCSKA(scat_spec_mode, k0, surface.x, surface.y,
                                 surface.dx, surface.dy)
        else:
            raise NotImplementedError('RCS mode %s for specular scattering not implemented' % scat_spec_mode)

    # Bragg
    if scat_bragg_enable:
        phase_bragg = np.zeros([2, surface.Ny, surface.Nx])
        bragg_scats = np.zeros([2, surface.Ny, surface.Nx], dtype=np.complex)
        # dop_phase_p = np.random.uniform(0., 2.*np.pi, size=[surface.Ny, surface.Nx])
        # dop_phase_m = np.random.uniform(0., 2.*np.pi, size=[surface.Ny, surface.Nx])
        tau_c = closure.grid_coherence(cfg.ocean.wind_U,surface.dx, f0)
        rndscat_p = closure.randomscat_ts(tau_c, (surface.Ny, surface.Nx), prf)
        rndscat_m = closure.randomscat_ts(tau_c, (surface.Ny, surface.Nx), prf)
        # NOTE: This ignores slope, may be changed
        k_b = 2.*k0*sin_inc
        c_b = sin_inc*np.sqrt(const.g/k_b + 0.072e-3*k_b)

        if scat_bragg_model == 'romeiser97':
            current_dir = np.deg2rad(cfg.ocean.current_dir)
            current_vec = (cfg.ocean.current_mag *
                           np.array([np.cos(current_dir),
                                     np.sin(current_dir)]))
            U_dir = np.deg2rad(cfg.ocean.wind_dir)
            U_vec = (cfg.ocean.wind_U *
                     np.array([np.cos(U_dir), np.sin(U_dir)]))
            U_eff_vec = U_vec - current_vec

            rcs_bragg = rcs.RCSRomeiser97(k0, inc, pol,
                                          surface.dx, surface.dy,
                                          linalg.norm(U_eff_vec),
                                          np.arctan2(U_eff_vec[1],
                                                     U_eff_vec[0]),
                                          surface.wind_fetch,
                                          scat_bragg_spec, scat_bragg_spread,
                                          scat_bragg_d)
        else:
            raise NotImplementedError('RCS model %s for Bragg scattering not implemented' % scat_bragg_model)

    surface_area = surface.dx * surface.dy * surface.Nx * surface.Ny
    ###################
    # SIMULATION LOOP #
    ###################
    if rank == 0:
        print('Computing profiles...')

    for az_step in np.arange(az_steps, dtype=np.int):

        # AZIMUTH & SURFACE UPDATE
        t_now = az_step * t_step
        az_now = (t_now - t_span/2.)*v_ground
        # az = np.repeat((surface.y - az_now)[:, np.newaxis], surface.Nx, axis=1)
        az = (surface.y - az_now).reshape((surface.Ny, 1))
        surface.t = t_now

        # COMPUTE RCS FOR EACH MODEL
        # Note: SAR processing is range independent as slant range is fixed
        sin_az = az / sr0
        az_proj_angle = np.arcsin(az / gr0)

        # Note: Projected displacements are added to slant range
        sr_surface = (sr - cos_inc*surface.Dz + az/2*sin_az
                      + surface.Dx*sin_inc + surface.Dy*sin_az)

        if do_hh:
            scene_hh = np.zeros([int(surface.Ny), int(surface.Nx)], dtype=np.complex)
        if do_vv:
            scene_vv = np.zeros([int(surface.Ny), int(surface.Nx)], dtype=np.complex)
        # Point target
        if add_point_target and rank == 0:
            sr_pt = (sr[0, surface.Nx/2] + az[surface.Ny/2, 0]/2 *
                     sin_az[surface.Ny/2, surface.Nx/2])
            pt_scat = (100. * np.exp(-1j * 2. * k0 * sr_pt))
            if do_hh:
                scene_hh[surface.Ny/2, surface.Nx/2] = pt_scat
            if do_vv:
                scene_vv[surface.Ny/2, surface.Nx/2] = pt_scat
            sr_surface[surface.Ny/2, surface.Nx/2] = sr_pt

        # Specular
        if scat_spec_enable:
            if scat_spec_mode == 'kodis':
                Esn_sp = np.sqrt(4.*np.pi)*rcs_spec.field(az_proj_angle, sr_surface,
                                                          surface.Diffx, surface.Diffy,
                                                          surface.Diffxx, surface.Diffyy, surface.Diffxy)
                if do_hh:
                    scene_hh += Esn_sp
                if do_vv:
                    scene_vv += Esn_sp
            else:
                # FIXME
                if do_hh:
                    pol_tmp = 'hh'
                    Esn_sp = (np.exp(-1j*(2.*k0*sr_surface)) * (4.*np.pi)**1.5 *
                              rcs_spec.field(1, 1, pol_tmp[0], pol_tmp[1],
                                             inc, inc,
                                             az_proj_angle, az_proj_angle + np.pi,
                                             surface.Dz,
                                             surface.Diffx, surface.Diffy,
                                             surface.Diffxx,
                                             surface.Diffyy,
                                             surface.Diffxy))
                    scene_hh += Esn_sp
                if do_vv:
                    pol_tmp = 'vv'
                    Esn_sp = (np.exp(-1j*(2.*k0*sr_surface)) * (4.*np.pi)**1.5 *
                              rcs_spec.field(1, 1, pol_tmp[0], pol_tmp[1],
                                             inc, inc,
                                             az_proj_angle, az_proj_angle + np.pi,
                                             surface.Dz,
                                             surface.Diffx, surface.Diffy,
                                             surface.Diffxx,
                                             surface.Diffyy,
                                             surface.Diffxy))
                    scene_vv += Esn_sp
            nrcs_avg_hh[az_step] += (np.sum(np.abs(Esn_sp)**2) / surface_area)
            nrcs_avg_vv[az_step] += nrcs_avg_hh[az_step]

        # Bragg
        if scat_bragg_enable:
            if (t_now - t_last_rcs_bragg) > ocean_dt:

                if scat_bragg_model == 'romeiser97':
                    if pol == 'DP':
                        rcs_bragg_hh, rcs_bragg_vv = rcs_bragg.rcs(az_proj_angle,
                                                                   surface.Diffx,
                                                                   surface.Diffy)
                    elif pol=='hh':
                        rcs_bragg_hh = rcs_bragg.rcs(az_proj_angle,
                                                     surface.Diffx,
                                                     surface.Diffy)
                    else:
                        rcs_bragg_vv = rcs_bragg.rcs(az_proj_angle,
                                                     surface.Diffx,
                                                     surface.Diffy)

                if use_hmtf:
                    # Fix Bad MTF points
                    surface.hMTF[np.where(surface.hMTF < -1)] = -1
                    if do_hh:
                        rcs_bragg_hh[0] *= (1 + surface.hMTF)
                        rcs_bragg_hh[1] *= (1 + surface.hMTF)
                    if do_vv:
                        rcs_bragg_vv[0] *= (1 + surface.hMTF)
                        rcs_bragg_vv[1] *= (1 + surface.hMTF)

                t_last_rcs_bragg = t_now

            if do_hh:
                scat_bragg_hh = np.sqrt(rcs_bragg_hh)
                nrcs_bragg_hh_instant_avg = np.sum(rcs_bragg_hh) / surface_area
                nrcs_avg_hh[az_step] += nrcs_bragg_hh_instant_avg
            if do_vv:
                scat_bragg_vv = np.sqrt(rcs_bragg_vv)
                nrcs_bragg_vv_instant_avg = np.sum(rcs_bragg_vv) / surface_area
                nrcs_avg_vv[az_step] += nrcs_bragg_vv_instant_avg


            # Doppler phases (Note: Bragg radial velocity taken constant!)
            surf_phase = - (2 * k0) * sr_surface
            cap_phase = (2 * k0) * t_step * c_b * (az_step + 1)
            phase_bragg[0] = surf_phase - cap_phase # + dop_phase_p
            phase_bragg[1] = surf_phase + cap_phase # + dop_phase_m
            bragg_scats[0] = rndscat_m.scats(t_now)
            bragg_scats[1] = rndscat_p.scats(t_now)
            if do_hh:
                scene_hh += ne.evaluate('sum(scat_bragg_hh * exp(1j*phase_bragg) * bragg_scats, axis=0)')
            if do_vv:
                scene_vv += ne.evaluate('sum(scat_bragg_vv * exp(1j*phase_bragg) * bragg_scats, axis=0)')

        # ANTENNA PATTERN
        #  FIXME: this assume co-located Tx and Tx, so it will not work for true bistatic configurations
        if cfg.sar.L_total:
            beam_pattern = sinc_1tx_nrx(sin_az, ant_l * num_ch, f0, num_ch, field=True)
        else:
            beam_pattern = sinc_1tx_nrx(sin_az, ant_l, f0, 1, field=True)

        #  GENERATE CHANEL PROFILES
        for ch in np.arange(num_ch, dtype=np.int):

            if do_hh:
                scene_bp = scene_hh * beam_pattern
                # Add channel phase & compute profile
                scene_bp *= np.exp(-1j*k0*d_chan*ch*sin_az)
                if use_numba:
                    raw.chan_profile_numba(sr_surface.flatten(),
                                           scene_bp.flatten(),
                                           sr_res / over_fs,
                                           min_sr,
                                           chan_sinc_vec,
                                           n_sinc_samples, sinc_ovs,
                                           proc_raw_hh[ch][az_step])

                else:
                    raw.chan_profile_weave(sr_surface.flatten(),
                                           scene_bp.flatten(),
                                           sr_res / over_fs,
                                           min_sr,
                                           chan_sinc_vec,
                                           n_sinc_samples, sinc_ovs,
                                           proc_raw_hh[ch][az_step])
            if do_vv:
                scene_bp = scene_vv * beam_pattern
                # Add channel phase & compute profile
                scene_bp *= np.exp(-1j*k0*d_chan*ch*sin_az)
                if use_numba:
                    raw.chan_profile_numba(sr_surface.flatten(),
                                           scene_bp.flatten(),
                                           sr_res / over_fs,
                                           min_sr,
                                           chan_sinc_vec,
                                           n_sinc_samples, sinc_ovs,
                                           proc_raw_vv[ch][az_step])

                else:
                    raw.chan_profile_weave(sr_surface.flatten(),
                                           scene_bp.flatten(),
                                           sr_res / over_fs,
                                           min_sr,
                                           chan_sinc_vec,
                                           n_sinc_samples, sinc_ovs,
                                           proc_raw_vv[ch][az_step])

        # SHOW PROGRESS (%)
        current_progress = np.int((100 * az_step) / az_steps)
        if current_progress != last_progress:
            last_progress = current_progress
            print('SP,%d,%d,%d' % (rank, size, current_progress))

    # PROCESS REDUCED RAW DATA & SAVE (ROOT)
    if rank == 0:
        print('Processing and saving results...')

        # Filter and decimate
        #range_filter = np.ones_like(total_raw)
        #range_filter[:, :, rg_samp/(2*2*cfg.sar.over_fs):-rg_samp/(2*2*cfg.sar.over_fs)] = 0

        #total_raw = np.fft.ifft(range_filter*np.fft.fft(total_raw))
        if do_hh:
            proc_raw_hh = proc_raw_hh[:, :, :rg_samp_orig]
        if do_vv:
            proc_raw_vv = proc_raw_vv[:, :, :rg_samp_orig]

        # Calibration factor (projected antenna pattern integrated in azimuth)
        az_axis = np.arange(-t_span/2.*v_ground, t_span/2.*v_ground, sr0*const.c/(np.pi*f0*ant_l*10.))

        if cfg.sar.L_total:
            pattern = sinc_1tx_nrx(az_axis/sr0, ant_l * num_ch, f0,
                                   num_ch, field=True)
        else:
            pattern = sinc_1tx_nrx(az_axis/sr0, ant_l, f0, 1,
                                   field=True)
        cal_factor = (1. / np.sqrt(np.trapz(np.abs(pattern)**2., az_axis) *
                      sr_res/np.sin(inc_angle)))

        if do_hh:
            noise = (utils.db2lin(nesz, amplitude=True) / np.sqrt(2.) *
                     (np.random.normal(size=proc_raw_hh.shape) +
                      1j*np.random.normal(size=proc_raw_hh.shape)))
            total_raw_hh = proc_raw_hh * cal_factor + noise
        if do_vv:
            noise = (utils.db2lin(nesz, amplitude=True) / np.sqrt(2.) *
                     (np.random.normal(size=proc_raw_vv.shape) +
                      1j*np.random.normal(size=proc_raw_vv.shape)))
            total_raw_vv = proc_raw_vv * cal_factor + noise

        # Add slow-time error
        # if use_errors:
        #     if do_hh:
        #         total_raw_hh *= errors.beta_noise
        #     if do_vv:
        #         total_raw_vv *= errors.beta_noise

        # Save RAW data (and other properties, used by 3rd party software)
        if do_hh and do_vv:
            rshp = (1,) + proc_raw_hh.shape
            proc_raw = np.concatenate((proc_raw_hh.reshape(rshp),
                                        proc_raw_vv.reshape(rshp)))
            rshp = (1,) + nrcs_avg_hh.shape
            NRCS_avg = np.concatenate((nrcs_avg_hh.reshape(rshp),
                                       nrcs_avg_vv.reshape(rshp)))
        elif do_hh:
            rshp = (1,) + proc_raw_hh.shape
            proc_raw = proc_raw_hh.reshape(rshp)
            rshp = (1,) + nrcs_avg_hh.shape
            NRCS_avg = nrcs_avg_hh.reshape(rshp)
        else:
            rshp = (1,) + proc_raw_vv.shape
            proc_raw = proc_raw_vv.reshape(rshp)
            rshp = (1,) + nrcs_avg_vv.shape
            NRCS_avg = nrcs_avg_vv.reshape(rshp)

        raw_file = tpio.RawFile(output_file, 'w', proc_raw.shape)
        raw_file.set('inc_angle', np.rad2deg(inc_angle))
        raw_file.set('f0', f0)
        raw_file.set('num_ch', num_ch)
        raw_file.set('ant_l', ant_l)
        raw_file.set('prf', prf)
        raw_file.set('v_ground', v_ground)
        raw_file.set('orbit_alt', alt)
        raw_file.set('sr0', sr0)
        raw_file.set('rg_sampling', rg_bw*over_fs)
        raw_file.set('rg_bw', rg_bw)
        raw_file.set('raw_data*', proc_raw)
        raw_file.set('NRCS_avg', NRCS_avg)
        raw_file.close()

        print(time.strftime("Finished [%Y-%m-%d %H:%M:%S]", time.localtime()))
示例#9
0
def skimraw(cfg_file,
            output_file,
            ocean_file,
            reuse_ocean_file,
            errors_file,
            reuse_errors_file,
            plot_save=True):

    ###################
    # INITIALIZATIONS #
    ###################

    # MPI SETUP
    comm = MPI.COMM_WORLD
    size, rank = comm.Get_size(), comm.Get_rank()

    #  WELCOME
    if rank == 0:
        print(
            '-------------------------------------------------------------------'
        )
        print(
            time.strftime("- OCEANSAR SKIM RAW GENERATOR: %Y-%m-%d %H:%M:%S",
                          time.localtime()))
        # print('- Copyright (c) Gerard Marull Paretas, Paco Lopez Dekker')
        print(
            '-------------------------------------------------------------------'
        )

    #  CONFIGURATION FILE
    # Note: variables are 'copied' to reduce code verbosity
    cfg = tpio.ConfigFile(cfg_file)
    info = utils.PrInfo(cfg.sim.verbosity, "SKIM raw")
    # RAW
    wh_tol = cfg.srg.wh_tol
    nesz = cfg.srg.nesz
    use_hmtf = cfg.srg.use_hmtf
    scat_spec_enable = cfg.srg.scat_spec_enable
    scat_spec_mode = cfg.srg.scat_spec_mode
    scat_bragg_enable = cfg.srg.scat_bragg_enable
    scat_bragg_model = cfg.srg.scat_bragg_model
    scat_bragg_d = cfg.srg.scat_bragg_d
    scat_bragg_spec = cfg.srg.scat_bragg_spec
    scat_bragg_spread = cfg.srg.scat_bragg_spread

    # SAR
    inc_angle = np.deg2rad(cfg.radar.inc_angle)
    f0 = cfg.radar.f0
    pol = cfg.radar.pol
    squint_r = np.radians(90 - cfg.radar.azimuth)
    if pol == 'DP':
        do_hh = True
        do_vv = True
    elif pol == 'hh':
        do_hh = True
        do_vv = False
    else:
        do_hh = False
        do_vv = True

    prf = cfg.radar.prf
    num_ch = int(cfg.radar.num_ch)
    ant_L = cfg.radar.ant_L
    alt = cfg.radar.alt
    v_ground = cfg.radar.v_ground
    rg_bw = cfg.radar.rg_bw
    over_fs = cfg.radar.Fs / cfg.radar.rg_bw
    sigma_n_tx = cfg.radar.sigma_n_tx
    phase_n_tx = np.deg2rad(cfg.radar.phase_n_tx)
    sigma_beta_tx = cfg.radar.sigma_beta_tx
    phase_beta_tx = np.deg2rad(cfg.radar.phase_beta_tx)
    sigma_n_rx = cfg.radar.sigma_n_rx
    phase_n_rx = np.deg2rad(cfg.radar.phase_n_rx)
    sigma_beta_rx = cfg.radar.sigma_beta_rx
    phase_beta_rx = np.deg2rad(cfg.radar.phase_beta_rx)

    # OCEAN / OTHERS
    ocean_dt = cfg.ocean.dt
    if hasattr(cfg.sim, "cal_targets"):
        if cfg.sim.cal_targets is False:
            add_point_target = False  # This for debugging
            point_target_floats = True  # Not really needed, but makes coding easier later
        else:
            print("Adding cal targets")
            add_point_target = True
            if cfg.sim.cal_targets.lower() == 'floating':
                point_target_floats = True
            else:
                point_target_floats = False
    else:
        add_point_target = False  # This for debugging
        point_target_floats = True

    n_sinc_samples = 10
    sinc_ovs = 20
    chan_sinc_vec = raw.calc_sinc_vec(n_sinc_samples, sinc_ovs, Fs=over_fs)
    # Set win direction with respect to beam
    # I hope the following line is correct, maybe sign is wrong
    wind_dir = cfg.radar.azimuth - cfg.ocean.wind_dir
    # OCEAN SURFACE
    if rank == 0:
        print('Initializing ocean surface...')
        surface_full = OceanSurface()
        # Setup compute values
        compute = ['D', 'Diff', 'Diff2']
        if use_hmtf:
            compute.append('hMTF')
        # Try to reuse initialized surface
        if reuse_ocean_file:
            try:
                surface_full.load(ocean_file, compute)
            except RuntimeError:
                pass

        if (not reuse_ocean_file) or (not surface_full.initialized):
            if hasattr(cfg.ocean, 'use_buoy_data'):
                if cfg.ocean.use_buoy_data:
                    bdataf = cfg.ocean.buoy_data_file
                    date = datetime.datetime(np.int(cfg.ocean.year),
                                             np.int(cfg.ocean.month),
                                             np.int(cfg.ocean.day),
                                             np.int(cfg.ocean.hour),
                                             np.int(cfg.ocean.minute), 0)
                    date, bdata = tpio.load_buoydata(bdataf, date)
                    # FIX-ME: direction needs to consider also azimuth of beam
                    buoy_spec = tpio.BuoySpectra(bdata,
                                                 heading=cfg.radar.heading,
                                                 depth=cfg.ocean.depth)
                    dirspectrum_func = buoy_spec.Sk2
                    # Since the wind direction is included in the buoy data
                    wind_dir = 0
                else:
                    dirspectrum_func = None
                    if cfg.ocean.swell_dir_enable:
                        dir_swell_spec = s_spec.ardhuin_swell_spec
                    else:
                        dir_swell_spec = None

                    wind_dir = np.deg2rad(wind_dir)
            else:
                if cfg.ocean.swell_dir_enable:
                    dir_swell_spec = s_spec.ardhuin_swell_spec
                else:
                    dir_swell_spec = None
                dirspectrum_func = None
                wind_dir = np.deg2rad(wind_dir)

            surface_full.init(
                cfg.ocean.Lx,
                cfg.ocean.Ly,
                cfg.ocean.dx,
                cfg.ocean.dy,
                cfg.ocean.cutoff_wl,
                cfg.ocean.spec_model,
                cfg.ocean.spread_model,
                wind_dir,
                cfg.ocean.wind_fetch,
                cfg.ocean.wind_U,
                cfg.ocean.current_mag,
                np.deg2rad(cfg.radar.azimuth - cfg.ocean.current_dir),
                cfg.radar.azimuth - cfg.ocean.dir_swell_dir,
                cfg.ocean.freq_r,
                cfg.ocean.sigf,
                cfg.ocean.sigs,
                cfg.ocean.Hs,
                cfg.ocean.swell_dir_enable,
                cfg.ocean.swell_enable,
                cfg.ocean.swell_ampl,
                np.deg2rad(cfg.radar.azimuth - cfg.ocean.swell_dir),
                cfg.ocean.swell_wl,
                compute,
                cfg.ocean.opt_res,
                cfg.ocean.fft_max_prime,
                choppy_enable=cfg.ocean.choppy_enable,
                depth=cfg.ocean.depth,
                dirspectrum_func=dirspectrum_func,
                dir_swell_spec=dir_swell_spec)

            surface_full.save(ocean_file)
            # Now we plot the directional spectrum
            # self.wave_dirspec[good_k] = dirspectrum_func(self.kx[good_k], self.ky[good_k])
            plt.figure()
            plt.imshow(np.fft.fftshift(surface_full.wave_dirspec),
                       extent=[
                           surface_full.kx.min(),
                           surface_full.kx.max(),
                           surface_full.ky.min(),
                           surface_full.ky.max()
                       ],
                       origin='lower',
                       cmap='inferno_r')

            plt.grid(True)
            pltax = plt.gca()
            pltax.set_xlim((-1, 1))
            pltax.set_ylim((-1, 1))
            Narr_length = 0.08  # np.min([surface_full.kx.max(), surface_full.ky.max()])
            pltax.arrow(0,
                        0,
                        -Narr_length * np.sin(np.radians(cfg.radar.heading)),
                        Narr_length * np.cos(np.radians(cfg.radar.heading)),
                        fc="k",
                        ec="k")
            plt.xlabel('$k_x$ [rad/m]')
            plt.ylabel('$k_y$ [rad/m]')
            plt.colorbar()
            #plt.show()
            # Create plots directory
            plot_path = os.path.dirname(output_file) + os.sep + 'raw_plots'
            if plot_save:
                if not os.path.exists(plot_path):
                    os.makedirs(plot_path)

            plt.savefig(os.path.join(plot_path, 'input_dirspectrum.png'))

            plt.close()

            if cfg.ocean.swell_dir_enable:
                plt.figure()
                plt.imshow(np.fft.fftshift(np.abs(surface_full.swell_dirspec)),
                           extent=[
                               surface_full.kx.min(),
                               surface_full.kx.max(),
                               surface_full.ky.min(),
                               surface_full.ky.max()
                           ],
                           origin='lower',
                           cmap='inferno_r')

                plt.grid(True)
                pltax = plt.gca()
                pltax.set_xlim((-0.1, 0.1))
                pltax.set_ylim((-0.1, 0.1))
                Narr_length = 0.08  # np.min([surface_full.kx.max(), surface_full.ky.max()])
                pltax.arrow(
                    0,
                    0,
                    -Narr_length * np.sin(np.radians(cfg.radar.heading)),
                    Narr_length * np.cos(np.radians(cfg.radar.heading)),
                    fc="k",
                    ec="k")
                plt.xlabel('$k_x$ [rad/m]')
                plt.ylabel('$k_y$ [rad/m]')
                plt.colorbar()
                #plt.show()
                # Create plots directory
                plot_path = os.path.dirname(output_file) + os.sep + 'raw_plots'
                if plot_save:
                    if not os.path.exists(plot_path):
                        os.makedirs(plot_path)

                plt.savefig(
                    os.path.join(plot_path, 'input_dirspectrum_combined.png'))

                plt.close()

    else:
        surface_full = None

    # Initialize surface balancer
    surface = OceanSurfaceBalancer(surface_full, ocean_dt)

    # CALCULATE PARAMETERS
    if rank == 0:
        print('Initializing simulation parameters...')

    # SR/GR/INC Matrixes
    sr0 = geosar.inc_to_sr(inc_angle, alt)
    gr0 = geosar.inc_to_gr(inc_angle, alt)
    gr = surface.x + gr0
    sr, inc, _ = geosar.gr_to_geo(gr, alt)
    print(sr.dtype)
    look = geosar.inc_to_look(inc, alt)
    min_sr = np.min(sr)
    # sr -= np.min(sr)
    #inc = np.repeat(inc[np.newaxis, :], surface.Ny, axis=0)
    #sr = np.repeat(sr[np.newaxis, :], surface.Ny, axis=0)
    #gr = np.repeat(gr[np.newaxis, :], surface.Ny, axis=0)
    #Let's try to safe some memory and some operations
    inc = inc.reshape(1, inc.size)
    look = look.reshape(1, inc.size)
    sr = sr.reshape(1, sr.size)
    gr = gr.reshape(1, gr.size)
    sin_inc = np.sin(inc)
    cos_inc = np.cos(inc)

    # lambda, K, resolution, time, etc.
    l0 = const.c / f0
    k0 = 2. * np.pi * f0 / const.c
    sr_res = const.c / (2. * rg_bw)
    sr_smp = const.c / (2 * cfg.radar.Fs)
    if cfg.radar.L_total:
        ant_L = ant_L / np.float(num_ch)

    if v_ground == 'auto':
        v_ground = geosar.orbit_to_vel(alt, ground=True)
        v_orb = geosar.orbit_to_vel(alt, ground=False)
    else:
        v_orb = v_ground
    t_step = 1. / prf
    az_steps = int(cfg.radar.n_pulses)
    t_span = az_steps / prf
    rg_samp = np.int(utils.optimize_fftsize(cfg.radar.n_rg))
    #min_sr = np.mean(sr) - rg_samp / 2 * sr_smp
    print(sr_smp)
    max_sr = np.mean(sr) + rg_samp / 2 * sr_smp
    sr_prof = (np.arange(rg_samp) - rg_samp / 2) * sr_smp + np.mean(sr)
    gr_prof, inc_prof, look_prof, b_prof = geosar.sr_to_geo(sr_prof, alt)
    look_prof = look_prof.reshape((1, look_prof.size))
    sr_prof = sr_prof.reshape((1, look_prof.size))
    dop_ref = 2 * v_orb * np.sin(look_prof) * np.sin(squint_r) / l0
    print("skim_raw: Doppler Centroid is %f Hz" % (np.mean(dop_ref)))
    if cfg.srg.two_scale_Doppler:
        # We will compute less surface realizations
        n_pulses_b = utils.optimize_fftsize(int(
            cfg.srg.surface_coh_time * prf)) / 2
        print("skim_raw: down-sampling rate =%i" % (n_pulses_b))
        # n_pulses_b = 4
        az_steps_ = int(np.ceil(az_steps / n_pulses_b))
        t_step = t_step * n_pulses_b
        # Maximum length in azimuth that we can consider to have the same geometric Doppler
        dy_integ = cfg.srg.phase_err_tol / (2 * k0 * v_ground / min_sr *
                                            cfg.srg.surface_coh_time)
        surface_dy = surface.y[1] - surface.y[0]
        ny_integ = (dy_integ / surface.dy)
        print("skim_raw: ny_integ=%f" % (ny_integ))
        if ny_integ < 1:
            ny_integ = 1
        else:
            ny_integ = int(2**np.floor(np.log2(ny_integ)))
        info.msg("skim_raw: size of intermediate radar data: %f MB" %
                 (8 * ny_integ * az_steps_ * rg_samp * 1e-6),
                 importance=1)
        info.msg("skim_raw: ny_integ=%i" % (ny_integ), importance=1)
        if do_hh:
            proc_raw_hh = np.zeros(
                [az_steps_, int(surface.Ny / ny_integ), rg_samp],
                dtype=np.complex)
            proc_raw_hh_step = np.zeros([surface.Ny, rg_samp],
                                        dtype=np.complex)
        if do_vv:
            proc_raw_vv = np.zeros(
                [az_steps_, int(surface.Ny / ny_integ), rg_samp],
                dtype=np.complex)
            proc_raw_vv_step = np.zeros([surface.Ny, rg_samp],
                                        dtype=np.complex)
        # Doppler centroid
        # sin(a+da) = sin(a) + cos(a)*da - 1/2*sin(a)*da**2
        az = surface.y.reshape((surface.Ny, 1))
        # FIX-ME: this is a coarse approximation
        da = az / gr_prof
        sin_az = np.sin(
            squint_r) + np.cos(squint_r) * da - 0.5 * np.sin(squint_r) * da**2
        dop0 = 2 * v_orb * np.sin(look_prof) * sin_az / l0
        # az / 2 * sin_sr   _az
        # az_now = (t_now - t_span / 2.) * v_ground * np.cos(squint_r)
        # az = np.repeat((surface.y - az_now)[:, np.newaxis], surface.Nx, axis=1)
        # az = (surface.y - az_now).reshape((surface.Ny, 1))
        # print("Max az: %f" % (np.max(az)))
        #dop0 = np.mean(np.reshape(dop0, (surface.Ny/ny_integ, ny_integ, rg_samp)), axis=1)
        s_int = np.int(surface.Ny / ny_integ)
        dop0 = np.mean(np.reshape(dop0, (s_int, np.int(ny_integ), rg_samp)),
                       axis=1)
    else:
        az_steps_ = az_steps
        if do_hh:
            proc_raw_hh = np.zeros([az_steps, rg_samp], dtype=np.complex)
        if do_vv:
            proc_raw_vv = np.zeros([az_steps, rg_samp], dtype=np.complex)

    t_last_rcs_bragg = -1.
    last_progress = -1
    NRCS_avg_vv = np.zeros(az_steps, dtype=np.float)
    NRCS_avg_hh = np.zeros(az_steps, dtype=np.float)

    ## RCS MODELS
    # Specular
    if scat_spec_enable:
        if scat_spec_mode == 'kodis':
            rcs_spec = rcs.RCSKodis(inc, k0, surface.dx, surface.dy)
        elif scat_spec_mode == 'fa' or scat_spec_mode == 'spa':
            spec_ph0 = np.random.uniform(0.,
                                         2. * np.pi,
                                         size=[surface.Ny, surface.Nx])
            rcs_spec = rcs.RCSKA(scat_spec_mode, k0, surface.x, surface.y,
                                 surface.dx, surface.dy)
        else:
            raise NotImplementedError(
                'RCS mode %s for specular scattering not implemented' %
                scat_spec_mode)

    # Bragg
    if scat_bragg_enable:
        phase_bragg = np.zeros([2, surface.Ny, surface.Nx])
        bragg_scats = np.zeros([2, surface.Ny, surface.Nx], dtype=np.complex)
        # dop_phase_p = np.random.uniform(0., 2.*np.pi, size=[surface.Ny, surface.Nx])
        # dop_phase_m = np.random.uniform(0., 2.*np.pi, size=[surface.Ny, surface.Nx])
        tau_c = closure.grid_coherence(cfg.ocean.wind_U, surface.dx, f0)
        rndscat_p = closure.randomscat_ts(tau_c, (surface.Ny, surface.Nx), prf)
        rndscat_m = closure.randomscat_ts(tau_c, (surface.Ny, surface.Nx), prf)
        # NOTE: This ignores slope, may be changed
        k_b = 2. * k0 * sin_inc
        c_b = sin_inc * np.sqrt(const.g / k_b + 0.072e-3 * k_b)

        if scat_bragg_model == 'romeiser97':
            current_dir = np.deg2rad(cfg.ocean.current_dir)
            current_vec = (cfg.ocean.current_mag * np.array(
                [np.cos(current_dir), np.sin(current_dir)]))
            U_dir = np.deg2rad(cfg.ocean.wind_dir)
            U_vec = (cfg.ocean.wind_U *
                     np.array([np.cos(U_dir), np.sin(U_dir)]))
            U_eff_vec = U_vec - current_vec

            rcs_bragg = rcs.RCSRomeiser97(
                k0, inc, pol, surface.dx, surface.dy, linalg.norm(U_eff_vec),
                np.arctan2(U_eff_vec[1], U_eff_vec[0]), surface.wind_fetch,
                scat_bragg_spec, scat_bragg_spread, scat_bragg_d)
        else:
            raise NotImplementedError(
                'RCS model %s for Bragg scattering not implemented' %
                scat_bragg_model)

    surface_area = surface.dx * surface.dy * surface.Nx * surface.Ny
    ###################
    # SIMULATION LOOP #
    ###################
    if rank == 0:
        print('Computing profiles...')

    for az_step in np.arange(az_steps_, dtype=np.int):

        # AZIMUTH & SURFACE UPDATE
        t_now = az_step * t_step
        az_now = (t_now - t_span / 2.) * v_ground * np.cos(squint_r)
        # az = np.repeat((surface.y - az_now)[:, np.newaxis], surface.Nx, axis=1)
        az = (surface.y - az_now).reshape((surface.Ny, 1))
        surface.t = t_now
        if az_step == 0:
            # Check wave-height
            info.msg(
                "Standard deviation of wave-height (peak-to-peak; i.e. x2): %f"
                % (2 * np.std(surface.Dz)))
        #if az_step == 0:
        # print("Max Dx: %f" % (np.max(surface.Dx)))
        # print("Max Dy: %f" % (np.max(surface.Dy)))
        # print("Max Dz: %f" % (np.max(surface.Dz)))
        # print("Max Diffx: %f" % (np.max(surface.Diffx)))
        # print("Max Diffy: %f" % (np.max(surface.Diffy)))
        # print("Max Diffxx: %f" % (np.max(surface.Diffxx)))
        # print("Max Diffyy: %f" % (np.max(surface.Diffyy)))
        # print("Max Diffxy: %f" % (np.max(surface.Diffxy)))
        # COMPUTE RCS FOR EACH MODEL
        # Note: SAR processing is range independent as slant range is fixed
        sin_az = az / sr
        az_proj_angle = np.arcsin(az / gr0)
        # Note: Projected displacements are added to slant range
        if point_target_floats is False:  # This can only happen if point targets are enabled
            surface.Dx[int(surface.Ny / 2), int(surface.Nx / 2)] = 0
            surface.Dy[int(surface.Ny / 2), int(surface.Nx / 2)] = 0
            surface.Dz[int(surface.Ny / 2), int(surface.Nx / 2)] = 0

        if cfg.srg.two_scale_Doppler:
            # slant-range for phase
            sr_surface = (sr - cos_inc * surface.Dz + surface.Dx * sin_inc +
                          surface.Dy * sin_az)
            if cfg.srg.rcm:
                # add non common rcm
                sr_surface4rcm = sr_surface + az / 2 * sin_az
            else:
                sr_surface4rcm = sr_surface
        else:
            # FIXME: check if global shift is included, in case we care about slow simulations
            # slant-range for phase and Doppler
            sr_surface = (sr - cos_inc * surface.Dz + az / 2 * sin_az +
                          surface.Dx * sin_inc + surface.Dy * sin_az)
            sr_surface4rcm = sr_surface

        if do_hh:
            scene_hh = np.zeros(
                [int(surface.Ny), int(surface.Nx)], dtype=np.complex)
        if do_vv:
            scene_vv = np.zeros(
                [int(surface.Ny), int(surface.Nx)], dtype=np.complex)

        # Specular
        if scat_spec_enable:
            if scat_spec_mode == 'kodis':
                Esn_sp = np.sqrt(4. * np.pi) * rcs_spec.field(
                    az_proj_angle, sr_surface, surface.Diffx, surface.Diffy,
                    surface.Diffxx, surface.Diffyy, surface.Diffxy)
                if do_hh:
                    scene_hh += Esn_sp
                if do_vv:
                    scene_vv += Esn_sp
            else:
                # FIXME
                if do_hh:
                    pol_tmp = 'hh'
                    Esn_sp = (
                        np.exp(-1j * (2. * k0 * sr_surface)) *
                        (4. * np.pi)**1.5 * rcs_spec.field(
                            1, 1, pol_tmp[0], pol_tmp[1], inc, inc,
                            az_proj_angle, az_proj_angle + np.pi, surface.Dz,
                            surface.Diffx, surface.Diffy, surface.Diffxx,
                            surface.Diffyy, surface.Diffxy))
                    scene_hh += Esn_sp
                if do_vv:
                    pol_tmp = 'vv'
                    Esn_sp = (
                        np.exp(-1j * (2. * k0 * sr_surface)) *
                        (4. * np.pi)**1.5 * rcs_spec.field(
                            1, 1, pol_tmp[0], pol_tmp[1], inc, inc,
                            az_proj_angle, az_proj_angle + np.pi, surface.Dz,
                            surface.Diffx, surface.Diffy, surface.Diffxx,
                            surface.Diffyy, surface.Diffxy))
                    scene_vv += Esn_sp
            NRCS_avg_hh[az_step] += (np.sum(np.abs(Esn_sp)**2) / surface_area)
            NRCS_avg_vv[az_step] += NRCS_avg_hh[az_step]

        # Bragg
        if scat_bragg_enable:
            if (t_now - t_last_rcs_bragg) > ocean_dt:

                if scat_bragg_model == 'romeiser97':
                    if pol == 'DP':
                        RCS_bragg_hh, RCS_bragg_vv = rcs_bragg.rcs(
                            az_proj_angle, surface.Diffx, surface.Diffy)
                    elif pol == 'hh':
                        RCS_bragg_hh = rcs_bragg.rcs(az_proj_angle,
                                                     surface.Diffx,
                                                     surface.Diffy)
                    else:
                        RCS_bragg_vv = rcs_bragg.rcs(az_proj_angle,
                                                     surface.Diffx,
                                                     surface.Diffy)

                if use_hmtf:
                    # Fix Bad MTF points
                    surface.hMTF[np.where(surface.hMTF < -1)] = -1
                    if do_hh:
                        RCS_bragg_hh[0] *= (1 + surface.hMTF)
                        RCS_bragg_hh[1] *= (1 + surface.hMTF)
                    if do_vv:
                        RCS_bragg_vv[0] *= (1 + surface.hMTF)
                        RCS_bragg_vv[1] *= (1 + surface.hMTF)

                t_last_rcs_bragg = t_now

            if do_hh:
                scat_bragg_hh = np.sqrt(RCS_bragg_hh)
                NRCS_bragg_hh_instant_avg = np.sum(RCS_bragg_hh) / surface_area
                NRCS_avg_hh[az_step] += NRCS_bragg_hh_instant_avg
            if do_vv:
                scat_bragg_vv = np.sqrt(RCS_bragg_vv)
                NRCS_bragg_vv_instant_avg = np.sum(RCS_bragg_vv) / surface_area
                NRCS_avg_vv[az_step] += NRCS_bragg_vv_instant_avg

            # Doppler phases (Note: Bragg radial velocity taken constant!)
            surf_phase = -(2 * k0) * sr_surface
            cap_phase = (2 * k0) * t_step * c_b * (az_step + 1)
            phase_bragg[0] = surf_phase - cap_phase  # + dop_phase_p
            phase_bragg[1] = surf_phase + cap_phase  # + dop_phase_m
            bragg_scats[0] = rndscat_m.scats(t_now)
            bragg_scats[1] = rndscat_p.scats(t_now)
            if do_hh:
                scene_hh += ne.evaluate(
                    'sum(scat_bragg_hh * exp(1j*phase_bragg) * bragg_scats, axis=0)'
                )
            if do_vv:
                scene_vv += ne.evaluate(
                    'sum(scat_bragg_vv * exp(1j*phase_bragg) * bragg_scats, axis=0)'
                )

        if add_point_target:
            # Now we replace scattering at center by fixed value
            pt_y = int(surface.Ny / 2)
            pt_x = int(surface.Nx / 2)
            if do_hh:
                scene_hh[pt_y, pt_x] = 1000 * np.exp(
                    -1j * 2 * k0 * sr_surface[pt_y, pt_x])
            if do_vv:
                scene_vv[pt_y, pt_x] = 1000 * np.exp(
                    -1j * 2 * k0 * sr_surface[pt_y, pt_x])
        ## ANTENNA PATTERN
        ## FIXME: this assume co-located Tx and Tx, so it will not work for true bistatic configurations
        if cfg.radar.L_total:
            beam_pattern = sinc_1tx_nrx(sin_az,
                                        ant_L * num_ch,
                                        f0,
                                        num_ch,
                                        field=True)
        else:
            beam_pattern = sinc_1tx_nrx(sin_az, ant_L, f0, 1, field=True)

        # GENERATE CHANEL PROFILES
        if cfg.srg.two_scale_Doppler:
            sr_surface_ = sr_surface4rcm
            if do_hh:
                proc_raw_hh_step[:, :] = 0
                proc_raw_hh_ = proc_raw_hh_step
                scene_bp_hh = scene_hh * beam_pattern
            if do_vv:
                proc_raw_vv_step[:, :] = 0
                proc_raw_vv_ = proc_raw_vv_step
                scene_bp_vv = scene_vv * beam_pattern
        else:
            sr_surface_ = sr_surface4rcm.flatten()
            if do_hh:
                proc_raw_hh_ = proc_raw_hh[az_step]
                scene_bp_hh = (scene_hh * beam_pattern).flatten()
            if do_vv:
                proc_raw_vv_ = proc_raw_vv[az_step]
                scene_bp_vv = (scene_vv * beam_pattern).flatten()
        if do_hh:
            raw.chan_profile_numba(sr_surface_,
                                   scene_bp_hh,
                                   sr_smp,
                                   sr_prof.min(),
                                   chan_sinc_vec,
                                   n_sinc_samples,
                                   sinc_ovs,
                                   proc_raw_hh_,
                                   rg_only=cfg.srg.two_scale_Doppler)
        if do_vv:
            raw.chan_profile_numba(sr_surface_,
                                   scene_bp_vv,
                                   sr_smp,
                                   sr_prof.min(),
                                   chan_sinc_vec,
                                   n_sinc_samples,
                                   sinc_ovs,
                                   proc_raw_vv_,
                                   rg_only=cfg.srg.two_scale_Doppler)
        if cfg.srg.two_scale_Doppler:
            #Integrate in azimuth
            s_int = np.int(surface.Ny / ny_integ)
            if do_hh:
                proc_raw_hh[az_step] = np.sum(np.reshape(
                    proc_raw_hh_, (s_int, ny_integ, rg_samp)),
                                              axis=1)
                info.msg("Max abs(HH): %f" %
                         np.max(np.abs(proc_raw_hh[az_step])),
                         importance=1)
            if do_vv:
                #print(proc_raw_vv.shape)
                proc_raw_vv[az_step] = np.sum(np.reshape(
                    proc_raw_vv_, (s_int, ny_integ, rg_samp)),
                                              axis=1)
                info.msg("Max abs(VV): %f" %
                         np.max(np.abs(proc_raw_vv[az_step])),
                         importance=1)
        # SHOW PROGRESS (%)
        current_progress = np.int((100 * az_step) / az_steps_)
        if current_progress != last_progress:
            last_progress = current_progress
            info.msg('SP,%d,%d,%d%%' % (rank, size, current_progress),
                     importance=1)

    if cfg.srg.two_scale_Doppler:
        # No we have to up-sample and add Doppler
        info.msg("skim_raw: Dopplerizing and upsampling")
        print(dop0.max())
        print(n_pulses_b)
        print(prf)
        if do_hh:
            proc_raw_hh = upsample_and_dopplerize(proc_raw_hh, dop0,
                                                  n_pulses_b, prf)
        if do_vv:
            proc_raw_vv = upsample_and_dopplerize(proc_raw_vv, dop0,
                                                  n_pulses_b, prf)

    # MERGE RESULTS
    if do_hh:
        total_raw_hh = np.empty_like(proc_raw_hh) if rank == 0 else None
        comm.Reduce(proc_raw_hh, total_raw_hh, op=MPI.SUM, root=0)
    if do_vv:
        total_raw_vv = np.empty_like(proc_raw_vv) if rank == 0 else None
        comm.Reduce(proc_raw_vv, total_raw_vv, op=MPI.SUM, root=0)

    ## PROCESS REDUCED RAW DATA & SAVE (ROOT)
    if rank == 0:
        info.msg('calibrating and saving results...')

        # Filter and decimate
        #range_filter = np.ones_like(total_raw)
        #range_filter[:, :, rg_samp/(2*2*cfg.radar.over_fs):-rg_samp/(2*2*cfg.radar.over_fs)] = 0

        #total_raw = np.fft.ifft(range_filter*np.fft.fft(total_raw))
        if do_hh:
            total_raw_hh = total_raw_hh[:, :cfg.radar.n_rg]
        if do_vv:
            total_raw_vv = total_raw_vv[:, :cfg.radar.n_rg]

        # Calibration factor (projected antenna pattern integrated in azimuth)
        az_axis = np.arange(-t_span / 2. * v_ground, t_span / 2. * v_ground,
                            sr0 * const.c / (np.pi * f0 * ant_L * 10.))

        if cfg.radar.L_total:
            pattern = sinc_1tx_nrx(az_axis / sr0,
                                   ant_L * num_ch,
                                   f0,
                                   num_ch,
                                   field=True)
        else:
            pattern = sinc_1tx_nrx(az_axis / sr0, ant_L, f0, 1, field=True)
        cal_factor = (1. / np.sqrt(
            np.trapz(np.abs(pattern)**2., az_axis) * sr_res /
            np.sin(inc_angle)))

        if do_hh:
            noise = (utils.db2lin(nesz, amplitude=True) / np.sqrt(2.) *
                     (np.random.normal(size=total_raw_hh.shape) +
                      1j * np.random.normal(size=total_raw_hh.shape)))
            total_raw_hh = total_raw_hh * cal_factor + noise
        if do_vv:
            noise = (utils.db2lin(nesz, amplitude=True) / np.sqrt(2.) *
                     (np.random.normal(size=total_raw_vv.shape) +
                      1j * np.random.normal(size=total_raw_vv.shape)))
            total_raw_vv = total_raw_vv * cal_factor + noise

        # Add slow-time error
        # if use_errors:
        #     if do_hh:
        #         total_raw_hh *= errors.beta_noise
        #     if do_vv:
        #         total_raw_vv *= errors.beta_noise

        # Save RAW data
        if do_hh and do_vv:
            rshp = (1, ) + total_raw_hh.shape
            total_raw = np.concatenate(
                (total_raw_hh.reshape(rshp), total_raw_vv.reshape(rshp)))
            rshp = (1, ) + NRCS_avg_hh.shape
            NRCS_avg = np.concatenate(
                (NRCS_avg_hh.reshape(rshp), NRCS_avg_vv.reshape(rshp)))
        elif do_hh:
            rshp = (1, ) + total_raw_hh.shape
            total_raw = total_raw_hh.reshape(rshp)
            rshp = (1, ) + NRCS_avg_hh.shape
            NRCS_avg = NRCS_avg_hh.reshape(rshp)
        else:
            rshp = (1, ) + total_raw_vv.shape
            total_raw = total_raw_vv.reshape(rshp)
            rshp = (1, ) + NRCS_avg_vv.shape
            NRCS_avg = NRCS_avg_vv.reshape(rshp)

        raw_file = tpio.SkimRawFile(output_file, 'w', total_raw.shape)
        raw_file.set('inc_angle', np.rad2deg(inc_angle))
        raw_file.set('f0', f0)
        # raw_file.set('num_ch', num_ch)
        raw_file.set('ant_L', ant_L)
        raw_file.set('prf', prf)
        raw_file.set('v_ground', v_ground)
        raw_file.set('orbit_alt', alt)
        raw_file.set('sr0', sr0)
        raw_file.set('rg_sampling', rg_bw * over_fs)
        raw_file.set('rg_bw', rg_bw)
        raw_file.set('raw_data*', total_raw)
        raw_file.set('NRCS_avg', NRCS_avg)
        raw_file.set('azimuth', cfg.radar.azimuth)
        raw_file.set('dop_ref', dop_ref)
        raw_file.close()

        print(time.strftime("Finished [%Y-%m-%d %H:%M:%S]", time.localtime()))
示例#10
0
def delta_k_processing(raw_data, cfg_file):
    #parameters
    cfg = tpio.ConfigFile(cfg_file)
    Az_smaples = cfg.radar.n_pulses
    # Az_smaples = 1028
    PRF = cfg.radar.prf
    fs = cfg.radar.Fs
    R_samples = cfg.radar.n_rg
    #Sp_x = cfg.ocean.dx
    inc = cfg.radar.inc_angle

    #processing parameters
    analysis_deltan =  np.linspace(0,300,301) #for delta-k spectrum analysis
    RCS_power = np.zeros_like(analysis_deltan)
    wave_scale = 25 #44.1#44.1#29.3#26.4#100##25.3#26.21#26.5##25.34#26.31##10#
    r_int_num = 700
    r_int_num_fd = 400
    az_int = 300
    num_az = 1
    list = range(1,num_az)
    analyse_num = range(2,Az_smaples - az_int -1 - num_az)
    Omiga_p = np.zeros((np.size(analyse_num),np.size(list)+1), dtype=np.float)
    Omiga_p_z = np.zeros(np.size(analyse_num), dtype=np.float)
    fil_Omiga_p = np.zeros(np.size(analyse_num), dtype=np.float)
    fd = np.zeros(np.size(analyse_num), dtype=np.float)
    pha1 = np.zeros(np.size(analyse_num), dtype=np.float)
    Scene_scope = ((R_samples-1) * const.c / fs / 2) / 2
    dk_higha = np.zeros((np.size(analyse_num),r_int_num), dtype='complex128')

    #pulse_pulse processing
    #PP = False

    #intensity
    raw_int = np.abs(raw_data) # * np.conj(raw_data)

    if plot_pattern:
        plt.figure()
        #plt.imshow(np.log10(np.abs(RCS)))
        xs = 2 * const.c / fs * np.linspace(0,R_samples-1,R_samples)
        ys = np.linspace(0,Az_smaples-1,Az_smaples) / PRF
        plt.imshow(np.abs(raw_int))
        #plt.xlim(xs)
        #plt.ylim(ys)
        #plt.imshow(ys,xs,np.abs(raw_int))
        #plt.grid(False)
        plt.xlabel("Range [m]")
        plt.ylabel("Slow time (s)")

    intens_spectrum = np.mean(np.fft.fft(np.abs(raw_data)*2, axis=1),  axis=0)
    aoff = 2
    intens_xspectrum = np.mean(np.fft.fft(raw_data[0:-aoff] * np.conj(raw_data[aoff:]), axis=1), axis=0)
    intens_xispectrum = np.mean(np.fft.fft(raw_int[0:-aoff] * np.conj(raw_int[aoff:]), axis=1), axis=0)

    if plot_spectrum:
        plt.figure()
        #plt.plot(np.linspace(1,np.int(Sp_x/2),np.int(Sp_x/2)-1) / 4 / Scene_scope, 10 * np.log10(np.abs(intens_spectrum[1:np.int(Sp_x/2)]) / np.abs(intens_spectrum[1:np.int(Sp_x/2)]).max()))
        plt.plot(10 * np.log10(np.abs(intens_spectrum[1:np.int(R_samples/2)])))
        plt.plot(10 * np.log10(np.abs(intens_xspectrum[1:np.int(R_samples / 2)])), '--')
        plt.plot(10 * np.log10(np.abs(intens_xispectrum[1:np.int(R_samples / 2)])), ':')
        plt.xlabel("Delta_k [1/m]")
        plt.ylabel("Power (dB)")

    spck_f = np.fft.fftshift(np.fft.fft(raw_data, axis=1), axes=(1,))
    #Calculating the required delta_f value
    delta_f = cal_delta_f(const.c, inc, wave_scale)
    delta_k = delta_f / const.c
    ind_N = np.int(round(delta_k * (2 * Scene_scope) * 2))


    #Extracting the information of wave_scale

    dk_high = spck_f[aoff:,0:r_int_num] * np.conj(spck_f[0:-aoff,ind_N:ind_N+r_int_num])

    analysis_deltaf  = analysis_deltan / 4 / Scene_scope  * const.c

    for ind in range(np.size(analysis_deltan)):
        dk_ind = spck_f[:,0:r_int_num] * np.conj(spck_f[:,ind:ind+r_int_num])
        RCS_power[ind] = np.mean(np.abs(np.mean(dk_ind,axis=0)))
        #RCS_power[ind] = np.abs(np.mean(dk_ind))

    if np.size(list)>0:
        for iii in list:
            for ind in range(np.size(analyse_num)):

            #dk_ind = spck_f[:,0:-1-ind] * np.conj(spck_f[:,ind:-1]) * amp_delta_k
                dk_inda = spck_f[iii:,0:r_int_num] * np.conj(spck_f[0:-iii,ind_N:ind_N+r_int_num])
                #RCS_power[ind_N] = RCS_power[ind_N] + np.mean(np.abs(np.mean(dk_ind1,axis=0)))
                #dk_higha[ind,:] = np.mean(dk_ind1, axis=0)


                #estimate the wave velocity
                dk_higha = np.mean(dk_inda, axis=1)
                #dk_higha = np.mean(dk_high, axis=1)
                #for ind in range(np.size(analyse_num)):
                           #pha = -np.angle(np.mean(dk_higha[analyse_num[ind]:] * np.conj(dk_higha[0:-analyse_num[ind]])))
                pha = -np.angle(np.mean(dk_higha[analyse_num[ind]:analyse_num[ind]+az_int] * np.conj(dk_higha[0:az_int])))
                #pha1[ind] = pha
                Omiga_p[ind,iii] = pha / analyse_num[ind] * PRF

            #dk_ind = spck_f[:,0:-1-ind] * np.conj(spck_f[:,ind:-1]) * amp_delta_k
            print(iii / (np.size(list)+1))
        dk_higha = np.mean(dk_high, axis=1)
        for ind in range(np.size(analyse_num)):
            pha = -np.angle(np.mean(dk_higha[analyse_num[ind]:analyse_num[ind]+az_int] * np.conj(dk_higha[0:az_int])))
            Omiga_p_z[ind] = pha / analyse_num[ind] * PRF

        Omiga_p[:,0] = Omiga_p_z
        fil_Omiga_p = np.mean(Omiga_p,axis=1)
    else:
        dk_higha = np.mean(dk_high, axis=1)
        for ind in range(np.size(analyse_num)):
            print(ind)
            pha = -np.angle(np.mean(dk_higha[analyse_num[ind]:analyse_num[ind]+az_int] * np.conj(dk_higha[0:az_int])))
            Omiga_p_z[ind] = pha / analyse_num[ind] * PRF
        fil_Omiga_p = Omiga_p_z
        #print(v_p)

    #Doppler frequency estimation with different time intervals
    #for ind in range(np.size(analyse_num)):
     #   dk_ind_t = spck_f[0,0:r_int_num_fd] * np.conj(spck_f[analyse_num[ind],ind_N:ind_N+r_int_num_fd])
     #   fd[ind] = np.angle(np.mean(dk_ind_t)) / (analyse_num[ind] / PRF) / 2 / np.pi -  Omiga_p[ind] / 2 / np.pi
        #print(fd[inn,0])

    #Maxim = np.max(RCS_power[1:np.size(analysis_deltan)])
    xx = analysis_deltaf[1:np.size(analysis_deltan)] / 1e6
    xxn = const.c / 2 / analysis_deltaf[1:np.size(analysis_deltan)] / np.sin(12 * np.pi /180)
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(xx, 10*(np.log10(RCS_power[1:np.size(analysis_deltan)])));
    #ax2 = ax1.twiny() # this is the important function
    #ax2.plot(xx,xxn)
    #ax2.invert_xaxis()
        #plt.plot(xx, 10*(np.log10(RCS_power[1:np.size(analysis_deltan)])))
    ax1.set_xlabel("Delta_f [MHz]")
    #ax2.set_xlabel("Wave_length [m]")
    ax1.set_ylabel("Power (dB)")


    plt.figure()
    plt.plot(analyse_num, fil_Omiga_p)
    plt.xlabel("Azimuth interval [Pixel]")
    plt.ylabel("Angular velocity (rad/s)")

    #Remove polyfit
    #poly = np.polyfit(analyse_num,fil_Omiga_p,deg=7)
    #z = np.polyval(poly, analyse_num)
    #plt.plot(analyse_num, z)
    #print(z)

    if np.size(list)>0:
        return Omiga_p
    else:
        return Omiga_p_z
示例#11
0
def surface_rel(cfg_file=None, inc_deg=None, ntimes=2, t_step=10e-3):
    """ This function generates a (short) time series of surface realizations.

        :param scf_file: the full path to the configuration with all OCEANSAR parameters
        :param inc_deg: the incident angle, in degree
        :param ntimes: number of time samples generated.
        :param t_step: spacing between time samples. This can be interpreted as the Pulse Repetition Interval

        :returns: a tuple with the configuration object, the surfaces, the radial velocities for each grid point,
                  and the complex scattering coefficients
    """

    cfg_file = utils.get_parFile(parfile=cfg_file)
    cfg = ocs_io.ConfigFile(cfg_file)
    use_hmtf = cfg.srg.use_hmtf
    scat_spec_enable = cfg.srg.scat_spec_enable
    scat_spec_mode = cfg.srg.scat_spec_mode
    scat_bragg_enable = cfg.srg.scat_bragg_enable
    scat_bragg_model = cfg.srg.scat_bragg_model
    scat_bragg_d = cfg.srg.scat_bragg_d
    scat_bragg_spec = cfg.srg.scat_bragg_spec
    scat_bragg_spread = cfg.srg.scat_bragg_spread

    # SAR
    inc_angle = np.deg2rad(cfg.sar.inc_angle)
    alt = cfg.sar.alt
    f0 = cfg.sar.f0
    prf = cfg.sar.prf
    pol = cfg.sar.pol
    l0 = const.c / f0
    k0 = 2. * np.pi * f0 / const.c
    if pol == 'DP':
        do_hh = True
        do_vv = True
    elif pol == 'hh':
        do_hh = True
        do_vv = False
    else:
        do_hh = False
        do_vv = True
    # OCEAN / OTHERS
    ocean_dt = cfg.ocean.dt
    surface = OceanSurface()
    compute = ['D', 'Diff', 'Diff2', 'V', 'A']
    if use_hmtf:
        compute.append('hMTF')
    surface.init(cfg.ocean.Lx,
                 cfg.ocean.Ly,
                 cfg.ocean.dx,
                 cfg.ocean.dy,
                 cfg.ocean.cutoff_wl,
                 cfg.ocean.spec_model,
                 cfg.ocean.spread_model,
                 np.deg2rad(cfg.ocean.wind_dir),
                 cfg.ocean.wind_fetch,
                 cfg.ocean.wind_U,
                 cfg.ocean.current_mag,
                 np.deg2rad(cfg.ocean.current_dir),
                 0,
                 0,
                 0,
                 0,
                 0,
                 False,
                 cfg.ocean.swell_enable,
                 cfg.ocean.swell_ampl,
                 np.deg2rad(cfg.ocean.swell_dir),
                 cfg.ocean.swell_wl,
                 compute,
                 cfg.ocean.opt_res,
                 cfg.ocean.fft_max_prime,
                 choppy_enable=cfg.ocean.choppy_enable)
    # Get a surface realization calculated
    surface.t = 0
    return surface
示例#12
0
def sar_focus(cfg_file, raw_output_file, output_file):

    ###################
    # INITIALIZATIONS #
    ###################

    print(
        '-------------------------------------------------------------------')
    print(
        time.strftime("- OCEANSAR SAR Processor: %Y-%m-%d %H:%M:%S",
                      time.localtime()))
    print(
        '-------------------------------------------------------------------')

    # CONFIGURATION FILE
    cfg = tpio.ConfigFile(cfg_file)

    # PROCESSING
    az_weighting = cfg.processing.az_weighting
    doppler_bw = cfg.processing.doppler_bw
    plot_format = cfg.processing.plot_format
    plot_tex = cfg.processing.plot_tex
    plot_save = cfg.processing.plot_save
    plot_path = cfg.processing.plot_path
    plot_raw = cfg.processing.plot_raw
    plot_rcmc_dopp = cfg.processing.plot_rcmc_dopp
    plot_rcmc_time = cfg.processing.plot_rcmc_time
    plot_image_valid = cfg.processing.plot_image_valid

    # SAR
    f0 = cfg.sar.f0
    prf = cfg.sar.prf
    num_ch = cfg.sar.num_ch
    alt = cfg.sar.alt
    v_ground = cfg.sar.v_ground
    rg_bw = cfg.sar.rg_bw
    over_fs = cfg.sar.over_fs

    # CALCULATE PARAMETERS
    l0 = const.c / f0
    if v_ground == 'auto':
        v_ground = geo.orbit_to_vel(alt, ground=True)
    rg_sampling = rg_bw * over_fs

    # RAW DATA
    raw_file = tpio.RawFile(raw_output_file, 'r')
    raw_data = raw_file.get('raw_data*')
    sr0 = raw_file.get('sr0')
    raw_file.close()

    # OTHER INITIALIZATIONS
    # Create plots directory
    plot_path = os.path.dirname(output_file) + os.sep + plot_path
    if plot_save:
        if not os.path.exists(plot_path):
            os.makedirs(plot_path)

    slc = []

    ########################
    # PROCESSING MAIN LOOP #
    ########################
    for ch in np.arange(num_ch):

        if plot_raw:
            utils.image(np.real(raw_data[0, ch]),
                        min=-np.max(np.abs(raw_data[0, ch])),
                        max=np.max(np.abs(raw_data[0, ch])),
                        cmap='gray',
                        aspect=np.float(raw_data[0, ch].shape[1]) /
                        np.float(raw_data[0, ch].shape[0]),
                        title='Raw Data',
                        xlabel='Range samples',
                        ylabel='Azimuth samples',
                        usetex=plot_tex,
                        save=plot_save,
                        save_path=plot_path + os.sep + 'plot_raw_real_%d.%s' %
                        (ch, plot_format),
                        dpi=150)
            utils.image(np.imag(raw_data[0, ch]),
                        min=-np.max(np.abs(raw_data[0, ch])),
                        max=np.max(np.abs(raw_data[0, ch])),
                        cmap='gray',
                        aspect=np.float(raw_data[0, ch].shape[1]) /
                        np.float(raw_data[0, ch].shape[0]),
                        title='Raw Data',
                        xlabel='Range samples',
                        ylabel='Azimuth samples',
                        usetex=plot_tex,
                        save=plot_save,
                        save_path=plot_path + os.sep + 'plot_raw_imag_%d.%s' %
                        (ch, plot_format),
                        dpi=150)
            utils.image(np.abs(raw_data[0, ch]),
                        min=0,
                        max=np.max(np.abs(raw_data[0, ch])),
                        cmap='gray',
                        aspect=np.float(raw_data[0, ch].shape[1]) /
                        np.float(raw_data[0, ch].shape[0]),
                        title='Raw Data',
                        xlabel='Range samples',
                        ylabel='Azimuth samples',
                        usetex=plot_tex,
                        save=plot_save,
                        save_path=plot_path + os.sep + 'plot_raw_amp_%d.%s' %
                        (ch, plot_format),
                        dpi=150)
            utils.image(np.angle(raw_data[0, ch]),
                        min=-np.pi,
                        max=np.pi,
                        cmap='gray',
                        aspect=np.float(raw_data[0, ch].shape[1]) /
                        np.float(raw_data[0, ch].shape[0]),
                        title='Raw Data',
                        xlabel='Range samples',
                        ylabel='Azimuth samples',
                        usetex=plot_tex,
                        save=plot_save,
                        save_path=plot_path + os.sep + 'plot_raw_phase_%d.%s' %
                        (ch, plot_format),
                        dpi=150)

        # Optimize matrix sizes
        az_size_orig, rg_size_orig = raw_data[0, ch].shape
        optsize = utils.optimize_fftsize(raw_data[0, ch].shape)
        optsize = [raw_data.shape[0], optsize[0], optsize[1]]
        data = np.zeros(optsize, dtype=complex)
        data[:, :raw_data[0, ch].shape[0], :raw_data[
            0, ch].shape[1]] = raw_data[:, ch, :, :]

        az_size, rg_size = data.shape[1:]

        # RCMC Correction
        print('Applying RCMC correction... [Channel %d/%d]' % (ch + 1, num_ch))

        #fr = np.linspace(-rg_sampling/2., rg_sampling/2., rg_size)
        fr = (np.arange(rg_size) - rg_size / 2) * rg_sampling / rg_size
        fr = np.roll(fr, int(-rg_size / 2))

        fa = (np.arange(az_size) - az_size / 2) * prf / az_size
        fa = np.roll(fa, int(-az_size / 2))

        ## Compensation of ANTENNA PATTERN
        ## FIXME this will not work for a long separation betwen Tx and Rx!!!
        ant_L = cfg.sar.ant_L
        # fa = 2 * v_orb / l0 * sin_az
        sin_az = fa * l0 / (2 * v_ground)
        if cfg.sar.L_total:
            beam_pattern = sinc_1tx_nrx(sin_az,
                                        ant_L * num_ch,
                                        f0,
                                        num_ch,
                                        field=True)
        else:
            beam_pattern = sinc_1tx_nrx(sin_az, ant_L, f0, 1, field=True)
        #fa[az_size/2:] = fa[az_size/2:] - prf
        rcmc_fa = sr0 / np.sqrt(1 - (fa * (l0 / 2.) / v_ground)**2.) - sr0

        data = np.fft.fft2(data)

        #        for i in np.arange(az_size):
        #            data[i,:] *= np.exp(1j*2*np.pi*2*rcmc_fa[i]/const.c*fr)
        data = (data * np.exp(4j * np.pi * rcmc_fa.reshape(
            (1, az_size, 1)) / const.c * fr.reshape((1, 1, rg_size))))
        data = np.fft.ifft(data, axis=2)

        if plot_rcmc_dopp:
            utils.image(np.abs(data[0]),
                        min=0.,
                        max=3. * np.mean(np.abs(data)),
                        cmap='gray',
                        aspect=np.float(rg_size) / np.float(az_size),
                        title='RCMC Data (Range Dopler Domain)',
                        usetex=plot_tex,
                        save=plot_save,
                        save_path=plot_path + os.sep + 'plot_rcmc_dopp_%d.%s' %
                        (ch, plot_format))

        if plot_rcmc_time:
            rcmc_time = np.fft.ifft(data[0],
                                    axis=0)[:az_size_orig, :rg_size_orig]
            rcmc_time_max = np.max(np.abs(rcmc_time))
            utils.image(np.real(rcmc_time),
                        min=-rcmc_time_max,
                        max=rcmc_time_max,
                        cmap='gray',
                        aspect=np.float(rg_size) / np.float(az_size),
                        title='RCMC Data (Time Domain)',
                        usetex=plot_tex,
                        save=plot_save,
                        save_path=plot_path + os.sep +
                        'plot_rcmc_time_real_%d.%s' % (ch, plot_format))
            utils.image(np.imag(rcmc_time),
                        min=-rcmc_time_max,
                        max=rcmc_time_max,
                        cmap='gray',
                        aspect=np.float(rg_size) / np.float(az_size),
                        title='RCMC Data (Time Domain)',
                        usetex=plot_tex,
                        save=plot_save,
                        save_path=plot_path + os.sep +
                        'plot_rcmc_time_imag_%d.%s' % (ch, plot_format))

        # Azimuth compression
        print('Applying azimuth compression... [Channel %d/%d]' %
              (ch + 1, num_ch))

        n_samp = 2 * (np.int(doppler_bw / (fa[1] - fa[0])) / 2)
        weighting = (az_weighting - (1. - az_weighting) *
                     np.cos(2 * np.pi * np.linspace(0, 1., int(n_samp))))
        # Compensate amplitude loss

        L_win = np.sum(np.abs(weighting)**2) / weighting.size
        weighting /= np.sqrt(L_win)
        if fa.size > n_samp:
            zeros = np.zeros(az_size)
            zeros[0:int(n_samp)] = weighting
            # zeros[:n_samp/2] = weighting[:n_samp/2]
            # zeros[-n_samp/2:] = weighting[-n_samp/2:]
            weighting = np.roll(zeros, int(-n_samp / 2))
        weighting = np.where(
            np.abs(beam_pattern) > 0, weighting / beam_pattern, 0)
        ph_ac = 4. * np.pi / l0 * sr0 * \
            (np.sqrt(1. - (fa * l0 / 2. / v_ground)**2.) - 1.)
        #        for i in np.arange(rg_size):
        #            data[:,i] *= np.exp(1j*ph_ac)*weighting
        data = data * (np.exp(1j * ph_ac) * weighting).reshape((1, az_size, 1))

        data = np.fft.ifft(data, axis=1)

        print('Finishing... [Channel %d/%d]' % (ch + 1, num_ch))
        # Reduce to initial dimension
        data = data[:, :int(az_size_orig), :int(rg_size_orig)]

        # Removal of non valid samples
        n_val_az_2 = np.floor(doppler_bw / 2. /
                              (2. * v_ground**2. / l0 / sr0) * prf / 2.) * 2.
        # data = raw_data[ch, n_val_az_2:(az_size_orig - n_val_az_2 - 1), :]
        data = data[:, int(n_val_az_2):int(az_size_orig - n_val_az_2 - 1), :]
        if plot_image_valid:
            plt.figure()
            plt.imshow(np.abs(data[0]),
                       origin='lower',
                       vmin=0,
                       vmax=np.max(np.abs(data)),
                       aspect=np.float(rg_size_orig) / np.float(az_size_orig),
                       cmap='gray')
            plt.xlabel("Range")
            plt.ylabel("Azimuth")
            plt.savefig(
                os.path.join(plot_path,
                             ('plot_image_valid_%d.%s' % (ch, plot_format))))

        slc.append(data)

    # Save processed data
    slc = np.array(slc, dtype=np.complex)
    print("Shape of SLC: " + str(slc.shape), flush=True)
    proc_file = tpio.ProcFile(output_file, 'w', slc.shape)
    proc_file.set('slc*', slc)
    proc_file.close()

    print('-----------------------------------------')
    print(
        time.strftime("Processing finished [%Y-%m-%d %H:%M:%S]",
                      time.localtime()))
    print('-----------------------------------------')
示例#13
0
def l2_wavespectrum(cfg_file, proc_output_file, ocean_file, output_file):

    print(
        '-------------------------------------------------------------------')
    print(
        time.strftime("- OCEANSAR L2 Wavespectra: [%Y-%m-%d %H:%M:%S]",
                      time.localtime()))
    print(
        '-------------------------------------------------------------------')

    print('Initializing...')

    ## CONFIGURATION FILE
    cfg = tpio.ConfigFile(cfg_file)

    # SAR
    inc_angle = np.deg2rad(cfg.sar.inc_angle)
    f0 = cfg.sar.f0
    prf = cfg.sar.prf
    num_ch = cfg.sar.num_ch
    ant_L = cfg.sar.ant_L
    alt = cfg.sar.alt
    v_ground = cfg.sar.v_ground
    rg_bw = cfg.sar.rg_bw
    over_fs = cfg.sar.over_fs
    pol = cfg.sar.pol
    if pol == 'DP':
        polt = ['hh', 'vv']
    elif pol == 'hh':
        polt = ['hh']
    else:
        polt = ['vv']
        # L2 wavespectrum
    rg_ml = cfg.L2_wavespectrum.rg_ml
    az_ml = cfg.L2_wavespectrum.az_ml
    krg_ml = cfg.L2_wavespectrum.krg_ml
    kaz_ml = cfg.L2_wavespectrum.kaz_ml
    ml_win = cfg.L2_wavespectrum.ml_win
    plot_save = cfg.L2_wavespectrum.plot_save
    plot_path = cfg.L2_wavespectrum.plot_path
    plot_format = cfg.L2_wavespectrum.plot_format
    plot_tex = cfg.L2_wavespectrum.plot_tex
    plot_surface = cfg.L2_wavespectrum.plot_surface
    plot_proc_ampl = cfg.L2_wavespectrum.plot_proc_ampl
    plot_spectrum = cfg.L2_wavespectrum.plot_spectrum
    n_sublook = cfg.L2_wavespectrum.n_sublook
    sublook_weighting = cfg.L2_wavespectrum.sublook_az_weighting

    ## CALCULATE PARAMETERS
    if v_ground == 'auto': v_ground = geosar.orbit_to_vel(alt, ground=True)
    k0 = 2. * np.pi * f0 / const.c
    rg_sampling = rg_bw * over_fs

    # PROCESSED RAW DATA
    proc_content = tpio.ProcFile(proc_output_file, 'r')
    proc_data = proc_content.get('slc*')
    proc_content.close()

    # OCEAN SURFACE
    surface = OceanSurface()
    surface.load(ocean_file, compute=['D', 'V'])
    surface.t = 0.

    # OUTPUT FILE
    output = open(output_file, 'w')

    # OTHER INITIALIZATIONS
    # Enable TeX
    if plot_tex:
        plt.rc('font', family='serif')
        plt.rc('text', usetex=True)

    # Create plots directory
    plot_path = os.path.dirname(output_file) + os.sep + plot_path
    if plot_save:
        if not os.path.exists(plot_path):
            os.makedirs(plot_path)

    # SURFACE VELOCITIES
    grg_grid_spacing = (const.c / 2. / rg_sampling / np.sin(inc_angle))
    rg_res_fact = grg_grid_spacing / surface.dx
    az_grid_spacing = (v_ground / prf)
    az_res_fact = az_grid_spacing / surface.dy
    res_fact = np.ceil(np.sqrt(rg_res_fact * az_res_fact))

    # SURFACE RADIAL VELOCITY
    v_radial_surf = surface.Vx * np.sin(inc_angle) - surface.Vz * np.cos(
        inc_angle)
    v_radial_surf_ml = utils.smooth(utils.smooth(v_radial_surf,
                                                 res_fact * rg_ml,
                                                 axis=1),
                                    res_fact * az_ml,
                                    axis=0)
    v_radial_surf_mean = np.mean(v_radial_surf)
    v_radial_surf_std = np.std(v_radial_surf)
    v_radial_surf_ml_std = np.std(v_radial_surf_ml)

    # Expected mean azimuth shift
    sr0 = geosar.inc_to_sr(inc_angle, alt)
    avg_az_shift = -v_radial_surf_mean / v_ground * sr0
    std_az_shift = v_radial_surf_std / v_ground * sr0

    print('Starting Wavespectrum processing...')

    # Get dimensions & calculate region of interest
    rg_span = surface.Lx
    az_span = surface.Ly
    rg_size = proc_data[0].shape[2]
    az_size = proc_data[0].shape[1]

    # Note: RG is projected, so plots are Ground Range
    rg_min = 0
    rg_max = np.int(rg_span / (const.c / 2. / rg_sampling / np.sin(inc_angle)))
    az_min = np.int(az_size / 2. + (-az_span / 2. + avg_az_shift) /
                    (v_ground / prf))
    az_max = np.int(az_size / 2. + (az_span / 2. + avg_az_shift) /
                    (v_ground / prf))
    az_guard = np.int(std_az_shift / (v_ground / prf))
    az_min = az_min + az_guard
    az_max = az_max - az_guard
    if (az_max - az_min) < (2 * az_guard - 10):
        print('Not enough edge-effect free image')
        return

    # Adaptive coregistration
    if cfg.sar.L_total:
        ant_L = ant_L / np.float(num_ch)
        dist_chan = ant_L / 2
    else:
        if np.float(cfg.sar.Spacing) != 0:
            dist_chan = np.float(cfg.sar.Spacing) / 2
        else:
            dist_chan = ant_L / 2
    # dist_chan = ant_L/num_ch/2.
    print('ATI Spacing: %f' % dist_chan)
    inter_chan_shift_dist = dist_chan / (v_ground / prf)
    # Subsample shift in azimuth
    for chind in range(proc_data.shape[0]):
        shift_dist = -chind * inter_chan_shift_dist
        shift_arr = np.exp(
            -2j * np.pi * shift_dist *
            np.roll(np.arange(az_size) - az_size / 2, int(-az_size / 2)) /
            az_size)
        shift_arr = shift_arr.reshape((1, az_size, 1))
        proc_data[chind] = np.fft.ifft(np.fft.fft(proc_data[chind], axis=1) *
                                       shift_arr,
                                       axis=1)

    # First dimension is number of channels, second is number of pols
    ch_dim = proc_data.shape[0:2]
    npol = ch_dim[1]
    proc_data_rshp = [np.prod(ch_dim), proc_data.shape[2], proc_data.shape[3]]
    # Compute extended covariance matrices...
    proc_data = proc_data.reshape(proc_data_rshp)
    # Intensities
    i_all = []
    for chind in range(proc_data.shape[0]):
        this_i = utils.smooth(utils.smooth(np.abs(proc_data[chind])**2.,
                                           rg_ml,
                                           axis=1,
                                           window=ml_win),
                              az_ml,
                              axis=0,
                              window=ml_win)
        i_all.append(this_i[az_min:az_max, rg_min:rg_max])
    i_all = np.array(i_all)

    ## Wave spectra computation
    ## Processed Doppler bandwidth
    proc_bw = cfg.processing.doppler_bw
    PRF = cfg.sar.prf
    fa = np.fft.fftfreq(proc_data_rshp[1], 1 / PRF)
    # Filters
    sublook_filt = []
    sublook_bw = proc_bw / n_sublook
    for i_sbl in range(n_sublook):
        fa_min = -1 * proc_bw / 2 + i_sbl * sublook_bw
        fa_max = fa_min + sublook_bw
        fa_c = (fa_max + fa_min) / 2
        win = np.where(
            np.logical_and(fa > fa_min, fa < fa_max),
            (sublook_weighting -
             (1 - sublook_weighting) * np.cos(2 * np.pi *
                                              (fa - fa_min) / sublook_bw)), 0)
        sublook_filt.append(win)

    # Apply sublooks
    az_downsmp = int(np.floor(az_ml / 2))
    rg_downsmp = int(np.floor(rg_ml / 2))
    sublooks = []
    sublooks_f = []
    for i_sbl in range(n_sublook):
        # Go to frequency domain
        sublook_data = np.fft.ifft(np.fft.fft(proc_data, axis=1) *
                                   sublook_filt[i_sbl].reshape(
                                       (1, proc_data_rshp[1], 1)),
                                   axis=1)
        # Get intensities
        sublook_data = np.abs(sublook_data)**2
        # Multilook
        for chind in range(proc_data.shape[0]):
            sublook_data[chind] = utils.smooth(utils.smooth(
                sublook_data[chind], rg_ml, axis=1),
                                               az_ml,
                                               axis=0)
        # Keep only valid part and down sample
        sublook_data = sublook_data[:, az_min:az_max:az_downsmp,
                                    rg_min:rg_max:rg_downsmp]
        sublooks.append(sublook_data)
        sublooks_f.append(
            np.fft.fft(np.fft.fft(sublook_data - np.mean(sublook_data),
                                  axis=1),
                       axis=2))

    kaz = 2 * np.pi * np.fft.fftfreq(sublook_data.shape[1],
                                     az_downsmp * az_grid_spacing)
    kgrg = 2 * np.pi * np.fft.fftfreq(sublook_data.shape[2],
                                      rg_downsmp * grg_grid_spacing)

    xspecs = []
    tind = 0
    xspec_lut = np.zeros((len(sublooks), len(sublooks)), dtype=int)

    for ind1 in range(len(sublooks)):
        for ind2 in range(ind1 + 1, len(sublooks)):
            xspec_lut[ind1, ind2] = tind
            tind = tind + 1
            xspec = sublooks_f[ind1] * np.conj(sublooks_f[ind2])
            xspecs.append(xspec)

    with open(output_file, 'wb') as output:
        pickle.dump(xspecs, output, pickle.HIGHEST_PROTOCOL)
        pickle.dump([kaz, kgrg], output, pickle.HIGHEST_PROTOCOL)

    # PROCESSED AMPLITUDE
    if plot_proc_ampl:
        for pind in range(npol):
            save_path = (plot_path + os.sep + 'amp_dB_' + polt[pind] + '.' +
                         plot_format)
            plt.figure()
            plt.imshow(utils.db(i_all[pind]),
                       aspect='equal',
                       origin='lower',
                       vmin=utils.db(np.max(i_all[pind])) - 20,
                       extent=[0., rg_span, 0., az_span],
                       interpolation='nearest',
                       cmap='viridis')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("Amplitude")
            plt.colorbar()
            plt.savefig(save_path)

            save_path = (plot_path + os.sep + 'amp_' + polt[pind] + '.' +
                         plot_format)
            int_img = (i_all[pind])**0.5
            vmin = np.mean(int_img) - 3 * np.std(int_img)
            vmax = np.mean(int_img) + 3 * np.std(int_img)
            plt.figure()
            plt.imshow(int_img,
                       aspect='equal',
                       origin='lower',
                       vmin=vmin,
                       vmax=vmax,
                       extent=[0., rg_span, 0., az_span],
                       interpolation='nearest',
                       cmap='viridis')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("Amplitude")
            plt.colorbar()
            plt.savefig(save_path)

    ## FIXME: I am plotting the cross spectrum for the first polarization and the first channel only, which is not
    ## very nice. To be fixed, in particular por multiple polarizations

    for ind1 in range(len(sublooks)):
        for ind2 in range(ind1 + 1, len(sublooks)):
            save_path_abs = os.path.join(plot_path,
                                         ('xspec_abs_%i%i.%s' %
                                          (ind1 + 1, ind2 + 1, plot_format)))
            save_path_pha = os.path.join(plot_path,
                                         ('xspec_pha_%i%i.%s' %
                                          (ind1 + 1, ind2 + 1, plot_format)))
            save_path_im = os.path.join(plot_path,
                                        ('xspec_im_%i%i.%s' %
                                         (ind1 + 1, ind2 + 1, plot_format)))
            ml_xspec = utils.smooth(utils.smooth(np.fft.fftshift(
                xspecs[xspec_lut[ind1, ind2]][0]),
                                                 krg_ml,
                                                 axis=1),
                                    kaz_ml,
                                    axis=0)
            plt.figure()
            plt.imshow(np.abs(ml_xspec),
                       origin='lower',
                       cmap='inferno_r',
                       extent=[kgrg.min(),
                               kgrg.max(),
                               kaz.min(),
                               kaz.max()],
                       interpolation='nearest')
            plt.grid(True)
            pltax = plt.gca()
            pltax.set_xlim((-0.1, 0.1))
            pltax.set_ylim((-0.1, 0.1))
            northarr_length = 0.075  # np.min([surface_full.kx.max(), surface_full.ky.max()])
            pltax.arrow(0,
                        0,
                        -northarr_length * np.sin(np.radians(cfg.sar.heading)),
                        northarr_length * np.cos(np.radians(cfg.sar.heading)),
                        fc="k",
                        ec="k")
            plt.xlabel('$k_x$ [rad/m]')
            plt.ylabel('$k_y$ [rad/m]')
            plt.colorbar()
            plt.savefig(save_path_abs)
            plt.close()
            plt.figure()
            ml_xspec_pha = np.angle(ml_xspec)
            ml_xspec_im = np.imag(ml_xspec)
            immax = np.abs(ml_xspec_im).max()
            whimmax = np.abs(ml_xspec_im).flatten().argmax()
            phmax = np.abs(ml_xspec_pha.flatten()[whimmax])
            plt.imshow(ml_xspec_pha,
                       origin='lower',
                       cmap='bwr',
                       extent=[kgrg.min(),
                               kgrg.max(),
                               kaz.min(),
                               kaz.max()],
                       interpolation='nearest',
                       vmin=-2 * phmax,
                       vmax=2 * phmax)
            plt.grid(True)
            pltax = plt.gca()
            pltax.set_xlim((-0.1, 0.1))
            pltax.set_ylim((-0.1, 0.1))
            northarr_length = 0.075  # np.min([surface_full.kx.max(), surface_full.ky.max()])
            pltax.arrow(0,
                        0,
                        -northarr_length * np.sin(np.radians(cfg.sar.heading)),
                        northarr_length * np.cos(np.radians(cfg.sar.heading)),
                        fc="k",
                        ec="k")
            plt.xlabel('$k_x$ [rad/m]')
            plt.ylabel('$k_y$ [rad/m]')
            plt.colorbar()
            plt.savefig(save_path_pha)
            plt.close()
            plt.figure()
            plt.imshow(ml_xspec_im,
                       origin='lower',
                       cmap='bwr',
                       extent=[kgrg.min(),
                               kgrg.max(),
                               kaz.min(),
                               kaz.max()],
                       interpolation='nearest',
                       vmin=-2 * immax,
                       vmax=2 * immax)
            plt.grid(True)
            pltax = plt.gca()
            pltax.set_xlim((-0.1, 0.1))
            pltax.set_ylim((-0.1, 0.1))
            northarr_length = 0.075  # np.min([surface_full.kx.max(), surface_full.ky.max()])
            pltax.arrow(0,
                        0,
                        -northarr_length * np.sin(np.radians(cfg.sar.heading)),
                        northarr_length * np.cos(np.radians(cfg.sar.heading)),
                        fc="k",
                        ec="k")
            plt.xlabel('$k_x$ [rad/m]')
            plt.ylabel('$k_y$ [rad/m]')
            plt.colorbar()
            plt.savefig(save_path_im)
            plt.close()
示例#14
0
def ati_process(cfg_file, insar_output_file, ocean_file, output_file):

    print(
        '-------------------------------------------------------------------')
    print(
        time.strftime("- OCEANSAR ATI Processor: [%Y-%m-%d %H:%M:%S]",
                      time.localtime()))
    print(
        '-------------------------------------------------------------------')

    print('Initializing...')

    ## CONFIGURATION FILE
    cfg = tpio.ConfigFile(cfg_file)

    # SAR
    pol = cfg.sar.pol
    if pol == 'DP':
        polt = ['hh', 'vv']
    elif pol == 'hh':
        polt = ['hh']
    else:
        polt = ['vv']
    # ATI

    ml_win = cfg.ati.ml_win
    plot_save = cfg.ati.plot_save
    plot_path = cfg.ati.plot_path
    plot_format = cfg.ati.plot_format
    plot_tex = cfg.ati.plot_tex
    plot_surface = cfg.ati.plot_surface
    plot_proc_ampl = cfg.ati.plot_proc_ampl
    plot_coh = cfg.ati.plot_coh
    plot_coh_all = cfg.ati.plot_coh_all
    plot_ati_phase = cfg.ati.plot_ati_phase
    plot_ati_phase_all = cfg.ati.plot_ati_phase_all
    plot_vel_hist = cfg.ati.plot_vel_hist
    plot_vel = cfg.ati.plot_vel

    # PROCESSED InSAR L1b DATA
    insar_data = tpio.L1bFile(insar_output_file, 'r')
    i_all = insar_data.get('ml_intensity')
    cohs = insar_data.get('ml_coherence') * np.exp(
        1j * insar_data.get('ml_phase'))
    coh_lut = insar_data.get('coh_lut')
    sr0 = insar_data.get('sr0')
    inc_angle = insar_data.get('inc_angle')
    b_ati = insar_data.get('b_ati')
    b_xti = insar_data.get('b_xti')
    f0 = insar_data.get('f0')
    az_sampling = insar_data.get('az_sampling')
    num_ch = insar_data.get('num_ch')
    rg_sampling = insar_data.get('rg_sampling')
    v_ground = insar_data.get('v_ground')
    alt = insar_data.get('orbit_alt')
    inc_angle = np.deg2rad(insar_data.get('inc_angle'))
    rg_ml = insar_data.get('rg_ml')
    az_ml = insar_data.get('az_ml')
    insar_data.close()

    # CALCULATE PARAMETERS
    k0 = 2. * np.pi * f0 / const.c

    # OCEAN SURFACE
    surface = OceanSurface()
    surface.load(ocean_file, compute=['D', 'V'])
    surface.t = 0.

    # OUTPUT FILE
    output = open(output_file, 'w')

    # OTHER INITIALIZATIONS
    # Enable TeX
    if plot_tex:
        plt.rc('font', family='serif')
        plt.rc('text', usetex=True)

    # Create plots directory
    plot_path = os.path.dirname(output_file) + os.sep + plot_path
    if plot_save:
        if not os.path.exists(plot_path):
            os.makedirs(plot_path)

    # SURFACE VELOCITIES
    grg_grid_spacing = (const.c / 2. / rg_sampling / np.sin(inc_angle))
    rg_res_fact = grg_grid_spacing / surface.dx
    az_grid_spacing = (v_ground / az_sampling)
    az_res_fact = az_grid_spacing / surface.dy
    res_fact = np.ceil(np.sqrt(rg_res_fact * az_res_fact))

    # SURFACE RADIAL VELOCITY
    v_radial_surf = surface.Vx * np.sin(inc_angle) - surface.Vz * np.cos(
        inc_angle)
    v_radial_surf_ml = utils.smooth(utils.smooth(v_radial_surf,
                                                 res_fact * rg_ml,
                                                 axis=1),
                                    res_fact * az_ml,
                                    axis=0)
    v_radial_surf_mean = np.mean(v_radial_surf)
    v_radial_surf_std = np.std(v_radial_surf)
    v_radial_surf_ml_std = np.std(v_radial_surf_ml)

    # SURFACE HORIZONTAL VELOCITY
    v_horizo_surf = surface.Vx
    v_horizo_surf_ml = utils.smooth(utils.smooth(v_horizo_surf,
                                                 res_fact * rg_ml,
                                                 axis=1),
                                    res_fact * az_ml,
                                    axis=0)
    v_horizo_surf_mean = np.mean(v_horizo_surf)
    v_horizo_surf_std = np.std(v_horizo_surf)
    v_horizo_surf_ml_std = np.std(v_horizo_surf_ml)

    # Expected mean azimuth shift
    sr0 = geosar.inc_to_sr(inc_angle, alt)
    avg_az_shift = -v_radial_surf_mean / v_ground * sr0
    std_az_shift = v_radial_surf_std / v_ground * sr0

    az_guard = np.int(std_az_shift / (v_ground / az_sampling))
    ##################
    # ATI PROCESSING #
    ##################

    print('Starting ATI processing...')

    # Get dimensions & calculate region of interest
    rg_span = surface.Lx
    az_span = surface.Ly

    # First dimension is number of channels, second is number of pols
    ch_dim = i_all.shape[0:2]
    npol = ch_dim[1]

    print('Generating plots and estimating values...')

    # SURFACE HEIGHT
    if plot_surface:
        plt.figure()
        plt.imshow(surface.Dz,
                   cmap="ocean",
                   extent=[0, surface.Lx, 0, surface.Ly],
                   origin='lower')
        plt.title('Surface Height')
        plt.xlabel('Ground range [m]')
        plt.ylabel('Azimuth [m]')
        cbar = plt.colorbar()
        cbar.ax.set_xlabel('[m]')

        if plot_save:
            plt.savefig(plot_path + os.sep + 'plot_surface.' + plot_format,
                        bbox_inches='tight')
            plt.close()
        else:
            plt.show()

    # PROCESSED AMPLITUDE
    if plot_proc_ampl:
        for pind in range(npol):
            save_path = (plot_path + os.sep + 'amp_dB_' + polt[pind] + '.' +
                         plot_format)
            plt.figure()
            plt.imshow(utils.db(i_all[0, pind]),
                       aspect='equal',
                       origin='lower',
                       vmin=utils.db(np.max(i_all[pind])) - 20,
                       extent=[0., rg_span, 0., az_span],
                       interpolation='nearest',
                       cmap='viridis')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("Amplitude")
            plt.colorbar()
            plt.savefig(save_path)
            plt.close()
            save_path = (plot_path + os.sep + 'amp_' + polt[pind] + '.' +
                         plot_format)
            int_img = (i_all[0, pind])**0.5
            vmin = np.mean(int_img) - 3 * np.std(int_img)
            vmax = np.mean(int_img) + 3 * np.std(int_img)
            plt.figure()
            plt.imshow(int_img,
                       aspect='equal',
                       origin='lower',
                       vmin=vmin,
                       vmax=vmax,
                       extent=[0., rg_span, 0., az_span],
                       interpolation='nearest',
                       cmap='viridis')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("Amplitude")
            plt.colorbar()
            plt.savefig(save_path)
            plt.close()

    if plot_coh and ch_dim[0] > 1:
        for pind in range(npol):
            save_path = (plot_path + os.sep + 'ATI_coh_' + polt[pind] +
                         polt[pind] + '.' + plot_format)
            coh_ind = coh_lut[0, pind, 1, pind]
            plt.figure()
            plt.imshow(np.abs(cohs[coh_ind]),
                       aspect='equal',
                       origin='lower',
                       vmin=0,
                       vmax=1,
                       extent=[0., rg_span, 0., az_span],
                       cmap='bone')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("ATI Coherence")
            # plt.colorbar()
            plt.savefig(save_path)

    # ATI PHASE

    tau_ati = b_ati / v_ground

    ati_phases = []
    # Hack to avoid interferogram computation if there are no interferometric channels
    if num_ch > 1:
        npol_ = npol
    else:
        npol_ = 0
    for pind in range(npol_):
        save_path = (plot_path + os.sep + 'ATI_pha_' + polt[pind] +
                     polt[pind] + '.' + plot_format)
        coh_ind = coh_lut[(0, pind, 1, pind)]
        ati_phase = uwphase(cohs[coh_ind])
        ati_phases.append(ati_phase)
        v_radial_est = -ati_phase / tau_ati[1] / (k0 * 2.)
        if plot_ati_phase:
            phase_mean = np.mean(ati_phase)
            phase_std = np.std(ati_phase)
            vmin = np.max([
                -np.abs(phase_mean) - 4 * phase_std, -np.abs(ati_phase).max()
            ])
            vmax = np.min(
                [np.abs(phase_mean) + 4 * phase_std,
                 np.abs(ati_phase).max()])
            plt.figure()
            plt.imshow(ati_phase,
                       aspect='equal',
                       origin='lower',
                       vmin=vmin,
                       vmax=vmax,
                       extent=[0., rg_span, 0., az_span],
                       cmap='hsv')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("ATI Phase")
            plt.colorbar()
            plt.savefig(save_path)

            save_path = (plot_path + os.sep + 'ATI_rvel_' + polt[pind] +
                         polt[pind] + '.' + plot_format)
            vmin = -np.abs(v_radial_surf_mean) - 4. * v_radial_surf_std
            vmax = np.abs(v_radial_surf_mean) + 4. * v_radial_surf_std
            plt.figure()
            plt.imshow(v_radial_est,
                       aspect='equal',
                       origin='lower',
                       vmin=vmin,
                       vmax=vmax,
                       extent=[0., rg_span, 0., az_span],
                       cmap='bwr')
            plt.xlabel('Ground range [m]')
            plt.ylabel('Azimuth [m]')
            plt.title("Estimated Radial Velocity " + polt[pind])
            plt.colorbar()
            plt.savefig(save_path)

    if npol_ == 4:  # Bypass this for now
        # Cross pol interferogram
        coh_ind = coh_lut[(0, 1)]
        save_path = (plot_path + os.sep + 'POL_coh_' + polt[0] + polt[1] +
                     '.' + plot_format)
        utils.image(np.abs(cohs[coh_ind]),
                    max=1,
                    min=0,
                    aspect='equal',
                    cmap='gray',
                    extent=[0., rg_span, 0., az_span],
                    xlabel='Ground range [m]',
                    ylabel='Azimuth [m]',
                    title='XPOL Coherence',
                    usetex=plot_tex,
                    save=plot_save,
                    save_path=save_path)
        save_path = (plot_path + os.sep + 'POL_pha_' + polt[0] + polt[1] +
                     '.' + plot_format)
        ati_phase = uwphase(cohs[coh_ind])
        phase_mean = np.mean(ati_phase)
        phase_std = np.std(ati_phase)
        vmin = np.max([-np.abs(phase_mean) - 4 * phase_std, -np.pi])
        vmax = np.min([np.abs(phase_mean) + 4 * phase_std, np.pi])
        utils.image(ati_phase,
                    aspect='equal',
                    min=vmin,
                    max=vmax,
                    cmap=utils.bwr_cmap,
                    extent=[0., rg_span, 0., az_span],
                    xlabel='Ground range [m]',
                    ylabel='Azimuth [m]',
                    title='XPOL Phase',
                    cbar_xlabel='[rad]',
                    usetex=plot_tex,
                    save=plot_save,
                    save_path=save_path)

    if num_ch > 1:
        ati_phases = np.array(ati_phases)

        output.write('--------------------------------------------\n')
        output.write('SURFACE RADIAL VELOCITY - NO SMOOTHING\n')
        output.write('MEAN(SURF. V) = %.4f\n' % v_radial_surf_mean)
        output.write('STD(SURF. V) = %.4f\n' % v_radial_surf_std)
        output.write('--------------------------------------------\n\n')

        output.write('--------------------------------------------\n')
        output.write(
            'SURFACE RADIAL VELOCITY - SMOOTHING (WIN. SIZE=%dx%d)\n' %
            (az_ml, rg_ml))
        output.write('MEAN(SURF. V) = %.4f\n' % v_radial_surf_mean)
        output.write('STD(SURF. V) = %.4f\n' % v_radial_surf_ml_std)
        output.write('--------------------------------------------\n\n')

        output.write('--------------------------------------------\n')
        output.write('SURFACE HORIZONTAL VELOCITY - NO SMOOTHING\n')
        output.write('MEAN(SURF. V) = %.4f\n' % v_horizo_surf_mean)
        output.write('STD(SURF. V) = %.4f\n' % v_horizo_surf_std)
        output.write('--------------------------------------------\n\n')

        if plot_vel_hist:
            # PLOT RADIAL VELOCITY
            plt.figure()

            plt.hist(v_radial_surf.flatten(),
                     200,
                     density=True,
                     histtype='step')
            #plt.hist(v_radial_surf_ml.flatten(), 500, density=True, histtype='step')
            plt.grid(True)
            plt.xlim([
                -np.abs(v_radial_surf_mean) - 4. * v_radial_surf_std,
                np.abs(v_radial_surf_mean) + 4. * v_radial_surf_std
            ])
            plt.xlabel('Radial velocity [m/s]')
            plt.ylabel('PDF')
            plt.title('Surface velocity')

            if plot_save:
                plt.savefig(plot_path + os.sep + 'TRUE_radial_vel_hist.' +
                            plot_format)
                plt.close()
            else:
                plt.show()

            plt.figure()
            plt.hist(v_radial_surf_ml.flatten(),
                     200,
                     density=True,
                     histtype='step')
            #plt.hist(v_radial_surf_ml.flatten(), 500, density=True, histtype='step')
            plt.grid(True)
            plt.xlim([
                -np.abs(v_radial_surf_mean) - 4. * v_radial_surf_std,
                np.abs(v_radial_surf_mean) + 4. * v_radial_surf_std
            ])
            plt.xlabel('Radial velocity [m/s]')
            plt.ylabel('PDF')
            plt.title('Surface velocity (low pass filtered)')

            if plot_save:
                plt.savefig(plot_path + os.sep + 'TRUE_radial_vel_ml_hist.' +
                            plot_format)
                plt.close()
            else:
                plt.show()

        if plot_vel:

            utils.image(
                v_radial_surf,
                aspect='equal',
                cmap=utils.bwr_cmap,
                extent=[0., rg_span, 0., az_span],
                xlabel='Ground range [m]',
                ylabel='Azimuth [m]',
                title='Surface Radial Velocity',
                cbar_xlabel='[m/s]',
                min=-np.abs(v_radial_surf_mean) - 4. * v_radial_surf_std,
                max=np.abs(v_radial_surf_mean) + 4. * v_radial_surf_std,
                usetex=plot_tex,
                save=plot_save,
                save_path=plot_path + os.sep + 'TRUE_radial_vel.' +
                plot_format)
            utils.image(
                v_radial_surf_ml,
                aspect='equal',
                cmap=utils.bwr_cmap,
                extent=[0., rg_span, 0., az_span],
                xlabel='Ground range [m]',
                ylabel='Azimuth [m]',
                title='Surface Radial Velocity',
                cbar_xlabel='[m/s]',
                min=-np.abs(v_radial_surf_mean) - 4. * v_radial_surf_std,
                max=np.abs(v_radial_surf_mean) + 4. * v_radial_surf_std,
                usetex=plot_tex,
                save=plot_save,
                save_path=plot_path + os.sep + 'TRUE_radial_vel_ml.' +
                plot_format)

        ##  ESTIMATED VELOCITIES

        # Note: plot limits are taken from surface calculations to keep the same ranges

        # ESTIMATE RADIAL VELOCITY
        v_radial_ests = -ati_phases / tau_ati[1] / (k0 * 2.)

        # ESTIMATE HORIZONTAL VELOCITY
        v_horizo_ests = -ati_phases / tau_ati[1] / (k0 *
                                                    2.) / np.sin(inc_angle)

        #Trim edges
        v_radial_ests = v_radial_ests[:, az_guard:-az_guard, 5:-5]
        v_horizo_ests = v_horizo_ests[:, az_guard:-az_guard, 5:-5]
        output.write('--------------------------------------------\n')
        output.write('ESTIMATED RADIAL VELOCITY - NO SMOOTHING\n')
        for pind in range(npol):
            output.write("%s Polarization\n" % polt[pind])
            output.write('MEAN(EST. V) = %.4f\n' %
                         np.mean(v_radial_ests[pind]))
            output.write('STD(EST. V) = %.4f\n' % np.std(v_radial_ests[pind]))
        output.write('--------------------------------------------\n\n')

        output.write('--------------------------------------------\n')
        output.write(
            'ESTIMATED RADIAL VELOCITY - SMOOTHING (WIN. SIZE=%dx%d)\n' %
            (az_ml, rg_ml))
        for pind in range(npol):
            output.write("%s Polarization\n" % polt[pind])
            output.write('MEAN(EST. V) = %.4f\n' % np.mean(
                utils.smooth(utils.smooth(v_radial_ests[pind], rg_ml, axis=1),
                             az_ml,
                             axis=0)))
            output.write('STD(EST. V) = %.4f\n' % np.std(
                utils.smooth(utils.smooth(v_radial_ests[pind], rg_ml, axis=1),
                             az_ml,
                             axis=0)))
        output.write('--------------------------------------------\n\n')

        output.write('--------------------------------------------\n')
        output.write('ESTIMATED HORIZONTAL VELOCITY - NO SMOOTHING\n')
        for pind in range(npol):
            output.write("%s Polarization\n" % polt[pind])
            output.write('MEAN(EST. V) = %.4f\n' %
                         np.mean(v_horizo_ests[pind]))
            output.write('STD(EST. V) = %.4f\n' % np.std(v_horizo_ests[pind]))
        output.write('--------------------------------------------\n\n')

    # Processed NRCS

    NRCS_est_avg = 10 * np.log10(
        np.mean(np.mean(i_all[:, :, az_guard:-az_guard, 5:-5], axis=-1),
                axis=-1))
    output.write('--------------------------------------------\n')
    for pind in range(npol):
        output.write("%s Polarization\n" % polt[pind])
        output.write('Estimated mean NRCS = %5.2f\n' % NRCS_est_avg[0, pind])
    output.write('--------------------------------------------\n\n')

    # Some bookkeeping information
    output.write('--------------------------------------------\n')
    output.write('GROUND RANGE GRID SPACING = %.4f\n' % grg_grid_spacing)
    output.write('AZIMUTH GRID SPACING = %.4f\n' % az_grid_spacing)
    output.write('--------------------------------------------\n\n')

    output.close()

    if plot_vel_hist and num_ch > 1:
        # PLOT RADIAL VELOCITY
        plt.figure()
        plt.hist(v_radial_surf.flatten(),
                 200,
                 density=True,
                 histtype='step',
                 label='True')
        for pind in range(npol):
            plt.hist(v_radial_ests[pind].flatten(),
                     200,
                     density=True,
                     histtype='step',
                     label=polt[pind])
        plt.grid(True)
        plt.xlim([
            -np.abs(v_radial_surf_mean) - 4. * v_radial_surf_std,
            np.abs(v_radial_surf_mean) + 4. * v_radial_surf_std
        ])
        plt.xlabel('Radial velocity [m/s]')
        plt.ylabel('PDF')
        plt.title('Estimated velocity')
        plt.legend()

        if plot_save:
            plt.savefig(plot_path + os.sep + 'ATI_radial_vel_hist.' +
                        plot_format)
            plt.close()
        else:
            plt.show()

    # Save some statistics to npz file
    #
    if num_ch > 1:
        filenpz = os.path.join(os.path.dirname(output_file), 'ati_stats.npz')
        # Mean coh
        cohs = np.array(cohs)[:, az_guard:-az_guard, 5:-5]

        np.savez(filenpz,
                 nrcs=NRCS_est_avg,
                 v_r_dop=np.mean(np.mean(v_radial_ests, axis=-1), axis=-1),
                 v_r_surf=v_radial_surf_mean,
                 v_r_surf_std=v_radial_surf_std,
                 coh_mean=np.mean(np.mean(cohs, axis=-1), axis=-1),
                 abscoh_mean=np.mean(np.mean(np.abs(cohs), axis=-1), axis=-1),
                 coh_lut=coh_lut,
                 pols=polt)
    print('----------------------------------------')
    print(
        time.strftime("ATI Processing finished [%Y-%m-%d %H:%M:%S]",
                      time.localtime()))
    print('----------------------------------------')
示例#15
0
def postprocess_batch_sim(template_file,
                          plots=True,
                          fontsize=14,
                          pltsymb=['o', 'D', 's', '^', 'p']):
    cfg_file = utils.get_parFile(parfile=template_file)
    ref_cfg = osrio.ConfigFile(cfg_file)
    step = 0
    npol = (2 if ref_cfg.sar.pol == 'DP' else 1)
    nch = int(ref_cfg.sar.num_ch)
    nim = nch * npol

    sim_path_ref = ref_cfg.sim.path + os.sep + 'sim_U%d_wdir%d_smag%d_sdir%d_inc%d_i%i'
    n_all = (np.size(v_wind_U) * np.size(v_wind_dir) * np.size(v_current_mag) *
             np.size(v_current_dir) * np.size(v_inc_angle) * n_rep)
    mean_cohs = np.zeros(
        (np.size(v_wind_U), np.size(v_wind_dir), np.size(v_current_mag),
         np.size(v_current_dir), np.size(v_inc_angle), n_rep,
         int((nim) * (nim - 1) / 2)),
        dtype=np.complex)
    mean_abscohs = np.zeros(
        (np.size(v_wind_U), np.size(v_wind_dir), np.size(v_current_mag),
         np.size(v_current_dir), np.size(v_inc_angle), n_rep,
         int((nim) * (nim - 1) / 2)),
        dtype=np.float)
    nrcs = np.zeros(
        (np.size(v_wind_U), np.size(v_wind_dir), np.size(v_current_mag),
         np.size(v_current_dir), np.size(v_inc_angle), n_rep, nch * npol),
        dtype=np.float)
    v_r_dop = np.zeros(
        (np.size(v_wind_U), np.size(v_wind_dir), np.size(v_current_mag),
         np.size(v_current_dir), np.size(v_inc_angle), n_rep, npol),
        dtype=np.float)
    for ind_w_U in range(np.size(v_wind_U)):
        for ind_w_dir in range(np.size(v_wind_dir)):
            for ind_c_mag in range(np.size(v_current_mag)):
                for ind_c_dir in range(np.size(v_current_dir)):
                    for ind_inc in range(np.size(v_inc_angle)):
                        for i_rep in range(n_rep):
                            # Read data
                            path = sim_path_ref % (v_wind_U[ind_w_U],
                                                   v_wind_dir[ind_w_dir],
                                                   v_current_mag[ind_c_mag],
                                                   v_current_dir[ind_c_dir],
                                                   v_inc_angle[ind_inc], i_rep)
                            try:
                                data_filename = os.path.join(
                                    path, 'ati_stats.npz')
                                npzfile = np.load(data_filename)
                                mean_cohs[ind_w_U, ind_w_dir, ind_c_mag,
                                          ind_c_dir, ind_inc,
                                          i_rep] = npzfile['coh_mean']
                                mean_abscohs[ind_w_U, ind_w_dir, ind_c_mag,
                                             ind_c_dir, ind_inc,
                                             i_rep] = npzfile['abscoh_mean']
                                nrcs[ind_w_U, ind_w_dir, ind_c_mag, ind_c_dir,
                                     ind_inc, i_rep] = npzfile['nrcs']
                                v_r_dop[ind_w_U, ind_w_dir, ind_c_mag,
                                        ind_c_dir, ind_inc,
                                        i_rep] = npzfile['v_r_dop']
                                coh_lut = npzfile['coh_lut']
                            except OSError:
                                print("Issues with %s" % data_filename)
                                mean_cohs[ind_w_U, ind_w_dir, ind_c_mag,
                                          ind_c_dir, ind_inc, i_rep] = np.NaN
                                mean_abscohs[ind_w_U, ind_w_dir, ind_c_mag,
                                             ind_c_dir, ind_inc,
                                             i_rep] = np.NaN
                                nrcs[ind_w_U, ind_w_dir, ind_c_mag, ind_c_dir,
                                     ind_inc, i_rep] = np.NaN
                                v_r_dop[ind_w_U, ind_w_dir, ind_c_mag,
                                        ind_c_dir, ind_inc, i_rep] = np.NaN
    if plots:
        font = {'family': "Arial", 'weight': 'normal', 'size': fontsize}
        mpl.rc('font', **font)

        for ind_inc in range(np.size(v_inc_angle)):
            plt.figure()
            for ind in range(np.size(v_wind_U)):
                plt.plot(v_wind_dir,
                         np.abs(mean_cohs[ind, :, 0, 0, ind_inc, 0, 4]),
                         pltsymb[int(np.mod(ind, len(pltsymb)))],
                         label=("U = %2.1f" % (v_wind_U[ind])))
            plt.xlabel("Wind direction w.r.t. radar LOS [deg]")
            plt.ylabel("$\gamma$")
            plt.title(r"Coherence at $\theta_i=%i$" %
                      int(v_inc_angle[ind_inc]))
            plt.legend(loc=0)
            plt.grid(True)
            plt.tight_layout()

        plt.figure()
        for ind in range(np.size(v_wind_U)):
            plt.plot(v_wind_dir,
                     v_r_dop[ind, :, 0, 0, 1, 0, 1],
                     pltsymb[int(np.mod(ind, len(pltsymb)))],
                     label=("U = %2.1f" % (v_wind_U[ind])))

        plt.xlabel("Wind direction w.r.t. radar LOS [deg]")
        plt.ylabel("$v_{Dop} [m/s]$")
        plt.legend(loc=0)
        plt.grid(True)
        plt.tight_layout()

        plt.figure()
        for ind in range(np.size(v_wind_U)):
            plt.plot(v_wind_dir,
                     nrcs[ind, :, 0, 0, 1, 0, 1],
                     pltsymb[int(np.mod(ind, len(pltsymb)))],
                     label=("U = %2.1f" % (v_wind_U[ind])))

        plt.xlabel("Wind direction w.r.t. radar LOS [deg]")
        plt.ylabel("$NRCS [dB]$")
        plt.legend(loc=0)
        plt.grid(True)
        plt.tight_layout()

    return mean_cohs, mean_abscohs, v_r_dop, nrcs, coh_lut