This is an automated email from the ASF dual-hosted git repository. tloubrieu pushed a commit to branch ascending_latitudes in repository https://gitbox.apache.org/repos/asf/incubator-sdap-ingester.git
commit e13d7ccd20f6721b2fd53b25acfa6db4d2a060c1 Author: thomas loubrieu <[email protected]> AuthorDate: Wed Apr 7 22:36:43 2021 -0700 add processor to make latitude ascending --- .../processors/ForceAscendingLatitude.py | 48 ++++++++++++++++++ .../processors/test_ForceAscendingLatitude.py | 58 ++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/granule_ingester/granule_ingester/processors/ForceAscendingLatitude.py b/granule_ingester/granule_ingester/processors/ForceAscendingLatitude.py new file mode 100644 index 0000000..01b2e88 --- /dev/null +++ b/granule_ingester/granule_ingester/processors/ForceAscendingLatitude.py @@ -0,0 +1,48 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import numpy as np + +from nexusproto.serialization import from_shaped_array, to_shaped_array +from granule_ingester.processors.TileProcessor import TileProcessor + +class ForceAscendingLatitude(TileProcessor): + + def process(self, tile, *args, **kwargs): + """ + This method will reverse the ordering of latitude values in a tile if necessary to ensure that the latitude values are ascending. + + :param self: + :param tile: The nexus_tile + :return: Tile data with altered latitude values + """ + + the_tile_type = tile.tile.WhichOneof("tile_type") + + the_tile_data = getattr(tile.tile, the_tile_type) + + latitudes = from_shaped_array(the_tile_data.latitude) + + data = from_shaped_array(the_tile_data.variable_data) + + # Only reverse latitude ordering if current ordering is descending. + if len(latitudes) > 1: + delta = latitudes[1] - latitudes[0] + if delta < 0: + latitudes = np.flip(latitudes) + data = np.flip(data, axis=0) + the_tile_data.latitude.CopyFrom(to_shaped_array(latitudes)) + the_tile_data.variable_data.CopyFrom(to_shaped_array(data)) + + return tile diff --git a/granule_ingester/tests/processors/test_ForceAscendingLatitude.py b/granule_ingester/tests/processors/test_ForceAscendingLatitude.py new file mode 100644 index 0000000..d593909 --- /dev/null +++ b/granule_ingester/tests/processors/test_ForceAscendingLatitude.py @@ -0,0 +1,58 @@ +import unittest + +import xarray as xr +import numpy as np +from os import path +from nexusproto import DataTile_pb2 as nexusproto + +from nexusproto.serialization import from_shaped_array, to_shaped_array + +from granule_ingester.processors.ForceAscendingLatitude import ForceAscendingLatitude +from granule_ingester.processors.reading_processors.GridReadingProcessor import GridReadingProcessor + +class TestForceAscendingLatitude(unittest.TestCase): + + def read_tile(self): + reading_processor = GridReadingProcessor('B03', 'lat', 'lon', time='time') + granule_path = path.join(path.dirname(__file__), + '/Users/loubrieu/Documents/sdap/HLS/HLS.S30.T11SPC.2020001.v1.4.hdf.nc') + input_tile = nexusproto.NexusTile() + input_tile.summary.granule = granule_path + + dimensions_to_slices = { + 'time': slice(0, 1), + 'lat': slice(0, 30), + 'lon': slice(0, 30) + } + + with xr.open_dataset(granule_path) as ds: + return reading_processor._generate_tile(ds, dimensions_to_slices, input_tile) + + def test_process(self): + processor = ForceAscendingLatitude() + + tile = self.read_tile() + + tile_type = tile.tile.WhichOneof("tile_type") + tile_data = getattr(tile.tile, tile_type) + latitudes = from_shaped_array(tile_data.latitude) + variable_data = from_shaped_array(tile_data.variable_data) + print(latitudes) + print(variable_data) + + + flipped_tile = processor.process(tile) + + the_flipped_tile_type = flipped_tile.tile.WhichOneof("tile_type") + the_flipped_tile_data = getattr(flipped_tile.tile, the_flipped_tile_type) + + flipped_latitudes = from_shaped_array(the_flipped_tile_data.latitude) + flipped_data = from_shaped_array(the_flipped_tile_data.variable_data) + + print(flipped_latitudes[1]) + np.testing.assert_almost_equal(flipped_latitudes[1], 38.72608, decimal=5, err_msg='', verbose=True) + print(flipped_data[1,1]) + np.testing.assert_almost_equal(flipped_data[1,1], 0.3116, decimal=4, err_msg='', verbose=True) + + +
