Skip to content

Commit

Permalink
Remove soon-to-be-deprecated usage of AsdfFile.open
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Nov 9, 2018
1 parent 610a06d commit b9152f6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
9 changes: 5 additions & 4 deletions docs/jwst/assign_wcs/asdf-howto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,18 @@ Save a transform to an ASDF file
`asdf <http://asdf.readthedocs.io/en/latest/>`__ is used to read and write reference files in
`ASDF <http://asdf-standard.readthedocs.org/en/latest/>`__ format. Once the model is create using the rules in the above section, it needs to be assigned to the ASDF tree.

>>> import asdf
>>> from asdf import AsdfFile
>>> f = AsdfFile()
>>> f.tree['model'] = model
>>> f.write_to('reffile.asdf')

The ``write_to`` command validates the file and writes it to disk. It will catch any errors due to
inconsistent inputs/outputs or invalid parameters.
The ``write_to`` command validates the file and writes it to disk. It will
catch any errors due to inconsistent inputs/outputs or invalid parameters.

To test the file, it can be read in again using the ``AsdfFile.open()`` method:
To test the file, it can be read in again using the ``asdf.open()`` function:

>>> ff = AsdfFile.open('reffile.asdf')
>>> ff = asdf.open('reffile.asdf')
>>> model = ff.tree['model']
>>> model(1, 1)
-152.2
Expand Down
4 changes: 2 additions & 2 deletions jwst/assign_wcs/niriss.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from asdf import AsdfFile
import asdf
from astropy import coordinates as coord
from astropy import units as u
from astropy.modeling.models import Const1D, Mapping, Identity
Expand Down Expand Up @@ -131,7 +131,7 @@ def niriss_soss(input_model, reference_files):
world = cf.CompositeFrame([sky, spec], name='world')

try:
with AsdfFile.open(reference_files['specwcs']) as wl:
with asdf.open(reference_files['specwcs']) as wl:
wl1 = wl.tree[1].copy()
wl2 = wl.tree[2].copy()
wl3 = wl.tree[3].copy()
Expand Down
21 changes: 11 additions & 10 deletions jwst/datamodels/model_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from astropy.time import Time
from astropy.wcs import WCS

import asdf
from asdf import AsdfFile
from asdf import yamlutil
from asdf import schema as asdf_schema
Expand Down Expand Up @@ -119,13 +120,13 @@ def __init__(self, init=None, schema=None, extensions=None,
asdf_kwargs = dict(inline_threshold=kwargs.pop('inline_threshold', 0))

if init is None:
asdf = AsdfFile(extensions=extensions, **asdf_kwargs)
asdffile = AsdfFile(extensions=extensions, **asdf_kwargs)

elif isinstance(init, dict):
asdf = AsdfFile(init, extensions=self._extensions, **asdf_kwargs)
asdffile = AsdfFile(init, extensions=self._extensions, **asdf_kwargs)

elif isinstance(init, np.ndarray):
asdf = AsdfFile(extensions=self._extensions, **asdf_kwargs)
asdffile = AsdfFile(extensions=self._extensions, **asdf_kwargs)
shape = init.shape
is_array = True

Expand All @@ -135,7 +136,7 @@ def __init__(self, init=None, schema=None, extensions=None,
raise ValueError("shape must be a tuple of ints")

shape = init
asdf = AsdfFile(**asdf_kwargs)
asdffile = AsdfFile(**asdf_kwargs)
is_shape = True

elif isinstance(init, DataModel):
Expand All @@ -145,10 +146,10 @@ def __init__(self, init=None, schema=None, extensions=None,
return

elif isinstance(init, AsdfFile):
asdf = init
asdffile = init

elif isinstance(init, fits.HDUList):
asdf = fits_support.from_fits(init, self._schema,
asdffile = fits_support.from_fits(init, self._schema,
self._extensions, self._ctx)

elif isinstance(init, (str, bytes)):
Expand All @@ -158,13 +159,13 @@ def __init__(self, init=None, schema=None, extensions=None,

if file_type == "fits":
hdulist = fits.open(init)
asdf = fits_support.from_fits(hdulist, self._schema,
asdffile = fits_support.from_fits(hdulist, self._schema,
self._extensions, self._ctx)

self._files_to_close.append(hdulist)

elif file_type == "asdf":
asdf = AsdfFile.open(init, extensions=self._extensions)
asdffile = asdf.open(init, extensions=self._extensions)

else:
# TODO handle json files as well
Expand All @@ -177,8 +178,8 @@ def __init__(self, init=None, schema=None, extensions=None,

# Initialize object fields as determined from the code above
self._shape = shape
self._instance = asdf.tree
self._asdf = asdf
self._instance = asdffile.tree
self._asdf = asdffile

# Initalize class dependent hidden fields
self._no_asdf_extension = False
Expand Down

0 comments on commit b9152f6

Please sign in to comment.