johntruckenbrodt / pyroSAR

framework for large-scale SAR satellite data processing
MIT License
512 stars 112 forks source link

SNAP workflow: node navigation by object instead of ID #150

Closed florianbeyer closed 2 years ago

florianbeyer commented 3 years ago

Hello,

I tried a lot up to now, but I do not find my mistake. Could anyone help me? Unfortunately the error message doesn't help me...

It works when I comment Terrain-Correction and LinearToFromdB...

from pyroSAR.snap.auxil import parse_recipe, parse_node

workflow = parse_recipe('blank')

read = parse_node('Read')
read.parameters['file'] = image
read.parameters['formatName'] = 'SENTINEL-1'
workflow.insert_node(read)

orb = parse_node('Apply-Orbit-File')
orb.parameters['orbitType'] = 'Sentinel Precise (Auto Download)'
workflow.insert_node(orb, before=read.id)

tnr = parse_node('ThermalNoiseRemoval')
workflow.insert_node(tnr, before=orb.id)

bnr = parse_node('Remove-GRD-Border-Noise')
bnr.parameters['selectedPolarisations'] = ['Intensity_VH','Intensity_VV']
workflow.insert_node(bnr, before=tnr.id)

cal = parse_node('Calibration')
cal.parameters['outputSigmaBand'] = False
cal.parameters['outputGammaBand'] = False
cal.parameters['outputBetaBand'] = True
cal.parameters['sourceBands'] = ['Intensity_VH','Intensity_VV']
cal.parameters['selectedPolarisations'] = ['VH','VV']
cal.parameters['outputImageScaleInDb'] = False
workflow.insert_node(cal, before=bnr.id)

tf = parse_node('Terrain-Flattening')
tf.parameters['demName'] = 'SRTM 1Sec HGT'
tf.parameters['additionalOverlap'] = 0.1
tf.parameters['oversamplingMultiple'] = 1.0
tf.parameters['demResamplingMethod'] = 'BILINEAR_INTERPOLATION'
workflow.insert_node(tf, before=cal.id)

tc = parse_node('Terrain-Correction')
tc.parameters['demName'] = 'SRTM 1Sec HGT'
tc.parameters['imgResamplingMethod'] = 'BILINEAR_INTERPOLATION'
tc.parameters['pixelSpacingInMeter'] = 10
tc.parameters['nodataValueAtSea'] = True
tc.parameters['saveSelectedSourceBand'] = True
tc.parameters['applyRadiometricNormalization'] = True
workflow.insert_node(tc, before=tf)

db = parse_node('LinearToFromdB')
db.parameters['sourceBands'] = ['Gamma0_VH','Gamma0_VV']
workflow.insert_node(db, before=tc)

write = parse_node('Write')
write.parameters['file'] = '/home/jupyter-florian/data_storage/florian/S1/backscatter_pyroSAR-tests/_Flo_'+id_.outname_base()
write.parameters['formatName'] = 'BEAM-DIMAP'
workflow.insert_node(write, before=tc.id)

workflow.write('/home/jupyter-florian/data_storage/florian/S1_backscatter/pyroSAR-tests/pyro_S1_SRTM')

The error message I get is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_19674/949773897.py in <module>
     42 # tc.parameters['saveSelectedSourceBand'] = True
     43 # tc.parameters['applyRadiometricNormalization'] = True
---> 44 workflow.insert_node(tc, before=tf)
     45 
     46 

/opt/tljh/user/envs/SAR/lib/python3.9/site-packages/pyroSAR/snap/auxil.py in insert_node(self, node, before, after, resetSuccessorSource, void)
    842                 predecessor = self[before[indices.index(max(indices))]]
    843             else:
--> 844                 predecessor = self[before]
    845             log.debug('inserting node {} after {}'.format(node.id, predecessor.id))
    846             position = self.index(predecessor) + 1

/opt/tljh/user/envs/SAR/lib/python3.9/site-packages/pyroSAR/snap/auxil.py in __getitem__(self, item)
    648                     raise KeyError('unknown key: {}'.format(item))
    649         else:
--> 650             raise TypeError('item must be of type int or str')
    651 
    652     def __len__(self):

TypeError: item must be of type int or str
​
florianbeyer commented 3 years ago

I've found my mistake:

before=variable.id

The .id was missing...

johntruckenbrodt commented 3 years ago

Hi @florianbeyer. Glad you could figure it out before I was able to answer. I agree, it would be more intuitive if one could also just pass the node object instead of its ID.

johntruckenbrodt commented 3 years ago

This should be quite straightforward to realize for anyone who likes to contribute..

pbrotoisworo commented 2 years ago

Hi @johntruckenbrodt ,

Indeed, it is straightforward.

In my code, I have added these lines:

if isinstance(before, Node):
    before = before.id

To Workflow.insert_node() in this line: https://github.com/johntruckenbrodt/pyroSAR/blob/0ea043ed55157fe81c833ca02a1fcda04e58c94d/pyroSAR/snap/auxil.py#L913

And it works. This fix would still allow existing users to still use the Node.id property and not break existing their workflows. I can do a PR if you are okay with this suggestion.

johntruckenbrodt commented 2 years ago

@pbrotoisworo thanks a lot for looking into this too! Yes, your suggestion works like a charm, a PR would be most welcome. Could you repeat the same two lines for the argument after as well?