<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Galaxies that are missing from Simard+2011
Summary
* A total of 44 galaxies are not in galfit sample
* 31/44 are not in the SDSS catalog, so these would not have been targeted by Simard+2011
* this is not true because simard drew from phot catalog.  Need to check all.
* 1 arcmin cutouts show
Step1:  Galaxies not in SDSS phot catalog
Step2:  Galaxies in SDSS but no B/T fit
Step3:  Download SDSS Images
Step4:  NSAID 69538 (244.060699,  34.258434)
http
Step5:  NSAID 143514 (202.163284,  11.387049)
(too bright)
NSAID 163615 (202.697357,  11.200765)
(too bright)
NSAID 146832 (243.526657,  34.870651)
BINNED1 SATURATED INTERP COSMIC_RAY CHILD
(saturated)
NSAID 146875 (244.288803,  34.878895)
DEBLENDED_AT_EDGE BINNED1 NOTCHECKED INTERP CHILD EDGE
r = 13.92
(too bright)
NSAID 165409 (220.235657,   3.527517)
DEBLEND_NOPEAK INTERP_CENTER BINNED1 DEBLENDED_AS_PSF INTERP CHILD
r = 18.82
(deblended and too faint)
NSAID 166699 (241.500229,  22.641125)
BAD_MOVING_FIT MOVED BINNED1 INTERP COSMIC_RAY CHILD
r = 15.01, ext_r = 0.18
(not sure)
NSAID 146638 (241.287476,  17.729904)
STATIONARY BINNED1 SATURATED INTERP COSMIC_RAY CHILD
r = 13.57, ext_r = .13
(too bright, saturated)
NSAID 146659 (241.416641,  18.055758)
STATIONARY BINNED1 INTERP COSMIC_RAY CHILD
r = 14.52, ext_r = 0.14
(not sure)
NSAID 146664 (241.435760,  17.715572)
STATIONARY MOVED BINNED1 INTERP COSMIC_RAY CHILD
r = 14.65, ext_r = 0.13
(not sure)
not in simard
NSAID 140139 (175.954529,  19.968401)
DEBLEND_DEGENERATE PSF_FLUX_INTERP DEBLENDED_AT_EDGE BAD_MOVING_FIT MOVED BINNED1 INTERP COSMIC_RAY NODEBLEND CHILD BLENDED
r = 13.78, ext_r = 0.06
(too bright)
NSAID 140160 (176.071716,  20.223295)
STATIONARY BINNED1 INTERP COSMIC_RAY CHILD
r = 13.90, ext_r = 0.06
(too bright) 
NSAID 140174 (176.204865,  19.795046)
STATIONARY BINNED1 INTERP COSMIC_RAY CHILD
r = 13.18, ext_r = 0.07
(too bright)
NSAID 140175 (176.195999,  20.125084)
STATIONARY BINNED1 INTERP COSMIC_RAY CHILD
r = 13.38, ext_r = 0.07
not in simard
NSAID 140187 (176.228104,  20.017143)
STATIONARY BINNED1 INTERP CHILD
r = 16.19, ext_r = 0.08
(not sure)
not in simard
NSAID 146094 (230.452133,   8.410197)
STATIONARY BINNED1 CHILD
r = 15.86, ext_r = 0.10
(not sure)
IN SIMARD!!!
NSAID 146121 (230.750824,   8.465475)
MOVED BINNED1 INTERP COSMIC_RAY CHILD
r = 15.80, ext_r = 0.09
NSAID 146127 (230.785812,   8.334576)
PSF_FLUX_INTERP INTERP_CENTER STATIONARY BINNED1 INTERP NOPETRO NODEBLEND CHILD BLENDED
r = 17.20, ext_r = 0.09
(blended?)
NSAID 146130 (230.800995,   8.549866)
BINNED1 INTERP COSMIC_RAY CHILD
r = 15.36, ext_r = 0.09
(not sure, maybe blended?)
NSAID 145965 (228.749756,   6.804669)
STATIONARY MOVED BINNED1 INTERP COSMIC_RAY NOPETRO CHILD
r = 16.70, ext_r = 0.10
(no petro)
NSAID 145984 (229.076614,   6.803605)
BINNED1 INTERP COSMIC_RAY CHILD
r = 15.72, ext_r = 0.1
(not sure)
NSAID 145998 (229.185364,   7.021626)
NSAID 145999 (229.187805,   7.055664)
NSAID 146012 (229.295181,   6.941795)
NSAID 146041 (229.713806,   6.435888)
NSAID 166042 (228.910904,   8.302397)
NSAID 166044 (228.936951,   6.958703)
NSAID 166083 (229.217957,   6.539137)
NSAID 142797 (195.073654,  27.955275)
NSAID 142819 (195.169479,  28.519848)
NSAID 142833 (195.214752,  28.042875)
NSAID 162838 (195.280670,  28.121592)
Oh No!
seems like galaxies that are in simard are not in my catalog 
<ASSISTANT_TASK:>
Python Code:
%run ~/Dropbox/pythonCode/LCSanalyzeblue.py
t = s.galfitflag & s.lirflag & s.sizeflag & ~s.agnflag & s.sbflag
galfitnogim = t & ~s.gim2dflag
sum(galfitnogim)
Explanation: Galaxies that are missing from Simard+2011
Summary
* A total of 44 galaxies are not in galfit sample
* 31/44 are not in the SDSS catalog, so these would not have been targeted by Simard+2011
* this is not true because simard drew from phot catalog.  Need to check all.
* 1 arcmin cutouts show: 5 have a bright star overlapping or nearby the galaxy, 2
have a close companion.
* 69538 has problem with NSA coords
* 68342 - not in DR7 for some reason
By galaxy:
NSAID 70630 (202.306824,  11.275839)
STATIONARY BAD_MOVING_FIT BINNED1 INTERP COSMIC_RAY CHILD
r = 13.87 (too bright)
NSAID 70685 (202.269455,  12.238585)
DEBLENDED_AT_EDGE STATIONARY MOVED BINNED1 DEBLENDED_AS_PSF INTERP CHILD, 
affected by point source that is offset from center of galaxy.  might a foreground star.
(blended)
NSAID 43712 (244.129181,  35.708172)
BINNED1 INTERP COSMIC_RAY CHILD
r = 13.66 (too bright)
NSAID 69538 (244.060699,  34.258434)
NSA has problem with coords, chose nearby pt source rather than galaxy
STATIONARY BINNED1 INTERP COSMIC_RAY CHILD
r = 18.83 (too faint)
NSAID 18158 (219.578888,   3.639615)
PSF_FLUX_INTERP INTERP_CENTER BAD_MOVING_FIT BINNED1 NOTCHECKED SATURATED INTERP COSMIC_RAY CHILD
r = 15.31, ext_r = 0.11
(not sure)
NSAID 68283 (242.047577,  24.507439)
not in dr7?
NSAID 68286 (241.749313,  24.160772)
STATIONARY MOVED BINNED1 INTERP COSMIC_RAY NOPETRO NODEBLEND CHILD BLENDED PrimTarget
r = 17.66, ext_r = 0.2 (too faint)
NSAID 68299 (240.918945,  24.602676)
STATIONARY BINNED1 INTERP COSMIC_RAY CHILD
r = 15.4, ext_r = 0.2
(not sure) why this is not in simard
NSAID 68342 (241.297867,  24.960102)
does not come up under dr7 search.  get nearby object instead
NSAID 113068 (175.995667,  20.077011)
STATIONARY MOVED BINNED1 INTERP COSMIC_RAY CHILD
r = 13.74, ext_r = 0.06 (too bright)
NSAID 72631 (230.999481,   8.508963)
MAYBE_CR BAD_MOVING_FIT MOVED BINNED1 INTERP CHILD
Type probably not 3 
r = 16.98, ext_r = 0.1
NSAID 103927 (194.490204,  27.443319)
STATIONARY BINNED1 INTERP CHILD
r = 17.64, ext_r = 0.02 (maybe petro r is too faint?)
(too faint?)
NSAID 103966 (194.025421,  27.677467)
DEBLEND_DEGENERATE BAD_MOVING_FIT MOVED BINNED1 INTERP COSMIC_RAY NODEBLEND CHILD BLENDED
r = 15.13, ext_r = 0.02
(not sure) why this isn't in simard
End of explanation
s.s.ISDSS[galfitnogim]
print sum(s.s.ISDSS[galfitnogim] == -1)
Explanation: Galaxies not in SDSS phot catalog
End of explanation
galfitsdssnogim = galfitnogim & (s.s.ISDSS != -1)
sum(galfitsdssnogim)
s.s.NSAID[galfitsdssnogim]
Explanation: Galaxies in SDSS but no B/T fit
End of explanation
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.table import Table
try:
    # Python 3.x
    from urllib.parse import urlencode
    from urllib.request import urlretrieve
