Commit d5d012a3 authored by Florian de Boissieu's avatar Florian de Boissieu
Browse files

add tempfile at S1CalibOrthoWithROI to avoid partially written file in case of interruption

parent aa84b02f
"""
TODO:
- There is a border effect in function of ROI size due to speckle filtering, either:
- "bufferize" ROI raster and crop to non-bufferized ROI after filtering
- include filtering in pipe before mosaic: how to deal with superimpose? Won't it have a border effect anyway if ROI is at the border of transect?
"""
#import argparse
import glob, os, sys, warnings
import subprocess
......@@ -147,34 +153,76 @@ def S1CalibOrtho(fld, epsg=None, demfld=None, geoid=None, overwrite=False):
return out
def S1CalibOrthoWithROI(infile, outfile, roi, demfld, geoid, lut='sigma', overwrite=False, ram=1024):
if (os.path.exists(outfile) and not overwrite):
def S1CalibOrthoWithROI(infile, outfile, roi, demfld, geoid, lut='sigma', orthofit=False, overwrite=False, ram=1024):
"""
Piped calibration > ortho > superimpose ROI
Parameters
----------
infile: str
Input S1 tiff file path
outfile: str
Output file path
roi: str
Raster
demfld: str
Directory containing DEM rasters
geoid: str
File containing geoid
lut: str
Type of calibration: sigma or gamma
orthofit: bool
Use orthofit within orthorectification instead of superimpose post-processing
overwrite: bool
If True and file exists, it is overwritten.
ram: int
Memory chunk used by OTB (in MB)
Returns
-------
srt
outfile path
"""
infile = Path(infile)
outfile = Path(outfile)
if (outfile.exists() and not overwrite):
print('Calibration and Orthorectification process skipped, file already exists:\n\t{}'.format(outfile))
return outfile
else:
if checkRoiInS1Acquisition(infile, roi):
pipe = []
pipe.append(otb.Registry.CreateApplication('SARCalibration'))
pipe[-1].SetParameterString('in', infile)
pipe[-1].SetParameterString('lut', lut)
pipe[-1].SetParameterString('ram', str(ram))
pipe[-1].Execute()
pipe.append(otb.Registry.CreateApplication('OrthoRectification'))
pipe[-1].SetParameterInputImage('io.in', pipe[-2].GetParameterOutputImage('out'))
pipe[-1].SetParameterString('elev.dem', demfld)
pipe[-1].SetParameterString('elev.geoid', geoid)
pipe[-1].SetParameterString('map', 'epsg')
pipe[-1].SetParameterString('map.epsg.code', getRasterEPSG(roi))
pipe[-1].SetParameterInt('opt.gridspacing', 40)
pipe[-1].SetParameterString('opt.ram', str(ram))
pipe[-1].Execute()
pipe.append(otb.Registry.CreateApplication('Superimpose'))
pipe[-1].SetParameterInputImage('inm', pipe[-2].GetParameterOutputImage('io.out'))
pipe[-1].SetParameterString('inr', roi)
pipe[-1].SetParameterString('ram', str(ram))
pipe[-1].SetParameterString('out', outfile)
pipe[-1].ExecuteAndWriteOutput()
with tempfile.TemporaryDirectory(dir=Path(outfile).parent) as tmpdir:
tmpfile = Path(tmpdir) / outfile.name
pipe = []
pipe.append(otb.Registry.CreateApplication('SARCalibration'))
pipe[-1].SetParameterString('in', infile)
pipe[-1].SetParameterString('lut', lut)
pipe[-1].SetParameterString('ram', str(ram))
pipe[-1].Execute()
pipe.append(otb.Registry.CreateApplication('OrthoRectification'))
pipe[-1].SetParameterInputImage('io.in', pipe[-2].GetParameterOutputImage('out'))
pipe[-1].SetParameterString('elev.dem', demfld)
pipe[-1].SetParameterString('elev.geoid', geoid)
pipe[-1].SetParameterString('opt.ram', str(ram))
pipe[-1].SetParameterInt('opt.gridspacing', 40)
pipe[-1].SetParameterString('map', 'epsg')
pipe[-1].SetParameterString('map.epsg.code', getRasterEPSG(roi))
if orthofit:
print('Using mode "orthofit" for ortho-rectification')
pipe[-1].SetParameterString('outputs.mode', 'orthofit')
pipe[-1].SetParameterString('outputs.ortho', roi)
pipe[-1].SetParameterString('io.out', tmpfile)
else:
pipe[-1].Execute()
pipe.append(otb.Registry.CreateApplication('Superimpose'))
pipe[-1].SetParameterInputImage('inm', pipe[-2].GetParameterOutputImage('io.out'))
pipe[-1].SetParameterString('inr', roi)
pipe[-1].SetParameterString('ram', str(ram))
pipe[-1].SetParameterString('out', tmpfile)
pipe[-1].ExecuteAndWriteOutput()
tmpfile.move(outfile)
return outfile
else:
......@@ -855,7 +903,7 @@ def s1process(indir, outdir, epsg=None,
geoid = download_geoid(cache_dir, verbose)
outfiles = [ortho_dir / infile.name.replace('.tiff', '_clipped_cal' + lut + '_ortho.tiff') for infile in proc_files]
mp_args = [(infile, outfile, roi, dem_dir, geoid, lut, overwrite, ram) for infile, outfile in
mp_args = [(infile, outfile, roi, dem_dir, geoid, lut, orthofit, overwrite, ram) for infile, outfile in
zip(proc_files, outfiles)]
"""
files = []
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment