Skip to content

Commit

Permalink
Merge pull request #112 from SAR-ARD/bugfix/slice_id
Browse files Browse the repository at this point in the history
support for scenes acquired in NRT Slicing mode
  • Loading branch information
johntruckenbrodt authored Jul 4, 2023
2 parents 4cb37a0 + dacd284 commit ac7b8c5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
50 changes: 34 additions & 16 deletions S1_NRB/ancillary.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,37 @@ def check_acquisition_completeness(scenes, archive):
groupsize = 3
has_successor = True
has_predecessor = True
if slice == 0:
raise RuntimeError(f'invalid value for sliceNumber: 0')
if n_slices == 0:
raise RuntimeError(f'invalid value for totalSlices: 0')
if slice == 1: # first slice in the data take
groupsize -= 1
has_predecessor = False
if slice == n_slices: # last slice in the data take
groupsize -= 1
has_successor = False

f = '%Y%m%dT%H%M%S'
td = timedelta(seconds=2)
start = datetime.strptime(scene.start, f) - td
start = datetime.strftime(start, f)
stop = datetime.strptime(scene.stop, f) + td
stop = datetime.strftime(stop, f)
ref = None
if slice == 0 or n_slices == 0:
# NRT slicing mode
ref = asf_select(sensor=scene.sensor,
product=scene.product,
acquisition_mode=scene.acquisition_mode,
mindate=start,
maxdate=stop)
match = [re.search(scene.pattern, x + '.SAFE').groupdict() for x in ref]
ref_start_min = min([x['start'] for x in match])
ref_stop_max = max([x['stop'] for x in match])
if ref_start_min == scene.start:
groupsize -= 1
has_predecessor = False
if ref_stop_max == scene.stop:
groupsize -= 1
has_successor = False
else:
if slice == 1: # first slice in the data take
groupsize -= 1
has_predecessor = False
if slice == n_slices: # last slice in the data take
groupsize -= 1
has_successor = False
# Do another database selection to get the scene in question as well as its potential
# predecessor and successor by adding an acquisition time buffer of two seconds.
group = archive.select(sensor=scene.sensor,
Expand All @@ -75,13 +90,15 @@ def check_acquisition_completeness(scenes, archive):
date_strict=False)
group = identify_many(group)
# if the number of selected scenes is lower than the expected group size,
# check whether the predecessor, the successor or both are missing.
# check whether the predecessor, the successor or both are missing by
# cross-checking with the ASF database.
if len(group) < groupsize:
ref = asf_select(sensor=scene.sensor,
product=scene.product,
acquisition_mode=scene.acquisition_mode,
mindate=start,
maxdate=stop)
if ref is None:
ref = asf_select(sensor=scene.sensor,
product=scene.product,
acquisition_mode=scene.acquisition_mode,
mindate=start,
maxdate=stop)
match = [re.search(scene.pattern, x + '.SAFE').groupdict() for x in ref]
ref_start_min = min([x['start'] for x in match])
ref_stop_max = max([x['stop'] for x in match])
Expand Down Expand Up @@ -321,6 +338,7 @@ def _log_process_config(logger, config):
aoi_geometry {config['aoi_geometry']}
mindate {config['mindate'].isoformat()}
maxdate {config['maxdate'].isoformat()}
date_strict {config['date_strict']}
sensor {config['sensor']}
acq_mode {config['acq_mode']}
product {config['product']}
Expand Down
48 changes: 47 additions & 1 deletion S1_NRB/snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import itertools
import shutil
from math import ceil
from lxml import etree
from dateutil.parser import parse as dateparse
from spatialist import bbox, Raster
from spatialist.envi import HDRobject
from spatialist.ancillary import finder
Expand Down Expand Up @@ -161,7 +163,7 @@ def grd_buffer(src, dst, workflow, neighbors, buffer=100, gpt_args=None):
Parameters
----------
src: str
the file name of the source scene
the file name of the source scene in BEAM-DIMAP format.
dst: str
the file name of the target scene. Format is BEAM-DIMAP.
workflow: str
Expand All @@ -182,6 +184,10 @@ def grd_buffer(src, dst, workflow, neighbors, buffer=100, gpt_args=None):
scenes = identify_many([src] + neighbors, sortkey='start')
wf = parse_recipe('blank')
############################################
# modify the slice number if it is 0
for scene in scenes:
nrt_slice_num(dim=scene.scene)
############################################
read_ids = []
for scene in scenes:
read = parse_node('Read')
Expand Down Expand Up @@ -925,3 +931,43 @@ def get_metadata(scene, outdir):
raise RuntimeError(msg.format('\n'.join(wf_mli)))
return {'azlks': azlks,
'rlks': rlks}


def nrt_slice_num(dim):
"""
Compute a slice number for a scene acquired NRT Slicing mode.
In this mode both `sliceNumber` and `totalSlices` are 0 in the manifest.safe file.
`sliceNumber` is however needed in function :func:`~S1_NRB.snap.grd_buffer` for
the SNAP operator `SliceAssembly`.
The time from `segmentStartTime` to `last_line_time` is divided by
the acquisition duration (`last_line_time` - `first_line_time`).
`totalSlices` is set to 100, which is expected to exceed the maximum possible value.
Parameters
----------
dim: str
the scene in BEAM-DIMAP format
Returns
-------
"""
with open(dim, 'rb') as f:
root = etree.fromstring(f.read())
abstract = root.xpath("//MDElem[@name='Abstracted_Metadata']")[0]
slice_num = abstract.xpath("./MDATTR[@name='slice_num']")[0]
if slice_num.text == '0':
flt = dateparse(abstract.xpath("./MDATTR[@name='first_line_time']")[0].text)
llt = dateparse(abstract.xpath("./MDATTR[@name='last_line_time']")[0].text)
sst = dateparse(root.xpath("//MDATTR[@name='segmentStartTime']")[0].text)
aqd = llt - flt
slice_num_new = str(int(round((llt - sst) / aqd)))
slice_num.text = slice_num_new
for item in root.xpath("//MDATTR[@name='sliceNumber']"):
item.text = slice_num_new
for item in root.xpath("//MDATTR[@name='totalSlices']"):
item.text = '100'
etree.indent(root, space=' ')
tree = etree.ElementTree(root)
tree.write(dim, pretty_print=True, xml_declaration=True,
encoding='utf-8')
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ SNAP
mli
pre
rtc
sgr

.. rubric:: ancillary functions

Expand All @@ -58,6 +59,7 @@ SNAP
find_datasets
get_metadata
postprocess
nrt_slice_num

NRB
^^^
Expand Down

0 comments on commit ac7b8c5

Please sign in to comment.