00001 int cosmic_rays(DRMS_Record_t *record, float *image, int *badpix, int nbad, int *cosmic, int *n_cosmic, int nx, int ny)
00002 {
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 struct fresize_struct fresizes;
00013
00014
00015 float coef[5];
00016
00017
00018 float stdd=fwhm/2.0/sqrt(2.0*log(2.0));
00019 int nwd=(int)(fwhm*kernel_size);
00020 init_fresize_gaussian(&fresizes,stdd,nwd,1);
00021 if (fresize == NULL){printf("can not initialize fresize\n"); exit(EXIT_FAILURE);}
00022
00023 coef[0]=1.00000;
00024 coef[1]=0.0;
00025 coef[2]=0.0;
00026 coef[3]=0.0;
00027 coef[4]=0.0;
00028
00029
00030 int i, j;
00031 int count;
00032
00033 float *imhp;
00034 imhp=(float *)(malloc(nx*ny*sizeof(float)));
00035 if (imhp == NULL){printf("can not allocate memory\n"); exit(EXIT_FAILURE);}
00036
00037 float *img;
00038 img=(float *)(malloc(nx*ny*sizeof(float)));
00039 if (img == NULL){printf("can not allocate memory\n"); exit(EXIT_FAILURE);}
00040
00041 float sigma, avint;
00042 float sum, inty;
00043 float sig, r, factor;
00044 float x0, y0, rad;
00045 float rsun_obs, image_scl;
00046 int nump=nx/cent_frac*ny/cent_frac;
00047 int status=1, status1, status2;
00048 int stat_pos=0;
00049 int focid, camid;
00050
00051
00052
00053 x0=drms_getkey_float(record,X0_MP_key,&status);
00054 if (status != 0 || isnan(x0)){stat_pos=1; printf("no center\n");}
00055
00056 y0=drms_getkey_float(record,Y0_MP_key,&status);
00057 if (status != 0 || isnan(y0)){stat_pos=1;printf("no center\n");}
00058
00059 rsun_obs=drms_getkey_double(record,RSUN_OBS_key,&status1);
00060 image_scl=drms_getkey_float(record,IMSCL_MP_key,&status2);
00061 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"); }
00062
00063 focid=drms_getkey_int(record,HCFTID_key,&status1);
00064 camid=drms_getkey_int(record,HCAMID_key,&status1);
00065
00066 if (status1 != 0 || status2 != 0){printf("no foc id or camera\n"); exit(EXIT_FAILURE);}
00067
00068 if ((camid != light_val1 && camid != light_val2) || focid < 1 || focid > 16){stat_pos=1; factor=2.0;} else factor=1;
00069
00070
00071
00072 #pragma omp parallel for private(i,j)
00073 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) img[j*nx+i]=image[j*nx+i];
00074
00075 for (i=0; i<nbad; ++i)if (badpix[i] < 0 || badpix[i] >= 16777216){printf("invalid pixel coordinate\n"); exit(EXIT_FAILURE);}
00076 for (i=0; i<nbad; ++i){img[badpix[i]]=NAN; cosmic[i]=badpix[i];}
00077
00078
00079
00080
00081
00082 fresize(&fresizes,img, imhp, nx,ny,nx,nx,ny,nx,0,0,NAN);
00083
00084
00085
00086 #pragma omp parallel for private(i,j)
00087 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) imhp[j*nx+i]=img[j*nx+i]-imhp[j*nx+i];
00088
00089
00090
00091 sum=0.0;
00092 inty=0.0;
00093 count=0;
00094
00095
00096
00097 #pragma omp parallel for reduction(+:sum,inty,count) private(i,j)
00098 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)
00099 {
00100 if (!isnan(imhp[j*nx+i]))
00101 {
00102 sum += imhp[j*nx+i]*imhp[j*nx+i];
00103 inty += image[j*nx+i];
00104 ++count;
00105 }
00106 }
00107
00108
00109 if (count > nump/2){sigma=sqrt(sum/(float)(count)); avint=inty/(float)count;} else sigma=NAN;
00110
00111
00112
00113
00114
00115 count=nbad;
00116
00117
00118 if (!isnan(sigma) && sigma > sigmamin && sigma < sigmamax && avint > avmin && avint < avmax)
00119 {
00120 #pragma omp parallel for private(i,j,r)
00121 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i)
00122 {
00123
00124 if (!isnan(imhp[j*nx+i]))
00125 {
00126 if (stat_pos == 0) r=sqrt(pow((float)j-y0,2)+pow((float)i-x0,2))/rad; else r=0.0;
00127 if (r< 0.98){
00128 sig=(coef[0]+coef[1]*r+coef[2]*pow(r,2)+coef[3]*pow(r,3)+coef[4]*pow(r,4))*sigma;
00129 if (imhp[j*nx+i] > limit*factor*sig || image[j*nx+i] > maxval)
00130 {
00131 #pragma omp critical(detect)
00132 {
00133 cosmic[count]=j*nx+i;
00134 ++count;
00135 }
00136 }
00137 }
00138 }
00139
00140
00141 }
00142 status=0;
00143 }
00144
00145
00146 *n_cosmic=count;
00147
00148
00149
00150
00151
00152 free_fresize(&fresizes);
00153 free(imhp);
00154 free(img);
00155
00156
00157
00158
00159 return status;
00160
00161 }