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

Cropping S3 image not working #2502

Closed
system123 opened this issue Jun 7, 2023 · 4 comments
Closed

Cropping S3 image not working #2502

system123 opened this issue Jun 7, 2023 · 4 comments
Assignees

Comments

@system123
Copy link
Contributor

Describe the bug
I am loading a Sentinel-3 OLCI Level 2 image and trying to crop it to my ROI bounds (specified in EPSG:4326) but this fails with a NotImplementedError on the get_area_slices method from pyresample

To Reproduce

from satpy.scene import Scene

scn = Scene(glob('S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/*.nc'), reader='olci_l2')

bbox = (12.621294639250404, -37.43494532540599, 20.614759096943878, -28.59809280356893)

scn.load(['Oa03'])
scn.crop(ll_bbox=bbox)

Expected behavior
I'd expect the resultant image to be cropped to the region of interest specified.

Actual results

[DEBUG: 2023-06-07 14:11:57 : satpy.readers.yaml_reader] Reading ('~opt/anaconda3/envs/represent/lib/python3.9/site-packages/satpy/etc/readers/olci_l2.yaml',)
[DEBUG: 2023-06-07 14:11:57 : satpy.readers.yaml_reader] Assigning to olci_l2: ['~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa04_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa08_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa17_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa12_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa06_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa18_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa21_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa05_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa01_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa07_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa02_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa11_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa16_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa09_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa03_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/Oa10_reflectance.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/chl_nn.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/chl_oc4me.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/iop_nn.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/trsp.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/tsm_nn.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/wqsf.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/iwv.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/tie_geometries.nc', '~Downloads/TestData/2023-05-16/SENTINEL3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/S3B_OL_2_WFR____20230516T075824_20230516T080124_20230516T094314_0179_079_263_3600_MAR_O_NR_003.SEN3/geo_coordinates.nc']
[DEBUG: 2023-06-07 14:11:57 : satpy.readers.olci_nc] Reading latitude.
[DEBUG: 2023-06-07 14:11:57 : satpy.readers.yaml_reader] No coordinates found for DataID(name='latitude', resolution=300, modifiers=())
[DEBUG: 2023-06-07 14:11:57 : satpy.readers.olci_nc] Reading longitude.
[DEBUG: 2023-06-07 14:11:57 : satpy.readers.yaml_reader] No coordinates found for DataID(name='longitude', resolution=300, modifiers=())
[DEBUG: 2023-06-07 14:11:57 : satpy.readers.olci_nc] Reading Oa03.
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
~Documents/Research/Chl/Code/S3OCLI_Chla.ipynb Cell 6 in ()
      5 bbox = (12.621294639250404, -37.43494532540599, 20.614759096943878, -28.59809280356893)
      7 scn.load(['Oa03'])
----> 8 scn.crop(ll_bbox=bbox)

File ~/opt/anaconda3/envs/represent/lib/python3.9/site-packages/satpy/scene.py:722, in Scene.crop(self, area, ll_bbox, xy_bbox, dataset_ids)
    720 if isinstance(area, str):
    721     area = get_area_def(area)
--> 722 new_coarsest_area, min_y_slice, min_x_slice = self._slice_area_from_bbox(
    723     coarsest_area, area, ll_bbox, xy_bbox)
    724 new_target_areas = {}
    725 for src_area, dataset_ids in new_scn.iter_by_area():

File ~/opt/anaconda3/envs/represent/lib/python3.9/site-packages/satpy/scene.py:605, in Scene._slice_area_from_bbox(src_area, dst_area, ll_bbox, xy_bbox)
    600 elif xy_bbox is not None:
    601     dst_area = AreaDefinition(
    602         'crop_area', 'crop_area', 'crop_xy',
    603         src_area.crs, src_area.width, src_area.height,
    604         xy_bbox)
--> 605 x_slice, y_slice = src_area.get_area_slices(dst_area)
    606 return src_area[y_slice, x_slice], y_slice, x_slice

File ~/opt/anaconda3/envs/represent/lib/python3.9/site-packages/pyresample/geometry.py:590, in BaseDefinition.get_area_slices(self, area_to_cover)
    588 def get_area_slices(self, area_to_cover):
    589     """Compute the slice to read based on an `area_to_cover`."""
--> 590     raise NotImplementedError

NotImplementedError:

Screenshots
If applicable, add screenshots to help explain your problem.

Environment Info:

  • OSX
  • satpy: 0.42.2
  • pyresample: 1.27.0
  • python: 3.9.12
@mraspaud
Copy link
Member

mraspaud commented Jun 7, 2023

@system123 thanks for reporting this issue.

From what I understand, the data you are working with (Sentinel 3, olci data, level 2) is not provided reprojected, but rather is provided with longitudes and latitude for every pixel. As we cannot make any assumptions on the pixel positions, this is a difficult problem and is indeed not implemented at the moment.

As an alternative, I can suggest trying to resample the data to a gridded region of interest if that helps?

@mraspaud mraspaud self-assigned this Jun 7, 2023
@system123
Copy link
Contributor Author

Thanks that makes sense. Resampling the data to a grid appears to work fine.

Ideally I wanted to crop images from other sensors to the bounds of the S3 image that I have, how could the be achieved if S3 doesn't have an associated AreaDefinition? Is there a way to extract the bounding box of the S3 image without resampling it?

@mraspaud
Copy link
Member

mraspaud commented Jun 7, 2023

In principle you should be able to get the boundary longitudes and latitudes using the boundary method on SwathDefinition objects (which for S3 data will be in the channels attribute called area)

@mraspaud
Copy link
Member

Is it ok closing this issue now?

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

No branches or pull requests

2 participants