<jupyter_start><jupyter_text>Preparing a proposalThe Story: Suppose that you are preparing to write a proposal on NGC1365, aiming to investigate the intriguing black hole spin this galaxy with Chandra grating observations (see: https://www.space.com/19980-monster-black-hole-spin-discovery.html ) In writing proposals, there are often the same tasks that are required: including finding and analyzing previous observations of the proposal, and creating figures that include, e.g., multiwavelength images and spectrum for the source.<jupyter_code># As a hint, we include the code block for Python modules that you will likely need to import:   
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline  
import requests, io
from astropy.table import Table

## Use NAVO utility for Registry, cone searches and making images and spectra
import sys
from navo_utils.registry import Registry
from navo_utils.image import Image, ImageColumn
from navo_utils.spectra import Spectra, SpectraColumn
from navo_utils.tap import Tap


## There are a number of relatively unimportant warnings that 
## show up, so for now, suppress them:
import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Step 1: Find out what the previously quoted Chandra 2-10 keV flux of the central source is for NGC 1365.  Hint: Do a table access protocol (TAP) search in the heasarc, to find potential table with this information<jupyter_code>tap_services=Registry.query(service_type='table',source='heasarc')
url=str(tap_services[0]['access_url'])
# Since there are lots of tables in the HEASARC service, we will narrow the search of Chandra tables by setting contains="chan"
Tap.list_tables(url, contains="chan")<jupyter_output>Retrieving tables...
Parsing tables...
Done.
public.bmwchancat
public.chandfn1ms
public.chandfn2ms
public.chandfs1ms
public.chandfs2ms
public.chandfs4ms
public.chandfs7ms
public.chanextdfs
public.changbscat
public.chanmaster
public.channsgpsc
public.chansexagn
public.chansexoid
public.chansexsi
public.chansngcat
public.chantypgpr
public.chanulxcat
public.chanvguide<jupyter_text>Hint: The Chansngcat ( https://heasarc.gsfc.nasa.gov/W3Browse/chandra/chansngcat.html ) table is likely the best table.  Create a table with ra, dec, exposure time, and flux (and flux errors) from the public.chansngcat catalog for Chandra observations matched within 0.1 degree.<jupyter_code># list the columns from the query above
Tap.list_columns(url,tablename="public.chansngcat")
# Get the coordinate for NGC 1365
import astropy.coordinates as coord
pos=coord.SkyCoord.from_name("ngc1365")
# Show the results from your table query for NGC 1365 and print the ra, dec, exposure time, flux, and flux errors
query="""SELECT ra, dec, exposure, flux, flux_lower, flux_upper FROM public.chansngcat as cat 
    where contains(point('ICRS',cat.ra,cat.dec),circle('ICRS',{},{},0.1))=1 
    and cat.exposure > 0 order by cat.exposure""".format(pos.ra.deg, pos.dec.deg)
results=Tap.query(url,query)
results<jupyter_output><empty_output><jupyter_text>Step 2: Make Images:  Create ultraviolet and X-ray imagesHint: Start by checking what UV image services exist (e.g., GALEX?)<jupyter_code>uv_tables=Registry.query(service_type='image',keyword='galex', waveband='uv')
im_gal = uv_tables[np.isin(uv_tables['short_name'], 
                                    ['GALEX'])]
im_gal['short_name', 'access_url']<jupyter_output><empty_output><jupyter_text>Hint: Next create a UV image for the source<jupyter_code># Do an image search for NGC 1365 in the UV service found above

im_table=Image.query(uv_tables[0],coords=pos,radius=0.1)
im_table[0]
# Get the FITS file (which is index 0 for the NUV image or index=2 for the FUV image)
hdu_list=Image.get_fits(im_table[0][0])
# make an image: 
from pylab import figure, cm
from matplotlib.colors import LogNorm
plt.matshow(hdu_list[0].data, origin='lower', cmap=cm.gray_r, norm=LogNorm(vmin=0.005, vmax=0.3))<jupyter_output><empty_output><jupyter_text>Hint: Repeat steps for X-ray image. (Note: Ideally, we would find an image in the Chandra 'cxc' catalog)<jupyter_code>x_tables=Registry.query(service_type='image',waveband='x-ray', source='cxc')
x_tables
# Do an image search for NGC 1365 in the X-ray CDA service found above
xim_table=Image.query(x_tables[0],coords=pos,radius=0.1)
xim_table[0]
# Create an image from the first FITS file (index=1) by downloading:
xhdu_list=Image.get_fits(xim_table[0][1])

plt.imshow(xhdu_list[0].data, origin='lower', cmap='cool', norm=LogNorm(vmin=0.1, vmax=500.))
plt.xlim(460, 560)
plt.ylim(460, 560)<jupyter_output><empty_output><jupyter_text>Step 3: Make a spectrum:  Find what Chandra spectral observations exist already for this source. Hint: try searching for X-ray spectral data tables using the registry query<jupyter_code># Use the TAP protocol to list services that contain X-ray spectral data
services=Registry.query(service_type='spectr',waveband='x-ray')
services
# We want this last listed service and we can identify this by specifying the short_name
chan_tables = services[np.isin(services['short_name'],['Chandra'])]
chan_tables['short_name', 'access_url']<jupyter_output><empty_output><jupyter_text>Hint 2: Take a look at what data exist for our candidate, NGC 1365.<jupyter_code>from astropy.coordinates import SkyCoord
target=SkyCoord.from_name("NGC1365")
pos='{},{}'.format(target.ra.deg,target.dec.deg)

spec_tables=Spectra.query(service=chan_tables[0],coords=pos,radius=0.1,verbose=True)

spec_tables[0]['time'].format='.4f'
spec_tables[0].show_in_notebook()<jupyter_output>Querying service https://heasarc.gsfc.nasa.gov/xamin/vo/ssa?table=chanmaster&amp;
    Got 2 results for parameters[0]<jupyter_text>Hint 3: Download the data to make a spectrum. Note: you might end here and use Xspec to plot and model the spectrum. Or ... you can also try to take a quick look at the spectrum.<jupyter_code>hdu_list=Spectra.get_fits(spec_tables[0][1])
# Or write it to disk
Spectra.get_fits(spec_tables[0][1],filename='downloads/ngc1365_spectrum.fits')
spec_table=Table(hdu_list[1].data)
spec_table<jupyter_output><empty_output><jupyter_text>Extension: Making a "quick look" spectrum. For our purposes, the 1st order of the HEG grating data would be sufficient.<jupyter_code>j=1
for i in range(len(spec_table)):
    matplotlib.rcParams['figure.figsize'] = (8, 3)
    if abs(spec_table['TG_M'][i]) == 1 and (spec_table['TG_PART'][i]) == 1:
        ax=plt.subplot(1,2,j)
        pha = plt.plot( spec_table['CHANNEL'][i],spec_table['COUNTS'][i])
        ax.set_yscale('log')
        if spec_table['TG_PART'][i] == 1:
            instr='HEG'
        ax.set_title("{grating}{order:+d}".format(grating=instr, order=spec_table['TG_M'][i]))
        plt.tight_layout()
        j=j+1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10798534798534798, 'fraction_numerical': 0.01904761904761905, 'mean_word_length': 6.0371134020618555, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 40, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, '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}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '484513', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2237, 'n_words': 645, 'path': 'Science Use Case - Proposal Prep.ipynb', 'repo_name': 'trjaffe/workshop-dev', 'size': '6825'}