예제 #1
0
def clearcal(
    vis=None,
    field=None,
    spw=None,
    intent=None,
    addmodel=None,
):

    casalog.origin('clearcal')

    # Do the trivial parallelization
    if ParallelTaskHelper.isParallelMS(vis):
        helper = ParallelTaskHelper('clearcal', locals())
        helper.go()
        return

    # Local versions of the tools
    tblocal = tbtool()
    cblocal = cbtool()
    mslocal = mstool()

    try:

        # we will initialize scr cols only if we don't create them
        doinit = False

        if (type(vis) == str) & os.path.exists(vis):
            tblocal.open(vis)
            doinit = tblocal.colnames().count('CORRECTED_DATA') > 0
            tblocal.close()

            # We ignore selection if creating the scratch columns
            if not doinit:
                casalog.post(
                    'Need to create scratch columns; ignoring selection.')

            cblocal.setvi(old=True, quiet=False)
            # Old VI for now
            cblocal.open(vis, addmodel=addmodel)
        else:
            raise Exception, \
                'Visibility data set not found - please verify the name'

        # If necessary (scr col not just created), initialize scr cols
        if doinit:
            cblocal.selectvis(field=field, spw=spw, intent=intent)
            cblocal.initcalset(1)
        cblocal.close()

        # Write history to the MS
        param_names = clearcal.func_code.co_varnames[:clearcal.func_code.
                                                     co_argcount]
        param_vals = [eval(p) for p in param_names]
        casalog.post('Updating the history in the output', 'DEBUG1')
        write_history(mslocal, vis, 'clearcal', param_names, param_vals,
                      casalog)

    except Exception, instance:

        print '*** Error ***', instance
예제 #2
0
def clearcal(
    vis=None,
    field=None,
    spw=None,
    intent=None,
    addmodel=None,
    ):

    casalog.origin('clearcal')

    # Do the trivial parallelization
    if ParallelTaskHelper.isParallelMS(vis):
        helper = ParallelTaskHelper('clearcal', locals())
        helper.go()
        return

    # Local versions of the tools
    tblocal = tbtool()
    cblocal = cbtool()
    mslocal = mstool()

    try:

        # we will initialize scr cols only if we don't create them
        doinit = False

        if (type(vis) == str) & os.path.exists(vis):
            tblocal.open(vis)
            doinit = tblocal.colnames().count('CORRECTED_DATA') > 0
            tblocal.close()

            # We ignore selection if creating the scratch columns
            if not doinit:
                casalog.post('Need to create scratch columns; ignoring selection.'
                             )

            cblocal.open(vis, addmodel=addmodel)
        else:
            raise Exception, \
                'Visibility data set not found - please verify the name'

        # If necessary (scr col not just created), initialize scr cols
        if doinit:
            cblocal.selectvis(field=field, spw=spw, intent=intent)
            cblocal.initcalset(1)
        cblocal.close()

        # Write history to the MS
        param_names = clearcal.func_code.co_varnames[:clearcal.func_code.co_argcount]
        param_vals = [eval(p) for p in param_names]
        casalog.post('Updating the history in the output', 'DEBUG1')
        write_history(mslocal, vis, 'clearcal', param_names,
                      param_vals, casalog)
        
    except Exception, instance:

        print '*** Error ***', instance
