コード例 #1
0
def run_nadir(file_param):

    if os.path.isfile(file_param):
#    basedir=os.path.dirname(swotsimulator.__file__)
        shutil.copyfile(file_param, 'params.py') #os.path.join(basedir,'params.py'))
    else: 
        print("Error: No such file: '%s'" % file_param)
        sys.exit()
    try: 
        import params as p
    except:
        if os.path.isfile('params.py'):
            print("There is a wrong entry in your params file")
            import params
        else: 
            print("Error: No params.py module found")
            sys.exit()
    import swotsimulator.build_swath as build_swath
    import swotsimulator.rw_data as rw_data
    import swotsimulator.build_error as build_error
    import swotsimulator.mod_tools as mod_tools

## - Initialize some parameters values
    try: p.shift_lon=p.shift_lon
    except: p.shift_lon=None
    try: p.shift_time=p.shift_time
    except: p.shift_time=None
    try: model=p.model
    except: model='NETCDF_MODEL'; p.model= model
    try: p.model_nan=p.model_nan
    except: p.model_nan=0.
    try:   p.SSH_factor=p.SSH_factor
    except: p.SSH_factor=1. #; p.SSH_factor=SSH_factor
    p.halfswath=0.


## - Read list of user model files """
    if p.file_input:
        list_file = [line.strip() for line in open(p.file_input)]

## - Read model input coordinates """
    if p.file_input: model_data= eval('rw_data.'+model+'(file=p.indatadir+os.sep+list_file[0])')
    if  p.modelbox:
        modelbox=numpy.array(p.modelbox, dtype='float')
        if modelbox[0]<0: modelbox[0]=modelbox[0]+360
        if modelbox[1]<0:modelbox[1]=modelbox[1]+360
    else: 
        try: modelbox=model_data.calc_box()
        except: 
            print('Modelbox could not be computed, a modelbox should be given if no model file is provided')
            sys.exit()
    if p.file_input: 
        model_data.read_coordinates()
        model_index=numpy.where(((modelbox[0]-1)<=model_data.vlon) & (model_data.vlon<=(modelbox[1]+1)) & ((modelbox[2]-1)<=model_data.vlat) & (model_data.vlat<=(modelbox[3]+1)))#[0]


## - Initialize random coefficients that are use to compute 
##   random errors following the specified spectrum
    errnad=build_error.errornadir(p)
    if p.file_coeff:
        if os.path.isfile(p.file_coeff[:-3]+'_nadir.nc') and (not p.makesgrid):
            print('\n WARNING: old random coefficient file are used')
            errnad.load_coeff(p)
        else:
            errnad.init_error(p)
            errnad.save_coeff(p)
    else: 
        errnad.init_error(p)
## - Compute interpolated SSH and errors for each pass, at each
##   cycle
    print('Compute interpolated SSH and errors:')
##   load all SWOT grid files (one for each pass)
##   model time step
    modeltime=numpy.arange(0,p.nstep*p.timestep, p.timestep) 
##   remove the grid from the list of model files
    if p.file_input: list_file.remove(list_file[0])
##   initialize progress bar variables
    istep=0; ntot=1
## - Loop on SWOT grid files
 # for ifile in p.filsat:
    istring=len(p.dir_setup)
    if not isinstance(p.filesat, list): 
        p.filesat=[p.filesat]
    for filesat in p.filesat:
##   load SWOT grid file
        ntmp, nfilesat=os.path.split(filesat[istring:-4])
        if p.makesgrid:
            print('\n Force creation of satellite grid')
            orb=build_swath.makeorbit(modelbox, p, orbitfile=filesat) 
            orb.file=p.outdatadir+os.sep+nfilesat+'_grid.nc'
            orb.write_orb()
        else:
            orb = rw_data.Sat_nadir(file=p.filesgrid+'.nc')
            cycle=0 ; x_al=[] ;  al_cycle=0 ; timeshift=0
            orb.load_orb(cycle=cycle,x_al=x_al, al_cycle=al_cycle, timeshift=timeshift)
        cycle=orb.cycle ; x_al=orb.x_al ; al_cycle=orb.al_cycle ; timeshift=orb.timeshift
##   Look for model indices corresponding to the localization of the pass
##   If the pass is not in the model region, continue to next loop
        if numpy.min(orb.lon)<1. and numpy.max(orb.lon)>359.:
            orb.lon[numpy.where(orb.lon>180.)]=orb.lon[numpy.where(orb.lon>180.)]-360
            if p.file_input: model_data.vlon[numpy.where(model_data.vlon>180.)]=model_data.vlon[numpy.where(model_data.vlon>180.)]-360
            if modelbox[0]>180.: modelbox[0]=modelbox[0]-360
            if modelbox[1]>180.: modelbox[1]=modelbox[1]-360
        if p.file_input: 
            model_index=numpy.where(((numpy.min(orb.lon))<=model_data.vlon) & (model_data.vlon<=(numpy.max(orb.lon))) & ((numpy.min(orb.lat))<=model_data.vlat) & (model_data.vlat<=(numpy.max(orb.lat))))
            lonmodel1d=model_data.vlon[model_index].ravel()
            latmodel1d=model_data.vlat[model_index].ravel()
            if not model_index[0].any():
                continue
##   Compute number of cycles needed to cover all nstep model timesteps
        rcycle=(p.timestep*p.nstep)/float(orb.cycle)
        ncycle=int(rcycle)
##   Switch to (-180,180) longitude range to interpolate near 360-0

## - Loop on all cycles
        for cycle in range(0,ncycle+1):
##   Initialiaze errors and SSH
            errnad.nadir=numpy.zeros((numpy.shape(orb.x_al)[0]))
            errnad.wet_tropo1=numpy.zeros((numpy.shape(orb.x_al)[0]))
            errnad.wt=numpy.zeros((numpy.shape(orb.x_al)[0]))
            date1=cycle*orb.cycle
            SSH_true=numpy.zeros((numpy.shape(orb.x_al)[0])) #, p.nstep))
            #SSH_obs=numpy.empty((numpy.shape(orb.x_al)[0]))
            vindice=numpy.zeros(numpy.shape(SSH_true))
## Definition of the time in the model 
            stime=orb.time+date1
## Look for satellite data that are beween step-p.timesetp/2 end setp+p.step/2
            if p.file_input: 
                index=numpy.where(((stime[-1]-timeshift)>=(modeltime-p.timestep/2.)) & ((stime[0]-timeshift)<(modeltime+p.timestep/2.)) )#[0]
## At each step, look for the corresponding time in the satellite data
                for ifile in index[0]:
                    ntot=1
                    mod_tools.update_progress(float(istep)/float(p.nstep*ntot*numpy.shape(p.filesat)[0]), 'Sat: '+ filesat[istring:], 'model file: '+list_file[ifile] +', cycle:'+str(cycle+1))
                    #mod_tools.update_progress(float(istep)/float(p.nstep*ntot), 'Sat: '+ filesat[istring:], 'model file: '+list_file[ifile] +', cycle:'+str(cycle+1))
