casa
$Rev:20696$
|
00001 import os 00002 from taskinit import * 00003 00004 00005 def flagmanager( 00006 vis=None, 00007 mode=None, 00008 versionname=None, 00009 oldname=None, 00010 comment=None, 00011 merge=None, 00012 ): 00013 00014 casalog.origin('flagmanager') 00015 aflocal = casac.agentflagger() 00016 00017 try: 00018 if type(vis) == str and os.path.exists(vis): 00019 if mode != 'rename': 00020 aflocal.open(vis) 00021 else: 00022 raise Exception, \ 00023 'Visibility data set not found - please verify the name' 00024 if mode == 'list': 00025 aflocal.getflagversionlist() 00026 print 'See logger for flag versions for this MS' 00027 elif mode == 'save': 00028 if versionname == '': 00029 raise Exception("Illegal versionname: ''") 00030 casalog.post('Save current flagversion in ' + versionname) 00031 aflocal.saveflagversion(versionname=versionname, 00032 comment=comment, merge=merge) 00033 elif mode == 'restore': 00034 if versionname == '': 00035 raise Exception("Illegal versionname: ''") 00036 casalog.post('Restore flagversion ' + versionname) 00037 aflocal.restoreflagversion(versionname=versionname, 00038 merge=merge) 00039 elif mode == 'delete': 00040 if versionname == '': 00041 raise Exception("Illegal versionname: ''") 00042 aflocal.deleteflagversion(versionname=versionname) 00043 elif mode == 'rename': 00044 if versionname == '': 00045 raise Exception("Illegal versionname: ''") 00046 00047 if oldname == '': 00048 raise Exception("Illegal oldname: ''") 00049 00050 # The directory structure is unlikely to change 00051 olddir = vis + '.flagversions/flags.' + oldname 00052 newdir = vis + '.flagversions/flags.' + versionname 00053 if not os.path.isdir(olddir): 00054 raise Exception, 'No such flagversion: ' + str(oldname) 00055 if os.path.exists(newdir): 00056 raise Exception, 'Flagversion ' + str(versionname) \ 00057 + ' already exists!' 00058 00059 casalog.post('Rename flagversion "%s" to "%s"' % (oldname, 00060 versionname)) 00061 00062 os.rename(olddir, newdir) 00063 00064 # Edit entry in .flagversions/FLAG_VERSION_LIST 00065 # For realistic usecases, this file is short enough to keep in memory 00066 file = vis + '.flagversions/FLAG_VERSION_LIST' 00067 fd = open(file) 00068 lines = fd.readlines() 00069 fd.close() 00070 00071 for i in range(len(lines)): 00072 if (lines[i])[:len(oldname) + 3] == oldname + ' : ': 00073 lines[i] = versionname + ' : ' + comment + '\n' 00074 break 00075 00076 fd = open(file, 'w') 00077 fd.writelines(lines) 00078 fd.close() 00079 00080 else: 00081 raise Exception, 'Unknown mode' + str(mode) 00082 00083 aflocal.done() 00084 except Exception, instance: 00085 print '*** Error ***', instance 00086 00087