Commit 01e26ed6 authored by Christian Hohmann's avatar Christian Hohmann

Merge branch 'json_to_tiff_mask_opt' into 'master'

Add option to json_to_tiff to skip mask conversion.

Closes #31

See merge request !16
parents 48fb8fa3 be405d44
Pipeline #19666 passed with stages
in 45 minutes and 19 seconds
......@@ -398,7 +398,7 @@ def mk_rgb(basedir, outdir, rgb_comb=("B04", "B03", "B02"), rgb_gamma=(1.0, 1.0,
def json_to_tiff(out_mode, api_result, only_tile, outpath, out_prefix, wl,
level, stack_resolution, bands, tif_opts, logger=None):
level, stack_resolution, bands, tif_opts, keep_mask, logger=None):
"""
Get data from json dict and save if as singletif files OR:
save all requested bands plus cloudmask as one tiff file per date and tile.
......@@ -464,7 +464,7 @@ def json_to_tiff(out_mode, api_result, only_tile, outpath, out_prefix, wl,
tif_list.append(outfile)
# create tif file for clm
if level != "L1C":
if level != "L1C" and keep_mask:
clm = api_result['Metadata'][tile_key]['MSK_' + str(res) + 'm']['data'][ti]
clm_arr = np.asarray(clm)
clm_outfile = "{path}/{sensor}_{level}_{pref}_{date}_{tile}_MSK_{res}m.tif".format(
......@@ -540,7 +540,7 @@ def json_to_tiff(out_mode, api_result, only_tile, outpath, out_prefix, wl,
geo_proj = api_result['Results'][tile_key][band_key]['mapinfo']['geo_projection']
geotrans = (x_min, int(stack_resolution), 0, y_max, 0, -int(stack_resolution))
driver = gdal.GetDriverByName("GTiff")
if level == "L1C":
if level == "L1C" or not keep_mask:
outfile = "{path}/{sensor}_{level}_{pref}_{date}_{tile}_{band}_{res}m.tif".format(
path=outpath, pref=out_prefix, date=ac_date, tile=tile_key, band=bands, level=level,
res=stack_resolution, sensor=sensor)
......@@ -548,7 +548,7 @@ def json_to_tiff(out_mode, api_result, only_tile, outpath, out_prefix, wl,
outfile = "{path}/{sensor}_{level}_{pref}_{date}_{tile}_{band}_MSK_{res}m.tif".format(
path=outpath, pref=out_prefix, date=ac_date, tile=tile_key, band=bands, level=level,
res=stack_resolution, sensor=sensor)
img = driver.Create(outfile, cols, rows, count_bands + 1, gdal.GDT_Int32)
img = driver.Create(outfile, cols, rows, count_bands + keep_mask, gdal.GDT_Int32, options=tif_opts)
img.SetGeoTransform(geotrans)
img.SetProjection(geo_proj)
......@@ -564,11 +564,11 @@ def json_to_tiff(out_mode, api_result, only_tile, outpath, out_prefix, wl,
slice += 1
# This is done outside of the loop because one can not set a nodata_value for each band
img.GetRasterBand(slice).SetNoDataValue(api_result['Results'][tile_key][band_key]["fill_value"])
img.GetRasterBand(slice-1).SetNoDataValue(api_result['Results'][tile_key][band_key]["fill_value"])
img.SetMetadata({'TIFFTAG_YRESOLUTION': '%s' % stack_resolution,
'TIFFTAG_XRESOLUTION': '%s' % stack_resolution})
if level != "L1C":
if level != "L1C" and keep_mask:
clm = api_result['Metadata'][tile_key]['MSK_%sm' % stack_resolution]['data'][ti]
clm_arr = np.asarray(clm)
img.GetRasterBand(slice).WriteArray(clm_arr)
......@@ -745,7 +745,7 @@ def __get_auth(logger=None):
def client(outpath="", out_prefix="", out_mode="json", compress="ZSTD-2" , geo_ll=(), geo_ur=(), sensor="S2A", bands="", max_cloudy="0.5",
level="L2A", start_date="", end_date="", version="0.15", suffix="", minimum_fill="0.8",
only_tile="", stack_resolution="10", quiet=False, rgb_extension="jpg", rgb_bands_selection="realistic",
only_tile="", stack_resolution="10", keep_mask=True, quiet=False, rgb_extension="jpg", rgb_bands_selection="realistic",
merge_tifs=False, merge_tile=None, onlytime=False, timeout=None):
"""
Downloads data via API and saves it in a wanted file format (.json, .tiff or .nc) or alternatively returns a python
......@@ -868,7 +868,7 @@ def client(outpath="", out_prefix="", out_mode="json", compress="ZSTD-2" , geo_l
elif out_mode == "single" or out_mode == "stack":
logger.info("Converting data to %s tif-files ..." % out_mode, )
tif_list = json_to_tiff(out_mode, api_result, only_tile, outpath, out_prefix, wl, level, stack_resolution,
bands, tif_opts=tif_opts, logger=logger)
bands, tif_opts=tif_opts, keep_mask=keep_mask, logger=logger)
if merge_tifs is True:
merge_tiles(tif_list, out_mode=out_mode, target_tile=merge_tile)
......@@ -877,7 +877,7 @@ def client(outpath="", out_prefix="", out_mode="json", compress="ZSTD-2" , geo_l
with tempfile.TemporaryDirectory(dir=outpath) as tmp_dir:
tif_list = json_to_tiff("stack", api_result, only_tile, tmp_dir, out_prefix, wl, level, stack_resolution,
bands, tif_opts=tif_opts, logger=logger)
bands, tif_opts=tif_opts, keep_mask=keep_mask, logger=logger)
if merge_tifs is True:
merge_tiles(tif_list, out_mode="stack", target_tile=merge_tile)
......
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