Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions ale/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,9 @@ def read_geodata(self):
from osgeo import gdal
gdal.UseExceptions()
except:
self._projection = ""
return self._projection
self._geodata = None
return self._geodata

self._geodata = None
if isinstance(self._file, pvl.PVLModule):
# save it to a temp folder
with tempfile.NamedTemporaryFile() as tmp:
Expand Down
119 changes: 95 additions & 24 deletions ale/base/label_isis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pvl
import json
import datetime

class IsisLabel():
"""
Expand All @@ -21,16 +23,44 @@ def label(self):
Raised when an invalid label is provided.
"""
if not hasattr(self, "_label"):
if isinstance(self._file, pvl.PVLModule):
if isinstance(self._file, pvl.PVLModule) or isinstance(self._file, dict):
self._label = self._file
return self._label
else:
self._label = None

grammar = pvl.grammar.ISISGrammar()
grammar.comments+=(("#", "\n"), )

try:
self._label = pvl.loads(self._file, grammar=grammar)
except Exception:
self._label = pvl.load(self._file, grammar=grammar)
except:
pass

if not self._label:
try:
self._label = pvl.load(self._file, grammar=grammar)
except:
pass

if not self._label:
try:
self._label = json.loads(self._file)
except:
pass

if not self._label:
try:
from osgeo import gdal
gdal.UseExceptions()
geodata = gdal.Open(self._file)
self._label = json.loads(geodata.GetMetadata("json:ISIS3")[0])
except:
pass

if not self._label:
raise ValueError("{} is not a valid label".format(self._file))

return self._label

@property
Expand Down Expand Up @@ -196,6 +226,9 @@ def spacecraft_clock_start_count(self):
if isinstance(self._clock_start_count, pvl.Quantity):
self._clock_start_count = self._clock_start_count.value

if isinstance(self._clock_start_count, dict):
self._clock_start_count = self._clock_start_count["value"]

self._clock_start_count = str(self._clock_start_count)

return self._clock_start_count
Expand All @@ -222,6 +255,9 @@ def spacecraft_clock_stop_count(self):
if isinstance(self._clock_stop_count, pvl.Quantity):
self._clock_stop_count = self._clock_stop_count.value

if isinstance(self._clock_stop_count, dict):
self._clock_stop_count = self._clock_stop_count["value"]

self._clock_stop_count = str(self._clock_stop_count)

return self._clock_stop_count
Expand All @@ -238,7 +274,10 @@ def utc_start_time(self):
: datetime
Start time of the image in UTC
"""
return self.label['IsisCube']['Instrument']['StartTime']
utc_time = self.label['IsisCube']['Instrument']['StartTime']
if isinstance(utc_time, str):
utc_time = datetime.datetime.fromisoformat(utc_time)
return utc_time

@property
def utc_stop_time(self):
Expand All @@ -252,7 +291,10 @@ def utc_stop_time(self):
: datetime
Stop time of the image in UTC
"""
return self.label['IsisCube']['Instrument']['StopTime']
utc_time = self.label['IsisCube']['Instrument']['StopTime']
if isinstance(utc_time, str):
utc_time = datetime.datetime.fromisoformat(utc_time)
return utc_time

@property
def exposure_duration(self):
Expand All @@ -269,14 +311,23 @@ def exposure_duration(self):
# Check for units on the PVL keyword
if isinstance(exposure_duration, pvl.collections.Quantity):
units = exposure_duration.units
if "ms" in units.lower() or 'milliseconds' in units.lower():
exposure_duration = exposure_duration.value * 0.001
else:
# if not milliseconds, the units are probably seconds
exposure_duration = exposure_duration.value
value = exposure_duration.value
elif isinstance(exposure_duration, dict):
units = ""
value = exposure_duration["value"]
if "unit" in exposure_duration:
units = exposure_duration["unit"]
else:
# if no units are available, assume the exposure duration is given in milliseconds
exposure_duration = exposure_duration * 0.001
units = "ms"
value = exposure_duration

if "ms" in units.lower() or 'milliseconds' in units.lower():
exposure_duration = value * 0.001
else:
# if not milliseconds, the units are probably seconds
exposure_duration = value