예제 #3
0
def split_core(vis, outputvis, datacolumn, field, spw, width, antenna,
               timebin, timerange, scan, intent, array, uvrange,
               correlation, observation, combine, keepflags):

    retval = True

    if not outputvis or outputvis.isspace():
        raise ValueError, 'Please specify outputvis'

    myms = mstool()
    mytb = None
    if ((type(vis)==str) & (os.path.exists(vis))):
        myms.open(vis, nomodify=True)
    else:
        raise ValueError, 'Visibility data set not found - please verify the name'

    if os.path.exists(outputvis):
        myms.close()
        raise ValueError, "Output MS %s already exists - will not overwrite." % outputvis

    if (os.path.exists(outputvis+".flagversions")):
        myms.close()
        raise ValueError, "The flagversions \"%s.flagversions\" for the output MS already exist. Please delete." % outputvis

    # No longer needed.  When did it get put in?  Note that the default
    # spw='*' in myms.split ends up as '' since the default type for a variant
    # is BOOLVEC.  (Of course!)  Therefore both split and myms.split must
    # work properly when spw=''.
    #if(spw == ''):
    #    spw = '*'
    
    if(type(antenna) == list):
        antenna = ', '.join([str(ant) for ant in antenna])

    ## Accept digits without units ...assume seconds
    timebin = qa.convert(qa.quantity(timebin), 's')['value']
    timebin = str(timebin) + 's'
    
    if timebin == '0s':
        timebin = '-1s'

    # MSStateGram is picky ('CALIBRATE_WVR.REFERENCE, OBSERVE_TARGET_ON_SOURCE'
    # doesn't work, but 'CALIBRATE_WVR.REFERENCE,OBSERVE_TARGET_ON_SOURCE'
    # does), and I don't want to mess with bison now.  A .upper() might be a
    # good idea too, but the MS def'n v.2 does not say whether OBS_MODE should
    # be case-insensitive.
    intent = intent.replace(', ', ',')

    if '^' in spw:
        casalog.post("The interpretation of ^n in split's spw strings has changed from 'average n' to 'skip n' channels!", 'WARN')
        casalog.post("Watch for Slicer errors", 'WARN')
        
    if type(width) == str:
        try:
            if(width.isdigit()):
                width=[string.atoi(width)]
            elif(width.count('[') == 1 and width.count(']') == 1):
                width = width.replace('[', '')
                width = width.replace(']', '')
                splitwidth = width.split(',')
                width = []
                for ws in splitwidth:
                    if(ws.isdigit()):
                        width.append(string.atoi(ws)) 
            else:
                width = [1]
        except:
            raise TypeError, 'parameter width is invalid...using 1'

    if type(correlation) == list:
        correlation = ', '.join(correlation)
    correlation = correlation.upper()

    if hasattr(combine, '__iter__'):
        combine = ', '.join(combine)

    if type(spw) == list:
        spw = ','.join([str(s) for s in spw])
    elif type(spw) == int:
        spw = str(spw)
    do_chan_mod = spw.find('^') > -1     # '0:2~11^1' would be pointless.
    if not do_chan_mod:                  # ...look in width.
        if type(width) == int and width > 1:
            do_chan_mod = True
        elif hasattr(width, '__iter__'):
            for w in width:
                if w > 1:
                    do_chan_mod = True
                    break

    do_both_chan_and_time_mod = (do_chan_mod and
                                 string.atof(timebin[:-1]) > 0.0)
    if do_both_chan_and_time_mod:
        # Do channel averaging first because it might be included in the spw
        # string.
        import tempfile
        # We want the directory outputvis is in, not /tmp, because /tmp
        # might not have enough space.
        # outputvis is itself a directory, so strip off a trailing slash if
        # it is present.
        # I don't know if giving tempfile an absolute directory is necessary -
        # dir='' is effectively '.' in Ubuntu.
        workingdir = os.path.abspath(os.path.dirname(outputvis.rstrip('/')))
        cavms = tempfile.mkdtemp(suffix=outputvis, dir=workingdir)

        casalog.post('Channel averaging to ' + cavms)
        if not myms.split(outputms=cavms,     field=field,
                          spw=spw,            step=width,
                          baseline=antenna,   subarray=array,
                          timebin='',         time=timerange,
                          whichcol=datacolumn,
                          scan=scan,          uvrange=uvrange,
                          combine=combine,
                          correlation=correlation, intent=intent,
                          obs=str(observation)):
            myms.close()
            if os.path.isdir(cavms):
                import shutil
                shutil.rmtree(cavms)
            return False
        
        # The selection was already made, so blank them before time averaging.
        field = ''
        spw = ''
        width = [1]
        antenna = ''
        array = ''
        timerange = ''
        datacolumn = 'all'
        scan = ''
        intent = ''
        uvrange = ''
        observation = ''

        myms.close()
        myms.open(cavms)
        casalog.post('Starting time averaging')

    if keepflags:
        taqlstr = ''
    else:
        taqlstr = 'NOT (FLAG_ROW OR ALL(FLAG))'

    if not myms.split(outputms=outputvis,  field=field,
                      spw=spw,             step=width,
                      baseline=antenna,    subarray=array,
                      timebin=timebin,     time=timerange,
                      whichcol=datacolumn,
                      scan=scan,           uvrange=uvrange,
                      combine=combine,
                      correlation=correlation,
                      taql=taqlstr, intent=intent,
                      obs=str(observation)):
        myms.close()
        return False
    myms.close()

    if do_both_chan_and_time_mod:
        import shutil
        shutil.rmtree(cavms)

    # Write history to output MS, not the input ms.
    try:
        param_names = split_core.func_code.co_varnames[:split_core.func_code.co_argcount]
        param_vals = [eval(p) for p in param_names]   
        retval &= write_history(myms, outputvis, 'oldsplit', param_names, param_vals,
                                casalog)
    except Exception, instance:
        casalog.post("*** Error \'%s\' updating HISTORY" % (instance),
                     'WARN')
