Coverage for marshallEngine/services/panstarrs_location_stamps.py : 26%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/local/bin/python
2# encoding: utf-8
3"""
4*generate colour panstarrs location stamps for regions of transients*
6:Author:
7 David Young
8"""
9from builtins import object
10import sys
11import os
12os.environ['TERM'] = 'vt100'
13from fundamentals import tools
14from fundamentals.mysql import readquery, writequery
15from panstamps.downloader import downloader
16from panstamps.image import image
19class panstarrs_location_stamps(object):
20 """
21 *The worker class for the panstarrs_location_stamps module*
23 **Key Arguments**
25 - ``log`` -- logger
26 - ``settings`` -- the settings dictionary
27 - ``dbConn`` -- dbConn
28 - ``transientId`` -- will download for one transient if single ID given. Default *None*
31 **Usage**
33 To setup your logger, settings and database connections, please use the ``fundamentals`` package (`see tutorial here <http://fundamentals.readthedocs.io/en/latest/#tutorial>`_).
35 To initiate a panstarrs_location_stamps object, use the following:
37 ```python
38 from marshallEngine.services import panstarrs_location_stamps
39 ps_stamp = panstarrs_location_stamps(
40 log=log,
41 settings=settings,
42 dbConn=dbConn,
43 transientId=None
44 ).get()
45 ```
47 """
49 def __init__(
50 self,
51 log,
52 dbConn,
53 transientId=None,
54 settings=False
55 ):
56 self.log = log
57 log.debug("instansiating a new 'panstarrs_location_stamps' object")
58 self.settings = settings
59 self.dbConn = dbConn
60 self.transientId = transientId
62 # xt-self-arg-tmpx
64 return None
66 def get(self):
67 """
68 *get the panstarrs_location_stamps object*
69 """
70 self.log.debug('starting the ``get`` method')
72 # FOR A SINGLE TRANSIENT
73 if self.transientId:
74 transientId = self.transientId
75 sqlQuery = u"""
76 select t.transientBucketId, t.raDeg,t.decDeg from pesstoObjects p, transientBucketSummaries t where p.transientBucketId = t.transientBucketId and t.transientBucketId = %(transientId)s;
77 """ % locals()
78 # OR THE NEXT 200 TRANSIENTS NEEDING STAMPS
79 else:
80 # GET NEXT 200 TRANSIENTS NEEDING PANSTARRS STAMPS
81 sqlQuery = u"""
82 select * from pesstoObjects p, transientBucketSummaries t where (ps1_map is null or ps1_map not in (0,1)) and p.transientBucketId = t.transientBucketId order by t.transientBucketId desc limit 200;
83 """ % locals()
84 rows = readquery(
85 log=self.log,
86 sqlQuery=sqlQuery,
87 dbConn=self.dbConn
88 )
90 # FOR EACH TRANSIENT DOWNLOAD STAMP TO CACHE DIRECTORY
91 downloadDirectoryPath = self.settings[
92 "cache-directory"] + "/transients/"
94 for row in rows:
95 transientBucketId = row["transientBucketId"]
96 downloadPath = f"{downloadDirectoryPath}/{transientBucketId}"
97 ra = row["raDeg"]
98 dec = row["decDeg"]
100 fitsPaths, jpegPaths, colorPath = downloader(
101 log=self.log,
102 settings=self.settings,
103 downloadDirectory=downloadPath,
104 fits=False,
105 jpeg=False,
106 arcsecSize=60,
107 filterSet='gri',
108 color=True,
109 singleFilters=False,
110 ra=ra,
111 dec=dec,
112 imageType="stack" # warp | stack
113 ).get()
115 # CHECK FOR FAILED IMAGES AND FLAG IN DATABASE
116 if len(colorPath) == 0 or not colorPath[0]:
117 sqlQuery = u"""
118 update pesstoObjects p, transientBucketSummaries t set p.ps1_map = 0 where p.transientBucketId=t.transientBucketId and (ps1_map is null or ps1_map != 0) and t.decDeg < -40;
119 update pesstoObjects set ps1_map = 2 where transientBucketId = %(transientBucketId)s and ps1_map is null;
120 update pesstoObjects set ps1_map = 2+ps1_map where transientBucketId = %(transientBucketId)s and ps1_map is not null;
121 update pesstoObjects set ps1_map = 0 where transientBucketId = %(transientBucketId)s and ps1_map > 10;
122 """ % locals()
123 writequery(
124 log=self.log,
125 sqlQuery=sqlQuery,
126 dbConn=self.dbConn
127 )
128 continue
130 source = colorPath[0]
131 destination = downloadPath + "/ps1_map_color.jpeg"
132 try:
133 os.rename(source, destination)
135 # DOWNLOAD THE COLOR IMAGE
136 myimage = image(
137 log=self.log,
138 settings=self.settings,
139 imagePath=destination,
140 arcsecSize=60,
141 crosshairs=True,
142 transient=False,
143 scale=True,
144 invert=False,
145 greyscale=False
146 ).get()
148 # UPDATE DATABASE FLAG
149 sqlQuery = u"""
150 update pesstoObjects set ps1_map = 1 where transientBucketId = %(transientBucketId)s
151 """ % locals()
153 writequery(
154 log=self.log,
155 sqlQuery=sqlQuery,
156 dbConn=self.dbConn
157 )
158 except:
159 self.log.warning(
160 "Could not process the image %(destination)s" % locals())
162 self.log.debug('completed the ``get`` method')
163 return None