casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
setOrder.py
Go to the documentation of this file.
00001 #####################################
00002 ###  Re-sort an MS, to optimize for the CASA access pattern.
00003 ###
00004 ###  Desired order :
00005 ###        ARRAY
00006 ###        FIELD
00007 ###        SPW (or data_desc_id)
00008 ###        TIME
00009 ###        ANTENNA1
00010 ###        ANTENNA2
00011 ###
00012 ###  -- Antenna1,2 are the fastest-varying axes.
00013 ###  -- Data are by-default internally partitioned on field, spw, array (or observation).
00014 ###  -- For each field/spw/observation, data is accessed in "buffers" that 
00015 ###     contain all baselines for one timestep.
00016 ###
00017 ###  To check if an MS is in the correct order, open it in
00018 ###  'casabrowser', and check the ANTENNA1, ANTENNA2, 
00019 ###  DATA_DESC_ID,  FIELD_ID, ARRAY_ID, and TIME columns.
00020 ###
00021 #####################################
00022 
00023 #!/usr/bin/env python
00024 
00025 import os;
00026 import shutil;
00027 from  casac import *;
00028 
00029 def setToCasaOrder(inputMS='',outputMS=''):
00030   """
00031   This script will re-sort the input MS into the following sort order
00032      ARRAY,  FIELD, SPW (or data_desc_id),  TIME,  ANTENNA1, ANTENNA2
00033 
00034   For I/O operations, data are by-default partitioned on array, field and spw.
00035   CASA prefers data in which ANTENNA1,ANTENNA2 are the fastest-varying axes.
00036 
00037   To check if your MS is in the correct order (and if you need this or not),
00038   open the MS using 'casabrowser' or 'browsetable()', and look at the 
00039   ANTENNA1, ANTENNA2, DATA_DESC_ID,  FIELD_ID, ARRAY_ID, and TIME columns.
00040   The sort order in the MS should be apparant from the indices in these columns.
00041 
00042   Re-sorting makes the most difference in some cases when UVFITS files 
00043   written out of AIPS are read into CASA via importuvfits. Often, SPW (data_desc_id)
00044   is the fastest-varying index, and this is the worst-possible case for the CASA 
00045   data-access pattern. 
00046   Note : This is due to a difference in the choice of default sort order 
00047             in AIPS and CASA, which the importuvfits task currently ignores.
00048 
00049   This script will create a temporary reference-MS (a list of row indices containing the 
00050   mapping from the input to output MSs) called 'tmpreftable.tab' in the current
00051   working directory. Therefore, please do not run two instances of this script in
00052   the same directory. 
00053 
00054  """
00055   ## Make a local table tool
00056   tb = casac.table()
00057 
00058   ## Old MS Name
00059   if(not os.path.exists(inputMS)):
00060      print "Original MS ", inputMS, " not found ";
00061      return False;
00062 
00063   if(os.path.exists(outputMS)):
00064      print "An MS named ", outputMS, " already exists. Please delete it first, or choose another output MS name";
00065      return False;
00066 
00067   ## Make a reference table with the correct sort order.
00068   ## This creates a list of row-numbers that index
00069   ## into the data in the desired order.
00070   print "Making reference table.";
00071   tb.open(inputMS);
00072   tmptb = tb.query(name='tmpreftable.tab', query='DATA_DESC_ID>-1',sortlist='ARRAY_ID,FIELD_ID,DATA_DESC_ID,TIME,ANTENNA1,ANTENNA2');
00073   tmptb.close()
00074   tb.close();
00075 
00076   ## Save a new table with the sort-order
00077   ## specified by the list of indices in the
00078   ## reference table.
00079   print "Making deep-copy of ref-table";
00080   tb.open('tmpreftable.tab');
00081   tmptb = tb.copy(outputMS,deep=True);
00082   tmptb.close()
00083   tb.close();
00084 
00085   ## Remove the reference table
00086   print "Removing ref-table";
00087   if(os.path.exists('tmpreftable.tab')):
00088       shutil.rmtree('tmpreftable.tab');
00089 
00090 #####################################