return exposure_duration
else:
return self.line_exposure_duration
Expand All @@ -292,16 +343,26 @@ def line_exposure_duration(self):
Line exposure duration in seconds
"""
line_exposure_duration = self.label['IsisCube']['Instrument']['LineExposureDuration']
# Check for units on the PVL keyword
if isinstance(line_exposure_duration, pvl.collections.Quantity):
units = line_exposure_duration.units
if "ms" in units.lower():
line_exposure_duration = line_exposure_duration.value * 0.001
else:
# if not milliseconds, the units are probably seconds
line_exposure_duration = line_exposure_duration.value
value = line_exposure_duration.value
elif isinstance(line_exposure_duration, dict):
units = ""
value = line_exposure_duration["value"]
if "unit" in line_exposure_duration:
units = line_exposure_duration["unit"]
else:
# if no units are available, assume the exposure duration is given in milliseconds
line_exposure_duration = line_exposure_duration * 0.001
units = "ms"
value = line_exposure_duration

if "ms" in units.lower() or 'milliseconds' in units.lower():
line_exposure_duration = value * 0.001
else:
# if not milliseconds, the units are probably seconds
line_exposure_duration = value

return line_exposure_duration


Expand All @@ -315,16 +376,26 @@ def interframe_delay(self):
: float
interframe delay in seconds
"""
interframe_delay = self.label['IsisCube']['Instrument']['InterframeDelay']
interframe_delay = self.label['IsisCube']['Instrument'].get('InterframeDelay', None)
if interframe_delay == None:
interframe_delay = self.label['IsisCube']['Instrument'].get('InterFrameDelay', None)
if isinstance(interframe_delay, pvl.collections.Quantity):
units = interframe_delay.units
if "ms" in units.lower():
interframe_delay = interframe_delay.value * 0.001
else:
# if not milliseconds, the units are probably seconds
interframe_delay = interframe_delay.value
value = interframe_delay.value
elif isinstance(interframe_delay, dict):
units = "ms"
value = interframe_delay["value"]
if "unit" in interframe_delay:
units = interframe_delay["unit"]
else:
# if no units are available, assume the interframe delay is given in milliseconds
interframe_delay = interframe_delay * 0.001
units = "ms"
value = interframe_delay

if "ms" in units.lower() or 'milliseconds' in units.lower():
interframe_delay = interframe_delay.value * 0.001
else:
# if not milliseconds, the units are probably seconds
interframe_delay = interframe_delay.value

return interframe_delay
4 changes: 2 additions & 2 deletions ale/driver_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def main(image):
run_spiceinit_isis(image_isis_path)

# try ale.loads
isis_kerns = ale.util.generate_kernels_from_cube(image_isis_path, expand=True)
isis_kerns = ale.kernel_access.generate_kernels_from_cube(image_isis_path, expand=True)
# this can be uncommented and used when the PVL loads fix PR goes in (#587)
isis_label = pvl.load(image_isis_path)
try:
Expand All @@ -333,7 +333,7 @@ def main(image):
run_spiceinit_ale(image_ale_path)

# try ale.loads
ale_kerns = ale.util.generate_kernels_from_cube(image_ale_path, expand=True)
ale_kerns = ale.kernel_access.generate_kernels_from_cube(image_ale_path, expand=True)
ale.loads(image_ale_path, props={"kernels": ale_kerns}, only_naif_spice=True)

