00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <math.h>
00038 #include <gsl/gsl_sort_double.h>
00039 #include <gsl/gsl_blas.h>
00040 #include <gsl/gsl_linalg.h>
00041 #include <gsl/gsl_statistics.h>
00042 #include <HMIparam.h>
00043 #include <omp.h>
00044 #include <fresize.h>
00045 #include "interpol_code.h"
00046
00047
00048 char *module_name = "phasemaps_test_voigt";
00049
00050 #define kRecSetIn "input_series" //names of the arguments of the module
00051 #define kDSOut "phasemap_series"
00052 #define khcamid "hcamid" //front or side camera?
00053 #define kreduced "reduced" //maps in 64x64 or 256x256?
00054 #define minval(x,y) (((x) < (y)) ? (x) : (y))
00055 #define maxval(x,y) (((x) < (y)) ? (y) : (x))
00056
00057
00058 #define LIGHT_SIDE 2 //SIDE CAMERA
00059 #define LIGHT_FRONT 3 //FRONT CAMERA
00060 #define DARK_SIDE 0 //SIDE CAMERA
00061 #define DARK_FRONT 1 //FRONT CAMERA
00062
00063
00064
00065
00066 #define KEY_PHASE_NBMICHELSON "PHASENBM"
00067 #define KEY_PHASE_WBMICHELSON "PHASEWBM"
00068 #define KEY_PHASE_LYOT "PHASELYO"
00069 #define KEY_PHASE_LINEWIDTH "LW_MEAN"
00070 #define KEY_PHASE_LINEDEPTH "LD_MEAN"
00071 #define KEY_PHASE_LINECONTINUUM "CON_MEAN"
00072
00073
00074 ModuleArgs_t module_args[] =
00075 {
00076 {ARG_STRING, kRecSetIn, "" , "Input data series."},
00077 {ARG_STRING, kDSOut, "" , "Phase Maps series."},
00078 {ARG_INT , khcamid, "1", "Front (1) or Side (0) camera?"},
00079 {ARG_INT , kreduced, "0", "64x64 (1), 32x32 (2), 128x128 (3), or standard (256x256) resolution (0)?"},
00080 {ARG_DOUBLE , "FSRNB", "" , "FSR NB"},
00081 {ARG_DOUBLE , "FSRWB", "" , "FSR WB"},
00082 {ARG_DOUBLE , "FSRE1", "" , "FSR E1"},
00083 {ARG_DOUBLE , "FSRE2", "" , "FSR E2"},
00084 {ARG_DOUBLE , "FSRE3", "" , "FSR E3"},
00085 {ARG_DOUBLE , "FSRE4", "" , "FSR E4"},
00086 {ARG_DOUBLE , "FSRE5", "" , "FSR E5"},
00087 {ARG_FLOAT , "center", "" , "center of blocker filter"},
00088 {ARG_FLOAT , "shift", "" , "wavelength shift of the non-tunable profile"},
00089 {ARG_DOUBLE , "thresh", "", "threshold"},
00090 {ARG_INT , "cal", 0, "calibration used"},
00091 {ARG_END}
00092 };
00093
00094
00095
00096
00097
00098
00099
00100
00101 void lininterp1f(double *yinterp, double *xv, double *yv, double *x, double ydefault, int m, int minterp)
00102 {
00103 int i, j;
00104 int nrowsinterp, nrowsdata;
00105 nrowsinterp = minterp;
00106 nrowsdata = m;
00107 for (i=0; i<nrowsinterp; i++)
00108 {
00109 if((x[i] < xv[0]) || (x[i] > xv[nrowsdata-1])) yinterp[i] = ydefault;
00110 else
00111 {
00112 for(j=1; j<nrowsdata; j++)
00113 {
00114 if(x[i]<=xv[j])
00115 {
00116 yinterp[i] = (x[i]-xv[j-1]) / (xv[j]-xv[j-1]) * (yv[j]-yv[j-1]) + yv[j-1];
00117 break;
00118 }
00119 }
00120 }
00121 }
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 int MaskCreation(unsigned char *Mask, int nx, int ny, DRMS_Array_t *BadPixels, int HIMGCFID, float *image, DRMS_Array_t *CosmicRays, int nbadperm)
00136 {
00137
00138 int status=1;
00139
00140 if(nx != 4096 || ny != 4096) return status;
00141
00142 char *filename="/home/production/img_cnfg_ids";
00143 const int minid =80;
00144 const int maxid =124;
00145 const int n_tables=1;
00146 const int maxtab =256;
00147
00148 int *badpixellist=BadPixels->data;
00149 int nBadPixels=BadPixels->axis[0];
00150 printf("Number of bad pixels: %d\n",nBadPixels);
00151 int *cosmicraylist=NULL;
00152 int ncosmic=0;
00153 if(CosmicRays != NULL)
00154 {
00155 cosmicraylist=CosmicRays->data;
00156 ncosmic=CosmicRays->axis[0];
00157 }
00158 else ncosmic = -1;
00159
00160 printf("Number of cosmic-ray hits: %d\n",ncosmic);
00161
00162 int x_orig,y_orig,x_dir,y_dir;
00163 int skip, take, skip_x, skip_y;
00164 int datum, nsx, nss, nlin,nq,nqq,nsy;
00165 int idn=-1;
00166
00167 int *id=NULL, *tab=NULL, *nrows=NULL, *ncols=NULL, *rowstr=NULL, *colstr=NULL, *config=NULL;
00168 id =(int *)(malloc(maxtab*sizeof(int)));
00169 if(id == NULL)
00170 {
00171 printf("Error: memory could not be allocated to id\n");
00172 exit(EXIT_FAILURE);
00173 }
00174 tab =(int *)(malloc(maxtab*sizeof(int)));
00175 if(tab == NULL)
00176 {
00177 printf("Error: memory could not be allocated to tab\n");
00178 exit(EXIT_FAILURE);
00179 }
00180 nrows =(int *)(malloc(maxtab*sizeof(int)));
00181 if(nrows == NULL)
00182 {
00183 printf("Error: memory could not be allocated to nrows\n");
00184 exit(EXIT_FAILURE);
00185 }
00186 ncols =(int *)(malloc(maxtab*sizeof(int)));
00187 if(ncols == NULL)
00188 {
00189 printf("Error: memory could not be allocated to ncols\n");
00190 exit(EXIT_FAILURE);
00191 }
00192 rowstr=(int *)(malloc(maxtab*sizeof(int)));
00193 if(rowstr == NULL)
00194 {
00195 printf("Error: memory could not be allocated to rowstr\n");
00196 exit(EXIT_FAILURE);
00197 }
00198 colstr=(int *)(malloc(maxtab*sizeof(int)));
00199 if(colstr == NULL)
00200 {
00201 printf("Error: memory could not be allocated to colstr\n");
00202 exit(EXIT_FAILURE);
00203 }
00204 config=(int *)(malloc(maxtab*sizeof(int)));
00205 if(config == NULL)
00206 {
00207 printf("Error: memory could not be allocated to config\n");
00208 exit(EXIT_FAILURE);
00209 }
00210
00211 int skipt[4*2048];
00212 int taket[4*2048];
00213
00214 int **kx=NULL;
00215 int *kkx=NULL;
00216
00217 kx=(int **)(malloc(9*sizeof(int *)));
00218 if(kx == NULL)
00219 {
00220 printf("Error: memory could not be allocated to kx\n");
00221 exit(EXIT_FAILURE);
00222 }
00223
00224
00225 int kx0[11]={4,0,0,1,0,1,1,0,1,1,1};
00226 kx[0]=kx0;
00227 int kx1[7]={2,1,0,1,1,1,2};
00228 kx[2]=kx1;
00229 int kx2[7]={2,0,1,0,0,1,2} ;
00230 kx[4]=kx2;
00231 int kx3[7]={2,0,0,1,0,2,1};
00232 kx[1]=kx3;
00233 int kx4[7]={2,1,1,0,1,2,1};
00234 kx[3]=kx4;
00235 int kx5[7]={1,0,0,2,2};
00236 kx[5]=kx5;
00237 int kx6[5]={1,1,0,2,2};
00238 kx[6]=kx6;
00239 int kx7[5]={1,1,1,2,2};
00240 kx[7]=kx7;
00241 int kx8[5]={1,0,1,2,2};
00242 kx[8]=kx8;
00243
00244 char **filename_table=NULL;
00245 filename_table=(char **)(malloc(n_tables*sizeof(char *)));
00246 if(filename_table == NULL)
00247 {
00248 printf("Error: memory could not be allocated to filename_table\n");
00249 exit(EXIT_FAILURE);
00250 }
00251
00252 char *filename_croptable="/home/cvsuser/cvsroot/EGSE/tables/crop/crop6";
00253 filename_table[0]=filename_croptable;
00254
00255 int i,j,k;
00256 int ix, jx;
00257 int n_config;
00258 char string[256];
00259 char strng[5];
00260
00261
00262 FILE *config_file=NULL;
00263 FILE *crop_table=NULL;
00264
00265
00266 config_file=fopen(filename, "r");
00267
00268 if (config_file != NULL){
00269 fgets(string, 256, config_file);
00270 fgets(string, 256, config_file);
00271 fgets(string, 256, config_file);
00272
00273
00274 n_config=0;
00275 fscanf(config_file, "%d", &datum);
00276 id[n_config]=datum;
00277
00278 do {
00279
00280 fscanf(config_file, "%s", string);
00281
00282 config[n_config]=0;
00283 if (string[0] == '4')config[n_config]=0;
00284 if (string[0] == '2') if (string[7] == 'E')config[n_config]=1;
00285 if (string[0] == '2') if (string[7] == 'F')config[n_config]=2;
00286 if (string[0] == '2') if (string[7] == 'G')config[n_config]=3;
00287 if (string[0] == '2') if (string[7] == 'H')config[n_config]=4;
00288 if (string[0] == '1') if (string[7] == 'E')config[n_config]=5;
00289 if (string[0] == '1') if (string[7] == 'F')config[n_config]=6;
00290 if (string[0] == '1') if (string[7] == 'G')config[n_config]=7;
00291 if (string[0] == '1') if (string[7] == 'H')config[n_config]=8;
00292
00293
00294 fscanf(config_file, "%s", string);
00295
00296 fscanf(config_file, "%s", string);
00297
00298 fscanf(config_file, "%s", string);
00299
00300 if (string[0] == 'N') tab[n_config]=-1;
00301 if (string[0] == 'c') tab[n_config]=0;
00302
00303 fscanf(config_file, "%s", string);
00304
00305 fscanf(config_file, "%d", &datum);
00306
00307 fscanf(config_file, "%d", &datum);
00308
00309 fscanf(config_file, "%s", string);
00310
00311 fscanf(config_file, "%d", &datum);
00312 nrows[n_config]=datum;
00313
00314 fscanf(config_file, "%d", &datum);
00315 ncols[n_config]=datum;
00316
00317 fscanf(config_file, "%d", &datum);
00318 rowstr[n_config]=datum;
00319
00320 fscanf(config_file, "%d", &datum);
00321 colstr[n_config]=datum;
00322
00323 fscanf(config_file, "%d", &datum);
00324
00325 fscanf(config_file, "%d", &datum);
00326
00327
00328
00329 ++n_config;
00330 fscanf(config_file, "%d", &datum);
00331 id[n_config]=datum;
00332
00333
00334
00335
00336 }
00337 while (!feof(config_file) && n_config < maxtab);
00338
00339 fclose(config_file);
00340 }
00341
00342
00343
00344 for (i=0; i<n_config; ++i) if (id[i] == HIMGCFID) idn=i;
00345
00346 skip_x=ncols[idn]/2;
00347 skip_y=nrows[idn]/2;
00348
00349
00350
00351 kkx=kx[config[idn]];
00352 nq=kkx[0];
00353 nlin=kkx[2*nq+3-2]*ny/2;
00354 nss=kkx[2*nq+3-1]*nx/2;
00355
00356
00357
00358 if (idn == -1)
00359 {printf("Error: invalid HIMGCFID\n"); status=1;}
00360 else
00361 {
00362 if (tab[idn] != -1)
00363 {
00364 filename_croptable=filename_table[tab[idn]];
00365 crop_table=fopen(filename_croptable, "r");
00366
00367
00368 fscanf(crop_table, "%d", &datum);
00369
00370 fscanf(crop_table, "%d", &datum);
00371 fscanf(crop_table, "%d", &nqq);
00372
00373 for (j=0; j<nqq; ++j)
00374 {
00375 fscanf(crop_table, "%d", &skip);
00376 fscanf(crop_table, "%d", &take);
00377 skipt[j]=skip;
00378 taket[j]=take;
00379 }
00380
00381 fclose(crop_table);
00382
00383 }
00384 else
00385 {
00386 printf("no crop table\n");
00387
00388 for (k=0; k<nq; ++k)
00389 for (j=0; j<nlin; ++j)
00390 {
00391 skipt[k*nss+j]=0;
00392 taket[k*nss+j]=nss;
00393 }
00394 }
00395
00396 nsx=nss-skip_x;
00397 nsy=nlin-skip_y;
00398
00399
00400
00401 for (k=0; k<nq; ++k)
00402 {
00403 x_orig=kkx[k*2+1]*nx - kkx[k*2+1];
00404 y_orig=kkx[k*2+2]*ny - kkx[k*2+2];
00405 x_dir=-(kkx[k*2+1])*2+1;
00406 y_dir=-(kkx[k*2+2])*2+1;
00407
00408
00409 for (j=0; j<skip_y; ++j) for (i=0; i<nss; ++i) Mask[(y_orig+y_dir*j)*nx+x_orig+x_dir*i]=2;
00410 for (j=0; j<nlin; ++j) for (i=0; i<skip_x; ++i) Mask[(y_orig+y_dir*j)*nx+x_orig+x_dir*i]=2;
00411
00412
00413 for (j=0; j<nsy; ++j)
00414 {
00415 jx=j+skip_y;
00416
00417 for (i=0; i<minval(skipt[k*nss+j],nss); ++i){ix=i+skip_x; Mask[(y_orig+y_dir*jx)*nx+x_orig+x_dir*ix]=2;}
00418 for (i=skipt[k*nss+j]; i<minval(skipt[k*nss+j]+taket[k*nss+j],nss); ++i){ix=i+skip_x; Mask[(y_orig+y_dir*jx)*nx+x_orig+x_dir*ix]=0;}
00419 for (i=(skipt[k*nss+j]+taket[k*nss+j]); i<nss; ++i){ix=i+skip_x; Mask[(y_orig+y_dir*jx)*nx+x_orig+x_dir*ix]=2;}
00420 }
00421 }
00422
00423
00424 for(k=0;k<nx*ny;++k)
00425 {
00426 if(Mask[k] == 0)
00427 {
00428 if(isnan(image[k])) Mask[k] = 1;
00429 }
00430 }
00431
00432
00433 if(ncosmic != -1 && nbadperm != -1) nBadPixels = nbadperm;
00434 if(nBadPixels > 0)
00435 {
00436 for (k=0;k<nBadPixels;++k)
00437 {
00438 if(Mask[badpixellist[k]] == 0) Mask[badpixellist[k]] = 1;
00439 }
00440 }
00441
00442
00443 if(ncosmic > 0)
00444 {
00445 for (k=0;k<ncosmic;++k)
00446 {
00447 if(Mask[cosmicraylist[k]] == 0) Mask[cosmicraylist[k]] = 1;
00448 }
00449 }
00450
00451 status=0;
00452
00453 }
00454
00455 free(id);
00456 free(nrows);
00457 free(ncols);
00458 free(rowstr);
00459 free(colstr);
00460 free(config);
00461 free(kx);
00462 free(filename_table);
00463
00464 return status;
00465 }
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 void cotunetable(double NBphase,double WBphase,double E1phase,int table[4][20])
00488 {
00489
00490 int HCMNB, HCMWB, HCME1,i;
00491
00492 HCMNB = (int)round(-NBphase/6.0)+60;
00493 HCMWB = (int)round(-WBphase/6.0)+60;
00494 HCME1 = (int)round( E1phase/6.0)+60;
00495
00496
00497 for(i=0;i<20;++i) table[2][i]=0;
00498 table[0][0]=-30; table[1][0]=0; table[3][0]=0;
00499 table[0][1]=-27; table[1][1]=-6; table[3][1]=-12;
00500 table[0][2]=-24; table[1][2]=-12; table[3][2]=-24;
00501 table[0][3]=-21; table[1][3]=-18; table[3][3]= 24;
00502 table[0][4]=-18; table[1][4]=-24; table[3][4]= 12;
00503 table[0][5]=-15; table[1][5]=-30; table[3][5]=0;
00504 table[0][6]=-12; table[1][6]=24; table[3][6]=-12;
00505 table[0][7]=-9; table[1][7]=18; table[3][7]=-24;
00506 table[0][8]=-6; table[1][8]=12; table[3][8]=24;
00507 table[0][9]=-3; table[1][9]=6; table[3][9]=12;
00508 table[0][10]=0; table[1][10]=0; table[3][10]=0;
00509 table[0][11]=3; table[1][11]=-6; table[3][11]=-12;
00510 table[0][12]=6; table[1][12]=-12;table[3][12]=-24;
00511 table[0][13]=9; table[1][13]=-18;table[3][13]=24;
00512 table[0][14]=12; table[1][14]=-24;table[3][14]=12;
00513 table[0][15]=15; table[1][15]=-30;table[3][15]=0;
00514 table[0][16]=18; table[1][16]=24; table[3][16]=-12;
00515 table[0][17]=21; table[1][17]=18; table[3][17]=-24;
00516 table[0][18]=24; table[1][18]=12; table[3][18]=24;
00517 table[0][19]=27; table[1][19]=6; table[3][19]=12;
00518
00519
00520 for(i=0;i<20;++i)
00521 {
00522 table[0][i] = table[0][i] + HCME1;
00523 table[1][i] = table[1][i] + HCMWB;
00524 table[3][i] = table[3][i] + HCMNB;
00525 }
00526
00527 }
00528
00529 static int setKey(DRMS_Record_t *rec, const char *key, DRMS_Value_t *value)
00530 {
00531 int rv = 0;
00532
00533 if (!rec || !key || !*key || !value)
00534 {
00535 rv = 1;
00536 }
00537 else
00538 {
00539 rv = (drms_setkey_p(rec, key, value) != DRMS_SUCCESS);
00540 }
00541
00542 if (rv)
00543 {
00544 fprintf(stderr, "Failed to set keyword %s.\n", key);
00545 }
00546
00547 return rv;
00548 }
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558 int DoIt(void) {
00559
00560 int errbufstat = setvbuf(stderr, NULL, _IONBF, BUFSIZ);
00561 int outbufstat = setvbuf(stdout, NULL, _IONBF, BUFSIZ);
00562
00563 const char *inRecQuery = cmdparams_get_str(&cmdparams , kRecSetIn,NULL);
00564 const char *dsout = cmdparams_get_str(&cmdparams , kDSOut ,NULL);
00565 int camera = cmdparams_get_int(&cmdparams , khcamid ,NULL);
00566 int reduced = cmdparams_get_int(&cmdparams , kreduced ,NULL);
00567 FSR[0] = cmdparams_get_double(&cmdparams,"FSRNB" , NULL);
00568 FSR[1] = cmdparams_get_double(&cmdparams,"FSRWB" , NULL);
00569 FSR[2] = cmdparams_get_double(&cmdparams,"FSRE1" , NULL);
00570 FSR[3] = cmdparams_get_double(&cmdparams,"FSRE2" , NULL);
00571 FSR[4] = cmdparams_get_double(&cmdparams,"FSRE3" , NULL);
00572 FSR[5] = cmdparams_get_double(&cmdparams,"FSRE4" , NULL);
00573 FSR[6] = cmdparams_get_double(&cmdparams,"FSRE5" , NULL);
00574 float centerblocker2 = cmdparams_get_float(&cmdparams,"center", NULL);
00575 float shiftw = cmdparams_get_float(&cmdparams, "shift" , NULL);
00576 thresh = cmdparams_get_double(&cmdparams,"thresh", NULL);
00577 int calibration = cmdparams_get_int(&cmdparams, "cal" , NULL);
00578
00579 char COMMENT[256];
00580
00581 char *fnamePath = strdup(__FILE__);
00582
00583 XASSERT(fnamePath != NULL);
00584
00585 char *fname = basename(fnamePath);
00586
00587 XASSERT(strlen(fname) > 1 || *fname != '.');
00588
00589 snprintf(COMMENT, sizeof(COMMENT), "Code used: %s; CALIBRATION USED IS:", fname);
00590 if(calibration == 0)
00591 {
00592 printf("CALIBRATION USED IS CALIBRATION 11, VALID FROM MAY 2010 TO JANUARY 18, 2012\n");
00593 strcat(COMMENT," 11");
00594 }
00595 if(calibration == 1)
00596 {
00597 printf("CALIBRATION USED IS CALIBRATION 12, VALID FROM JANUARY 18, 2012 TO JANUARY 15, 2014\n");
00598 strcat(COMMENT," 12");
00599 }
00600 if(calibration == 2)
00601 {
00602 printf("CALIBRATION USED IS CALIBRATION 13, VALID FROM JANUARY 15, 2014\n");
00603 strcat(COMMENT," 13");
00604 }
00605 if(calibration != 0 && calibration != 1 && calibration != 2)
00606 {
00607 printf("the calibration requested does not exist\n");
00608 exit(1);
00609 }
00610
00611 char *COMMENTS= "COMMENT";
00612
00613 printf("FSR PARAMETERS = %f %f %f %f %f %f %f %f %f %f\n",FSR[0],FSR[1],FSR[2],FSR[3],FSR[4],FSR[5],FSR[6],centerblocker2,shiftw,thresh);
00614 printf("COMMENT: %s\n",COMMENT);
00615
00616 if(reduced == 1)
00617 {
00618 nx2=64;
00619 ny2=64;
00620 }
00621 if(reduced == 2)
00622 {
00623 nx2=32;
00624 ny2=32;
00625 }
00626 if(reduced == 3)
00627 {
00628 nx2=128;
00629 ny2=128;
00630 }
00631
00632 if(reduced != 3)
00633 {
00634 printf("THE CODE WAS UPDATED TO GET RID OF 2 BAD PIXELS, BUT SO FAR THE UPDATE WAS ONLY IMPLEMENTED IF NX=128\n");
00635 exit(EXIT_FAILURE);
00636 }
00637
00638 if(camera !=0 && camera !=1)
00639 {
00640 printf("Error: camera must be 0 or 1\n");
00641 exit(EXIT_FAILURE);
00642 }
00643 if(camera == 0) camera = LIGHT_SIDE;
00644 else camera = LIGHT_FRONT;
00645
00646 int status = DRMS_SUCCESS;
00647 int error = 0;
00648 int nx = 4096;
00649 int ny = 4096;
00650 int Nelem = nx*ny;
00651 int ratio;
00652 double iterstop = 1.e-7;
00653 #define nseq 27 //number of wavelength positions in the detune sequences (EXLUDING THE DARK FRAMES)
00654 #define maxsteps 155 //maximum steps allowed for the iterative Least-Squares algorithm
00655 #define nparam 6 //number of parameters we fit for
00656 int nimages = nseq+3;
00657 int indeximage[nimages];
00658 double factor = 1000.;
00659 double dpi = 2.0*M_PI;
00660 int i,j,k,iii,jjj;
00661 float distance2[nx*ny];
00662 float distance[ny2*nx2];
00663
00664 double Inten[ny2][nx2][nseq];
00665 float solarradiusmax;
00666
00667
00668 struct init_files initfiles;
00669
00670
00671 char DISTCOEFFILEF[]="/home/couvidat/cvs/JSOC/proj/lev1.5_hmi/libs/lev15/distmodel_front_o6_100624.txt";
00672 char DISTCOEFFILES[]="/home/couvidat/cvs/JSOC/proj/lev1.5_hmi/libs/lev15/distmodel_side_o6_100624.txt";
00673 char ROTCOEFFILE[] ="/home/couvidat/cvs/JSOC/proj/lev1.5_hmi/libs/lev15/rotcoef_file.txt";
00674 char CosmicRaySeries[]= "hmi.cosmic_rays";
00675 char HMISeriesTemp[256];
00676 char FSNtemps[]="00000000000000";
00677 DRMS_RecordSet_t *rectemp = NULL;
00678
00679 initfiles.dist_file_front=DISTCOEFFILEF;
00680 initfiles.dist_file_side=DISTCOEFFILES;
00681 initfiles.diffrot_coef=ROTCOEFFILE;
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691 #define nlam 9500 //MUST BE AN EVEN NUMBER
00692
00693 double dlam = 0.0016647572;
00694
00695 double lam[nlam];
00696 for(i=0;i<nlam;i++) lam[i] = ((double)i-((double)nlam-1.0)/2.0)*dlam;
00697
00698 double frontwindowint[nlam];
00699 double blockerint[nlam];
00700 double templine[nlam];
00701 double line[nlam];
00702 double linea[nlam];
00703 double lineb[nlam];
00704 double linec[nlam];
00705 double lined[nlam];
00706 double gaussian[nlam];
00707 double l;
00708 double a=0.03;
00709 if (calibration == 2) a=-0.09;
00710 double profilef[nlam];
00711 double profileg[nlam];
00712 double dlinedI0[nlam];
00713 double dlinedIc[nlam];
00714 double dlinedw[nlam];
00715 double tempval;
00716 double tempres[nseq];
00717 double err[maxsteps];
00718 double history[maxsteps][nparam];
00719 int nthreads;
00720 double ydefault = 0.;
00721 int converg;
00722 int compteur;
00723 double dIntendI0[nseq];
00724 double dIntendw[nseq];
00725 double dIntendIc[nseq];
00726 double dIntendPhi0[nseq];
00727 double dIntendPhi1[nseq];
00728 double dIntendPhi2[nseq];
00729 double Icg[nx2][nx2];
00730 double fwidthg[nx2][nx2];
00731 double fdepthg[nx2][nx2];
00732 double residual;
00733 double jose;
00734
00735 thresh=thresh/factor;
00736 size_t minimum=0;
00737 FILE *fp=NULL;
00738 int row, column;
00739 int indexref;
00740
00741
00742 char *envval = getenv("OMP_NUM_THREADS");
00743 nthreads = envval ? atoi(envval) : 1;
00744 omp_set_num_threads(nthreads);
00745 printf("number of threads for OpenMP = %d\n",nthreads);
00746
00747
00748
00749 if( (nx % nx2) == 0) ratio =nx/nx2;
00750 else
00751 {
00752 printf("Error: nx = %d must be a multiple of nx2 = %d\n",nx,nx2);
00753 exit(EXIT_FAILURE);
00754 }
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764 printf("QUERY = %s\n",inRecQuery);
00765 DRMS_RecordSet_t *data = drms_open_records(drms_env,inRecQuery,&status);
00766
00767 if (status == DRMS_SUCCESS && data != NULL && data->n > 0)
00768 {
00769 int nRecs = data->n;
00770 printf("Number of filtergrams to be read= %d \n",nRecs);
00771 if (nRecs != numberoffiltergrams_detune)
00772 {
00773 printf("Problem: program requires %d filtergrams to produce the phase maps\n",numberoffiltergrams_detune);
00774 exit(EXIT_FAILURE);
00775 }
00776
00777
00778 if(camera == LIGHT_FRONT) for(i=0;i<nimages;++i) indeximage[i]=i*2;
00779 else for(i=0;i<nimages;++i) indeximage[i]=i*2+1;
00780
00781 DRMS_Record_t *rec[nimages];
00782 DRMS_Record_t *rec2[2] ;
00783 for(i=0;i<nimages;++i) rec[i] = data->records[indeximage[i]];
00784 rec2[0] = data->records[0];
00785 rec2[1] = data->records[2*nimages-1];
00786
00787 DRMS_Segment_t *segin = NULL;
00788 int keyvalue = 0;
00789 int keyvalue2 = 0;
00790 int keyvalue3 = 0;
00791 DRMS_Type_t type = DRMS_TYPE_FLOAT;
00792 DRMS_Type_t typeEr = DRMS_TYPE_CHAR;
00793
00794 const char *keyname = "FSN";
00795 const char *keyname2 = "T_OBS";
00796 const char *HCMNB = "HWL4POS";
00797 const char *HCMPOL = "HWL3POS";
00798 const char *HCMWB = "HWL2POS";
00799 const char *HCME1 = "HWL1POS";
00800 const char *CRPIX1 = "CRPIX1";
00801 const char *CRPIX2 = "CRPIX2";
00802 const char *HIMGCFID = "HIMGCFID";
00803 const char *SCALE = "CDELT1";
00804 const char *VR = "OBS_VR";
00805 const char *VW = "OBS_VW";
00806 const char *VN = "OBS_VN";
00807 const char *pkey = "FSN_START";
00808 const char *pkey2 = "T_START";
00809 const char *pkey3 = "HCAMID";
00810 const char *key3 = "FSN_STOP";
00811 const char *key4 = "T_STOP";
00812 const char *key5 = "T_REC";
00813 const char *key6 = "FSN_REC";
00814 const char *FOCUS = "HCFTID";
00815 const char *RADIUS = "R_SUN";
00816 const char *EXPTIME = "EXPTIME";
00817 const char *HCMNB0 = "HCMNB";
00818 const char *HCMWB0 = "HCMWB";
00819 const char *HCME10 = "HCME1";
00820 const char *HCMPOL0 = "HCMPOL";
00821 const char *NXs = "NX";
00822 const char *FSRNBs = "FSRNB";
00823 const char *FSRWBs = "FSRWB";
00824 const char *FSRE1s = "FSRE1";
00825 const char *FSRE2s = "FSRE2";
00826 const char *FSRE3s = "FSRE3";
00827 const char *FSRE4s = "FSRE4";
00828 const char *FSRE5s = "FSRE5";
00829 const char *CBLOCKERs = "CBLOCKER";
00830 const char *DATEs = "DATE";
00831
00832
00833
00834 TIME interntime;
00835 TIME interntime2;
00836 TIME interntime3;
00837 float X0[nseq],Y0[nseq];
00838 float CDELT1[nseq];
00839 float RSUN[nseq];
00840 int FSN,NBADPERM,imcfg,HCAMID[nseq],HCFTID[nseq];
00841 double VELOCITY[nseq];
00842 double EXPOSURE[nseq];
00843 double velocity0=0.0, velocity1=0.0;
00844 double OBS_VR, CROTA2;
00845
00846
00847
00848
00849 struct initial const_param;
00850 unsigned char *Mask;
00851
00852 char dpath[]="/home/jsoc/cvs/Development/JSOC/";
00853 status = initialize_interpol(&const_param,&initfiles,nx,ny,dpath);
00854 if(status != 0)
00855 {
00856 printf("Error: could not initialize the gapfilling routine\n");
00857 exit(EXIT_FAILURE);
00858 }
00859
00860 Mask = (unsigned char *)malloc(Nelem*sizeof(unsigned char));
00861 if(Mask == NULL)
00862 {
00863 printf("Error: cannot allocate memory for Mask\n");
00864 exit(EXIT_FAILURE);
00865 }
00866
00867 struct fresize_struct fresizes;
00868 init_fresize_bin(&fresizes,ratio);
00869
00870
00871
00872
00873
00874
00875 int tuningint[nseq][3];
00876 int tuningpol[nseq];
00877 int axisout22[2] = {nx,ny};
00878
00879 double tuning[nseq][3];
00880 float *arrinL = NULL;
00881 float tempframe[ny2*nx2];
00882 char *ierror = NULL;
00883
00884 DRMS_Array_t *arrin = NULL;
00885 DRMS_Array_t *Ierror = NULL;
00886 DRMS_Array_t *BadPixels = NULL;
00887 DRMS_Array_t *CosmicRays= NULL;
00888
00889 Ierror = drms_array_create(typeEr,2,axisout22,NULL,&status);
00890 if(status != DRMS_SUCCESS || Ierror == NULL)
00891 {
00892 printf("Error: could not create Ierror\n");
00893 exit(EXIT_FAILURE);
00894 }
00895
00896
00897
00898 printf("READING, GAPFILLING, AND REBINNING THE FILTERGRAMS in %dx%d\n",nx2,ny2);
00899 for(i=0;i<nseq;i++)
00900 {
00901
00902
00903 segin = drms_segment_lookupnum(rec[i+2],0);
00904 arrin = drms_segment_read(segin,type,&status);
00905 if(status != DRMS_SUCCESS)
00906 {
00907 printf("Error: unable to read a data segment\n");
00908 exit(EXIT_FAILURE);
00909 }
00910 arrinL = arrin->data;
00911
00912
00913 FSN = drms_getkey_int(rec[i+2],"FSN",&status);
00914 printf("FSN IMAGE = %d\n",FSN);
00915 NBADPERM = drms_getkey_int(rec[i+2],"NBADPERM",&status);
00916 if(status != DRMS_SUCCESS) NBADPERM=-1;
00917
00918 tuningint[i][0] = drms_getkey_int(rec[i+2],HCMNB,&status);
00919 if(status != DRMS_SUCCESS)
00920 {
00921 printf("Error: unable to read the keyword %s\n",HCMNB);
00922 exit(EXIT_FAILURE);
00923 } else printf(" %d ",tuningint[i][0]);
00924 tuningint[i][1] = drms_getkey_int(rec[i+2],HCMWB,&status);
00925 if(status != DRMS_SUCCESS)
00926 {
00927 printf("Error: unable to read the keyword %s\n",HCMWB);
00928 exit(EXIT_FAILURE);
00929 } else printf(" %d ",tuningint[i][1]);
00930 tuningint[i][2] = drms_getkey_int(rec[i+2],HCME1,&status);
00931 if(status != DRMS_SUCCESS)
00932 {
00933 printf("Error: unable to read the keyword %s\n",HCME1);
00934 exit(EXIT_FAILURE);
00935 } else printf(" %d ",tuningint[i][2]);
00936 tuningpol[i] = drms_getkey_int(rec[i+2],HCMPOL,&status);
00937 if(status != DRMS_SUCCESS)
00938 {
00939 printf("Error: unable to read the keyword %s\n",HCMPOL);
00940 exit(EXIT_FAILURE);
00941 } else printf(" %d ",tuningpol[i]);
00942
00943
00944
00945
00946 X0[i]= 2047.5;
00947
00948
00949
00950 printf(" %f ",X0[i]);
00951
00952
00953
00954
00955 Y0[i]= 2047.5;
00956
00957
00958
00959 printf(" %f ",Y0[i]);
00960 CDELT1[i] = drms_getkey_float(rec[i+2],SCALE,&status);
00961 if(status != DRMS_SUCCESS || isnan(CDELT1[i]))
00962 {
00963 printf("Error: unable to read the keyword %s\n",SCALE);
00964 CDELT1[i]=0.5;
00965
00966 }
00967 printf(" %f ",CDELT1[i]);
00968
00969
00970
00971
00972 RSUN[i]=2047.5;
00973
00974
00975 printf(" %f ",RSUN[i]);
00976 HCFTID[i] = drms_getkey_int(rec[i+2],FOCUS,&status);
00977 if(status != DRMS_SUCCESS)
00978 {
00979 printf("Error: unable to read the keyword %s\n",FOCUS);
00980 exit(EXIT_FAILURE);
00981 } else printf(" %d ",HCFTID[i]);
00982 imcfg = drms_getkey_int(rec[i+2],HIMGCFID,&status);
00983 if(status != DRMS_SUCCESS)
00984 {
00985 printf("Error: unable to read the keyword %s\n",HIMGCFID);
00986 exit(EXIT_FAILURE);
00987 } else printf(" %d ",imcfg);
00988 VELOCITY[i]= drms_getkey_double(rec[i+2],VR,&status);
00989 if(status != DRMS_SUCCESS || isnan(VELOCITY[i]))
00990 {
00991 printf("Error: unable to read the keyword %s\n",VR);
00992 exit(EXIT_FAILURE);
00993 } printf(" %f ",VELOCITY[i]);
00994 EXPOSURE[i]= drms_getkey_double(rec[i+2],EXPTIME,&status);
00995 if(status != DRMS_SUCCESS || isnan(EXPOSURE[i]))
00996 {
00997 printf("Error: unable to read the keyword %s\n",EXPTIME);
00998 exit(EXIT_FAILURE);
00999 } printf(" %f ",EXPOSURE[i]);
01000 HCAMID[i] = drms_getkey_int(rec[i+2],pkey3,&status);
01001 if(status != DRMS_SUCCESS)
01002 {
01003 printf("Error: unable to read the keyword %s\n",pkey3);
01004 exit(EXIT_FAILURE);
01005 } printf(" %d\n ",HCAMID[i]);
01006
01007
01008 if(i > 0)
01009 {
01010 if(tuningpol[i] != tuningpol[i-1])
01011 {
01012 printf("Error: the tuning polarizer position changed during the detune sequence\n");
01013 exit(EXIT_FAILURE);
01014 }
01015 if(HCFTID[i] != HCFTID[i-1])
01016 {
01017 printf("Error: the focus block changed during the detune sequence\n");
01018 exit(EXIT_FAILURE);
01019 }
01020 }
01021 if(EXPOSURE[i] == 0.0)
01022 {
01023 printf("Error: there is a dark frame in the sequence, at index %d\n",i);
01024 exit(EXIT_FAILURE);
01025 }
01026 if(HCAMID[i] != camera)
01027 {
01028 printf("Error: the filtergram index %d should have HCAMID=%d instead of %d\n",i,camera,HCAMID[i]);
01029 exit(EXIT_FAILURE);
01030 }
01031
01032
01033 segin = drms_segment_lookupnum(rec[i+2],1);
01034 BadPixels = drms_segment_read(segin,segin->info->type,&status);
01035 if(status != DRMS_SUCCESS || BadPixels == NULL)
01036 {
01037 printf("Error: cannot read the list of bad pixels of level 1 filtergram index %d\n",i);
01038 exit(EXIT_FAILURE);
01039 }
01040
01041
01042
01043 strcpy(HMISeriesTemp,CosmicRaySeries);
01044 strcat(HMISeriesTemp,"[][");
01045 sprintf(FSNtemps,"%d",FSN);
01046 strcat(HMISeriesTemp,FSNtemps);
01047 strcat(HMISeriesTemp,"]");
01048 rectemp=NULL;
01049 rectemp=drms_open_records(drms_env,HMISeriesTemp,&status);
01050
01051 if(status == DRMS_SUCCESS && rectemp != NULL && rectemp->n != 0)
01052 {
01053 segin = drms_segment_lookupnum(rectemp->records[0],0);
01054 CosmicRays = NULL;
01055 CosmicRays = drms_segment_read(segin,segin->info->type,&status);
01056 if(status != DRMS_SUCCESS || CosmicRays == NULL)
01057 {
01058 printf("Error: the list of cosmic-ray hits could not be read for FSN %d\n",FSN);
01059 CosmicRays=NULL;
01060 }
01061 }
01062 else
01063 {
01064 printf("Unable to open the series %s for FSN %d\n",HMISeriesTemp,FSN);
01065 CosmicRays=NULL;
01066 }
01067
01068
01069 status = MaskCreation(Mask,nx,ny,BadPixels,imcfg,arrinL,CosmicRays,NBADPERM);
01070 if(status != DRMS_SUCCESS)
01071 {
01072 printf("Error: unable to create a mask for the gap filling function\n");
01073 exit(EXIT_FAILURE);
01074 }
01075
01076
01077 ierror = Ierror->data;
01078 status = do_gapfill(arrinL,Mask,&const_param,ierror,axisout22[0],axisout22[1]);
01079 if(status != 0)
01080 {
01081 printf("Error: gapfilling failed at index %d\n",i);
01082 exit(EXIT_FAILURE);
01083 }
01084
01085
01086 fresize(&fresizes,arrinL,tempframe,nx,ny,nx,nx2,ny2,nx2,0,0,0.0f);
01087
01088
01089 for(iii=0;iii<ny2;iii++) for(jjj=0;jjj<nx2;++jjj) Inten[iii][jjj][i] = (double)tempframe[jjj+iii*nx2];
01090
01091 }
01092
01093
01094 drms_free_array(arrin);
01095 drms_free_array(BadPixels);
01096 drms_free_array(Ierror);
01097 free_interpol(&const_param);
01098 free(Mask);
01099 if(rectemp != NULL) drms_close_records(rectemp,DRMS_FREE_RECORD);
01100
01101
01102
01103 DRMS_Array_t *arrout = NULL;
01104 int axisout[3] = {5,nx2,ny2};
01105 type = DRMS_TYPE_FLOAT;
01106 arrout = drms_array_create(type,3,axisout,NULL,&status);
01107 if(status != DRMS_SUCCESS || arrout == NULL)
01108 {
01109 printf("Error: cannot create array arrout\n");
01110 exit(EXIT_FAILURE);
01111 }
01112
01113 DRMS_Array_t *arrout2 = NULL;
01114 int axisout2[3] = {nseq,nx2,ny2};
01115 type = DRMS_TYPE_DOUBLE;
01116 arrout2 = drms_array_create(type,3,axisout2,NULL,&status);
01117 if(status != DRMS_SUCCESS || arrout2 == NULL)
01118 {
01119 printf("Error: cannot create array arrout\n");
01120 exit(EXIT_FAILURE);
01121 }
01122
01123 DRMS_Array_t *arrout3 = NULL;
01124 int axisout3[2] = {nx2,ny2};
01125 type = DRMS_TYPE_SHORT;
01126 arrout3 = drms_array_create(type,2,axisout3,NULL,&status);
01127 if(status != DRMS_SUCCESS || arrout3 == NULL)
01128 {
01129 printf("Error: cannot create array arrout\n");
01130 exit(EXIT_FAILURE);
01131 }
01132
01133 float *Phig = arrout->data;
01134 double *IntenR = arrout2->data;
01135 short *quality= arrout3->data;
01136 memset(arrout->data,0.0,drms_array_size(arrout));
01137 memset(arrout2->data,0.0,drms_array_size(arrout2));
01138 memset(arrout3->data,0,drms_array_size(arrout3));
01139
01140
01141
01142 keyvalue = drms_getkey_int(rec2[0],keyname,&status);
01143 if(status != DRMS_SUCCESS)
01144 {
01145 printf("Error: unable to read keyword %s of the first filtergram\n",keyname);
01146 exit(EXIT_FAILURE);
01147 }
01148 interntime = drms_getkey_time(rec2[0],keyname2,&status);
01149 if(status != DRMS_SUCCESS)
01150 {
01151 printf("Error: unable to read keyword %s of the first filtergram\n",keyname2);
01152 exit(EXIT_FAILURE);
01153 }
01154 velocity0 = drms_getkey_double(rec2[0],VR,&status);
01155 if(status != DRMS_SUCCESS || isnan(velocity0))
01156 {
01157 printf("Error: unable to read the keyword %s of the first filtergram\n",VR);
01158 exit(EXIT_FAILURE);
01159 }
01160 keyvalue2 = drms_getkey_int(rec2[1],keyname,&status);
01161 if(status != DRMS_SUCCESS)
01162 {
01163 printf("Error: unable to read keyword %s of the last filtergram\n",keyname);
01164 exit(EXIT_FAILURE);
01165 }
01166 interntime2= drms_getkey_time(rec2[1],keyname2,&status);
01167 if(status != DRMS_SUCCESS)
01168 {
01169 printf("Error: unable to read keyword %s of the last filtergram\n",keyname2);
01170 exit(EXIT_FAILURE);
01171 }
01172 velocity1 = drms_getkey_double(rec2[1],VR,&status);
01173 if(status != DRMS_SUCCESS || isnan(velocity1))
01174 {
01175 printf("Error: unable to read the keyword %s of the last filtergram\n",VR);
01176 exit(EXIT_FAILURE);
01177 }
01178 OBS_VR = drms_getkey_double(rec2[0],VR,&status);
01179 OBS_VR += drms_getkey_double(rec2[1],VR,&status);
01180 OBS_VR /= 2.0;
01181 CROTA2 = drms_getkey_double(rec2[0], "CROTA2", &status);
01182 CROTA2 += drms_getkey_double(rec2[1], "CROTA2", &status);
01183 CROTA2 /= 2.0;
01184
01185 printf("FSN value of first filtergram %d\n",keyvalue);
01186 printf("FSN value of last filtergram %d\n",keyvalue2);
01187
01188
01189
01190 for(i=0;i<nseq;++i)
01191 {
01192
01193 tuning[i][0] = (double)((tuningint[i][0] * 6) % 360);
01194 if(tuning[i][0] != 0.0 && tuning[i][0] != 120.0 && tuning[i][0] != 240.0 && tuning[i][0] != -120.0 && tuning[i][0] != -240.0)
01195 {
01196 printf("Error: detune sequence should have phases of 0, 120, or 240 degrees only: %f\n",tuning[i][0]);
01197 exit(EXIT_FAILURE);
01198 }
01199 tuning[i][0] *=M_PI/180.0;
01200
01201
01202 tuning[i][1] = (double)((tuningint[i][1] * 6) % 360);
01203 if(tuning[i][1] != 0.0 && tuning[i][1] != 120.0 && tuning[i][1] != 240.0 && tuning[i][1] != -120.0 && tuning[i][1] != -240.0)
01204 {
01205 printf("Error: detune sequence should have phases of 0, 120, or 240 degrees only: %f\n",tuning[i][1]);
01206 exit(EXIT_FAILURE);
01207 }
01208 tuning[i][1] *=M_PI/180.0;
01209
01210
01211 tuning[i][2] = (double)((tuningint[i][2] *(-6)) % 360);
01212 if(tuning[i][2] != 0.0 && tuning[i][2] != 120.0 && tuning[i][2] != 240.0 && tuning[i][2] != -120.0 && tuning[i][2] != -240.0)
01213 {
01214 printf("Error: detune sequence should have phases of 0, 120, or 240 degrees only: %f\n",tuning[i][2]);
01215 exit(EXIT_FAILURE);
01216 }
01217 tuning[i][2] *=M_PI/180.0;
01218 }
01219
01220
01221
01222 solarradiusmax = 10000.;
01223 indexref=0;
01224 for(k=0;k<nseq;k++) if(RSUN[k] < solarradiusmax)
01225 {
01226 solarradiusmax = RSUN[k];
01227 indexref=k;
01228 }
01229 solarradiusmax -= ratio;
01230
01231 printf("Maximum radius (in pixels) at which phases are computed %f\n",solarradiusmax);
01232
01233
01234
01235 printf("Pixel position of the image center used to compute the phase maps: %f %f\n",X0[indexref],Y0[indexref]);
01236
01237 for(iii=0;iii<ny*nx;iii++)
01238 {
01239 row = iii / nx ;
01240 column = iii % nx ;
01241 distance2[iii]=sqrt( ((float)row-Y0[indexref])*((float)row-Y0[indexref])+((float)column-X0[indexref])*((float)column-X0[indexref]) );
01242 }
01243 fresize(&fresizes,distance2,distance,nx,ny,nx,nx2,ny2,nx2,0,0,0.0f);
01244 free_fresize(&fresizes);
01245
01246
01247
01248
01249
01250
01251
01252 printf("Reading the front window and blocker filter transmission profiles\n");
01253 for(i=0;i<nfront;++i)
01254 {
01255 wavelengthd[i]=wavelengthd[i]*10.0-lam0;
01256 frontwindowd[i]=frontwindowd[i]/100.0;
01257 }
01258 for(i=0;i<nblocker;++i)
01259 {
01260 wavelengthbd[i]=wavelengthbd[i]+(double)centerblocker2-lam0;
01261 blockerd[i]=blockerd[i]/100.0;
01262 }
01263
01264 lininterp1f(frontwindowint,wavelengthd,frontwindowd,lam,ydefault,nfront,nlam);
01265 lininterp1f(blockerint,wavelengthbd,blockerd,lam,ydefault,nblocker,nlam);
01266 for(i=0;i<nlam;++i) blockerint[i]=blockerint[i]*frontwindowint[i];
01267
01268 for(i=0;i<7;++i) FSR[i] = dpi/FSR[i];
01269
01270
01271
01272
01273
01274
01275
01276
01277 velocity0= (velocity0+velocity1)/2.0;
01278 lam0 = dlamdv*velocity0;
01279 printf("VELOCITY = %f\n",velocity0);
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298 int nelemPHASENT=4*nx2*nx2;
01299 float phaseNT[nelemPHASENT];
01300 float contrastNT[nelemPHASENT];
01301 int nread;
01302
01303 printf("READ PHASES OF NON-TUNABLE ELEMENTS\n");
01304 if(nx2 == 256) fp = fopen(filephasesnontunable,"rb");
01305 if(nx2 == 64) fp = fopen(filephasesnontunable64,"rb");
01306 if(nx2 == 32) fp = fopen(filephasesnontunable32,"rb");
01307 if(nx2 == 128) fp = fopen(filephasesnontunable128,"rb");
01308 if(fp == NULL)
01309 {
01310 printf("CANNOT OPEN FILE OF NON-TUNABLE ELEMENT PHASES\n");
01311 exit(EXIT_FAILURE);
01312 }
01313 nread=fread(phaseNT,sizeof(float),nelemPHASENT,fp);
01314 fclose(fp);
01315 for(i=0;i<nelemPHASENT;++i) phaseNT[i] = phaseNT[i]*M_PI/180.;
01316
01317 printf("READ CONTRASTS OF NON-TUNABLE ELEMENTS\n");
01318 if(nx2 == 256) fp = fopen(filecontrastsnontunable,"rb");
01319 if(nx2 == 64) fp = fopen(filecontrastsnontunable64,"rb");
01320 if(nx2 == 32) fp = fopen(filecontrastsnontunable32,"rb");
01321 if(nx2 == 128) fp = fopen(filecontrastsnontunable128,"rb");
01322 if(fp == NULL)
01323 {
01324 printf("CANNOT OPEN FILE OF NON-TUNABLE ELEMENT CONTRASTS\n");
01325 exit(EXIT_FAILURE);
01326 }
01327 nread=fread(contrastNT,sizeof(float),nelemPHASENT,fp);
01328 fclose(fp);
01329
01330
01331
01332 int nelemCONTRASTT=3*nx2*nx2;
01333 float contrastT[nelemCONTRASTT];
01334 printf("READ CONTRASTS OF TUNABLE ELEMENTS\n");
01335 if(nx2 == 256) fp = fopen(filecontraststunable,"rb");
01336 if(nx2 == 64) fp = fopen(filecontraststunable64,"rb");
01337 if(nx2 == 32) fp = fopen(filecontraststunable32,"rb");
01338 if(nx2 == 128) fp = fopen(filecontraststunable128,"rb");
01339 if(fp == NULL)
01340 {
01341 printf("CANNOT OPEN FILE OF NON-TUNABLE ELEMENT PHASES\n");
01342 exit(EXIT_FAILURE);
01343 }
01344 nread=fread(contrastT,sizeof(float),nelemCONTRASTT,fp);
01345 fclose(fp);
01346
01347 gsl_vector *Residual = NULL;
01348 gsl_matrix *Jac = NULL;
01349 gsl_matrix *Weights = NULL;
01350 gsl_matrix *VV = NULL;
01351 gsl_vector *SS = NULL;
01352 gsl_vector *work = NULL;
01353 gsl_vector *tempvec = NULL;
01354 gsl_vector *tempvec2 = NULL;
01355
01356 int location,location1[8];
01357 double phaseE2,phaseE3,phaseE4,phaseE5,contrasts[7];
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368 printf("START LOOP\n");
01369 printf("NB: IF THERE ARE MANY NON CONVERGENCE, REMEMBER TO CHECK THE VALUE OF thresh IN HMIparam.h\n");
01370 #pragma omp parallel default(none) shared(calibration,phaseguess,solarradiusmax,factor,Inten,lam0,axisout,axisout2,depth0,thresh,width0,nx2,ny2,FSR,dpi,lam,tuning,dlam,Phig,distance,phaseNT,contrastNT,contrastT,blockerint,IntenR,quality,iterstop,shiftw,Icg,fdepthg,fwidthg,a) private(location,profilef,residual,Residual,i,j,iii,jjj,k,converg,compteur,line,templine,dlinedI0,dlinedw,tempval,profileg,dIntendI0,dIntendw,dIntendIc,dIntendPhi0,dIntendPhi1,dIntendPhi2,tempres,history,err,minimum,Jac,Weights,VV,SS,work,tempvec,tempvec2,jose,phaseE2,phaseE3,phaseE4,phaseE5,contrasts,l,linea,lineb,linec,lined,gaussian,dlinedIc)
01371 {
01372
01373 Residual = gsl_vector_alloc(nseq);
01374 Jac = gsl_matrix_alloc(nseq,nparam);
01375 Weights = gsl_matrix_alloc(nparam,nparam);
01376 VV = gsl_matrix_alloc(nparam,nparam);
01377 SS = gsl_vector_alloc(nparam);
01378 work = gsl_vector_alloc(nparam);
01379 tempvec = gsl_vector_alloc(nparam);
01380 tempvec2 = gsl_vector_alloc(nparam);
01381
01382 #pragma omp for
01383 for(jjj=0;jjj<ny2;jjj++)
01384 {
01385 printf("%d/%d\n",jjj,ny2);
01386
01387 for(iii=0;iii<nx2;iii++)
01388 {
01389
01390 if(distance[jjj*nx2+iii] <= solarradiusmax)
01391 {
01392
01393
01394 converg = 0;
01395 compteur= 0;
01396
01397 Icg[jjj][iii] = thresh;
01398 fdepthg[jjj][iii] = depth0*thresh;
01399 fwidthg[jjj][iii] = width0;
01400
01401 Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]] = (float)((double)phaseguess[0]*M_PI/180./FSR[0]);
01402 Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]] = (float)((double)phaseguess[1]*M_PI/180./FSR[1]);
01403 Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]] = (float)((double)phaseguess[2]*M_PI/180./FSR[2]);
01404
01405
01406
01407 location =iii+jjj*nx2;
01408 contrasts[0]=(double)contrastT[location];
01409 contrasts[1]=(double)contrastT[location+nx2*ny2];
01410 contrasts[2]=(double)contrastT[location+2*nx2*ny2];
01411 contrasts[3]=(double)contrastNT[location];
01412 contrasts[4]=(double)contrastNT[location+nx2*ny2];
01413 contrasts[5]=(double)contrastNT[location+2*nx2*ny2];
01414 contrasts[6]=(double)contrastNT[location+3*nx2*ny2];
01415 phaseE2 =(double)phaseNT[location];
01416 phaseE3 =(double)phaseNT[location+nx2*ny2];
01417 phaseE4 =(double)phaseNT[location+2*nx2*ny2];
01418 phaseE5 =(double)phaseNT[location+3*nx2*ny2];
01419
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444 phaseE4+=0.4;
01445 phaseE5-=1.1;
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455 for(j=0;j<nlam;++j) profilef[j]=blockerint[j]*(1.+contrasts[3]*cos(FSR[3]*lam[j]+phaseE2))/2.*(1.+contrasts[4]*cos(FSR[4]*lam[j]+phaseE3))/2.*(1.+contrasts[5]*cos(FSR[5]*lam[j]+phaseE4))/2.*(1.+contrasts[6]*cos(FSR[6]*lam[j]+phaseE5))/2.;
01456
01457
01458 while(converg == 0)
01459 {
01460 for (k=0;k<nlam;k++)
01461 {
01462
01463 if(calibration == 0) gaussian[k]= 0.015*exp(-(lam[k]-lam0+0.225)*(lam[k]-lam0+0.225)/0.2/0.2)-0.004*exp(-(lam[k]-lam0-0.150)*(lam[k]-lam0-0.150)/0.22/0.22);
01464 if(calibration == 1) gaussian[k]= -0.010*exp(-(lam[k]-lam0+0.225)*(lam[k]-lam0+0.225)/0.2/0.2)-0.015*exp(-(lam[k]-lam0-0.100)*(lam[k]-lam0-0.100)/0.25/0.25);
01465 if(calibration == 2) gaussian[k]= -0.0074*exp(-(lam[k]-lam0+0.200)*(lam[k]-lam0+0.200)/0.13/0.13)-0.021*exp(-(lam[k]-lam0-0.05)*(lam[k]-lam0-0.05)/0.18/0.18);
01466
01467 dlinedIc[k]= 1.0-gaussian[k];
01468 l=(lam[k]-lam0)/fwidthg[jjj][iii];
01469
01470 if(fabs(l) > 26.5)
01471 {
01472 line[k]=Icg[jjj][iii]-gaussian[k]*Icg[jjj][iii];
01473 dlinedw[k] =0.0;
01474 dlinedI0[k] =0.0;
01475 }
01476 else
01477 {
01478 line[k] = Icg[jjj][iii]-fdepthg[jjj][iii]*exp(-l*l)*(1.0-a*2.0/sqrt(M_PI)*(1.0/2.0/l/l)*((4.0*l*l+3.0)*(l*l+1.0)*exp(-l*l)-1.0/l/l*(2.0*l*l+3.0)*sinh(l*l)))-gaussian[k]*Icg[jjj][iii];
01479
01480 l=(lam[k]-lam0)/(fwidthg[jjj][iii]+0.0001);
01481 linea[k]= Icg[jjj][iii]-fdepthg[jjj][iii]*exp(-l*l)*(1.0-a*2.0/sqrt(M_PI)*(1.0/2.0/l/l)*((4.0*l*l+3.0)*(l*l+1.0)*exp(-l*l)-1.0/l/l*(2.0*l*l+3.0)*sinh(l*l)))-gaussian[k]*Icg[jjj][iii];
01482 l=(lam[k]-lam0)/(fwidthg[jjj][iii]-0.0001);
01483 lineb[k]= Icg[jjj][iii]-fdepthg[jjj][iii]*exp(-l*l)*(1.0-a*2.0/sqrt(M_PI)*(1.0/2.0/l/l)*((4.0*l*l+3.0)*(l*l+1.0)*exp(-l*l)-1.0/l/l*(2.0*l*l+3.0)*sinh(l*l)))-gaussian[k]*Icg[jjj][iii];
01484 dlinedw[k] = (linea[k]-lineb[k])/(2.0*0.0001);
01485
01486
01487 l=(lam[k]-lam0)/fwidthg[jjj][iii];
01488 linec[k]=Icg[jjj][iii]-(fdepthg[jjj][iii]+0.001)*exp(-l*l)*(1.0-a*2.0/sqrt(M_PI)*(1.0/2.0/l/l)*((4.0*l*l+3.0)*(l*l+1.0)*exp(-l*l)-1.0/l/l*(2.0*l*l+3.0)*sinh(l*l)))-gaussian[k]*Icg[jjj][iii];
01489 lined[k]=Icg[jjj][iii]-(fdepthg[jjj][iii]-0.001)*exp(-l*l)*(1.0-a*2.0/sqrt(M_PI)*(1.0/2.0/l/l)*((4.0*l*l+3.0)*(l*l+1.0)*exp(-l*l)-1.0/l/l*(2.0*l*l+3.0)*sinh(l*l)))-gaussian[k]*Icg[jjj][iii];
01490 dlinedI0[k] = (linec[k]-lined[k])/(2.0*0.001);
01491 }
01492 }
01493
01494 for(j=0;j<nseq;j++)
01495 {
01496 residual = 0.0;
01497 dIntendI0[j] = 0.0;
01498 dIntendw [j] = 0.0;
01499 dIntendIc[j] = 0.0;
01500 dIntendPhi0[j]= 0.0;
01501 dIntendPhi1[j]= 0.0;
01502 dIntendPhi2[j]= 0.0;
01503
01504 for (k=0;k<nlam;k++)
01505 {
01506 profileg[k] = 0.125*profilef[k]*(1.0+contrasts[0]*cos(FSR[0]*(lam[k]+(double)Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][0]))*(1.0+contrasts[1]*cos(FSR[1]*(lam[k]+(double)Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][1]))*(1.0+contrasts[2]*cos(FSR[2]*(lam[k]+(double)Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][2]));
01507 residual += line[k]*profileg[k]*dlam;
01508 dIntendI0[j] += (dlinedI0[k]*profileg[k])*dlam;
01509 dIntendw [j] += (dlinedw[k] *profileg[k])*dlam;
01510 dIntendIc[j] += (dlinedIc[k]*profileg[k])*dlam;
01511 dIntendPhi0[j]+= (line[k]*(-contrasts[0]*FSR[0]*sin(FSR[0]*(lam[k]+(double)Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][0]))*(1.0+contrasts[1]*cos(FSR[1]*(lam[k]+(double)Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][1]))*(1.0+contrasts[2]*cos(FSR[2]*(lam[k]+(double)Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][2]))/8.0*profilef[k])*dlam;
01512 dIntendPhi1[j]+= (line[k]*(-contrasts[1]*FSR[1]*sin(FSR[1]*(lam[k]+(double)Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][1]))*(1.0+contrasts[0]*cos(FSR[0]*(lam[k]+(double)Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][0]))*(1.0+contrasts[2]*cos(FSR[2]*(lam[k]+(double)Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][2]))/8.0*profilef[k])*dlam;
01513 dIntendPhi2[j]+= (line[k]*(-contrasts[2]*FSR[2]*sin(FSR[2]*(lam[k]+(double)Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][2]))*(1.0+contrasts[0]*cos(FSR[0]*(lam[k]+(double)Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][0]))*(1.0+contrasts[1]*cos(FSR[1]*(lam[k]+(double)Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][1]))/8.0*profilef[k])*dlam;
01514 }
01515 jose=Inten[jjj][iii][j]/factor-residual;
01516
01517 gsl_vector_set(Residual,j,jose);
01518 gsl_matrix_set(Jac,(size_t)j,0,dIntendI0[j]);
01519 gsl_matrix_set(Jac,(size_t)j,1,dIntendIc[j]);
01520 gsl_matrix_set(Jac,(size_t)j,2,dIntendw[j]);
01521 gsl_matrix_set(Jac,(size_t)j,3,dIntendPhi0[j]);
01522 gsl_matrix_set(Jac,(size_t)j,4,dIntendPhi1[j]);
01523 gsl_matrix_set(Jac,(size_t)j,5,dIntendPhi2[j]);
01524
01525 }
01526
01527
01528
01529 gsl_linalg_SV_decomp(Jac,VV,SS,work);
01530 gsl_matrix_set_zero(Weights);
01531 for(i=0;i<nparam;i++) gsl_matrix_set(Weights,(size_t)i,(size_t)i,1.0/gsl_vector_get(SS,(size_t)i));
01532 gsl_blas_dgemv(CblasTrans ,1.0,Jac,Residual ,0.0,tempvec );
01533 gsl_blas_dgemv(CblasNoTrans,1.0,Weights,tempvec,0.0,tempvec2);
01534 gsl_blas_dgemv(CblasNoTrans,1.0,VV,tempvec2 ,0.0,tempvec );
01535
01536
01537 tempres[0] = fabs(gsl_vector_get(tempvec,0)/fdepthg[jjj][iii]);
01538 tempres[1] = fabs(gsl_vector_get(tempvec,1)/Icg[jjj][iii]);
01539 tempres[2] = fabs(gsl_vector_get(tempvec,2)/fwidthg[jjj][iii]);
01540 tempres[3] = fabs(gsl_vector_get(tempvec,3)/(double)Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]]);
01541 tempres[4] = fabs(gsl_vector_get(tempvec,4)/(double)Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]]);
01542 tempres[5] = fabs(gsl_vector_get(tempvec,5)/(double)Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]]);
01543
01544 err[compteur]= fmax(tempres[0],fmax(tempres[1],fmax(tempres[2],fmax(tempres[3],fmax(tempres[4],tempres[5])))));
01545
01546 if( (compteur == maxsteps-1) && (err[compteur] > iterstop) ) converg = 2;
01547 if(err[compteur] <= iterstop) converg = 1;
01548
01549 if(converg == 2)
01550 {
01551 gsl_sort_smallest_index(&minimum,1,err,1,maxsteps);
01552 fdepthg[jjj][iii] = history[minimum][0];
01553 Icg[jjj][iii] = history[minimum][1];
01554 fwidthg[jjj][iii] = history[minimum][2];
01555 Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]] = (float)history[minimum][3];
01556 Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]] = (float)history[minimum][4];
01557 Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]] = (float)history[minimum][5];
01558 quality[location]=1;
01559 }
01560
01561 if(converg != 2)
01562 {
01563 fdepthg[jjj][iii] += gsl_vector_get(tempvec,0);
01564 if( (fdepthg[jjj][iii] < (0.2*thresh*depth0)) || (fdepthg[jjj][iii] > (3.0*depth0*thresh)) || isnan(fdepthg[jjj][iii])) fdepthg[jjj][iii] = depth0*thresh;
01565 Icg[jjj][iii] += gsl_vector_get(tempvec,1);
01566 if( (Icg[jjj][iii] < (0.2*thresh)) || (Icg[jjj][iii] > (3.0*thresh)) || isnan(Icg[jjj][iii])) Icg[jjj][iii] = thresh;
01567 fwidthg[jjj][iii] += gsl_vector_get(tempvec,2);
01568 if( (fwidthg[jjj][iii] > (5.0*width0)) || (fwidthg[jjj][iii] < (0.3*width0)) || isnan(fwidthg[jjj][iii])) fwidthg[jjj][iii] = width0;
01569 Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]] += (float)gsl_vector_get(tempvec,3);
01570 if( fabsf(Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]]) > (1.2*M_PI/FSR[0]) ) Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]] = 0.;
01571 Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]] += (float)gsl_vector_get(tempvec,4);
01572 if( fabsf(Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]]) > (1.2*M_PI/FSR[1]) ) Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]] = 0.;
01573 Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]] += (float)gsl_vector_get(tempvec,5);
01574 if( fabsf(Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]]) > (1.2*M_PI/FSR[2]) ) Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]] = 0.;
01575 }
01576
01577 history[compteur][0]=fdepthg[jjj][iii];
01578 history[compteur][1]=Icg[jjj][iii];
01579 history[compteur][2]=fwidthg[jjj][iii];
01580 history[compteur][3]=(double)Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]];
01581 history[compteur][4]=(double)Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]];
01582 history[compteur][5]=(double)Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]];
01583
01584 compteur += 1;
01585
01586 if(converg == 2) printf("no convergence\n");
01587 }
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597 for(k=0;k<nlam;++k)
01598 {
01599 l=(lam[k]-lam0)/fwidthg[jjj][iii];
01600
01601 if(fabs(l) <= 26.5)
01602 {
01603 line[k] = Icg[jjj][iii]-fdepthg[jjj][iii]*exp(-l*l)*(1.0-a*2.0/sqrt(M_PI)*(1.0/2.0/l/l)*((4.0*l*l+3.0)*(l*l+1.0)*exp(-l*l)-1.0/l/l*(2.0*l*l+3.0)*sinh(l*l)))-gaussian[k]*Icg[jjj][iii];
01604 }
01605 else line[k]=Icg[jjj][iii]-gaussian[k]*Icg[jjj][iii];
01606 }
01607 for(j=0;j<nseq;j++)
01608 {
01609 residual = 0.0;
01610 for(k=0;k<nlam;++k)
01611 {
01612 profileg[k]=profilef[k]*0.125*(1.0+contrasts[0]*cos(FSR[0]*(lam[k]+(double)Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][0]))*(1.0+contrasts[1]*cos(FSR[1]*(lam[k]+(double)Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][1]))*(1.0+contrasts[2]*cos(FSR[2]*(lam[k]+(double)Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]])+tuning[j][2]));
01613 residual += line[k]*profileg[k]*dlam;
01614 }
01615 IntenR[j+iii*axisout2[0]+jjj*axisout2[0]*axisout2[1]] = residual;
01616 }
01617
01618 Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=(float)((double)Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]]*(FSR[0]*180.0/M_PI));
01619 Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=(float)((double)Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]]*(FSR[1]*180.0/M_PI));
01620 Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=(float)((double)Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]]*(FSR[2]*180.0/M_PI));
01621 Phig[3+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=fwidthg[jjj][iii];
01622 Phig[4+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=fdepthg[jjj][iii]/Icg[jjj][iii];
01623 }
01624 else
01625 {
01626 Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=0.0;
01627 Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=0.0;
01628 Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=0.0;
01629 Icg[jjj][iii]=0.0;
01630 fwidthg[jjj][iii]=0.0;
01631 fdepthg[jjj][iii]=0.0;
01632 Phig[3+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=0.0;
01633 Phig[4+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=0.0;
01634 }
01635
01636
01637 }
01638
01639 }
01640
01641
01642 gsl_vector_free(Residual);
01643 gsl_matrix_free(Jac);
01644 gsl_matrix_free(VV);
01645 gsl_vector_free(SS);
01646 gsl_vector_free(work);
01647 gsl_vector_free(tempvec);
01648 gsl_vector_free(tempvec2);
01649 gsl_matrix_free(Weights);
01650 }
01651
01652
01653 Phig[0+89*axisout[0]+117*axisout[0]*axisout[1]]=(Phig[0+89*axisout[0]+116*axisout[0]*axisout[1]]+Phig[0+89*axisout[0]+118*axisout[0]*axisout[1]])/2.0;
01654 Phig[0+90*axisout[0]+117*axisout[0]*axisout[1]]=(Phig[0+90*axisout[0]+116*axisout[0]*axisout[1]]+Phig[0+90*axisout[0]+118*axisout[0]*axisout[1]])/2.0;
01655 Phig[1+89*axisout[0]+117*axisout[0]*axisout[1]]=(Phig[1+89*axisout[0]+116*axisout[0]*axisout[1]]+Phig[1+89*axisout[0]+118*axisout[0]*axisout[1]])/2.0;
01656 Phig[1+90*axisout[0]+117*axisout[0]*axisout[1]]=(Phig[1+90*axisout[0]+116*axisout[0]*axisout[1]]+Phig[1+90*axisout[0]+118*axisout[0]*axisout[1]])/2.0;
01657 Phig[2+89*axisout[0]+117*axisout[0]*axisout[1]]=(Phig[2+89*axisout[0]+116*axisout[0]*axisout[1]]+Phig[2+89*axisout[0]+118*axisout[0]*axisout[1]])/2.0;
01658 Phig[2+90*axisout[0]+117*axisout[0]*axisout[1]]=(Phig[2+90*axisout[0]+116*axisout[0]*axisout[1]]+Phig[2+90*axisout[0]+118*axisout[0]*axisout[1]])/2.0;
01659 Phig[3+89*axisout[0]+117*axisout[0]*axisout[1]]=(Phig[3+89*axisout[0]+116*axisout[0]*axisout[1]]+Phig[3+89*axisout[0]+118*axisout[0]*axisout[1]])/2.0;
01660 Phig[3+90*axisout[0]+117*axisout[0]*axisout[1]]=(Phig[3+90*axisout[0]+116*axisout[0]*axisout[1]]+Phig[3+90*axisout[0]+118*axisout[0]*axisout[1]])/2.0;
01661 Phig[4+89*axisout[0]+117*axisout[0]*axisout[1]]=(Phig[4+89*axisout[0]+116*axisout[0]*axisout[1]]+Phig[4+89*axisout[0]+118*axisout[0]*axisout[1]])/2.0;
01662 Phig[4+90*axisout[0]+117*axisout[0]*axisout[1]]=(Phig[4+90*axisout[0]+116*axisout[0]*axisout[1]]+Phig[4+90*axisout[0]+118*axisout[0]*axisout[1]])/2.0;
01663
01664
01665
01666
01667
01668
01669
01670 double meanInten[nseq], meanIntenR[nseq];
01671 double NBphase=0.0, WBphase=0.0, E1phase=0.0;
01672 double meanIcg=0.0,meanwidthg=0.0,meandepthg=0.0;
01673 compteur = 0;
01674
01675 for(j=0;j<nseq;++j)
01676 {
01677 meanInten[j] =0.0;
01678 meanIntenR[j]=0.0;
01679 }
01680
01681 for(jjj=0;jjj<ny2;jjj++)
01682 {
01683 for(iii=0;iii<nx2;iii++)
01684 {
01685
01686 if(distance[jjj*nx2+iii] <= (900.0/CDELT1[indexref]) && quality[iii+jjj*nx2] == 0)
01687 {
01688 NBphase += Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]];
01689 WBphase += Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]];
01690 E1phase += Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]];
01691 meanIcg += Icg[jjj][iii];
01692 meanwidthg += fwidthg[jjj][iii];
01693 meandepthg += fdepthg[jjj][iii];
01694 compteur+= 1;
01695 for(j=0;j<nseq;++j)
01696 {
01697 meanInten[j] +=Inten[jjj][iii][j]/factor;
01698 meanIntenR[j]+=IntenR[j+iii*axisout2[0]+jjj*axisout2[0]*axisout2[1]];
01699 }
01700 }
01701 }
01702 }
01703
01704 NBphase = NBphase/(double)compteur;
01705 WBphase = WBphase/(double)compteur;
01706 E1phase = E1phase/(double)compteur;
01707 meanIcg = meanIcg/(double)compteur;
01708 meanwidthg = meanwidthg/(double)compteur;
01709 meandepthg = meandepthg/(double)compteur;
01710
01711
01712 drms_free_array(arrout2);
01713
01714
01715
01716
01717
01718
01719
01720 printf("CORRECTION OF PIXELS WHERE THERE WAS NO CONVERGENCE, USING NEIGHBOR AVERAGING\n");
01721 for(jjj=0;jjj<ny2;jjj++)
01722 {
01723 for(iii=0;iii<nx2;iii++)
01724 {
01725 location=iii+jjj*nx2;
01726 if(quality[location] ==1)
01727 {
01728
01729 location1[0]=(jjj+1)*nx2+iii;
01730 location1[1]=(jjj+1)*nx2+(iii+1);
01731 location1[2]=jjj*nx2+(iii+1);
01732 location1[3]=(jjj-1)*nx2+(iii+1);
01733 location1[4]=(jjj-1)*nx2+iii;
01734 location1[5]=(jjj-1)*nx2+(iii-1);
01735 location1[6]=jjj*nx2+(iii-1);
01736 location1[7]=(jjj+1)*nx2+(iii-1);
01737 compteur=0;
01738 Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=0.0;
01739 Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=0.0;
01740 Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=0.0;
01741 Phig[3+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=0.0;
01742 Phig[4+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=0.0;
01743 for(j=0;j<8;++j)
01744 {
01745 if(quality[location1[j]] == 0)
01746 {
01747 Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]]+=Phig[0+axisout[0]*(location1[j])];
01748 Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]]+=Phig[1+axisout[0]*(location1[j])];
01749 Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]]+=Phig[2+axisout[0]*(location1[j])];
01750 Phig[3+iii*axisout[0]+jjj*axisout[0]*axisout[1]]+=Phig[3+axisout[0]*(location1[j])];
01751 Phig[4+iii*axisout[0]+jjj*axisout[0]*axisout[1]]+=Phig[4+axisout[0]*(location1[j])];
01752 compteur+=1;
01753 }
01754 }
01755 if(compteur != 0)
01756 {
01757 Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=Phig[0+iii*axisout[0]+jjj*axisout[0]*axisout[1]]/(float)compteur;
01758 Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=Phig[1+iii*axisout[0]+jjj*axisout[0]*axisout[1]]/(float)compteur;
01759 Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=Phig[2+iii*axisout[0]+jjj*axisout[0]*axisout[1]]/(float)compteur;
01760 Phig[3+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=Phig[3+iii*axisout[0]+jjj*axisout[0]*axisout[1]]/(float)compteur;
01761 Phig[4+iii*axisout[0]+jjj*axisout[0]*axisout[1]]=Phig[4+iii*axisout[0]+jjj*axisout[0]*axisout[1]]/(float)compteur;
01762 }
01763 }
01764 }
01765 }
01766
01767
01768
01769
01770
01771 printf("RESULTS\n");
01772 printf("-----------------------------------------------------------------------------\n");
01773 printf("\n SPATIALLY AVERAGED INTENSITIES \n");
01774 for(j=0;j<nseq;++j) printf("%f %f\n",meanInten[j],meanIntenR[j]);
01775 printf("\n SPATIALLY AVERAGED PHASES (IN DEGREES)\n");
01776 printf("NB Michelson: %f\n",NBphase);
01777 printf("WB Michelson: %f\n",WBphase);
01778 printf("Lyot E1: %f\n",E1phase);
01779 printf("\n SPATIALLY AVERAGED WIDTH, DEPTH, AND CONTINUUM\n");
01780 printf("WIDTH: %f \n",meanwidthg);
01781 printf("DEPTH: %f \n",meandepthg);
01782 printf("CONTINUUM: %f \n",meanIcg);
01783 printf("\n COTUNE TABLE \n");
01784 printf("E1 WB POL NB\n");
01785 int table[4][20];
01786
01787 cotunetable(NBphase,WBphase,E1phase,table);
01788
01789 for(j = 0; j < 20; ++j)
01790 {
01791 printf("%d %d %d %d\n",table[0][j],table[1][j],table[2][j],table[3][j]);
01792 }
01793
01794
01795
01796
01797
01798 DRMS_RecordSet_t *dataout = NULL;
01799 DRMS_Record_t *recout = NULL;
01800 DRMS_Segment_t *segout = NULL;
01801
01802 dataout = drms_create_records(drms_env,1,dsout,DRMS_PERMANENT,&status);
01803
01804 if (status != DRMS_SUCCESS)
01805 {
01806 printf("Could not create a record for the phase maps\n");
01807 exit(EXIT_FAILURE);
01808 }
01809 if (status == DRMS_SUCCESS)
01810 {
01811 DRMS_Value_t val;
01812
01813 printf("Writing a record on the DRMS for the phase maps\n");
01814 recout = dataout->records[0];
01815
01816
01817
01818
01819
01820
01821
01822
01823 val.type = DRMS_TYPE_FLOAT;
01824 val.value.float_val = (float)NBphase;
01825 if (setKey(recout, KEY_PHASE_NBMICHELSON, &val))
01826 {
01827 return EXIT_FAILURE;
01828 }
01829
01830 val.value.float_val = (float)WBphase;
01831 if (setKey(recout, KEY_PHASE_WBMICHELSON, &val))
01832 {
01833 return EXIT_FAILURE;
01834 }
01835
01836 val.value.float_val = (float)E1phase;
01837 if (setKey(recout, KEY_PHASE_LYOT, &val))
01838 {
01839 return EXIT_FAILURE;
01840 }
01841
01842 val.value.float_val = (float)meanwidthg;
01843 if (setKey(recout, KEY_PHASE_LINEWIDTH, &val))
01844 {
01845 return EXIT_FAILURE;
01846 }
01847
01848 val.value.float_val = (float)meandepthg;
01849 if (setKey(recout, KEY_PHASE_LINEDEPTH, &val))
01850 {
01851 return EXIT_FAILURE;
01852 }
01853
01854 val.value.float_val = (float)meanIcg;
01855 if (setKey(recout, KEY_PHASE_LINECONTINUUM, &val))
01856 {
01857 return EXIT_FAILURE;
01858 }
01859
01860 status = drms_setkey_int(recout,pkey,keyvalue);
01861 status = drms_setkey_time(recout,pkey2,interntime);
01862 status = drms_setkey_int(recout,pkey3,camera);
01863 status = drms_setkey_int(recout,key3,keyvalue2);
01864 status = drms_setkey_time(recout,key4,interntime2);
01865 keyvalue3= (keyvalue+keyvalue2)/2;
01866 interntime3 = (interntime+interntime2)/2.0;
01867 status = drms_setkey_int(recout,key6,keyvalue3);
01868 status = drms_setkey_time(recout,key5,interntime3);
01869 status = drms_setkey_string(recout,COMMENTS,COMMENT);
01870 drms_appendcomment(recout,"Input recordspec is:",1);
01871 drms_appendcomment(recout,inRecQuery,0);
01872 status += drms_setkey_float(recout,"CROTA2",CROTA2);
01873 status += drms_setkey_float(recout,"OBS_VR",OBS_VR);
01874 status = drms_setkey_float(recout,CRPIX1,X0[indexref]+1.0);
01875 status = drms_setkey_float(recout,CRPIX2,Y0[indexref]+1.0);
01876 status = drms_setkey_float(recout,SCALE,CDELT1[indexref]);
01877 status = drms_setkey_int(recout,FOCUS,HCFTID[indexref]);
01878 status = drms_setkey_float(recout,RADIUS,solarradiusmax);
01879 status = drms_setkey_int(recout,HCMNB0,(int)round(-NBphase/6.0)+60);
01880 status = drms_setkey_int(recout,HCMWB0,(int)round(-WBphase/6.0)+60);
01881 status = drms_setkey_int(recout,HCME10,(int)round( E1phase/6.0)+60);
01882 status = drms_setkey_int(recout,HCMPOL0,0);
01883 status = drms_setkey_int(recout,NXs,nx2);
01884 char DATEOBS[256];
01885 sprint_time(DATEOBS,CURRENT_SYSTEM_TIME,"UTC",1);
01886 status = drms_setkey_string(recout,DATEs,DATEOBS);
01887 status = drms_setkey_float(recout,FSRNBs,2.0*M_PI/FSR[0]);
01888 status = drms_setkey_float(recout,FSRWBs,2.0*M_PI/FSR[1]);
01889 status = drms_setkey_float(recout,FSRE1s,2.0*M_PI/FSR[2]);
01890 status = drms_setkey_float(recout,FSRE2s,2.0*M_PI/FSR[3]);
01891 status = drms_setkey_float(recout,FSRE3s,2.0*M_PI/FSR[4]);
01892 status = drms_setkey_float(recout,FSRE4s,2.0*M_PI/FSR[5]);
01893 status = drms_setkey_float(recout,FSRE5s,2.0*M_PI/FSR[6]);
01894 status = drms_setkey_float(recout,CBLOCKERs,centerblocker2);
01895
01896
01897
01898 segout = drms_segment_lookupnum(recout, 0);
01899 drms_segment_write(segout, arrout, 0);
01900 segout = drms_segment_lookupnum(recout, 1);
01901 drms_segment_write(segout, arrout3,0);
01902
01903
01904 drms_close_records(dataout, DRMS_INSERT_RECORD);
01905 }
01906 drms_free_array(arrout);
01907 drms_free_array(arrout3);
01908 }
01909
01910 if (fnamePath)
01911 {
01912 free(fnamePath);
01913 fnamePath = NULL;
01914 }
01915
01916 return error;
01917
01918 }