1 | #!/bin/env python |
---|
2 | # encoding: utf-8 |
---|
3 | |
---|
4 | import pyfits |
---|
5 | from optparse import OptionParser |
---|
6 | |
---|
7 | import numpy |
---|
8 | from scipy import * |
---|
9 | |
---|
10 | parser = OptionParser() |
---|
11 | |
---|
12 | parser.add_option("-t","--threshold",dest="threshold", default=0.5, |
---|
13 | help="threshold for edge detection (relative to max, default 0.5)") |
---|
14 | |
---|
15 | parser.add_option("-w","--width",dest="hw_fen_int", default=5, |
---|
16 | help="window for measuring central intensity, size will be 2*hw_fen_int+1") |
---|
17 | |
---|
18 | (options, args) = parser.parse_args () |
---|
19 | |
---|
20 | if (len(args) != 1): |
---|
21 | print "Usage : cherche_centre.py [-t threshold] [-w width] image.fits" |
---|
22 | exit(1) |
---|
23 | |
---|
24 | |
---|
25 | # Ouverture de l'image fits en argument |
---|
26 | hdulist = pyfits.open(args[0]) |
---|
27 | scidata = hdulist[0].data |
---|
28 | |
---|
29 | |
---|
30 | # Estimation fond (4 coins) |
---|
31 | tf = 256 # taille fond, en pix |
---|
32 | fond_bas = .5 * (mean(scidata[:tf,:tf]) + mean(scidata[:tf,2048-tf:])) |
---|
33 | fond_haut= .5 * (mean(scidata[2048-tf:,:tf]) + mean(scidata[2048-tf:,2048-tf:])) |
---|
34 | |
---|
35 | |
---|
36 | if (abs(fond_haut - fond_bas) > .5 * (fond_haut + fond_bas) ): |
---|
37 | print "Ereur : offsets déséquilibrés. Corriger le fond." |
---|
38 | exit(3) |
---|
39 | |
---|
40 | def filtre_median(profil,hw): |
---|
41 | tmp = numpy.zeros(2048) |
---|
42 | for i in range(hw,2048-hw-1): |
---|
43 | tmp[i] = median(profil[i-hw:i+hw+1]) |
---|
44 | return tmp |
---|
45 | |
---|
46 | # Moyenne suivant lignes et colonnes |
---|
47 | moyenne_h = filtre_median(sum(scidata, axis=0) / 2048.,5) |
---|
48 | moyenne_v = filtre_median(sum(scidata, axis=1) / 2048.,5) |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | of = open("profils"+args[0][:-5]+".csv",'w') |
---|
53 | for i in range(2048): |
---|
54 | of.write("%4i %f %f\n" % (i, moyenne_h[i], moyenne_v[i])) |
---|
55 | of.close() |
---|
56 | |
---|
57 | def get_rayon_centre(profil): |
---|
58 | # Détermination du seuil, ici 10% |
---|
59 | seuil = max(profil) * float(options.threshold) |
---|
60 | # Méthode bête |
---|
61 | bord1 = 0 |
---|
62 | for i in range(2047): |
---|
63 | if ((profil[i] < seuil) and (profil[i+1] >= seuil)): |
---|
64 | bord1 = i |
---|
65 | |
---|
66 | bord2 = 2048 |
---|
67 | for i in range(2047,1,-1): |
---|
68 | if((profil[i] < seuil) and (profil[i-1] >= seuil)): |
---|
69 | bord2 = i |
---|
70 | |
---|
71 | flag="" |
---|
72 | if ((bord1 == 0) or (bord2 == 2048)) : |
---|
73 | flag = "*" |
---|
74 | print "Attention ! un seul bord détecté !" |
---|
75 | rayon = 0 |
---|
76 | centre = 0 |
---|
77 | else: |
---|
78 | rayon = (bord2 - bord1) / 2. |
---|
79 | centre = (bord1 + bord2) / 2. |
---|
80 | return (rayon,centre,flag) |
---|
81 | |
---|
82 | |
---|
83 | (rayon_h,centre_h,flag) = get_rayon_centre(moyenne_h) |
---|
84 | (rayon_v,centre_v,flag) = get_rayon_centre(moyenne_v) |
---|
85 | |
---|
86 | |
---|
87 | hw_fen_int = int(options.hw_fen_int) |
---|
88 | |
---|
89 | int_centre = sum(sum(scidata[int(centre_v)-hw_fen_int:int(centre_v)+hw_fen_int+1, |
---|
90 | int(centre_h)-hw_fen_int:int(centre_h)+hw_fen_int+1]))/((2*hw_fen_int + 1)**2) |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | print "%13s %4i %6.1f %4i %6.1f %6.0f %1s" % (args[0], |
---|
95 | rayon_h, |
---|
96 | centre_h+1, # fits commence à 1 |
---|
97 | rayon_v, |
---|
98 | centre_v+1, # fits commence à 1 |
---|
99 | int_centre, |
---|
100 | flag) |
---|