예제 #1
0
import datetime, sys
from class_pg_admin import pg_admin
from communication_client import Client
sys.path.append('object_obs_timewindow')
from object_obs_timewindow import object_obs_timewindow
sys.path.append('object_sort')
from object_sort import object_sort
sys.path.append('telescope_assign')
from ToP_GW_followup_F60_F30_galaxy_assign import main as telescope_assign

def write_log(logpath, logline):
    log = open(logpath, 'a')
    log.write(str(logline) + '\n')
    log.close()

client = Client(client_type='object_sort')
pgdb_admin = pg_admin()

location = 'xinglong'
mode = 'observation'
last_run_date = ''
object_list_current = 'object_list_current'

while True:
    client.Recv()
    t_b = datetime.datetime.now()
    data_recv = client.recv_data
    current_utc_datetime = datetime.datetime.utcnow()
    current_date = current_utc_datetime.strftime("%Y/%m/%d")
    log_date = current_utc_datetime.strftime("%Y%m%d")
    current_time = current_utc_datetime.strftime("%Y/%m/%d %H:%M:%S")
예제 #2
0
    res_list = []
    sql = "SELECT object_list_current.obj_id FROM object_list_current, object_list_all WHERE object_list_current.obj_id=object_list_all.obj_id and object_list_current.obs_stag='sent' and group_id='" + type + "' ORDER BY object_list_current.id"
    #print sql
    res = sql_get(sql)
    if res:
        #print res
        for i in res:
            res_list.append(i[0])
        return res_list
    else:
        return []


