from __future__ import absolute_import
import os
import glob
from casatasks.private.casa_transition import is_CASA6
if is_CASA6:
from casatools import table as tbtool
from casatasks import casalog
else:
from taskinit import *
def concatephem(ephems=[], outputephem=''):
"""
concatephem
Concatenate the given ephemeris tables into a single output table
filling gaps with dummy rows and removing overlap rows.
Before concatenation, test that basic conditions are fulfilled:
same time grid, list of input ephemerides is ordered in time.
ephems - List of the ephemeris tables to be concatenated
default:
outputephem - Name of the output ephemeris to be created from the concatenation
If empty, concatephem will only perform a dryrun.
default: ''
"""
mytb = tbtool()
starts = []
ends = []
stepsizes = []
hasoverlap_with_previous = []
gap_to_previous = []
shouldconcat_with_previous = []
canconcat_with_previous = []
stepsize_rel_tolerance = 1E-6
stepsize_abs_tolerance_d = 0.1/86400.
for myephem in ephems:
mytb.open(myephem)
mynrows = mytb.nrows()
if mynrows==0:
mytb.close()
casalog.post('Ephemeris '+myephem+' has no rows.', 'SEVERE')
return
mystart = mytb.getcell('MJD',0)
myend = mytb.getcell('MJD', mynrows-1)
if mynrows<2:
mystepsize = 0
casalog.post('Ephemeris '+myephem+' has only one row.', 'WARN')
else:
mystepsize = mytb.getcell('MJD',1) - mystart
mytb.close()
starts.append(mystart)
ends.append(myend)
stepsizes.append(mystepsize)
if os.path.exists(outputephem):
casalog.post('Output ephemeris table '+outputephem+' exists already.', 'SEVERE')
return
casalog.post('Ephem no., startMJD, endMJD, step size (d)', 'INFO')
for i in range(0,len(starts)):
casalog.post(str(i)+', '+str(starts[i])+', '+str(ends[i])+', '+str(stepsizes[i]), 'INFO')
backstep = 0
for i in range(0,len(starts)):
shouldconcat_with_previous.append(False)
canconcat_with_previous.append(False)
hasoverlap_with_previous.append(False)
gap_to_previous.append(0)
if i>0:
if (abs(stepsizes[i] - stepsizes[i-1-backstep])/stepsizes[i] < stepsize_rel_tolerance) \
and abs(stepsizes[i] - stepsizes[i-1-backstep]) < stepsize_abs_tolerance_d:
casalog.post( 'Ephemerides '+str(i-1-backstep)+' and '+str(i)+' have same step size.', 'INFO')
if starts[i-1-backstep] <= starts[i]:
casalog.post( 'Ephemeris '+str(i-1-backstep)+' begins before '+str(i), 'INFO')
if ends[i-1-backstep] < ends[i]:
casalog.post( 'Ephemeris '+str(i-1-backstep)+' ends before '+str(i), 'INFO')
shouldconcat_with_previous[i] = True