## If there are satellite data, Get true SSH from model
                    if numpy.shape(index)[1]>0:
                        ntot=ntot+numpy.shape(index)[1]-1
                        #if numpy.shape(index)[1]>1:
                        ind=numpy.where(((stime-timeshift)>=(modeltime[ifile]-p.timestep/2.)) & ((stime-timeshift)<(modeltime[ifile]+p.timestep/2.)) )
                        exec('model_step=rw_data.'+model+'(file=p.indatadir+os.sep+list_file[ifile], var=p.var)')
                        model_step.read_var(index=model_index)
                        SSH_model=model_step.vvar
                        SSH_true[ind[0]]=interpolate.griddata((lonmodel1d, latmodel1d), SSH_model.ravel(), (orb.lon[ind[0]], orb.lat[ind[0]]), method=p.interpolation)
                        if p.interpolation=='nearest':
                            if modelbox[0]>modelbox[1]:
                                SSH_true[numpy.where(((orb.lon<modelbox[0]) & (orb.lon>modelbox[1])) | (orb.lat<modelbox[2]) | (orb.lat>modelbox[3]))] = numpy.nan 
                            else:
                                SSH_true[numpy.where((orb.lon<modelbox[0]) | (orb.lon>modelbox[1]) | (orb.lat<modelbox[2]) | (orb.lat>modelbox[3]))] = numpy.nan 
                        #SSH_true[numpy.where((orb.lon<modelbox[0]) | (orb.lon>modelbox[1]) | (orb.lat<modelbox[2]) | (orb.lat>modelbox[3]))] = numpy.nan 
                        vindice[ind[0]]=ifile
                        del ind, SSH_model, model_step
                    istep+=1
            else:
                istep+=1
                mod_tools.update_progress(float(istep)/float(rcycle*numpy.shape(p.filesat)[0]), 'Sat: '+ filesat[istring:], 'no model file provide '+', cycle:'+str(cycle+1))

            SSH_true[SSH_true==0]=numpy.nan
            errnad.make_error(orb, cycle, SSH_true, p) #, ind[0])
            if p.wet_tropo: errnad.SSH=SSH_true+errnad.nadir+errnad.wet_tropo1 #make_SSH_errornadir(SSH_true,p)
            else: errnad.SSH=SSH_true+errnad.nadir #make_SSH_errornadir(SSH_true,p)
## Save outputs in a netcdf file
            if vindice.any() or not p.file_input:
                file_output=p.file_output+'_c'+str(cycle+1).zfill(2)+'.nc'
                Output=rw_data.Sat_nadir(file=file_output, lon=(orb.lon+360)%360, lat=orb.lat, time=stime, x_al=orb.x_al, cycle=orb.cycle)
                Output.write_data(SSH_model=SSH_true, index=vindice, nadir_err=errnad.nadir, SSH_obs=errnad.SSH, pd_err_1b=errnad.wet_tropo1,pd=errnad.wt) #, ncycle=cycle)
        del stime
        if p.file_input: del index
        #orb.lon=(orb.lon+360)%360
        #if p.file_input: model_data.vlon=(model_data.vlon+360)%360
        #modelbox[0]=(modelbox[0]+360)%360
        #modelbox[1]=(modelbox[1]+360)%360
        del orb
## - Write Selected parameters in a txt file
    rw_data.write_params(p,p.outdatadir+os.sep+'orbit_simulator.output')
    print("\n Simulated orbit files have been written in " + p.outdatadir)
    print("----------------------------------------------------------")
コード例 #2
0
def run_nadir(p, die_on_error=False):

    # - Initialize some parameters values
    timestart = datetime.datetime.now()
    mod_tools.initialize_parameters(p)
    p.nadir = True
    p.karin = False
    p.phase = False
    p.roll = False
    p.baseline_dilation = False
    p.timing = False
    p.halfswath = 60.

    # - Progress bar variables are global
    global ntot

    # Build model time steps from parameter file
    modeltime = numpy.arange(0, p.nstep * p.timestep, p.timestep)
    # - Read list of user model files """
    if p.file_input is not None:
        list_file = [line.strip() for line in open(p.file_input)]
        if len(modeltime) > len(list_file):
            logger.error('There is not enough model files in the list of '
                         'files')
            sys.exit(1)
    else:
        list_file = None

    # ############################################
    # Select the spatial and temporal domains
    # ############################################

    # - Read model input coordinates '''
    model_data, list_file = mod.load_coordinate_model(p)
    # if no modelbox is specified (modelbox=None), the domain of the input data
    # is taken as a modelbox
    # coordinates from the region defined by modelbox are selected
    logger.debug('Read input')
    if p.modelbox is not None:
        modelbox = numpy.array(p.modelbox, dtype='float')
        # Use convert to 360 data
        modelbox[0] = (modelbox[0] + 360) % 360
        if modelbox[1] != 360:
            modelbox[1] = (modelbox[1] + 360) % 360
    else:
        if p.file_input is not None:
            modelbox = model_data.calc_box()
        else:
            logger.error('modelbox should be provided if no model file is '
                         'provided')
            sys.exit()
    p.modelbox_calc = modelbox
    logger.debug(p.file_input)
    if p.file_input is not None:
        model_data.read_coordinates()
        # Select model data in the region modelbox
        model_data.len_coord = len(numpy.shape(model_data.vlon))
        if p.grid == 'regular' or model_data.len_coord == 1:
            if modelbox[0] < modelbox[1]:
                _i_lon = numpy.where(((modelbox[0] - 1) <= model_data.vlon)
                                     & (model_data.vlon <=
                                        (modelbox[1] + 1)))[0]
            else:
                _i_lon = numpy.where(((modelbox[0] - 1) <= model_data.vlon)
                                     | (model_data.vlon <= (modelbox[1] +
                                                            1)))[0]
            model_data.model_index_lon = _i_lon
            _i_lat = numpy.where(((modelbox[2] - 1) <= model_data.vlat)
                                 & (model_data.vlat <= (modelbox[3] + 1)))[0]

            model_data.model_index_lat = _i_lat
            model_data.vlon = model_data.vlon[model_data.model_index_lon]
            model_data.vlat = model_data.vlat[model_data.model_index_lat]

        else:
            if modelbox[0] < modelbox[1]:
                _i_box = numpy.where(((modelbox[0] - 1) <= model_data.vlon)
                                     & (model_data.vlon <= (modelbox[1] + 1))
                                     & ((modelbox[2] - 1) <= model_data.vlat)
                                     & (model_data.vlat <= (modelbox[3] + 1)))
            else:
                _i_box = numpy.where(((modelbox[0] - 1) <= model_data.vlon)
                                     | (model_data.vlon <= (modelbox[1] + 1))
                                     & ((modelbox[2] - 1) <= model_data.vlat)
                                     & (model_data.vlat <= (modelbox[3] + 1)))

            model_data.model_index = _i_box
            model_data.vlon = model_data.vlon[model_data.model_index]
            model_data.vlat = model_data.vlat[model_data.model_index]
        model_data.model = p.model
        model_data.vloncirc = numpy.rad2deg(numpy.unwrap(model_data.vlon))
    # Ugly trick when model box is [0 360] to avoid box being empty (360=0%360)
    if modelbox[1] == 0:
        modelbox[1] = 359.99

    # - Initialize random coefficients that are used to compute
    #   random errors following the specified spectrum
    err, errnad = mod.load_error(p, nadir_alone=nadir_alone)

    # - Compute interpolated SSH and errors for each pass, at each
    #   cycle
    logger.info('Compute interpolated SSH and errors:')
    #   Remove the grid from the list of model files
    if p.file_input:
        list_file.remove(list_file[0])
        if len(modeltime) > len(list_file):
            logger.error('There is not enough model files in the list of'
                         ' files')
            sys.exit(1)
    #   Initialize progress bar variables
    ntot = 1

    #   Initialize list of satellites
    if not isinstance(p.filesat, list):
        p.filesat = [p.filesat]
    for filesat in p.filesat:
        # Select satellite
        # ntmp, nfilesat = os.path.split(filesat[istring:-4])
        nfilesat = os.path.basename(os.path.splitext(filesat)[0])
        # Make satellite orbit grid
        if p.makesgrid is True:
            logger.warning('\n Force creation of satellite grid')
            ngrid = build_swath.makeorbit(modelbox, p, orbitfile=filesat)
            ngrid.file = '{}{}_grid.nc'.format((p.filesgrid).strip(),
                                               nfilesat.strip())
            ngrid.write_orb()
            ngrid.ipass = nfilesat
            ngrid.gridfile = '{}{}_grid.nc'.format((p.filesgrid).strip(),
                                                   nfilesat.strip())
        else:
            # To be replaced by load_ngrid
            gridfile = '{}{}_grid.nc'.format((p.filesgrid).strip(),
                                             nfilesat.strip())
            ngrid = rw_data.Sat_nadir(nfile=gridfile)
            ngrid.file = gridfile
            ngrid.ipass = nfilesat
            cycle = 0
            x_al = []
            al_cycle = 0
            timeshift = 0
            ngrid.load_orb(cycle=cycle,
                           x_al=x_al,
                           al_cycle=al_cycle,
                           timeshift=timeshift)
            ngrid.loncirc = numpy.rad2deg(numpy.unwrap(ngrid.lon))
            # ngrid=load_ngrid(sgridfile, p)
        # Select model data around the swath to reduce interpolation
        # cost in griddata
        # if p.file_input is not None:
        #    _ind = numpy.where((numpy.min(ngrid.lon) <= model_data.vlon)
        #                       & (model_data.vlon <= numpy.max(ngrid.lon))
        #                       & (numpy.min(ngrid.lat) <= model_data.vlat)
        #                       & (model_data.vlat <= numpy.max(ngrid.lat)))
        #    model_index = _ind
        # - Generate and nadir-like data:
        #   Compute number of cycles needed to cover all nstep model timesteps
        rcycle = (p.timestep * p.nstep) / float(ngrid.cycle)
        ncycle = int(rcycle)
        #   Loop on all cycles
        for cycle in range(0, ncycle + 1):
            ### TODO move this line somwhere where we have ifile information
            #if ifile > (p.nstep/p.timestep + 1):
            #    break
            #   Create SWOT-like and Nadir-like data
            if p.file_input is None:
                model_data = []
            logger.debug('compute SSH nadir')
            nfile = numpy.shape(p.filesat)[0] * rcycle
            create = mod.create_Nadirlikedata(cycle,
                                              nfile,
                                              list_file,
                                              modelbox,
                                              ngrid,
                                              model_data,
                                              modeltime,
                                              errnad,
                                              p,
                                              progress_bar=True)
            SSH_true_nadir, vindice, time, progress = create
            # SSH_true_nadir, vindice_nadir=create_Nadirlikedata(cycle, sgrid,
            # ngrid, model_data, modeltime, err, errnad, p)
            #   Save outputs in a netcdf file
            ngrid.gridfile = filesat
            if (~numpy.isnan(vindice)).any() or p.file_input is None:
                err = errnad
                err.wtnadir = numpy.zeros((1))
                err.wet_tropo2nadir = numpy.zeros((1))
                logger.debug('write file')
                mod.save_Nadir(cycle,
                               ngrid,
                               errnad,
                               err,
                               p,
                               time=time,
                               vindice_nadir=vindice,
                               SSH_true_nadir=SSH_true_nadir)
            del time
            # if p.file_input: del index
        ngrid.lon = (ngrid.lon + 360) % 360
        if p.file_input:
            model_data.vlon = (model_data.vlon + 360) % 360
        modelbox[0] = (modelbox[0] + 360) % 360
        modelbox[1] = (modelbox[1] + 360) % 360
        del ngrid
    if progress != 1:
        str1 = 'All passes have been processed'
        progress = mod_tools.update_progress(1, str1, '')
    # - Write Selected parameters in a txt file
    timestop = datetime.datetime.now()
    timestop = timestop.strftime('%Y%m%dT%H%M%SZ')
    timestart = timestart.strftime('%Y%m%dT%H%M%SZ')
    op_file = 'nadir_simulator_{}_{}.output'.format(timestart, timestop)
    op_file = os.path.join(p.outdatadir, op_file)
    rw_data.write_params(p, op_file)
    logger.info("\nSimulated orbit files have been written in {}".format(
        p.outdatadir))
    logger.info("----------------------------------------------------------")