if __name__ == "__main__":

    s = Client('object_generator')

    gwac_sent = get_sent_indb('XL001')
    f60_sent = get_sent_indb('XL002')
    f30_sent = get_sent_indb('XL003')
    #print gwac_sent,f60_sent,f30_sent
    n = 0
    k = {}
    print '\nAbout: Obj_ID Unit_ID Obj_Name Ra Dec Filter Expdur Frmcnt Priority'
    print "\nGWAC:"
    if gwac_sent:
        for i in gwac_sent:
            n += 1
            k[str(n)] = i
            infs = get_obj_infs(i)
            res = pg_db('pd_log_current', 'select', [['unit_id'], {
예제 #3
0
def abandon_objs():
    from communication_client import Client
    s = Client('object_generator')
    gwac_sent = get_sent_indb('XL001')
    f60_sent = get_sent_indb('XL002')
    f30_sent = get_sent_indb('XL003')
    #print gwac_sent,f60_sent,f30_sent
    n = 0
    k = {}
    print '\nAbout: Obj_ID Unit_ID Obj_Name Ra Dec Filter Expdur Frmcnt Priority'
    print "\nGWAC:" 
    if gwac_sent:
        for i in gwac_sent:
            n += 1
            k[str(n)] = i
            infs = get_obj_infs(i)
            res = pg_db('pd_log_current','select',[['unit_id'],{'obj_id':i,'obs_stag':'sent'}])
            if res:
                unit_id = res[0][0]
            else:
                unit_id = 'None'
            print('    '+str(n)+': '+i+' '+unit_id+' '+', '.join([str(infs[i]) for i in range(2,10)]))
    else:
        print('    '+'None')
    
    print "\nF60:"
    if f60_sent:
        for i in f60_sent:
            n += 1
            k[str(n)] = i
            infs = get_obj_infs(i)
            res = pg_db('pd_log_current','select',[['unit_id'],{'obj_id':i,'obs_stag':'sent'}])
            if res:
                unit_id = res[0][0]
            else:
                unit_id = 'None'
            print('    '+str(n)+': '+i+' '+unit_id+' '+', '.join([str(i) for i in infs[2:10]]))
    else:
        print('    '+'None')

    print "\nF30:"
    if f30_sent:
        for i in f30_sent:
            n += 1
            k[str(n)] = i
            infs = get_obj_infs(i)
            res = pg_db('pd_log_current','select',[['unit_id'],{'obj_id':i,'obs_stag':'sent'}])
            if res:
                unit_id = res[0][0]
            else:
                unit_id = 'None'
            print('    '+str(n)+': '+i+' '+unit_id+' '+', '.join([str(i) for i in infs[2:10]]))
    else:
        print('    '+'None')
    if (gwac_sent+f60_sent+f30_sent):
        #print k
        print '\n\nPlease choose the order number which you want to break'
        print '    Example: 1  << Type "0" to abandon them all; Type "Enter" to quit >>'
        while True:
            inpt = raw_input(':')
            inpt = inpt.strip()
            if inpt in k.keys():
                break
            elif inpt == '0':
                print '\nAbandon them all.'
                break
            elif not inpt:
                exit('\nNothing done.\n')
            else:
                print '\nWARNING: Input wrong. Please input once again.'
        for i in range(len(k.keys())):
            if inpt != '0':
                obj = k[inpt]
            else:
                obj = k[str(i+1)]
            log_com_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
            pg_db('pd_log_current','update',[{'obj_comp_time':log_com_time,'obs_stag':'break'},{'obj_id':obj,'obs_stag':'sent'}])
            send_db_in_end(obj)
            s.Send({"obj_id":obj,"obs_stag":'break'},['update','object_list_current','obs_stag'])
            if inpt != '0':
                break
            else:
                time.sleep(3)
        #s.Send("Hello World",['insert'])
    else:
        print '\nThere is no sent objs in db.'
    print '\nDone.\n'
예제 #4
0
def pd_followup():
    from communication_client import Client
    from object_generator import obj_insert
    global lf_fu, gwac_db, yunwei_db
    gwac_db = con_db('gwac_db')
    yunwei_db = con_db('yunwei_db')

    ###
    obj_generator = Client('object_generator')
    ###
    mark_id = 0
    init_res = init()
    close(yunwei_db)
    if init_res:
        mark_id = init_res
    i = 0
    lf_fu.write('\n\n\nRestart at: %s' %
                datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
    while True:
        cur_date = datetime.datetime.utcnow().strftime("%Y-%m-%d")
        time_h = datetime.datetime.now().strftime(
            "%H:%M")  #time.strftime("%H:%M", time.localtime(time.time()))
        if time_h == '8:05':
            lf_fu.close()
            lf_fu = codecs.open("obslogs/follow_up_log_%s.txt" % cur_date,
                                "a+", 'utf-8')
            lf_fu.write("# obj_name objrank begin_time end_time group_id\n")
            time.sleep(60)
        i += 1
        if not mark_id:
            sql = "SELECT fo_id FROM follow_up_observation ORDER BY fo_id DESC LIMIT 1"
            res = sql_act(gwac_db, sql)
            if res:
                mark_id = res[0][0]
            # else:
            #     print 'Wrong WITH GETTING THE fo_id.'
        else:
            sql = "SELECT fo_id FROM follow_up_observation WHERE fo_id > " + str(
                mark_id) + " ORDER BY fo_id"
            ids = sql_act(gwac_db, sql)
            if ids:
                lf_fu.write('\n\nRun times:%s \n' % str(i))
                lf_fu.write('Last id : %s \n' % str(mark_id))
                fo_id = 0
                for id in ids:
                    fo_id = id[0]
                    sql = "SELECT fo_name, user_id, ra, dec, epoch, expose_duration, frame_count, filter, priority, trigger_time, telescope_id, obj_name FROM follow_up_observation WHERE fo_id = " + str(
                        fo_id)
                    res = sql_act(gwac_db, sql)
                    if res:
                        run_name, user_id, objra, objdec, epoch, expdur, frmcnt, filter, priority, trigger_time, telescope_id, obj_name = res[
                            0][:]
                        try:
                            date_indb = (trigger_time + datetime.timedelta(
                                hours=-8)).strftime("%Y/%m/%d")
                        except:
                            date_indb = datetime.datetime.utcnow().strftime(
                                "%Y/%m/%d")
                        if telescope_id == 1:
                            group_id = 'XL002'
                            unit_id = '001'
                        if telescope_id == 2:
                            group_id = 'XL003'
                            unit_id = '001'
                        observer = 'GWAC'
                        # if user_id == 0:
                        #     observer = 'GWAC'
                        # else:
                        #     sql = "SELECT login_name FROM user_info WHERE ui_id = " + str(user_id)
                        #     observer = sql_act(gwac_db,sql)[0][0]
                        if int(priority) < 80:
                            priority = str(int(priority) - 40 + 80)
                        obj_line = "obj_name=%s objsour=GWAC_followup observer=%s objra=%s objdec=%s objepoch=%s objerror=0.0|0.0 objrank=0 group_id=%s unit_id=%s obs_type=goa obs_stra=pointing date_beg=%s date_end=%s day_int=0 imgtype=object filter=%s expdur=%s delay=0 frmcnt=%s priority=%s run_name=%s note=%s mode=observation\n" % \
                            (obj_name, observer, objra, objdec, epoch, group_id, unit_id, date_indb, date_indb, filter, expdur, frmcnt, priority, run_name, str(id[0]))
                        lf_fu.write(obj_line + str(trigger_time) + '\n')
                        obj_insert(obj_line)
                mark_id = int(fo_id)
                obj_generator.Send("Hello World", ['insert'])
        time.sleep(0.1)
예제 #5
0
def main():
    def creat_kwargs():
        key, value = i.split('=')
        if 'pg' in key:
            print i
            pg_kwargs[key.lower()] = value
        elif 'skip_confirm' in key:
            skip_confirm = value
            return skip_confirm
        else:
            kwargs[key.lower()] = value

    params_list = sys.argv[1:]

    skip_confirm = False
    arg = ''
    args = []
    kwargs = {}
    pg_kwargs = {}

    if not params_list:
        obj_admin = og()
        obj_admin.object_generate()

    elif os.path.isfile(params_list[0]) or os.path.isdir(params_list[0]):
        if len(params_list) == 1:
            obj_admin = og()
            obj_admin.object_generate(params_list[0])

        else:
            print params_list
            for i in params_list[1:]:
                if '=' in i:
                    if creat_kwargs():
                        skip_confirm = True
                else:
                    args.append(i)

            obj_admin = og(*args, skip_confirm=skip_confirm, **kwargs)

            if 'observer' in kwargs:
                del kwargs['observer']
            if pg_kwargs:
                obj_admin.set_pg(**pg_kwargs)
            obj_admin.object_generate(params_list[0], **kwargs)

    else:
        for i in params_list:
            if '=' in i:
                if creat_kwargs():
                    skip_confirm = True
            else:
                arg += i + '\t'

        obj_admin = og(skip_confirm=skip_confirm, **kwargs)
        if 'observer' in kwargs:
            del kwargs['observer']
        if pg_kwargs:
            obj_admin.set_pg(**pg_kwargs)

        obj_admin.object_generate(arg, **kwargs)

    if obj_admin.object_params:
        # print skip_confirm
        print u'目标参数:\n'
        for i in obj_admin.object_params:
            print i
        print 'len: ', len(str(obj_admin.object_params))
        pg_action = pg_kwargs[
            'pg_action'] if 'pg_action' in pg_kwargs else 'insert'

        if pg_action == 'insert':
            print u'\n确认写入数据库: Y/n'
        elif pg_action == 'delete':
            print u'\n确认从数据库删除: Y/n'

        if raw_input('>>> ').lower() == 'y':
            obj_admin.pg_proc_object_params(pg_action)
            client = Client('object_generator')
            client.Send()
            raw_input('ok')
예제 #6
0
#!/usr/bin/env python
#coding=utf-8

from __future__ import unicode_literals
import os, sys, json, psycopg2, time, datetime, codecs
from communication_client import Client
#sys.path.append('object_generator')
from object_generator import obj_insert

###
obj_generator = Client('object_generator')
###


def load_params():
    json_file = './pd_params.json'
    with open(json_file) as read_file:
        pd_params = json.load(read_file)
    return pd_params


def con_db(db_name):
    pd_params = load_params()
    try:
        db = psycopg2.connect(**pd_params[db_name])
    except psycopg2.Error as e:
        print(e)
        return False
    else:
        return db