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

The debug run reports an error, but there is no problem running after stopping at the breakpoint #2907

Closed
haiyangdaozhang opened this issue Sep 24, 2024 · 14 comments

Comments

@haiyangdaozhang
Copy link

Describe the bug
I encountered a very strange problem. When I used debug or ran directly, an error was reported. However, I set a breakpoint on the last line. When I ran to the breakpoint and ran the last line ( scn_new.save_dataset(iproduct,filename=filename_product)) in the interactive interface, there was no error and an image was generated.
The received image is not very good, there are some bad sectors, is it related to this?
To Reproduce

# Your code here
        reader = "viirs_sdr"
        compsite_name_list = ['ocean_color']
        files_no_project = self.find_files_not_process(sat_type="viirs")

        projection = {'proj': 'latlong'}
        area_def = create_area_def(area_id='User_define', projection=projection, resolution=0.02, units='degrees')

        for ifile in files_no_project:
            day_night = self.judgeDayNight(reader, ifile)
            if day_night == "night":
                continue
            else:
                filenames = self.search_file(ifile)
                scn = Scene(reader=reader, filenames=filenames)

                scn.load(compsite_name_list)
                scn_new = scn.resample(area_def,resampler='nearest')

                filename_output = self.get_output_filename(ifile)

                for iproduct in compsite_name_list:

                    filename_product = filename_output.replace("needname", iproduct)
                    scn_new.save_dataset(iproduct,filename=filename_product)

The error reported when running directly is as follows:

