This python routine is designed to search a table (usually the VIRUS_IFU.table made by a run of vir_ifu_pat.sh) and print information for a user-requested IFUSLOT value. The task is simple, but I show the entire python code below since it incorporates a number of newly gained skills (as of July 2015) regarding file reading and error checking. To start with, I show a normal run on a modified VIRUS_IFU.table file that I have renamed "A". The point here is that "A" has a bad data line (the Xcen value for ifuslot=040 is not a number), and the dX,dY values for ifuslot=030 have nonzero values.
% vtabread.py A 030 -44240.000000 -79670.000000 -90.000000 % cat A # VIRUS IFU Table: Tue Jul 7 13:15:21 CDT 2015 # linear units: microns col01 = IFU slot name (ccr) {t1,a3} col02 = Xcen { t5,f12.3} col03 = Ycen {t18,f12.3} col04 = Theta {t34,f10.5} degrees col05 = dx {t48,f9.3} col06 = dy {t58,f9.3} col07 = scale {t72,f6.2} microns/arcsec # data 030 -44250.000 -79650.000 -90.00000 10.000 -20.000 177.00 040 nnnnnnnnnn -79650.000 -90.00000 0.000 0.000 177.00 050 -8850.000 -79650.000 -90.00000 0.000 0.000 177.00 035 -44250.000 8850.000 -90.00000 0.000 0.000 177.00We see that the simple run above was successful: we foun the information for ifuslot=030, and the dX,dY were succesfully added so that the final corrected Xcen,Ycen values for this IFU position could be loacted on the VIRUS IHMP.
Below I show the usual type of useful output we would get if we invoke the "-h" or "--help" flags:
% vtabread.py --help usage: vtabread.py [-h] [-v] arg1 arg2 positional arguments: arg1 input file (VIRUS_IFU.table) arg2 IFUSLOT number (ccr) optional arguments: -h, --help show this help message and exit -v, --verbose Verbose responsesNote that we could also invoke the "-v" or "--verbose" flag when we run vtabread.py and this would create a large amount of extra (possibly useful) information that would be sent to standard out.
Finally, I show below a case where we have requested an IFUSLOT that does have an entry in our table, but that data entry has a corrupt value. We send the output to a file named "a.out", and then we see that a.out is empty. The important point here is that requesting the bad case (ifuslot=040) does NOT cause vtabread.py to crash. We can handle the empty contents of "a.out" later, but if vtabread.py crashes in the middle of some larger processing script, then we'd generally be SOL at that point.
% vtabread.py A 040 >a.out % cat a.out %
Below is the code for vtabread.py.
#!/usr/bin/python """ Read the IFU table (typically a file named VIRUS_IFU.table made with vir_ifu_pat.sh) and extract the X,Y positions for a requested IFU slot position (ccr notation). S. Odewahn July2015 """ import argparse as ARGP parser = ARGP.ArgumentParser() parser.add_argument("arg1", help="input file (VIRUS_IFU.table)") parser.add_argument("arg2", help="IFUSLOT number (ccr)") parser.add_argument("-v","--verbose", help="Verbose responses", action="store_true") args = parser.parse_args() fileXY = args.arg1 ifuslot = args.arg2 if args.verbose: print "Input file = %s\n" % (fileXY), print "IFUSLOT requested = %s\n" % (ifuslot), #================================================== #================================================== # Open the file for read-only f = open(fileXY, 'r') # Look until you find the string "# data" for line in f: p = line.split() if (len(p) == 2): if p[0] == "#" and p[1] == "data": if args.verbose: print "found the string" break # Read the data lines fname = [] xr = [] yr = [] pa = [] dx = [] dy = [] i = 0 for line in f: if args.verbose: print line, # split up the input data line into words p = line.split() #++++++++++++++++++++++++++++++++++++++++++++++++++++ # Check all data values # Check that the Xcen value is a readable float try: a_x = float(p[1]) xchek = "OK" except: a_x = -999.99 xchek = "BAD" if args.verbose: print "a_x,xchek : %f %s\n" % (a_x,xchek), # Check that the Ycen value is a readable float try: a_y = float(p[2]) ychek = "OK" except: a_y = -999.99 ychek = "BAD" if args.verbose: print "a_y,ychek : %f %s\n" % (a_y,ychek), # Check that the PA value is a readable float try: par = float(p[3]) pachek = "OK" except: par = -999.99 pachek = "BAD" if args.verbose: print "par,pachek : %f %s\n" % (par,pachek), # Check that the dX value is a readable float try: dxr = float(p[4]) dxchek = "OK" except: dxr = -999.99 dxchek = "BAD" if args.verbose: print "dxr,dxchek : %f %s\n" % (dxr,dxchek), # Check that the dY value is a readable float try: dyr = float(p[5]) dychek = "OK" except: dyr = -999.99 dychek = "BAD" if args.verbose: print "dyr,dychek : %f %s\n" % (dyr,dychek), #++++++++++++++++++++++++++++++++++++++++++++++++++++ icheck = 0 if xchek!="BAD": icheck = icheck+1 if ychek!="BAD": icheck = icheck+1 if pachek!="BAD": icheck = icheck+1 if dxchek!="BAD": icheck = icheck+1 if dychek!="BAD": icheck = icheck+1 # print p[0],icheck # if all tests were passed (i.e. icheck=5) then store the data if icheck == 5: fname.append(p[0]) xr.append(a_x) yr.append(a_y) pa.append(par) dx.append(dxr) dy.append(dyr) i=i+1 n = i if args.verbose: print "The number of valid DATA lines = %d\n" % (n) # close the input file f.close() #================================================== #================================================== # Print the fname values that you read from the file i = 0 while i < n: if args.verbose: print "fname(%d) = %s\n" % (i,fname[i]), if fname[i] == ifuslot: xfinal = xr[i] + dx[i] yfinal = yr[i] + dy[i] print "%f %f %f" % (xr[i]+dx[i], yr[i]+dy[i], pa[i]), break i=i+1