예제 #4
0
def uvcontsub3(vis, fitspw, combine, fitorder, field, spw,
               scan, intent, correlation, observation):
    """Extract the line(s) of an MS."""
    retval = True
    casalog.origin('uvcontsub3')

    myms = mstool()
    mytb = tbtool()
    # This one is redundant - it is already checked at the XML level.
    if not ((type(vis) == str) and os.path.isdir(vis)):
        casalog.post('Visibility data set not found - please verify the name', 'SEVERE')
        return False

    outputvis = vis + '.contsub'
    if os.path.exists(outputvis):
        casalog.post("Output MS " + outputvis + " already exists - will not overwrite.", 'SEVERE')
        return False

    if combine and combine.lower() != 'spw':
        casalog.post("uvcontsub3 deliberately does not support combination by",
                     'SEVERE')
        casalog.post("anything except spw.", 'SEVERE')
        return False

    # MSStateGram is picky ('CALIBRATE_WVR.REFERENCE, OBSERVE_TARGET_ON_SOURCE'
    # doesn't work, but 'CALIBRATE_WVR.REFERENCE,OBSERVE_TARGET_ON_SOURCE'
    # does), and I don't want to mess with bison now.  A .upper() might be a
    # good idea too, but the MS def'n v.2 does not say whether OBS_MODE should
    # be case-insensitive.
    intent = intent.replace(', ', ',')

    if type(spw) == list:
        spw = ','.join([str(s) for s in spw])
    elif type(spw) == int:
        spw = str(spw)

    ## if ':' in spw:
    ##     casalog.post("uvcontsub3 does not yet support selection by channel for the output",
    ##                  'SEVERE')
    ##     casalog.post("Meanwhile, use split to select the desired channels", 'WARN')
    ##     return False

    if ';' in spw:
        casalog.post("uvcontsub3 does not yet support writing multiple channel groups per output spw",
                     'SEVERE')
        return False

    mytb.open(vis + '/SPECTRAL_WINDOW')
    allspw = '0~' + str(mytb.nrows() - 1)
    mytb.close()
    if 'spw' not in combine:
        spwmfitspw = subtract_spws(spw, fitspw)
        if spwmfitspw == 'UNKNOWN':
            spwmfitspw = subtract_spws(allspw, fitspw)
        if spwmfitspw:
            raise Exception, "combine must include 'spw' when the fit is being applied to spws outside fitspw."

    if type(correlation) == list:
        correlation = ', '.join(correlation)
    correlation = correlation.upper()

    mytb.open(vis, nomodify=True)
    if 'CORRECTED_DATA' in mytb.colnames():
        datacolumn = 'CORRECTED_DATA'
    else:
        # DON'T remind the user that split before uvcontsub wastes time -
        # scratch columns will eventually go away.
        datacolumn = 'DATA'
    mytb.close()

    myms.open(vis, nomodify=True)
    if not myms.contsub(outputms=outputvis,   fitspw=fitspw,
                        fitorder=fitorder,    combine=combine,
                        spw=spw,              unionspw=join_spws(fitspw, spw),
                        field=field,          scan=scan,
                        intent=intent,        correlation=correlation,
                        obs=str(observation), whichcol=datacolumn):
        myms.close()
        return False
    myms.close()

    # Write history to output MS, not the input ms.
    try:
        param_names = uvcontsub3.func_code.co_varnames[:uvcontsub3.func_code.co_argcount]
        param_vals = [eval(p) for p in param_names]   
        retval &= write_history(myms, outputvis, 'uvcontsub3', param_names, param_vals,
                                casalog)
    except Exception, instance:
        casalog.post("*** Error \'%s\' updating HISTORY" % (instance),
                     'WARN')