コード例 #3
0
def run_simulator(p, die_on_error=False, nadir_alone=False):
    '''Main routine to run simulator, input is the imported parameter file,
    no outputs are returned but netcdf grid and data files are written as well
    as a skimulator.output file to store all used parameter.
    '''
    # - Initialize some parameters values
    timestart = datetime.datetime.now()
    mod_tools.initialize_parameters(p)
    mod_tools.check_path(p)

    # - Read list of user model files """
    model_data, list_file = mod.load_coordinate_model(p)
    ## - Read model input coordinates '''
    ## if no modelbox is specified (modelbox=None), the domain of the input
    ## data is taken as a modelbox
    ## coordinates from the region defined by modelbox are selected
    if p.modelbox is not None:
        modelbox = numpy.array(p.modelbox, dtype='float')
        # Use convert to 360 data
        modelbox[0] = (modelbox[0] + 360) % 360
        if modelbox[1] != 360:
            modelbox[1] = (modelbox[1] + 360) % 360
    else:
        if p.file_input is not None:
            modelbox = model_data.calc_box()
        else:
            logger.error('modelbox should be provided if no model file is'
                         'provided')
            sys.exit(1)
    p.modelbox_calc = modelbox
    if p.file_input is not None:
        model_data.read_coordinates()
        # Select model data in the region modelbox
        model_data.len_coord = len(numpy.shape(model_data.vlon))
        if p.grid == 'regular' or model_data.len_coord == 1:
            if modelbox[0] < modelbox[1]:
                _i_lon = numpy.where(((modelbox[0] - 1) <= model_data.vlon)
                                     & (model_data.vlon <=
                                        (modelbox[1] + 1)))[0]
            else:
                _i_lon = numpy.where(((modelbox[0] - 1) <= model_data.vlon)
                                     | (model_data.vlon <= (modelbox[1] +
                                                            1)))[0]
            model_data.model_index_lon = _i_lon
            _i_lat = numpy.where(((modelbox[2] - 1) <= model_data.vlat)
                                 & (model_data.vlat <= (modelbox[3] + 1)))[0]
            model_data.model_index_lat = _i_lat
            model_data.vlon = model_data.vlon[model_data.model_index_lon]
            model_data.vlat = model_data.vlat[model_data.model_index_lat]

        else:
            if modelbox[0] < modelbox[1]:
                _i_box = numpy.where(((modelbox[0] - 1) <= model_data.vlon)
                                     & (model_data.vlon <= (modelbox[1] + 1))
                                     & ((modelbox[2] - 1) <= model_data.vlat)
                                     & (model_data.vlat <= (modelbox[3] + 1)))
            else:
                _i_box = numpy.where(((modelbox[0] - 1) <= model_data.vlon)
                                     | (model_data.vlon <= (modelbox[1] + 1))
                                     & ((modelbox[2] - 1) <= model_data.vlat)
                                     & (model_data.vlat <= (modelbox[3] + 1)))
            model_data.model_index = _i_box
            model_data.vlon = model_data.vlon[model_data.model_index]
            model_data.vlat = model_data.vlat[model_data.model_index]
        model_data.model = p.model
        model_data.vloncirc = numpy.rad2deg(numpy.unwrap(model_data.vlon))
    if modelbox[1] == 0:
        modelbox[1] = 359.99
    # - Make SWOT grid if necessary """
    if p.makesgrid is True:
        logger.info('\n Force creation of SWOT grid')
        # make nadir orbit
        orb = build_swath.makeorbit(modelbox, p, orbitfile=p.filesat)
        # build swath for this orbit
        if nadir_alone is True:
            build_swath.orbit2nadir(modelbox, p, orb, die_on_error)
            logger.info("\n Nadir tracks have been written in "
                        "{}".format(p.outdatadir))
        else:
            build_swath.orbit2swath(modelbox, p, orb, die_on_error)
            logger.info("\n SWOT Grids and nadir tracks have been written in "
                        "{}".format(p.outdatadir))
        logger.info("-----------------------------------------------")

    # - Initialize random coefficients that are used to compute
    #   random errors following the specified spectrum
    err, errnad = mod.load_error(p, nadir_alone=nadir_alone)

    # - Compute interpolated SSH and errors for each pass, at each
    #   cycle
    logger.info('Compute interpolated SSH and errors:')
    #   load all SWOT grid files (one for each pass)
    listsgridfile = sorted(glob.glob(p.filesgrid + '_p*.nc'))
    if not listsgridfile:
        logger.error('\n There is no SWOT grid file in {}, run simulator with'
                     ' option makesgrid set to true in your params'
                     ' file'.format(p.outdatadir))
        sys.exit(1)
    # Build model time steps from parameter file
    modeltime = numpy.arange(0, p.nstep * p.timestep, p.timestep)
    #   Remove the grid from the list of model files
    if p.file_input and p.file_grid_model is None:
        logger.info("WARNING: the first file is not used to build data")
        list_file.remove(list_file[0])
        if len(modeltime) > len(list_file):
            logger.error('There is not enough model files in the list of'
                         'files')
            sys.exit(1)
    # - Loop on SWOT grid files to construct a list of jobs
    jobs = []
    p2 = mod_tools.todict(p)
    for sgridfile in listsgridfile:
        jobs.append([
            sgridfile, p2, listsgridfile, list_file, modelbox, model_data,
            modeltime, err, errnad
        ])
    ok = False
    # - Process list of jobs using multiprocessing
    try:
        ok = make_swot_data(p.proc_count, jobs, die_on_error, p.progress_bar)
    except DyingOnError:
        logger.error('An error occurred and all errors are fatal')
        sys.exit(1)
    # - Write Selected parameters in a txt file
    timestop = datetime.datetime.now()
    timestop = timestop.strftime('%Y%m%dT%H%M%SZ')
    timestart = timestart.strftime('%Y%m%dT%H%M%SZ')
    op_file = 'swot_simulator_{}_{}.output'.format(timestart, timestop)
    op_file = os.path.join(p.outdatadir, op_file)
    rw_data.write_params(p, op_file)
    if ok is True:
        if p.progress_bar is True:
            __ = mod_tools.update_progress(1, 'All passes have been processed',
                                           '')
        else:
            __ = logger.info('All passes have been processed')
        logger.info("\n Simulated swot files have been written in {}".format(
            p.outdatadir))
        logger.info(''.join(['-'] * 61))
        #"----------------------------------------------------------")
        sys.exit(0)
    logger.error('\nERROR: At least one of the outputs was not saved.')
    sys.exit(1)
