Repository: brandon-rhodes/python-sgp4

Issue:
export_tle roundtrip issues
While studying #70, I discovered a roundtrip problem with `export_tle`:

```
>>> lines = """1 46272U 20061A   20311.82207422  .00000083  00000-0  68546-5 0  9999
... 2 46272  97.3995  23.7575 0006827 352.5560   7.5569 15.20743438  9810"""
>>> print(lines)
1 46272U 20061A   20311.82207422  .00000083  00000-0  68546-5 0  9999
2 46272  97.3995  23.7575 0006827 352.5560   7.5569 15.20743438  9810
>>> sat = twoline2rv(*lines.splitlines(), wgs84)
>>> print("\n".join(export_tle(sat)))
1 46272U 20061A   2020311.82207422  .00000083  00000-0  68546-5 0  9993
2 46272  97.3995  23.7575 0006827 352.5560   7.5569 15.20743438  9810
```

And I think it also happens with other examples in https://github.com/brandon-rhodes/python-sgp4/blob/master/sgp4/SGP4-VER.TLE.

Discussion:

Patch:
diff --git a/sgp4/exporter.py b/sgp4/exporter.py
index 759c034..32d6baf 100644
--- a/sgp4/exporter.py
+++ b/sgp4/exporter.py
@@ -30,7 +30,10 @@ def export_tle(satrec):
     append(satrec.intldesg.ljust(8, " ") + " ")
 
     # Add epoch year and days in YYDDD.DDDDDDDD format
-    append(str(satrec.epochyr).zfill(2) + "{:012.8f}".format(satrec.epochdays) + " ")
+    epochyr = satrec.epochyr
+    # Undo non-standard 4-digit year for old satrec objects
+    epochyr %= 100
+    append(str(epochyr).zfill(2) + "{:012.8f}".format(satrec.epochdays) + " ")
 
     # Add First Time Derivative of the Mean Motion (don't use "+")
     append("{0: 8.8f}".format(satrec.ndot * (_xpdotp * 1440.0)).replace("0", "", 1) + " ")


Test patch:
diff --git a/sgp4/tests.py b/sgp4/tests.py
index ba63b1d..3fb6d41 100644
--- a/sgp4/tests.py
+++ b/sgp4/tests.py
@@ -198,14 +198,18 @@ def test_tle_export():
         line1 = line1[:69]
         line2 = line2[:69]
         satrec = Satrec.twoline2rv(line1, line2)
+        satrec_old = io.twoline2rv(line1, line2, wgs72)
 
         # Generate TLE from satrec
         out_line1, out_line2 = export_tle(satrec)
+        out_line1_old, out_line2_old = export_tle(satrec_old)
 
         if satrec.satnum not in expected_errs_line1:
             assertEqual(out_line1, line1)
+            assertEqual(out_line1_old, line1)
         if satrec.satnum not in expected_errs_line2:
             assertEqual(out_line2, line2)
+            assertEqual(out_line2_old, line2)
 
 def test_export_tle_raises_error_for_out_of_range_angles():
     # See https://github.com/brandon-rhodes/python-sgp4/issues/70