diff options
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/gen_points.py | 81 | ||||
-rw-r--r-- | bin/img2list.py | 28 |
2 files changed, 109 insertions, 0 deletions
diff --git a/bin/gen_points.py b/bin/gen_points.py new file mode 100755 index 0000000..57733dc --- /dev/null +++ b/bin/gen_points.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Aaron LI +# 2015/06/19 + +""" +Generate the required number of random points within the required region. +""" + +__version__ = "0.1.0" +__date__ = "2015/06/19" +DEBUG = True + +import sys +import argparse +import random +import time +import re + +from rand.sphere import sphere_point +from region.region import Region + +random.seed(time.time()) + + +def parse_region(regstring): + reg_par = re.sub(r"[(),]", " ", regstring).split() + regtype = reg_par[0].lower() + if regtype == "box": + xc = float(reg_par[1]) + yc = float(reg_par[2]) + width = parse_reg_value(reg_par[3]) + height = parse_reg_value(reg_par[4]) + rotation = float(reg_par[5]) + reg = Region(regtype, xc=xc, yc=yc, + width=width, height=height, rotation=rotation) + else: + raise ValueError("region type '%s' currently not implemented" % regtype) + return reg + + +def parse_reg_value(valstring): + if valstring[-1] == '"': + # arcsec -> deg + value = float(valstring.split('"')[0]) / 60.0 / 60.0 + elif valstring[-1] == "'": + # arcmin -> deg + value = float(valstring.split("'")[0]) / 60.0 + else: + value = float(valstring) + return value + + +def main(): + parser = argparse.ArgumentParser( + description="Generate random point within the given region.") + parser.add_argument("-V", "--version", action="version", + version="%(prog)s " + "%s (%s)" % (__version__, __date__)) + parser.add_argument("-n", "--number", dest="number", + type=int, default=1, + help="number of points to be generated") + parser.add_argument("-r", "--region", dest="region", required=True, + help="DS9 region") + args = parser.parse_args() + + reg = parse_region(args.region) + if DEBUG: + print("DEBUG: region: ", reg.dump(), file=sys.stderr) + + points = [] + while len(points) < args.number: + p = sphere_point(unit="deg") + if reg.is_inside(p): + points.append(p) + print("%s %s" % p) + + +if __name__ == "__main__": + main() + diff --git a/bin/img2list.py b/bin/img2list.py new file mode 100644 index 0000000..48d0de4 --- /dev/null +++ b/bin/img2list.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Aaron LI +# 2015/06/23 +# + + +import numpy as np +from astropy.io import fits + + +def img2list(imgdata, mask=None): + """ + Convert a image matrix to list of point coordinates. + The input image matrix is taken as an integer matrix. + If one pixel has value n (>1), then it is repeated n times. + """ + img = imgdata.astype(int) + points = [] + ii, jj = np.nonzero(img >= 1) + while len(ii) > 0: + for i, j in zip(ii, jj): + points.append([i, j]) + img[ii, jj] -= 1 + ii, jj = np.nonzero(img >= 1) + return np.array(points) + |