コード例 #4
0
def run_simulator(p):

    # - Initialize some parameters values
    timestart = datetime.datetime.now()
    mod_tools.initialize_parameters(p)
    mod_tools.check_path(p)

    # - Progress bar variables are global
    global istep
    global ntot

    # - Read list of user model files """
    if p.file_input is not None:
        list_file = [line.strip() for line in open(p.file_input)]
    else:
        list_file = None
    # - Read model input coordinates '''
    # if no modelbox is specified (modelbox=None), the domain of the input
    # data is taken as a modelbox
    # coordinates from the region defined by modelbox are selected
    if p.file_input is not None:
        model_data_ctor = getattr(rw_data, p.model)
        nfile = os.path.join(p.indatadir, list_file[0])
        model_data = model_data_ctor(p, nfile=nfile)
    if p.modelbox is not None:
        modelbox = numpy.array(p.modelbox, dtype='float')
        # Use convert to 360 data
        modelbox[0] = (modelbox[0] + 360) % 360
        if modelbox[1] != 360:
            modelbox[1] = (modelbox[1] + 360) % 360
    else:
        if p.file_input is not None:
            modelbox = model_data.calc_box()
        else:
            logger.error('modelbox should be provided if no model file is'
                         'provided')
            sys.exit(1)
    p.modelbox_calc = modelbox
    if p.file_input is not None:
        model_data.read_coordinates()
        # Select model data in the region modelbox
        model_data.len_coord = len(numpy.shape(model_data.vlon))
        if p.grid == 'regular' or model_data.len_coord == 1:
            if modelbox[0] < modelbox[1]:
                _i_lon = numpy.where(((modelbox[0]-1) <= model_data.vlon)
                                     & (model_data.vlon <= (modelbox[1]+1)))[0]
            else:
                _i_lon = numpy.where(((modelbox[0]-1) <= model_data.vlon)
                                     | (model_data.vlon <= (modelbox[1]+1)))[0]
            model_data.model_index_lon = _i_lon
            _i_lat = numpy.where(((modelbox[2]-1) <= model_data.vlat)
                                 & (model_data.vlat <= (modelbox[3]+1)))[0]
            model_data.model_index_lat = _i_lat
            model_data.vlon = model_data.vlon[model_data.model_index_lon]
            model_data.vlat = model_data.vlat[model_data.model_index_lat]

        else:
            if modelbox[0] < modelbox[1]:
                _i_box = numpy.where(((modelbox[0]-1) <= model_data.vlon)
                                     & (model_data.vlon <= (modelbox[1]+1))
                                     & ((modelbox[2]-1) <= model_data.vlat)
                                     & (model_data.vlat <= (modelbox[3]+1)))
            else:
                _i_box = numpy.where(((modelbox[0]-1) <= model_data.vlon)
                                     | (model_data.vlon <= (modelbox[1]+1))
                                     & ((modelbox[2]-1) <= model_data.vlat)
                                     & (model_data.vlat <= (modelbox[3]+1)))
            model_data.model_index = _i_box
            model_data.vlon = model_data.vlon[model_data.model_index]
            model_data.vlat = model_data.vlat[model_data.model_index]
        model_data.model = p.model
        model_data.vloncirc = numpy.rad2deg(numpy.unwrap(model_data.vlon))
    if modelbox[1] == 0:
        modelbox[1] = 359.99
    # - Make SWOT grid if necessary """
    if p.makesgrid is True:
        logger.info('\n Force creation of SWOT grid')
        orb = build_swath.makeorbit(modelbox, p, orbitfile=p.filesat)
        # build swath for this orbit
        build_swath.orbit2swath(modelbox, p, orb)
        logger.info("\n SWOT Grids and nadir tracks have been written in "
                    "{}".format(p.outdatadir))
        logger.info("-----------------------------------------------")

    # - Initialize random coefficients that are used to compute
    #   random errors following the specified spectrum
    err, errnad = load_error(p)

    # - Compute interpolated SSH and errors for each pass, at each
    #   cycle
    logger.info('Compute interpolated SSH and errors:')
    #   load all SWOT grid files (one for each pass)
    listsgridfile = sorted(glob.glob(p.filesgrid + '_p*.nc'))
    if not listsgridfile:
        logger.error('\n There is no SWOT grid file in {}, run simulator with'
                     ' option makesgrid set to true in your params'
                     ' file'.format(p.outdatadir))
        sys.exit(1)
    # Build model time steps from parameter file
    modeltime = numpy.arange(0, p.nstep*p.timestep, p.timestep)
    #   Remove the grid from the list of model files
    if p.file_input:
        list_file.remove(list_file[0])
        if len(modeltime) > len(list_file):
            logger.error('There is not enough model files in the list of'
                         'files')
            sys.exit(1)
    #   Initialize progress bar variables
    istep = 0
    ntot = 1

    # - Loop on SWOT grid files
    for sgridfile in listsgridfile:
        #   Load SWOT grid files (Swath and nadir)
        sgrid = load_sgrid(sgridfile, p)
        sgrid.gridfile = sgridfile
        if p.nadir is True:
            ngrid = load_ngrid(sgridfile, p)
            ngrid.gridfile = sgridfile
        else:
            ngrid = None
        # Set Teval and nTeval to None to interpolate the mask once
        Teval = None
        nTeval = None
    #   Select model data around the swath to reduce interpolation cost in
    #   griddata

    # - Generate SWOT like and nadir-like data:
    #   Compute number of cycles needed to cover all nstep model timesteps
        rcycle = (p.timestep * p.nstep)/float(sgrid.cycle)
        ncycle = int(rcycle)
    #   Loop on all cycles
        for cycle in range(0, ncycle+1):
            if ifile > (p.nstep/p.timestep + 1):
                break
            #   Create SWOT-like and Nadir-like data
            if not p.file_input:
                model_data = []

            SSH_true, SSH_true_nadir, vindice, vindice_nadir, time, progress, Teval, nTeval, mask_land = create_SWOTlikedata(
                    cycle, numpy.shape(listsgridfile)[0]*rcycle, list_file,
                    modelbox, sgrid, ngrid, model_data, modeltime, err, errnad,
                    p, progress_bar=True, Teval=Teval, nTeval=nTeval)
            #   Save outputs in a netcdf file
            if (~numpy.isnan(vindice)).any() or not p.file_input:
                save_SWOT(cycle, sgrid, err, p, mask_land, time=time, vindice=vindice,
                          SSH_true=SSH_true, save_var=p.save_variables)
                if p.nadir is True:
                    save_Nadir(cycle, ngrid, errnad, err, p, time=time,
                               vindice_nadir=vindice_nadir,
                               SSH_true_nadir=SSH_true_nadir)
            del time
            # if p.file_input: del index
        sgrid.lon = (sgrid.lon + 360) % 360
        if p.nadir is True:
            ngrid.lon = (ngrid.lon + 360) % 360
        if p.file_input is not None:
            model_data.vlon = (model_data.vlon + 360) % 360
        modelbox[0] = (modelbox[0] + 360) % 360
        modelbox[1] = (modelbox[1] + 360) % 360
        del sgrid
        if p.nadir is True:
            del ngrid
    if progress != 1:
        str1 = 'All passes have been processed'
        progress = mod_tools.update_progress(1, str1, '')
    # - Write Selected parameters in a txt file
    timestop = datetime.datetime.now()
    timestop = timestop.strftime('%Y%m%dT%H%M%SZ')
    timestart = timestart.strftime('%Y%m%dT%H%M%SZ')
    op_file = 'swot_simulator_{}_{}.output'.format(timestart, timestop)
    op_file = os.path.join(p.outdatadir, op_file)
    rw_data.write_params(p, op_file)
    logger.info("\n Simulated swot files have been written in {}".format(
                 p.outdatadir))
    logger.info("----------------------------------------------------------")
