#!/usr/bin/env python #************************************************************************** # Copyright (C) 2009-2011 by Walter Brisken * # * # This program is free software; you can redistribute it and/or modify * # it under the terms of the GNU General Public License as published by * # the Free Software Foundation; either version 3 of the License, or * # (at your option) any later version. * # * # This program is distributed in the hope that it will be useful, * # but WITHOUT ANY WARRANTY; without even the implied warranty of * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * # GNU General Public License for more details. * # * # You should have received a copy of the GNU General Public License * # along with this program; if not, write to the * # Free Software Foundation, Inc., * # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * #************************************************************************** #=========================================================================== # SVN properties (DO NOT CHANGE) # # $Id$ # $HeadURL: $ # $LastChangedRevision$ # $Author$ # $LastChangedDate$ # #============================================================================ from string import split, strip, find from sys import exit, argv from os.path import isfile from os import umask program = 'oms2v2d' version = '1.1' verdate = '20111213' author = 'Walter Brisken' def usage(pgm): print '%s ver. %s %s %s\n' % (program, version, author, verdate) print 'Program to take sched\'s .oms file and produce a skeleton .v2d file\n' print 'Usage: %s [options] \n' % pgm print 'options can include\n' print ' --help' print ' -h print this help info and quit\n' print ' --force' print ' -f force operation, even if output file exits\n' print ' --verbose' print ' -v be more verbose in execution (use -v -v for even more!)\n' print ' --quiet' print ' -q be less verbose\n' print ' is a .oms file produced by sched\n' exit(0) def readOMS(filename, verbose): if verbose > 1: print 'Reading: %s' % filename data = open(filename).readlines() section = [] current = {} n = 0; minbw = 1.0e9 maxbw = 0.0 spaces = '' for D in data: n = n + 1 d = strip(D) if(len(d) < 1): continue p = find(d, "=") if p < 0: key = '=COMMENT' value = strip(d) else: key = strip(d[0:p]) if len(key) < 1: print 'Illegal line [%d] : %s' % (n, d) exit(0); value = strip(d[p+1:]) if key == 'BEGIN': section.append( (value, current) ) last = current current = {} if not last.has_key(value): last[value] = [] last[value].append(current) if verbose > 2: print '%sBEGIN %s' % (spaces, value) spaces = spaces + ' ' elif key == 'END': spaces = spaces[:-2] if len(section) == 0: print 'Error: line %d unmatched END: %s' % (n, d) exit(0) oldvalue, current = section.pop() if verbose > 2: print '%sEND %s' % (spaces, value) if oldvalue != value: print 'END with value %s does not match previous BEGIN with value %s, line %d' % (value, oldvalue, n) exit(0) elif key == 'BANDWIDTH': if verbose > 3: print '%s%s = %s' % (spaces, key, value); for v in split(strip(value)): f = float(v) if f > maxbw: maxbw = f if f < minbw: minbw = f else: if verbose > 3: print '%s%s = %s' % (spaces, key, value); if not current.has_key(key): current[key] = [] for v in split(strip(value)): current[key].append(v) if len(section) > 0: print 'Error: unmatched BEGIN' exit(0); current['MINBW'] = minbw current['MAXBW'] = maxbw if verbose > 1: print 'Min channel bandwidth = %f MHz' % minbw print 'Max channel bandwidth = %f MHz' % maxbw return current def writev2d(oms, v2dFile, verbose): corr = oms['CORRELATOR_PARAMETERS'][0] if verbose > 0: print 'Writing: %s' % v2dFile out = open(v2dFile, 'w') bw = oms['MAXBW'] if bw != oms['MINBW']: print 'Warning: different bandwidths used within this file.' print 'Using the maximum bandwidth to set spectral resolution.' try: nFFT = int(corr['FFT_SIZE'][0]) except KeyError: print 'Warning: FFT SIZE not provided. Assuming 256.' nFFT = 256 try: specAvg = int(corr['SPECTRAL_AVERAGE'][0]) except KeyError: specAvg = 256/nFFT if specAvg == 0: specAvg = 1 if 'POLARIZATION' in corr: if corr['POLARIZATION'][0] == 'NO': doPolar = 'False' else: doPolar = 'True' stations = [] for s in oms['STATION_INFO']: stations.append(s['STATION_ID'][0]) stations.sort() stnString = stations[0] for s in stations[1:]: stnString = stnString + ', ' + s out.write('# base .v2d file generated by %s version %s operating on file %s\n\n' % (program, version, omsFile)) out.write('vex = %s\n\n' % vexFile) out.write('antennas =%s\n\n' % stnString) for s in oms['SOURCE_INFO']: srcParams = '' if s.has_key('CAL_CODE'): if len(s['CAL_CODE']) > 0: c = s['CAL_CODE'][0] if len(c) > 0: srcParams = srcParams + ' calCode = ' + c out.write('SOURCE %s {%s }\n' % (s['SOURCE_NAME'][0], srcParams)) out.write('\n') for s in stations: out.write('ANTENNA %s { toneSelection=smart }\n' % s) out.write('\n') out.write('SETUP default\n') out.write('{\n') if 'TIME_AVERAGE' in corr: out.write(' tInt = %s\n' % corr['TIME_AVERAGE'][0]) if specAvg != -1 and nFFT !=-1: out.write(' fftSpecRes = %10.8f\n' % (bw/(nFFT/2))) out.write(' specRes = %10.8f\n' % (bw/(nFFT/(2*specAvg)))) if 'POLARIZATION' in corr: out.write(' doPolar = %s\n' % doPolar) if nFFT > 256: out.write(' numBufferedFFTs = 10\n') out.write(' maxNSBetweenACAvg = 2000000\n') out.write('}\n') out.close() # main below here umask(02) print '' inFile = '' force = False stop = False verbose = 1 for a in argv[1:]: if a[0] == '-': if a in ['-h', '--help']: usage(argv[0]) elif a in ['-f', '--force']: force = True elif a in ['-v', '--verbose']: verbose += 1 elif a in ['-q', '--quiet']: verbose -= 1 else: print 'Unknown command line option: %s' % a stop = True else: if inFile == '': inFile = a else: print 'Extra command line arg. given: %s' % a stop = True if inFile == '': print 'No input .oms file provided.' stop = True if stop: print 'Please run with -h to get usage instructions.\n' exit(0) if inFile[-4:] == '.oms': prefix = inFile[:-4] else: prefix = inFile omsFile = prefix + '.oms' v2dFile = prefix + '.v2d' if not isfile(omsFile): print 'oms2v2d: Input oms file %s not found. Quitting.\n' % omsFile exit(0) if isfile(prefix + '.skd') and isfile(prefix + '.vex'): print 'Warning: Found both %s.skd and %s.vex .' % (prefix, prefix) print 'Assuming %s.vex is the correct one!\n' % prefix if isfile(prefix + '.vex'): vexFile = prefix + '.vex.obs' elif isfile(prefix + '.skd'): vexFile = prefix + '.skd.obs' else: print 'No vex file found here. It should be called %s.vex or %s.skd.' % (prefix, prefix) if force: vexFile = prefix + '.vex.obs' print 'Assuming vex file is called %s.\n' % (prefix + '.vex') else: print 'Run with --force to continue anyway.\n' exit(0) if isfile(v2dFile): if force: if verbose > 0: print 'Warning: overwriting exisiting file %s.\n' % v2dFile else: print 'Error: output file %s exists. Run with --force to continue.\n' % v2dFile exit(0) oms = readOMS(omsFile, verbose) writev2d(oms, v2dFile, verbose)