Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Ginga viewer for ASDF #3

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ RELIC-INFO
relic/
*.wpr
*wpu
.ipynb_checkpoints/
212 changes: 212 additions & 0 deletions general/Ginga_ASDF_Demo_1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Viewing JWST ASDF in Ginga"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is a basic example to view JWST ASDF datamodel using Ginga viewer in a Jupyter notebook. Image used in this example is from NIRCam but the general concept should be instrument-agnostic.\n",
"\n",
"**TODO**\n",
"\n",
"* Add GWCS support (pending upstream fixes). Currently, Ginga would report \"bad WCS\"; ignore that until this is addressed.\n",
"* Polish the example based on PR review."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"from jwst import datamodels\n",
"\n",
"filename = ('/grp/jwst/ssb/test_build7.1/examples_for_dms/'\n",
" 'level3/calwebb_image3/sources/nircam_sim/'\n",
" 'jw99999001002_03101_00001_nrcalong_cal.fits')\n",
"\n",
"# Open JWST ASDF data model, ignoring warnings\n",
"with warnings.catch_warnings():\n",
" warnings.simplefilter('ignore')\n",
" im = datamodels.open(filename)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from ginga.web.pgw import ipg\n",
"\n",
"# Set to some large number\n",
"portnum = 9914\n",
"\n",
"# Set this to True if you have a non-buggy python OpenCv bindings;\n",
"# it greatly speeds up some operations\n",
"use_opencv = False\n",
"\n",
"# Set up Ginga local server\n",
"server = ipg.make_server(\n",
" host='localhost', port=portnum, use_opencv=use_opencv)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Start viewer server\n",
"# IMPORTANT: if running in an IPython/Jupyter notebook,\n",
"# use the no_ioloop=True option\n",
"server.start(no_ioloop=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Get a viewer\n",
"# This will get a handle to the viewer \"v1\"\n",
"v1 = server.get_viewer('v1')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'http://localhost:9914/app?id=v1'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Where is my viewer?\n",
"v1.url"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now copy and paste the link given by `.url` to open viewer in a separate browser tab/window."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Change this to match datamodel as needed\n",
"ext_map = {1: 'data', 2: 'err', 3: 'dq'}\n",
"\n",
"# Define a function for the slider\n",
"def change_ext(ext):\n",
" \"\"\"\n",
" Change displayed extension when it slides.\n",
" \n",
" Parameters\n",
" ----------\n",
" ext : int\n",
" Integer value of the extension.\n",
" \n",
" \"\"\"\n",
" v1.load_data(getattr(im, ext_map[ext]))\n",
" v1.show()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "04fc907486d347f495f7e9545bf0baed",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"A Jupyter Widget"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import ipywidgets as widgets\n",
"from ipywidgets import interact\n",
"\n",
"# Initialize and display the slider\n",
"interact(change_ext, ext=widgets.IntSlider(min=1,max=3,step=1,value=1));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you are done, run this last cell to close the datamodel handler."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"im.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}