コード例 #5
0
ファイル: run_simulator.py プロジェクト: jinbow/swotsimulator
def run_simulator(file_param):
    import swotsimulator.build_swath as build_swath
    import swotsimulator.rw_data as rw_data
    import swotsimulator.build_error as build_error
    import swotsimulator.mod_tools as mod_tools
    import swotsimulator.const as const

## - Initialize some parameters values
    try: p.shift_lon=p.shift_lon
    except: p.shift_lon=None
    try: p.shift_time=p.shift_time
    except: p.shift_time=None
    try: model=p.model
    except: model='NETCDF_MODEL'; p.model= model
    try: p.model_nan=p.model_nan
    except: p.model_nan=0.
    try:   p.SSH_factor=p.SSH_factor
    except: p.SSH_factor=1. #; p.SSH_factor=SSH_factor
    try: p.nadir=p.nadir
    except: p.nadir=True
    try: p.grid=p.grid
    except: p.grid='regular'
## - Progress bar variables are global
    global istep
    global ntot
## - Read list of user model files """
    if p.file_input:
        list_file = [line.strip() for line in open(p.file_input)]

## - Read model input coordinates '''
     # if no modelbox is specified (modelbox=None), the domain of the input data is taken as a modelbox
     # coordinates from the region defined by modelbox are selected
    if p.file_input: model_data=eval('rw_data.'+model+'(file=p.indatadir+os.sep+list_file[0])')
    if  p.modelbox:
        modelbox=numpy.array(p.modelbox, dtype='float')
        ## Use convert to 360 data
        modelbox[0]=(modelbox[0]+360)%360
        if modelbox[1] != 360: 
          modelbox[1]=(modelbox[1]+360)%360
    else: 
        if p.file_input:
           modelbox=model_data.calc_box()
        else: 
            print('modelbox should be provided if no model file is provided')
            sys.exit()
    if p.file_input: 
        model_data.read_coordinates()
        ## Select model data in the region modelbox
        if p.grid=='regular':
          if modelbox[0]<modelbox[1]:
            model_data.model_index_lon=numpy.where(((modelbox[0]-1)<=model_data.vlon) & (model_data.vlon<=(modelbox[1]+1)))[0]
          else:
            model_data.model_index_lon=numpy.where(((modelbox[0]-1)<=model_data.vlon) | (model_data.vlon<=(modelbox[1]+1)))[0]
          model_data.model_index_lat=numpy.where(((modelbox[2]-1)<=model_data.vlat) & (model_data.vlat<=(modelbox[3]+1)))[0]
          model_data.vlon=model_data.vlon[model_data.model_index_lon]
          model_data.vlat=model_data.vlat[model_data.model_index_lat]

        else:
          if modelbox[0]<modelbox[1]:
            model_data.model_index=numpy.where(((modelbox[0]-1)<=model_data.vlon) & (model_data.vlon<=(modelbox[1]+1)) & ((modelbox[2]-1)<=model_data.vlat) & (model_data.vlat<=(modelbox[3]+1)))#[0]
          else: 
            model_data.model_index=numpy.where(((modelbox[0]-1)<=model_data.vlon) | (model_data.vlon<=(modelbox[1]+1)) & ((modelbox[2]-1)<=model_data.vlat) & (model_data.vlat<=(modelbox[3]+1)))#[0]

        model_data.model=model
        model_data.vloncirc=numpy.rad2deg(numpy.unwrap(model_data.vlon))
    if modelbox[1]==0: modelbox[1]=359.99
## - Make SWOT grid if necessary """
    if p.makesgrid:
        print('\n Force creation of SWOT grid')
        orb=build_swath.makeorbit(modelbox, p, orbitfile=p.filesat) 
        build_swath.orbit2swath(modelbox, p, orb) 
        print("\n SWOT Grids and nadir tracks have been written in "+ p.outdatadir )
        print("-----------------------------------------------")

## - Initialize random coefficients that are used to compute 
##   random errors following the specified spectrum
    err, errnad=load_error(p)

## - Compute interpolated SSH and errors for each pass, at each
##   cycle
    print('Compute interpolated SSH and errors:')
##   load all SWOT grid files (one for each pass)
    listsgridfile = sorted(glob.glob(p.filesgrid+'_p*.nc'))
    if not listsgridfile:
        print('\n There is no SWOT grid file in '+p.outdatadir+ ', run simulator with option makesgrid set to true in your params file' )
        sys.exit()
##   Model time step
    modeltime=numpy.arange(0,p.nstep*p.timestep, p.timestep) 
##   Remove the grid from the list of model files
    if p.file_input: list_file.remove(list_file[0])
##   Initialize progress bar variables
    istep=0; ntot=1
## - Loop on SWOT grid files
    for sgridfile in listsgridfile:
##   Load SWOT grid files (Swath and nadir)
        sgrid=load_sgrid(sgridfile, p)
        sgrid.gridfile=sgridfile
        if p.nadir: 
            ngrid=load_ngrid(sgridfile, p)
            ngrid.gridfile=sgridfile
##    Select model data around the swath to reduce interpolation cost in griddata
## - Generate SWOT like and nadir-like data:
##   Compute number of cycles needed to cover all nstep model timesteps
        rcycle=(p.timestep*p.nstep)/float(sgrid.cycle)
        ncycle=int(rcycle)
##   Loop on all cycles
        for cycle in range(0,ncycle+1):
            if ifile>(p.nstep/p.timestep +1): break
##   Create SWOT-like and Nadir-like data
            if not p.file_input : model_data=[] 

            SSH_true, SSH_true_nadir, vindice, vindice_nadir, time, progress=create_SWOTlikedata(cycle, numpy.shape(listsgridfile)[0]*rcycle, list_file, modelbox, sgrid, ngrid, model_data, modeltime, err, errnad, p, progress_bar=True)
            #SSH_true_nadir, vindice_nadir=create_Nadirlikedata(cycle, sgrid, ngrid, model_data, modeltime, err, errnad, p)
