casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
checknan.py
Go to the documentation of this file.
00001 from taskinit import *
00002 import numpy as np
00003 
00004 def CheckUVWForNan(ms='', fix=False):
00005     """
00006     script to check for NaN in uvw
00007     if fix is true it will flag the row with nan data
00008     and replace the uvw with 0
00009     you can see how to do that to other columns too
00010     """
00011     tb.open(ms, nomodify=(not fix))
00012     uvw=tb.getcol('UVW')
00013     flg=tb.getcol('FLAG_ROW')
00014     nanoo=np.isnan(uvw)
00015     for k in range(nanoo.shape[1]):
00016       if(nanoo[0,k] or nanoo[1,k] or nanoo[2,k]):
00017           print 'row=', k
00018           uvw[0,k]=0
00019           uvw[1,k]=0
00020           uvw[2,k]=0
00021           flg[k]=True
00022     if(fix):
00023         tb.putcol('UVW', uvw)
00024         tb.putcol('FLAG_ROW', flg)
00025     tb.done()
00026      
00027 ##########################################
00028 def CheckColsForNan(msname='',fix=False,colnames=[],timeinterval=1000):
00029     """
00030     Check for NaNs in specified columns of the MS.
00031     ms : Name of MS. 
00032     fix : False/True : If True, 
00033                              For data/corrected_data, set the corresponding value to zero and
00034                                        flag to True in the FLAG column
00035                              For weight, weight_spectrum, set the corresponding value to zero
00036                              For all other columns, do not fix/change anything.
00037     colnames : ['data','weight'] :  MS Column names on which to check for NaNs
00038     timeinterval : 1000 : timerange in seconds, to decide chunk sizes while iterating 
00039                                    through the MS (remember, chunks of this size are read 
00040                                    into python).
00041     Alternate (recommended) way to flag NaNs from data columns :
00042         tflagdata(vis=ms, mode='clip')    
00043     """
00044     ms = casac.ms()
00045 
00046     # Open the MS with the intent to modify it.
00047     ms.open(msname,nomodify=(not fix));
00048 
00049     # Iterate through the dataset in chunks defined by
00050     # a time interval of 1000 seconds.
00051     ms.iterinit(interval=timeinterval);
00052     
00053     ## Initialize the ms iterator
00054     ms.iterorigin();
00055     
00056     ## Iterate until the end of the MS is reached.
00057     counter=0;
00058     moretodo=True;
00059     while moretodo: 
00060         counter=counter+1;
00061         
00062         print 'Chunk : ', counter 
00063 
00064         resdat = ms.getdata(items=colnames);
00065   
00066         for cname in colnames:
00067             shp = resdat[cname].shape
00068             nancount = np.isnan( resdat[cname] )
00069             if np.any( nancount ) :
00070                 print 'Found  in chunk : ', counter, ' for col :', cname
00071 
00072                 if fix:
00073                     if cname.upper() in ['DATA','CORRECTED_DATA','MODEL_DATA'] :
00074                         flags = ms.getdata(items=['FLAG'])
00075                         flags['flag'] = flags['flag'] + nancount
00076                         resdat[cname][nancount==True] = complex(0.0,0.0)
00077                         flags[cname] = resdat[cname]
00078                         ms.putdata(flags)
00079                         print 'Setting value for column : ', cname , ' to zero and corresponding flag to True'
00080                     elif cname.upper() in ['WEIGHT','WEIGHT_SPECTRUM'] :
00081                         resdat[cname][nancount==True] = complex(0.0,0.0)
00082                         ms.putdata({cname:resdat[cname]})
00083                         print 'Setting value for column : ', cname , ' to zero'
00084                     else:
00085                         print "Cannot fix. Please use 'CheckUVWForNan()' for UVW column"
00086           
00087           
00088         moretodo = ms.iternext();
00089           
00090     ## Close the MS
00091     ms.close();
00092