The following datasets were not created and may require resampling to be generated: DataID(name='ocean_color')
Could not load dataset 'DataID(name='M05', wavelength=WavelengthRange(min=0.662, central=0.672, max=0.682, unit='µm'), resolution=742, calibration=<calibration.reflectance>, modifiers=('sunz_corrected',))': Chunks do not add up to shape. Got chunks=((752, -15888, -15888, -15888, 752, 768), (3200,)), shape=(752, 3200)
Traceback (most recent call last):
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 836, in _load_dataset_with_area
    ds = self._load_dataset_data(file_handlers, dsid, **kwargs)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 736, in _load_dataset_data
    proj = self._load_dataset(dsid, ds_info, file_handlers, **kwargs)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 712, in _load_dataset
    projectable = fh.get_dataset(dsid, ds_info)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_sdr.py", line 134, in get_dataset
    data = self.scale_data_to_specified_unit(data, dataset_id, ds_info)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 191, in scale_data_to_specified_unit
    return self.scale_swath_data(data, factors, dataset_group)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 175, in scale_swath_data
    data = self._map_and_apply_factors(data, factors, rows_per_gran)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 206, in _map_and_apply_factors
    dask_data = data.data.rechunk((tuple(rows_per_gran), data.data.chunks[1]))
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\core.py", line 2745, in rechunk
    return rechunk(self, chunks, threshold, block_size_limit, balance)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\rechunk.py", line 297, in rechunk
    chunks = normalize_chunks(
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\core.py", line 3105, in normalize_chunks
    raise ValueError(
ValueError: Chunks do not add up to shape. Got chunks=((752, -15888, -15888, -15888, 752, 768), (3200,)), shape=(752, 3200)
Could not load dataset 'DataID(name='M03', wavelength=WavelengthRange(min=0.478, central=0.488, max=0.498, unit='µm'), resolution=742, calibration=<calibration.reflectance>, modifiers=('sunz_corrected',))': Chunks do not add up to shape. Got chunks=((752, -15888, -15888, -15888, 752, 768), (3200,)), shape=(752, 3200)
Traceback (most recent call last):
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 836, in _load_dataset_with_area
    ds = self._load_dataset_data(file_handlers, dsid, **kwargs)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 736, in _load_dataset_data
    proj = self._load_dataset(dsid, ds_info, file_handlers, **kwargs)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 712, in _load_dataset
    projectable = fh.get_dataset(dsid, ds_info)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_sdr.py", line 134, in get_dataset
    data = self.scale_data_to_specified_unit(data, dataset_id, ds_info)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 191, in scale_data_to_specified_unit
    return self.scale_swath_data(data, factors, dataset_group)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 175, in scale_swath_data
    data = self._map_and_apply_factors(data, factors, rows_per_gran)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 206, in _map_and_apply_factors
    dask_data = data.data.rechunk((tuple(rows_per_gran), data.data.chunks[1]))
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\core.py", line 2745, in rechunk
    return rechunk(self, chunks, threshold, block_size_limit, balance)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\rechunk.py", line 297, in rechunk
    chunks = normalize_chunks(
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\core.py", line 3105, in normalize_chunks
    raise ValueError(
ValueError: Chunks do not add up to shape. Got chunks=((752, -15888, -15888, -15888, 752, 768), (3200,)), shape=(752, 3200)
Could not load dataset 'DataID(name='M04', wavelength=WavelengthRange(min=0.545, central=0.555, max=0.565, unit='µm'), resolution=742, calibration=<calibration.reflectance>, modifiers=('sunz_corrected',))': Chunks do not add up to shape. Got chunks=((752, -15888, -15888, -15888, 752, 768), (3200,)), shape=(752, 3200)
Traceback (most recent call last):
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 836, in _load_dataset_with_area
    ds = self._load_dataset_data(file_handlers, dsid, **kwargs)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 736, in _load_dataset_data
    proj = self._load_dataset(dsid, ds_info, file_handlers, **kwargs)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 712, in _load_dataset
    projectable = fh.get_dataset(dsid, ds_info)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_sdr.py", line 134, in get_dataset
    data = self.scale_data_to_specified_unit(data, dataset_id, ds_info)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 191, in scale_data_to_specified_unit
    return self.scale_swath_data(data, factors, dataset_group)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 175, in scale_swath_data
    data = self._map_and_apply_factors(data, factors, rows_per_gran)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 206, in _map_and_apply_factors
    dask_data = data.data.rechunk((tuple(rows_per_gran), data.data.chunks[1]))
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\core.py", line 2745, in rechunk
    return rechunk(self, chunks, threshold, block_size_limit, balance)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\rechunk.py", line 297, in rechunk
    chunks = normalize_chunks(
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\core.py", line 3105, in normalize_chunks
    raise ValueError(
ValueError: Chunks do not add up to shape. Got chunks=((752, -15888, -15888, -15888, 752, 768), (3200,)), shape=(752, 3200)
Could not load dataset 'DataID(name='I01', wavelength=WavelengthRange(min=0.6, central=0.64, max=0.68, unit='µm'), resolution=371, calibration=<calibration.reflectance>, modifiers=('sunz_corrected_iband',))': Chunks do not add up to shape. Got chunks=((1504, -31776, -31776, -31776, 1504, 1536), (6400,)), shape=(1504, 6400)
Traceback (most recent call last):
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 836, in _load_dataset_with_area
    ds = self._load_dataset_data(file_handlers, dsid, **kwargs)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 736, in _load_dataset_data
    proj = self._load_dataset(dsid, ds_info, file_handlers, **kwargs)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\yaml_reader.py", line 712, in _load_dataset
    projectable = fh.get_dataset(dsid, ds_info)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_sdr.py", line 134, in get_dataset
    data = self.scale_data_to_specified_unit(data, dataset_id, ds_info)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 191, in scale_data_to_specified_unit
    return self.scale_swath_data(data, factors, dataset_group)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 175, in scale_swath_data
    data = self._map_and_apply_factors(data, factors, rows_per_gran)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\readers\viirs_atms_sdr_base.py", line 206, in _map_and_apply_factors
    dask_data = data.data.rechunk((tuple(rows_per_gran), data.data.chunks[1]))
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\core.py", line 2745, in rechunk
    return rechunk(self, chunks, threshold, block_size_limit, balance)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\rechunk.py", line 297, in rechunk
    chunks = normalize_chunks(
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\dask\array\core.py", line 3105, in normalize_chunks
    raise ValueError(
ValueError: Chunks do not add up to shape. Got chunks=((1504, -31776, -31776, -31776, 1504, 1536), (6400,)), shape=(1504, 6400)
The following datasets were not created and may require resampling to be generated: DataID(name='ocean_color')
Traceback (most recent call last):
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\scene.py", line 908, in _get_finalized_destination_area
    finest_area = new_scn.finest_area()
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\scene.py", line 335, in finest_area
    return self._compare_areas(datasets=datasets, compare_func=max)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\scene.py", line 266, in _compare_areas
    areas = self._gather_all_areas(datasets)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\scene.py", line 319, in _gather_all_areas
    raise ValueError("No dataset areas available")
ValueError: No dataset areas available

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Program Files\JetBrains\PyCharm2023\plugins\python\helpers\pydev\pydevd.py", line 1534, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:\Program Files\JetBrains\PyCharm2023\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "H:\study_program\AAmy_program\python\ship_rcv_donghaisuo\project_data.py", line 331, in <module>
    sat_project.project_viirs()
  File "H:\study_program\AAmy_program\python\ship_rcv_donghaisuo\project_data.py", line 70, in project_viirs
    scn_new = scn.resample(area_def,resampler='nearest')
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\scene.py", line 980, in resample
    self._resampled_scene(new_scn, destination, resampler=resampler,
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\scene.py", line 869, in _resampled_scene
    destination_area = self._get_finalized_destination_area(destination_area, new_scn)
  File "D:\myprograms\miniconda3\envs\p2g\lib\site-packages\satpy\scene.py", line 911, in _get_finalized_destination_area
    raise ValueError("No dataset areas available to freeze "
ValueError: No dataset areas available to freeze DynamicAreaDefinition.

Environment Info:

  • OS: windows11
  • Satpy Version: 0.51.0
  • PyResample Version: 1.25.1

image

@mraspaud
Copy link
Member

@haiyangdaozhang thanks for reaching out to us!
So, from the image it looks like that the swath is missing quite some lines, and that might explain why the dynamic area can't be frozen: It needs the lons/lats of the corners to compute the projection parameters.

Now why that does not happen systematically, I don't know... but maybe NaNs aren't very stable?

@djhoese
Copy link
Member

djhoese commented Sep 24, 2024

@mraspaud If you look higher up in their error traceback you'll see:

ValueError: Chunks do not add up to shape. Got chunks=((1504, -31776, -31776, -31776, 1504, 1536), (6400,)), shape=(1504, 6400)

and

ValueError: Chunks do not add up to shape. Got chunks=((752, -15888, -15888, -15888, 752, 768), (3200,)), shape=(752, 3200)

This would usually mean you're providing different amounts of geolocation versus data files, but I find it extremely odd (impossible?) to have negative chunk sizes. I'm not sure how that happened.

Otherwise, I have no idea why a breakpoint or no breakpoint would change the results in your code unless we have a bug in how we're using dask.

Other coding things to note @haiyangdaozhang:

First:

                for iproduct in compsite_name_list:

                    filename_product = filename_output.replace("needname", iproduct)
                    scn_new.save_dataset(iproduct,filename=filename_product)

Could likely have the loop removed and just do filename_product = filename_output.replace("needname", "{name}"). Satpy will fill in the {name} field with the name of the product being saved. So scn_new.save_datasets(filename=filename_product) would work and no for loop would be needed. You could even do other metadata fields in the filename like {start_time:%Y%m%d%H%M%S}.

Second:

            day_night = self.judgeDayNight(reader, ifile)
            if day_night == "night":
                continue
            else:
                filenames = self.search_file(ifile)

Could be:

            if self.judgeDayNight(reader, ifile) == "night":
                continue

            filenames = self.search_file(ifile)

That is, you don't need the else and can de-dent the entire rest of the code. This isn't a Satpy thing, but I just thought it might clean up your code a bit.

@haiyangdaozhang
Copy link
Author

Thanks for the advice! This is indeed a very strange question. Maybe the interactive run introduces something new?
If I want to use it for adaptive projection range, maybe I can read the surrounding range of the L1B hdf file and do a custom projection?

@djhoese
Copy link
Member

djhoese commented Sep 24, 2024

The error you're getting starts in the reader. The dynamic area definition stuff doesn't really matter as it is mainly complaining that no datasets were loaded so it can't resample anything or even create a "frozen" area definition.

What type of files are you providing to the Scene? Are you sure you're providing the same number of granules for I and M bands? Where did you get your files from? The negative values in the chunk sizes makes me think that the files have invalid values for some of the information in them (ex. number of scans). Are the files you're providing aggregate files with more than one granule in them? What about with more than one band (ex. SVI01 and SVI02 in the same file)?

@haiyangdaozhang
Copy link
Author

image
These are the files (self.search_file(ifile)) .
Data is received through the antenna. RAW data is processed to L0 by using RT-STPS and L1B by using CSPP_SDR_4.0

@djhoese
Copy link
Member

djhoese commented Sep 24, 2024

How many files are you passing in scn = Scene(reader=reader, filenames=filenames)? A single time/granule of files? Looks like the files span ~5 minutes so I think that's 3 or more regular granules if I remember correctly.

To further debug this may require us getting access to your files to look at the metadata and try to reproduce the issue.

@haiyangdaozhang
Copy link
Author

Thank you for your help. I will put the data into Google Drive, which will take some time. Perhaps the snr is not good enough, causing quality issues with the received data?

@djhoese
Copy link
Member

djhoese commented Sep 24, 2024

That may play a part later, but unless I'm missing something the errors are purely talking about the shape of the arrays in the files. Satpy takes various parameters from the metadata like number of rows per instrument scan and number of scans in the file and "chunks" the data into separate pieces. These separate chunks are then processed in parallel (using dask). For some reason the chunks being provided to dask from the VIIRS SDR reader include negative numbers. Again, these chunk sizes are computed from the size of the arrays and the metadata from the file. For them to be negative likely means that the metadata is invalid or that the reader in Satpy needs to be updated to handle "fill values" for some of these metadata items. For example, if number of scans is set to a fill value of -999 that might explain how the rest of the math done in the reader is producing such and odd chunk size.

@haiyangdaozhang
Copy link
Author

These are the data I used. Thank you again!
https://drive.google.com/drive/folders/1-o9cpATGRM17LNLFq7Uw_QCVfutw0xWj?usp=sharing

@djhoese
Copy link
Member

djhoese commented Sep 25, 2024

Thanks for the files. It took a bit for google to let me download all of them, but the following code worked for me on a PopOS Linux system with Python 3.12 and latest Satpy:

from satpy import Scene; from glob import glob

scn = Scene(reader="viirs_sdr", filenames=glob("viirs_l1b_test/*.h5"))
scn.load(["ocean_color"])

projection = {'proj': 'latlong'}
area_def = create_area_def(area_id='User_define', projection=projection, resolution=0.02, units='degrees')

scn_new = scn.resample(area_def, resampler="nearest")
scn_new.save_datasets()

@haiyangdaozhang
Copy link
Author

Thank you! I created Python 3.12 on Rocky, and it's working fine!

@djhoese
Copy link
Member

djhoese commented Sep 26, 2024

Just curious, you said you were on Windows before, what version of Python were you running?

@haiyangdaozhang
Copy link
Author

Windows 11 & Python 3.9.13.
But dask version is very old. The software source image I used for installation did not have a high enough version. I guess it was a problem during installation. Sorry for taking up so much of your time...
dask version:
dask 2022.11.0
dask-core 2022.11.0

@djhoese
Copy link
Member

djhoese commented Sep 27, 2024

Hm good find. Yeah seeing as the error was about dask array chunk sizes, it seems reasonable that the old version of dask is causing the issue. I'll close this as it seems like we figured it out. We can reopen if you would like.

@djhoese djhoese closed this as completed Sep 27, 2024
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

3 participants