except ImportError:
    # Python 2.x
    from urllib import urlencode
    from urllib import urlretrieve
import IPython.display
r = 22.5 - 2.5*log10(s.s.NMGY[:,4])
flag = galfitnogim & (r >= 14.) & (r <= 18.)
print sum(flag)
ra = s.s.RA[flag]
dec = s.s.DEC[flag]
ids = s.s.NSAID[flag]
coords = SkyCoord(ra*u.deg, dec*u.deg, frame='icrs')
testcoord = coords[0]
impix = 100
imsize = 1*u.arcmin
cutoutbaseurl = 'http://skyservice.pha.jhu.edu/DR12/ImgCutout/getjpeg.aspx'
for i in range(len(coords.ra)):
    query_string = urlencode(dict(ra=coords[i].ra.deg, 
                                     dec=coords[i].dec.deg, 
                                     width=impix, height=impix, 
                                     scale=imsize.to(u.arcsec).value/impix))
    url = cutoutbaseurl + '?' + query_string
    # this downloads the image to your disk
    urlretrieve(url, 'images/'+str(ids[i])+'_SDSS_cutout.jpg')
    print 'NSAID %i (%10.6f, %10.6f)'%(ids[i],ra[i],dec[i])    
    t = IPython.display.Image('images/'+str(ids[i])+'_SDSS_cutout.jpg')
    IPython.display.display(t)
