source: corel/corel.py

Last change on this file was 5, checked in by meynadie, 16 years ago

mises à jour diverses

  • Property svn:executable set to *
File size: 4.8 KB
Line 
1#!/bin/env python
2# coding: utf-8
3
4# Calcul du dépointage à partir d'images du collimateur
5# Frederic.Meynadier@aerov.jussieu.fr Janvier 2008
6
7
8import params
9
10import pyfits
11from optparse import OptionParser
12import sys
13
14from numpy import *
15from math import *
16
17#from fitgaussian import *
18
19VERSION='0.0'
20
21label = sys.argv[1]
22
23(ref,img,liste_points,sh_guess) = params.pset(label)
24
25
26sh_guess = array(sh_guess)
27
28size=128
29subsize=size/2
30
31for point in liste_points :
32    limites = [size,2048-size]
33    target = array(point) + sh_guess
34    print target
35    if (target[0] < limites[0] or target[0] > limites[1]\
36        or target[1] < limites[0] or target[1] > limites[1]):
37        print "Point ",point," hors cadre dans l'image destination"
38        sys.exit(1)
39
40cor_list = []
41for coords in liste_points :
42
43    tgt_ref=array(coords)
44    tgt_img = tgt_ref + sh_guess
45
46
47
48
49    ## parser = OptionParser()
50    ## parser.add_option("-i","--input",dest="InputFilename",
51    ##                   help="Input filename", metavar="RAWIMAGE")
52    ## parser.add_option("-o","--output",dest="OutputFilename",
53    ##                   help="Output Filename", metavar="FITSIMAGE")
54
55    #(options, args) = parser.parse_args ()
56
57
58    # Ouverture de l'image de référence
59    refhdulist = pyfits.open(ref)
60    refdata = array(refhdulist[0].data[tgt_ref[1]-size/2:tgt_ref[1]+size/2,\
61                                       tgt_ref[0]-size/2:tgt_ref[0]+size/2])
62    refhdulist[0].data = refdata
63    refhdulist.writeto('ref'+str(len(cor_list))+'.fits',clobber=True)
64
65
66    # Ouverture de l'image à mesurer
67
68    imghdulist = pyfits.open(img)
69    imgdata = imghdulist[0].data[tgt_img[1]-subsize/2:tgt_img[1]+subsize/2,\
70                                 tgt_img[0]-subsize/2:tgt_img[0]+subsize/2]
71
72    imghdulist[0].data = imgdata
73    imghdulist.writeto('img'+str(len(cor_list))+'.fits',clobber=True)
74
75    # Etablissement de la carte de corrélation
76
77    corsize = size-subsize+1
78
79    cordata = zeros([corsize,corsize])
80
81    motif = sqrt(sum(sum(imgdata*imgdata)))
82
83    for i in range(corsize):
84        for j in range(corsize):
85            refpart = refdata[i:i+subsize,j:j+subsize]
86            cordata[i,j] = \
87                 sum(sum( imgdata * refpart))\
88                 / (motif * sqrt(sum(sum(refpart*refpart))))
89
90
91    cor_list.append(cordata)
92
93    refhdulist[0].data = cordata
94    prihdr = refhdulist[0].header
95
96    prihdr.update('CRPIX1',(corsize-1)/2,'crpix1')
97    prihdr.update('CRPIX2',(corsize-1)/2,'crpix2')
98    prihdr.update('CRVAL1',sh_guess[0],'crval1')
99    prihdr.update('CRVAL2',sh_guess[1],'crval2')
100    prihdr.update('CDELT1',-1.0,'crdelt1')
101    prihdr.update('CDELT2',-1.0,'crdelt2')
102    prihdr.update('CROTA1',0.0,'crota1')
103    prihdr.update('CROTA2',0.0,'crota2')
104    prihdr.update('CTYPE1',' ','ctype1')
105    prihdr.update('CTYPE2',' ','ctype2')
106
107
108    refhdulist.writeto('cor'+str(len(cor_list)-1)+'.fits',clobber=True)       
109
110
111
112cor_prod = cor_list[0]
113
114for i in range(1,len(cor_list)):
115    cor_prod *= cor_list[i]
116
117
118refhdulist[0].data = cor_prod
119prihdr = refhdulist[0].header
120
121crpix1 = (corsize-1)/2
122crpix2 = (corsize-1)/2
123crval1 = sh_guess[0]+1
124crval2 = sh_guess[1]+1
125cdelt1 = -1.0
126cdelt2 = -1.0
127
128prihdr.update('CRPIX1',crpix1,'crpix1')
129prihdr.update('CRPIX2',crpix2,'crpix2')
130prihdr.update('CRVAL1',crval1,'crval1')
131prihdr.update('CRVAL2',crval2,'crval2')
132prihdr.update('CDELT1',cdelt1,'crdelt1')
133prihdr.update('CDELT2',cdelt2,'crdelt2')
134prihdr.update('CROTA1',0.0,'crota1')
135prihdr.update('CROTA2',0.0,'crota2')
136prihdr.update('CTYPE1',' ','ctype1')
137prihdr.update('CTYPE2',' ','ctype2')
138
139
140refhdulist.writeto('cor_prod.fits',clobber=True)       
141
142refhdulist.close()
143imghdulist.close()
144
145# On cherche le maximum du tableau -> passage en 1D
146lin_max = argmax(cor_prod.ravel())
147x_max = lin_max % corsize
148y_max = floor(lin_max / corsize)
149#print "Pixel max :",(int(x_max),int(y_max))
150#print "Décalage (entier) correspondant : (%d,%d)" %\
151#      (int((x_max-crpix1)*cdelt1+crval1-1),
152#       int((y_max-crpix2)*cdelt2+crval2-1))
153
154# On cherche le centroïde (bloc 2*(npfit)+1**2)
155npfit = 1
156
157
158bloc = cor_prod[y_max-npfit:y_max+npfit+1,
159                x_max-npfit:x_max+npfit+1]
160bloc = bloc - min(bloc.ravel())
161
162og = array([0.0,0.0])
163
164for i in range(2*npfit+1):
165    for j in range(2*npfit+1):
166        vec = array([j+1,i+1])
167        og += vec*bloc[i,j]
168
169og /= sum(sum(bloc))
170og -=1
171
172
173print "ref :",ref
174print "img :",img
175
176#print "Centroïde en : %f %f" % (og[0]+x_max,og[1]+y_max)
177print "Décalage centroïde : %f %f" % (((og[0]+x_max)-crpix1)*cdelt1+crval1,
178                                      ((og[1]+y_max)-crpix2)*cdelt2+crval2)
179
180
181#params = fitgaussian(bloc)
182#fit = gaussian(*params)
183#(height, x, y, width_x, width_y) = params
184#print "Décalage gaussienne : %f %f" % (((x+x_max)-crpix1)*cdelt1+crval1,
185#                                      ((y+y_max)-crpix2)*cdelt2+crval2)
186
Note: See TracBrowser for help on using the repository browser.