###################################################################### # # readscans.py # # readscans: reads Scan.xml SDM table and parses # into returned dictionary # # listflags: prints scans from dictionary returned by readflags # # Created S.T. Myers 2010-12-01 v1.0 from readflags.py # Modified T.R. Hunter 2010-12-01 v1.1 to print durations in listobs # # Usage: # from readscans import * # sdmfile = '10B-148.sb2269046.eb2519548.55530.44014204861' # myscans = readscans(sdmfile) # # then # listscans(myscans) # # Notes: readflags # Returns a dictionary with the parameters # from the SDM Scan.xml tables indexed by scan_no: # myscans[scan_no]['start'] start date/time (string) # myscans[scan_no]['end'] end date/time (string) # myscans[scan_no]['timerange'] start~end date/time (string) # myscans[scan_no]['source'] source name (string) # myscans[scan_no]['intent'] scan intent(s) (string) # myscans[scan_no]['nsubs'] number of subscans (int) # # Time strings are in format YYYY/MM/DD/HH:MM:SS.SS # e.g. 2010/11/30/10:33:51.50 # with precision set to 2 sub-second digits (10ms) # # (I)Python Notes: (courtesy A.Deller) # # Nominally, the flagant.py needs to be in the same directory you # are running casapy from. If you want to put the source in a # different directory, then: # # 1) Set up a place where you will put your modules and point a # variable at it, e.g.: # export CASAPYUTILS="~/src/python_mods/" # 2) Then make sure ipython knows about it by putting: # ip.ex("sys.path.append(os.environ['CASAPYUTILS'])") # in your ~/.ipython/ipy_user_conf.py (under the main: block). # ###################################################################### def readscans(sdmfile): try: import casac except ImportError, e: print "failed to load casa:\n", e exit(1) qatool = casac.homefinder.find_home_by_name('quantaHome') qa = casac.qa = qatool.create() try: from xml.dom import minidom except ImportError, e: print "failed to load xml.dom.minidom:\n", e exit(1) # read Scan.xml into dictionary also and make a list xmlscans = minidom.parse(sdmfile+'/Scan.xml') scandict = {} scanlist = [] rowlist = xmlscans.getElementsByTagName("row") for rownode in rowlist: rowfid = rownode.getElementsByTagName("scanNumber") fid = int(rowfid[0].childNodes[0].nodeValue) scandict[fid] = {} scanlist.append(fid) # number of subscans rowsubs = rownode.getElementsByTagName("numSubScan") nsubs = int(rowsubs[0].childNodes[0].nodeValue) # intents rownint = rownode.getElementsByTagName("numIntent") nint = int(rownint[0].childNodes[0].nodeValue) rowintents = rownode.getElementsByTagName("scanIntent") sint = str(rowintents[0].childNodes[0].nodeValue) sints = sint.split() rint = '' for r in range(nint): intent = sints[2+r] if rint=='': rint = intent else: rint += ' '+intent # start and end times in mjd ns rowstart = rownode.getElementsByTagName("startTime") start = int(rowstart[0].childNodes[0].nodeValue) startmjd = float(start)*1.0E-9/86400.0 t = qa.quantity(startmjd,'d') starttime = qa.time(t,form="ymd",prec=8) rowend = rownode.getElementsByTagName("endTime") end = int(rowend[0].childNodes[0].nodeValue) endmjd = float(end)*1.0E-9/86400.0 t = qa.quantity(endmjd,'d') endtime = qa.time(t,form="ymd",prec=8) # source name rowsrc = rownode.getElementsByTagName("sourceName") src = str(rowsrc[0].childNodes[0].nodeValue) scandict[fid]['start'] = starttime scandict[fid]['end'] = endtime timestr = starttime+'~'+endtime scandict[fid]['timerange'] = timestr scandict[fid]['source'] = src scandict[fid]['intent'] = rint scandict[fid]['nsubs'] = nsubs scandict[fid]['duration'] = endmjd-startmjd print ' Found ',rowlist.length,' scans in Scan.xml' # return the dictionary for later use return scandict # Done def listscans(myscans): # Loop over scans for key in myscans.keys(): mys = myscans[key] src = mys['source'] tim = mys['timerange'] sint= mys['intent'] dur = mys['duration']*1440 print '%8i %24s %48s %.1f minutes %-56s ' % (key, src, tim, dur, sint) durations = duration(myscans) for key in durations: print ' Total %24s %.1f minutes' % (key[0],key[1]) return # Done def duration(myscans): durations = [] for key in myscans.keys(): mys = myscans[key] src = mys['source'] dur = mys['duration']*1440 new = 1 for s in range(len(durations)): if (src == durations[s][0]): new = 0 source = s if (new == 1): durations.append([src,dur]) else: durations[source][1] = durations[source][1] + dur return(durations)