for i in range(10,len(ids)):
    print '* NSAID %i (%10.6f, %10.6f)'%(ids[i],ra[i],dec[i])  
    print 'http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=%.5f&dec=%.5f'%(ra[i],dec[i])
Explanation: Download SDSS Images
End of explanation
for i in range(len(coords.ra)):
    query_string = urlencode(dict(ra=coords[i].ra.deg, 
                                     dec=coords[i].dec.deg, 
                                     width=impix, height=impix, 
                                     scale=imsize.to(u.arcsec).value/impix))
    url = cutoutbaseurl + '?' + query_string
    # this downloads the image to your disk
    urlretrieve(url, 'images/'+str(nsaids[i])+'_SDSS_cutout.jpg')
    print i, nsaids[i],coords[i].ra,coords[i].dec
    print 'NSAID %i (%10.6f, %10.6f)'%(nsaids[i],coords[i].ra.deg,coords[i].dec)    
    t = IPython.display.Image('images/'+str(nsaids[i])+'_SDSS_cutout.jpg')
    IPython.display.display(t)
for i in range(len(coords.ra)):
    print 'NSAID %i (%10.6f, %10.6f)'%(ids[i],ra[i],dec[i])    
ids = where(galfitnogim & (s.s.ISDSS == -1))
print ids
Explanation: NSAID 69538 (244.060699,  34.258434)
http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=244.06070&dec=34.25843
too faint according to DR7 catalog 
NSAID 18158 (219.578888,   3.639615)
http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=219.57889&dec=3.63961
PSF_FLUX_INTERP INTERP_CENTER BAD_MOVING_FIT BINNED1 NOTCHECKED SATURATED INTERP COSMIC_RAY CHILD
(saturated)
NSAID 165409 (220.235657,   3.527517)
http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=220.23566&dec=3.52752
DEBLEND_NOPEAK INTERP_CENTER BINNED1 DEBLENDED_AS_PSF INTERP CHILD
(blended)
NSAID 68283 (242.047577,  24.507439)
http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=242.04758&dec=24.50744
not in DR7
NSAID 68286 (241.749313,  24.160772)
http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=241.74931&dec=24.16077
STATIONARY MOVED BINNED1 INTERP COSMIC_RAY NOPETRO NODEBLEND CHILD BLENDED
NOT IN SIMARD
(too faint maybe?)
NSAID 68299 (240.918945,  24.602676)
http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=240.91895&dec=24.60268
STATIONARY BINNED1 INTERP COSMIC_RAY CHILD
(not sure)
NOT IN SIMARD
NSAID 68342 (241.297867,  24.960102)
http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=241.29787&dec=24.96010
TOO_FEW_GOOD_DETECTIONS PSF_FLUX_INTERP INTERP_CENTER BINNED1 DEBLENDED_AS_PSF INTERP NOPETRO CHILD
BAD COORDS in DR7?  Image on website above does not match with galaxy.
NSAID 166124 (230.213974,   8.623065)
http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=230.21397&dec=8.62306
BINNED1 INTERP CHILD
NOT IN SIMARD CAT
(not sure)
NSAID 146012 (229.295181,   6.941795)
http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=229.29518&dec=6.94179
BINNED1 NOTCHECKED SATURATED INTERP COSMIC_RAY CHILD
(saturated)
NSAID 166042 (228.910904,   8.302397)
http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=228.91090&dec=8.30240
DEBLEND_DEGENERATE PSF_FLUX_INTERP BAD_MOVING_FIT MOVED BINNED1 INTERP COSMIC_RAY NODEBLEND CHILD BLENDED
NOT IN SIMARD
(not sure)
NSAID 142819 (195.169479,  28.519848)
http://cas.sdss.org/dr7/en/tools/explore/obj.asp?ra=195.16948&dec=28.51985
DEBLENDED_AT_EDGE BAD_MOVING_FIT BINNED1 DEBLENDED_AS_PSF NOTCHECKED INTERP NODEBLEND CHILD BLENDED EDGE
(too faint, blended)
End of explanation
lcs = fits.getdata('/Users/rfinn/research/LocalClusters/NSAmastertables/LCS_all_size.fits')
gim = fits.getdata('/Users/rfinn/research/SimardSDSS2011/table1.fits')virgocat = SkyCoord(vdat.RA*u.degree,vdat.DEC*u.degree,frame='icrs')
from astropy.coordinates import SkyCoord
from astropy import units as u
%matplotlib inline
lcat = SkyCoord(lcs.RA*u.degree,lcs.DEC*u.degree,frame='icrs')
gcat = SkyCoord(gim._RAJ2000*u.degree,gim._DEJ2000*u.degree,frame='icrs')
index,dist2d,dist3d = lcat.match_to_catalog_sky(gcat)
plt.figure()
plt.plot
# only keep matches with matched RA and Dec w/in 1 arcsec
matchflag = dist2d.degree < 3./3600
matchedarray1=np.zeros(len(lcat),dtype=gim.dtype)
matchedarray1[matchflag] = gim[index[matchflag]]
print 'percent of LCS galaxies matched = %.1f'%(sum(matchflag)*1./len(matchflag)*100.)
# get rid of names that start with __
# these cause trouble in the analysis program
t = []
for a in matchedarray1.dtype.names:
    t.append(a)
