00001
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <time.h>
00005 #include <string.h>
00006 #include <math.h>
00007 #include <omp.h>
00008 #include "interpol_code.h"
00009
00010 #define minval(x,y) (((x) < (y)) ? (x) : (y))
00011 #define maxval(x,y) (((x) < (y)) ? (y) : (x))
00012
00014
00015
00016 int initialize_interpol(struct initial *const_param, struct init_files *infiles, int nx, int ny, const char *dpath)
00017 {
00018
00019 char *namep;
00020 int i, j, s, t;
00021 float datum;
00022 int status;
00023 int nlead=nx;
00024
00025 int dist_order=6;
00026 int maxo=dist_order+1;
00027 float *dist_param_front, *dist_param_side;
00028 float *distx, *disty;
00029
00030 int gapfill_order=10;
00031 int gapfill_method=11;
00032 float gapfill_regular=0.0295;
00033
00034 int interpolation_order=10;
00035
00036
00037 int gapfill_order2=gapfill_order/2;
00038 int nconst=1;
00039
00040 int malign=32;
00041
00042 struct fill_struct fills;
00043 status=init_fill(gapfill_method, gapfill_regular, gapfill_order,gapfill_order2,gapfill_order2,&fills,&namep,dpath);
00044 printf("table for gapfill %s\n", namep);
00045
00046 float **dist_par_front, **dist_par_side;
00047 dist_par_front=(float **)(MKL_malloc(16*sizeof(float *),malign));
00048 dist_par_side=(float **)(MKL_malloc(16*sizeof(float *),malign));
00049
00050 for (j=0; j<16; ++j)
00051 {
00052 dist_param_front=(float *)(MKL_malloc((dist_order+1)*(dist_order+1)*2*sizeof(float),malign));
00053 *(dist_par_front+j)=dist_param_front;
00054
00055 dist_param_side=(float *)(MKL_malloc((dist_order+1)*(dist_order+1)*2*sizeof(float),malign));
00056 *(dist_par_side+j)=dist_param_side;
00057 }
00058
00059
00060
00061
00062
00063
00064
00065 FILE *distfile;
00066 size_t bytes_read;
00067
00068 float *dparam_front;
00069 dparam_front=(float *)(malloc(maxo*maxo*2*sizeof(float)));
00070
00071 float *dparam_side;
00072 dparam_side=(float *)(malloc(maxo*maxo*2*sizeof(float)));
00073
00074 char *filename_distfront=infiles->dist_file_front;
00075
00076 distfile=fopen(filename_distfront, "rb");
00077
00078 if (distfile != NULL){
00079 for (i=0; i<maxo*maxo*2; ++i){
00080 fscanf(distfile, "%f", &datum);
00081 dparam_front[i]=datum;
00082 }
00083 }
00084 fclose(distfile);
00085
00086 char *filename_distside=infiles->dist_file_side;
00087
00088 distfile=fopen(filename_distside, "rb");
00089 if (distfile != NULL){
00090 for (i=0; i<maxo*maxo*2; ++i){
00091 fscanf(distfile, "%f", &datum);
00092 dparam_side[i]=datum;
00093 }
00094 }
00095 fclose(distfile);
00096
00097
00098
00099
00100
00101 for (j=0; j<16; ++j)
00102 {
00103 dist_param_front=*(dist_par_front+j);
00104 dist_param_side=*(dist_par_side+j);
00105 for (i=0; i<maxo*maxo*2; ++i) dist_param_front[i]=dparam_front[i];
00106 for (i=0; i<maxo*maxo*2; ++i) dist_param_side[i]=dparam_side[i];
00107 }
00108
00109
00110
00111
00112
00113 float *diffrot_param;
00114
00115 int diffrot_order;
00116 char *rotcoef_file;
00117 rotcoef_file=infiles->diffrot_coef;
00118
00119 FILE *diffrot;
00120 diffrot=fopen(rotcoef_file, "r");
00121
00122 if (diffrot != NULL){
00123 fscanf(diffrot, "%d", &diffrot_order);
00124 if (diffrot_order > 0)
00125 {
00126 diffrot_param=(float *)(MKL_malloc((diffrot_order+1)*sizeof(float),malign));
00127 for (j=0; j<=diffrot_order; ++j)
00128 {
00129 fscanf(diffrot, "%f", &datum);
00130 diffrot_param[j]=datum;
00131 }
00132 }
00133 }
00134
00135
00136
00137
00138
00139 for (j=0; j<16; ++j){dist_param_front=*(dist_par_front+j); const_param->dist_coef[0][j]=dist_param_front;}
00140 for (j=0; j<16; ++j){dist_param_side=*(dist_par_side+j); const_param->dist_coef[1][j]=dist_param_side;}
00141
00142
00143 const_param->order_dist_coef=dist_order;
00144 const_param->diffrot_coef=diffrot_param;
00145 const_param->order2_rot_coef=diffrot_order;
00146 const_param->order_int=interpolation_order;
00147 const_param->nconst=nconst;
00148 const_param->fills=fills;
00149
00150 const_param->code_version="INTERPOL_2.0";
00151 MKL_free(dist_par_front);
00152 MKL_free(dist_par_side);
00153
00154 return status;
00155 }
00156
00157
00158
00159
00160
00161 void free_interpol(struct initial *const_param)
00162 {
00163 int j;
00164 free_fill(&const_param->fills);
00165 for (j=0; j<16; ++j) MKL_free(const_param->dist_coef[0][j]);
00166 for (j=0; j<16; ++j) MKL_free(const_param->dist_coef[1][j]);
00167
00168
00169
00170
00171
00172
00173
00174
00175 MKL_free(const_param->diffrot_coef);
00176 }
00177
00178
00179
00180
00181 int do_gapfill(float *image, unsigned char *mask, struct initial *const_param, char *ierror, int nx, int ny)
00182 {
00183
00184 int isample;
00185 int status;
00186 float t1;
00187 float *cnorm, *ierror_float;
00188 int i, j;
00189 int nlead=nx;
00190 int malign=32;
00191
00192 cnorm=(float *)(MKL_malloc(nlead*ny*sizeof(float),malign));
00193 ierror_float=(float *)(MKL_malloc(nlead*ny*sizeof(float),malign));
00194
00195 struct fill_struct fills=const_param->fills;
00196
00197 status=fgap_fill(&fills,image,nx,ny,nlead,mask,cnorm,ierror_float);
00198
00199 for (j=0;j<ny;j++) for (i=0;i<nx;i++) ierror[j*nlead+i]=(char)(ierror_float[j*nlead+i]*64.0-64.0);
00200
00201 MKL_free(cnorm);
00202 MKL_free(ierror_float);
00203
00204
00205 return status;
00206
00207
00208 }
00209
00210
00211 int do_interpolate(float **images, char **ierrors, float *image_out, struct keyword *key, struct keyword *key_out, struct initial *const_param, int nsample, int nx, int ny, float average, const char *dpath)
00212 {
00213
00214 void derotation(float, float, float, float, float, float, float *, int, float *, int, int);
00215 void derotation_full(struct fint_struct *fints, float *, float *, double, float, float, float, float, float, float, float, float, float, float, float, float, float *, int, int, int, int, float *, float *);
00216 long factorial(int);
00217 float intsincos(unsigned int n, unsigned int m);
00218
00219
00220
00221
00222 int i,j,isample,s,t,k;
00223 float **int_images;
00224 unsigned char **masks;
00225 float *image_intout;
00226 double t1, t0;
00227 int *status_arr;
00228 int status;
00229 struct keyword keyl;
00230
00231 int nlead=nx;
00232 int malign=32;
00233 float maxext=0.0;
00234 float fillval=sqrt(-1.0);
00235 float thresold=1.0;
00236
00237
00238
00239 int_images=(float **)(MKL_malloc(nsample*sizeof(float *), malign));
00240 masks=(unsigned char **)(MKL_malloc(nsample*sizeof(unsigned char *),malign));
00241
00242 char *namep;
00243 float *par;
00244
00245 float *rrsun, *bb0, *pp0, *xx0, *yy0, *ddist;
00246 int *ccamera, *ffocus;
00247 double *tsample;
00248
00249 ccamera=(int *)(MKL_malloc(nsample*sizeof(int),malign));
00250 ffocus=(int *)(MKL_malloc(nsample*sizeof(int),malign));
00251 rrsun=(float *)(MKL_malloc(nsample*sizeof(float),malign));
00252 bb0=(float *)(MKL_malloc(nsample*sizeof(float),malign));
00253 pp0=(float *)(MKL_malloc(nsample*sizeof(float),malign));
00254 xx0=(float *)(MKL_malloc(nsample*sizeof(float),malign));
00255 yy0=(float *)(MKL_malloc(nsample*sizeof(float),malign));
00256 ddist=(float *)(MKL_malloc(nsample*sizeof(float),malign));
00257 tsample=(double *)(MKL_malloc(nsample*sizeof(double),malign));
00258
00259 status_arr=(int *)(MKL_malloc(nsample*sizeof(int),malign));
00260
00261 float rsun, b0, p0, cent_x, cent_y, dist;
00262 double tint;
00263
00264
00265
00266
00267 float *distx, *disty;
00268 static float *dist_vec[4];
00269 float *dcoef_front, *dcoef_side;
00270
00271
00272
00273 for (isample=0; isample<nsample; ++isample){
00274 keyl=*(key+isample);
00275 ccamera[isample]=keyl.camera;
00276 ffocus[isample]=keyl.focus;
00277 rrsun[isample]=keyl.rsun;
00278 bb0[isample]=keyl.b0;
00279 pp0[isample]=keyl.p0;
00280 xx0[isample]=keyl.xx0;
00281 yy0[isample]=keyl.yy0;
00282 ddist[isample]=keyl.dist;
00283 tsample[isample]=keyl.time;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 int maxo=const_param->order_dist_coef+1;
00295
00296 float coef_x_front[maxo][maxo], coef_y_front[maxo][maxo], coef_x_side[maxo][maxo], coef_y_side[maxo][maxo];
00297 float y1[maxo], x1[maxo];
00298
00299 float rad=(float)nx/2.0;
00300
00301
00302 static int focus_current=0;
00303
00304 float *distx_front, *disty_front, *distx_side, *disty_side;
00305
00306 if (focus_current == 0)
00307 {
00308 distx_front=(float *)(malloc(nx*ny*sizeof(float)));
00309 disty_front=(float *)(malloc(nx*ny*sizeof(float)));
00310 distx_side=(float *)(malloc(nx*ny*sizeof(float)));
00311 disty_side=(float *)(malloc(nx*ny*sizeof(float)));
00312 }
00313
00314
00315 if (ffocus[0] != focus_current)
00316 {
00317 focus_current=ffocus[0];
00318
00319 dcoef_front=const_param->dist_coef[0][ffocus[0]-1];
00320 dcoef_side=const_param->dist_coef[1][ffocus[0]-1];
00321
00322
00323 t0=dsecnd();
00324
00325
00326 for (s=0; s<maxo; ++s){for (t=0; t<maxo; ++t){coef_x_front[s][t]=dcoef_front[s*maxo+t]; coef_y_front[s][t]=dcoef_front[s*maxo+t+maxo*maxo];}}
00327 for (s=0; s<maxo; ++s){for (t=0; t<maxo; ++t){coef_x_side[s][t]=dcoef_side[s*maxo+t]; coef_y_side[s][t]=dcoef_side[s*maxo+t+maxo*maxo];}}
00328
00329
00330
00331
00332 #pragma omp parallel for private(j,i,y1,x1,s,t)
00333 for (j=0; j<ny; ++j)
00334 for (i=0; i<nx; ++i)
00335 {
00336 y1[0]=1.0;
00337 y1[1]=((float)j-rad)/rad;
00338 for (t=2; t<maxo; ++t)y1[t]=y1[t-1]*y1[1];
00339
00340 x1[0]=1.0;
00341 x1[1]=((float)i-rad)/rad;
00342 for (s=2; s<maxo; ++s)x1[s]=x1[s-1]*x1[1];
00343
00344
00345
00346 distx_front[j*nx+i]=0.0;
00347 disty_front[j*nx+i]=0.0;
00348
00349 distx_side[j*nx+i]=0.0;
00350 disty_side[j*nx+i]=0.0;
00351
00352 for (s=0; s<maxo; ++s){
00353 for (t=0; t<maxo; ++t){
00354 if (coef_x_front[s][t] != 0.0) distx_front[j*nx+i]=distx_front[j*nx+i]+coef_x_front[s][t]*x1[s]*y1[t]*rad;
00355 if (coef_y_front[s][t] != 0.0) disty_front[j*nx+i]=disty_front[j*nx+i]+coef_y_front[s][t]*x1[s]*y1[t]*rad;
00356
00357 if (coef_x_side[s][t] != 0.0) distx_side[j*nx+i]=distx_side[j*nx+i]+coef_x_side[s][t]*x1[s]*y1[t]*rad;
00358 if (coef_y_side[s][t] != 0.0) disty_side[j*nx+i]=disty_side[j*nx+i]+coef_y_side[s][t]*x1[s]*y1[t]*rad;
00359 }
00360 }
00361
00362 }
00363
00364 dist_vec[0]=distx_front;
00365 dist_vec[1]=disty_front;
00366 dist_vec[2]=distx_side;
00367 dist_vec[3]=disty_side;
00368
00369
00370
00371 }
00372
00373
00374
00375
00376
00377
00378
00379 rsun=key_out->rsun;
00380 b0=key_out->b0;
00381 p0=key_out->p0;
00382 cent_x=key_out->xx0;
00383 cent_y=key_out->yy0;
00384 dist=key_out->dist;
00385 tint=key_out->time;
00387
00388 float p0min=pp0[0],p0max=pp0[0],b0min=bb0[0],b0max=bb0[0],rsmin=rrsun[0],rsmax=rrsun[0],x0min=xx0[0],x0max=xx0[0],y0min=yy0[0],y0max=yy0[0];
00389
00390 float limp0=0.0028*M_PI/180;
00391 float limb0=0.05*M_PI/180;
00392 float limrsun=0.1;
00393 float limx0=1.0;
00394 float limy0=1.0;
00395
00396
00397
00398
00399 for (isample=1; isample < nsample; ++isample){
00400
00401 p0min=minval(pp0[isample], p0min);
00402 p0max=maxval(pp0[isample], p0max);
00403 b0min=minval(bb0[isample], b0min);
00404 b0max=maxval(bb0[isample], b0max);
00405 rsmin=minval(rrsun[isample], rsmin);
00406 rsmax=maxval(rrsun[isample], rsmax);
00407 x0min=minval(xx0[isample], x0min);
00408 x0max=maxval(xx0[isample], x0max);
00409 y0min=minval(yy0[isample], y0min);
00410 y0max=maxval(yy0[isample], y0max);
00411 }
00412
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 int derot_status=0;
00430
00431 if ((p0max-p0min) > limp0) derot_status=1;
00432 if ((b0max-b0min) > limb0) derot_status=1;
00433 if ((rsmax-rsmin) > limrsun) derot_status=1;
00434 if ((x0max-x0min) > limx0) derot_status=1;
00435 if ((y0max-y0min) > limy0) derot_status=1;
00436
00437
00438 if (fabs(p0max-p0) > limp0) derot_status=1;
00439 if (fabs(p0-p0min) > limp0) derot_status=1;
00440
00441
00442 printf("derot status %d\n", derot_status);
00444
00445 for (isample=0; isample < nsample; ++isample) status_arr[isample]=0;
00446
00447
00448
00449
00450 t0=dsecnd();
00451 t1=dsecnd();
00452
00453 float *xin, *yin;
00454
00455 xin=(float *)(MKL_malloc(nlead*ny*sizeof(float),malign));
00456 yin=(float *)(MKL_malloc(nlead*ny*sizeof(float),malign));
00457
00458
00459 struct fint_struct fints, fints_error;
00460
00461
00462 int order_int=const_param->order_int;
00463 int order2_int=order_int/2;
00464
00465
00466
00467
00468 init_finterpolate_wiener(&fints,order_int,0,1.0,2,1,1,&namep, dpath);
00469 printf("table name for interpolation %s\n", namep);
00470 free(namep);
00471
00472 init_finterpolate_linear(&fints_error, 0.0);
00473
00474
00475 double time;
00476
00477
00478
00479
00480 float coef_x[maxo][maxo], coef_y[maxo][maxo];
00481
00482 float *dcoef;
00483
00484
00486
00487
00488 long bin_si, bin_tj, bin_sip, bin_tjp;
00489 float av_offset_x=0.0;
00490 float av_offset_y=0.0;
00491 int sp, tp, ip, jp;
00492 float rad_term1=0.0, rad_term2=0.0, rad_term3=0.0, rad_term4=0.0;
00493
00494
00495
00496
00497
00498
00499
00500
00501 for (isample=0; isample<nsample; ++isample){
00502
00503 dcoef=const_param->dist_coef[ccamera[isample]][ffocus[isample]-1];
00504 for (s=0; s<maxo; ++s){for (t=0; t<maxo; ++t){coef_x[s][t]=dcoef[s*maxo+t]; coef_y[s][t]=dcoef[s*maxo+t+maxo*maxo];}}
00505
00506
00507
00508 float xc=(xx0[isample]-rad)/rad;
00509 float yc=(yy0[isample]-rad)/rad;
00510
00511
00512
00513
00514
00515 av_offset_x=0.0;
00516 av_offset_y=0.0;
00517 rad_term1=0.0; rad_term2=0.0; rad_term3=0.0; rad_term4=0.0;
00518
00519 for (s=0; s<maxo; ++s) for (t=0; t<=(maxo-1-s); ++t) for (i=0; i<=s; ++i) for (j=0; j<=t; ++j){
00520
00521 bin_si=(float)factorial(s)/factorial(i)/factorial(s-i);
00522 bin_tj=(float)factorial(t)/factorial(j)/factorial(t-j);
00523
00524 av_offset_x += coef_x[s][t]*bin_si*bin_tj*pow(rrsun[isample]/rad, s+t-(i+j))*pow(xc, i)*pow(yc, j)*intsincos(s-i+2,t-j)*2.0*rad;
00525 av_offset_y += coef_y[s][t]*bin_si*bin_tj*pow(rrsun[isample]/rad, s+t-(i+j))*pow(xc, i)*pow(yc, j)*intsincos(s-i,t-j+2)*2.0*rad;
00526
00527 rad_term1 += 2.0*coef_x[s][t]*bin_si*bin_tj*pow(rrsun[isample]/rad,s+t-(i+j)+1)*pow(xc,i)*pow(yc,j)*intsincos(s-i+1,t-j);
00528 rad_term3 += 2.0*coef_y[s][t]*bin_si*bin_tj*pow(rrsun[isample]/rad,s+t-(i+j)+1)*pow(xc,i)*pow(yc,j)*intsincos(s-i,t-j+1);
00529
00530 for (sp=0; sp<maxo; ++sp) for (tp=0; tp<=(maxo-1-s); ++tp) for (ip=0; ip<=sp; ++ip) for (jp=0; jp<=tp; ++jp){
00531
00532 bin_sip=(float)factorial(sp)/factorial(ip)/factorial(sp-ip);
00533 bin_tjp=(float)factorial(tp)/factorial(jp)/factorial(tp-jp);
00534
00535 rad_term2 += coef_x[s][t]*coef_x[sp][tp]*bin_si*bin_tj*bin_sip*bin_tjp*pow(rsun/rad,s+t+sp+tp-(i+j+ip+jp))*pow(xc, i+ip)*pow(yc, j+jp)*intsincos(s+sp-i-ip, t+tp-j-jp);
00536 rad_term4 += coef_y[s][t]*coef_y[sp][tp]*bin_si*bin_tj*bin_sip*bin_tjp*pow(rsun/rad,s+t+sp+tp-(i+j+ip+jp))*pow(xc, i+ip)*pow(yc, j+jp)*intsincos(s+sp-i-ip, t+tp-j-jp);
00537 }
00538 }
00539
00540
00541
00542
00543
00544 xx0[isample]=xx0[isample]-av_offset_x;
00545 yy0[isample]=yy0[isample]-av_offset_y;
00546 rrsun[isample]=sqrt(rrsun[isample]*rrsun[isample]-(rad_term1+rad_term2+rad_term3+rad_term4)*rad*rad);
00547
00548
00549 }
00550
00552
00553 t1=dsecnd()-t1;
00554
00555
00556
00557 t1=dsecnd();
00558
00559 int order2_rot_coef=const_param->order2_rot_coef;
00560
00561 float *rot_coef;
00562 rot_coef=(float *)(MKL_malloc((order2_rot_coef+1)*sizeof(float), malign));
00563
00564 for (i=0; i<=order2_rot_coef; ++i) rot_coef[i]=const_param->diffrot_coef[i];
00565
00566
00567
00568 t1=dsecnd()-t1;
00569
00570
00572 float *ierror_out, *ierror_float;
00573 unsigned char *maskp;
00574 ierror_float=(float *)(MKL_malloc(nlead*ny*sizeof(float),malign));
00575 ierror_out=(float *)(MKL_malloc(nlead*ny*sizeof(float),malign));
00576 for (j=0; j<nlead; ++j) for (i=0; i<nx; ++i) ierror_out[j*nx+i]=0.0;
00577
00578 float *shift;
00579 shift=(float *)(malloc(nlead*ny*2*sizeof(float)));
00580
00581
00582 derotation(rsun, cent_x, cent_y, dist, p0, b0, rot_coef, order2_rot_coef, shift, nx, ny);
00584 for (isample=0; isample<nsample; ++isample){
00585 printf("pang %f %f\n", pp0[isample], p0);
00586 if (status_arr[isample] == 0){
00587
00588
00589 time=(double)(tsample[isample]-tint);
00590 printf("time lag: %g \n", time);
00591
00592
00593
00594
00595 distx=dist_vec[ccamera[isample]*2];
00596 disty=dist_vec[ccamera[isample]*2+1];
00597
00598
00599
00601 t1=dsecnd();
00602
00603 if (derot_status == 1)
00604 derotation_full(&fints_error, distx, disty, time, rrsun[isample], rsun, xx0[isample], cent_x, yy0[isample], cent_y, ddist[isample], dist, pp0[isample], p0, bb0[isample], b0, rot_coef, order2_rot_coef,const_param->order_dist_coef, nx, ny, xin, yin);
00605
00606
00607 if (derot_status == 0)
00608 {
00609 #pragma omp parallel for private(j,i)
00610 for (j=0; j<ny; ++j)
00611 for (i=0; i<nx; ++i)
00612 {
00613 xin[j*nlead+i]=(float)i + distx[j*nlead+i]+(xx0[isample]-cent_x)+((float)i-cent_x)/rsun*(rrsun[isample]-rsun)+shift[j*nlead+i]*time;
00614 yin[j*nlead+i]=(float)j + disty[j*nlead+i]+(yy0[isample]-cent_y)+((float)j-cent_y)/rsun*(rrsun[isample]-rsun)+shift[ny*nlead+j*nlead+i]*time;
00615 }
00616 }
00617
00618
00620
00621 image_intout=(float *)(MKL_malloc(nlead*ny*sizeof(float),malign));
00622
00623
00624 status_arr[isample]=finterpolate(&fints,*(images+isample), xin, yin, image_intout, nx,ny,nlead,nx,ny,nlead,fillval);
00625 t1=dsecnd()-t1;
00626
00627
00628
00629 *(int_images+isample)=image_intout;
00631
00632
00633
00634
00636
00637 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) ierror_float[j*nlead+i]=(float)((*(ierrors+isample))[j*nlead+i]+64)/64.0;
00638 finterpolate(&fints_error,ierror_float, xin, yin, ierror_out, nx,ny,nlead,nx,ny,nlead,2.0f);
00639
00640
00641 maskp=(unsigned char *)(MKL_malloc(nlead*ny*sizeof(unsigned char),malign));
00642 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) if (ierror_out[j*nlead+i] > thresold || isnan(image_intout[j*nlead+i])) maskp[j*nlead+i]=1; else maskp[j*nlead+i]=0;
00643 *(masks+isample)=maskp;
00644
00645
00646 }
00647
00648 }
00649
00650
00651
00652
00653
00654
00656
00657
00658 int nvalid=0;
00659
00660 for (isample=0; isample<nsample; ++isample){nvalid=nvalid+status_arr[isample];}
00661 nvalid=nsample-nvalid;
00662
00663
00664
00665
00666
00667 double *tvalid;
00668 float **valid_images;
00669 unsigned char **valid_masks;
00670
00671
00672 tvalid=(double *)(MKL_malloc(nvalid*sizeof(double),malign));
00673 valid_images=(float **)(MKL_malloc(nvalid*sizeof(float *),malign));
00674 valid_masks=(unsigned char **)(MKL_malloc(nvalid*sizeof(unsigned char *),malign));
00675
00676 int count=0;
00677 for (isample=0; isample<nsample; ++isample){
00678 if (status_arr[isample] == 0){
00679 tvalid[count]=tsample[isample];
00680 valid_images[count]=int_images[isample];
00681 valid_masks[count]=masks[isample];
00682 ++count;
00683 }
00684 }
00685
00686
00687
00688
00689
00690
00691 char *namep_temp;
00692 t1=dsecnd();
00693 if (average < 0.0)
00694 {
00695 status=tinterpolate(nvalid,tvalid,tint,const_param->nconst,valid_images,valid_masks,image_out,nx,ny,nlead,1,&namep_temp, fillval,dpath);
00696 }
00697 else {
00698
00699 int torder=4;
00700 double avgval=round(average/2.0/45.0/2.0)*2.0;
00701 int hwidth=(int)(avgval*1.5);
00702 status=taverage(nvalid,tvalid,tint,const_param->nconst,valid_images,valid_masks,image_out,nx,ny,nlead,1,&namep_temp, tavg_cosine,torder,45.0,hwidth,avgval,avgval/2.0,fillval,dpath);
00703 }
00704
00705
00706 printf("table name for temporal interpolation %s\n", namep_temp);
00707 free(namep_temp);
00708
00709 t0=dsecnd()-t0;
00710
00711
00712 MKL_Free_Buffers();
00713
00714
00715 for (isample=0;isample<nsample;isample++){if (status_arr[isample] ==0){MKL_free(*(int_images+isample)); MKL_free(*(masks+isample));}}
00716 free_finterpolate(&fints_error);
00717 free_finterpolate(&fints);
00718 free(shift);
00719
00720 MKL_free(int_images);
00721
00722 MKL_free(masks);
00723
00724 MKL_free(xin);
00725
00726 MKL_free(yin);
00727
00728
00729
00730 MKL_free(rot_coef);
00731
00732
00733
00734 MKL_free(ccamera);
00735 MKL_free(ffocus);
00736 MKL_free(tsample);
00737 MKL_free(ddist);
00738 MKL_free(xx0);
00739 MKL_free(yy0);
00740 MKL_free(pp0);
00741 MKL_free(bb0);
00742 MKL_free(rrsun);
00743
00744 MKL_free(status_arr);
00745
00746 MKL_free(ierror_out);
00747 MKL_free(ierror_float);
00748
00749 if (nvalid > 0){
00750 MKL_free(tvalid);
00751 MKL_free(valid_images);
00752 MKL_free(valid_masks);
00753 }
00755
00756
00757 return status;
00758
00759
00760
00761 }
00762
00763
00764 void derotation_full(struct fint_struct *fints, float *distx, float *disty, double time, float radius, float radius_t, float cent_x, float cent_x_t, float cent_y, float cent_y_t, float dist, float dist_t, float p0, float p0_t, float b0, float b0_t, float *rot_coef, int order2_rot_coef, int order_dist, int nx, int ny, float *xin, float *yin){
00765
00766
00767 int i,j, k, s, t;
00768
00769
00770
00771 float xyzr[3], xyzb[3];
00772
00773 int nlead=nx;
00774
00775 float xy[2], xy_t[2], xyp_t[2], xyz[3], xyz_t[3];
00776 float inr, ind;
00777 float inr_t, ind_t;
00778 float xyp[2], xyo[2];
00779 float ikf;
00780
00781 float phie, beta;
00782 float slat;
00783 float omeg_t;
00784 float singam, cosgam, phi, ing, inf;
00785
00786 const float au=215.020*dist;
00787 const float au_t=215.020*dist_t;
00788
00789 int maxo=order_dist+1;
00790
00791 float maxphi=asin(1.0/au);
00792 float maxphi_t=asin(1.0/au_t);
00793 float coef_x[maxo][maxo], coef_y[maxo][maxo];
00794
00795 float *xinput, *yinput;
00796 xinput=(float *)(malloc(nlead*ny*sizeof(float)));
00797 yinput=(float *)(malloc(nlead*ny*sizeof(float)));
00798
00799
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811 float rad=(float)nx/2.0;
00812 float x1a,x2a,x3a,xx,yy, y1,y2,y3;
00813
00814 float cos_p0_t=cos(p0_t);
00815 float sin_p0_t=sin(p0_t);
00816 float cos_b0_t=cos(b0_t);
00817 float sin_b0_t=sin(b0_t);
00818 float cos_b0=cos(b0);
00819 float sin_b0=sin(b0);
00820 float sin_p0=sin(p0);
00821 float cos_p0=cos(p0);
00822
00823
00824
00826
00827
00828
00829
00830
00831
00832
00833 #pragma omp parallel for private(j,i,k,xy,xy_t,xyp,inf,inr_t,xyp_t,phie,beta,ind_t,xyz_t,xyzb,slat,omeg_t,xyzr,xyz,singam,cosgam,phi,ing,ikf,xyo,x1a,x2a,x3a,y1,y2,y3,xx,yy)
00834 for (j=0; j<ny; j++){
00835 for (i=0; i< nx; i++){
00836
00837
00838
00839 xy[1]=(float)j;
00840 xy_t[1]=((float)j-cent_y_t)/radius_t;
00841
00842 xy[0]=(float)i;
00843 xy_t[0]=((float)i-cent_x_t)/radius_t;
00844
00845 inr_t=sqrt(xy_t[0]*xy_t[0]+xy_t[1]*xy_t[1]);
00846
00847 xyp_t[0]=cos_p0_t*xy_t[0]+sin_p0_t*xy_t[1];
00848 xyp_t[1]=-sin_p0_t*xy_t[0]+cos_p0_t*xy_t[1];
00849
00850
00851 if (inr_t >= 1.0)
00852 {
00853 xy[0]=(xy[0]-cent_x_t)/inr_t+cent_x_t;
00854 xy[1]=(xy[1]-cent_y_t)/inr_t+cent_y_t;
00855 xyp_t[0]=xyp_t[0]/inr_t;
00856 xyp_t[1]=xyp_t[1]/inr_t;
00857 inr_t=1.0;
00858 phie=maxphi_t;
00859 ind_t=sqrt(1.0-1.0/au_t/au_t);
00860 }
00861 else
00862 {
00863 phie=inr_t*maxphi_t;
00864 beta=tan(phie);
00865 ind_t=(beta*au_t-beta*sqrt(1.0+beta*beta-au_t*au_t*beta*beta))/(1+beta*beta);
00866 }
00867
00868
00869
00870
00871 if (inr_t != 0.0) ikf=ind_t/inr_t; else ikf=(au_t-1.0)/au_t;
00872
00873 xyz_t[0]=sqrt(1.0-ind_t*ind_t);
00874 xyz_t[1]= xyp_t[0]*ikf;
00875 xyz_t[2]= xyp_t[1]*ikf;
00876
00877
00878 xyzb[0]=cos_b0_t*xyz_t[0]-sin_b0_t*xyz_t[2];
00879 xyzb[1]=xyz_t[1];
00880 xyzb[2]=sin_b0_t*xyz_t[0]+cos_b0_t*xyz_t[2];
00881
00882
00883
00884 slat=xyzb[2];
00885
00886
00887 omeg_t=1e-6*(rot_coef[1]*pow(slat,2)+rot_coef[2]*pow(slat,4)+rot_coef[0])*(float)time;
00888
00889
00890
00891 xyzr[0]=cos(omeg_t)*xyzb[0]-sin(omeg_t)*xyzb[1];
00892 xyzr[1]=sin(omeg_t)*xyzb[0]+cos(omeg_t)*xyzb[1];
00893 xyzr[2]=xyzb[2];
00894
00895
00896
00897
00898
00899
00900
00901 xyz[0]=cos_b0*xyzr[0]+sin_b0*xyzr[2];
00902 xyz[1]=xyzr[1];
00903 xyz[2]=-sin_b0*xyzr[0]+cos_b0*xyzr[2];
00904
00905
00906
00907
00908 singam=sqrt(xyz[1]*xyz[1]+xyz[2]*xyz[2]);
00909 cosgam=xyz[0];
00910 phi=atan(singam/(au-cosgam));
00911 ing=phi/maxphi;
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929 if (singam != 0.0) ikf=ing/singam; else ikf=au_t/(au_t-1.0);
00930
00931
00932 xyp[0]=xyz[1]*ikf;
00933 xyp[1]=xyz[2]*ikf;
00934
00935
00936
00937
00938 xyo[0]=(cos_p0*xyp[0]-sin_p0*xyp[1]);
00939 xyo[1]=(sin_p0*xyp[0]+cos_p0*xyp[1]);
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949 xx=(float)i+xyo[0]*radius+cent_x-xy[0];
00950 yy=(float)j+xyo[1]*radius+cent_y-xy[1];
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965 xinput[j*nlead+i]=xx;
00966 yinput[j*nlead+i]=yy;
00967 }
00968 }
00969
00970
00971 float *distx_out, *disty_out;
00972 distx_out=(float *)(malloc(nlead*ny*sizeof(float)));
00973 disty_out=(float *)(malloc(nlead*ny*sizeof(float)));
00974
00975 finterpolate(fints,distx, xinput, yinput, distx_out, nx,ny,nlead,nx,ny,nlead,0.0);
00976 finterpolate(fints,disty, xinput, yinput, disty_out, nx,ny,nlead,nx,ny,nlead,0.0);
00977
00978
00979
00980 #pragma omp parallel for private(j,i)
00981 for (j=0; j<ny; ++j)
00982 for (i=0; i<nx; ++i)
00983 {
00984 xin[j*nlead+i]=xinput[j*nlead+i]+distx_out[j*nlead+i];
00985 yin[j*nlead+i]=yinput[j*nlead+i]+disty_out[j*nlead+i];
00986 }
00987
00988 free(distx_out);
00989 free(disty_out);
00990 free(xinput);
00991 free(yinput);
00992
00993 }
00994
00995
00996 void derotation(float radius, float cent_x, float cent_y, float dist, float p0, float b0, float *rot_coef, int order2_rot_coef, float *shift, int nx, int ny){
00997
00998
00999 int i, j, k;
01000 float xy[2], xyp[2];
01001 float xyz[3], sxyz[3];
01002 float sxyp[2], sxy[2];
01003 float inr, ind, ikf, phie, ing, beta;
01004
01005
01006 float slat, omeg;
01007 float singam, cosgam, phi;
01008
01009
01010 const float au=215.020*dist;
01011 const float maxphi=asin(1.0/au);
01012
01013
01014 float sinp0=sin(p0);
01015 float cosp0=cos(p0);
01016 float sinb0=sin(b0);
01017 float cosb0=cos(b0);
01018
01019 int nlead=nx;
01020
01021
01022 #pragma omp parallel for private(i,j,xyp,inr,phie,ind,ikf,beta,xyz,slat,omeg,sxyz,sxyp,sxy,xy)
01023 for (j=0; j<ny; ++j){
01024 for (i=0; i<nx; ++i){
01025
01026 xy[0]=(cosp0*(i-cent_x)-sinp0*(j-cent_y))/radius;
01027 xy[1]=(sinp0*(i-cent_x)+cosp0*(j-cent_y))/radius;
01028
01029
01030
01031
01032
01033 inr=sqrt(xy[0]*xy[0]+xy[1]*xy[1]);
01034
01035
01036
01037 if (inr >= 1.0)
01038 {
01039 xy[0]=xy[0]/inr;
01040 xy[1]=xy[1]/inr;
01041 inr=1.0;
01042
01043 phie=maxphi;
01044 ind=sqrt(1.0-1.0/au/au);
01045 }
01046 else
01047 {
01048 phie=inr*maxphi;
01049 beta=tan(phie);
01050 ind=(beta*au-beta*sqrt(1.0+beta*beta-au*au*beta*beta))/(1+beta*beta);
01051 }
01052
01053 if (inr != 0.0) ikf=ind/inr; else ikf=(au-1.0)/au;
01054
01055
01056 xyz[0]=sqrt(1.0-ind*ind);
01057 xyz[1]=xy[0]*ikf;
01058 xyz[2]=xy[1]*ikf;
01059
01060
01061 slat=sinb0*xyz[0]+cosb0*xyz[2];
01062
01063
01064 omeg=1e-6*(rot_coef[1]*pow(slat,2)+rot_coef[2]*pow(slat,4)+rot_coef[0]);
01065
01066 sxyz[0]=-omeg*xyz[1]*cosb0;
01067 sxyz[1]=omeg*(xyz[0]*cosb0-xyz[2]*sinb0);
01068 sxyz[2]=omeg*xyz[1]*sinb0;
01069
01070 singam=sqrt(xyz[1]*xyz[1]+xyz[2]*xyz[2]);
01071 cosgam=xyz[0];
01072 phi=atan(singam/(au-cosgam));
01073 ing=phi/maxphi;
01074
01075
01076 if (singam != 0.0) ikf=ing/singam; else ikf=au/(au-1.0);
01077
01078 sxyp[0]=sxyz[1]/ikf;
01079 sxyp[1]=sxyz[2]/ikf;
01080
01081 sxy[0]=cosp0*sxyp[0]-sinp0*sxyp[1];
01082 sxy[1]=sinp0*sxyp[0]+cosp0*sxyp[1];
01083
01084
01085
01086
01087 shift[j*nlead+i]=sxy[0]*radius;
01088 shift[ny*nlead+j*nlead+i]=sxy[1]*radius;
01089
01090 }
01091 }
01092
01093 }
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105 long factorial(int n)
01106 {
01107 int i;
01108 long res=1;
01109 if (n >=1){
01110 for (i=1; i<=n; ++i) res=res*(long)i;
01111 }
01112 return res;
01113 }
01114
01115
01116 float intsincos(unsigned int n, unsigned int m)
01117 {
01118 int mp, np;
01119 float f=1.0;
01120
01121 if (n % 2 == 0 && m % 2 ==0){
01122
01123 for (np=2; np<=n; np=np+2){
01124 f=f*((float)np-1.0)/((float)np+(float)m);
01125 }
01126
01127
01128 for (mp=2; mp<=m; mp=mp+2){
01129 f=f*((float)mp-1.0)/(float)mp;
01130 }
01131
01132 return f;
01133 }
01134 else return 0.0;
01135
01136 }
01137
01138 char *interpol_version()
01139 {
01140 return strdup("$Id: interpol_code.c,v 1.6 2017/08/31 22:17:13 arta Exp $");
01141 }
01142
01143
01144