pytroll / satpy

Python package for earth-observing satellite data processing
http://satpy.readthedocs.org/en/latest/
GNU General Public License v3.0
1.06k stars 293 forks source link

Question (maybe a bug): Why does RBG array exported with scn.save_dataset contain values greater than 255 ? #710

Closed isobelrlawrence closed 5 years ago

isobelrlawrence commented 5 years ago

Hello,

When I export my 'true_color' satellite image as a netcdf file containing RGB values of each pixel I end up with individual values for R,G or B greater than 255. Surely output RGB values should be between 0 and 255 or 0 and 1. Perhaps there is a scaling factor missing from the netcdf header?

Here is my code:

from satpy.scene import Scene
from satpy import find_files_and_readers
from datetime import datetime
from netCDF4 import Dataset
import numpy as np

files = find_files_and_readers(sensor='olci', 
              start_time=datetime(2019, 3, 15, 4, 28),
              end_time=datetime(2019, 3, 15, 4, 30),
              base_dir="/Users/il/Downloads/",
              reader='olci_l1b')

scn = Scene(filenames=files)
scn.load(['true_color'])
scn.save_dataset('true_color', writer='cf', filename='true_color.nc')

OLCI_data=Dataset('true_color.nc')
OLCI_z=np.asarray(OLCI_data['true_color'])
print(np.nanmax(OLCI_z))

Returns: 351.80767121471672

Here is the netcdf header:

float64 true_color(bands, y, x)
    _FillValue: nan
    platform_name: Sentinel-3A
    sensor: olci
    ancillary_variables: []
    optional_datasets: []
    standard_name: true_color
    prerequisites: ["DatasetID(name='Oa08', wavelength=None, resolution=None, polarization=None, calibration=None, level=None, modifiers=('effective_solar_pathlength_corrected', 'rayleigh_corrected'))", "DatasetID(name='Oa06', wavelength=None, resolution=None, polarization=None, calibration=None, level=None, modifiers=('effective_solar_pathlength_corrected', 'rayleigh_corrected'))", "DatasetID(name='Oa03', wavelength=None, resolution=None, polarization=None, calibration=None, level=None, modifiers=('effective_solar_pathlength_corrected', 'rayleigh_corrected'))"]
    optional_prerequisites: []
    mode: RGB
    coordinates: longitude latitude
    long_name: true_color
unlimited dimensions: 
current shape = (3, 4091, 4865)
filling on

Many thanks,

mraspaud commented 5 years ago

Hi,

Thanks for reporting this. Saving an RGB image to netcdf will not apply the enhancements, so the data you have then are the untouched physical values read from the files, in this case reflectances. In turn these are more that 100% sometimes because of sun zenith angle normalisation for example. Usually for RGB, we use GeoTiffs or other image formats here, but do you plan to visualise RGBs in netcdf in your case ?

isobelrlawrence commented 5 years ago

Hi,

Yes that was my intention - I wanted to avoid working with GeoTiffs mainly just because I never have before and thought this could be a quicker way of doing things. Looks like I might have to though? Perhaps it ought to be changed in the output netcdf so it doesn't say 'R', 'G', 'B' in the 'bands' parameter as this is a little misleading.

Many thanks for responding so promptly! Isobel

mraspaud commented 5 years ago

Hi,

At the moment, having enhanced RGBs (stretched to 0-255) isn't implemented for netcdf saving, but it shouldn't be to difficult to implement. If you feel like you want to give it a try, be sure to join us on the pytroll slack if you need some guidance (see pytroll.org to find out how to connect to slack).

isobelrlawrence commented 5 years ago

Thank you!