Science Platforms Exercise 1 Solution

This is the solution notebook for the first part of the exercises in Science Platforms.


In [60]:
import SciServer
from SciServer import CasJobs     # Communicate between SciServer Compute and CasJobs
print('Imported SciServer modules')

import pandas                                # data analysis tools
import numpy as np                           # numerical tools
import matplotlib.pyplot as plt
import skimage.io
from datetime import datetime, timedelta     # date and timestamp tools
from pprint import pprint                    # print human-readable output
print('Imported other needed modules')


Imported SciServer modules
Imported other needed modules

In [61]:
# Run the CROSS APPLY query with the distance between the object and its neighbor included
# Also include the distance that the fGetNearbyObjEq function returns.  To get the nearest
# neighbor, you actually SELECT the object with the smallest distance.
# Run the query in Submit mode in CasJobs, just in case it takes longer than a minute.
# Save the results to a table called MyNbrsDist.
myquery = 'SELECT a.id, a.objid as objid1, b.objid as objid2, \n'
myquery += '    a.ra, a.dec, b.distance \n'
myquery += 'INTO MyDB.MyNbrsDist\n'
myquery += 'FROM MyDB.MyObjs a\n'
myquery += 'CROSS APPLY (SELECT TOP 1 objid, distance \n'
myquery += '     FROM dbo.fGetNearbyObjEq( a.ra, a.dec, 1.0 ) \n'
myquery += '      WHERE objid != a.objID ORDER BY distance) b \n'
myjobid = CasJobs.submitJob(sql=myquery, context="DR15")

In [62]:
# This is just a way to give the job enough time to finish. You will get an error if it has not
# finished because the table wont exist yet.
waited = CasJobs.waitForJob(jobId=myjobid)      # waited is a dummy variable; just print wait msg
jobDescription = CasJobs.getJobStatus(myjobid)

In [65]:
# Now fetch the MyDB table into our result buffer 
myquery = 'SELECT * FROM MyNbrsDist'
result = CasJobs.executeQuery(myquery, 'MyDB')
print(result)

# Set up the parameters for the cutout displays
width=200
height=200
pixelsize=0.396
plt.figure(figsize=(15, 75))
subPlotNum = 1
# Get the number fo rows in the plot if you want to show 5 plots per row
rows = int(len(result)/5)


# We fetch JPEG cutouts from SkyServer for each object in our list
# The size of the cutout is determined from the distance between the object and its neighbor
# The distance returmed by fGetNearbyObjEq is in arcmin, so we multiply it by 60 and use
# twice the distance as the width of the cutout, which is centered on the object's ra,dec.
# Note that 'height' and 'width' give the physical box size of each cutout, and 'scale' is the
# quantity that determines size of the cutout on the sky.
# NOTE: The plots will take a minute or two to show up, for the 100 objs in the data table.

print('Please wait while the cutouts are being genrated ...')
for index,obj in result.iterrows():
    scale=2*60*obj['distance']/pixelsize/width
    # The cutout is obtained with a call to the (DR15) SkyServer ImgCutout web service
    url="http://skyserver.sdss.org/dr15/SkyServerWS/ImgCutout/getjpeg?ra="+str(obj['ra'])
    url+="&dec="+str(obj['dec'])+"&scale="""+str(scale)+"&width="+str(width)
    url+="&height="+str(height)
    img=skimage.io.imread(url)
    plt.subplot(rows,5,subPlotNum)
    subPlotNum += 1
    plt.imshow(img)
    plt.title(int(obj['id']))


     id               objid1               objid2          ra       dec  \
