显示标签为“方法”的博文。显示所有博文
显示标签为“方法”的博文。显示所有博文

2014年5月10日星期六

【image】Bad Pixel Mask using DS9 Region Files

The basic idea to create the mask image is simple. Firstly, you need to define the bad pixels with ds9 region like box and polygon (one can only use these two type regions only usually). Usually,  there is two main steps to realize the idea.
(1) convert the ds9 region to a uniform format. Both the box and polygon regions can be treated as polygons.
an example of an image mask. The green outline is the polygon drawn into SAOimage DS9. [FROM the website of galfit]


(2) solve the PIP (point in polygon) problems (http://en.wikipedia.org/wiki/Point_in_polygon) and mask all the points in the polygon. I attached one useful python script to get rid of the PIP problem.
  If you want to make two loops in python to find all the inside points, you may find the speed is too low! Please avoid to use 'For' in python. The fast way is to use the exist program in numpymatplotlib.nxutils.points_inside_pol as following:

import numpy as np
from matplotlib.nxutils import points_inside_poly

nx, ny = 10, 10
poly_verts = [(1,1), (5,1), (5,9),(3,2),(1,1)]

# Create vertex coordinates for each grid cell...
# (<0,0> is at the top left of the grid in this system)
x, y = np.meshgrid(np.arange(nx), np.arange(ny))
x, y = x.flatten(), y.flatten()

points = np.vstack((x,y)).T

grid = points_inside_poly(points, poly_verts)
grid = grid.reshape((ny,nx))

print grid
[From: http://stackoverflow.com/questions/3654289/scipy-create-2d-polygon-mask]

(3) save your masked array !

=======================================================================

Python script to decide whether the point is inside the polygon.


def point_in_poly(x,y,poly):
    '''
    Decide whether the point is inside the polygon.
    '''
    n = len(poly)
    inside = False

    p1x,p1y = poly[0]
    for i in range(n+1):
        p2x,p2y = poly[i % n]
        if y > min(p1y,p2y):
            if y <= max(p1y,p2y):
                if x <= max(p1x,p2x):
                    if p1y != p2y:
                        xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
                    if p1x == p2x or x <= xints:
                        inside = not inside
        p1x,p1y = p2x,p2y

    return inside    


=======================================================================

Two example to realize the idea:

(1) A copy from here ( http://www.cfht.hawaii.edu/~chyan/wircam/badpix/badpix.html)
The program "wircampolyregion" and other IDL programs are attached in the end of the website.

(2) Another way and program is mentioned in GALFIT (http://users.obs.carnegiescience.edu/peng/work/galfit/MASKING.html).

=======================================================================

5. IDL Source Code

; NAME:
;       MASKREGION
; PURPOSE:
;       Produce a new bad pixel mask based on an exist one and ds9 region file 
;
; CALLING SEQUENCE:
;       MASKREGION, BADPIX = badpix, REGION = ds9.reg, NEWBADPIX= newbadpix
;
; INPUTS:
;       BADPIX    -  string scalar, the name of the original bad pixel mask.
;       REGION    -  DS9 region file, only polygon and box allowed.
;       NEWBADPIX -  string scalar, the new bad pixel mask.
;
;
; REVISION HISTORY:
;       initial version   Chi-hung Yan   March 2007
;-



PRO RDWIRCAMCUBE, file, im
    im=fltarr(2048,2048,4)
    for i=0,3 do begin
        im[*,*,i]=mrdfits(file,i+1)
    endfor
END


PRO MASKPOLYREGION, im, ext, x, y
    ind=polyfillv(x,y,2048,2048)
    tim=im[*,*,ext-1]
    tim[ind]=!values.f_nan
    im[*,*,ext-1]=tim[*,*]
END

PRO WRWIRCAMCUBE, im, fits, new
    main_hd=headfits(fits,exten=0)
    mwrfits, 3,new , main_hd, /create
    for i=1,4 do begin
        hdr = headfits(fits, exten=i)
        mwrfits,im[*,*,i-1],new,hdr
    endfor
END


PRO MASKREGION, badpix=badpix, region=region, newbadpix=newbadpix
    file=region
    fits=badpix    
        
    ; Read image        
    rdwircamcube,fits,im
        
        
    spawn,"wircampolyregion "+file+" /tmp/region.txt"
    readcol,"/tmp/region.txt",id,ext,x,y
    n=0
    
    xx=fltarr(1)
     yy=fltarr(1)
    for i=0,n_elements(x)-1 do begin
        if i eq 0 then begin
            temp=id[i]
        endif else begin
            temp=id[i-1]
        endelse
        
        if i eq 0 then begin
            xx=x[i]
            yy=y[i]
        endif else if id[i] eq temp then begin
            xx=[xx,x[i]]
            yy=[yy,y[i]]
        endif else begin
            maskpolyregion,im,ext[i-1],xx,yy
            xx=x[i]
            yy=y[i]
            n=n+1
        endelse
        
    end
    spawn,'rm -rf /tmp/region.txt'
    wrwircamcube, im, fits, newbadpix
    
END

6. Perl Script


#! /usr/bin/perl 
#----------------------------------------------------------------------------
#                             wircampolyregion
#----------------------------------------------------------------------------
# Contents: This script parse the ds9 region file into better format.  The out
#                out file can be easily used for IDL program.
#----------------------------------------------------------------------------
# Part of:     WIRcam C pipeline                               
# From:        ASIAA                    
# Author:      Chi-hung Yan(chyan@asiaa.sinica.edu.tw)                       
#----------------------------------------------------------------------------

use warnings;
use strict;

# Define global variable
our $SCRIPT_NAME = 'wircampolyregion';

sub trim {
   my $string=$_;
   for ($string) {
       s/^\s+//;
       s/\s+$//;
    }
   return $string;
}

# This finction is used to return program usage.
sub usage(){

print<<EOF
 USAGE: $SCRIPT_NAME <ds9.reg> <output.txt>


 EXAMPLES:
  $SCRIPT_NAME ds9.reg output.txt

EOF
}

if(@ARGV == 0 || @ARGV != 2){
   usage();
   exit 0;
}

my $file = $ARGV[0];
my $i=0;
my $j=0;
my $ext;
my $reg=0;

open(OUT,">$ARGV[1]");
open(FILE,$file);
while(<FILE>){
    if (/^# tile/){
        my @chip=split(" ",$_);
        $ext = $chip[2];
    }
    if (/^polygon/){
        $reg=$reg+1;
        my @word=split(/[\(\)]/,$_);
        my @coord=split(",",$word[1]);
        my $n=@coord/2;
        for ($i=0;$i<$n;$i++){
            printf(OUT "%d %d %7.2f %7.2f\n",$reg,$ext,$coord[2*$i],$coord[2*$i+1]);
        }    
    }
    if (/^box/){
        $reg=$reg+1;
        my @word=split(/[\(\)]/,$_);
        my @para=split(",",$word[1]);
        printf("$word[1]\n");
         printf(OUT "%d %d %7.2f %7.2f\n",$reg,$ext,$para[0]-$para[2]/2,$para[1]-$para[3]/2);
         printf(OUT "%d %d %7.2f %7.2f\n",$reg,$ext,$para[0]+$para[2]/2,$para[1]-$para[3]/2);
         printf(OUT "%d %d %7.2f %7.2f\n",$reg,$ext,$para[0]+$para[2]/2,$para[1]+$para[3]/2);
         printf(OUT "%d %d %7.2f %7.2f\n",$reg,$ext,$para[0]-$para[2]/2,$para[1]+$para[3]/2);
    }    


}

close(OUT);

2014年2月13日星期四

英文写作常见错误以及检查事项

句法

(1) So作为句子的开头太频繁,可以适当使用“Therefore”, “Hence”, “Consequently”, “Thus”, “As a result”
(2) And”, “But”作为句子的开头太频繁,读起来很难受,可以适当使用 “However”“Nevertheless” 等。

(3) 科技论文中不用下列口语化的用法,应该在科技论文中避免:it's, can't, couldn't”,“doesn't等应该改成“it is”, “canno(t 美式英语)或者can no(t 英式或者欧式英语)”, “could not”,“does not”


(4) We作为句子的开头太频繁:只有作者特别想强调是我们做的,而不是别人做的时 候才使用“We”作为句子的开头,在一篇文章的开始总结部分可以适当地使用一些,正文 中间一般都是使用倒装句字。


(5) 要表达的主要含义放句子前面, 目的、位置、原因和时间等放句子后面。 

ADDITION:
(6) 不要用数字作为句子的开始。“One of the most prominent spectral features in star- burst galaxies is the presence of strong emission lines ...

作图

(1) figure, plot, picture, photo, illustration, schematic, diagram, flow chart 的区别 “figure”是统称 “plot”指数据点或者曲线画出的图, “picture”“photo”指图片或者照片或者 图片, “illustration”, “schematic”, “diagram” 都是指示意图,“flow chart”指流程图。

常用句型

(1) “A 随着B 增加而增加的正确翻译是:A increases with increasing B.或者“A increases with B.” 
(2)  不规则变化
单数
复数
单数
复数
datum
data
nucleus
nuclei
spectrum
spectra
focus
foci
medium
media
radius
radii
nova
novae
locus
loci
formula
formulae
torus
tori
index
indices
modulus
moduli
continuum
continua
(3) 科技文章中为了避免歧义,一般情况下使用单词的第一个意思。
    (a) for的意思包括为了因为,但是在科技文章里面只使用为了。需要因为的时 候用“because”或者“since”。再比如“as”的意思包括作为因为等,但是在科技文章里面 只使用作为
    (b) while主要有两个意思:...... 时候然而。但是科技英文尽可能使用单词的第一 个意思,因此建议只使用“while”...... 时候,当需要使用然而的时候用whereas
 (4) 大部分学生都知道even有几个意思,但是很多学生不清楚如何使用。举几个例子:
[1] Even though (虽然) a scientist does not make a lot of money, I still choose doing research
because ...
[2] Even if (尽管) this book costs a lot of money, I will still buy it because ...
[3] This book is very good, even (甚至) better than the most popular book written by... 
(5) similar to,different fromdiffer from”经常被误用为similar with或者“similar as”,different withdiffer with


标点符号
(1) 由于方程是句子的一部分,前后(主要是方程后面) 必须有标点符号
(2) 文件里面正确输入引号。正确的做法:`test’生成ps或者PDF
文件之后是‘test’. 但是大部分学生都是输入:’test’. 类似的情况是: ``test"而不是”test”.
(3) 为了避免一个句子太长,或者强调主要的内容,通常英文中分号后面是对前面的补充(中
文的分号是排比).分号“;”也常用于分隔的含有逗号的并列成分
(4)  短破折号en dash的使用. 也就是数学的负号(latex里面用 --,注意是两个-连着,或者$-$。 天体源的名字里面通常都有短破折号,但是通常都被错误地写成了连字符(见下面), 比如 GRO J1655--40,通常被错误地写成了GRO J1655-40Cyg X--1,通常被错误地写成 了Cyg X-1.


2013年10月28日星期一

[Oct28] The choice of slit size, one method in work

About the slit size:

You know tht the slit width will impact the spectroscopy. And do you know what is the proper width you should choose?

In an observation that you need both high resolution and perfect normalization, you can do like this. Observed with an narrow slit with enough exposure time to get the spectrum with enough resolution, then with a quite short time exposure with a wide enough slit which contain all of the flux of the source to get the real flux level. The width slit spectrum is only useful to determine the normalization of the flux. Just shift the the high resolution spectrum and you will get the perfect spectrum with both high resolution and real flux level.
You can have a simulation here: http://terpconnect.umd.edu/~toh/models/AbsSlitWidth.html


About the normal study:
    检查类比
Usually, you make a code or you make a simulation similar with others', but you do not know whether it is correct. Then try to compare the different results to make sure it is correct and also can help you to improve the results.