Commits
Sandra M Castro authored 3df1b9aa1e6
10 10 | |
11 11 | from casatasks import casalog, flagdata |
12 12 | from casatools import table,quanta,ms,agentflagger |
13 13 | from .parallel.parallel_task_helper import ParallelTaskHelper |
14 14 | |
15 15 | ###some helper tools |
16 16 | tblocal = table() |
17 17 | mslocal = ms() |
18 18 | qalocal = quanta() |
19 19 | |
20 - | # common function to use to get a dictionary item iterator |
21 - | def lociteritems(adict): |
22 - | return adict.items() |
23 - | |
24 20 | ''' |
25 21 | A set of helper functions for the tasks flagdata and flagcmd. |
26 22 | Class Parser: to parse flag commands |
27 23 | |
28 24 | I/O functions: |
29 25 | get_flag_cmd_list |
30 26 | readFile |
31 27 | readFiles |
32 28 | readAndParse |
33 29 | parseDictionary |
801 797 | fei = 0 # count the number of lines with feed |
802 798 | ari = 0 # count the number of lines with array |
803 799 | ui = 0 # count the number of lines with uvrange |
804 800 | pi = 0 # count the number of lines with spw |
805 801 | oi = 0 # count the number of lines with observation |
806 802 | |
807 803 | for k in flagdict.keys(): |
808 804 | # Each key is a dictionary of one flag command |
809 805 | cmddict = flagdict[k]['command'] |
810 806 | |
811 - | for xkey,xval in lociteritems(cmddict): |
807 + | for xkey,xval in cmddict.items(): |
812 808 | # Check which parameter |
813 809 | if xkey == "scan": |
814 810 | scans += xval + ',' |
815 811 | si += 1 |
816 812 | elif xkey == "field": |
817 813 | fields += xval + ',' |
818 814 | fi += 1 |
819 815 | elif xkey == "antenna": |
820 816 | ants += xval + ';' |
821 817 | ai += 1 |
889 885 | dictpars['observation'] = obs |
890 886 | |
891 887 | # Compress the selection list to reduce MSSelection parsing time. |
892 888 | # 'field','spw','antenna' strings in dicpars will be modified in-place. |
893 889 | compressSelectionList(vis,dictpars); |
894 890 | |
895 891 | # The number of keys in flagdict is the number of flag commands |
896 892 | ncmds = flagdict.__len__() |
897 893 | |
898 894 | # Make the union. Only leave non-empty parameters in dictionary |
899 - | for k,v in lociteritems(npars): |
895 + | for k,v in npars.items(): |
900 896 | if v < ncmds: |
901 897 | dictpars.pop(k) |
902 898 | |
903 899 | return dictpars |
904 900 | |
905 901 | def _merge_timerange(commands): |
906 902 | ''' merge manual commands that only differ in timerange and agentname |
907 903 | this speeds up manual flagging using large lists |
908 904 | |
909 905 | cmd -> list of flagging commands |
1189 1185 | cmddict = flagdict[key]['command'] |
1190 1186 | reason = flagdict[key]['reason'] |
1191 1187 | |
1192 1188 | # Add new reason or replace old one with new reason |
1193 1189 | if reason2add: |
1194 1190 | reason = add_reason |
1195 1191 | |
1196 1192 | if reason != '': |
1197 1193 | cmddict['reason'] = reason |
1198 1194 | |
1199 - | for k,v in lociteritems(cmddict): |
1195 + | for k,v in cmddict.items(): |
1200 1196 | cmdstr = "" |
1201 1197 | if isinstance(v, str): |
1202 1198 | # Add quotes to string values |
1203 1199 | if v.count("'") > 0: |
1204 1200 | v = v.strip("'") |
1205 1201 | if v.count('"') > 0: |
1206 1202 | v = v.strip("'") |
1207 1203 | cmdstr = "'"+v+"'" |
1208 1204 | cmdline = cmdline + k + '=' + str(cmdstr) + ' ' |
1209 1205 | else: |
1231 1227 | cmddict.pop('reason') |
1232 1228 | # Summary cmds should not go to FLAG_CMD |
1233 1229 | if 'mode' in cmddict and cmddict['mode'] == 'summary': |
1234 1230 | casalog.post("Commands with mode='summary' are not allowed in the FLAG_CMD table", 'WARN') |
1235 1231 | continue |
1236 1232 | # Add to REASON column the user input reason if requested |
1237 1233 | reason = flagdict[key]['reason'] |
1238 1234 | if reason2add: |
1239 1235 | reason = add_reason |
1240 1236 | |
1241 - | for k,v in lociteritems(cmddict): |
1237 + | for k,v in cmddict.items(): |
1242 1238 | cmdstr = "" |
1243 1239 | if isinstance(v, str): |
1244 1240 | # Add quotes to string values |
1245 1241 | if v.count("'") > 0: |
1246 1242 | v = v.strip("'") |
1247 1243 | if v.count('"') > 0: |
1248 1244 | v = v.strip("'") |
1249 1245 | cmdstr = "'"+v+"'" |
1250 1246 | cmdline = cmdline + k + '=' + str(cmdstr) + ' ' |
1251 1247 | |
1830 1826 | '''Evaluate the correct types in a string with parameters |
1831 1827 | |
1832 1828 | Keyword arguments: |
1833 1829 | pardict -- a dictionary with flag commands as returned by parseDictionary() |
1834 1830 | |
1835 1831 | It evaluates each value to its correct Python type. |
1836 1832 | ''' |
1837 1833 | |
1838 1834 | cmddict = OrderedDict() |
1839 1835 | |
1840 - | for key,val in lociteritems(pardict): |
1836 + | for key,val in pardict.items(): |
1841 1837 | newval = None |
1842 1838 | |
1843 1839 | if val.startswith('['): |
1844 1840 | newval = ast.literal_eval(val) |
1845 1841 | elif val.startswith('{'): |
1846 1842 | newval = eval(val) |
1847 1843 | elif val in ['True', 'False']: |
1848 1844 | if eval(val) in [True, False]: |
1849 1845 | newval = eval(val) |
1850 1846 | if newval is None: |
2002 1998 | for k, v in reftup: |
2003 1999 | reference[k].append(v) |
2004 2000 | |
2005 2001 | refkeys = reference.keys() |
2006 2002 | |
2007 2003 | # Check the user dictionary against the reference |
2008 2004 | count = 0 |
2009 2005 | for idx in pardict.keys(): |
2010 2006 | mydict = pardict[idx]['command'] |
2011 2007 | count += 1 |
2012 - | for key,val in lociteritems(mydict): |
2008 + | for key,val in mydict.items(): |
2013 2009 | if key not in refkeys: |
2014 2010 | raise ValueError('Parameter \'%s\' in row=%s is not a valid flagdata parameter'%(key,idx)) |
2015 2011 | |
2016 2012 | # reference[key] is always a list |
2017 2013 | refval = reference[key] |
2018 2014 | vtypes = "" |
2019 2015 | match = False |
2020 2016 | for tt in refval: |
2021 2017 | vtypes = vtypes + str(type(tt)) + ',' |
2022 2018 | if type(val) == type(tt): |
3044 3040 | # Compress the selection list to reduce MSSelection parsing time. |
3045 3041 | # 'field','spw','antenna' strings in dicpars will be modified in-place. |
3046 3042 | compressSelectionList(vis,dicpars); |
3047 3043 | |
3048 3044 | # Real number of input lines |
3049 3045 | # Get the number of occurrences of each parameter |
3050 3046 | npars = getNumPar(cmddict) |
3051 3047 | nlines = nrows - npars['comment'] |
3052 3048 | |
3053 3049 | # Make the union. |
3054 - | for k,v in lociteritems(npars): |
3050 + | for k,v in npars.items(): |
3055 3051 | if k != 'comment': |
3056 3052 | if v < nlines: |
3057 3053 | dicpars[k] = '' |
3058 3054 | |
3059 3055 | |
3060 3056 | uniondic = dicpars.copy() |
3061 3057 | # Remove empty parameters from the dictionary |
3062 - | for k,v in lociteritems(dicpars): |
3058 + | for k,v in dicpars.items(): |
3063 3059 | if v == '': |
3064 3060 | uniondic.pop(k) |
3065 3061 | |
3066 3062 | return uniondic |
3067 3063 | |
3068 3064 | def getNumPar(cmddict): |
3069 3065 | '''Get the number of occurrences of all parameter keys |
3070 3066 | cmdlist --> list of strings with parameters and values |
3071 3067 | ''' |
3072 3068 | |
3264 3260 | if add_reason != '': |
3265 3261 | reason = str(add_reason) |
3266 3262 | |
3267 3263 | if reason != '': |
3268 3264 | cmdline = cmdline + ' reason=' + str(reason) + ' ' |
3269 3265 | |
3270 3266 | # For flagcmd saving |
3271 3267 | newdict = evalString(cmdline) |
3272 3268 | # newdict = evaluateString(cmdline) |
3273 3269 | cmdline = '' |
3274 - | for k,v in lociteritems(newdict): |
3270 + | for k,v in newdict.items(): |
3275 3271 | cmdstr = "" |
3276 3272 | # Add quotes to non-quoted strings |
3277 3273 | if isinstance(v, str): |
3278 3274 | if v.count("'") > 0: |
3279 3275 | v = v.strip("'") |
3280 3276 | if v.count('"') > 0: |
3281 3277 | v = v.strip('"') |
3282 3278 | |
3283 3279 | # Add quotes to string values |
3284 3280 | cmdstr = "'"+v+"'" |
3331 3327 | |
3332 3328 | # Add addantenna back |
3333 3329 | if myflags[key].__contains__('addantenna'): |
3334 3330 | addantenna = myflags[key]['addantenna'] |
3335 3331 | command = command + ' addantenna=' + str(addantenna) |
3336 3332 | |
3337 3333 | # Evaluate the types |
3338 3334 | newdict = evalString(command) |
3339 3335 | # newdict = evaluateString(command) |
3340 3336 | cmdline = '' |
3341 - | for k,v in lociteritems(newdict): |
3337 + | for k,v in newdict.items(): |
3342 3338 | cmdstr = "" |
3343 3339 | if isinstance(v, str): |
3344 3340 | if v.count("'") > 0: |
3345 3341 | v = v.strip("'") |
3346 3342 | if v.count('"') > 0: |
3347 3343 | v = v.strip('"') |
3348 3344 | # Add quotes to string values |
3349 3345 | cmdstr = "'"+v+"'" |
3350 3346 | v = cmdstr |
3351 3347 | cmdline = cmdline + k + '=' + str(v) + ' ' |