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

Native resampler fails for (some) GEO readers due to reduce_data=True #2476

Open
ameraner opened this issue May 10, 2023 · 21 comments
Open

Native resampler fails for (some) GEO readers due to reduce_data=True #2476

ameraner opened this issue May 10, 2023 · 21 comments

Comments

@ameraner
Copy link
Member

ameraner commented May 10, 2023

Describe the bug
The native resampler fails when trying to resampler GEO data onto grids (with the same projection).
This is solved by deactivateding the data reduction (scn_r = scn.resample('mtg_fci_fdss_1km', resampler='native', reduce_data=False)).
A plausible theory when looking at the code is that the reduce_data crops the deep space pixels around the disk, ending up with the slightly smaller array shape that then doesn't match anymore. The slices returned from get_area_slices here

satpy/satpy/scene.py

Lines 914 to 915 in 2ec18f6

slice_x, slice_y = source_area.get_area_slices(
destination_area, shape_divisible_by=factor)
are (slice(158, 10998, None), slice(157, 10983, None))

To Reproduce

filenames = sorted(glob(path_to_testdata + "*BODY*.nc"))
scn = Scene(filenames=filenames , reader='fci_l1c_nc')
scn.load(['vis_04'])
scn_resampled = scn.resample('mtg_fci_fdss_1km', resampler='native')

Similar failures have been reported by @simonrp84 using AHI data.

Expected behavior
Correct resampling.

Actual results

