Skip to content

Commit

Permalink
Merge pull request #645 from TAlonglong/feature-sar-ocn-l2
Browse files Browse the repository at this point in the history
Reader for the SAR OCN L2 wind product in SAFE format.
  • Loading branch information
mraspaud authored Apr 9, 2019
2 parents 1e9f483 + a99e1ca commit 6cabadd
Show file tree
Hide file tree
Showing 4 changed files with 430 additions and 1 deletion.
169 changes: 169 additions & 0 deletions satpy/etc/readers/safe_sar_l2_ocn.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
reader:
description: SAFE Reader for SAR L2 OCN data
name: safe_sar_l2_ocn
sensors: [sar-c]
default_channels: []
reader: !!python/name:satpy.readers.yaml_reader.FileYAMLReader

file_types:
safe_measurement:
file_reader: !!python/name:satpy.readers.safe_sar_l2_ocn.SAFENC
file_patterns: ['{fmission_id:3s}_{fsar_mode:2s}_{fproduct_type:3s}{fresolution:1s}_{fprocessing_level:1s}{fproduct_class:1s}{fpolarization:2s}_{fstart_time:%Y%m%dT%H%M%S}_{fend_time:%Y%m%dT%H%M%S}_{forbit_number:6d}_{fmission_data_take_id:6s}_{fproduct_unique_id:4s}.SAFE/measurement/{mission_id:3s}-{swath_id:2s}-{product_type:3s}-{polarization:2s}-{start_time:%Y%m%dt%H%M%S}-{end_time:%Y%m%dt%H%M%S}-{orbit_number:6d}-{mission_data_take_id:6s}-{image_number:3s}.nc']

datasets:
owiLat:
name: owiLat
file_type: safe_measurement
standard_name: latitude
units: degree

owiLon:
name: owiLon
file_type: safe_measurement
standard_name: longitude
units: degree

owiWindDirection:
name: owiWindDirection
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: degree

owiWindSpeed:
name: owiWindSpeed
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: m s-1

owiEcmwfWindDirection:
name: owiEcmwfWindDirection
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: degree

owiEcmwfWindSpeed:
name: owiEcmwfWindSpeed
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: m s-1

owiHs:
name: owiHs
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: m

owiWl:
name: owiWl
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: m

owiDirmet:
name: owiDirmet
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: degrees

owiWindSeaHs:
name: owiWindSeaHs
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: m

owiIncidenceAngle:
name: owiIncidenceAngle
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: degrees

owiElevationAngle:
name: owiElevationAngle
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: degrees

owiNrcs:
name: owiNrcs
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: m2/m2

owiNesz:
name: owiNesz
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: m2/m2

owiNrcsNeszCorr:
name: owiNrcsNeszCorr
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: m2/m2

owiPolarisationName:
name: owiPolarisationName
sensor: sar-c
file_type: safe_measurement

owiPBright:
name: owiPBright
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: '%'

owiNrcsCmod:
name: owiNrcsCmod
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: m2/m2

owiCalConstObsi:
name: owiCalConstObsi
sensor: sar-c
file_type: safe_measurement

owiCalConstInci:
name: owiCalConstInci
sensor: sar-c
file_type: safe_measurement

owiInversionQuality:
name: owiInversionQuality
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]

owiMask:
name: owiMask
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]

owiHeading:
name: owiHeading
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
units: degrees

owiWindQuality:
name: owiWindQuality
sensor: sar-c
file_type: safe_measurement
coordinates: [owiLon, owiLat]
133 changes: 133 additions & 0 deletions satpy/readers/safe_sar_l2_ocn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2019 Pytroll developers

# Author(s):