##   Save outputs in a netcdf file
            if (~numpy.isnan(vindice)).any() or not p.file_input:
                save_SWOT(cycle, sgrid, err, p, time=time, vindice=vindice, SSH_true=SSH_true)
                if p.nadir:
                    save_Nadir(cycle, ngrid, errnad, err , p,time=time, vindice_nadir=vindice_nadir, SSH_true_nadir=SSH_true_nadir)
            del time
            #if p.file_input: del index
        sgrid.lon=(sgrid.lon+360)%360
        ngrid.lon=(ngrid.lon+360)%360
        if p.file_input: model_data.vlon=(model_data.vlon+360)%360
        modelbox[0]=(modelbox[0]+360)%360
        modelbox[1]=(modelbox[1]+360)%360
        del sgrid, ngrid
    if progress!=1: progress=mod_tools.update_progress(1, 'All passes have been processed', '')
## - Write Selected parameters in a txt file
    rw_data.write_params(p,p.outdatadir+os.sep+'swot_simulator.output')
    print("\n Simulated swot files have been written in " + p.outdatadir)
    print("----------------------------------------------------------")
コード例 #6
0
ファイル: run_simulator.py プロジェクト: jinbow/swotsimulator
def run_nadir(file_param):
    if os.path.isfile(file_param):
#    basedir=os.path.dirname(swotsimulator.__file__)
        shutil.copyfile(file_param, 'params.py') #os.path.join(basedir,'params.py'))
    else:
        print("Error: No such file: '%s'" % file_param)
        sys.exit()
    try:
        import params as p
    except:
         if os.path.isfile('params.py'):
            print("There is a wrong entry in your params file" )
            import params
         else:
            print("Error: No params.py module found")
            sys.exit()
    import swotsimulator.build_swath as build_swath
    import swotsimulator.rw_data as rw_data
    import swotsimulator.build_error as build_error
    import swotsimulator.mod_tools as mod_tools
    import swotsimulator.const as const

## - Initialize some parameters values
    try: p.shift_lon=p.shift_lon
    except: p.shift_lon=None
    try: p.shift_time=p.shift_time
    except: p.shift_time=None
    try: model=p.model
    except: model='NETCDF_MODEL'; p.model= model
    try: p.model_nan=p.model_nan
    except: p.model_nan=0.
    try:   p.SSH_factor=p.SSH_factor
    except: p.SSH_factor=1. #; p.SSH_factor=SSH_factor
    try: p.nadir=p.nadir
    except: p.nadir=True
    try: p.grid=p.grid
    except: p.grid='regular'
    p.karin=False ; p.phase=False ; p.roll=False ; p.baseline_dilation=False ; p.timing=False
    p.halfswath=60.
## - Progress bar variables are global
    global istep
    global ntot
## - Read list of user model files """
    if p.file_input:
        list_file = [line.strip() for line in open(p.file_input)]    


## - Read model input coordinates '''
     # if no modelbox is specified (modelbox=None), the domain of the input data is taken as a modelbox
     # coordinates from the region defined by modelbox are selected
    if p.file_input: model_data=eval('rw_data.'+model+'(file=p.indatadir+os.sep+list_file[0])')
    if  p.modelbox:
        modelbox=numpy.array(p.modelbox, dtype='float')
        ## Use convert to 360 data
        modelbox[0]=(modelbox[0]+360)%360
        if modelbox[1] != 360: 
          modelbox[1]=(modelbox[1]+360)%360
    else:
        if p.file_input:
           modelbox=model_data.calc_box()
        else:
            print('modelbox should be provided if no model file is provided')
            sys.exit()
    if p.file_input:
        model_data.read_coordinates()
        ## Select model data in the region modelbox
        if p.grid=='regular':
          model_data.model_index_lon=numpy.where(((modelbox[0]-1)<=model_data.vlon) & (model_data.vlon<=(modelbox[1]+1)))[0]
          model_data.model_index_lat=numpy.where(((modelbox[2]-1)<=model_data.vlat) & (model_data.vlat<=(modelbox[3]+1)))[0]
          model_data.vlon=model_data.vlon[model_data.model_index_lon]
          model_data.vlat=model_data.vlat[model_data.model_index_lat]

        else:
          model_data.model_index=numpy.where(((modelbox[0]-1)<=model_data.vlon) & (model_data.vlon<=(modelbox[1]+1)) & ((modelbox[2]-1)<=model_data.vlat) & (model_data.vlat<=(modelbox[3]+1)))#[0]
        model_data.model=model
        model_data.vloncirc=numpy.rad2deg(numpy.unwrap(model_data.vlon))
## Ugly trick when model box is [0 360] to avoid box being empty (360=0%360)
    if modelbox[1]==0: modelbox[1]=359.99

## - Initialize random coefficients that are used to compute
##   random errors following the specified spectrum
    err, errnad=load_error(p)

## - Compute interpolated SSH and errors for each pass, at each
##   cycle
    print('Compute interpolated SSH and errors:')
##   Model time step
    modeltime=numpy.arange(0,p.nstep*p.timestep, p.timestep)
##   Remove the grid from the list of model files
    if p.file_input: list_file.remove(list_file[0])
##   Initialize progress bar variables
    istep=0; ntot=1
##   Initialize list of satellites
    istring=len(p.dir_setup)
    if not isinstance(p.filesat, list):
        p.filesat=[p.filesat]
    for filesat in p.filesat:
## Select satellite
        ntmp, nfilesat=os.path.split(filesat[istring:-4])
## Make satellite orbit grid
        if p.makesgrid:
            print('\n Force creation of satellite grid')
            ngrid=build_swath.makeorbit(modelbox, p, orbitfile=filesat)
            ngrid.file=p.outdatadir+os.sep+nfilesat+'_grid.nc'
            ngrid.write_orb()
            ngrid.ipass=nfilesat
            ngrid.gridfile=p.outdatadir+os.sep+nfilesat+'_grid.nc'
        else:
## To be replaced by load_ngrid
            ngrid = rw_data.Sat_nadir(file=p.filesgrid+'.nc')
            ngrid.ipass=nfilesat
            ngrid.gridfile=p.outdatadir+os.sep+nfilesat+'_grid.nc'
            cycle=0 ; x_al=[] ;  al_cycle=0 ; timeshift=0
            ngrid.load_orb(cycle=cycle,x_al=x_al, al_cycle=al_cycle, timeshift=timeshift)
            ngrid.loncirc=numpy.rad2deg(numpy.unwrap(ngrid.lon))
            #ngrid=load_ngrid(sgridfile, p)
##    Select model data around the swath to reduce interpolation cost in griddata
## - Generate SWOT like and nadir-like data:
##   Compute number of cycles needed to cover all nstep model timesteps
        if p.file_input:
            model_index=numpy.where(((numpy.min(ngrid.lon))<=model_data.vlon) & (model_data.vlon<=(numpy.max(ngrid.lon))) & ((numpy.min(ngrid.lat))<=model_data.vlat) & (model_data.vlat<=(numpy.max(ngrid.lat))))
        rcycle=(p.timestep*p.nstep)/float(ngrid.cycle)
        ncycle=int(rcycle)
##   Loop on all cycles
        for cycle in range(0,ncycle+1):
            if ifile>(p.nstep/p.timestep +1): break
##   Create SWOT-like and Nadir-like data
            if not p.file_input : model_data=[]

            SSH_true_nadir, vindice, time, progress=create_Nadirlikedata(cycle, numpy.shape(p.filesat)[0]*rcycle, list_file, modelbox, ngrid, model_data, modeltime, errnad, p, progress_bar=True)
            #SSH_true_nadir, vindice_nadir=create_Nadirlikedata(cycle, sgrid, ngrid, model_data, modeltime, err, errnad, p)
##   Save outputs in a netcdf file
            ngrid.gridfile=filesat
            if (~numpy.isnan(vindice)).any() or not p.file_input:
                err=errnad ; err.wtnadir=numpy.zeros((1)) ; err.wet_tropo2nadir=numpy.zeros((1))
                save_Nadir(cycle, ngrid, errnad, err , p,time=time, vindice_nadir=vindice, SSH_true_nadir=SSH_true_nadir)
            del time
            #if p.file_input: del index
        ngrid.lon=(ngrid.lon+360)%360
        if p.file_input: model_data.vlon=(model_data.vlon+360)%360
        modelbox[0]=(modelbox[0]+360)%360
        modelbox[1]=(modelbox[1]+360)%360
        del ngrid
    if progress!=1: progress=mod_tools.update_progress(1, 'All passes have been processed', '')
