1 |
int cosmic_rays(DRMS_Record_t *record, float *image, int *badpix, int nbad, int *cosmic, int *n_cosmic, int nx, int ny) |
2 |
{ |
3 |
//fresize: structure obtained from init_fresize_gaussian(&fresizes,stdd,nwd,1); |
4 |
//image: input image |
5 |
//limit: detection limit in sigma (somewhere between 4.0 and 7.0) |
6 |
//cosmic: integer array of image size: will be used to write cosmic ray coordinates into [0...n_cosmic-1] |
7 |
//n_cosmic: number of cosmic ray hits |
8 |
//nx: horizontal dimension |
9 |
//ny: vertical dimension |
10 |
// |
11 |
|
12 |
struct fresize_struct fresizes; |
13 |
|
14 |
|
15 |
float coef[5]; |
16 |
|
17 |
|
18 |
float stdd=fwhm/2.0/sqrt(2.0*log(2.0)); |
19 |
int nwd=(int)(fwhm*kernel_size); //kernel size |
20 |
init_fresize_gaussian(&fresizes,stdd,nwd,1); |
21 |
if (fresize == NULL){printf("can not initialize fresize\n"); exit(EXIT_FAILURE);} |
22 |
|
23 |
coef[0]=1.00000; |
24 |
coef[1]=0.0; |
25 |
coef[2]=0.0; |
26 |
coef[3]=0.0; |
27 |
coef[4]=0.0; |
28 |
|
29 |
|
30 |
int i, j; |
31 |
int count; |
32 |
|
33 |
float *imhp; |
34 |
imhp=(float *)(malloc(nx*ny*sizeof(float))); |
35 |
if (imhp == NULL){printf("can not allocate memory\n"); exit(EXIT_FAILURE);} |
36 |
|
37 |
float *img; |
38 |
img=(float *)(malloc(nx*ny*sizeof(float))); |
39 |
if (img == NULL){printf("can not allocate memory\n"); exit(EXIT_FAILURE);} |
40 |
|
41 |
float sigma, avint; |
42 |
float sum, inty; |
43 |
float sig, r, factor; |
44 |
float x0, y0, rad; |
45 |
float rsun_obs, image_scl; |
46 |
int nump=nx/cent_frac*ny/cent_frac; |
47 |
int status=1, status1, status2; |
48 |
int stat_pos=0; |
49 |
int focid, camid; |
50 |
|
51 |
|
52 |
|
53 |
x0=drms_getkey_float(record,X0_MP_key,&status); |
54 |
if (status != 0 || isnan(x0)){stat_pos=1; printf("no center\n");} |
55 |
|
56 |
y0=drms_getkey_float(record,Y0_MP_key,&status); |
57 |
if (status != 0 || isnan(y0)){stat_pos=1;printf("no center\n");} |
58 |
|
59 |
rsun_obs=drms_getkey_double(record,RSUN_OBS_key,&status1); |
60 |
image_scl=drms_getkey_float(record,IMSCL_MP_key,&status2); |
61 |
if (status1 == 0 && status2 == 0 && !isnan(image_scl) && image_scl > 0.0 && !isnan(rsun_obs) && rsun_obs > 0.0){rad=rsun_obs/image_scl;} else {stat_pos=1;printf("no radius\n"); } |
62 |
|
63 |
focid=drms_getkey_int(record,HCFTID_key,&status1); |
64 |
camid=drms_getkey_int(record,HCAMID_key,&status1); |
65 |
|
66 |
if (status1 != 0 || status2 != 0){printf("no foc id or camera\n"); exit(EXIT_FAILURE);} |
67 |
|
68 |
if ((camid != light_val1 && camid != light_val2) || focid < 1 || focid > 16){stat_pos=1; factor=2.0;} else factor=1; |
69 |
|
70 |
|
71 |
//copy image |
72 |
#pragma omp parallel for private(i,j) |
73 |
for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) img[j*nx+i]=image[j*nx+i]; |
74 |
|
75 |
for (i=0; i<nbad; ++i)if (badpix[i] < 0 || badpix[i] >= 16777216){printf("invalid pixel coordinate\n"); exit(EXIT_FAILURE);} |
76 |
for (i=0; i<nbad; ++i){img[badpix[i]]=NAN; cosmic[i]=badpix[i];} // set bad pixels to NAN |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
fresize(&fresizes,img, imhp, nx,ny,nx,nx,ny,nx,0,0,NAN); //convolve with gaussian |
83 |
|
84 |
|
85 |
|
86 |
#pragma omp parallel for private(i,j) |
87 |
for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) imhp[j*nx+i]=img[j*nx+i]-imhp[j*nx+i]; //highpass filter |
88 |
|
89 |
//get stddev at center |
90 |
//******************************************************************************************************* |
91 |
sum=0.0; |
92 |
inty=0.0; |
93 |
count=0; |
94 |
|
95 |
|
96 |
|
97 |
#pragma omp parallel for reduction(+:sum,inty,count) private(i,j) |
98 |
for (j=ny*(cent_frac-1)/cent_frac/2; j<ny*(cent_frac+1)/cent_frac/2; ++j) for (i=nx*(cent_frac-1)/cent_frac/2; i<nx*(cent_frac+1)/cent_frac/2; ++i) |
99 |
{ |
100 |
if (!isnan(imhp[j*nx+i])) |
101 |
{ |
102 |
sum += imhp[j*nx+i]*imhp[j*nx+i]; |
103 |
inty += image[j*nx+i]; |
104 |
++count; |
105 |
} |
106 |
} |
107 |
|
108 |
//detect everything that stick out more than limit*sigma |
109 |
if (count > nump/2){sigma=sqrt(sum/(float)(count)); avint=inty/(float)count;} else sigma=NAN; |
110 |
|
111 |
//******************************************************************************************************** |
112 |
|
113 |
//check for outliers |
114 |
//******************************************************************************************************* |
115 |
count=nbad; |
116 |
|
117 |
|
118 |
if (!isnan(sigma) && sigma > sigmamin && sigma < sigmamax && avint > avmin && avint < avmax) |
119 |
{ |
120 |
#pragma omp parallel for private(i,j,r) |
121 |
for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) |
122 |
{ |
123 |
|
124 |
if (!isnan(imhp[j*nx+i])) |
125 |
{ |
126 |
if (stat_pos == 0) r=sqrt(pow((float)j-y0,2)+pow((float)i-x0,2))/rad; else r=0.0; |
127 |
if (r< 0.98){ |
128 |
sig=(coef[0]+coef[1]*r+coef[2]*pow(r,2)+coef[3]*pow(r,3)+coef[4]*pow(r,4))*sigma; |
129 |
if (imhp[j*nx+i] > limit*factor*sig || image[j*nx+i] > maxval) |
130 |
{ |
131 |
#pragma omp critical(detect) |
132 |
cosmic[count]=j*nx+i; |
133 |
++count; |
134 |
} |
135 |
} |
136 |
} |
137 |
|
138 |
|
139 |
} |
140 |
status=0; |
141 |
} |
142 |
//******************************************************************************************************** |
143 |
|
144 |
*n_cosmic=count; |
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
free_fresize(&fresizes); |
151 |
free(imhp); |
152 |
free(img); |
153 |
|
154 |
|
155 |
|
156 |
|
157 |
return status; |
158 |
|
159 |
} |