# Trygve Aspenes <trygveas@met.no>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""SAFE SAR L2 OCN format reader
The OCN data contains various parameters, but mainly the wind speed and direction
calculated from SAR data and input model data from ECMWF
Implemented in this reader is the OWI, Ocean Wind field.
See more at ESA webpage https://sentinel.esa.int/web/sentinel/ocean-wind-field-component
"""

import logging

from satpy.readers.file_handlers import BaseFileHandler
from satpy import CHUNK_SIZE

import numpy as np
import xarray as xr

logger = logging.getLogger(__name__)


class SAFENC(BaseFileHandler):
"""Measurement file reader."""

def __init__(self, filename, filename_info, filetype_info):
super(SAFENC, self).__init__(filename, filename_info,
filetype_info)

self._start_time = filename_info['start_time']
self._end_time = filename_info['end_time']
# For some SAFE packages, fstart_time differs, but start_time is the same
# To avoid over writing exiting file with same start_time, a solution is to
# use fstart_time
self._fstart_time = filename_info['fstart_time']
self._fend_time = filename_info['fend_time']

self._polarization = filename_info['polarization']

self.lats = None
self.lons = None
self._shape = None
self.area = None

self.nc = xr.open_dataset(filename,
decode_cf=True,
mask_and_scale=False,
chunks={'owiAzSize': CHUNK_SIZE,
'owiRaSize': CHUNK_SIZE})
self.nc = self.nc.rename({'owiAzSize': 'y'})
self.nc = self.nc.rename({'owiRaSize': 'x'})
self.filename = filename

def get_dataset(self, key, info):
"""Load a dataset."""
if key.name in ['owiLat', 'owiLon']:
if self.lons is None or self.lats is None:
self.lons = self.nc['owiLon']
self.lats = self.nc['owiLat']
if key.name == 'owiLat':
res = self.lats
else:
res = self.lons
res.attrs = info
else:
res = self.nc[key.name]
if key.name in ['owiHs', 'owiWl', 'owiDirmet']:
res = xr.DataArray(res, dims=['y', 'x', 'oswPartitions'])
elif key.name in ['owiNrcs', 'owiNesz', 'owiNrcsNeszCorr']:
res = xr.DataArray(res, dims=['y', 'x', 'oswPolarisation'])
elif key.name in ['owiPolarisationName']:
res = xr.DataArray(res, dims=['owiPolarisation'])
elif key.name in ['owiCalConstObsi', 'owiCalConstInci']:
res = xr.DataArray(res, dims=['owiIncSize'])
elif key.name.startswith('owi'):
res = xr.DataArray(res, dims=['y', 'x'])
else:
res = xr.DataArray(res, dims=['y', 'x'])
res.attrs.update(info)
if '_FillValue' in res.attrs:
res = res.where(res != res.attrs['_FillValue'])
res.attrs['_FillValue'] = np.nan

if 'missionName' in self.nc.attrs:
res.attrs.update({'platform_name': self.nc.attrs['missionName']})

res.attrs.update({'fstart_time': self._fstart_time})
res.attrs.update({'fend_time': self._fend_time})

if not self._shape:
self._shape = res.shape

return res

@property
def start_time(self):
"""Product start_time, parsed from the measurement file name."""
return self._start_time

@property
def end_time(self):
"""Product end_time, parsed from the measurement file name."""
return self._end_time

@property
def fstart_time(self):
"""Product fstart_time meaning the start time parsed from the SAFE directory."""
return self._fstart_time

@property
def fend_time(self):
"""Product fend_time meaning the end time parsed from the SAFE directory."""
return self._fend_time
4 changes: 3 additions & 1 deletion satpy/tests/reader_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
test_nc_slstr, test_olci_nc,
test_viirs_edr_flood, test_nwcsaf_nc,
test_seviri_l1b_hrit, test_sar_c_safe,
test_modis_l1b, test_viirs_edr_active_fires)
test_modis_l1b, test_viirs_edr_active_fires,
test_safe_sar_l2_ocn)


if sys.version_info < (2, 7):
Expand Down Expand Up @@ -84,5 +85,6 @@ def suite():
mysuite.addTests(test_sar_c_safe.suite())
mysuite.addTests(test_modis_l1b.suite())
mysuite.addTests(test_viirs_edr_active_fires.suite())
mysuite.addTests(test_safe_sar_l2_ocn.suite())

return mysuite
Loading

0 comments on commit 6cabadd

Please sign in to comment.