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 int ix_bad[32];
00068
00069 int ix_bad_front[32]={738657 , 1100979 , 1100980 , 1420552 , 1424648 , 1428744 , 1502539 , 1502540 , 1506635 , 1506636 , 5961670 , 6051854, 6330150 , 10171860, 10175955 , 10175956};
00070
00071 int ix_bad_side[32]={1800075 ,1800076 ,1804173 ,9958325 ,9958326 ,9958327 ,9962423 ,9962424 ,9962425 ,9966520 ,12176519 ,12176520 ,12176521 ,12180613 ,12180614 ,12180615 ,12180616 ,12180617 ,12184709 , 12184710 ,12184711 ,12184712 ,12184713 ,12188806 ,12188807 , 12188808 , 12188809 ,12426166 ,12426167 ,12430263 , 12430264 ,12434360};
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 if (camera == 2){nbad=16;
00108 for (k=0; k<nbad; ++k) ix_bad[k]=ix_bad_front[k];
00109 }
00110
00111
00112 if (camera == 1){nbad=32;
00113 for (k=0; k<nbad; ++k) ix_bad[k]=ix_bad_side[k];
00114 }
00115
00116
00117 printf("reading data\n");
00118 int fi;
00119 for (fi=0; fi < n_foc; ++fi){
00120
00121
00122
00123 int foc=valfoc[fi];
00124 char fnumb[2]={""};
00125 sprintf(fnumb, "%i", foc);
00126
00128 char fname[30]={""};
00129
00130
00131 strcat(fname, "imr_hmi");
00132 strcat(fname, fnumb);
00133 strcat(fname, ".bin");
00134
00135 printf("input name %s \n", fname);
00137
00138
00139
00140
00141
00142 char ffname[40]={""};
00143
00144 strcat(ffname, "flatfield_out");
00145 strcat(ffname, fnumb);
00146 strcat(ffname, ".bin");
00147
00148 printf("output name %s \n", ffname);
00150
00151 imm=(float *)(malloc(nx*sizeof(float)));
00152 imr=(float **)(malloc(nl*sizeof(float*)));
00153
00154 FILE *imfile;
00155 size_t bytes_read;
00156 imfile=fopen(fname, "rb");
00157
00158 if (imfile==NULL){fputs("File error", stderr); exit(1);};
00159 {
00160 for (k=0; k<nl; ++k){
00161 printf("%u \n", k);
00162 imp=(float *)(malloc(nx*ny*sizeof(float)));
00163 *(imr+k)=imp;
00164 bytes_read=fread(imp,sizeof(float), nx*ny,imfile);
00165 }
00166 }
00167 fclose(imfile);
00168
00169
00170
00171
00172
00173
00174 FILE *legpos;
00175 legpos=fopen("legpos2", "r");
00176
00177 if (legpos == NULL){fputs("File error", stderr); exit(1);};
00178
00179 {
00180 for (i=0; i<nl; i++){
00181 fscanf(legpos, "%f", &datum);
00182 xx[i]=datum/samp;
00183 printf("%f\n", xx[i]);
00184 }
00185 printf("\n");
00186 for (i=0; i<nl; i++){
00187 fscanf(legpos, "%f", &datum);
00188 yy[i]=datum/samp;
00189 printf("%f\n", yy[i]);
00190 }
00191 }
00192 fclose(legpos);
00193
00194
00195
00196
00197
00198 int low9=0;
00199 float nr=nx/2*fac;
00200 float nb=nx/2*fac*0.0;
00201
00202 float nrs=nx/2*facr;
00203 float nbs=nx/2*facr*0.0;
00204
00205 apod_circ(nx, nr, nb, 0, 0.0, 0.0, vdl,nx,ny);
00206
00207
00208 for (k=0; k<nbad; ++k) vdl[ix_bad[k]]=0.0;
00209
00210 for (k=0; k<nl; ++k){
00211 printf("%d\n", k);
00212 apod_circ(nx, nrs, nbs, 0, xx[k], yy[k], vdo,nx,ny);
00213
00214 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];
00215 }
00216
00217
00218
00219
00220
00221 int xf, yf, xg, yg;
00222 float rx, ry;
00223 float xm, ym;
00224 float xn, yn;
00225
00226
00227
00228
00229 int nthreads;
00230 nthreads=omp_get_num_procs();
00231 omp_set_num_threads(nthreads);
00232 printf("Number of threads run in parallel by the subroutine= %d \n",nthreads);
00233
00234
00235 printtime();
00236
00237 printf("zero iteration\n");
00238
00239 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) kap[j*nx+i]=0.0;
00240
00241 #pragma omp parallel for private(i,j,im,jm,xm,xf,ym,yf,yn,yg,xn,xg)
00242 for (j=0; j<ny; ++j){
00243 for (i=0; i<nx; ++i){
00244 n[j*nx+i]=0.0;
00245 for (jm=0; jm<(nl-1); ++jm){
00246 im=jm+1;
00247
00248 imr_im=*(imr+im);
00249 imr_jm=*(imr+jm);
00250
00251 xm=i-xx[im]+xx[jm];
00252 xf=(int)(xm+0.5);
00253 ym=j-yy[im]+yy[jm];
00254 yf=(int)(ym+0.5);
00255
00256 if (xf >= 0 && xf <= nx-2 && yf >= 0 && yf <= ny-2){
00257 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){
00258
00259 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]));
00260 n[j*nx+i]=n[j*nx+i]+(vdp[im*nx*ny+j*nx+i])*(vdp[jm*nx*ny+yf*nx+xf]);
00261
00262 }
00263 }
00264
00265 yn=j+yy[im]-yy[jm];
00266 yg=(int)(yn+0.5);
00267 xn=i+xx[im]-xx[jm];
00268 xg=(int)(xn+0.5);
00269
00270 if (xg >= 0 && xg <= nx-2 && yg >=0 && yg <= ny-2){
00271
00272 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){
00273
00274
00275 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]));
00276
00277 n[j*nx+i]=n[j*nx+i]+(vdp[jm*nx*ny+j*nx+i])*(vdp[im*nx*ny+yg*nx+xg]);
00278 }
00279 }
00280
00281 }
00282
00283 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;
00284
00285 }
00286 }
00287
00288
00289
00290
00291
00292 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) g[j*nx+i]=kap[j*nx+i];
00293 for (i=0; i<niter; ++i) res[i]=0.0;
00294
00295
00296 printtime();
00297
00298
00299
00300 for (iter=0; iter<niter; ++iter){
00301
00302 printf("iteration %u \n", iter+1);
00303
00304 #pragma omp parallel for private(i,j,im,jm,xm,xf,ym,yf,yn,yg,xn,xg)
00305 for (j=0; j<ny; ++j){
00306 for (i=0; i<nx; ++i){
00307 n[j*nx+i]=0.0;
00308 gn[j*nx+i]=0.0;
00309
00310
00311 for (jm=0; jm<nl; ++jm){
00312 im=jm+1;
00313
00314 xm=i-xx[im]+xx[jm];
00315 xf=(int)(xm+0.5);
00316 ym=j-yy[im]+yy[jm];
00317 yf=(int)(ym+0.5);
00318
00319 if (yf >= 0 && yf <= ny-2 && xf >= 0 && xf <= nx-2){
00320 if (vdp[im*nx*ny+j*nx+i] != 0.0 && vdp[jm*nx*ny+yf*nx+xf] !=0.0){
00321
00322 n[j*nx+i]=n[j*nx+i]+vdp[im*nx*ny+j*nx+i]*vdp[jm*nx*ny+yf*nx+xf];
00323 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];
00324
00325
00326 }
00327 }
00328
00329
00330
00331 xn=i+xx[im]-xx[jm];
00332 xg=(int)(xn+0.5);
00333 yn=j+yy[im]-yy[jm];
00334 yg=(int)(yn+0.5);
00335
00336 if (yg >= 0 && yg <= ny-2 && xg >= 0 && xg <= nx-2){
00337 if (vdp[jm*nx*ny+j*nx+i] !=0.0 && vdp[im*nx*ny+yg*nx+xg] !=0.0){
00338
00339 n[j*nx+i]=n[j*nx+i]+vdp[jm*nx*ny+j*nx+i]*vdp[im*nx*ny+yg*nx+xg];
00340 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];
00341
00342
00343 }
00344 }
00345
00346
00347 }
00348
00349 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;
00350 delta=kap[j*nx+i]-g[j*nx+i]+gn[j*nx+i];
00351 g[j*nx+i]=g[j*nx+i]+omega*delta;
00352
00353
00354 }
00355 }
00356
00357
00358
00359 if (residuum){
00360 for (j=0; j<ny; ++j){
00361 for (i=0; i<nx; ++i){
00362
00363 for (jm=0; jm<(nl-1); ++jm){
00364 im=jm+1;
00365
00366 imr_im=*(imr+im);
00367 imr_jm=*(imr+jm);
00368
00369 xf=(int)(i+xx[im]);
00370 yf=(int)(j+yy[im]);
00371 xg=(int)(i+xx[jm]);
00372 yg=(int)(j+yy[jm]);
00373
00374 if (xf >=0 && xf <nx && xg >=0 && xg <nx && yf>=0 && yf<ny && yg >=0 && yg < ny){
00375 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){
00376 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);
00377 }
00378 }
00379
00380
00381 }
00382
00383 }
00384 }
00385
00386 printf("residuum: %f\n", res[iter]);
00387 }
00388 }
00389
00390
00391 printtime();
00392
00393
00394 FILE *outname;
00395 outname = fopen (ffname, "w");
00396 if (outname == NULL){fputs("File error", stderr); exit(1);};
00397
00398 {
00399 for (j=0;j<ny;j++){
00400 for (i=0; i<nx; ++i) imm[i]=exp(g[j*nx+i]);
00401 fwrite ((char*)(imm),sizeof(float),nx,outname);
00402 }
00403 fclose(outname);
00404
00405
00406
00407 for (k=0; k<nl; ++k) free(*(imr+k));
00408 free(imr);
00409 }
00410
00411 free(imm);
00412
00413 return 0;
00414 }
00415
00416
00417 void apod_circ(int nn, float rad, float nb, int low9, float offx, float offy, float *vd, int nx, int ny)
00418 {
00419 float *rarr;
00420 rarr=(float *)(malloc(nx*ny*sizeof(float)));
00421 int i, j;
00422
00423 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)));
00424
00425 for (i=0; i<nn; ++i){
00426 for (j=0; j<nn; ++j){
00427
00428 if (rarr[j*nx+i] < rad)
00429 vd[j*nx+i]=1.0;
00430
00431 if (rarr[j*nx+i] >= rad && rarr[j*nx+i] < (rad+nb))
00432 vd[j*nx+i]=0.5*cos(M_PI/nb*(rarr[j*nx+i]-rad))+0.5;
00433
00434 if (rarr[j*nx+i] >= (rad+nb))
00435 vd[j*nx+i]=0.0;
00436
00437 if (low9 != 0){
00438
00439 if (j < low9) vd[j*nx+i]=0.0;
00440 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];
00441 }
00442
00443
00444
00445
00446 }
00447 }
00448
00449 }
00450
00451
00452
00453
00454 void printtime()
00455 {
00456 time_t timer, timerd;
00457 char *timestring;
00458 int i;
00459
00460 timerd=time(&timer);
00461 timestring=ctime(&timer);
00462 for (i=0; i<24; ++i) printf("%c", *(timestring+i));
00463 printf("\n");
00464 }
00465
00466