for i in range(len(t)):
    if t[i].startswith('__'):
        t[i] = t[i][2:]
t = tuple(t)
#print t
matchedarray1.dtype.names = t
outfile = '/Users/rfinn/research/LocalClusters/NSAmastertables/LCS_all.gim2d.tab1.fits'
fits.writeto(outfile,matchedarray1,overwrite=True)
diff = (lcs.B_T_r - matchedarray1['B_T_r'])
bad_matches = (abs(diff) > .01) & matchflag
print 'number of bad matches = ',sum(bad_matches)
s.s.NSAID[bad_matches]
plt.figure()
plt.plot(lcs.RA[bad_matches],lcs.DEC[bad_matches],'ko')
print lcs.CLUSTER[bad_matches]
print sum(s.galfitflag[bad_matches])
print sum(diff < 0.)
outfile = '/Users/rfinn/research/LocalClusters/NSAmastertables/LCS_all.gim2d.tab1.fits'
gdat = fits.getdata(outfile)
gdat.__B_T_r
Explanation: NSAID 143514 (202.163284,  11.387049)
(too bright)
NSAID 163615 (202.697357,  11.200765)
(too bright)
NSAID 146832 (243.526657,  34.870651)
BINNED1 SATURATED INTERP COSMIC_RAY CHILD
(saturated)
NSAID 146875 (244.288803,  34.878895)
DEBLENDED_AT_EDGE BINNED1 NOTCHECKED INTERP CHILD EDGE
r = 13.92
(too bright)
NSAID 165409 (220.235657,   3.527517)
DEBLEND_NOPEAK INTERP_CENTER BINNED1 DEBLENDED_AS_PSF INTERP CHILD
r = 18.82
(deblended and too faint)
NSAID 166699 (241.500229,  22.641125)
BAD_MOVING_FIT MOVED BINNED1 INTERP COSMIC_RAY CHILD
r = 15.01, ext_r = 0.18
(not sure)
NSAID 146638 (241.287476,  17.729904)
STATIONARY BINNED1 SATURATED INTERP COSMIC_RAY CHILD
r = 13.57, ext_r = .13
(too bright, saturated)
NSAID 146659 (241.416641,  18.055758)
STATIONARY BINNED1 INTERP COSMIC_RAY CHILD
r = 14.52, ext_r = 0.14
(not sure)
NSAID 146664 (241.435760,  17.715572)
STATIONARY MOVED BINNED1 INTERP COSMIC_RAY CHILD
r = 14.65, ext_r = 0.13
(not sure)
not in simard
NSAID 140139 (175.954529,  19.968401)
DEBLEND_DEGENERATE PSF_FLUX_INTERP DEBLENDED_AT_EDGE BAD_MOVING_FIT MOVED BINNED1 INTERP COSMIC_RAY NODEBLEND CHILD BLENDED
r = 13.78, ext_r = 0.06
(too bright)
NSAID 140160 (176.071716,  20.223295)
STATIONARY BINNED1 INTERP COSMIC_RAY CHILD
r = 13.90, ext_r = 0.06
(too bright) 
NSAID 140174 (176.204865,  19.795046)
STATIONARY BINNED1 INTERP COSMIC_RAY CHILD
r = 13.18, ext_r = 0.07
(too bright)
NSAID 140175 (176.195999,  20.125084)
STATIONARY BINNED1 INTERP COSMIC_RAY CHILD
r = 13.38, ext_r = 0.07
not in simard
NSAID 140187 (176.228104,  20.017143)
STATIONARY BINNED1 INTERP CHILD
r = 16.19, ext_r = 0.08
(not sure)
not in simard
NSAID 146094 (230.452133,   8.410197)
STATIONARY BINNED1 CHILD
r = 15.86, ext_r = 0.10
(not sure)
IN SIMARD!!!
NSAID 146121 (230.750824,   8.465475)
MOVED BINNED1 INTERP COSMIC_RAY CHILD
r = 15.80, ext_r = 0.09
NSAID 146127 (230.785812,   8.334576)
PSF_FLUX_INTERP INTERP_CENTER STATIONARY BINNED1 INTERP NOPETRO NODEBLEND CHILD BLENDED
r = 17.20, ext_r = 0.09
(blended?)
NSAID 146130 (230.800995,   8.549866)
BINNED1 INTERP COSMIC_RAY CHILD
r = 15.36, ext_r = 0.09
(not sure, maybe blended?)
NSAID 145965 (228.749756,   6.804669)
STATIONARY MOVED BINNED1 INTERP COSMIC_RAY NOPETRO CHILD
r = 16.70, ext_r = 0.10
(no petro)
NSAID 145984 (229.076614,   6.803605)
BINNED1 INTERP COSMIC_RAY CHILD
r = 15.72, ext_r = 0.1
(not sure)
NSAID 145998 (229.185364,   7.021626)
NSAID 145999 (229.187805,   7.055664)
NSAID 146012 (229.295181,   6.941795)
NSAID 146041 (229.713806,   6.435888)
NSAID 166042 (228.910904,   8.302397)
NSAID 166044 (228.936951,   6.958703)
NSAID 166083 (229.217957,   6.539137)
NSAID 142797 (195.073654,  27.955275)
NSAID 142819 (195.169479,  28.519848)
NSAID 142833 (195.214752,  28.042875)
NSAID 162838 (195.280670,  28.121592)
Oh No!
seems like galaxies that are in simard are not in my catalog :(
Going to read in my catalog
read in simard catalog
match them 
and then see what's going on
End of explanation
<END_TASK>