fgreg commented on a change in pull request #10: SDAP-155 add processor to 
extract timestamp from granule metadata
URL: 
https://github.com/apache/incubator-sdap-ningesterpy/pull/10#discussion_r225267137
 
 

 ##########
 File path: sdap/processors/extracttimestampprocessor.py
 ##########
 @@ -0,0 +1,63 @@
+# 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 nexusproto
+from nexusproto.serialization import from_shaped_array
+
+import datetime
+import time
+import logging
+from netCDF4 import Dataset, num2date
+from pytz import timezone
+
+from sdap.processors import NexusTileProcessor
+
+EPOCH = timezone('UTC').localize(datetime.datetime(1970, 1, 1))
+
+
+def to_seconds_from_epoch(timestamp, pattern='%Y-%m-%dT%H:%M:%S'):
+    try:
+        seconds = int(time.mktime(time.strptime(timestamp[:19], pattern)))
+    except ValueError:
+        logging.logger.error(timestamp + ' timestamp is not of the format 
\'%Y-%m-%dT%H:%M:%S\'')
+    return seconds
+
+class ExtractTimestampProcessor(NexusTileProcessor):
+
+    def __init__(self, attribute_name, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+
+        self.stored_var_name = self.environ['STORED_VAR_NAME']
+        self.attribute_name = attribute_name
+
+    def process_nexus_tile(self, nexus_tile):
+        output_tile = nexusproto.DataTile_pb2.NexusTile()
+        output_tile.CopyFrom(nexus_tile)
+
+        file_path = output_tile.summary.granule
+        file_path = file_path[len('file:'):] if file_path.startswith('file:') 
else file_path
+
+        dimtoslice = {}
+        for dimension in output_tile.summary.section_spec.split(','):
+            name, start, stop = dimension.split(':')
+            dimtoslice[name] = slice(int(start), int(stop))
+
+        with Dataset(file_path) as ds:
+            # MODIS: time_coverage_start
+            timestamp = getattr(ds,self.attribute_name)
+            seconds = to_seconds_from_epoch(timestamp)
+            nexus_tile.tile.grid_tile.time = seconds
 
 Review comment:
   Not every `nexus_tile.tile` has a `grid_tile`. It can be one of `grid_tile`, 
`swath_tile`, or `time_series_tile` 
   
   
https://github.com/apache/incubator-sdap-nexusproto/blob/master/src/main/proto/DataTile.proto#L122
   
   For this processor it makes sense that it only applies to `grid_tile` types, 
so I would make this explicit by throwing an exception if the tile passed in is 
not a `grid_tile` (this could also be a test case in your unit test).
   
   In this case I think if `grid_tile` does not exist (which it won't for 
non-grid_tile tiles), `nexus_tile.tile.grid_tile.time` will throw 
`AttributeError`. So you could try/catch just that one line and add a more 
meaningful error message about how this processor only supports grid tiles.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to