<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "\n",
    "import logging\n",
    "import webbrowser\n",
    "from cStringIO import StringIO\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "from astropy.io import fits\n",
    "from stginga import nbinteract\n",
    "\n",
    "from IPython.html.widgets import interact\n",
    "from IPython import display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "server = nbinteract.GingaServer()\n",
    "server.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "server.get_viewer_urls()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "display.IFrame(server.get_viewer_urls()['Main Viewer'], 625, 625)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next cell will open a new window with the same view as above"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "webbrowser.open(server.get_viewer_urls()['Main Viewer'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that this next cell may take some time to download the first time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f = fits.open('https://archive.stsci.edu/pub/hlsp/angst/acs/hlsp_angst_hst_acs-wfc_10210-ugc8760_f814w_v1_ref.fits')\n",
    "server.load_fits(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You'll need to download have some acs image for this dataset to run the next cell.  A convenient one (to match the above) might be: https://archive.stsci.edu/cgi-bin/mastpreview?mission=hst&dataid=J8YY05021"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fi = fits.open('j8yy05kbq_drc.fits')\n",
    "\n",
    "@interact(hdunum=(1,3))\n",
    "def switch_hdu(hdunum=1):\n",
    "    server.load_fits(fi[hdunum])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "v = server.viewers['Main Viewer']\n",
    "aim = v.fitsimage.get_image()\n",
    "\n",
    "for r in v.canvas.get_objects()[1:]:\n",
    "    corners = np.array(r.get_points())\n",
    "    xmin, xmax = np.min(corners[:, 0]), np.max(corners[:, 0])\n",
    "    ymin, ymax = np.min(corners[:, 1]), np.max(corners[:, 1])\n",
    "    data_square = aim.get_data()[ymin:ymax, xmin:xmax]\n",
    "    flux = np.sum(data_square)\n",
    "    print('Rectangle centered on', r.get_center_pt(), 'has flux', flux)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# clear all rectangles\n",
    "v = server.viewers['Main Viewer']\n",
    "v.canvas.deleteObjects(v.canvas.get_objects()[1:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Debug code "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "log = logging.Logger('nbinteract sio logger')\n",
    "logsio = StringIO()\n",
    "#log.addHandler(logging.StreamHandler(stream=logsio))\n",
    "log.addHandler(logging.NullHandler())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {<commit_msg>update notebook<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example of nbinteract usage "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "\n",
    "import logging\n",
    "import webbrowser\n",
    "from cStringIO import StringIO\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "from astropy.io import fits\n",
    "from stginga import nbinteract\n",
    "\n",
    "from IPython.html.widgets import interact\n",
    "from IPython import display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "server = nbinteract.GingaServer()\n",
    "server.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "server.get_viewer_urls()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "display.IFrame(server.get_viewer_urls()['Main Viewer'], 625, 625)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next cell will open a new window with the same view as above"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "webbrowser.open(server.get_viewer_urls()['Main Viewer'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that this next cell may take some time to download the first time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f = fits.open('https://archive.stsci.edu/pub/hlsp/angst/acs/hlsp_angst_hst_acs-wfc_10210-ugc8760_f814w_v1_ref.fits')\n",
    "server.load_fits(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You'll need to download some ACS image for this dataset to run the next cell.  A convenient one (to match the above) might be: https://archive.stsci.edu/cgi-bin/mastpreview?mission=hst&dataid=J8YY05021"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fi = fits.open('j8yy05kbq_drc.fits')\n",
    "\n",
    "@interact(hdunum=(1,3))\n",
    "def switch_hdu(hdunum=1):\n",
    "    server.load_fits(fi[hdunum])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "v = server.viewers['Main Viewer']\n",
    "aim = v.fitsimage.get_image()\n",
    "\n",
    "for r in v.canvas.get_objects()[1:]:\n",
    "    corners = np.array(r.get_points())\n",
    "    xmin, xmax = np.min(corners[:, 0]), np.max(corners[:, 0])\n",
    "    ymin, ymax = np.min(corners[:, 1]), np.max(corners[:, 1])\n",
    "    data_square = aim.get_data()[ymin:ymax, xmin:xmax]\n",
    "    flux = np.sum(data_square)\n",
    "    print('Rectangle centered on', r.get_center_pt(), 'has flux', flux)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# clear all rectangles\n",
    "v = server.viewers['Main Viewer']\n",
    "v.canvas.deleteObjects(v.canvas.get_objects()[1:])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
{'fraction_non_alphanumeric': 0.20337197049525815, 'fraction_numerical': 0.009747102212855638, 'mean_word_length': 2.7701092353525323, 'pattern_counts': {'":': 144, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4659578', 'n_tokens_mistral': 3019, 'n_tokens_neox': 2927, 'n_words': 531}