-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonoff_db.py
132 lines (98 loc) · 4.15 KB
/
onoff_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
operations on observator (obs) database for onoff measurements
Created Jan 2020
@author: jkulpa
"""
from ATATools import logger_defaults,snap_array_helpers
#from ATAobs import obs_db
import ATASQL
def get_all_meas_dict(setid,antenna_list):
logger= logger_defaults.getModuleLogger(__name__)
mydb = ATASQL.connectObsDb()
mycursor = mydb.cursor()
if not antenna_list:
logger.warning('antenna list empty for id {}'.format(setid))
return None
insertcmd_part = ("select recordings.freq,recordings.description,recordings.id, "
"rec_ants.ant,rec_ants.az,rec_ants.el "
"from (recordings inner join rec_ants on recordings.id = rec_ants.id ) "
"where recordings.status = 'OK' and recordings.setid = %s ")
cpart2 = (" and rec_ants.ant in (%s)")
in_p=', '.join(map(lambda x: '%s', antenna_list))
insertcmd_part2 = cpart2 % in_p
insertcmd = insertcmd_part + insertcmd_part2
exec_list = [setid] + antenna_list
logger.info("getting previous measurements for id {} antennas {}".format(setid,",".join(antenna_list)))
mycursor.execute(insertcmd,exec_list)
myiterator = mycursor.fetchall()
if not myiterator:
mycursor.close()
mydb.close()
return None
returndict = {}
for (freq,desc,obsid,ant,az,el) in myiterator:
if ant not in returndict:
cdict = {'freq':[],'desc':[],'obsid':[],'az':[],'el':[]}
returndict[ant] = cdict
returndict[ant]['freq'].append(freq)
returndict[ant]['desc'].append(desc)
returndict[ant]['obsid'].append(obsid)
returndict[ant]['az'].append(az)
returndict[ant]['el'].append(el)
mycursor.close()
mydb.close()
return returndict
def get_obs_params(setid,sources,ant_snap_dictionary,freq_list):
all_antennas_list = snap_array_helpers.dict_list_to_list(ant_snap_dictionary)
logger= logger_defaults.getModuleLogger(__name__)
meas_dictionary = get_all_meas_dict(setid,all_antennas_list)
snapKeys = ant_snap_dictionary.keys()
outputDict = {}
if meas_dictionary:
#using sets because it automatically removes duplicates
#we are allowing to re-measure some frequencies on some
#of the antennas. e.g. ant1a was measured on 1,2,3 GHz
#and ant1c on 1,2 GHz and they belong to different snaps,
#while order was 1,2,3,4 GHzwe would order to measure
#1a and 2c on 3 and 4 GHz, duplicating 3GHz for 1a
freq_set = set(freq_list)
todo_freq_set = set()
antennas_got = meas_dictionary.keys()
for sk in snapKeys:
clist = ant_snap_dictionary[sk]
#for each host, we are searching for first antenna that
#was not measured, or was measured but has still some
#not measured frequencies
for cant in clist:
if cant in antennas_got:
#antenna was measured, testing freq list
flist = meas_dictionary[cant]['freq']
diffset = freq_set - set(flist)
#we have some unmeasured freqencies for that antenna
if diffset:
outputDict[sk] = cant
todo_freq_set.update(diffset)
break
else:
outputDict[sk] = cant
todo_freq_set.update(freq_list)
break
outputFreqList = list(todo_freq_set)
else:
#there were no measurements for those antennas
#taking first antenna from each list and copying
#the frequency list
outputFreqList = freq_list
for sk in snapKeys:
clist = ant_snap_dictionary[sk]
outputDict[sk] = clist[0]
return outputDict,outputFreqList
def remove_antennas_from_dict(ant_groups,curr_ant_dict):
meas_keys = curr_ant_dict.keys()
for ck in meas_keys:
ant_groups[ck].remove(curr_ant_dict[ck])
#if there is no antenna on the list, pop the dictionary entry
if len(ant_groups[ck]) == 0:
ant_groups.pop(ck)