Commit 01ccc214 authored by Kaitlyn Lee's avatar Kaitlyn Lee Committed by Kelvin Rodriguez
Browse files

Added lro lroc nac notebook. (#196)

* Added lro lroc nac notebook.

* Config changes.

* Rough draft for changes to notebook.

* doc updates and config update

* Updated lro notebook and added config.yml to load metakernel paths.

* Made some quick fixes to documentation.

* Fixed config import

* Removed yaml warning message.

* Fixed error.
parent 65ade934
Loading
Loading
Loading
Loading
+27 −5
Original line number Diff line number Diff line
from . import drivers
from . import formatters
from .drivers import load, loads

import os

import pathlib
from shutil import copyfile
import yaml
from pkg_resources import get_distribution, DistributionNotFound

class DotDict(dict):
    """dot.notation access to dictionary attributes"""""
    __getattr__ = dict.get
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__

    def __str__(self):
        return yaml.dump(dict(self))

home_path = os.path.expanduser('~')
config_dir = pathlib.Path(os.path.join(home_path, '.ale'))
config_file_path = pathlib.Path(os.path.join(home_path, '.ale', 'config.yml'))

config_dir.mkdir(parents=True, exist_ok=True)

if not config_file_path.is_file():
  copyfile(os.path.join(os.path.dirname(__file__), 'config.yml'), config_file_path)

config = DotDict(yaml.load(open(config_file_path), Loader=yaml.FullLoader))

try:
    _dist = get_distribution('ale')
    # Normalize case for Windows systems
@@ -19,3 +37,7 @@ except DistributionNotFound:
else:
    __version__ = _dist.version

# bring ale stuff into main ale module
from . import drivers
from . import formatters
from . drivers import load, loads

ale/config.py

deleted100644 → 0
+0 −12
Original line number Diff line number Diff line
"""
Config File
"""

# Directory with metakernals
spice_root = "/data/spice/"
cassini = '/usgs/cpkgs/isis3/data/cassini/kernels/mk/' # Cassini ISS
mdis = '/data/spice/mess-e_v_h-spice-6-v1.0/messsp_1000/extras/mk' # Messenger
mro = '/data/spice/mro-m-spice-6-v1.0/mrosp_1000/extras/mk' # Mars Reconnaissance Orbiter
kaguya = '/data/spice/SELENE/kernels/mk/'
dawn = '/data/spice/dawn-m_a-spice-6-v1.0/dawnsp_1000/extras/mk'
lro = '/usgs/cpkgs/isis3/data/lro/kernels/mk/' # LRO

ale/config.yml

0 → 100644
+7 −0
Original line number Diff line number Diff line
spice_root: "/data/spice/"
cassini: '/usgs/cpkgs/isis3/data/cassini/kernels/mk/' # Cassini ISS
mdis: '/data/spice/mess-e_v_h-spice-6-v1.0/messsp_1000/extras/mk' # Messenger
mro: '/data/spice/mro-m-spice-6-v1.0/mrosp_1000/extras/mk' # Mars Reconnaissance Orbiter
kaguya: '/data/spice/SELENE/kernels/mk/'
dawn: '/data/spice/dawn-m_a-spice-6-v1.0/dawnsp_1000/extras/mk'
lro: '/scratch/jlaura/spice/lro-l-spice-6-v1.0/lrosp_1000/extras/mk/' # LRO
+90 −0
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

# Writing out a USGSCSM ISD from a PDS3 LRO LROC NAC image

%% Cell type:code id: tags:

``` python
import ale
from ale.drivers.lro_drivers import LroLrocPds3LabelNaifSpiceDriver
from ale.formatters.usgscsm_formatter import to_usgscsm
import json
import os
```

%% Cell type:markdown id: tags:

## Instantiating an ALE driver

ALE drivers are objects that define how to acquire common ISD keys from an input image format, in this case we are reading in a PDS3 image using NAIF SPICE kernels for exterior orientation data. If the driver utilizes NAIF SPICE kernels, it is implemented as a [context manager](https://docs.python.org/3/reference/datamodel.html#context-managers) and will furnish metakernels when entering the context (i.e. when entering the `with` block) and free the metakernels on exit. This maintains the integrity of spicelib's internal data structures. These driver objects are short-lived and are input to a formatter function that consumes the API to create a serializable file format. `ale.formatters` contains available formatter functions.

The default config file is located at `ale/config.yml` and is copied into your home directory at `.ale/config.yml` on first use of the library. The config file can be modified using a text editor. `ale.config` is loaded into memory as a dictionary. It is used to find metakernels for different missions. For example, there is an entry for LRO that points to `/usgs/cpkgs/isis3/data/lro/kernels/mk/` by default. If you want to use your own metakernels, you will need to udpate this path. For example, if the metakernels are located in `/data/lrolrocnac/mk/` the LRO entry should be updated with this path. If you are using the default metakernels, then you do not need to update the path.

ALE has a two step process for writing out an ISD: 1. Instantiate your driver (in this case `LroLrocPds3LabelNaifSpiceDriver`) within a context and 2. pass the driver object into a formatter (in this case, `to_usgscsm`).

Requirements:
 * A PDS3 LRO LROC NAC image
 * NAIF metakernels installed
 * Config file path for LRO (ale.config.lro) pointing to LRO NAIF metakernel directory
 * A conda environment with ALE installed into it usisng the `conda install` command or created using the environment.yml file at the base of ALE.

%% Cell type:code id: tags:

``` python
# printing config displays the yaml formatted string
print(ale.config)

# config object is a dictionary so it has the same access patterns
print('LRO spice directory:', ale.config['lro'])

# updating config for new LRO path in this notebook
# Note: this will not change the path in `.ale/config.yml`. This change only lives in the notebook.
# ale.config['lro'] = '/data/lrolrocnac/mk/'
```

%% Output

    cassini: /usgs/cpkgs/isis3/data/cassini/kernels/mk/
    dawn: /data/spice/dawn-m_a-spice-6-v1.0/dawnsp_1000/extras/mk
    kaguya: /data/spice/SELENE/kernels/mk/
    lro: /scratch/jlaura/spice/lro-l-spice-6-v1.0/lrosp_1000/extras/mk/
    mdis: /data/spice/mess-e_v_h-spice-6-v1.0/messsp_1000/extras/mk
    mro: /data/spice/mro-m-spice-6-v1.0/mrosp_1000/extras/mk
    spice_root: /data/spice/
    
    LRO spice directory: /scratch/jlaura/spice/lro-l-spice-6-v1.0/lrosp_1000/extras/mk/

%% Cell type:code id: tags:

``` python
# change to desired PDS3 image path
fileName = 'M1142142198RE.IMG'

# metakernels are furnsh-ed when entering the context (with block) with a driver instance
# most driver constructors simply accept an image path
with LroLrocPds3LabelNaifSpiceDriver(fileName) as driver:
    # pass driver instance into formatter function
    usgscsmString = to_usgscsm(driver)
```

%% Cell type:markdown id: tags:

### Write ISD to disk

ALE formatter functions generally return bytes or a string that can be written out to disk. ALE's USGSCSM formatter function returns a JSON encoded string that can be written out using any JSON library.

USGSCSM requires the ISD to be colocated with the image file with a `.json` extension in place of the image extension.

%% Cell type:code id: tags:

``` python
# load the json encoded string ISD
usgscsm_dict = json.loads(usgscsmString)

# strip the image file extension and append .json
jsonFile = os.path.splitext(fileName)[0] + '.json'

# write to disk
with open(jsonFile, 'w') as fp:
    json.dump(usgscsm_dict, fp)
```