Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

t.sentinel.import: Add option to add a band offset after importing S-2 data #23

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions t.sentinel.import/t.sentinel.import.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ <h2>DESCRIPTION</h2>

<em>t.sentinel.import</em> is a GRASS GIS addon Python script to
download and import the Sentinel-2 scenes and create a STRDS.
<p>
Since Sentinel-2 <a href="https://forum.step.esa.int/t/info-introduction-of-additional-radiometric-offset-in-pb04-00-products/35431">Processing Baseline 4.0.0</a>, a systematic offset has been introduced to all reflectance values.
To account for this, the <b>offset</b> option allows the user to indicate how reflectance data should be corrected (e.g. -1000).
Comment on lines +6 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Since Sentinel-2 <a href="https://forum.step.esa.int/t/info-introduction-of-additional-radiometric-offset-in-pb04-00-products/35431">Processing Baseline 4.0.0</a>, a systematic offset has been introduced to all reflectance values.
To account for this, the <b>offset</b> option allows the user to indicate how reflectance data should be corrected (e.g. -1000).
Since Sentinel-2 <a href="https://forum.step.esa.int/t/info-introduction-of-additional-radiometric-offset-in-pb04-00-products/35431">Processing Baseline 4.0.0</a>, a systematic offset has been introduced
to all reflectance values.<br>
To account for this, the <b>offset</b> option allows the user to indicate how reflectance data should
be corrected (e.g. -1000).

(break long lines, see https://github.com/OSGeo/grass/blob/main/doc/development/style_guide.md#markup-style-guide - "More notes on markup")


<h2>EXAMPLE</h2>

Expand Down
42 changes: 42 additions & 0 deletions t.sentinel.import/t.sentinel.import.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@
# % answer: 1
# %end

# %option
# % key: offset
# % type: integer
# % required: no
# % description: Offset to add to the Sentinel bands to due to specific processing baseline (e.g. -1000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# % description: Offset to add to the Sentinel bands to due to specific processing baseline (e.g. -1000)
# % description: Offset to add to the Sentinel-2 bands to due to specific processing baseline (e.g. -1000)

Perhaps only S2 related?

# %end

# %rules
# % collective: start, end, producttype
# % excludes: s2names, start, end, producttype
Expand All @@ -201,6 +208,7 @@
import multiprocessing as mp
import os
import re
import shutil
import sys

import grass.script as grass
Expand Down Expand Up @@ -322,6 +330,11 @@ def main():
"\n" +
"g.extension i.zero2null")

if not grass.find_program('r.mapcalc.tiled', '--help'):
grass.fatal(_("The 'r.mapcalc.tiled' module was not found, install it first:") +
"\n" +
"g.extension r.mapcalc.tiled")

# create temporary directory to download data
if tmpdirectory:
if not os.path.isdir(tmpdirectory):
Expand Down Expand Up @@ -498,6 +511,35 @@ def main():
for rast in grass.parse_command('g.list', type='raster', mapset=new_mapset):
maplist.append(rast)
grass.run_command('g.copy', raster=rast + '@' + new_mapset + ',' + rast)

if options["offset"]:
# save the description.json
tmp_desc_dir = os.path.join(tmpdirectory, "descriptions_json")
if not os.path.isdir(tmp_desc_dir):
try:
os.makedirs(tmp_desc_dir)
except:
grass.fatal(_(f"Unable to create directory {tmp_desc_dir}"))

desc_file_save = os.path.join(tmp_desc_dir, f"{rast}_description.json")
desc_file_in = os.path.join(json_standard_folder, rast, "description.json")
shutil.copy(desc_file_in, desc_file_save)

# calculate offset (metadata in cell_misc will be lost)
tmp_rast = f"rast_tmp_{os.getpid()}"
mapc_exp = (f"{tmp_rast} = if({rast} + {options['offset']} < 0, "
f"0, {rast} + {options['offset']} )")
grass.run_command(f"r.mapcalc.tiled",
expression=mapc_exp,
nprocs=nprocs_final,
quiet=True)
grass.run_command("g.copy",
raster=f"{tmp_rast},{rast}",
overwrite=True,
quiet=True)
# copy the description.json back
shutil.copy(desc_file_save, desc_file_in)

grass.utils.try_rmdir(os.path.join(gisdbase, location, new_mapset))
# space time dataset
grass.message(_("Creating STRDS of Sentinel scenes ..."))
Expand Down