SAR-ARD / ERS_NRB

A prototype processor for the ASAR and ERS Normalised Radar Backscatter product
https://ers-nrb.readthedocs.io/en/latest/
MIT License
5 stars 0 forks source link

CARD4L bug and development issues #1

Closed MWilliamstpzv closed 1 year ago

MWilliamstpzv commented 1 year ago

1) The Data mask is not aligned with the other layers We need to clip the data mask to the gamm0 layer so that is has the same pixel size.

2) Generation of Gamma Log VRT is incorrect. There is an issue with the no data values. The other VRT's the Sigma0 Log and Sigma0 Lin are correct. can use this part of the code as a reference.

3) Multiple SLC generation. We currently only process one SLC image. Sentinel-1 has the code to process consecutive SLC images. We need to implement the same in this code.

4) Re-Processing of the tests datasets

gr4n0t4 commented 1 year ago

Issue 1:

    # Data mask
    print("Data mask")
    if not config['dem_type'] == 'GETASSE30' and not os.path.isfile(wbm):
        raise FileNotFoundError('External water body mask could not be found: {}'.format(wbm))

    dm_path = gs_path.replace('-gs.tif', '-dm.tif')
    with Raster(wbm) as ras_wbm:
        extent = ras_wbm.extent

    ancil.create_data_mask(outname=dm_path, valid_mask_list=snap_dm_tile_overlap, src_files=files,
                           extent=extent, epsg=epsg, driver=driver, creation_opt=write_options['layoverShadowMask'],
                           overviews=overviews, overview_resampling=ovr_resampling, wbm=wbm)

The extent of the datamask is defined by the WBM at the moment (not sure why), we can try to change it to any other. Just need the exact file name Issue 2:

    print("sigma nought RTC")
    for item in measure_paths:
        sigma0_rtc_lin = item.replace('g-lin.tif', 's-lin.vrt')
        sigma0_rtc_log = item.replace('g-lin.tif', 's-log.vrt')

        if not os.path.isfile(sigma0_rtc_lin):
            ancil.vrt_pixfun(src=[item, gs_path], dst=sigma0_rtc_lin, fun='mul',
                             options={'VRTNodata': 'NaN'}, overviews=overviews, overview_resampling=ovr_resampling)
            ancil.vrt_relpath(sigma0_rtc_lin)

        if not os.path.isfile(sigma0_rtc_log):
            ancil.vrt_pixfun(src=sigma0_rtc_lin, dst=sigma0_rtc_log, fun='log10', scale=10,
                             options={'VRTNodata': 'NaN'}, overviews=overviews, overview_resampling=ovr_resampling)

        gamma0_rtc_log = item.replace('lin.tif', 'log.vrt')
        if not os.path.isfile(gamma0_rtc_log):
            ancil.vrt_pixfun(src=item, dst=gamma0_rtc_log, fun='log10', scale=10,
                       options={'VRTNodata': 'NaN'}, overviews=overviews, overview_resampling=ovr_resampling)  

The code looks identical for gamma0_rtc_log and sigma0_rtc_log Maybe there is an error on the *.tif input images?

Issue 3: In s-1 they do

def group_by_time(scenes, time=3):
    """
    Group scenes by their acquisition time difference.
    Parameters
    ----------
    scenes:list[pyroSAR.drivers.ID or str]
        a list of image names
    time: int or float
        a time difference in seconds by which to group the scenes.
        The default of 3 seconds incorporates the overlap between SLCs.
    Returns
    -------
    list[list[pyroSAR.drivers.ID]]
        a list of sub-lists containing the file names of the grouped scenes
    """
    # sort images by time stamp
    scenes = identify_many(scenes, sortkey='start')

    if len(scenes) < 2:
        return [scenes]

    groups = [[scenes[0]]]
    group = groups[0]

    for i in range(1, len(scenes)):
        start = datetime.strptime(scenes[i].start, '%Y%m%dT%H%M%S')
        stop_pred = datetime.strptime(scenes[i - 1].stop, '%Y%m%dT%H%M%S')
        diff = (stop_pred - start).total_seconds()
        if diff <= time:
            group.append(scenes[i])
        else:
            groups.append([scenes[i]])
            group = groups[-1]
    return groups

Switching the selection_grouped in our code should work

    if nrb_flag:
        # selection_grouped = groupbyTime(images=selection, function=seconds, time=60)
        selection_grouped = selection
        for t, tile in enumerate(aoi_tiles):
            outdir = os.path.join(config['out_dir'], tile)
            os.makedirs(outdir, exist_ok=True)
            wbm = os.path.join(config['wbm_dir'], config['dem_type'], '{}_WBM.tif'.format(tile))
            # wbm = fname_wbm
            if not os.path.isfile(wbm):
                wbm = None
            for s, scenes in enumerate(selection_grouped):
                if isinstance(scenes, str):                    
                    scenes = [scenes]
                print('###### [NRB] Tile {t}/{t_total}: {tile} | '
                      'Scenes {s}/{s_total}: {scenes} '.format(tile=tile, t=t+1, t_total=len(aoi_tiles),
                                                               scenes=[os.path.basename(s) for s in scenes],
                                                               s=s+1, s_total=len(selection_grouped)))
                start_time = time.time()
                try:
                    nrb_processing(config=config, scenes=scenes, datadir=os.path.dirname(outdir), outdir=outdir,
                                    tile=tile, extent=geo_dict[tile]['ext'], epsg=epsg, wbm=wbm,
                                    multithread=gdal_prms['multithread'])
                    log.info('[    NRB] -- {scenes} -- {time}'.format(scenes=scenes,
                                                                time=round((time.time() - start_time), 2)))
                except Exception as e:
                    log.exception('[    NRB] -- {scenes} -- {error}'.format(scenes=scenes, error=e))
                    continue

        gdal.SetConfigOption('GDAL_NUM_THREADS', gdal_prms['threads_before'])
gr4n0t4 commented 1 year ago

Issue 1 Gdal sometimes creates an extra pixel, added a check to delete this extra pixel in the WBM https://github.com/SAR-ARD/ERS_NRB/commit/8fa2a5ca29bf47e6c8e7802c9fc824503f4726d7

gr4n0t4 commented 1 year ago

Issue 2 Gamma log vrt was refencing to the tif instead of the lin vrt, this was generating mix results 23824ed8aa373301c450363a5eb9a73dd777426a Open a new ticket for the Change requested (Issue 3)