예제 #5
0
        if len(correlation)>0:
            correlation=''
            casalog.post('Correlation selection in oldstatwt has been disabled as of CASA v4.5', 'WARN')

        myms.open(vis, nomodify=False)
        retval = myms.oldstatwt(dorms, byantenna, sepacs, fitspw, fitcorr, combine,
                             timebin, minsamp, field, spw, antenna, timerange, scan, intent,
                             array, correlation, obs, datacolumn)
        myms.close()
    except Exception, e:
        casalog.post("Error setting WEIGHT and SIGMA for %s:" % vis, 'SEVERE')
        casalog.post("%s" % e, 'SEVERE')
        if False:  # Set True for debugging.
            for p in oldstatwt.func_code.co_varnames[:statwt.func_code.co_argcount]:
                v = eval(p)
                print p, "=", v, ", type =", type(v)
        retval = False

    if retval:
        try:
            param_names = oldstatwt.func_code.co_varnames[:oldstatwt.func_code.co_argcount]
            param_vals = [eval(p) for p in param_names]
            retval &= write_history(myms, vis, 'oldstatwt', param_names, param_vals,
                                    casalog)
        except Exception, instance:
            casalog.post("*** Error \'%s\' updating HISTORY" % (instance),
                         'WARN')        
    return retval
        
    
예제 #6
0
def uvcontsub3(vis, fitspw, combine, fitorder, field, spw, scan, intent,
               correlation, observation):
    """Extract the line(s) of an MS."""
    retval = True
    casalog.origin('uvcontsub3')

    myms = mstool()
    mytb = tbtool()
    # This one is redundant - it is already checked at the XML level.
    if not ((type(vis) == str) and os.path.isdir(vis)):
        casalog.post('Visibility data set not found - please verify the name',
                     'SEVERE')
        return False

    outputvis = vis + '.contsub'
    if os.path.exists(outputvis):
        casalog.post(
            "Output MS " + outputvis + " already exists - will not overwrite.",
            'SEVERE')
        return False

    if combine and combine.lower() != 'spw':
        casalog.post("uvcontsub3 deliberately does not support combination by",
                     'SEVERE')
        casalog.post("anything except spw.", 'SEVERE')
        return False

    # MSStateGram is picky ('CALIBRATE_WVR.REFERENCE, OBSERVE_TARGET_ON_SOURCE'
    # doesn't work, but 'CALIBRATE_WVR.REFERENCE,OBSERVE_TARGET_ON_SOURCE'
    # does), and I don't want to mess with bison now.  A .upper() might be a
    # good idea too, but the MS def'n v.2 does not say whether OBS_MODE should
    # be case-insensitive.
    intent = intent.replace(', ', ',')

    if type(spw) == list:
        spw = ','.join([str(s) for s in spw])
    elif type(spw) == int:
        spw = str(spw)

    ## if ':' in spw:
    ##     casalog.post("uvcontsub3 does not yet support selection by channel for the output",
    ##                  'SEVERE')
    ##     casalog.post("Meanwhile, use split to select the desired channels", 'WARN')
    ##     return False

    if ';' in spw:
        casalog.post(
            "uvcontsub3 does not yet support writing multiple channel groups per output spw",
            'SEVERE')
        return False

    mytb.open(vis + '/SPECTRAL_WINDOW')
    allspw = '0~' + str(mytb.nrows() - 1)
    mytb.close()
    if 'spw' not in combine:
        spwmfitspw = subtract_spws(spw, fitspw)
        if spwmfitspw == 'UNKNOWN':
            spwmfitspw = subtract_spws(allspw, fitspw)
        if spwmfitspw:
            raise Exception, "combine must include 'spw' when the fit is being applied to spws outside fitspw."

    if type(correlation) == list:
        correlation = ', '.join(correlation)
    correlation = correlation.upper()

    mytb.open(vis, nomodify=True)
    if 'CORRECTED_DATA' in mytb.colnames():
        datacolumn = 'CORRECTED_DATA'
    else:
        # DON'T remind the user that split before uvcontsub wastes time -
        # scratch columns will eventually go away.
        datacolumn = 'DATA'
    mytb.close()

    myms.open(vis, nomodify=True)
    if not myms.contsub(outputms=outputvis,
                        fitspw=fitspw,
                        fitorder=fitorder,
                        combine=combine,
                        spw=spw,
                        unionspw=join_spws(fitspw, spw),
                        field=field,
                        scan=scan,
                        intent=intent,
                        correlation=correlation,
                        obs=str(observation),
                        whichcol=datacolumn):
        myms.close()
        return False
    myms.close()

    # Write history to output MS, not the input ms.
    try:
        param_names = uvcontsub3.func_code.co_varnames[:uvcontsub3.func_code.
                                                       co_argcount]
        param_vals = [eval(p) for p in param_names]
        retval &= write_history(myms, outputvis, 'uvcontsub3', param_names,
                                param_vals, casalog)
    except Exception, instance:
        casalog.post("*** Error \'%s\' updating HISTORY" % (instance), 'WARN')