[DEBUG: 2023-05-10 19:25:24 : satpy.scene] Resampling DataID(name='vis_04', wavelength=WavelengthRange(min=0.384, central=0.444, max=0.504, unit='µm'), resolution=1000, calibration=<calibration.reflectance>, modifiers=())
Traceback (most recent call last):
  File "/tcenas/home/andream/PycharmProjects/dev_src/dev_src/local_readers_dev/fci_native_fail_test.py", line 121, in <module>
    scn_r = scn.resample('mtg_fci_fdss_1km', resampler='native')
  File "/tcenas/home/andream/code/satpy_latest/satpy/satpy/scene.py", line 954, in resample
    self._resampled_scene(new_scn, destination, resampler=resampler,
  File "/tcenas/home/andream/code/satpy_latest/satpy/satpy/scene.py", line 870, in _resampled_scene
    res = resample_dataset(dataset, destination_area, **kwargs)
  File "/tcenas/home/andream/code/satpy_latest/satpy/satpy/resample.py", line 1433, in resample_dataset
    new_data = resample(source_area, dataset, destination_area, fill_value=fill_value, **kwargs)
  File "/tcenas/home/andream/code/satpy_latest/satpy/satpy/resample.py", line 1396, in resample
    res = resampler_instance.resample(data, **kwargs)
  File "/tcenas/home/andream/code/satpy_latest/satpy/satpy/resample.py", line 991, in resample
    return super(NativeResampler, self).resample(data,
  File "/tcenas/home/andream/code/satpy_latest/satpy/satpy/resample.py", line 449, in resample
    return self.compute(data, cache_id=cache_id, **kwargs)
  File "/tcenas/home/andream/code/satpy_latest/satpy/satpy/resample.py", line 1042, in compute
    d_arr = self._expand_reduce(data.data, repeats)
  File "/tcenas/home/andream/code/satpy_latest/satpy/satpy/resample.py", line 1004, in _expand_reduce
    return _replicate(d_arr, repeats)
  File "/tcenas/home/andream/code/satpy_latest/satpy/satpy/resample.py", line 1086, in _replicate
    repeated_chunks = _get_replicated_chunk_sizes(d_arr, repeats)
  File "/tcenas/home/andream/code/satpy_latest/satpy/satpy/resample.py", line 1099, in _get_replicated_chunk_sizes
    raise ValueError("Expand factor must be a whole number")
ValueError: Expand factor must be a whole number

Additional context

scn["vis_04"].attrs["area"].crs == get_area_def("mtg_fci_fdss_1km").crs
Out[2]: False
get_area_def("mtg_fci_fdss_1km").crs
Out[3]: 
<Derived Projected CRS: PROJCRS["unknown",BASEGEOGCRS["unknown",DATUM["Unk ...>
Name: unknown
Axis Info [cartesian]:
- E[east]: Easting (metre)
- N[north]: Northing (metre)
Area of Use:
- undefined
Coordinate Operation:
- name: unknown
- method: Geostationary Satellite (Sweep Y)
Datum: Unknown based on WGS84 ellipsoid
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

scn["vis_04"].attrs["area"].crs
Out[4]: 
<Derived Projected CRS: PROJCRS["unknown",BASEGEOGCRS["unknown",DATUM["unk ...>
Name: unknown
Axis Info [cartesian]:
- E[east]: Easting (metre)
- N[north]: Northing (metre)
Area of Use:
- undefined
Coordinate Operation:
- name: unknown
- method: Geostationary Satellite (Sweep Y)
Datum: unknown
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

This was reported and discussed with @djhoese around here on Slack https://pytroll.slack.com/archives/C0LNH7LMB/p1682076158103019?thread_ts=1681293614.646929&cid=C0LNH7LMB .

@djhoese
Copy link
Member

djhoese commented May 10, 2023

I've had trouble reproducing this, but my biggest confusion is that the AreaDefinition area slices logic should be checking if the two CRSes (of the source area and the target area) are the same:

/~https://github.com/pytroll/pyresample/blob/0bc1a0b47220bf9225b8448121ec0c7e930476bf/pyresample/geometry.py#L2576-L2579

And then doing pretty simple math after that. The fancy geostationary logic only happens if the CRSes are not the same. So we need a case where this is broken and see how/why the CRSes aren't equal.

Can someone get me AHI or FCI or some other data that fails this reduce_data=True default case? I don't think I was able to reproduce it on my machine. Has anyone noticed something specific to an operating system for this?

@ameraner
Copy link
Member Author

ameraner commented May 10, 2023

@djhoese
Copy link
Member

djhoese commented May 10, 2023

So loading the area from the YAML and loading the area for the vis04 band in your example code shows that the datums for the CRSes are not equal. It is possible they used to be, but PROJ seems to want to take the a and the rf from the PROJ.4 dict in the reader and say "this is some unknown WGS84 definition".

In [9]: scn['vis_04'].attrs['area'].crs.datum
Out[9]: 
DATUM["unknown",
    ELLIPSOID["WGS 84",6378137,298.257223563,
        LENGTHUNIT["metre",1,
            ID["EPSG",9001]]]]

But for the YAML area it either says it is

In [20]: area_def.crs.datum
Out[20]: 
DATUM["Unknown based on WGS84 ellipsoid",
    ELLIPSOID["WGS 84",6378137,298.257223563,
        LENGTHUNIT["metre",1],
        ID["EPSG",7030]]]

Or if I add datum: 'WGS84' to the YAML I get:

In [8]: area_def.crs.datum
Out[8]: 
DATUM["World Geodetic System 1984",
    ELLIPSOID["WGS 84",6378137,298.257223563,
        LENGTHUNIT["metre",1]],
    ID["EPSG",6326]]

I'll have to ask pyproj for help.

@djhoese
Copy link
Member

djhoese commented May 10, 2023

pyproj4/pyproj#1284

@gerritholl
Copy link
Member

gerritholl commented Dec 17, 2024

This problem occurs even with reduce_data=False:

import hdf5plugin
from satpy import Scene
from glob import glob
fci_files = glob("/media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/*BODY*O_0016_003[89].nc")

sc = Scene(filenames={"fci_l1c_nc": fci_files})
sc.load(["night_microphysics"], generate=False, upper_right_corner="NE", pad_data=False)
ls = sc.resample(resampler="native", reduce_data=False)
ls.save_datasets()

Result:

[DEBUG: 2024-12-17 09:35:20 : satpy.readers.yaml_reader] Reading ('/data/gholl/checkouts/satpy/satpy/etc/readers/fci_l1c_nc.yaml',)
[DEBUG: 2024-12-17 09:35:20 : satpy.readers.yaml_reader] Assigning to fci_l1c_nc: ['/media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023902_20241210023924_N_JLS_O_0016_0039.nc', '/media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc', '/media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc', '/media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023902_20241210023924_N_JLS_O_0016_0039.nc']
[DEBUG: 2024-12-17 09:35:20 : satpy.readers.fci_l1c_nc] Reading: /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023902_20241210023924_N_JLS_O_0016_0039.nc
[DEBUG: 2024-12-17 09:35:20 : satpy.readers.fci_l1c_nc] Start: 2024-12-10 02:30:00
[DEBUG: 2024-12-17 09:35:20 : satpy.readers.fci_l1c_nc] End: 2024-12-10 02:40:00
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Reading: /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Start: 2024-12-10 02:30:00
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] End: 2024-12-10 02:40:00
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Reading: /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Start: 2024-12-10 02:30:00
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] End: 2024-12-10 02:40:00
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Reading: /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023902_20241210023924_N_JLS_O_0016_0039.nc
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Start: 2024-12-10 02:30:00
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] End: 2024-12-10 02:40:00
[DEBUG: 2024-12-17 09:35:21 : satpy.composites.config_loader] Looking for composites config file fci.yaml
[DEBUG: 2024-12-17 09:35:21 : pyorbital.tlefile] Path to the Pyorbital configuration (where e.g. platforms.txt is found): /data/gholl/mambaforge/envs/py312/lib/python3.12/site-packages/pyorbital/etc
[DEBUG: 2024-12-17 09:35:21 : satpy.composites.config_loader] Looking for composites config file visir.yaml
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Reading ir_105 from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Reading ir_105 from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023902_20241210023924_N_JLS_O_0016_0039.nc
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Channel ir_105 resolution: 5568
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Row/Cols: 139 / 5568
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Calculated area extent: (-5567999.994203024, 5009999.994783957, 5567999.994203011, 4731999.99507339)
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Channel ir_105 resolution: 5568
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Row/Cols: 139 / 5568
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Calculated area extent: (-5567999.994203024, 5287999.994494526, 5567999.994203011, 5009999.994783957)
[INFO: 2024-12-17 09:35:21 : satpy.readers.yaml_reader] Flipping Dataset ir_105 upsidedown.
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Reading ir_105 from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Reading ir_105 from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023902_20241210023924_N_JLS_O_0016_0039.nc
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Channel ir_105 resolution: 11136
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Row/Cols: 279 / 11136
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Calculated area extent: (-5567999.998550753, 5010999.998695726, 5567999.998550748, 4731999.998768344)
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Channel ir_105 resolution: 11136
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Row/Cols: 278 / 11136
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Calculated area extent: (-5567999.998550753, 5288999.9986233665, 5567999.998550748, 5010999.998695726)
[INFO: 2024-12-17 09:35:21 : satpy.readers.yaml_reader] Flipping Dataset ir_105 upsidedown.
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Reading ir_123 from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc
[DEBUG: 2024-12-17 09:35:21 : satpy.readers.fci_l1c_nc] Reading ir_123 from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023902_20241210023924_N_JLS_O_0016_0039.nc
[INFO: 2024-12-17 09:35:22 : satpy.readers.yaml_reader] Flipping Dataset ir_123 upsidedown.
[DEBUG: 2024-12-17 09:35:22 : satpy.readers.fci_l1c_nc] Reading ir_38 from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc
[DEBUG: 2024-12-17 09:35:22 : satpy.readers.fci_l1c_nc] Reading ir_38 from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023902_20241210023924_N_JLS_O_0016_0039.nc
[INFO: 2024-12-17 09:35:22 : satpy.readers.yaml_reader] Flipping Dataset ir_38 upsidedown.
[DEBUG: 2024-12-17 09:35:22 : satpy.readers.fci_l1c_nc] Reading ir_38_pixel_quality from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc
[DEBUG: 2024-12-17 09:35:22 : satpy.readers.fci_l1c_nc] Reading ir_38_pixel_quality from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023902_20241210023924_N_JLS_O_0016_0039.nc
[INFO: 2024-12-17 09:35:22 : satpy.readers.yaml_reader] Flipping Dataset unknown_name upsidedown.
[DEBUG: 2024-12-17 09:35:22 : satpy.readers.fci_l1c_nc] Reading ir_123_pixel_quality from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc
[DEBUG: 2024-12-17 09:35:22 : satpy.readers.fci_l1c_nc] Reading ir_123_pixel_quality from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023902_20241210023924_N_JLS_O_0016_0039.nc
[INFO: 2024-12-17 09:35:22 : satpy.readers.yaml_reader] Flipping Dataset unknown_name upsidedown.
[DEBUG: 2024-12-17 09:35:22 : satpy.readers.fci_l1c_nc] Reading ir_105_pixel_quality from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc
[DEBUG: 2024-12-17 09:35:22 : satpy.readers.fci_l1c_nc] Reading ir_105_pixel_quality from /media/nas/x23352/MTG/FCI/L1c-cases/202412100230-norrbotten/W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023902_20241210023924_N_JLS_O_0016_0039.nc
[INFO: 2024-12-17 09:35:22 : satpy.readers.yaml_reader] Flipping Dataset unknown_name upsidedown.
[DEBUG: 2024-12-17 09:35:22 : satpy.scene] Resampling DataID(name='ir_105', wavelength=WavelengthRange(min=9.8, central=10.5, max=11.2, unit='µm'), resolution=1000, calibration=<2>, modifiers=())
[DEBUG: 2024-12-17 09:35:22 : satpy.scene] Data reduction disabled by the user
[DEBUG: 2024-12-17 09:35:22 : satpy.scene] Resampling DataID(name='ir_105_pixel_quality', resolution=1000, modifiers=())
[DEBUG: 2024-12-17 09:35:22 : satpy.scene] Data reduction disabled by the user
[DEBUG: 2024-12-17 09:35:22 : satpy.scene] Resampling DataID(name='ir_105', wavelength=WavelengthRange(min=9.8, central=10.5, max=11.2, unit='µm'), resolution=2000, calibration=<2>, modifiers=())
[DEBUG: 2024-12-17 09:35:22 : satpy.scene] Data reduction disabled by the user
Traceback (most recent call last):
  File "/data/gholl/checkouts/protocode/mwe/fci-native-resample-fails.py", line 10, in <module>
    ls = sc.resample(resampler="native", reduce_data=False)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/gholl/checkouts/satpy/satpy/scene.py", line 980, in resample
    self._resampled_scene(new_scn, destination, resampler=resampler,
  File "/data/gholl/checkouts/satpy/satpy/scene.py", line 896, in _resampled_scene
    res = resample_dataset(dataset, destination_area, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/gholl/checkouts/satpy/satpy/resample.py", line 1090, in resample_dataset
    new_data = resample(source_area, dataset, destination_area, fill_value=fill_value, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/gholl/checkouts/satpy/satpy/resample.py", line 1053, in resample
    res = resampler_instance.resample(data, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/gholl/checkouts/satpy/satpy/resample.py", line 677, in resample
    return super(NativeResampler, self).resample(data,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/gholl/mambaforge/envs/py312/lib/python3.12/site-packages/pyresample/resampler.py", line 146, in resample
    return self.compute(data, cache_id=cache_id, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/gholl/checkouts/satpy/satpy/resample.py", line 728, in compute
    d_arr = self._expand_reduce(data.data, repeats)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/gholl/checkouts/satpy/satpy/resample.py", line 690, in _expand_reduce
    return _replicate(d_arr, repeats)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/gholl/checkouts/satpy/satpy/resample.py", line 776, in _replicate
    repeated_chunks = _get_replicated_chunk_sizes(d_arr, repeats)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/gholl/checkouts/satpy/satpy/resample.py", line 789, in _get_replicated_chunk_sizes
    raise ValueError("Expand factor must be a whole number")
ValueError: Expand factor must be a whole number

@gerritholl
Copy link
Member

@djhoese I see that the proj issue linked in the pyproj discussion you linked is closed as completed. Does that mean that upgrading proj should solve this problem?

@djhoese
Copy link
Member

djhoese commented Dec 17, 2024

I don't remember most of this but the related PROJ PR was released in PROJ 9.3.0. If someone could test with PROJ 9.3+ with modern FCI data then that'd be nice.

I see now that your comment above was still experiencing it. I suppose there is a chance there is something else going on even with new-ish PROJ versions. We'd have to do the same comparisons as before to see if this is CRSes not being equal or something else.

@djhoese
Copy link
Member

djhoese commented Dec 17, 2024

The way you ran your code @gerritholl makes me think you have different amounts of data between the various bands. That is, padding being off and resampling taking the finest resolution area, I would assume some areas aren't equal and that's causing the problems. Can you print out the areas after sc.load?

@simonrp84
Copy link
Member

I've also seen this with FCI, even when I have all segments. Perhaps some rounding error in the area defs for the different resolutions? I vaguely recall that Himawari used to have a similar issue that was solved with some rounding logic or somehting like that...

@gerritholl
Copy link
Member

Areas:

In [9]: sc["ir_105"].attrs["area"]
Out[9]:
Area ID: mtg_fci_fdss_1km
Description: MTG FCI Full Disk Scanning Service area definition with 1 km resolution
Projection: {'ellps': 'WGS84', 'h': '35786400', 'lon_0': '0', 'no_defs': 'None', 'proj': 'geos', 'type': 'crs', 'units': 'm', 'x_0': '0', 'y_0': '0'}
Number of columns: 11136
Number of rows: 557
Area extent: (np.float64(-5567999.9986), np.float64(4731999.9988), np.float64(5567999.9986), np.float64(5288999.9986))

[DEBUG: 2024-12-18 10:36:00 : asyncio] Using selector: EpollSelector
In [10]: sc["ir_123"].attrs["area"]
Out[10]:
Area ID: mtg_fci_fdss_2km
Description: MTG FCI Full Disk Scanning Service area definition with 2 km resolution
Projection: {'ellps': 'WGS84', 'h': '35786400', 'lon_0': '0', 'no_defs': 'None', 'proj': 'geos', 'type': 'crs', 'units': 'm', 'x_0': '0', 'y_0': '0'}
Number of columns: 5568
Number of rows: 278
Area extent: (np.float64(-5567999.9942), np.float64(4731999.9951), np.float64(5567999.9942), np.float64(5287999.9945))

[DEBUG: 2024-12-18 10:36:12 : asyncio] Using selector: EpollSelector
In [11]: sc["ir_38"].attrs["area"]
Out[11]:
Area ID: mtg_fci_fdss_1km
Description: MTG FCI Full Disk Scanning Service area definition with 1 km resolution
Projection: {'ellps': 'WGS84', 'h': '35786400', 'lon_0': '0', 'no_defs': 'None', 'proj': 'geos', 'type': 'crs', 'units': 'm', 'x_0': '0', 'y_0': '0'}
Number of columns: 11136
Number of rows: 557
Area extent: (np.float64(-5567999.9986), np.float64(4731999.9988), np.float64(5567999.9986), np.float64(5288999.9986))

@djhoese
Copy link
Member

djhoese commented Dec 18, 2024

Where's the extra row coming from on the 1km? 278 * 2 => 556, not 557.

@gerritholl
Copy link
Member

From the data:

$ ncdump -h W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc | grep -A3 'group: ir_123'
  group: ir_123 {
    dimensions:
        x = 5568 ;
        y = UNLIMITED ; // (139 currently)
$ ncdump -h W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--CHK-BODY--DIS-NC4E_C_EUMT_20241210024051_IDPFI_OPE_20241210023849_20241210023919_N_JLS_O_0016_0038.nc | grep -A3 'group: ir_105'
  group: ir_105_hr {
    dimensions:
        x = 11136 ;
        y = UNLIMITED ; // (279 currently)

@simonrp84
Copy link
Member

Is this a single segment of FCI data? I'm trying to work out why the size is 557...that's not a factor of 5568 or 11136. Do some segments have a size of 556 and others of 557 perhaps, and in this specific case it happens that the size difference is not consistent between bands?

@gerritholl
Copy link
Member

Yes, this is a single segment. The case I posted yesterday consisted of two segments.

@djhoese
Copy link
Member

djhoese commented Dec 18, 2024

How are the pixels aligned between resolutions? For example, on ABI it is meant to be 4 250m pixels perfectly aligned with 1 500m, and similarly 4 500m make up 1 1000m pixel. At least that's how we're "allowed" to think about it. How does this work for this FCI data?

@ameraner
Copy link
Member Author

ameraner commented Dec 18, 2024

The single segments of FCI data have different sizes (+- 1 row between each other) and indeed the same segment can have different coverage depending on the channel resolution (I notice this only now as well :O).
So when you don't have all segments and you're not padding to full disk (as in Gerrit's example with pad_data=False, it's to be expected that native will fail.

However with padding activated this should not happen, because then the square arrays are always multiple of each other between resolutions. (The FCI padding mechanism is designed to take the different segment sizes into account)

And also for FCI, pixels of different resolutions are fully overlapping, same as Dave is describing for ABI.

@djhoese
Copy link
Member

djhoese commented Dec 18, 2024

And also for FCI, pixels of different resolutions are fully overlapping, same as Dave is describing for ABI.

@ameraner But how can this be true if there are a different number of rows (unaligned) between resolutions for each segment?

@gerritholl So I guess the main question as far as this issue is concerned is do you get this failure when you have all segments and no padding is used? @simonrp84 You mentioned you've seen it, but how long ago was that and what version of PROJ was installed in your environment? And were you padding or not?

@ameraner
Copy link
Member Author

It is always true when all segments are merged together and you get the complete full-disc, as then the single segment sizes don't matter anymore (they will always sum up to the full-disc grid size).
When having only partial segments this might not work indeed, but in that case you also have different area extents for your partial area between resolutions...

@gerritholl
Copy link
Member

So I guess the main question as far as this issue is concerned is do you get this failure when you have all segments and no padding is used?

No. Only if I have a subset of segments and I avoid using padding.

@ameraner Are the last ten segments (= Q4) guaranteed to be aligned between FDHSI and HRFI even without padding? That will be relevant in particular for MTG-I2 / RSS data.

@ameraner
Copy link
Member Author

Good question, I will check.

@simonrp84
Copy link
Member

@simonrp84 You mentioned you've seen it, but how long ago was that and what version of PROJ was installed in your environment? And were you padding or not?

It was a good while ago, maybe last year or even earlier, and I vaguely recall that you made a fix for it. Could be wrong though.
No idea what version of PROJ I had. Wasn't padding as I always have that switched off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants