00001
00002
00003
00004
00005
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <math.h>
00009 #include <jsoc_main.h>
00010 #include <string.h>
00011 #include <time.h>
00012 #include </home/jsoc/include/fftw3.h>
00013 #include <omp.h>
00014 #include "module_flatfield.h"
00015
00016
00017
00018
00019
00020
00021 long sign(double);
00022 double mat_rh(long[], double[], double[], int);
00023 void nine_diag(long[], double[], int, int, double[], long[], double);
00024 void blockiter(double[], double[], double[], double*, double[], int, int, double);
00025 void tridag(double veca[nx], double vecb[nx], double vecc[nx], double vecr[nx], double vecu[nx]);
00026 void mat_mult(double[nx], double[nx], double[nx], double[nx], double[nx], int);
00027 void printtime();
00028 void highpass(int, int, double, double[nx][ny]);
00029 void highpass_2d(int M, int N, double fwhm1, double fwhm2, double phi, double a[nx][ny]);
00030 void derotation(double, double, double, double, double, double, double *, int, double *);
00031 void derotation_test(double time, double radius, double cent_x, double cent_y, double p0, double b0, double *rotf);
00032
00033
00034
00035
00036 int flatfield(double *rhsp, double *rhsm, short *badpix, int pairs, double *flati, double *param, struct code_param cpa,
00037 double deltat)
00038 {
00039
00040
00041
00042 double convergence=cpa.convergence;
00043 int maxiter=cpa.maxiter;
00044 double omega=cpa.omega;
00045 double croprad=cpa.croprad;
00046 double norm=cpa.norm;
00047
00048 long count;
00049 int status;
00050 long i,j,k, l;
00051 int datum;
00052 double rdatum;
00053 double ddatum;
00054 double gout[nx*ny];
00055
00056
00057 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) gout[j*nx+i]=flati[j*nx+i];
00058
00059 int crop[ny][2];
00060 int xmin, xmax, ymin, ymax;
00061 long dirx, diry;
00062
00063 double rhs_a[nx*ny+1];
00064
00065 double aa_val[4*(nx-1)*ny+1];
00066 long aa_rowa[4*(nx-1)*ny+1], aa_rowb[4*(nx-1)*ny+1];
00067 long colarra[4*nx*ny], colarrb[4*nx*ny];
00068
00069 double ata_vala[5*nx*ny], ata_valb[5*nx*ny], temp_ata[5*nx*ny];
00070
00071 long aaro[4];
00072 double aava[4];
00073
00074 double rh_a[nx*ny], rh_b[nx*ny];
00075
00076 double gam0, gam1, gam2, gam3;
00077
00078
00079 double rsq;
00080 double gout_t[nx*ny];
00081
00082 int iter;
00083
00084
00085 double res;
00086 double resdu[nx*ny];
00087
00088
00089
00090 double R_SUN=*(param+0);
00091 double XX0=*(param+1);
00092 double YY0=*(param+2);
00093 double P_ANG=*(param+3);
00094 double B_ANG=*(param+4);
00095 double dist=*(param+5);
00096
00097
00098
00099 double *rot_coef, *rotf;
00100 rot_coef=(double *)(malloc(3*sizeof(double)));
00101
00102 rot_coef[0]=cpa.rotcoef0;
00103 rot_coef[1]=cpa.rotcoef1;
00104 rot_coef[2]=cpa.rotcoef2;
00105
00106 rotf=(double *)(malloc(nx*ny*2*sizeof(double)));
00107
00108
00109 printf("rotpat param %lf %lf %lf %lf %lf %lf\n", deltat, R_SUN, XX0, YY0, P_ANG, B_ANG);
00110 derotation_test(deltat, R_SUN, XX0, YY0, P_ANG, B_ANG, rotf);
00111
00112
00113 double rota[nx][ny][2];
00114 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) rota[i][j][0]=rotf[j*nx+i];
00115 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) rota[i][j][1]=rotf[nx*ny+j*nx+i];
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 double ax1, ax2, ay1, ay2;
00126
00127
00128 ay1=YY0-(int)R_SUN*croprad;
00129 ay2=YY0+(int)R_SUN*croprad;
00130
00131 ymin=(ay1 > 0 ? ay1:0);
00132 ymax=(ay2 < ny ? ay2:ny-1);
00133
00134
00135 for (j=0; j<ny; ++j){
00136 int xq=pow(R_SUN*croprad,2)-pow(j-YY0,2);
00137 if (xq > 0){ax1=XX0-sqrt(xq); ax2=XX0+sqrt(xq); crop[j][0]=(ax1 > 0 ? ax1:0); crop[j][1]=(ax2 < nx ? ax2:nx-1);} else {crop[j][0]=XX0; crop[j][1]=XX0-1;}
00138 }
00139
00140
00141
00142 const long dcount=4*(nx-1)*ny;
00143 const long drow=nx*ny;
00144
00145 for (i=0; i<5*nx*ny; ++i){ata_vala[i]=0.0; ata_valb[i]=0.0;}
00146
00147
00148
00149 printtime();
00150
00151 for (l=1; l>=0; --l){
00152 count=0;
00153
00154 for (i=0; i<(4*nx*ny); ++i){colarra[i]=dcount; colarrb[i]=dcount;}
00155 for (i=0; i<(4*(nx-1)*ny); ++i){aa_val[i]=0.0; aa_rowa[i]=0; aa_rowb[i]=0;}
00156 for (i=0; i<=nx*ny; ++i) rhs_a[i]=0.0;
00157
00158
00159 aa_val[dcount]=0.0;
00160 aa_rowa[dcount]=drow;
00161 aa_rowb[dcount]=drow;
00162
00163 for (j=ymin; j<=ymax; ++j){
00164 xmin=crop[j][0];
00165 xmax=crop[j][1];
00166
00167 for (i=xmin; i<=xmax; ++i){
00168
00169 diry=(l*2-1)*sign(rota[i][j][1]);
00170 dirx=(l*2-1)*sign(rota[i][j][0]);
00171
00172 if (!((i == xmax && dirx == 1) || (i == xmin && dirx == -1) || (j == ymax && diry == 1) || (j == ymin && diry == -1))){
00173
00174
00175
00176 aa_val[count]=-1.0+(1.0-fabs(rota[i][j][0]))*(1.0-fabs(rota[i][j][1]));
00177
00178
00179 aa_rowa[count]=j*nx+i;
00180
00181
00182 aa_rowb[count]=i*ny+j;
00183
00184
00185 colarra[4*(j*nx+i)]=count;
00186 colarrb[4*(i*ny+j)]=count;
00187 count=count+1;
00188
00189
00190 aa_val[count]=fabs(rota[i][j][0])*(1.0-fabs(rota[i][j][1]));
00191
00192
00193 aa_rowa[count]=j*nx+i;
00194
00195
00196 aa_rowb[count]=i*ny+j;
00197
00198 colarra[4*(j*nx+i+dirx)+1]=count;
00199 colarrb[4*((i+dirx)*ny+j)+1]=count;
00200 count=count+1;
00201
00202
00203 aa_val[count]=(1.0-fabs(rota[i][j][0]))*fabs(rota[i][j][1]);
00204
00205
00206 aa_rowa[count]=j*nx+i;
00207
00208
00209 aa_rowb[count]=i*ny+j;
00210
00211 colarra[4*((j+diry)*nx+i)+2]=count;
00212 colarrb[4*(i*ny+j+diry)+2]=count;
00213 count=count+1;
00214
00215
00216 aa_val[count]=fabs(rota[i][j][0])*fabs(rota[i][j][1]);
00217
00218
00219 aa_rowa[count]=j*nx+i;
00220
00221
00222 aa_rowb[count]=i*ny+j;
00223
00224
00225 colarra[4*((j+diry)*nx+i+dirx)+3]=count;
00226 colarrb[4*((i+dirx)*ny+j+diry)+3]=count;
00227 count=count+1;
00228
00229 }
00230 }
00231 }
00232
00233
00234
00235
00236 nine_diag(aa_rowa, aa_val, nx, ny, temp_ata, colarra, norm);
00237 for (i=0; i<5*nx*ny; ++i) ata_vala[i]=ata_vala[i]+temp_ata[i];
00238
00239
00240
00241 nine_diag(aa_rowb, aa_val, ny, nx, temp_ata, colarrb, norm);
00242 for (i=0; i<5*nx*ny; ++i) ata_valb[i]=ata_valb[i]+temp_ata[i];
00243
00244
00245
00246
00247
00248
00249
00250
00251 for (j=ymin; j<=ymax; ++j){
00252 xmin=crop[j][0];
00253 xmax=crop[j][1];
00254 for (i=xmin; i<=xmax; ++i){
00255 diry=(2*l-1)*sign(rota[i][j][1]);
00256 dirx=(2*l-1)*sign(rota[i][j][0]);
00257 if (!((i == xmax && dirx == 1) || (i == xmin && dirx == -1) || (j == ymax && diry == 1) || (j == ymin && diry == -1))){
00258
00259
00260 gam0=(1.0-fabs(rota[i][j][0]))*(1.0-fabs(rota[i][j][1]));
00261 gam1=fabs(rota[i][j][0])*(1.0-fabs(rota[i][j][1]));
00262 gam2=(1.0-fabs(rota[i][j][0]))*fabs(rota[i][j][1]);
00263 gam3=fabs(rota[i][j][0])*fabs(rota[i][j][1]);
00264
00265 switch(l){
00266
00267 case 1: rhs_a[j*nx+i]=gam0*rhsp[j*nx+i]
00268 +gam1*rhsp[j*nx+(i+dirx)]
00269 +gam2*rhsp[(j+diry)*nx+i]
00270 +gam3*rhsp[(j+diry)*nx+(i+dirx)]
00271 -rhsm[j*nx+i];
00272 break;
00273
00274 case 0: rhs_a[j*nx+i]=gam0*rhsm[j*nx+i]
00275 +gam1*rhsm[j*nx+i+dirx]
00276 +gam2*rhsm[(j+diry)*nx+i]
00277 +gam3*rhsm[(j+diry)*nx+i+dirx]
00278 -rhsp[j*nx+i];
00279 break;
00280 }
00281
00282 }
00283 }
00284 }
00285
00286
00287
00288
00289 for (i=0; i<nx*ny; ++i){
00290 for (j=0; j<4; ++j){
00291 aaro[j]=aa_rowa[colarra[4*i+j]];
00292 aava[j]=aa_val[colarra[4*i+j]];
00293 }
00294 switch(l){
00295
00296 case 1: rh_a[i]=mat_rh(aaro, aava, rhs_a, 4); break;
00297 case 0: rh_a[i]=rh_a[i]+mat_rh(aaro, aava, rhs_a, 4); break;
00298
00299 }
00300 }
00301
00302
00303
00304 }
00305
00306
00307
00308
00309
00310 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) rh_b[i*ny+j]=rh_a[j*nx+i];
00311
00312
00313
00314
00315
00316
00317
00318 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i){gout_t[i*ny+j]=gout[j*nx+i];}
00319
00320
00321 int icount=0;
00322
00323 if (fabs(rota[nx/2][ny/2][0]) >= fabs(rota[nx/2][ny/2][1])){
00324 do {
00325
00326 blockiter(ata_vala, rh_a, gout, &res, resdu, nx, ny, omega);
00327
00328 printf("%d\t%g a\n", icount, res);
00329 ++icount;
00330 }
00331 while (res > convergence && icount <maxiter);
00332 }
00333 else
00334 {
00335 do{
00336 blockiter(ata_valb, rh_b, gout_t, &res, resdu, ny, nx, omega);
00337
00338
00339 printf("%d\t%g b\n", icount, res);
00340 ++icount;
00341 }
00342 while (res > convergence && icount <maxiter);
00343
00344 for (i=0; i<nx; ++i) for (j=0; j<ny; ++j) gout[j*nx+i]=gout_t[i*ny+j];
00345
00346 }
00347
00348 if (icount < maxiter) status=0; else status=1;
00349
00350 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) flati[j*nx+i]=gout[j*nx+i];
00351
00352
00353 free(rot_coef);
00354 free(rotf);
00355 return status;
00356
00357 }
00358
00359
00360
00361
00362
00363 double mat_rh(long row_1[], double vec1[], double rhs[], int n)
00364
00365 {
00366 double res=0.0;
00367 int i;
00368 for (i=0; i<n; ++i) res=res+vec1[i]*rhs[row_1[i]];
00369
00370 return res;
00371 }
00372
00373
00374 double mat_ata(long row_1[], double vec1[], long row_2[], double vec2[], int n)
00375 {
00376
00377 double res=0.0;
00378 int i, c, kk;
00379
00380 for (i=0; i<n; ++i){
00381 kk=-1;
00382 for (c=0; c<n; ++c) if (row_1[i] == row_2[c]) kk=c;
00383 if (kk != -1) res+=vec1[i]*vec2[kk];
00384 }
00385 return res;
00386 }
00387
00388
00389 void nine_diag(long aa_row[], double aa[], int nnx, int nny, double ata[], long colarr[], double norm)
00390 {
00391
00392 long i, j, qi, qj, loc;
00393 long aaro1[4], aaro2[4];
00394 double aa1[4], aa2[4];
00395 long nn=nnx*nny;
00396
00397
00398 for (i=0; i<5*nx*ny; ++i) ata[i]=0.0;
00399
00400 for (i=0; i<(nn-1); ++i){
00401
00402 qi=i;
00403 qj=i;
00404
00405 loc=i;
00406 for (j=0; j<4; ++j){aaro1[j]=aa_row[colarr[4*qi+j]]; aaro2[j]=aa_row[colarr[4*qj+j]]; aa1[j]=aa[colarr[4*qi+j]]; aa2[j]=aa[colarr[4*qj+j]];}
00407 ata[loc]=mat_ata(aaro1, aa1, aaro2, aa2, 4)+norm;
00408
00409
00410 qi=i+1;
00411 for (j=0; j<4; ++j){aaro1[j]=aa_row[colarr[4*qi+j]]; aa1[j]=aa[colarr[4*qi+j]];}
00412
00413 loc=1*nn+i;
00414 ata[loc]=mat_ata(aaro1, aa1, aaro2, aa2, 4);
00415
00416
00417 if (i <= nnx*(nny-1)){
00418 qi=i+nnx-1;
00419
00420 for (j=0; j<4; ++j){aaro1[j]=aa_row[colarr[4*qi+j]]; aa1[j]=aa[colarr[4*qi+j]];}
00421
00422 loc=2*nn+i;
00423 ata[loc]=mat_ata(aaro1, aa1, aaro2, aa2, 4);
00424
00425 }
00426 if (i <= nnx*(nny-1)-1){
00427 qi=i+nnx;
00428 for (j=0; j<4; ++j){aaro1[j]=aa_row[colarr[4*qi+j]]; aa1[j]=aa[colarr[4*qi+j]];}
00429
00430 loc=3*nn+i;
00431 ata[loc]=mat_ata(aaro1, aa1, aaro2, aa2, 4);
00432
00433 }
00434
00435 if (i <= nnx*(nny-1)-2){
00436 qi=i+nnx+1;
00437 for (j=0; j<4; ++j){aaro1[j]=aa_row[colarr[4*qi+j]]; aa1[j]=aa[colarr[4*qi+j]];}
00438
00439 loc=4*nn+i;
00440 ata[loc]=mat_ata(aaro1, aa1, aaro2, aa2, 4);
00441
00442 }
00443 }
00444
00445 i=nnx*nny-1;
00446 qi=i;
00447 qj=i;
00448
00449 for (j=0; j<4; ++j){aaro1[j]=aa_row[colarr[4*qi+j]]; aaro2[j]=aa_row[colarr[4*qj+j]]; aa1[j]=aa[colarr[4*qi+j]]; aa2[j]=aa[colarr[4*qj+j]];}
00450
00451 loc=0*nn+i;
00452 ata[loc]=mat_ata(aaro1, aa1, aaro2, aa2, 4)+norm;
00453
00454
00455
00456 }
00457
00458
00459 long sign(double num)
00460
00461 {
00462 int rnum=floor((num*1000.0)+0.5);
00463 long out=1;
00464 if (rnum < 0) out=-1;
00465 if (rnum > 0) out=1;
00466 return out;
00467
00468 }
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 void blockiter(double ata[], double rh[], double x[], double *res, double resdu[], int nnx, int nny, double omega)
00484
00485
00486 {
00487 long nn=nnx*nny;
00488 long j,i;
00489 double fa[nnx], fb[nnx], fc[nnx], fx[nnx], ex[nnx], ax[nnx];
00490 double aa[nnx], ab[nnx], ac[nnx];
00491 double xa[nnx], rhs[nnx];
00492 double xi[nnx];
00493 double xh[nn];
00494
00495 double rhx[nn], rdatum;
00496
00497
00498
00499
00500
00501
00502 for (j=0; j<nny; ++j){
00503
00504 for (i=0; i<nnx; ++i){fx[i]=0.0; ex[i]=0.0;}
00505
00506 if (j < (nny-1)){
00507
00508 for (i=0; i<nnx; ++i){
00509 fa[i]=-ata[2*nn+j*nnx+i];
00510 fb[i]=-ata[3*nn+j*nnx+i];
00511 fc[i]=-ata[4*nn+j*nnx+i];
00512 xa[i]=x[(j+1)*nnx+i];
00513 }
00514 mat_mult(fa, fb, fc, xa, fx, nnx);
00515 }
00516
00517
00518
00519 if (j > 0){
00520 fa[0]=0.0;
00521 fb[0]=-ata[3*nn+(j-1)*nnx];
00522 fc[0]=-ata[2*nn+(j-1)*nnx+1];
00523 xa[0]=x[(j-1)*nnx];
00524
00525
00526 for (i=1; i<(nnx-1); ++i){
00527 fa[i]=-ata[4*nn+(j-1)*nnx+i-1];
00528 fb[i]=-ata[3*nn+(j-1)*nnx+i];
00529 fc[i]=-ata[2*nn+(j-1)*nnx+i+1];
00530
00531 xa[i]=x[(j-1)*nnx+i];
00532 }
00533 fa[nnx-1]=-ata[4*nn+j*nnx-2];
00534 fb[nnx-1]=-ata[3*nn+j*nnx-1];
00535 fc[nnx-1]=0.0;
00536 xa[nnx-1]=x[j*nnx-1];
00537
00538
00539
00540
00541 mat_mult(fa, fb, fc, xa, ex, nnx);
00542 }
00543
00544
00545
00546
00547
00548
00549 aa[0]=0.0;
00550 ab[0]=ata[j*nnx];
00551 ac[0]=ata[nn+j*nnx];
00552 xa[0]=x[j*nnx];
00553
00554 for (i=1; i<nnx; ++i){
00555 aa[i]=ata[nn+j*nnx-1+i];
00556 ab[i]=ata[j*nnx+i];
00557 ac[i]=ata[nn+j*nnx+i];
00558 xa[i]=x[j*nnx+i];
00559 }
00560
00561 mat_mult(aa, ab, ac, xa, ax, nnx);
00562
00563 for (i=0; i<nnx; ++i) rhs[i]=omega*(fx[i]+ex[i]+rh[j*nnx+i])+(1-omega)*ax[i];
00564
00565 tridag(aa, ab, ac, rhs, xi);
00566
00567
00568
00569
00570 for (i=0; i<nnx; ++i) x[j*nnx+i]=xi[i];
00571 }
00572
00573
00574
00575
00576
00577
00578
00579
00580 for (j=0; j<nny; ++j){
00581
00582 for (i=0; i<nnx; ++i){fx[i]=0.0; ex[i]=0.0; ax[i]=0.0;}
00583
00584 if (j < (nny-1)){
00585 for (i=0; i<nnx; ++i){
00586 fa[i]=ata[2*nn+j*nnx+i];
00587 fb[i]=ata[3*nn+j*nnx+i];
00588 fc[i]=ata[4*nn+j*nnx+i];
00589 xa[i]=x[(j+1)*nnx+i];
00590 }
00591 mat_mult(fa, fb, fc, xa, fx, nnx);
00592 }
00593
00594 if (j > 0){
00595 fa[0]=0.0;
00596 fb[0]=ata[3*nn+(j-1)*nnx];
00597 fc[0]=ata[2*nn+(j-1)*nnx+1];
00598 xa[0]=x[(j-1)*nnx];
00599
00600
00601 for (i=1; i<(nnx-1); ++i){
00602 fa[i]=ata[4*nn+(j-1)*nnx+i-1];
00603 fb[i]=ata[3*nn+(j-1)*nnx+i];
00604 fc[i]=ata[2*nn+(j-1)*nnx+i+1];
00605
00606 xa[i]=x[(j-1)*nnx+i];
00607
00608 }
00609
00610 fa[nnx-1]=ata[4*nn+j*nnx-2];
00611 fb[nnx-1]=ata[3*nn+j*nnx-1];
00612 fc[nnx-1]=0.0;
00613 xa[nnx-1]=x[j*nnx-1];
00614
00615 mat_mult(fa, fb, fc, xa, ex, nnx);
00616 }
00617
00618
00619 aa[0]=0.0;
00620 ab[0]=ata[j*nnx];
00621 ac[0]=ata[nn+j*nnx];
00622 xa[0]=x[j*nnx];
00623
00624 for (i=1; i<nnx; ++i){
00625 aa[i]=ata[nn+j*nnx-1+i];
00626 ab[i]=ata[j*nnx+i];
00627 ac[i]=ata[nn+j*nnx+i];
00628
00629 xa[i]=x[j*nnx+i];
00630
00631 }
00632 mat_mult(aa, ab, ac, xa, ax, nnx);
00633
00634 for (i=0; i<nnx; ++i) xh[j*nnx+i]=fx[i]+ex[i]+ax[i];
00635 }
00636
00637 *res=0.0;
00638 for (j=0; j<nny; ++j){
00639 for (i=0; i<nnx; ++i){
00640
00641 *res=*res+fabs((double)xh[j*nnx+i]-(double)rh[j*nnx+i]);
00642 resdu[j*nnx+i]=xh[j*nnx+i]-rh[j*nnx+i];
00643 }
00644 }
00645
00646 }
00647
00648
00649
00650
00651 void tridag(double veca[nx], double vecb[nx], double vecc[nx], double vecr[nx], double vecu[nx])
00652
00653 {
00654 double bet, gam[nx];
00655 int j;
00656
00657 if (vecb[0] == 0.0){printf("Error 1 in tridag\n"); return;};
00658
00659 bet=vecb[0];
00660 vecu[0]=vecr[0]/bet;
00661 for (j=1;j<nx;++j){
00662 gam[j]=vecc[j-1]/bet;
00663 bet=vecb[j]-veca[j]*gam[j];
00664 if (bet == 0.0){printf("Error 2 in tridag\n"); return;};
00665 vecu[j]=(vecr[j]-veca[j]*vecu[j-1])/bet;
00666 }
00667
00668 for (j=nx-2; j>=0; --j){
00669 vecu[j]=vecu[j]-gam[j+1]*vecu[j+1];
00670 }
00671 }
00672
00673
00674 void mat_mult(double a[nx], double b[nx], double c[nx], double x[nx], double fx[nx], int n)
00675
00676 {
00677
00678 int i;
00679 double x1, x2, x3;
00680 for (i=0; i<n; ++i){
00681
00682 x2=0.0;
00683 x3=0.0;
00684
00685 x1=b[i]*x[i];
00686 if (i > 0) x2=a[i]*x[i-1];
00687 if (i < (n-1)) x3=c[i]*x[i+1];
00688
00689 fx[i]=x1+x2+x3;
00690 }
00691 }
00692
00693
00694
00695
00696
00697
00698 void printtime()
00699
00700 {
00701 time_t timer, timerd;
00702 char *timestring;
00703 int i;
00704
00705 timerd=time(&timer);
00706 timestring=ctime(&timer);
00707 for (i=0; i<24; ++i) printf("%c", *(timestring+i));
00708 printf("\n");
00709 }
00710
00711
00712 void derotation_test(double time, double radius, double cent_x, double cent_y, double p0, double b0, double *rotf)
00713 {
00714
00715
00716 int i,j;
00717
00718
00719
00720 double svecp[2];
00721 int datum_int;
00722
00723 double xy[2], inr;
00724 double xyp[3], xym[3], xys[2], norm;
00725
00726 double slat, slat2, omeg;
00727
00728 for (i=0; i<nx; ++i){
00729 for (j=0; j<ny; ++j){
00730
00731
00732 xy[0]=((double)i+0.5-cent_x)/radius;
00733 xy[1]=((double)j+0.5-cent_y)/radius;
00734
00735 inr=sqrt(xy[0]*xy[0]+xy[1]*xy[1]);
00736
00737 if (inr <= 1.0)
00738 {
00739
00740 xyp[0]=cos(p0)*xy[0]+sin(p0)*xy[1];
00741 xyp[1]=-sin(p0)*xy[0]+cos(p0)*xy[1];
00742
00743 xym[0]=sqrt(1.0-(xyp[0]*xyp[0]+xyp[1]*xyp[1]));
00744 xym[1]=xyp[0];
00745 xym[2]=xyp[1];
00746
00747 slat=sin(b0)*xym[0]+cos(b0)*xym[2];
00748 slat2=slat*slat;
00749 omeg=2*M_PI*1e-9*(452. - 49.0*slat2 -84.0*slat2*slat2 - 31.7);
00750
00751 svecp[0]=omeg*(xym[0]*cos(b0)-xym[2]*sin(b0))*time*radius;
00752 svecp[1]=omeg*xym[1]*sin(b0)*time*radius;
00753
00754 }
00755
00756
00757
00758 else
00759 {
00760 xyp[0]=cos(p0)*xy[0]+sin(p0)*xy[1];
00761 xyp[1]=-sin(p0)*xy[0]+cos(p0)*xy[1];
00762
00763 norm=sqrt(xyp[0]*xyp[0]+xyp[1]*xyp[1]);
00764 xys[0]=xys[0]/norm;
00765 xys[1]= xys[1]/norm;
00766
00767 slat=cos(b0)*xys[1];
00768 slat2=slat*slat;
00769 omeg=2*M_PI*1e-9*(452. - 49.0*slat2 -84.0*slat2*slat2 - 31.7);
00770
00771 svecp[0]=-omeg*xys[1]*time*radius*sin(b0);
00772 svecp[1]=omeg*xys[0]*time*radius*sin(b0);
00773
00774 }
00775
00776
00777 rotf[j*nx+i]=cos(p0)*svecp[0]-sin(p0)*svecp[1];
00778 rotf[nx*ny+j*nx+i]=sin(p0)*svecp[1]+cos(p0)*svecp[1];
00779 }
00780 }
00781
00782
00783
00784 }
00785
00786
00787
00788
00789 void derotation(double radius, double cent_x, double cent_y, double dist, double p0, double b0, double *rot_coef, int order2_rot_coef, double *shift){
00790
00791
00792 int i, j, k;
00793 double xy[2], xyp[2];
00794 double xyz[3], sxyz[3];
00795 double sxyp[2], sxy[2];
00796 double inr, ind, ikf, phie, ing, beta;
00797
00798
00799 double slat, omeg;
00800
00801
00802
00803 const double au=215.020*dist;
00804 const double maxphi=asin(1.0/au);
00805
00806
00807 double sinp0=sin(p0);
00808 double cosp0=cos(p0);
00809 double sinb0=sin(b0);
00810 double cosb0=cos(b0);
00811
00812 int nlead=nx;
00813
00814
00815 #pragma omp parallel for private(i,j,xyp,inr,phie,ind,ikf,beta,xyz,slat,omeg,sxyz,sxyp,sxy)
00816 for (j=0; j<ny; ++j){
00817 for (i=0; i<nx; ++i){
00818
00819 xyp[0]=((double)i-cent_x)/radius;
00820 xyp[1]=((double)j-cent_y)/radius;
00821
00822
00823 inr=sqrt(xyp[0]*xyp[0]+xyp[1]*xyp[1]);
00824
00825
00826
00827 if (inr >= 1.0)
00828 {
00829 xyp[0]=xyp[0]/inr;
00830 xyp[1]=xyp[1]/inr;
00831 inr=1.0;
00832
00833 phie=maxphi;
00834 ind=sqrt(1.0-1.0/au/au);
00835 }
00836 else
00837 {
00838 phie=inr*maxphi;
00839 beta=tan(phie);
00840 ind=(beta*au-beta*sqrt(1.0+beta*beta-au*au*beta*beta))/(1+beta*beta);
00841 }
00842
00843 if (inr != 0.0) ikf=ind/inr; else ikf=(au-1.0)/au;
00844
00845
00846 xyz[0]=sqrt(1.0-ind*ind);
00847 xyz[1]=xyp[0]*ikf;
00848 xyz[2]=xyp[1]*ikf;
00849
00850
00851 slat=sinb0*xyz[0]+cosb0*xyz[2];
00852
00853
00854 omeg=1e-6*(rot_coef[1]*pow(slat,2)+rot_coef[2]*pow(slat,4)+rot_coef[0]);
00855
00856 sxyz[0]=-omeg*xyz[1]*cosb0;
00857 sxyz[1]=omeg*(xyz[0]*cosb0-xyz[2]*sinb0);
00858 sxyz[2]=omeg*xyz[1]*sinb0;
00859
00860
00861 sxyp[0]=sxyz[1]/ikf;
00862 sxyp[1]=sxyz[2]/ikf;
00863
00864 sxy[0]=cosp0*sxyp[0]-sinp0*sxyp[1];
00865 sxy[1]=sinp0*sxyp[0]+cosp0*sxyp[1];
00866
00867
00868
00869
00870 shift[j*nlead+i]=sxy[0]*radius;
00871 shift[ny*nlead+j*nlead+i]=sxy[1]*radius;
00872
00873 }
00874 }
00875
00876 }
00877
00878
00879
00880
00881
00882
00883 void highpass(int M, int N, double fwhm, double a[nx][ny])
00884
00885 {
00886
00887 double b[M][N];
00888 fftw_complex ac[M][N/2+1], bc[M][N/2+1];
00889 fftw_plan p;
00890 long i,j;
00891
00892 double sigma=fwhm/2.0/sqrt(2.0*log(2.0));
00893 double scale=1.0/((double)(M*N));
00894
00895
00896
00897 p = fftw_plan_dft_r2c_2d(M, N, &a[0][0], &ac[0][0], FFTW_ESTIMATE);
00898 fftw_execute(p);
00899 fftw_destroy_plan(p);
00900
00901
00902 for (j = 0; j < N; ++j){
00903 for (i = 0; i < M; ++i) {
00904
00905 b[i][j] = exp(-(pow((double)i-(double)M/2.0,2)+pow((double)j-(double)N/2.0,2))/2.0/pow(sigma,2))/2.0/M_PI/pow(sigma,2);
00906 }
00907 }
00908
00909
00910 p = fftw_plan_dft_r2c_2d(M, N, &b[0][0], &bc[0][0], FFTW_ESTIMATE);
00911 fftw_execute(p);
00912 fftw_destroy_plan(p);
00913
00914
00915
00916 for (i = 0; i < M; ++i){
00917 for (j = 0; j < N/2+1; ++j) {
00918 ac[i][j]=ac[i][j]*bc[i][j]*scale;
00919 }
00920 }
00921
00922
00923 p = fftw_plan_dft_c2r_2d(M, N, &ac[0][0], &b[0][0], FFTW_ESTIMATE);
00924 fftw_execute(p);
00925 fftw_destroy_plan(p);
00926
00927
00928 for (i = 0; i < M; ++i){
00929 for (j = 0; j < N; ++j){
00930 a[i][j]=a[i][j]-b[(i+M/2) % M][(j+N/2) % N];
00931 }
00932 }
00933
00934
00935
00936
00937 }
00938
00939
00940 void highpass_2d(int M, int N, double fwhm1, double fwhm2, double phi, double a[nx][ny])
00941
00942 {
00943
00944 double b[M][N];
00945 fftw_complex ac[M][N/2+1], bc[M][N/2+1];
00946 fftw_plan p;
00947 long i,j;
00948 double x,y;
00949
00950 double sigma1=fwhm1/2.0/sqrt(2.0*log(2.0));
00951 double sigma2=fwhm2/2.0/sqrt(2.0*log(2.0));
00952
00953 double scale=1.0/((double)(M*N));
00954 double sumgauss;
00955
00956
00957 p = fftw_plan_dft_r2c_2d(M, N, &a[0][0], &ac[0][0], FFTW_ESTIMATE);
00958 fftw_execute(p);
00959 fftw_destroy_plan(p);
00960
00961
00962 sumgauss=0.0;
00963 for (i = 0; i < N; ++i){
00964 for (j = 0; j < M; ++j) {
00965 x=cos(phi/180.0*M_PI)*((double)i-(double)M/2.0)-sin(phi/180.0*M_PI)*((double)j-(double)N/2.0);
00966 y=sin(phi/180.0*M_PI)*((double)i-(double)M/2.0)+cos(phi/180.0*M_PI)*((double)j-(double)N/2.0);
00967
00968 b[i][j] = exp(-(pow(x,2)/2.0/pow(sigma1,2)+pow(y,2)/2.0/pow(sigma2,2)));
00969 sumgauss=sumgauss+b[i][j];
00970 }
00971 }
00972
00973 for (i = 0; i < N; ++i){
00974 for (j = 0; j < M; ++j){
00975 b[i][j]=b[i][j]/sumgauss;
00976 }
00977 }
00978
00979
00980
00981
00982
00983
00984 p = fftw_plan_dft_r2c_2d(M, N, &b[0][0], &bc[0][0], FFTW_ESTIMATE);
00985 fftw_execute(p);
00986 fftw_destroy_plan(p);
00987
00988
00989
00990 for (i = 0; i < M; ++i){
00991 for (j = 0; j < N/2+1; ++j) {
00992 ac[i][j]=ac[i][j]*bc[i][j]*scale;
00993 }
00994 }
00995
00996
00997 p = fftw_plan_dft_c2r_2d(M, N, &ac[0][0], &b[0][0], FFTW_ESTIMATE);
00998 fftw_execute(p);
00999 fftw_destroy_plan(p);
01000
01001
01002 for (i = 0; i < M; ++i){
01003 for (j = 0; j < N; ++j){
01004 a[i][j]=a[i][j]-b[(i+M/2) % M][(j+N/2) % N];
01005 }
01006 }
01007
01008
01009
01010
01011 }
01012
01013
01014
01015
01016
01017 void limb_darkening(double radius, double cent_x, double cent_y, double *b, int order, double *limb_dark)
01018 {
01019
01020 int i, j, k;
01021 double *mu;
01022 double rad;
01023
01024
01025 mu=(double *)(malloc(nx*ny*sizeof(double)));
01026
01027 for (i=0; i<nx; ++i){
01028 for (j=0; j<ny; ++j){
01029 rad=sqrt(pow((double)i-cent_x,2)+pow((double)j-cent_y,2))/radius;
01030 if (rad >=1.0)
01031 {
01032 mu[j*nx+i]=1.0;
01033 }
01034 else
01035 {
01036 if (rad <= 0.998) mu[j*nx+i]=sqrt(1.0-rad*rad);
01037 if (rad > 0.998) mu[j*nx+i]=sqrt(0.002);
01038 }
01039
01040 limb_dark[j*nx+i]=0.0;
01041 }
01042 }
01043
01044
01045
01046 for (i=0; i<nx; ++i){
01047 for (j=0; j<ny; ++j){
01048 for (k=0; k<=order; ++k) limb_dark[j*nx+i]=limb_dark[j*nx+i]+b[k]*pow(mu[j*nx+i],k);
01049 }
01050 }
01051
01052 free(mu);
01053 }
01054
01055
01056
01057
01058
01059 void apod_circ(float rad, float nb, float offx, float offy, float *vd)
01060 {
01061 float *rarr;
01062 rarr=(float *)(malloc(nx*ny*sizeof(float)));
01063 int i, j;
01064
01065 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) rarr[j*nx+i]=sqrt(((float)i-((float)nx/2+offx))*((float)i-((float)ny/2+offx))+((float)j-((float)nx/2+offy))*((float)j-((float)ny/2+offy)));
01066
01067
01068 for (j=0; j<ny; ++j){
01069 for (i=0; i<nx; ++i){
01070 if (rarr[j*nx+i] < rad)
01071 vd[j*nx+i]=1.0;
01072
01073 if (rarr[j*nx+i] >= rad && rarr[j*nx+i] < (rad+nb))
01074 vd[j*nx+i]=0.5*cos(M_PI/nb*(rarr[j*nx+i]-rad))+0.5;
01075
01076 if (rarr[j*nx+i] >= (rad+nb))
01077 vd[j*nx+i]=0.0;
01078
01079
01080
01081 }
01082 }
01083
01084 }
01085
01086