예제 #7
0
def importfitsidi(fitsidifile, vis, constobsid=None, scanreindexgap_s=None):
    """Convert FITS-IDI visibility file into a CASA visibility file (MS).

	Keyword arguments:
	fitsidifile -- Name(s) of input FITS IDI file(s)
		default: None; example='3C273XC1.IDI' or ['3C273XC1.IDI1', '3C273XC1.IDI2']
	vis -- Name of output visibility file (MS)
		default: None; example: vis='3C273XC1.ms'
		
	constobsid -- If True a constant obs id == 0  of is given to all input files 
	        default = False (new obs id for each input file)

	scanreindexgap_s --  if > 0., a new scan is started whenever the gap between two
                integrations is > the given value (seconds) or when a new field starts
                default = 0. (no reindexing)
	"""

    #Python script
    retval = True
    try:
        casalog.origin('importfitsidi')
        casalog.post("")
        myms = mstool()
        mytb = tbtool()
        if (type(fitsidifile) == str):
            casalog.post('### Reading file ' + fitsidifile, 'INFO')
            myms.fromfitsidi(vis, fitsidifile)
            myms.close()
        elif (type(fitsidifile) == list):
            clist = fitsidifile
            casalog.post('### Reading file ' + clist[0], 'INFO')
            myms.fromfitsidi(vis, clist[0])
            myms.close()
            clist.pop(0)
            tname = '_importfitsidi_tmp_' + vis
            shutil.rmtree(tname, ignore_errors=True)
            for fidifile in clist:
                casalog.post('### Reading file ' + fidifile, 'INFO')
                myms.fromfitsidi(tname, fidifile)
                myms.close()
                myms.open(vis, nomodify=False)
                myms.concatenate(msfile=tname, freqtol='', dirtol='')
                myms.close()
                shutil.rmtree(tname, ignore_errors=True)
        else:
            raise Exception, 'Parameter fitsidifile should be of type str or list'

        if (constobsid):
            mytb.open(vis + '/OBSERVATION', nomodify=False)
            nobs = mytb.nrows()
            cando = True
            if nobs > 1:
                casalog.post(
                    'Trying to keep obsid constant == 0 for all input files',
                    'INFO')
                # check if all observations are from the same telescope; if not warn and leave as is
                tels = mytb.getcol('TELESCOPE_NAME')
                for i in range(1, nobs):
                    if tels[i] != tels[0]:
                        cando = False

                if cando:
                    # get min and max time and write them into the first row;
                    casalog.post('Adjusting OBSERVATION table', 'INFO')
                    timeranges = mytb.getcol('TIME_RANGE')
                    ttr = timeranges.transpose()
                    newmin = min(ttr[0])
                    newmax = max(ttr[1])
                    mytb.putcell('TIME_RANGE', 0, [newmin, newmax])
                    # delete the other rows
                    mytb.removerows(range(1, nobs))
                else:
                    casalog.post(
                        'The input files stem from different telescopes. Need to give different obs id.',
                        'WARN')
            mytb.close()

            if cando:
                # give the same obs id == 0 to the entire output MS
                casalog.post('Setting observation ID of all integrations to 0',
                             'INFO')
                mytb.open(vis, nomodify=False)
                for i in xrange(0, mytb.nrows()):
                    mytb.putcell('OBSERVATION_ID', i, 0)
                mytb.close()

        else:  # don't want constant obs id
            if (type(fitsidifile) == list and len(fitsidifile) > 1):
                casalog.post(
                    'Incrementing observation ID for each input file ...',
                    'INFO')

        if (scanreindexgap_s > 0.):
            # reindex the scan column
            mytb.open(vis, nomodify=False)
            times = mytb.getcol('TIME')
            fields = mytb.getcol('FIELD_ID')
            arrayids = mytb.getcol('ARRAY_ID')
            scannumbers = mytb.getcol('SCAN_NUMBER')

            timesorted = np.argsort(np.array(times))

            scannumber = 1
            prevtime = times[timesorted[0]]
            prevfield = fields[timesorted[0]]
            prevarrayid = arrayids[timesorted[0]]
            scannumbers[timesorted[0]] = scannumber

            for i in xrange(1, mytb.nrows()):
                ii = timesorted[i]
                timenow = times[ii]
                fieldnow = fields[ii]
                arrayidnow = arrayids[ii]
                if (timenow-prevtime > scanreindexgap_s) \
                     or (fieldnow != prevfield) \
                     or (arrayidnow != prevarrayid):
                    scannumber += 1
                    casalog.post("Starting new scan "+str(scannumber)+" at "+str(timenow)\
                           +", field "+str(fieldnow)+", array_id "+str(arrayidnow), 'INFO')
                scannumbers[ii] = scannumber
                prevtime = timenow
                prevfield = fieldnow
                prevarrayid = arrayidnow

            mytb.putcol('SCAN_NUMBER', scannumbers)
            mytb.close()

    # write history
        try:
            param_names = importfitsidi.func_code.co_varnames[:importfitsidi.
                                                              func_code.
                                                              co_argcount]
            param_vals = [eval(p) for p in param_names]
            retval &= write_history(myms, vis, 'importfitsidi', param_names,
                                    param_vals, casalog)

        except Exception, instance:
            casalog.post("*** Error \'%s\' updating HISTORY" % (instance),
                         'WARN')

    except Exception, instance:
        print '*** Error ***', instance
        shutil.rmtree('_importfitsidi_tmp_' + vis, ignore_errors=True)
        raise Exception, instance
