Coverage for /wheeldirectory/casa-6.7.0-12-py3.10.el8/lib/py/lib/python3.10/site-packages/casatasks/private/imtools.py: 9%

34 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-10-31 19:10 +0000

1import shutil 

2from casatools import image, table 

3 

4def pixelmask2cleanmask(imagename='',maskname='mask0',maskimage='',usemasked=False): 

5 """ 

6 convert pixel(T/F) mask (in a CASA image) to a mask image (1/0) 

7 used for clean 

8 imagename - input imagename that contain a mask to be used 

9 maskname - mask name in the image (default: mask0) 

10 maskimage - output mask image name 

11 usemasked - if True use masked region as a valid region 

12 """ 

13 ia = image( ) 

14 ia.open(imagename) 

15 masks=ia.maskhandler('get') 

16 ia.close() 

17 

18 inmaskname='' 

19 if type(masks)!=list: 

20 masks=[masks] 

21 for msk in masks: 

22 if maskname == msk: 

23 inmaskname=msk 

24 break 

25 if inmaskname=='': 

26 raise Exception("mask %s does not exist. Available masks are: %s" % (maskname,masks)) 

27 

28 tb = table( ) 

29 tb.open(imagename+'/'+maskname) 

30 dat0=tb.getcol('PagedArray') 

31 tb.close() 

32 

33 #os.system('cp -r %s %s' % (imagename, maskimage)) 

34 shutil.copytree(imagename,maskimage) 

35 ia.open(maskimage) 

36 # to unset mask 

37 ia.maskhandler('set',['']) 

38 # make all valid 

39 if (usemasked): 

40 ia.set(1) 

41 else: 

42 ia.set(0) 

43 ia.close() 

44 # 

45 tb.open(maskimage,nomodify=False) 

46 imd=tb.getcol('map') 

47 # maybe shape check here 

48 #by default use True part of bool mask 

49 masked=1 

50 if (usemasked): masked=0 

51 imd[dat0]=masked 

52 tb.putcol('map',imd) 

53 tb.close()