Skip to content

Commit

Permalink
Merge pull request #101 from nansencenter/apg_based
Browse files Browse the repository at this point in the history
Apg based
  • Loading branch information
akorosov authored Dec 12, 2023
2 parents 8e3a5e9 + 34ce587 commit 526c81b
Show file tree
Hide file tree
Showing 12 changed files with 1,461 additions and 665 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ target/
.DS_Store

fit_on_one
*png
39 changes: 22 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,13 @@ is to use [Anaconda](https://docs.conda.io/en/latest/miniconda.html).

```shell
# create conda environment with key requirements
conda create -y -n s1denoise gdal cartopy pip
conda env create -f environment.yml

# activate environment
conda activate s1denoise

# install other reqs using pip
pip install pythesint netcdf4 nansat

# update metadata vocabularies
python -c 'import pythesint as pti; pti.update_all_vocabularies()'

# install s1denoise
pip install https://github.com/nansencenter/sentinel1denoised/archive/v1.3.3.tar.gz
pip install https://github.com/nansencenter/sentinel1denoised/archive/v1.4.0.tar.gz

```

Expand All @@ -55,30 +49,42 @@ Do processing inside Python environment:
```python
from s1denoise import Sentinel1Image
# open access to file with S1 data
s1 = Sentinel1Image('/path/to/data/S1B_EW_GRDM_1SDH_INPUTFILE.zip')
input_file = '/path/to/data/S1B_EW_GRDM_1SDH_INPUTFILE.zip'
s1 = Sentinel1Image(input_file)

# run thermal noise correction in HV polarisation with the default ESA algorithm
s0hve = s1.remove_thermal_noise('HV', algorithm='ESA')

# run thermal noise correction in HV polarisation with the NEW algorithm
# run thermal noise correction in HV polarisation with the default NERSC algorithm
s0_hv = s1.remove_thermal_noise('HV')

# run thermal noise correction in HV polarisation with the NERSC_TG algorithm applicable for old Sentinel-1 data
s0_hv = s1.remove_thermal_noise('HV', algorithm='NERSC_TG')

# run thermal and texture noise correction in HV polarisation
s0_hv = s1.remove_texture_noise('HV')

# High level function for processing both polarisations
from s1denoise.tools import run_correction
d = run_correction(input_file)

```

Process a single file with thermal, textural and angular correction and export in dB:
Process a single file with thermal, textural and angular correction and export in [dB] in a numpy file:

`s1_correction.py INPUTFILE.zip OUTPUTFILE.tif`
`s1_correction.py INPUTFILE.zip OUTPUTFILE.npz`

Process a single file using Docker (replace `input_dir` and `output_dir` with actual directories):
Process a single file and export as GeoTIFF (requires Nansat):

`docker run --rm -v /input_dir:/input_dir -v /output_dir:/output_dir s1denoise s1_correction.py /input_dir/INPUTFILE.zip /output_dir/OUPUTFILE.tif`
`s1_correction.py INPUTFILE.zip OUTPUTFILE.npz -g`

With option `-m` the script will also save landmask in Numpy format in a separate file with name `OUTPUTFILE.tif_mask.npz`:

`s1_correction.py -m INPUTFILE.zip OUTPUTFILE.tif`
`s1_correction.py INPUTFILE.zip OUTPUTFILE.tif -m`

Process a single file using Docker (replace `input_dir` and `output_dir` with actual directories):

`docker run --rm -v /data_dir:/data_dir s1denoise s1_correction.py /data_dir/INPUTFILE.zip /data_dir/OUPUTFILE.tif`

Note that for enabling the landmask functionality, you need to download and install the watermask:

Expand All @@ -95,8 +101,7 @@ rm $MOD44WPATH/MOD44W.tgz

## Experimental scripts

Sub-directories in `s1denoise/experimentalData` contain scripts for training the noise scaling and power balancing coefficients and extra scaling.
See README files in these sub-dirs for details.
Sub-directories in `s1denoise/training` and `s1denoise/validation` contain scripts for training and validation of the noise scaling and power balancing coefficients and extra scaling. See README files in these sub-dirs for details.

## License
The project is licensed under the GNU general public license version 3.
9 changes: 9 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: s1denoise
dependencies:
- gdal
- lxml
- pip
- pip:
- beautifulsoup4
- requests

2 changes: 1 addition & 1 deletion s1denoise/denoising_parameters.json

Large diffs are not rendered by default.

50 changes: 43 additions & 7 deletions s1denoise/scripts/s1_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,59 @@
import argparse

import numpy as np
try:
from nansat import Nansat
except ImportError:
NANSAT_AVAILABLE = False
else:
NANSAT_AVAILABLE = True

from s1denoise.tools import run_correction

def parse_args(args):
''' parse input arguments '''
parser = argparse.ArgumentParser(
description="Correct Sentinel-1 TOPSAR EW GRDM for thermal and texture noise and angular dependence")
parser.add_argument('ifile', type=str, help='input Sentinel-1 file in SAFE format')
parser.add_argument('ofile', type=str, help='output GeoTIFF file')
parser.add_argument('-m', '--mask', help='Export land mask as a numpy file', action='store_true')
parser.add_argument('ifile', type=str, help='Input Sentinel-1 file in SAFE or zip format')
parser.add_argument('ofile', type=str, help='Output file')
parser.add_argument('-a', '--algorithm',
help='Name of the algorithm to use',
type=str,
default='NERSC',
choices=['ESA', 'NERSC', 'NERSC_TG'])
parser.add_argument('-g', '--geotiff', help='Export resulst as a GeoTIFF file', action='store_true')
parser.add_argument('-m', '--mask', help='Also export land mask as a numpy file', action='store_true')
return parser.parse_args(args)

def export_geotiff(ifile, ofile, d, mask):
""" Use Nansat to export the arrays as a geotiff file with georeference as in the input dataset """
remove_metadata = ['dataType', 'PixelFunctionType', 'SourceBand', 'SourceFilename', 'colormap', 'minmax', 'units', 'wkv']
n_inp = Nansat(ifile)
n_out = Nansat.from_domain(n_inp)
for pol in d:
parameters = n_inp.get_metadata(band_id='sigma0_%s' % pol)
for i in remove_metadata:
parameters.pop(i)
n_out.add_band(array=d[pol], parameters=parameters)
n_out.set_metadata(n_inp.get_metadata())
n_out.export(ofile, driver='GTiff')

def export_mask(ifile, ofile):
""" Export landmask as a numpy file """
n_inp = Nansat(ifile)
wm = n_inp.watermask()
np.savez_compressed(ofile + '_mask.npz', mask=wm[1])

if __name__ == '__main__':
args = parse_args(sys.argv[1:])
s1 = run_correction(args.ifile)
s1.export(args.ofile, driver='GTiff')
if (args.geotiff or args.mask) and not NANSAT_AVAILABLE:
raise ImportError(' Nansat is not installed and I can\'t export geotif or landmask. '
' Install Nansat or do not use "-g" and "-m". ')

d = run_correction(args.ifile, algorithm=args.algorithm)
if args.geotiff:
export_geotiff(args.ifile, args.ofile, d, args.mask)
else:
np.savez_compressed(args.ofile, **d)
if args.mask:
wm = s1.watermask()
np.savez_compressed(args.ofile + '_mask.npz', mask=wm[1])
export_mask(args.ifile, args.ofile)
Loading

0 comments on commit 526c81b

Please sign in to comment.