## - Write Selected parameters in a txt file
    rw_data.write_params(p,p.outdatadir+os.sep+'nadir_simulator.output')
    print("\n Simulated orbit files have been written in " + p.outdatadir)
    print("----------------------------------------------------------")
コード例 #7
0
def run_simulator(file_param):
    import swotsimulator.build_swath as build_swath
    import swotsimulator.rw_data as rw_data
    import swotsimulator.build_error as build_error
    import swotsimulator.mod_tools as mod_tools
    import swotsimulator.const as const

    ## - Initialize some parameters values
    try:
        p.shift_lon = p.shift_lon
    except:
        p.shift_lon = None
    try:
        p.shift_time = p.shift_time
    except:
        p.shift_time = None
    try:
        model = p.model
    except:
        model = 'NETCDF_MODEL'
        p.model = model
    try:
        p.model_nan = p.model_nan
    except:
        p.model_nan = 0.
    try:
        p.SSH_factor = p.SSH_factor
    except:
        p.SSH_factor = 1.  #; p.SSH_factor=SSH_factor
    try:
        p.nadir = p.nadir
    except:
        p.nadir = True
    try:
        p.grid = p.grid
    except:
        p.grid = 'regular'
    ## - Progress bar variables are global
    global istep
    global ntot
    ## - Read list of user model files """
    if p.file_input:
        list_file = [line.strip() for line in open(p.file_input)]

## - Read model input coordinates '''
# if no modelbox is specified (modelbox=None), the domain of the input data is taken as a modelbox
# coordinates from the region defined by modelbox are selected
    if p.file_input:
        model_data = eval('rw_data.' + model +
                          '(file=p.indatadir+os.sep+list_file[0])')
    if p.modelbox:
        modelbox = numpy.array(p.modelbox, dtype='float')
        ## Use convert to 360 data
        modelbox[0] = (modelbox[0] + 360) % 360
        if modelbox[1] != 360:
            modelbox[1] = (modelbox[1] + 360) % 360
    else:
        if p.file_input:
            modelbox = model_data.calc_box()
        else:
            print('modelbox should be provided if no model file is provided')
            sys.exit()
    if p.file_input:
        model_data.read_coordinates()
        ## Select model data in the region modelbox
        if p.grid == 'regular':
            if modelbox[0] < modelbox[1]:
                model_data.model_index_lon = numpy.where(
                    ((modelbox[0] - 1) <= model_data.vlon)
                    & (model_data.vlon <= (modelbox[1] + 1)))[0]
            else:
                model_data.model_index_lon = numpy.where(
                    ((modelbox[0] - 1) <= model_data.vlon)
                    | (model_data.vlon <= (modelbox[1] + 1)))[0]
            model_data.model_index_lat = numpy.where(
                ((modelbox[2] - 1) <= model_data.vlat)
                & (model_data.vlat <= (modelbox[3] + 1)))[0]
            model_data.vlon = model_data.vlon[model_data.model_index_lon]
            model_data.vlat = model_data.vlat[model_data.model_index_lat]

        else:
            if modelbox[0] < modelbox[1]:
                model_data.model_index = numpy.where(
                    ((modelbox[0] - 1) <= model_data.vlon) &
                    (model_data.vlon <= (modelbox[1] + 1)) &
                    ((modelbox[2] - 1) <= model_data.vlat) &
                    (model_data.vlat <= (modelbox[3] + 1)))  #[0]
            else:
                model_data.model_index = numpy.where(
                    ((modelbox[0] - 1) <= model_data.vlon) |
                    (model_data.vlon <= (modelbox[1] + 1)) &
                    ((modelbox[2] - 1) <= model_data.vlat) &
                    (model_data.vlat <= (modelbox[3] + 1)))  #[0]

        model_data.model = model
        model_data.vloncirc = numpy.rad2deg(numpy.unwrap(model_data.vlon))
    if modelbox[1] == 0: modelbox[1] = 359.99
    ## - Make SWOT grid if necessary """
    if p.makesgrid:
        print('\n Force creation of SWOT grid')
        orb = build_swath.makeorbit(modelbox, p, orbitfile=p.filesat)
        build_swath.orbit2swath(modelbox, p, orb)
        print("\n SWOT Grids and nadir tracks have been written in " +
              p.outdatadir)
        print("-----------------------------------------------")

## - Initialize random coefficients that are used to compute
##   random errors following the specified spectrum
    err, errnad = load_error(p)

    ## - Compute interpolated SSH and errors for each pass, at each
    ##   cycle
    print('Compute interpolated SSH and errors:')
    ##   load all SWOT grid files (one for each pass)
    listsgridfile = sorted(glob.glob(p.filesgrid + '_p*.nc'))
    if not listsgridfile:
        print(
            '\n There is no SWOT grid file in ' + p.outdatadir +
            ', run simulator with option makesgrid set to true in your params file'
        )
        sys.exit()
##   Model time step
    modeltime = numpy.arange(0, p.nstep * p.timestep, p.timestep)
    ##   Remove the grid from the list of model files
    if p.file_input: list_file.remove(list_file[0])
    ##   Initialize progress bar variables
    istep = 0
    ntot = 1
    ## - Loop on SWOT grid files
    for sgridfile in listsgridfile:
        ##   Load SWOT grid files (Swath and nadir)
        sgrid = load_sgrid(sgridfile, p)
        sgrid.gridfile = sgridfile
        if p.nadir:
            ngrid = load_ngrid(sgridfile, p)
            ngrid.gridfile = sgridfile


##    Select model data around the swath to reduce interpolation cost in griddata
## - Generate SWOT like and nadir-like data:
##   Compute number of cycles needed to cover all nstep model timesteps
        rcycle = (p.timestep * p.nstep) / float(sgrid.cycle)
        ncycle = int(rcycle)
        ##   Loop on all cycles
        for cycle in range(0, ncycle + 1):
            if ifile > (p.nstep / p.timestep + 1): break
            ##   Create SWOT-like and Nadir-like data
            if not p.file_input: model_data = []

            SSH_true, SSH_true_nadir, vindice, vindice_nadir, time, progress = create_SWOTlikedata(
                cycle,
                numpy.shape(listsgridfile)[0] * rcycle,
                list_file,
                modelbox,
                sgrid,
                ngrid,
                model_data,
                modeltime,
                err,
                errnad,
                p,
                progress_bar=True)
            #SSH_true_nadir, vindice_nadir=create_Nadirlikedata(cycle, sgrid, ngrid, model_data, modeltime, err, errnad, p)
            ##   Save outputs in a netcdf file
            if (~numpy.isnan(vindice)).any() or not p.file_input:
                save_SWOT(cycle,
                          sgrid,
                          err,
                          p,
                          time=time,
                          vindice=vindice,
                          SSH_true=SSH_true)
                if p.nadir:
                    save_Nadir(cycle,
                               ngrid,
                               errnad,
                               err,
                               p,
                               time=time,
                               vindice_nadir=vindice_nadir,
                               SSH_true_nadir=SSH_true_nadir)
            del time
            #if p.file_input: del index
        sgrid.lon = (sgrid.lon + 360) % 360
        ngrid.lon = (ngrid.lon + 360) % 360
        if p.file_input: model_data.vlon = (model_data.vlon + 360) % 360
        modelbox[0] = (modelbox[0] + 360) % 360
        modelbox[1] = (modelbox[1] + 360) % 360
        del sgrid, ngrid
    if progress != 1:
        progress = mod_tools.update_progress(1,
                                             'All passes have been processed',
                                             '')
    ## - Write Selected parameters in a txt file
    rw_data.write_params(p, p.outdatadir + os.sep + 'swot_simulator.output')
    print("\n Simulated swot files have been written in " + p.outdatadir)
    print("----------------------------------------------------------")
