Skip to content

Commit

Permalink
add config option to turn off mitiff corner corretion
Browse files Browse the repository at this point in the history
  • Loading branch information
Trygve Aspenes committed Jan 5, 2024
1 parent afb4049 commit 2185a19
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
51 changes: 51 additions & 0 deletions satpy/tests/writer_tests/test_mitiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,57 @@ def test_convert_proj4_string(self):
proj4_string = w._add_proj4_string(ds1, ds1)
assert proj4_string == check["proj4"]

def test_correction_proj4_string(self):
"""Test correction of proj4 lower left coordinate."""
import dask.array as da
import xarray as xr
from pyresample.geometry import AreaDefinition

from satpy.writers.mitiff import MITIFFWriter
area_def = AreaDefinition(
"test",
"test",
"test",
"+proj=merc",
100,
200,
(-1000., -1500., 1000., 1500.),
)

ds1 = xr.DataArray(
da.zeros((10, 20), chunks=20),
dims=("y", "x"),
attrs={"area": area_def}
)
default_expected_proj4_string = ' Proj string: +init=EPSG:3395 +towgs84=0,0,0 +units=km +x_0=1020.000000 +y_0=1515.000000\n'
w = MITIFFWriter(filename="dummy.tif", base_dir=self.base_dir)
proj4_string = w._add_proj4_string(ds1, ds1)
assert proj4_string == default_expected_proj4_string

kwargs = {'mitiff_pixel_adjustment': False}
new_expected_proj4_string = ' Proj string: +init=EPSG:3395 +towgs84=0,0,0 +units=km +x_0=1000.000000 +y_0=1500.000000\n'
w = MITIFFWriter(filename="dummy.tif", base_dir=self.base_dir)
proj4_string = w._add_proj4_string(ds1, ds1, **kwargs)
assert proj4_string == new_expected_proj4_string

area_def2 = AreaDefinition(
"test",
"test",
"test",
"+proj=merc +x_0=0 +y_0=0",
100,
200,
(-1000., -1500., 1000., 1500.),
)
ds2 = xr.DataArray(
da.zeros((10, 20), chunks=20),
dims=("y", "x"),
attrs={"area": area_def2}
)
w = MITIFFWriter(filename="dummy.tif", base_dir=self.base_dir)
proj4_string = w._add_proj4_string(ds2, ds2, **kwargs)
assert proj4_string == new_expected_proj4_string

def test_save_dataset_palette(self):
"""Test writer operation as palette."""
from satpy.writers.mitiff import MITIFFWriter
Expand Down
24 changes: 16 additions & 8 deletions satpy/writers/mitiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def _add_sizes(self, datasets, first_dataset):

return _image_description

def _add_proj4_string(self, datasets, first_dataset):
def _add_proj4_string(self, datasets, first_dataset, **kwargs):
import warnings

proj4_string = " Proj string: "
Expand Down Expand Up @@ -259,31 +259,39 @@ def _add_proj4_string(self, datasets, first_dataset):
if "units" not in proj4_string:
proj4_string += " +units=km"

proj4_string = self._append_projection_center(proj4_string, datasets, first_dataset, x_0, y_0)
proj4_string = self._append_projection_center(proj4_string, datasets, first_dataset, x_0, y_0, **kwargs)
LOG.debug("proj4_string: %s", proj4_string)
proj4_string += "\n"

return proj4_string

def _append_projection_center(self, proj4_string, datasets, first_dataset, x_0, y_0):
def _append_projection_center(self, proj4_string, datasets, first_dataset, x_0, y_0, **kwargs):
if isinstance(datasets, list):
dataset = first_dataset
else:
dataset = datasets
corner_correction_x = dataset.attrs["area"].pixel_size_x
corner_correction_y = dataset.attrs["area"].pixel_size_y
try:
if kwargs['mitiff_pixel_adjustment'] is False:
corner_correction_x = 0
corner_correction_y = 0
except KeyError:
pass
if "x_0" not in proj4_string:
proj4_string += " +x_0=%.6f" % (
(-dataset.attrs["area"].area_extent[0] +
dataset.attrs["area"].pixel_size_x) + x_0)
corner_correction_x) + x_0)
proj4_string += " +y_0=%.6f" % (
(-dataset.attrs["area"].area_extent[1] +
dataset.attrs["area"].pixel_size_y) + y_0)
corner_correction_y) + y_0)
elif "+x_0=0" in proj4_string and "+y_0=0" in proj4_string:
proj4_string = proj4_string.replace("+x_0=0", "+x_0=%.6f" % (
(-dataset.attrs["area"].area_extent[0] +
dataset.attrs["area"].pixel_size_x) + x_0))
corner_correction_x) + x_0))
proj4_string = proj4_string.replace("+y_0=0", "+y_0=%.6f" % (
(-dataset.attrs["area"].area_extent[1] +
dataset.attrs["area"].pixel_size_y) + y_0))
corner_correction_y) + y_0))
return proj4_string

def _convert_epsg_to_proj(self, proj4_string, x_0):
Expand Down Expand Up @@ -563,7 +571,7 @@ def _make_image_description(self, datasets, **kwargs):

_image_description += " Map projection: Stereographic\n"

_image_description += self._add_proj4_string(datasets, first_dataset)
_image_description += self._add_proj4_string(datasets, first_dataset, **kwargs)

_image_description += " TrueLat: 60N\n"
_image_description += " GridRot: 0\n"
Expand Down

0 comments on commit 2185a19

Please sign in to comment.