예제 #8
0
            minsamp,
            field,
            spw,
            antenna,
            timerange,
            scan,
            intent,
            array,
            correlation,
            obs,
            datacolumn,
        )
        myms.close()
    except Exception, e:
        casalog.post("Error setting WEIGHT and SIGMA for %s:" % vis, "SEVERE")
        casalog.post("%s" % e, "SEVERE")
        if False:  # Set True for debugging.
            for p in statwt.func_code.co_varnames[: statwt.func_code.co_argcount]:
                v = eval(p)
                print p, "=", v, ", type =", type(v)
        retval = False

    if retval:
        try:
            param_names = statwt.func_code.co_varnames[: statwt.func_code.co_argcount]
            param_vals = [eval(p) for p in param_names]
            retval &= write_history(myms, vis, "statwt", param_names, param_vals, casalog)
        except Exception, instance:
            casalog.post("*** Error '%s' updating HISTORY" % (instance), "WARN")
    return retval
예제 #9
0
def importfitsidi(fitsidifile,vis,constobsid=None,scanreindexgap_s=None):
	"""Convert FITS-IDI visibility file into a CASA visibility file (MS).

	Keyword arguments:
	fitsidifile -- Name(s) of input FITS IDI file(s)
		default: None; example='3C273XC1.IDI' or ['3C273XC1.IDI1', '3C273XC1.IDI2']
	vis -- Name of output visibility file (MS)
		default: None; example: vis='3C273XC1.ms'
		
	constobsid -- If True a constant obs id == 0  of is given to all input files 
	        default = False (new obs id for each input file)

	scanreindexgap_s --  if > 0., a new scan is started whenever the gap between two
                integrations is > the given value (seconds) or when a new field starts
                default = 0. (no reindexing)
	"""

	#Python script
        retval = True
	try:
		casalog.origin('importfitsidi')
		casalog.post("")
                myms = mstool()
		mytb = tbtool()
		if(type(fitsidifile)==str):
			casalog.post('### Reading file '+fitsidifile, 'INFO')
			myms.fromfitsidi(vis,fitsidifile)
			myms.close()
		elif(type(fitsidifile)==list):
			clist = fitsidifile
			casalog.post('### Reading file '+clist[0], 'INFO')
			myms.fromfitsidi(vis,clist[0])
			myms.close()
			clist.pop(0)
			tname = '_importfitsidi_tmp_'+vis
			shutil.rmtree(tname, ignore_errors=True)
			for fidifile in clist:
				casalog.post('### Reading file '+fidifile, 'INFO')
				myms.fromfitsidi(tname,fidifile)
				myms.close()
				myms.open(vis, nomodify=False)
				myms.concatenate(msfile=tname, freqtol='', dirtol='')
				myms.close()
				shutil.rmtree(tname, ignore_errors=True)
		else:
                        raise Exception, 'Parameter fitsidifile should be of type str or list'			

		if (constobsid):
			mytb.open(vis+'/OBSERVATION', nomodify=False)
			nobs = mytb.nrows()
			cando = True
			if nobs>1:
				casalog.post('Trying to keep obsid constant == 0 for all input files', 'INFO')
				# check if all observations are from the same telescope; if not warn and leave as is
				tels = mytb.getcol('TELESCOPE_NAME')
				for i in range(1,nobs):
					if tels[i]!=tels[0]:
						cando = False

				if cando:
					# get min and max time and write them into the first row;
					casalog.post('Adjusting OBSERVATION table', 'INFO')
					timeranges = mytb.getcol('TIME_RANGE')
					ttr = timeranges.transpose()
					newmin = min(ttr[0])
					newmax = max(ttr[1])
					mytb.putcell('TIME_RANGE', 0, [newmin,newmax])
					# delete the other rows
					mytb.removerows(range(1,nobs))
				else:
					casalog.post('The input files stem from different telescopes. Need to give different obs id.', 'WARN')
			mytb.close()
			
			if cando:
				# give the same obs id == 0 to the entire output MS
				casalog.post('Setting observation ID of all integrations to 0', 'INFO')
				mytb.open(vis, nomodify=False)
				for i in xrange(0, mytb.nrows()):
					mytb.putcell('OBSERVATION_ID', i, 0)
				mytb.close()


		else: # don't want constant obs id
			if(type(fitsidifile)==list and len(fitsidifile)>1):
				casalog.post('Incrementing observation ID for each input file ...', 'INFO')
			
		if (scanreindexgap_s > 0.):
			# reindex the scan column
			mytb.open(vis, nomodify=False)
			times = mytb.getcol('TIME')
			fields = mytb.getcol('FIELD_ID')
			arrayids = mytb.getcol('ARRAY_ID')
			scannumbers = mytb.getcol('SCAN_NUMBER')

			timesorted = np.argsort(np.array(times)) 

			scannumber = 1
			prevtime = times[timesorted[0]]
			prevfield = fields[timesorted[0]]
			prevarrayid = arrayids[timesorted[0]]
			scannumbers[timesorted[0]] = scannumber

			for i in xrange(1,mytb.nrows()):
				ii = timesorted[i]
				timenow = times[ii]
				fieldnow = fields[ii]
				arrayidnow = arrayids[ii]
				if (timenow-prevtime > scanreindexgap_s) \
					    or (fieldnow != prevfield) \
					    or (arrayidnow != prevarrayid):
					scannumber += 1
					casalog.post("Starting new scan "+str(scannumber)+" at "+str(timenow)\
							     +", field "+str(fieldnow)+", array_id "+str(arrayidnow), 'INFO')
				scannumbers[ii] = scannumber
				prevtime = timenow
				prevfield = fieldnow
				prevarrayid = arrayidnow

			mytb.putcol('SCAN_NUMBER', scannumbers)	
			mytb.close()
		
	        # write history
                try:
                        param_names = importfitsidi.func_code.co_varnames[:importfitsidi.func_code.co_argcount]
                        param_vals = [eval(p) for p in param_names]   
                        retval &= write_history(myms, vis, 'importfitsidi', param_names,
                                                param_vals, casalog)

                except Exception, instance:
                        casalog.post("*** Error \'%s\' updating HISTORY" % (instance),
                                     'WARN')

	except Exception, instance: 
		print '*** Error ***',instance
		shutil.rmtree('_importfitsidi_tmp_'+vis, ignore_errors=True)
		raise Exception, instance