casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
checkdataformat.py
Go to the documentation of this file.
00001 # guess what kind (format) of the input data is
00002 import commands
00003 import os
00004 from  casac import *
00005 
00006 tb = casac.table()
00007 
00008 # main function 
00009 def dataformat(dataname):
00010     """
00011     find(guess) the data format of the input data
00012     by quick checks on the data without opening or loading
00013     fullly.
00014     Currently check if it is MS, ALMA/EVLA SDM with its version,
00015     ASAP scantable with its version, CASA image, some kind of
00016     FITS, or ASCII text. 
00017     """    
00018     isMS = False
00019     isASDM = False
00020     isASAP = False
00021     isCASAimage = False
00022     dataformat = 'unknown'
00023 
00024 
00025     # directory?
00026     if(commands.getoutput('file '+dataname).count('directory')):
00027         # check for MS, ASDM, scantable..., CASA image, ... 
00028         # try to ms.open
00029         try:
00030             checkms(dataname)
00031             isMS = True
00032         except:
00033             isMS = False
00034             #check for ASDM
00035             (isASDM,isEVLA,ver)=checkasdm(dataname)
00036             #if not isASDM
00037             if not isASDM:
00038                 # is this ASAP scantable (for Single dish)
00039                 try:
00040                     (isASAP,ver)=checkscantable(dataname)
00041                 except:
00042                     # try if it is a CASA image
00043                     if(os.path.exists(dataname+'/table.dat')):
00044                          tb.open(dataname)
00045                          if tb.keywordnames().count('imageinfo')>0:
00046                              isCASAimage=True
00047                              dataformat = "CASA image"
00048                          elif tb.colnames()=='map' and \
00049                            any([k=='coords' for k in tb.keywordnames()]):
00050                              isCASAimage=True
00051                              dataformat ="CASA image" 
00052 
00053                          # todo: check for component?
00054     
00055                          tb.close()
00056         finally:
00057             if isMS:
00058                 dataformat="MeasurementSet"
00059             if isASDM:
00060                 if isEVLA:
00061                     dataformat="EVLA SDM"+ver
00062                 else:
00063                     dataformat="ALMA SDM"+ver
00064             if isASAP:
00065                 dataformat="ASAP Scantable ver."+ver
00066 
00067             print "%s is appeared to be %s " % (dataname, dataformat)
00068     elif(commands.getoutput('file '+dataname).count('text')):
00069          dataformat='ASCII'
00070     elif(commands.getoutput('file '+dataname).count('FITS')):
00071          print "Probably some kind of FITS (e.g. image fits,  uvfits, etc)" 
00072          dataformat='FITS'
00073 
00074     return dataformat
00075 
00076 
00077 def checkms(dname):
00078     """
00079     check if the input data is an MS
00080     """
00081     isMS = False
00082     mstables= set(["table.dat",
00083                    "ANTENNA/table.dat",
00084                    "DATA_DESCRIPTION/table.dat",
00085                    "FEED/table.dat",
00086                    "FIELD/table.dat",
00087                    "FLAG_CMD/table.dat",
00088                    "HISTORY/table.dat",
00089                    "OBSERVATION/table.dat",
00090                    "POINTING/table.dat",
00091                    "POLARIZATION/table.dat",
00092                    "PROCESSOR/table.dat",
00093                    "SPECTRAL_WINDOW/table.dat",
00094                    "STATE/table.dat"
00095                    ])
00096     for dat in mstables:
00097         if not os.path.exists(dname+'/'+dat):
00098             isMS=False
00099             raise Exception
00100         else:
00101             isMS=True
00102     return 
00103     
00104 
00105 def checkasdm(dname):
00106     """
00107     check if input data is ALMA/EVLA SDM
00108     """
00109     isASDM=False
00110     isEVLA=False
00111     version =''
00112     if(os.path.exists(dname+'/ASDM.xml')):
00113         from xml.etree.ElementTree import ElementTree
00114         rt = ElementTree(file=dname+'/ASDM.xml')
00115         iter = rt.getiterator()
00116         for k, n in iter[1].items():
00117             if n =='ASDM':
00118                 isASDM=True
00119             if k=='schemaVersion':
00120                 #if int(n) == 1:
00121                 #    ver='2'
00122                 #else:
00123                 #    ver = str(n)
00124                 ver = str(n)
00125                 version='v1.'+ver
00126             elif k=='entityId':
00127                 if n.count('evla'):
00128                     isEVLA=True
00129     else:
00130         isASDM=False
00131         isEVLA=False
00132 
00133     return (isASDM,isEVLA,version)     
00134 
00135 
00136 def checkscantable(dname):
00137     """
00138     check if the input data is ASAP Scantable 
00139     """
00140     scantables = set(["table.dat",
00141                       "FREQUENCIES/table.dat",
00142                       "WEATHER/table.dat",
00143                       "FOCUS/table.dat",
00144                       "TCAL/table.dat",
00145                       "MOLECULES/table.dat",
00146                       "HISTORY/table.dat",
00147                       "FIT/table.dat"
00148                       ]) 
00149     for dat in scantables:
00150         if not os.path.exists(dname+'/'+dat):
00151             raise Exception
00152     tb.open(dname)
00153     version=tb.getkeyword('VERSION')
00154     tb.close()
00155     return (True, str(version))