00001
00002
00003 #include <stdio.h>
00004 #include <math.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <omp.h>
00008
00009
00010
00011
00012
00013
00014
00015 const int niter=10;
00016 const float omega=1.2;
00017 const int residuum=0;
00018 const int n_foc=1;
00019
00020
00021
00022 void apod_circ(int, float, float, int, float, float, float *, int , int);
00023 void printtime();
00024
00025 int main(int argc, char *argv[]){
00026
00027 int camera;
00028 int nx, ny;
00029 int nl;
00030 float fac,facr;
00031
00032 if (argc != 6){fputs("Wrong number of arguments", stderr); exit(1);}
00033
00034 nx=atoi(argv[1]);
00035 ny=atoi(argv[1]);
00036 nl=atoi(argv[2]);
00037 camera=atoi(argv[3]);
00038 fac=(float)atoi(argv[4])/100.0;
00039 facr=(float)atoi(argv[5])/100.0;
00040
00041 printf("nx ny nl cam %d %d %d %d\n", nx, ny, nl, camera);
00042
00043 float samp=4096.0/(float)nx;
00044
00045
00046 float **imr;
00047 float *imp;
00048 float *imm;
00049
00050 float *imr_im, *imr_jm;
00051
00052
00053
00054
00055 float *vdo, *vdl;
00056 float *vdp;
00057
00058 vdo=(float *)(malloc(nx*ny*sizeof(float)));
00059 vdl=(float *)(malloc(nx*ny*sizeof(float)));
00060 vdp=(float *)(malloc(nx*ny*nl*sizeof(float)));
00061
00062 float xx[nl], yy[nl];
00063
00064 int nbad;
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 float *g, *kap, *n, *gn;
00085 g=(float *)(malloc(nx*ny*sizeof(float)));
00086 kap=(float *)(malloc(nx*ny*sizeof(float)));
00087
00088 n=(float *)(malloc(nx*ny*sizeof(float)));
00089 gn=(float *)(malloc(nx*ny*sizeof(float)));
00090
00091
00092 float res[niter];
00093
00094
00095 float datum;
00096 int im_number;
00097
00098 float delta;
00099
00100 int i, j, k, im, jm, iter;
00101
00102 int valfoc[7]={0,2,3,4,5,9,13};
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 printf("reading data\n");
00118 int fi;
00119 for (fi=0; fi < n_foc; ++fi){
00120
00121
00122
00123
00125 char *fname="imr_hmi0.bin";
00126
00128
00129
00130
00131
00132
00133 char *ffname="flatfield_out0.bin";
00134
00135
00136
00137
00138 printf("output name %s \n", ffname);
00140
00141 imm=(float *)(malloc(nx*sizeof(float)));
00142 imr=(float **)(malloc(nl*sizeof(float*)));
00143
00144 FILE *imfile;
00145 size_t bytes_read;
00146 imfile=fopen(fname, "rb");
00147
00148 if (imfile==NULL){fputs("File error", stderr); exit(1);};
00149 {
00150 for (k=0; k<nl; ++k){
00151 printf("%u \n", k);
00152 imp=(float *)(malloc(nx*ny*sizeof(float)));
00153 *(imr+k)=imp;
00154 bytes_read=fread(imp,sizeof(float), nx*ny,imfile);
00155 }
00156 }
00157 fclose(imfile);
00158
00159
00160
00161
00162
00163
00164 FILE *legpos;
00165 legpos=fopen("legpos2", "r");
00166
00167 if (legpos != NULL){
00168 for (i=0; i<nl; i++){
00169 fscanf(legpos, "%f", &datum);
00170 xx[i]=datum/samp;
00171 printf("%f\n", xx[i]);
00172 }
00173 printf("\n");
00174 for (i=0; i<nl; i++){
00175 fscanf(legpos, "%f", &datum);
00176 yy[i]=datum/samp;
00177 printf("%f\n", yy[i]);
00178 }
00179 }
00180 fclose(legpos);
00181
00182
00183
00184
00185
00186 int low9=0;
00187 float nr=nx/2*fac;
00188 float nb=nx/2*fac*0.0;
00189
00190 float nrs=nx/2*facr*0.95;
00191 float nbs=nx/2*facr*0.05;
00192
00193 apod_circ(nx, nr, nb, 0, 0.0, 0.0, vdl,nx,ny);
00194
00195
00196
00197
00198 for (k=0; k<nl; ++k){
00199 printf("%d\n", k);
00200 apod_circ(nx, nrs, nbs, 0, xx[k], yy[k], vdo,nx,ny);
00201
00202 for (j=0; j<nx; ++j) for (i=0; i<ny; ++i) vdp[k*nx*ny+j*nx+i]=vdl[j*nx+i]*vdo[j*nx+i];
00203 }
00204
00205
00206
00207
00208
00209 int xf, yf, xg, yg;
00210 float rx, ry;
00211 float xm, ym;
00212 float xn, yn;
00213
00214
00215
00216
00217 int nthreads;
00218
00219 nthreads=1;
00220 omp_set_num_threads(nthreads);
00221 printf("Number of threads run in parallel by the subroutine= %d \n",nthreads);
00222
00223
00224 printtime();
00225
00226 printf("zero iteration\n");
00227
00228 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) kap[j*nx+i]=0.0;
00229
00230 #pragma omp parallel for private(i,j,im,jm,xm,xf,ym,yf,yn,yg,xn,xg)
00231 for (j=0; j<ny; ++j){
00232 for (i=0; i<nx; ++i){
00233 n[j*nx+i]=0.0;
00234 for (jm=0; jm<nl; ++jm){
00235 for (im=jm+1; im<nl; ++im){
00236
00237 imr_im=*(imr+im);
00238 imr_jm=*(imr+jm);
00239
00240 xm=i-xx[im]+xx[jm];
00241 xf=(int)(xm+0.5);
00242 ym=j-yy[im]+yy[jm];
00243 yf=(int)(ym+0.5);
00244
00245 if (xf >= 0 && xf <= nx-2 && yf >= 0 && yf <= ny-2){
00246 if (vdp[im*nx*ny+j*nx+i] != 0.0 && vdp[jm*nx*ny+yf*nx+xf] != 0.0 && imr_im[j*nx+i] > 0.0 && imr_jm[yf*nx+xf] > 0.0){
00247
00248 kap[j*nx+i]=kap[j*nx+i]+(vdp[im*nx*ny+j*nx+i])*(vdp[jm*nx*ny+yf*nx+xf])*(log(imr_im[j*nx+i])-log(imr_jm[yf*nx+xf]));
00249 n[j*nx+i]=n[j*nx+i]+(vdp[im*nx*ny+j*nx+i])*(vdp[jm*nx*ny+yf*nx+xf]);
00250
00251 }
00252 }
00253
00254 yn=j+yy[im]-yy[jm];
00255 yg=(int)(yn+0.5);
00256 xn=i+xx[im]-xx[jm];
00257 xg=(int)(xn+0.5);
00258
00259 if (xg >= 0 && xg <= nx-2 && yg >=0 && yg <= ny-2){
00260
00261 if (vdp[jm*nx*ny+j*nx+i] != 0.0 && vdp[im*nx*ny+yg*nx+xg] !=0.0 && imr_jm[j*nx+i] > 0.0 && imr_im[yg*nx+xg] > 0.0){
00262
00263
00264 kap[j*nx+i]=kap[j*nx+i]+(vdp[jm*nx*ny+j*nx+i])*(vdp[im*nx*ny+yg*nx+xg])*(log(imr_jm[j*nx+i])-log(imr_im[yg*nx+xg]));
00265
00266 n[j*nx+i]=n[j*nx+i]+(vdp[jm*nx*ny+j*nx+i])*(vdp[im*nx*ny+yg*nx+xg]);
00267 }
00268 }
00269 }
00270 }
00271
00272 if (n[j*nx+i] > 0.0) kap[j*nx+i]=kap[j*nx+i]/n[j*nx+i]; else kap[j*nx+i]=0.0;
00273
00274 }
00275 }
00276
00277
00278
00279
00280
00281 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) g[j*nx+i]=kap[j*nx+i];
00282 for (i=0; i<niter; ++i) res[i]=0.0;
00283
00284
00285 printtime();
00286
00287
00288
00289 for (iter=0; iter<niter; ++iter){
00290
00291 printf("iteration %u \n", iter+1);
00292
00293 #pragma omp parallel for private(i,j,im,jm,xm,xf,ym,yf,yn,yg,xn,xg)
00294 for (j=0; j<ny; ++j){
00295 for (i=0; i<nx; ++i){
00296 n[j*nx+i]=0.0;
00297 gn[j*nx+i]=0.0;
00298
00299
00300 for (jm=0; jm<nl; ++jm){
00301 for (im=jm+1; im<nl; ++im){
00302
00303 xm=i-xx[im]+xx[jm];
00304 xf=(int)(xm+0.5);
00305 ym=j-yy[im]+yy[jm];
00306 yf=(int)(ym+0.5);
00307
00308 if (yf >= 0 && yf <= ny-2 && xf >= 0 && xf <= nx-2){
00309 if (vdp[im*nx*ny+j*nx+i] != 0.0 && vdp[jm*nx*ny+yf*nx+xf] !=0.0){
00310
00311 n[j*nx+i]=n[j*nx+i]+vdp[im*nx*ny+j*nx+i]*vdp[jm*nx*ny+yf*nx+xf];
00312 gn[j*nx+i]=gn[j*nx+i]+vdp[im*nx*ny+j*nx+i]*vdp[jm*nx*ny+yf*nx+xf]*g[yf*nx+xf];
00313
00314
00315 }
00316 }
00317
00318
00319
00320 xn=i+xx[im]-xx[jm];
00321 xg=(int)(xn+0.5);
00322 yn=j+yy[im]-yy[jm];
00323 yg=(int)(yn+0.5);
00324
00325 if (yg >= 0 && yg <= ny-2 && xg >= 0 && xg <= nx-2){
00326 if (vdp[jm*nx*ny+j*nx+i] !=0.0 && vdp[im*nx*ny+yg*nx+xg] !=0.0){
00327
00328 n[j*nx+i]=n[j*nx+i]+vdp[jm*nx*ny+j*nx+i]*vdp[im*nx*ny+yg*nx+xg];
00329 gn[j*nx+i]=gn[j*nx+i]+vdp[jm*nx*ny+j*nx+i]*vdp[im*nx*ny+yg*nx+xg]*g[yg*nx+xg];
00330
00331
00332 }
00333 }
00334
00335 }
00336 }
00337
00338 if (n[j*nx+i] > 0.0) gn[j*nx+i]=gn[j*nx+i]/n[j*nx+i]; else gn[j*nx+i]=0.0;
00339 delta=kap[j*nx+i]-g[j*nx+i]+gn[j*nx+i];
00340 g[j*nx+i]=g[j*nx+i]+omega*delta;
00341
00342
00343 }
00344 }
00345
00346
00347
00348 if (residuum){
00349 for (j=0; j<ny; ++j){
00350 for (i=0; i<nx; ++i){
00351
00352 for (jm=0; jm<(nl-1); ++jm){
00353 for (im=jm+1; im<nl; ++im){
00354
00355 imr_im=*(imr+im);
00356 imr_jm=*(imr+jm);
00357
00358 xf=(int)(i+xx[im]);
00359 yf=(int)(j+yy[im]);
00360 xg=(int)(i+xx[jm]);
00361 yg=(int)(j+yy[jm]);
00362
00363 if (xf >=0 && xf <nx && xg >=0 && xg <nx && yf>=0 && yf<ny && yg >=0 && yg < ny){
00364 if (vdp[im*nx*ny+yf*nx+xf]*vdp[jm*nx*ny+yg*nx+xg] !=0.0 && imr_im[yf*nx+xf] > 0.0 && imr_jm[yg*nx+xg] > 0.0){
00365 res[iter]=res[iter]+vdp[im*nx*ny+yf*nx+xf]*vdp[jm*nx*ny+yg*nx+xg]*pow(-g[yf*nx+xf]+g[yg*nx+xg]+log(imr_im[yf*nx+xf])-log(imr_jm[yg*nx+xg]),2);
00366 }
00367 }
00368
00369 }
00370 }
00371
00372 }
00373 }
00374
00375 printf("residuum: %f\n", res[iter]);
00376 }
00377 }
00378
00379
00380 printtime();
00381
00382
00383 FILE *outname;
00384 outname = fopen (ffname, "w");
00385 for (j=0;j<ny;j++){
00386 for (i=0; i<nx; ++i) imm[i]=exp(g[j*nx+i]);
00387 fwrite ((char*)(imm),sizeof(float),nx,outname);
00388 }
00389 fclose(outname);
00390
00391
00392
00393 for (k=0; k<nl; ++k) free(*(imr+k));
00394 free(imr);
00395 }
00396
00397 free(imm);
00398
00399 return 0;
00400 }
00401
00402
00403 void apod_circ(int nn, float rad, float nb, int low9, float offx, float offy, float *vd, int nx, int ny)
00404 {
00405 float *rarr;
00406 rarr=(float *)(malloc(nx*ny*sizeof(float)));
00407 int i, j;
00408
00409 for (i=0; i<nn; ++i) for (j=0; j<nn; ++j) rarr[j*nx+i]=sqrt(((float)i-((float)nn/2+offx))*((float)i-((float)nn/2+offx))+((float)j-((float)nn/2+offy))*((float)j-((float)nn/2+offy)));
00410
00411 for (i=0; i<nn; ++i){
00412 for (j=0; j<nn; ++j){
00413
00414 if (rarr[j*nx+i] < rad)
00415 vd[j*nx+i]=1.0;
00416
00417 if (rarr[j*nx+i] >= rad && rarr[j*nx+i] < (rad+nb))
00418 vd[j*nx+i]=0.5*cos(M_PI/nb*(rarr[j*nx+i]-rad))+0.5;
00419
00420 if (rarr[j*nx+i] >= (rad+nb))
00421 vd[j*nx+i]=0.0;
00422
00423 if (low9 != 0){
00424
00425 if (j < low9) vd[j*nx+i]=0.0;
00426 if (j >= low9 && j <= (low9+(int)nb)) vd[j*nx+i]=(-0.5*cos(M_PI/nb*(j-low9))+0.5)*vd[j*nx+i];
00427 }
00428
00429
00430
00431
00432 }
00433 }
00434
00435 }
00436
00437
00438
00439
00440 void printtime()
00441 {
00442 time_t timer, timerd;
00443 char *timestring;
00444 int i;
00445
00446 timerd=time(&timer);
00447 timestring=ctime(&timer);
00448 for (i=0; i<24; ++i) printf("%c", *(timestring+i));
00449 printf("\n");
00450 }
00451
00452