Beispiel #1
0
def worker_method_swot(*args, **kwargs):
    msg_queue, sgridfile, p2, listsgridfile = args[:4]

    list_file, modelbox, model_data, modeltime, err, errnad = args[4:]
    p = mod_tools.fromdict(p2)
    if err is None:
        nadir_alone = True
    else:
        nadir_alone = False
    compute_nadir = ((p.nadir is True) or (nadir_alone is True))
    #   Load SWOT grid files (Swath and nadir)
    if compute_nadir is True:
        ngrid = mod.load_ngrid(sgridfile, p, nadir_alone=nadir_alone)
        ngrid.gridfile = sgridfile
    else:
        ngrid = None
    if nadir_alone is False:
        sgrid = mod.load_sgrid(sgridfile, p)
        sgrid.gridfile = sgridfile
    else:
        sgrid = ngrid
    # 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):
        # #TODO move this somwhere where we have ifile information
        #if ifile > (p.nstep/p.timestep + 1):
        #    break
        # Add a message to tell the main program that the cycle is being
        # processed
        msg_queue.put((os.getpid(), sgridfile, cycle + 1, None))

        #   Process_cycle: create SWOT-like and Nadir-like data
        if not p.file_input:
            model_data = []
        create = mod.create_SWOTlikedata(cycle,
                                         list_file,
                                         modelbox,
                                         sgrid,
                                         ngrid,
                                         model_data,
                                         modeltime,
                                         err,
                                         errnad,
                                         p,
                                         Teval=Teval,
                                         nTeval=nTeval)
        out_var, time, Teval, nTeval = create
        #   Save outputs in a netcdf file
        if nadir_alone is True:
            out_var['vindice'] = +out_var['vindice_nadir']
        if (~numpy.isnan(out_var['vindice'])).any() or not p.file_input:
            if nadir_alone is False:
                mod.save_SWOT(cycle,
                              sgrid,
                              err,
                              p,
                              out_var,
                              time=time,
                              save_var=p.save_variables)
            if compute_nadir is True:
                mod.save_Nadir(cycle,
                               ngrid,
                               errnad,
                               err,
                               p,
                               out_var,
                               time=time)
    # Add a special message once a grid has been completely processed
    msg_queue.put((os.getpid(), sgridfile, None, None))
Beispiel #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("----------------------------------------------------------")