From b222c8ad97f592bdcc0a5a08503e321b1663f72d Mon Sep 17 00:00:00 2001
From: jlaura <jlaura@usgs.gov>
Date: Fri, 12 Nov 2021 09:19:20 -0700
Subject: [PATCH] Fixes 126 and PVL grammar issue (#167)

* Fixes 126 and PVL grammar issue

* Adds changelog.md

* Updates meta.yml for pvl version
---
 CHANGELOG.md                            | 39 +++++++++++++++++++++++++
 environment.yml                         |  2 +-
 plio/io/io_controlnetwork.py            |  6 +++-
 plio/io/tests/test_io_controlnetwork.py | 17 ++++++++---
 recipe/meta.yaml                        |  4 +--
 5 files changed, 60 insertions(+), 8 deletions(-)
 create mode 100644 CHANGELOG.md

diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..be70a7f
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,39 @@
+# Changelog
+
+All changes that impact users of this project will be documented in this file.
+
+The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
+and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+
+<!---
+This document is intended for users of the applications and API. Changes to things
+like tests should not be noted in this document.
+
+When updating this file for a PR, add an entry for your change under Unreleased
+and one of the following headings:
+ - Added - for new features.
+ - Changed - for changes in existing functionality.
+ - Deprecated - for soon-to-be removed features.
+ - Removed - for now removed features.
+ - Fixed - for any bug fixes.
+ - Security - in case of vulnerabilities.
+
+If the heading does not yet exist under Unreleased, then add it as a 3rd heading,
+with three #.
+
+
+When preparing for a public release candidate add a new 2nd heading, with two #, under
+Unreleased with the version number and the release date, in year-month-day
+format. Then, add a link for the new version at the bottom of this document and
+update the Unreleased link so that it compares against the latest release tag.
+
+
+When preparing for a bug fix release create a new 2nd heading above the Fixed
+heading to indicate that only the bug fixes and security fixes are in the bug fix
+release.
+-->
+## [Unreleased]
+
+### Added
+- Added this CHANGELOG.md file to track changes to the library
+- Added a warning when to_isis is called without a target name fixing [#126](https://github.com/USGS-Astrogeology/plio/issues/126).
diff --git a/environment.yml b/environment.yml
index f0394f4..6513c5f 100644
--- a/environment.yml
+++ b/environment.yml
@@ -8,7 +8,7 @@ dependencies:
   - numpy 
   - pyproj 
   - h5py 
-  - pvl >= 1.0 
+  - pvl >= 1.3.0
   - scipy 
   - protobuf 
   - affine 
diff --git a/plio/io/io_controlnetwork.py b/plio/io/io_controlnetwork.py
index 75196b5..3649d66 100644
--- a/plio/io/io_controlnetwork.py
+++ b/plio/io/io_controlnetwork.py
@@ -122,6 +122,10 @@ def to_isis(obj, path, mode='wb', version=2,
             description='None', username=DEFAULTUSERNAME,
             creation_date=None, modified_date=None,
             pointid_prefix=None, pointid_suffix=None):
+
+    if targetname == 'None':
+        warnings.warn("Users should provide a targetname to this function such as 'Moon' or 'Mars' in order to generate a valid ISIS control network.")
+
     with IsisStore(path, mode) as store:
         if not creation_date:
             creation_date = strftime("%Y-%m-%d %H:%M:%S", gmtime())
@@ -220,7 +224,7 @@ class IsisStore(object):
         Given an ISIS store, read the underlying ISIS3 compatible control network and
         return an IsisControlNetwork dataframe.
         """
-        pvl_header = pvl.load(self._path)
+        pvl_header = pvl.load(self._path, grammar=pvl.grammar.ISISGrammar())
         header_start_byte = find_in_dict(pvl_header, 'HeaderStartByte')
         header_bytes = find_in_dict(pvl_header, 'HeaderBytes')
         point_start_byte = find_in_dict(pvl_header, 'PointsStartByte')
diff --git a/plio/io/tests/test_io_controlnetwork.py b/plio/io/tests/test_io_controlnetwork.py
index 134206a..094c54d 100644
--- a/plio/io/tests/test_io_controlnetwork.py
+++ b/plio/io/tests/test_io_controlnetwork.py
@@ -56,6 +56,10 @@ def test_log_error():
     with pytest.raises(TypeError) as err:
         io_controlnetwork.MeasureLog(2, 'foo')
 
+def test_to_isis_wo_targetname(cnet_dataframe, tmpdir):
+    with pytest.warns(UserWarning, match="Users should provide a targetname"):
+        io_controlnetwork.to_isis(cnet_dataframe, tmpdir.join('test.net'), mode='wb', targetname='None') # 'None' is the default.  
+
 def test_to_protobuf():
     value = 1.25
     int_dtype = 2
@@ -87,7 +91,7 @@ def cnet_dataframe(tmpdir):
     
     df.creation_date = strftime("%Y-%m-%d %H:%M:%S", gmtime())
     df.modified_date = strftime("%Y-%m-%d %H:%M:%S", gmtime())
-    io_controlnetwork.to_isis(df, tmpdir.join('test.net'), mode='wb', targetname='Moon')
+    #io_controlnetwork.to_isis(df, tmpdir.join('test.net'), mode='wb', targetname='Moon')
 
     df.header_message_size = 78
     df.point_start_byte = 65614 # 66949
@@ -97,6 +101,8 @@ def cnet_dataframe(tmpdir):
     return df 
 
 def test_create_buffer_header(cnet_dataframe, tmpdir):
+    # Write the cnet
+    io_controlnetwork.to_isis(cnet_dataframe, tmpdir.join('test.net'), mode='wb', targetname='Moon')
     with open(tmpdir.join('test.net'), 'rb') as f:
         
         f.seek(io_controlnetwork.HEADERSTARTBYTE)
@@ -114,6 +120,8 @@ def test_create_buffer_header(cnet_dataframe, tmpdir):
         assert cnet_dataframe.measure_size == header_protocol.pointMessageSizes
 
 def test_create_point(cnet_dataframe, tmpdir):
+    # Write the cnet
+    io_controlnetwork.to_isis(cnet_dataframe, tmpdir.join('test.net'), mode='wb', targetname='Moon')
     with open(tmpdir.join('test.net'), 'rb') as f:
         f.seek(cnet_dataframe.point_start_byte)
         for i, length in enumerate(cnet_dataframe.measure_size):
@@ -148,9 +156,10 @@ def test_create_point_wo_reference_index(cnet_dataframe, tmpdir):
     assert (test_cnet.referenceIndex == reference_idx).all()
 
 def test_create_pvl_header(cnet_dataframe, tmpdir):
-    with open(tmpdir.join('test.net'), 'rb') as f:
-        pvl_header = pvl.load(f)
-
+    # Write the cnet
+    io_controlnetwork.to_isis(cnet_dataframe, tmpdir.join('test.net'), mode='wb', targetname='Moon')
+    with open(tmpdir.join('test.net'), 'rb') as stream:
+        pvl_header = pvl.load(stream, grammar=pvl.grammar.ISISGrammar())
     npoints = find_in_dict(pvl_header, 'NumberOfPoints')
     assert 5 == npoints
 
diff --git a/recipe/meta.yaml b/recipe/meta.yaml
index f4e2245..63a32af 100644
--- a/recipe/meta.yaml
+++ b/recipe/meta.yaml
@@ -20,7 +20,7 @@ requirements:
     - python
     - setuptools
     - numpy
-    - pvl
+    - pvl >= 1.3.0
     - protobuf
     - gdal
     - icu
@@ -36,7 +36,7 @@ requirements:
     - python
     - setuptools
     - numpy
-    - pvl >= 1.0
+    - pvl >= 1.3.0
     - protobuf
     - gdal
     - icu
-- 
GitLab