Hide keyboard shortcuts

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*The HTML template module for the `models_transients_element_post.py` resource* 

5 

6:Author: 

7 David Young 

8""" 

9from builtins import zip 

10from builtins import object 

11import sys 

12import os 

13import khufu 

14from marshallEngine.housekeeping import update_transient_summaries 

15from astrocalc.times import conversions 

16from fundamentals import times 

17from fundamentals.logs import emptyLogger 

18 

19 

20class models_transients_element_post(object): 

21 """ 

22 The worker class for the models_transients_element_post module 

23 

24 **Key Arguments** 

25 

26 - ``log`` -- logger 

27 - ``request`` -- the pyramid request 

28 - ``elementId`` -- the specific element id requests (or False) 

29 """ 

30 

31 def __init__( 

32 self, 

33 log, 

34 request 

35 ): 

36 self.log = log 

37 self.request = request 

38 self.transientBucketId = request.matchdict["elementId"] 

39 self.response = "" 

40 # xt-self-arg-tmpx 

41 

42 log.debug( 

43 "instansiating a new 'models_transients_element_post' object") 

44 

45 return None 

46 

47 def close(self): 

48 del self 

49 return None 

50 

51 def post(self): 

52 """execute the post method on the models_transients_element_post object 

53 

54 **Return** 

55 

56 - ``response`` -- the reponse to send to the browser 

57 """ 

58 self.log.debug('starting the ``post`` method') 

59 

60 # CHECK THE RIGHT KEYWORDS ARE PASSED IN THE QUERY STRING 

61 if set(("clsObsdate", "clsSource")) <= set(self.request.params): 

62 self._add_transient_classification() 

63 

64 # IF THE RIGHT KEYWORDS ARE NOT PASSED THROW A WARNING 

65 if len(self.response) == 0: 

66 self.response = "no action was performed" 

67 

68 self.log.debug('completed the ``post`` method') 

69 return self.response 

70 

71 def _add_transient_classification( 

72 self): 

73 """ add transient classification 

74 """ 

75 self.log.debug('starting the ``_add_transient_classification`` method') 

76 

77 transientBucketId = self.transientBucketId 

78 

79 # ASTROCALC CONVERTER 

80 converter = conversions( 

81 log=self.log 

82 ) 

83 

84 # FIRST SELECT OUT A ROW FROM THE TRANSIENTBUCKET AS A TEMPLATE FOR THE 

85 # CLASSIFICATION 

86 sqlQuery = """ 

87 select p.classifiedFlag, t.raDeg, t.decDeg, t.name from transientBucket t, pesstoObjects p where replacedByRowId = 0 and t.transientBucketId = %(transientBucketId)s and t.transientBucketId=p.transientBucketId limit 1 

88 """ % locals() 

89 

90 rowsTmp = self.request.db.execute(sqlQuery).fetchall() 

91 rows = [] 

92 rows[:] = [dict(list(zip(list(row.keys()), row))) for row in rowsTmp] 

93 

94 # ADD VARIABLES TO a 

95 params = dict(list(self.request.params.items())) 

96 params["now"] = times.get_now_sql_datetime() 

97 params["transientBucketId"] = self.transientBucketId 

98 for row in rows: 

99 params["raDeg"] = row["raDeg"] 

100 params["decDeg"] = row["decDeg"] 

101 params["name"] = row["name"] 

102 params["classifiedFlag"] = row["classifiedFlag"] 

103 

104 # CONVERT DATE TO MJD 

105 params["obsMjd"] = converter.ut_datetime_to_mjd( 

106 utDatetime="%(clsObsdate)sT00:00:00.0" % params) 

107 

108 # ADD SOME DEFAULT VALUES / NULL VALUES 

109 if "clsPeculiar" in params: 

110 params[ 

111 "clsSnClassification"] = "%(clsSnClassification)s-p" % params 

112 if params["clsType"] == "supernova": 

113 params["clsType"] = params["clsSnClassification"] 

114 if "clsRedshift" not in params or len(params["clsRedshift"]) == 0: 

115 params["clsRedshift"] = "null" 

116 if "clsClassificationWRTMax" not in params: 

117 params["clsClassificationWRTMax"] = "unknown" 

118 if "clsClassificationPhase" not in params or len(params["clsClassificationPhase"]) == 0: 

119 params["clsClassificationPhase"] = "null" 

120 

121 params["username"] = self.request.authenticated_userid 

122 

123 # INSERT THE NEW CLASSIFICATION ROW INTO THE TRANSIENTBUCKET 

124 sqlQuery = """ 

125 INSERT IGNORE INTO transientBucket (raDeg, decDeg, name, transientBucketId, observationDate, observationMjd, survey, spectralType, transientRedshift, classificationWRTMax, classificationPhase, reducer) VALUES(%(raDeg)s, %(decDeg)s, "%(name)s", %(transientBucketId)s, "%(clsObsdate)s", %(obsMjd)s, "%(clsSource)s", "%(clsType)s", %(clsRedshift)s, "%(clsClassificationWRTMax)s", %(clsClassificationPhase)s, "%(username)s") ON DUPLICATE KEY UPDATE raDeg = values(raDeg), decDeg = values(decDeg), name = values(name), transientBucketId = values(transientBucketId), observationDate = values(observationDate), observationMjd = values(observationMjd), survey = values(survey), spectralType = values(spectralType), transientRedshift = values(transientRedshift), classificationWRTMax = values(classificationWRTMax), classificationPhase = values(classificationPhase), reducer=values(reducer); 

126 """ % params 

127 

128 self.request.db.execute(sqlQuery) 

129 self.request.db.commit() 

130 

131 # UPDATE THE OBJECT'S LOCATION IN THE VARIOUS MARSHALL WORKFLOWS 

132 if self.request.params["clsSendTo"].lower() == "yes": 

133 awl = "queued for atel" 

134 else: 

135 awl = "archived without alert" 

136 

137 if params["classifiedFlag"] == 1: 

138 sqlQuery = """ 

139 update pesstoObjects set snoozed = 0, alertWorkflowLocation = "%(awl)s" where transientBucketId = %(transientBucketId)s 

140 """ % locals() 

141 else: 

142 sqlQuery = """ 

143 update pesstoObjects set classifiedFlag = 1, snoozed = 0, marshallWorkflowLocation = "review for followup", alertWorkflowLocation = "%(awl)s" where transientBucketId = %(transientBucketId)s 

144 """ % locals() 

145 self.request.db.execute(sqlQuery) 

146 self.request.db.commit() 

147 

148 dbConn = self.request.registry.settings["dbConn"] 

149 # RECONNECT IF CONNECT WAS LOST 

150 dbConn.ping(reconnect=True) 

151 updater = update_transient_summaries( 

152 log=emptyLogger(), 

153 settings=self.request.registry.settings["yaml settings"], 

154 dbConn=dbConn, 

155 transientBucketId=transientBucketId 

156 ).update() 

157 

158 self.response = self.response + \ 

159 "Add a classification to transient #%(transientBucketId)s " % locals( 

160 ) 

161 

162 self.log.debug( 

163 'completed the ``_add_transient_classification`` method') 

164 return None 

165 

166 # xt-class-method