def gettargetid(_name,_ra,_dec,conn,_radius=.01,verbose=False): import lsc from numpy import argmin if _name: _name = _name.lower().replace('at20', 'at20%').replace('sn20', 'sn20%').replace(' ', '%') command=['select distinct(r.name), r.targetid, t.id, t.ra0, t.dec0 from targetnames as r join targets as t where r.name like "'+\ _name +'" and t.id=r.targetid'] lista=lsc.mysqldef.query(command,conn) elif _ra and _dec: if ':' in str(_ra): _ra,_dec=lsc.deg2HMS(_ra,_dec) lista=lsc.mysqldef.getfromcoordinate(conn, 'targets', _ra, _dec,_radius) if verbose: print _ra,_dec,_radius print lista if lista: ll0={} for jj in lista[0].keys(): ll0[jj]=[] for i in range(0,len(lista)): for jj in lista[0].keys(): ll0[jj].append(lista[i][jj]) ########################################### if len(set(ll0['id']))==1: _targetid=ll0['id'][0] elif len(set(ll0['id']))>1: if 'hsine' in ll0.keys(): _targetid=ll0['id'][argmin(ll0['hsine'])] else: _targetid='' else: _targetid='' else: _targetid='' if verbose: print 'no objects' return _targetid
def gettargetid(_name, _ra, _dec, conn, _radius=.01, verbose=False): import lsc from numpy import argmin if _name: _name = _name.lower().replace('at20', 'at20%').replace( 'sn20', 'sn20%').replace(' ', '%') command=['select distinct(r.name), r.targetid, t.id, t.ra0, t.dec0 from targetnames as r join targets as t where r.name like "'+\ _name +'" and t.id=r.targetid'] lista = lsc.mysqldef.query(command, conn) elif _ra and _dec: if ':' in str(_ra): _ra, _dec = lsc.deg2HMS(_ra, _dec) lista = lsc.mysqldef.getfromcoordinate(conn, 'targets', _ra, _dec, _radius) if verbose: print _ra, _dec, _radius print lista if lista: ll0 = {} for jj in lista[0].keys(): ll0[jj] = [] for i in range(0, len(lista)): for jj in lista[0].keys(): ll0[jj].append(lista[i][jj]) ########################################### if len(set(ll0['id'])) == 1: _targetid = ll0['id'][0] elif len(set(ll0['id'])) > 1: if 'hsine' in ll0.keys(): _targetid = ll0['id'][argmin(ll0['hsine'])] else: _targetid = '' else: _targetid = '' else: _targetid = '' if verbose: print 'no objects' return _targetid
def targimg(img='', hdrt=None): import lsc from lsc.util import readkey3, readhdr from lsc.mysqldef import getfromcoordinate from lsc import conn import string _targetid = '' _group = '' if hdrt is None: hdrt = lsc.util.readhdr(img) if ('CAT-RA' in hdrt and 'CAT-DEC' in hdrt and hdrt['CAT-RA'] not in lsc.util.missingvalues and hdrt['CAT-DEC'] not in lsc.util.missingvalues): _ra = lsc.util.readkey3(hdrt, 'CAT-RA') _dec = lsc.util.readkey3(hdrt, 'CAT-DEC') elif ('RA' in hdrt and 'DEC' in hdrt and hdrt['RA'] not in lsc.util.missingvalues and hdrt['DEC'] not in lsc.util.missingvalues): _ra = lsc.util.readkey3(hdrt, 'RA') _dec = lsc.util.readkey3(hdrt, 'DEC') else: _ra = None _dec = None _object = lsc.util.readkey3(hdrt, 'object') if ':' in str(_ra): _ra, _dec = lsc.deg2HMS(_ra, _dec) ############# define groupid ################ aaa = lsc.mysqldef.query(['select idcode, groupidcode from programs'], conn) grname = [i['idcode'] for i in aaa] gr = [i['groupidcode'] for i in aaa] if lsc.util.readkey3(hdrt, 'propid') in grname: if gr[grname.index(lsc.util.readkey3(hdrt, 'propid'))] not in [1, None]: _group = gr[grname.index(lsc.util.readkey3(hdrt, 'propid'))] else: _group = 32769 else: _group = 32769 ############################################## ######## define targetid ################## _targetid = lsc.mysqldef.gettargetid(_object, '', '', conn, .01, False) if not _targetid: print '# no target with this name ' + _object _targetid = lsc.mysqldef.gettargetid('', _ra, _dec, conn, .01, False) if _targetid: print '# target at this coordinate with a different name, add name ' + str( _ra) + ' ' + str(_dec) dictionary1 = { 'name': _object, 'targetid': _targetid, 'groupidcode': _group } lsc.mysqldef.insert_values(conn, 'targetnames', dictionary1) bb2 = lsc.mysqldef.query( ['select id from targetnames where name = "' + _object + '"'], conn) dictionary3 = { 'userid': 67, 'tablemodified': 'targetnames', 'idmodified': bb2[0]['id'], 'columnmodified': 'New Row', 'newvalue': 'Multiple' } lsc.mysqldef.insert_values(conn, 'useractionlog', dictionary3) else: print 'not found targetid with ra and dec ' + str(_ra) + ' ' + str( _dec) else: print 'found name= ' + _object + ' targetid= ' + str(_targetid) ############################################## if not _targetid: # no target print 'add new target ' + str(_ra) + ' ' + str(_dec) + ' ' + _object dictionary = {'ra0': _ra, 'dec0': _dec} lsc.mysqldef.insert_values(conn, 'targets', dictionary) bb = lsc.mysqldef.getfromcoordinate(conn, 'targets', _ra, _dec, .000156) dictionary2 = { 'userid': 67, 'tablemodified': 'targets', 'idmodified': bb[0]['id'], 'columnmodified': 'New Row', 'newvalue': 'Multiple' } lsc.mysqldef.insert_values(conn, 'useractionlog', dictionary2) dictionary1 = { 'name': _object, 'targetid': bb[0]['id'], 'groupidcode': _group } lsc.mysqldef.insert_values(conn, 'targetnames', dictionary1) bb2 = lsc.mysqldef.query( ['select id from targetnames where name = "' + _object + '"'], conn) dictionary3 = { 'userid': 67, 'tablemodified': 'targetnames', 'idmodified': bb2[0]['id'], 'columnmodified': 'New Row', 'newvalue': 'Multiple' } lsc.mysqldef.insert_values(conn, 'useractionlog', dictionary3) _targetid = bb[0]['id'] print '\n add a target ' + str(_ra) + ' ' + str(_dec) + ' ' + str( bb[0]['id']) if _targetid and _group: cc = lsc.mysqldef.getfromdataraw(conn, 'permissionlog', 'targetid', str(_targetid), column2='groupname') if len(cc) == 0: _JDn = lsc.mysqldef.JDnow() print img dictionary2 = { 'targetid': _targetid, 'jd': _JDn, 'groupname': _group } lsc.mysqldef.insert_values(conn, 'permissionlog', dictionary2) return _targetid
def targimg(img='', hdrt=None): import lsc from lsc.util import readkey3, readhdr from lsc.mysqldef import getfromcoordinate from lsc import conn import string import os, json, requests _targetid = '' _group = '' if hdrt is None: hdrt = lsc.util.readhdr(img) _ra = lsc.util.readkey3(hdrt, 'CAT-RA') _dec = lsc.util.readkey3(hdrt, 'CAT-DEC') if _ra is None or _dec is None: # No CAT RA or dec, so send a warning message to slack and return exception # Send Slack message post_url = os.environ['SLACK_CHANNEL_WEBHOOK'] payload = { 'text': 'CAT-RA and CAT-DEC could not be found for {}'.format(img) } json_data = json.dumps(payload) headers = {'Content-Type': 'application/json'} response = requests.post(post_url, data=json_data.encode('ascii'), headers=headers) # Raise exception so pipeline moves on to ingesting the next image raise Exception( 'No CAT-RA or CAT-DEC could be found for {}'.format(img)) _object = lsc.util.readkey3(hdrt, 'object') if ':' in str(_ra): _ra, _dec = lsc.deg2HMS(_ra, _dec) ############# define groupid ################ aaa = lsc.mysqldef.query(['select idcode, groupidcode from programs'], conn) grname = [i['idcode'] for i in aaa] gr = [i['groupidcode'] for i in aaa] if lsc.util.readkey3(hdrt, 'propid') in grname: if gr[grname.index(lsc.util.readkey3(hdrt, 'propid'))] not in [1, None]: _group = gr[grname.index(lsc.util.readkey3(hdrt, 'propid'))] else: _group = 32769 else: _group = 32769 ############################################## ######## define targetid ################## _targetid = lsc.mysqldef.gettargetid(_object, '', '', conn, .01, False) if not _targetid: print '# no target with this name ' + _object _targetid = lsc.mysqldef.gettargetid('', _ra, _dec, conn, .01, False) if _targetid: print '# target at this coordinate with a different name, add name ' + str( _ra) + ' ' + str(_dec) dictionary1 = { 'name': _object, 'targetid': _targetid, 'groupidcode': _group } lsc.mysqldef.insert_values(conn, 'targetnames', dictionary1) bb2 = lsc.mysqldef.query( ['select id from targetnames where name = "' + _object + '"'], conn) dictionary3 = { 'userid': 67, 'tablemodified': 'targetnames', 'idmodified': bb2[0]['id'], 'columnmodified': 'New Row', 'newvalue': 'Multiple' } lsc.mysqldef.insert_values(conn, 'useractionlog', dictionary3) else: print 'not found targetid with ra and dec ' + str(_ra) + ' ' + str( _dec) else: print 'found name= ' + _object + ' targetid= ' + str(_targetid) ############################################## if not _targetid: # no target print 'add new target ' + str(_ra) + ' ' + str(_dec) + ' ' + _object dictionary = {'ra0': _ra, 'dec0': _dec} lsc.mysqldef.insert_values(conn, 'targets', dictionary) bb = lsc.mysqldef.getfromcoordinate(conn, 'targets', _ra, _dec, .000156) dictionary2 = { 'userid': 67, 'tablemodified': 'targets', 'idmodified': bb[0]['id'], 'columnmodified': 'New Row', 'newvalue': 'Multiple' } lsc.mysqldef.insert_values(conn, 'useractionlog', dictionary2) dictionary1 = { 'name': _object, 'targetid': bb[0]['id'], 'groupidcode': _group } lsc.mysqldef.insert_values(conn, 'targetnames', dictionary1) bb2 = lsc.mysqldef.query( ['select id from targetnames where name = "' + _object + '"'], conn) dictionary3 = { 'userid': 67, 'tablemodified': 'targetnames', 'idmodified': bb2[0]['id'], 'columnmodified': 'New Row', 'newvalue': 'Multiple' } lsc.mysqldef.insert_values(conn, 'useractionlog', dictionary3) _targetid = bb[0]['id'] print '\n add a target ' + str(_ra) + ' ' + str(_dec) + ' ' + str( bb[0]['id']) if _targetid and _group: cc = lsc.mysqldef.getfromdataraw(conn, 'permissionlog', 'targetid', str(_targetid), column2='groupname') if len(cc) == 0: _JDn = lsc.mysqldef.JDnow() print img dictionary2 = { 'targetid': _targetid, 'jd': _JDn, 'groupname': _group } lsc.mysqldef.insert_values(conn, 'permissionlog', dictionary2) return _targetid
def targimg(img='', hdrt=None): import lsc from lsc.util import readkey3,readhdr from lsc.mysqldef import getfromcoordinate from lsc import conn import string _targetid='' _group='' if hdrt is None: hdrt=lsc.util.readhdr(img) if ('CAT-RA' in hdrt and 'CAT-DEC' in hdrt and hdrt['CAT-RA'] not in lsc.util.missingvalues and hdrt['CAT-DEC'] not in lsc.util.missingvalues): _ra=lsc.util.readkey3(hdrt,'CAT-RA') _dec=lsc.util.readkey3(hdrt,'CAT-DEC') elif ('RA' in hdrt and 'DEC' in hdrt and hdrt['RA'] not in lsc.util.missingvalues and hdrt['DEC'] not in lsc.util.missingvalues): _ra=lsc.util.readkey3(hdrt,'RA') _dec=lsc.util.readkey3(hdrt,'DEC') else: _ra = None _dec = None _object=lsc.util.readkey3(hdrt,'object') if ':' in str(_ra): _ra,_dec=lsc.deg2HMS(_ra,_dec) ############# define groupid ################ aaa=lsc.mysqldef.query(['select idcode, groupidcode from programs'],conn) grname=[i['idcode'] for i in aaa] gr=[i['groupidcode'] for i in aaa] if lsc.util.readkey3(hdrt,'propid') in grname: if gr[grname.index(lsc.util.readkey3(hdrt,'propid'))] not in [1,None]: _group = gr[grname.index(lsc.util.readkey3(hdrt,'propid'))] else: _group=32769 else: _group=32769 ############################################## ######## define targetid ################## _targetid=lsc.mysqldef.gettargetid(_object,'','',conn,.01,False) if not _targetid: print '# no target with this name '+_object _targetid=lsc.mysqldef.gettargetid('',_ra,_dec,conn,.01,False) if _targetid: print '# target at this coordinate with a different name, add name '+str(_ra)+' '+str(_dec) dictionary1={'name':_object,'targetid':_targetid,'groupidcode':_group} lsc.mysqldef.insert_values(conn,'targetnames',dictionary1) bb2=lsc.mysqldef.query(['select id from targetnames where name = "'+_object+'"'],conn) dictionary3 = {'userid':67, 'tablemodified': 'targetnames', 'idmodified': bb2[0]['id'], 'columnmodified': 'New Row', 'newvalue': 'Multiple'} lsc.mysqldef.insert_values(conn,'useractionlog',dictionary3) else: print 'not found targetid with ra and dec '+str(_ra)+' '+str(_dec) else: print 'found name= '+_object+' targetid= '+str(_targetid) ############################################## if not _targetid: # no target print 'add new target '+str(_ra)+' '+str(_dec)+' '+_object dictionary={'ra0':_ra,'dec0':_dec} lsc.mysqldef.insert_values(conn,'targets',dictionary) bb=lsc.mysqldef.getfromcoordinate(conn, 'targets', _ra, _dec,.000156) dictionary2 = {'userid':67, 'tablemodified': 'targets', 'idmodified': bb[0]['id'], 'columnmodified': 'New Row', 'newvalue': 'Multiple'} lsc.mysqldef.insert_values(conn,'useractionlog',dictionary2) dictionary1 = {'name':_object,'targetid':bb[0]['id'],'groupidcode':_group} lsc.mysqldef.insert_values(conn,'targetnames',dictionary1) bb2=lsc.mysqldef.query(['select id from targetnames where name = "'+_object+'"'],conn) dictionary3 = {'userid':67, 'tablemodified': 'targetnames', 'idmodified': bb2[0]['id'], 'columnmodified': 'New Row', 'newvalue': 'Multiple'} lsc.mysqldef.insert_values(conn,'useractionlog',dictionary3) _targetid=bb[0]['id'] print '\n add a target '+str(_ra)+' '+str(_dec)+' '+str(bb[0]['id']) if _targetid and _group: cc=lsc.mysqldef.getfromdataraw(conn,'permissionlog','targetid', str(_targetid),column2='groupname') if len(cc)==0: _JDn=lsc.mysqldef.JDnow() print img dictionary2={'targetid':_targetid,'jd':_JDn,'groupname':_group} lsc.mysqldef.insert_values(conn,'permissionlog',dictionary2) return _targetid