0     1  1237648720693888083  1237648720693887498  180.000338 -0.496883   
1     2  1237648720693887509  1237674649928728803  180.000988 -0.420694   
2     3  1237654669748732845  1237648720693887509  180.001140 -0.420634   
3     4  1237648673430438800  1237648720693887926  180.002399 -0.428579   
4     5  1237648720693887926  1237648720693887519  180.002406 -0.428544   
5     6  1237674649928728804  1237674649928728803  180.002461 -0.414987   
6     7  1237648720693887513  1237648720693887514  180.002649 -0.478290   
7     8  1237648673430438422  1237648720693887513  180.002666 -0.478292   
8     9  1237654669748732471  1237674649928728803  180.002675 -0.415836   
9    10  1237674649928728803  1237674649928728804  180.002690 -0.415836   
10   11  1237648673430438424  1237648720693887514  180.002825 -0.483477   
11   12  1237648720693887514  1237648720693888167  180.002871 -0.483480   
12   13  1237648720693887928  1237648720693887523  180.003481 -0.458611   
13   14  1237648673430438803  1237648720693887928  180.003593 -0.458658   
14   15  1237648720693887519  1237648720693887926  180.004297 -0.428646   
15   16  1237674649928729616  1237674649928728803  180.004834 -0.416011   
16   17  1237648720693887522  1237648720693887538  180.005643 -0.487844   
17   18  1237648673430438804  1237648720693887522  180.005655 -0.487790   
18   19  1237648720693887116  1237648720693887319  180.005763 -0.467156   
19   20  1237648673430438052  1237648720693887116  180.005780 -0.467174   
20   21  1237648720693887523  1237648720693887533  180.005806 -0.462116   
21   22  1237648673430438430  1237648720693887523  180.005820 -0.462128   
22   23  1237648720693887319  1237648720693887531  180.006132 -0.469882   
23   24  1237648673430438215  1237648720693887319  180.006198 -0.469943   
24   25  1237648720693887120  1237648720693887534  180.006741 -0.455471   
25   26  1237648673430438058  1237648720693887120  180.006758 -0.455496   
26   27  1237674649928729921  1237674649928729619  180.006817 -0.403764   
27   28  1237648673430438435  1237648720693887528  180.006907 -0.475144   
28   29  1237648720693887528  1237648720693888203  180.006911 -0.475115   
29   30  1237648673430438434  1237648720693887529  180.007119 -0.499912   
..  ...                  ...                  ...         ...       ...   
70   71  1237648673430438454  1237648720693887549  180.014046 -0.493893   
71   72  1237648673430438455  1237648720693887551  180.014549 -0.455122   
72   73  1237648720693887327  1237648720693887326  180.014601 -0.475379   
73   74  1237648673430438226  1237648720693887327  180.014610 -0.475385   
74   75  1237648720693887551  1237648720693887555  180.014732 -0.455166   
75   76  1237648720693887550  1237648720693887943  180.014816 -0.442443   
76   77  1237648673430438457  1237648720693887550  180.014857 -0.442480   
77   78  1237648720693887555  1237648720693887543  180.014971 -0.460207   
78   79  1237648673430438461  1237648720693887555  180.014982 -0.460193   
79   80  1237648720693887557  1237648720693887558  180.015070 -0.490981   
80   81  1237648673430438462  1237648720693887557  180.015077 -0.491003   
81   82  1237648720693887558  1237648720693887557  180.015139 -0.490002   
82   83  1237648720693888087  1237648720693887131  180.015190 -0.468940   
83   84  1237648673430438932  1237648720693888087  180.015203 -0.468963   
84   85  1237648720693887942  1237648720693887154  180.016173 -0.499410   
85   86  1237648673430438089  1237648720693887942  180.016189 -0.499439   
86   87  1237648720693887940  1237648720693887558  180.016249 -0.488334   
87   88  1237648673430438465  1237648720693887940  180.016308 -0.488417   
88   89  1237648720693887561  1237648720693887549  180.016977 -0.495139   
89   90  1237648720693887142  1237648720693887144  180.017065 -0.427755   
90   91  1237648673430438072  1237648720693887142  180.017084 -0.427775   
91   92  1237648720693887943  1237648720693887550  180.017365 -0.444199   
92   93  1237648673430438811  1237648720693887943  180.017391 -0.444248   
93   94  1237648704038699688  1237674649928729637  180.017425 -0.403458   
94   95  1237674649928729637  1237674649928729932  180.017499 -0.403365   
95   96  1237654669748732722  1237674649928729637  180.017641 -0.403155   
96   97  1237648720693888089  1237648720693887944  180.017752 -0.491006   
97   98  1237648673430438095  1237648720693888089  180.017769 -0.491004   
98   99  1237648720693887563  1237648720693887947  180.017787 -0.478161   
99  100  1237648673430438471  1237648720693887563  180.017798 -0.478239   

    distance  
0   0.190965  
1   0.308838  
2   0.009768  
3   0.002134  
4   0.113625  
5   0.052748  
6   0.311667  
7   0.001040  
8   0.000920  
9   0.052748  
10  0.002763  
11  0.292192  
12  0.252330  
13  0.007278  
14  0.113625  
15  0.129085  
16  0.225000  
17  0.003310  
18  0.165004  
19  0.001447  
20  0.180873  
21  0.001150  
22  0.114510  
23  0.005459  
24  0.259466  
25  0.001826  
26  0.255495  
27  0.001740  
28  0.231141  
29  0.002309  
..       ...  
70  0.003660  
71  0.011316  
72  0.187651  
73  0.000634  
74  0.302787  
75  0.185730  
76  0.003311  
77  0.220289  
78  0.001029  
79  0.058872  
80  0.001386  
81  0.058872  
82  0.183312  
83  0.001636  
84  0.183031  
85  0.001945  
86  0.120184  
87  0.006135  
88  0.194711  
89  0.098147  
90  0.001678  
91  0.185730  
92  0.003339  
93  0.007114  
94  0.197171  
95  0.015254  
96  0.107157  
97  0.001003  
98  0.134620  
99  0.004702  

[100 rows x 6 columns]
Please wait while the cutouts are being genrated ...

Thank you!

Thanks for reviewing this SciServer example notebook. You can use this notebook as a template to develop your own notebooks, but please do so in a copy rather than in the original example notebook. As you begin to use any of our SciServer modules in your own notebooks, consult the SciServer scripting documentation at http://www.sciserver.org/docs/sciscript-python/SciServer.html (link opens in a new window).

If you have questions, please email the SciServer helpdesk at sciserver-helpdesk@jhu.edu.


In [ ]: