2014年1月17日金曜日

count in a fits image with a region file filter


#!/usr/bin/env python

# 2014-01-17
# return count in a fits image with a region file
# outfile is overwrite

import sys,shlex, subprocess, os
import subprocess as sp

# input
image = sys.argv[1]
region = sys.argv[2]
outim = sys.argv[3]

run = True
if os.path.exists(outim):
#    os.remove(outim)
    print '%s exists and exit' % outim
    run = False

if run:
    expr = 'regfilter("%s",A.P1,A.P2)? A:0' % region
    command = 'ftimgcalc %s ¥'%s¥' a=%s ' % (outim, expr, image)
    print command
    args = shlex.split(command)
    p = sp.Popen(args)
    p.wait()

    if os.path.exists(outim):
        print '%s is created¥n' % outim

# count pixel

    args = shlex.split('fimgstat %s INDEF INDEF' % outim)
    p = sp.Popen(args)
    p.wait()

    output = sp.Popen(["pget", "fimgstat", "sum"], stdout=sp.PIPE).communicate()[0]
    print 'count=%d¥n' % int(output)










0 件のコメント:

コメントを投稿