コード例 #8
0
def run_nadir(file_param):
    if os.path.isfile(file_param):
        #    basedir=os.path.dirname(swotsimulator.__file__)
        shutil.copyfile(file_param,
                        'params.py')  #os.path.join(basedir,'params.py'))
    else:
        print("Error: No such file: '%s'" % file_param)
        sys.exit()
    try:
        import params as p
    except:
        if os.path.isfile('params.py'):
            print("There is a wrong entry in your params file")
            import params
        else:
            print("Error: No params.py module found")
            sys.exit()
    import swotsimulator.build_swath as build_swath
    import swotsimulator.rw_data as rw_data
    import swotsimulator.build_error as build_error
    import swotsimulator.mod_tools as mod_tools
    import swotsimulator.const as const

    ## - Initialize some parameters values
    try:
        p.shift_lon = p.shift_lon
    except:
        p.shift_lon = None
    try:
        p.shift_time = p.shift_time
    except:
        p.shift_time = None
    try:
        model = p.model
    except:
        model = 'NETCDF_MODEL'
        p.model = model
    try:
        p.model_nan = p.model_nan
    except:
        p.model_nan = 0.
    try:
        p.SSH_factor = p.SSH_factor
    except:
        p.SSH_factor = 1.  #; p.SSH_factor=SSH_factor
    try:
        p.nadir = p.nadir
    except:
        p.nadir = True
    try:
        p.grid = p.grid
    except:
        p.grid = 'regular'
    p.karin = False
    p.phase = False
    p.roll = False
    p.baseline_dilation = False
    p.timing = False
    p.halfswath = 60.
    ## - Progress bar variables are global
    global istep
    global ntot
    ## - Read list of user model files """
    if p.file_input:
        list_file = [line.strip() for line in open(p.file_input)]

## - Read model input coordinates '''
# if no modelbox is specified (modelbox=None), the domain of the input data is taken as a modelbox
# coordinates from the region defined by modelbox are selected
    if p.file_input:
        model_data = eval('rw_data.' + model +
                          '(file=p.indatadir+os.sep+list_file[0])')
    if p.modelbox:
        modelbox = numpy.array(p.modelbox, dtype='float')
        ## Use convert to 360 data
        modelbox[0] = (modelbox[0] + 360) % 360
        if modelbox[1] != 360:
            modelbox[1] = (modelbox[1] + 360) % 360
    else:
        if p.file_input:
            modelbox = model_data.calc_box()
        else:
            print('modelbox should be provided if no model file is provided')
            sys.exit()
    if p.file_input:
        model_data.read_coordinates()
        ## Select model data in the region modelbox
        if p.grid == 'regular':
            model_data.model_index_lon = numpy.where(
                ((modelbox[0] - 1) <= model_data.vlon)
                & (model_data.vlon <= (modelbox[1] + 1)))[0]
            model_data.model_index_lat = numpy.where(
                ((modelbox[2] - 1) <= model_data.vlat)
                & (model_data.vlat <= (modelbox[3] + 1)))[0]
            model_data.vlon = model_data.vlon[model_data.model_index_lon]
            model_data.vlat = model_data.vlat[model_data.model_index_lat]

        else:
            model_data.model_index = numpy.where(
                ((modelbox[0] - 1) <= model_data.vlon) & (model_data.vlon <=
                                                          (modelbox[1] + 1)) &
                ((modelbox[2] - 1) <= model_data.vlat) &
                (model_data.vlat <= (modelbox[3] + 1)))  #[0]
        model_data.model = model
        model_data.vloncirc = numpy.rad2deg(numpy.unwrap(model_data.vlon))
## Ugly trick when model box is [0 360] to avoid box being empty (360=0%360)
    if modelbox[1] == 0: modelbox[1] = 359.99

    ## - Initialize random coefficients that are used to compute
    ##   random errors following the specified spectrum
    err, errnad = load_error(p)

    ## - Compute interpolated SSH and errors for each pass, at each
    ##   cycle
    print('Compute interpolated SSH and errors:')
    ##   Model time step
    modeltime = numpy.arange(0, p.nstep * p.timestep, p.timestep)
    ##   Remove the grid from the list of model files
    if p.file_input: list_file.remove(list_file[0])
    ##   Initialize progress bar variables
    istep = 0
    ntot = 1
    ##   Initialize list of satellites
    istring = len(p.dir_setup)
    if not isinstance(p.filesat, list):
        p.filesat = [p.filesat]
    for filesat in p.filesat:
        ## Select satellite
        ntmp, nfilesat = os.path.split(filesat[istring:-4])
        ## Make satellite orbit grid
        if p.makesgrid:
            print('\n Force creation of satellite grid')
            ngrid = build_swath.makeorbit(modelbox, p, orbitfile=filesat)
            ngrid.file = p.outdatadir + os.sep + nfilesat + '_grid.nc'
            ngrid.write_orb()
            ngrid.ipass = nfilesat
            ngrid.gridfile = p.outdatadir + os.sep + nfilesat + '_grid.nc'
        else:
            ## To be replaced by load_ngrid
            ngrid = rw_data.Sat_nadir(file=p.filesgrid + '.nc')
            ngrid.ipass = nfilesat
            ngrid.gridfile = p.outdatadir + os.sep + nfilesat + '_grid.nc'
            cycle = 0
            x_al = []
            al_cycle = 0
            timeshift = 0
            ngrid.load_orb(cycle=cycle,
                           x_al=x_al,
                           al_cycle=al_cycle,
                           timeshift=timeshift)
            ngrid.loncirc = numpy.rad2deg(numpy.unwrap(ngrid.lon))
            #ngrid=load_ngrid(sgridfile, p)


##    Select model data around the swath to reduce interpolation cost in griddata
## - Generate SWOT like and nadir-like data:
##   Compute number of cycles needed to cover all nstep model timesteps
        if p.file_input:
            model_index = numpy.where(
                ((numpy.min(ngrid.lon)) <= model_data.vlon)
                & (model_data.vlon <= (numpy.max(ngrid.lon)))
                & ((numpy.min(ngrid.lat)) <= model_data.vlat)
                & (model_data.vlat <= (numpy.max(ngrid.lat))))
        rcycle = (p.timestep * p.nstep) / float(ngrid.cycle)
        ncycle = int(rcycle)
        ##   Loop on all cycles
        for cycle in range(0, ncycle + 1):
            if ifile > (p.nstep / p.timestep + 1): break
            ##   Create SWOT-like and Nadir-like data
            if not p.file_input: model_data = []

            SSH_true_nadir, vindice, time, progress = create_Nadirlikedata(
                cycle,
                numpy.shape(p.filesat)[0] * rcycle,
                list_file,
                modelbox,
                ngrid,
                model_data,
                modeltime,
                errnad,
                p,
                progress_bar=True)
            #SSH_true_nadir, vindice_nadir=create_Nadirlikedata(cycle, sgrid, ngrid, model_data, modeltime, err, errnad, p)
            ##   Save outputs in a netcdf file
            ngrid.gridfile = filesat
            if (~numpy.isnan(vindice)).any() or not p.file_input:
                err = errnad
                err.wtnadir = numpy.zeros((1))
                err.wet_tropo2nadir = numpy.zeros((1))
                save_Nadir(cycle,
                           ngrid,
                           errnad,
                           err,
                           p,
                           time=time,
                           vindice_nadir=vindice,
                           SSH_true_nadir=SSH_true_nadir)
            del time
            #if p.file_input: del index
        ngrid.lon = (ngrid.lon + 360) % 360
        if p.file_input: model_data.vlon = (model_data.vlon + 360) % 360
        modelbox[0] = (modelbox[0] + 360) % 360
        modelbox[1] = (modelbox[1] + 360) % 360
        del ngrid
    if progress != 1:
        progress = mod_tools.update_progress(1,
                                             'All passes have been processed',
                                             '')
    ## - Write Selected parameters in a txt file
    rw_data.write_params(p, p.outdatadir + os.sep + 'nadir_simulator.output')
    print("\n Simulated orbit files have been written in " + p.outdatadir)
    print("----------------------------------------------------------")