# Generate ISD for both ALE and ISIS
Expand Down
18 changes: 17 additions & 1 deletion ale/drivers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ def load(label, props={}, formatter='ale', verbose=False, only_isis_spice=False,
if verbose:
logger.info("First parse attempt failed with")
logger.info(e)
# If pds3 label fails, try isis grammar
parsed_label = None

# If pds3 label fails, try isis grammar
if not parsed_label:
try:
parsed_label = parse_label(label, pvl.grammar.ISISGrammar())
except Exception as e:
Expand All @@ -135,6 +138,19 @@ def load(label, props={}, formatter='ale', verbose=False, only_isis_spice=False,
# If both fail, then don't parse the label, and just pass the driver a file.
parsed_label = None

# If pvl label loading fails, try gdal
if not parsed_label:
try:
from osgeo import gdal
gdal.UseExceptions()
geodata = gdal.Open(label)
parsed_label = json.loads(geodata.GetMetadata("json:ISIS3")["doc"])
except Exception as e:
if verbose:
logger.info("Gdal parse attempt failed with")
logger.info(e)
parsed_label = None

if verbose:
if parsed_label:
logger.info("Successfully pre-parsed label file")
Expand Down
20 changes: 14 additions & 6 deletions ale/drivers/apollo_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,22 @@ def exposure_duration(self):
# Check for units on the PVL keyword
if isinstance(exposure_duration, pvl.collections.Quantity):
units = exposure_duration.units
if "ms" in units.lower() or 'milliseconds' in units.lower():
exposure_duration = exposure_duration.value * 0.001
else:
# if not milliseconds, the units are probably seconds
exposure_duration = exposure_duration.value
value = exposure_duration.value
elif isinstance(exposure_duration, dict):
units = ""
value = exposure_duration["value"]
if "unit" in exposure_duration:
units = exposure_duration["unit"]
else:
# if no units are available, assume the exposure duration is given in milliseconds
exposure_duration = exposure_duration * 0.001
units = "ms"
value = exposure_duration

if "ms" in units.lower() or 'milliseconds' in units.lower():
exposure_duration = value * 0.001
else:
# if not milliseconds, the units are probably seconds
exposure_duration = value
return exposure_duration

@property
Expand Down
27 changes: 16 additions & 11 deletions ale/drivers/co_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,14 @@ def exposure_duration(self):
exposure_duration = self.label['IsisCube']['Instrument']['ExposureDuration']

for i in exposure_duration:
if i.units == "VIS":
exposure_duration = i

exposure_duration = exposure_duration.value * 0.001
return exposure_duration
if isinstance(i, pvl.collections.Quantity):
if i.units == "VIS":
exposure_duration = i.value
elif isinstance(i, dict):
if i["unit"] == "VIS":
exposure_duration = i["value"]

return exposure_duration * 0.001
else:
return self.line_exposure_duration

Expand Down Expand Up @@ -502,13 +505,15 @@ def exposure_duration(self):
"""
if 'ExposureDuration' in self.label['IsisCube']['Instrument']:
exposure_duration = self.label['IsisCube']['Instrument']['ExposureDuration']

for i in exposure_duration:
if i.units == "VIS":
exposure_duration = i

exposure_duration = exposure_duration.value * 0.001
return exposure_duration
if isinstance(exposure_duration, pvl.collections.Quantity):
if i.units == "VIS":
exposure_duration = i.value
elif isinstance(exposure_duration, dict):
if i["unit"] == "VIS":
exposure_duration = i["value"]

return exposure_duration * 0.001
else:
return self.line_exposure_duration

Expand Down
9 changes: 7 additions & 2 deletions ale/drivers/isis_ideal_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ def ephemeris_start_time(self):
float :
The image start ephemeris time
"""

return self.label.get('IsisCube').get('Instrument').get("EphemerisTime").value
if not hasattr(self, "_ephemeris_start_time"):
self._ephemeris_start_time = self.label['IsisCube']['Instrument']["EphemerisTime"]
if isinstance(self._ephemeris_start_time, pvl.collections.Quantity):
self._ephemeris_start_time = self._ephemeris_start_time.value
elif isinstance(self._ephemeris_start_time, dict):
self._ephemeris_start_time = self._ephemeris_start_time["value"]
return self._ephemeris_start_time


@property
Expand Down
20 changes: 16 additions & 4 deletions ale/drivers/lo_drivers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np
import pvl
import spiceypy as spice

from ale.base.data_naif import NaifSpice
from ale.base.label_isis import IsisLabel
from ale.base.type_sensor import Framer
Expand Down Expand Up @@ -151,11 +153,21 @@ def naif_keywords(self):
if (not hasattr(self, "_naif_keywords")):
# From ISIS LoCameraFiducialMap

p_fidSamples = self.label['IsisCube']['Instrument']['FiducialSamples']
p_fidLines = self.label['IsisCube']['Instrument']['FiducialLines']
p_fidXCoords = self.label['IsisCube']['Instrument']['FiducialXCoordinates']
p_fidYCoords = self.label['IsisCube']['Instrument']['FiducialYCoordinates']
# Read Fiducials
p_fidSamples = self.label['IsisCube']['Instrument']['FiducialSamples'].value
p_fidLines = self.label['IsisCube']['Instrument']['FiducialLines'].value
p_fidXCoords = self.label['IsisCube']['Instrument']['FiducialXCoordinates'].value
p_fidYCoords = self.label['IsisCube']['Instrument']['FiducialYCoordinates'].value
if isinstance(p_fidSamples, pvl.collections.Quantity):
p_fidSamples = p_fidSamples.value
p_fidLines = p_fidLines.value
p_fidXCoords = p_fidXCoords.value
p_fidYCoords = p_fidYCoords.value
elif isinstance(p_fidSamples, dict):
p_fidSamples = p_fidSamples["value"]
p_fidLines = p_fidLines["value"]
p_fidXCoords = p_fidXCoords["value"]
p_fidYCoords = p_fidYCoords["value"]

# Create Affine Transformation
p_src = [p_fidSamples, p_fidLines]
Expand Down
Loading
Loading