From a2660b1adbb3336423f3b0c8e568407b98bf4aa9 Mon Sep 17 00:00:00 2001
From: Greg Quinn <greg.quinn@ssec.wisc.edu>
Date: Mon, 30 Oct 2017 12:11:35 -0500
Subject: [PATCH] rdrgen: add RATMS support

---
 edosl0util/rdrgen.py | 24 +++++++++++++++++++++---
 tests/test_rdrgen.py | 17 ++++++++++++++---
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/edosl0util/rdrgen.py b/edosl0util/rdrgen.py
index 0092776..0ae6215 100644
--- a/edosl0util/rdrgen.py
+++ b/edosl0util/rdrgen.py
@@ -25,7 +25,7 @@ def packets_to_rdrs(sat, l0_files, **kwargs):
     build_rdr(sat, iter_pkts(l0_files), **kwargs)
 
 
-def build_rdr(sat, pkt_iter, aggr_level=None, diary_cushion=10000000, attr_overrides={}):
+def build_rdr(sat, pkt_iter, output_dir='.', aggr_level=None, diary_cushion=10000000, attr_overrides={}):
 
     # divy packets up into temp files organized by granule
     file_mgr = BinnedTemporaryFileManager()
@@ -51,7 +51,8 @@ def build_rdr(sat, pkt_iter, aggr_level=None, diary_cushion=10000000, attr_overr
     # now generate the RDRs
     rdr_files = []
     for aggr_iet in primary_aggr_iets:
-        rdr_writer = RdrWriter(sat, rdr_types, aggr_iet, aggr_level, **attr_overrides)
+        rdr_writer = RdrWriter(sat, rdr_types, aggr_iet, aggr_level, output_dir,
+                               **attr_overrides)
         rdr_writer.write_aggregate(primary_type, aggr_iet, aggr_level)
         gran_iets = get_aggregate_granule_times(
             sat, primary_type.gran_len, aggr_level, aggr_iet)
@@ -73,6 +74,7 @@ def build_rdr(sat, pkt_iter, aggr_level=None, diary_cushion=10000000, attr_overr
                 rdr_writer.write_granule(packaged_type, gran_iet, blob)
         rdr_writer.close()
         rdr_files.append(rdr_writer.file_name)
+    file_mgr.clean_up()
 
     return rdr_files
 
@@ -500,6 +502,21 @@ class CrisScienceRdrType(object):
     default_aggregation = 15
 
 
+@rdr_type_spec
+class AtmsScienceRdrType(object):
+    product_id = 'RATMS'
+    short_name = 'ATMS-SCIENCE-RDR'
+    gran_len = 31997000
+    sensor = 'atms'
+    type_id = 'SCIENCE'
+    document = '474-00448-02-02_JPSS-DD-Vol-II-Part-2_0200B.pdf'
+    apids = [ApidSpec(515, 'CAL', max_expected=5),
+             ApidSpec(528, 'SCI', max_expected=1249),
+             ApidSpec(530, 'ENG_TEMP', max_expected=13),
+             ApidSpec(531, 'ENG_HS', max_expected=5)]
+    default_aggregation = 15
+
+
 @rdr_type_spec
 class SpacecraftDiaryRdrType(object):
     product_id = 'RNSCA'
@@ -667,4 +684,5 @@ def get_overlapping_granules(sat, gran_len, start_iet, stop_iet):
 iet_epoch = Time('1958-01-01', scale='tai')
 satellite_base_times = {'snpp': 1698019234000000}
 platform_short_names = {'snpp': 'NPP'}
-instrument_short_names = {'viirs': 'VIIRS', 'cris': 'CrIS', None: 'SPACECRAFT'}
+instrument_short_names = {'viirs': 'VIIRS', 'cris': 'CrIS', 'atms': 'ATMS',
+                          None: 'SPACECRAFT'}
diff --git a/tests/test_rdrgen.py b/tests/test_rdrgen.py
index 4dc0b2b..8ee1c3a 100644
--- a/tests/test_rdrgen.py
+++ b/tests/test_rdrgen.py
@@ -34,10 +34,16 @@ def generate_rdr_blob_packets(blob):
     return jpss_packet_stream(BytesIO(pkt_buf))
 
 
-def test_can_reproduce_cris_rdr():
-    orig_file_name = 'RCRIS-RNSCA_npp_d20171008_t0004096_e0012095_b30810_c20171008061237136301_nobu_ops.h5'
+def test_can_reproduce_cris_rdr(tmpdir):
+    verify_rdr_reproduction(
+        'RCRIS-RNSCA_npp_d20171008_t0004096_e0012095_b30810_c20171008061237136301_nobu_ops.h5',
+        str(tmpdir))
+
+
+def verify_rdr_reproduction(orig_file_name, tmp_dir):
     orig_file = os.path.join(os.path.dirname(__file__), orig_file_name)
-    new_file, = m.build_rdr('snpp', generate_rdr_packets(orig_file))
+    new_file, = m.build_rdr('snpp', generate_rdr_packets(orig_file), output_dir=tmp_dir)
+    new_file = os.path.join(tmp_dir, new_file)
     with h5py.File(orig_file, 'r') as orig_h5, h5py.File(new_file, 'a') as new_h5:
         root_attrs = ['Distributor', 'N_Dataset_Source', 'N_HDF_Creation_Date',
                       'N_HDF_Creation_Time']
@@ -59,6 +65,11 @@ def test_can_reproduce_cris_rdr():
                 copy_attrs(
                     lambda h5: h5['Data_Products'][coll][coll + '_Gran_' + str(gran_idx)],
                     gran_attrs)
+    p = subprocess.Popen(
+        ['h5diff', '-c', '-p', '1e-5', orig_file, new_file],
+        stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    assert p.communicate()[0] == ''
+    assert p.returncode == 0
 
 
 
-- 
GitLab