00001
00002
00003
00004
00005
00050 #include <stdio.h>
00051 #include <stdlib.h>
00052 #include <math.h>
00053 #include <jsoc_main.h>
00054 #include <string.h>
00055 #include <time.h>
00056 #include <omp.h>
00057 #include <module_flatfield.h>
00058
00059 char *module_name = "module_flatfield";
00060
00061 #define kRecSetIn "input_series" //name of the series containing the input filtergrams
00062 #define kRecSetOut "cosmic_ray_series" //name of the series containing the output series for cosmic rays // optional
00063 #define kDSCosmic "cosmic_rays" //name of the cosmic ray series
00064 #define kDSFlatfield "flatfield"
00065 #define cadence_name "cadence" //cadence in sec
00066 #define fid_name "fid" //name of fid argument
00067 #define cam_name "camera" // 0 side, 1 front
00068 #define fsnf_name "fsn_first"
00069 #define fsnl_name "fsn_last"
00070 #define datumn "datum"
00071
00072
00073 #define minval(x,y) (((x) < (y)) ? (x) : (y))
00074 #define maxval(x,y) (((x) < (y)) ? (y) : (x))
00075
00076 ModuleArgs_t module_args[] = {
00077 {ARG_STRING, kRecSetIn, "", "Input data series."},
00078 {ARG_STRING, datumn, "", "datum string"},
00079 {ARG_INT, kDSCosmic, "", "Cosmic rays flag"},
00080 {ARG_INT, kDSFlatfield, "", "Flatfield flag"},
00081 {ARG_INT, cadence_name, "", "Cadence in sec"},
00082 {ARG_INT, fid_name, "", "FID"},
00083 {ARG_INT, cam_name, "", "Camera"},
00084 {ARG_INT, fsnf_name, "0"},
00085 {ARG_INT, fsnl_name, "2147483647"},
00086 {ARG_FLOAT, "rmax_crfix", "0.98",
00087 "Maximum value of r/R for cosmic ray detection"},
00088 {ARG_STRING, kRecSetOut, "default", "Cosmic ray output series."},
00089
00090 {ARG_END}
00091 };
00092
00093
00094
00098
00099
00100
00101
00102
00103
00104 long sign(double);
00105 double mat_rh(long[], double[], double[], int);
00106 void nine_diag(long[], double[], int, int, double[], long[], double);
00107 void blockiter(double[], double[], double[], double*, double[], int, int, double);
00108 void tridag(double veca[], double vecb[], double vecc[], double vecr[], double vecu[], int n);
00109 void mat_mult(double[], double[], double[], double[], double[], int);
00110 void printtime();
00111 void highpass(int, int, double, double[nx][ny]);
00112 void derotation(double, double, double, double, double, double, double [2][nx][ny]);
00113
00114 void limb_darkening(double radius, double cent_x, double cent_y, double *b, int order, double *limb_dark);
00115 int flatfield(double *rhsp, double *rhsm,short *badpix, int pairs, double *gout, double *param, struct code_param, double deltat);
00116 void apod_circ(float rad, float nb, float offx, float offy, float *vd);
00117
00118
00119 int get_flat(char *query, int camera, float *flatfield, float *offpoint,
00120 short *bad);
00121
00122 int get_latest_bad(TIME t_0, int camera, const char *fname, const char *segname, short *bad, long long *recnum);
00123
00124 int get_latest_flat(TIME t_0, int camera, const char *fname, const char *segname, float *offpoint, long long *recnum, int *focus);
00125
00126
00127
00128 int write_flatfield_fid(DRMS_Array_t *arrout_new, int camera, long long recnum_offpoint,
00129 TIME t_0, int focus,int fid,
00130 struct rotpar rot_new);
00131
00132
00133
00134
00135
00136
00137
00138 int DoIt(void)
00139 {
00140
00141 #include "module_flatfield_const.h"
00142
00144
00146
00147
00148 const char *inRecQuery = cmdparams_get_str(&cmdparams, kRecSetIn, NULL);
00149 const char *cosmic_ray_series = cmdparams_get_str(&cmdparams, kRecSetOut, NULL);
00150 if (strcmp(cosmic_ray_series, "default") == 0){cosmic_ray_series=filename_cosmic; }
00151
00152 int cosmic_flag=cmdparams_get_int(&cmdparams, kDSCosmic, NULL);
00153 int flatfield_flag=cmdparams_get_int(&cmdparams, kDSFlatfield, NULL);
00154 int cadence=cmdparams_get_int(&cmdparams, cadence_name, NULL);
00155
00156 int fid=cmdparams_get_int(&cmdparams, fid_name, NULL);
00157 int cam=cmdparams_get_int(&cmdparams, cam_name, NULL)-1;
00158 int fsn_first=cmdparams_get_int(&cmdparams, fsnf_name, NULL);
00159 int fsn_last=cmdparams_get_int(&cmdparams, fsnl_name, NULL);
00160 float crrmax = cmdparams_get_float (&cmdparams, "rmax_crfix", NULL);
00161
00162 const char *datum=cmdparams_get_str(&cmdparams, datumn, NULL);
00163
00164
00165 if (fid < minfid || fid > maxfid){printf("Not an observable FID\n"); return 1;}
00166
00167 int status, status1, status2;
00168 int status0=DRMS_SUCCESS;
00169 int stat = DRMS_SUCCESS;
00170 int status_flatfield=DRMS_SUCCESS;
00171 int status_flatfield_relative=DRMS_SUCCESS;
00172
00173
00174 DRMS_RecordSet_t *data, *data_flat, *data_last, *dataout;
00175 DRMS_Record_t *ff_last=NULL;
00176 DRMS_Array_t *arrin0;
00177 double *arrinL0;
00178 DRMS_Segment_t *segin = NULL, *segin_flat=NULL, *segout = NULL, *segin_offpoint=NULL, *segout_val=NULL, *segout_sig=NULL;
00179 DRMS_Record_t *rec_flat, *recout, *reclink_off;
00180
00181 DRMS_Type_t type_time = DRMS_TYPE_TIME;
00182 DRMS_Type_t type_int = DRMS_TYPE_INT;
00183 DRMS_Type_t type_float = DRMS_TYPE_FLOAT;
00184 DRMS_Type_t type_double = DRMS_TYPE_DOUBLE;
00185
00186
00187
00188
00189 double deltat=(double)cadence;
00190 printf("cadence %lf seconds\n", deltat);
00191
00192 TIME t_0, t_stamp;
00193 int focus,camid,fsns;
00194
00195 TIME interntime=sscan_time("2010.03.18_22:12:17.77_UTC");
00196 int axisbad[1];
00197
00198
00199 int i,j,k,km1,c,ki,iii,jjj,kkk;
00200 int signid=0;
00201 int nfr;
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 float *limw=(float *)(malloc(nx*ny*sizeof(float)));
00214
00215
00216
00217 DRMS_Array_t *arr_flat, *arr_offpoint;
00218 DRMS_Array_t *arrout_new;
00219 DRMS_Array_t *arrout, *arrout_val, *arrout_sig;
00220
00221 int *cosmic_ray_data;
00222 float *val_data, *sig_data;
00223
00224
00225
00226 short *bad;
00227 short *badpix;
00228 double *flati;
00229
00230 float *apod;
00231
00232 float *flat_off, *flat_relative;
00233 float *app_flat;
00234
00235 app_flat=(float *)(malloc(nx*ny*sizeof(float)));
00236 flat_off=(float *)(malloc(nx*ny*sizeof(float)));
00237 flat_relative=(float *)(malloc(nx*ny*sizeof(float)));
00238 badpix=(short *)(malloc(nx*ny*sizeof(short)));
00239 flati=(double *)(malloc(nx*ny*sizeof(double)));
00240
00241 apod=(float *)(malloc(nx*ny*sizeof(float)));
00242
00243 float *flatfield_new;
00244 float *fflat;
00245
00246
00247
00248
00249
00250 double flat[nx][ny];
00251 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i){flat[i][j]=0.0;}
00252
00253
00254
00255 double *rhsp, *rhsm;
00256 double *limb_dark;
00257 int *count_p, *count_m;
00258
00259 int count, ccount,ccount1,ccount2, count_filtergram;
00260 int count_flatfields;
00261
00262 int last, current, next, holder;
00263 int index_last, index_current, index_next;
00264 float wlr;
00265 float *rr, *a1,*a2, *w1,*w2,*cmc;
00266 int flatfound, flatrec_index;
00267
00268 rr=(float *)(malloc(nx*ny*sizeof(float)));
00269 a1=(float *)(malloc(nx*ny*sizeof(float)));
00270 a2=(float *)(malloc(nx*ny*sizeof(float)));
00271 w1=(float *)(malloc(nx*ny*sizeof(float)));
00272 w2=(float *)(malloc(nx*ny*sizeof(float)));
00273 cmc=(float *)(malloc(nx*ny*sizeof(float)));
00274
00275
00276 rhsp=(double *)(malloc(nx*ny*sizeof(double)));
00277 rhsm=(double *)(malloc(nx*ny*sizeof(double)));
00278
00279 count_p=(int *)(malloc(nx*ny*sizeof(int)));
00280 count_m=(int *)(malloc(nx*ny*sizeof(int)));
00281
00282 limb_dark=(double *)(malloc(nx*ny*sizeof(double)));
00283
00284
00285
00286
00287 float arrimg[3][ny][nx];
00288 int *cosmicarr;
00289 cosmicarr=(int *)(malloc(nx*ny*sizeof(int)));
00290 float *cosmicval;
00291 cosmicval=(float *)(malloc(nx*ny*sizeof(float)));
00292 float *cosmicsig;
00293 cosmicsig=(float *)(malloc(nx*ny*sizeof(float)));
00294 float diff_forward, diff_backward;
00295
00296 int fsnarr[3]={0 , 0, 0};
00297
00298 unsigned char *cmarr=(unsigned char *)(malloc(nx*ny*sizeof(unsigned char)));
00299
00300 char *query_flat, *query_flat_ref, *query_flat_relative;
00301 char flat_default[256]={"none"};
00302
00303 double *param;
00304 param=(double *)(malloc(6*sizeof(double)));
00305
00306
00307
00308 TIME tobs_link[2];
00309 long long recnum_offpoint, recnum_badpix;
00310 struct rotpar rot_cur;
00311 struct rotpar rot_new;
00312
00313 int rotpairs;
00314
00315
00316 printf("START!\n");
00317
00318
00319
00320
00321
00322 drms_series_exists(drms_env, filename_flatfield_fid, &status);
00323 if (status == DRMS_ERROR_UNKNOWNSERIES)
00324 {
00325 printf("Output series %s doesn't exist\n",filename_flatfield_fid);
00326 return 1;
00327 }
00328 if (status == DRMS_SUCCESS)
00329 {
00330 printf("Output series %s exists.\n",filename_flatfield_fid);
00331 }
00332
00333
00334
00335
00336
00337 drms_series_exists(drms_env, cosmic_ray_series, &status);
00338 if (status == DRMS_ERROR_UNKNOWNSERIES)
00339 {
00340 printf("Cosmic ray series %s doesn't exist\n",cosmic_ray_series);
00341 return 1;
00342 }
00343 if (status == DRMS_SUCCESS)
00344 {
00345 printf("Cosmic ray series %s exists.\n",cosmic_ray_series);
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355 int nthreads;
00356 nthreads=omp_get_max_threads();
00357
00358
00359 printf("Number of threads run in parallel = %d \n",nthreads);
00360
00361
00362
00363
00364
00365
00366 char timefirst[256]="";
00367 strcat(timefirst, datum);
00368 strcat(timefirst, "_00:00:00.00_TAI");
00369
00370 char timelast[256]="";
00371 strcat(timelast, datum);
00372 strcat(timelast, "_23:59:59.99_TAI");
00373
00374 int pad;
00375 pad=(int)(time_limit/filtergram_cadence+1.0);
00376
00377
00378 TIME tfirst, tlast;
00379
00380 char query0[256]="";
00381
00382 if (fsn_first == 0 || fsn_last == 2147483647)
00383 {
00384
00385 tfirst=sscan_time(timefirst);
00386 tlast=sscan_time(timelast);
00387
00388 char datefirst[256]="";
00389 sprint_ut(datefirst, tfirst-time_limit);
00390
00391 char datelast[256]="";
00392 sprint_ut(datelast, tlast+time_limit);
00393
00394
00395 strcat(query0, inRecQuery);
00396 strcat(query0, "[");
00397 strcat(query0, datefirst);
00398 strcat(query0, "-");
00399 strcat(query0, datelast);
00400 strcat(query0, "][?FID=");
00401 char ffnumb[2]={""};
00402 sprintf(ffnumb, "%5.5d", fid);
00403 strcat(query0, ffnumb);
00404 strcat(query0, "?][?CAMERA=");
00405 char fnumb[2]={""};
00406 sprintf(fnumb, "%1.1d", cam+1);
00407 strcat(query0, fnumb);
00408 strcat(query0, "?]");
00409
00410 printf("query string: %s\n", query0);
00411
00412 }
00413
00414
00415
00416 if (fsn_first != 0 && fsn_last != 2147483647)
00417 {
00418
00419 tfirst=0;
00420 tlast=3881520000.0;
00421
00422 strcat(query0, inRecQuery);
00423 strcat(query0, "[][");
00424 char fsnf[10]={""};
00425 sprintf(fsnf, "%d", fsn_first-pad);
00426 strcat(query0, fsnf);
00427 strcat(query0, "-");
00428 char fsnl[10]={""};
00429 sprintf(fsnl, "%d", fsn_last+pad);
00430 strcat(query0, fsnl);
00431 strcat(query0, "][?FID=");
00432 char ffnumb[2]={""};
00433 sprintf(ffnumb, "%5.5d", fid);
00434 strcat(query0, ffnumb);
00435 strcat(query0, "?][?CAMERA=");
00436 char fnumb[2]={""};
00437 sprintf(fnumb, "%1.1d", cam+1);
00438 strcat(query0, fnumb);
00439 strcat(query0, "?]");
00440 printf("query string: %s\n", query0);
00441 }
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453 data = drms_open_records(drms_env,query0,&stat);
00454 if (data == NULL || stat != 0 || data->n == 0){printf("can not open records\n"); return 1;}
00455
00456 drms_stage_records(data, 1, 0);
00457
00458
00459
00460
00461
00462
00463
00464 int nRecs=data->n;
00465 printf("number of records %d\n", nRecs);
00466
00467
00468
00469
00470 DRMS_Record_t *rec0[nRecs];
00471
00472
00473 int keyvalue_cam[nRecs];
00474 int keyvalue_wl[nRecs];
00475 int keyvalue_pl[nRecs];
00476 DRMS_Type_Value_t keyvalue_time;
00477 long tmind[nRecs+1];
00478 int keyvalue_fid[nRecs];
00479 char *keyvalue_iss[nRecs];
00480 char *keyvalue_flatnumb[nRecs];
00481 int keyvalue_flatr[nRecs];
00482 int keyvalue_fsn[nRecs];
00483 double time_fl[nRecs];
00484
00485
00486
00487 float keyvalue_rsun[nRecs];
00488 double keyvalue_dsun_obs[nRecs];
00489 float keyvalue_p0[nRecs];
00490 float keyvalue_b0[nRecs];
00491 float keyvalue_X0[nRecs];
00492 float keyvalue_Y0[nRecs];
00493 float keyvalue_vrad[nRecs];
00494
00495
00496
00497 int present[nRecs];
00498 int present_forward[nRecs];
00499 int present_backward[nRecs];
00500 int index[nRecs];
00501 int statarr[nRecs];
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 t_0 = drms_getkey_time(data->records[nRecs/2],keytobs,&status);
00513 fsns=drms_getkey_int(data->records[nRecs/2],keyfsn,&status);
00514 focus = drms_getkey_int(data->records[nRecs/2],keyfocus,&status);
00515
00516 char tmstr[256]="";
00517 strcat(tmstr, datum);
00518 strcat(tmstr, "_00:00:00.00_TAI");
00519
00520 t_stamp=sscan_time(tmstr)+24.0*60.0*60.0;
00521
00522
00523
00524
00525
00526 int update_stat=2;
00527
00528
00529
00530
00531
00532
00533 if (stat == DRMS_SUCCESS && data != NULL && nRecs > 0)
00534 {
00535
00536 nfr=0;
00537 for (k=0; k<nRecs; ++k)
00538 {
00539 statarr[k]=0;
00540 rec0[k]=data->records[k];
00541 keyvalue_cam[k]=drms_getkey_int(rec0[k],keycam,&status);
00542 if (status != 0 || (keyvalue_cam[k] != cam_id_front && keyvalue_cam[k] != cam_id_side)) statarr[k]=statarr[k]+1024;
00543 ++nfr;
00544 }
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561 for (k=0; k<nRecs; ++k){
00562 keyvalue_time=drms_getkey(rec0[k], keytobs, &type_time, &status);
00563 time_fl[k] = keyvalue_time.time_val;
00564 tmind[k]=(long)time_fl[k]-(long)interntime;
00565
00566
00567
00568
00569 keyvalue_fsn[k]=drms_getkey_int(rec0[k], keyfsn, &status);
00570
00571 keyvalue_wl[k]=drms_getkey_int(rec0[k],keywl,&status);
00572 keyvalue_pl[k]=drms_getkey_int(rec0[k],keypl,&status);
00573
00574
00575
00576
00577
00578 keyvalue_rsun[k]=drms_getkey_float(rec0[k], lev1_r_sun,&status);
00579 if (keyvalue_rsun[k] < rsun_min || keyvalue_rsun[k] > rsun_max || isnan(keyvalue_rsun[k]))statarr[k]=statarr[k]+32;
00580
00581 keyvalue_dsun_obs[k]=drms_getkey_double(rec0[k], lev1_dist ,&status);
00582 if (status !=0 || keyvalue_dsun_obs[k] < dsun_obs_min || keyvalue_dsun_obs[k] > dsun_obs_max || isnan(keyvalue_dsun_obs[k])) statarr[k]=statarr[k]+16;
00583
00584 keyvalue_p0[k]=drms_getkey_float(rec0[k], lev1_p0, &status);
00585 if (status !=0 || keyvalue_p0[k] < p0_min || keyvalue_p0[k] > p0_max || isnan(keyvalue_p0[k])) statarr[k]=statarr[k]+4;
00586
00587 keyvalue_b0[k]=drms_getkey_float(rec0[k], lev1_b0, &status);
00588 if (status !=0 || keyvalue_b0[k] < b0_min || keyvalue_b0[k] > b0_max || isnan(keyvalue_b0[k])) statarr[k]=statarr[k]+8;
00589
00590 keyvalue_X0[k]=drms_getkey_float(rec0[k], lev1_x0, &status1);
00591 keyvalue_Y0[k]=drms_getkey_float(rec0[k], lev1_y0,&status2);
00592 if (status1 !=0 || status2 != 0 || keyvalue_X0[k] < X0_min || keyvalue_X0[k] > X0_max || isnan(keyvalue_X0[k]) || keyvalue_Y0[k] < Y0_min || keyvalue_Y0[k] > Y0_max || isnan(keyvalue_Y0[k])) statarr[k]=statarr[k]+64;
00593
00594
00595 keyvalue_fid[k]=drms_getkey_int(rec0[k],fidkey,&status);
00596 if (status !=0 || keyvalue_fid[k] < minfid || keyvalue_fid[k] > maxfid || isnan(keyvalue_fid[k])) statarr[k]=statarr[k]+256;
00597 keyvalue_iss[k]=drms_getkey_string(rec0[k],isskey,&status);
00598 if (status !=0 || strcmp(keyvalue_iss[k], "CLOSED") != 0) statarr[k]=statarr[k]+1;
00599
00600 keyvalue_flatnumb[k]=drms_getkey_string(rec0[k],flatnkey,&status);
00601
00602
00603 keyvalue_vrad[k]=drms_getkey_float(rec0[k], lev1_vr, &status);
00604 if (keyvalue_vrad[k] < vrad_min || keyvalue_vrad[k] > vrad_max) statarr[k]=statarr[k]+512;
00605
00606
00607
00608 }
00609
00610
00611
00612
00613
00614
00615
00616 float R_SUN=0.0;
00617 float XX0=0.0;
00618 float YY0=0.0;
00619 float P_ANG=0.0;
00620 float B_ANG=0.0;
00621 float dist=0.0;
00622
00623 float dcount=0.0;
00624 for (k=0; k<nRecs; ++k){
00625
00626 if (statarr[k] == 0){
00627
00628 R_SUN=R_SUN+keyvalue_rsun[k];
00629 XX0=XX0+keyvalue_X0[k];
00630 YY0=YY0+keyvalue_Y0[k];
00631 P_ANG=P_ANG-keyvalue_p0[k]/180.0f*M_PI;
00632 B_ANG=B_ANG+keyvalue_b0[k]/180.0f*M_PI;
00633 dist=dist+(float)keyvalue_dsun_obs[k]/oneau;
00634 dcount=dcount+1.0f;
00635 }
00636 }
00637
00638
00639
00640
00641
00642 if (dcount > 0.0){
00643 R_SUN=R_SUN/dcount;
00644 XX0=XX0/dcount;
00645 YY0=YY0/dcount;
00646 P_ANG=P_ANG/dcount;
00647 B_ANG=B_ANG/dcount;
00648 dist=dist/dcount;
00649
00650 *(param+0)=(double)R_SUN;
00651 *(param+1)=(double)XX0;
00652 *(param+2)=(double)YY0;
00653 *(param+3)=(double)P_ANG;
00654 *(param+4)=(double)B_ANG;
00655 *(param+5)=(double)dist;
00656 }
00657 else
00658 {
00659 printf("Invalid lev1 keywords\n");
00660 return 1;
00661 }
00662
00663
00664 printf("disk center and radius: %f %f %f\n", XX0, YY0,R_SUN); ;
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675 if (cam ==0)camid=cam_id_side;
00676 if (cam ==1)camid=cam_id_front;
00677
00678
00679
00680 count_flatfields=0;
00681
00682 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i){rhsm[j*nx+i]=0.0; rhsp[j*nx+i]=0.0; count_p[j*nx+i]=0; count_m[j*nx+i]=0; flati[j*nx+i]=0.0; cmarr[j*nx+i]=1;}
00683
00684
00685 for (i=0; i<nRecs; ++i){present_backward[i]=0; present_forward[i]=0; present[i]=0;}
00686
00687
00688
00689
00690
00691 for (k=0; k<nRecs; ++k){present[k]=1; index[k]=k;}
00692 printf("number of filtergrams %d\n", nRecs);
00693 dataout = drms_create_records(drms_env,nRecs,(char *)cosmic_ray_series,DRMS_PERMANENT,&stat);
00694
00695
00696
00697
00698
00699 count_filtergram=nRecs;
00700
00701
00702
00703
00704 printf("filtergram with FID %d: %d\n", fid, count_filtergram);
00705 for (c=1; c<count_filtergram; ++c){
00706
00707
00708 present_backward[c]=present[c];
00709 if (statarr[c] != 0 || statarr[c-1] != 0 || keyvalue_wl[c] != keyvalue_wl[c-1] || keyvalue_pl[c] != keyvalue_pl[c-1] || (tmind[c]-tmind[c-1]) != (long)deltat || fabs(keyvalue_p0[c]- keyvalue_p0[c-1]) > 0.2 || sqrt(pow(keyvalue_X0[c]-keyvalue_X0[c-1],2)+pow(keyvalue_Y0[c]-keyvalue_Y0[c-1],2)) > limit_centerdiff[cam] || fabs(keyvalue_rsun[c]-keyvalue_rsun[c-1]) > limit_rsundiff){present_backward[c]=0;}
00710
00711 if (sqrt(pow(keyvalue_X0[c]-keyvalue_X0[c-1],2)+pow(keyvalue_Y0[c]-keyvalue_Y0[c-1],2)) > limit_centerdiff_cosmic) statarr[c]=statarr[c]+128;
00712
00713 }
00714
00715
00716
00717 ccount=count_filtergram-1;
00718 for (c=0; c<(count_filtergram-1); ++c){
00719 present_forward[c]=present[c];
00720 if (statarr[c] != 0 || statarr[c+1] != 0 || keyvalue_wl[c] != keyvalue_wl[c+1] || keyvalue_pl[c] != keyvalue_pl[c+1] || (tmind[c+1]-tmind[c]) != (long)deltat || fabs(keyvalue_p0[c+1]- keyvalue_p0[c]) > 0.2 || sqrt(pow(keyvalue_X0[c]-keyvalue_X0[c+1],2)+pow(keyvalue_Y0[c]-keyvalue_Y0[c+1],2)) > limit_centerdiff[cam] || fabs(keyvalue_rsun[c]-keyvalue_rsun[c+1]) > limit_rsundiff){present_forward[c]=0; --ccount;}
00721
00722 if (sqrt(pow(keyvalue_X0[c]-keyvalue_X0[c+1],2)+pow(keyvalue_Y0[c]-keyvalue_Y0[c+1],2)) > limit_centerdiff_cosmic || fabs(keyvalue_X0[c]-XX0) > limit_offpoint || fabs(keyvalue_Y0[c]-YY0) > limit_offpoint) statarr[c]=statarr[c]+128;
00723 }
00724
00725 printf("number of filtergrams, valid pairs of frames %d \t %d \n", count_filtergram, ccount);
00726
00727
00728
00729
00730
00731
00732
00733
00735
00736
00737 status_flatfield_relative=get_latest_flat(t_stamp, cam+1, filename_offpoint, segmentname_offpoint, flat_relative, &recnum_offpoint, &focus);
00738
00739
00740
00741
00742 if (status_flatfield_relative == 0)
00743 {
00744 printf("Relative flatfield read: record number:%ld\n", recnum_offpoint);
00745 }
00746 else
00747 {
00748 printf("WARNING: Could not read relative flatfield");
00749 }
00750
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763 #pragma omp parallel for private(jjj,iii)
00764 for (jjj=0; jjj<ny; ++jjj) for (iii=0; iii<nx; ++iii)
00765 {
00766
00767 rr[jjj*nx+iii]=sqrt(((float)iii-XX0)*((float)iii-XX0)+((float)jjj-YY0)*((float)jjj-YY0))/R_SUN;
00768 a1[jjj*nx+iii]=coef0[cam][0]+coef0[cam][1]*rr[jjj*nx+iii]+coef0[cam][2]*rr[jjj*nx+iii]*rr[jjj*nx+iii];
00769 a2[jjj*nx+iii]=coef1[cam][0]+coef1[cam][1]*rr[jjj*nx+iii]+coef1[cam][2]*rr[jjj*nx+iii]*rr[jjj*nx+iii];
00770 w1[jjj*nx+iii]=coef2[cam][0]+coef2[cam][1]*rr[jjj*nx+iii]+coef2[cam][2]*rr[jjj*nx+iii]*rr[jjj*nx+iii];
00771 w2[jjj*nx+iii]=w1[jjj*nx+iii];
00772 cmc[jjj*nx+iii]=coef4[cam][0]+coef4[cam][1]*rr[jjj*nx+iii]+coef4[cam][2]*rr[jjj*nx+iii]*rr[jjj*nx+iii];
00773
00774 }
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786 printf("cosmic ray detection\n");
00787
00788 ccount1=0;
00789 ccount2=0;
00790 last=2; current=0; next=1;
00791 double avg=0.0;
00792 query_flat_ref=flat_default;
00793 printtime();
00794
00795 printf("count filtergram %d\n", count_filtergram);
00796
00797 for (kkk=0; kkk<(count_filtergram+1); ++kkk){
00798
00799 if (cosmic_flag || ccount >= cthreshold[cam])
00800 {
00801
00802 km1=kkk-1;
00803
00804
00805
00806
00807 if (kkk < count_filtergram)
00808 {
00809 printf("fsn %d %d\n", kkk, keyvalue_fsn[kkk]);
00810
00811 segin = drms_segment_lookupnum(rec0[kkk], 0);
00812 arrin0= drms_segment_read(segin, type_double, &status);
00813
00814
00815
00816 if (status != DRMS_SUCCESS || arrin0 == NULL)
00817 {
00818
00819 printf("Error: there is a problem with the filtergram number %d \n", kkk);
00820 --ccount;
00821
00822 present_backward[kkk]=0;
00823 if (kkk < (count_filtergram-1)) present_backward[kkk+1]=0;
00824
00825 present_forward[kkk]=0;
00826 if (kkk > 0 ) present_forward[kkk-1]=0;
00827
00828 present[kkk]=0;
00829 for (jjj=0; jjj<ny; ++jjj) for (iii=0; iii<nx; ++iii) arrimg[next][jjj][iii]=NAN;
00830 flatfound=1;
00831 }
00832 else
00833
00834 {
00835
00836 printf("filtergram with FSN %d read\n",keyvalue_fsn[kkk]);
00837 arrinL0 = arrin0->data;
00838
00839
00840
00841
00842
00843 query_flat =keyvalue_flatnumb[kkk];
00844 if (strcmp(query_flat, query_flat_ref) != 0)
00845 {
00846 printf("%d %d read new flatfield\n", k, keyvalue_fsn[kkk]);
00847
00848
00849 status_flatfield=get_flat(query_flat, cam+1, app_flat, flat_off, badpix);
00850 flatfound=status_flatfield;
00851
00852 if (status_flatfield ==0)
00853 {
00854 printf("Flatfield %s read\n", query_flat);
00855
00856 }
00857 else
00858
00859 {
00860 printf("Failure reading flatfield / Use unity flatfield\n");
00861
00862 status=get_latest_bad(t_stamp, cam+1, filename_badpix, segmentname_badpix, badpix, &recnum_badpix);
00863 if (status != 0){printf("could not find bad pixel list"); return 1;}
00864 else {printf("recnum badpix %ld\n", recnum_badpix);}
00865 }
00866
00867
00868
00869 query_flat_ref=query_flat;
00870 }
00871
00872 if (status_flatfield != 0)
00873 {
00874 --ccount;
00875 statarr[kkk]=statarr[kkk]+2;
00876
00877 present_forward[kkk]=0;
00878 if (kkk > 0 ) present_forward[kkk-1]=0;
00879
00880 present_backward[kkk]=0;
00881 if (kkk < (count_filtergram-1)) present_backward[kkk+1]=0;
00882 printf("no flatfield for filtergram %d\n", kkk);
00883 }
00884
00885
00886 if (flatfound == 0) for (jjj=0; jjj<ny; ++jjj) for (iii=0; iii<nx; ++iii) arrimg[next][jjj][iii]=(float)arrinL0[jjj*nx+iii]*app_flat[jjj*nx+iii]/flat_relative[jjj*nx+iii];
00887 if (flatfound != 0) for (jjj=0; jjj<ny; ++jjj) for (iii=0; iii<nx; ++iii) arrimg[next][jjj][iii]=(float)arrinL0[jjj*nx+iii];
00888
00889 fsnarr[next]=keyvalue_fsn[kkk];
00890
00891 keyvalue_flatr[kkk]=flatfound;
00892 }
00893 drms_free_array(arrin0);
00894 }
00895
00896
00897
00898 count=-1;
00899
00900
00901
00902
00903 if (kkk >= 2 && kkk < count_filtergram)
00904 {
00905 index_last=kkk-2;
00906 index_current=kkk-1;
00907 index_next=kkk;
00908
00909 if (statarr[index_current] < 4)
00910 {
00911
00912 flatrec_index=keyvalue_flatr[index_last]+keyvalue_flatr[index_current]+keyvalue_flatr[index_next];
00913
00914
00915
00916 if (statarr[index_last] < 4 && statarr[index_next] < 4 && (tmind[index_current]-tmind[index_last]) < time_limit && (tmind[index_next]-tmind[index_current]) < time_limit && (flatrec_index == 0 || flatrec_index == 3))
00917 {
00918 count=0;
00919 #pragma omp parallel
00920 {
00921 #pragma omp for private(jjj,iii,wlr)
00922 for (jjj=0; jjj<ny; ++jjj) for (iii=0; iii<nx; ++iii)
00923 {
00924
00925
00926 if (rr[jjj*nx+iii] < crrmax)
00927 {
00928 wlr=((float)(keyvalue_wl[km1] % 20)-5.0)/2.0*lambda_sep-keyvalue_vrad[km1]/v_c*lambda0-(cos(keyvalue_p0[km1]/180.*M_PI)*((float)iii-keyvalue_X0[km1])-sin(keyvalue_p0[km1]/180.*M_PI)*((float)jjj-keyvalue_Y0[km1]))*cos(keyvalue_b0[km1]/180.0*M_PI)/keyvalue_rsun[km1]*cpa.rotcoef0*radsun_mm/v_c*lambda0;
00929 limw[jjj*nx+iii]=a1[jjj*nx+iii]*exp(-(wlr-coef3[cam][0])*(wlr-coef3[cam][0])/2.0/w1[jjj*nx+iii]/w1[jjj*nx+iii])+a2[jjj*nx+iii]*exp(-(wlr-coef3[cam][1])*(wlr-coef3[cam][1])/2.0/w2[jjj*nx+iii]/w2[jjj*nx+iii])+cmc[jjj*nx+iii];
00930 }
00931 }
00932
00933
00934
00935
00936
00937 #pragma omp for private(iii,jjj,diff_forward,diff_backward)
00938 for (jjj=0; jjj<ny; ++jjj) for (iii=0; iii<nx; ++iii)
00939 {
00940 cmarr[jjj*nx+iii]=1;
00941
00942
00943 if (rr[jjj*nx+iii] < crrmax)
00944 {
00945 diff_backward=arrimg[current][jjj][iii]-arrimg[last][jjj][iii];
00946 diff_forward=arrimg[current][jjj][iii]-arrimg[next][jjj][iii];
00947
00948 if (diff_forward < 0.0 && diff_backward < 0.0) signid=1; else signid=0;
00949
00950 if (fabs(diff_forward) > (factor[cam][signid]*limw[jjj*nx+iii]) && fabs(diff_backward) > (factor[cam][signid]*limw[jjj*nx+iii])){
00951
00952 if (badpix[jjj*nx+iii]){
00953 if (!isnan(arrimg[last][jjj][iii]) && !isnan(arrimg[current][jjj][iii]) && !isnan(arrimg[next][jjj][iii])){
00954 #pragma omp critical(detect)
00955 {
00956 cosmicarr[count]=jjj*nx+iii;
00957 if (diff_forward > 0.0 && diff_backward > 0.0)
00958 {
00959 cosmicval[count]=minval(diff_forward, diff_backward);
00960 cosmicsig[count]=fabs(cosmicval[count])/limw[jjj*nx+iii];
00961 ++count;
00962 cmarr[jjj*nx+iii]=0;
00963 }
00964 if (diff_forward < 0.0 && diff_backward < 0.0)
00965 {
00966 cosmicval[count]=maxval(diff_forward, diff_backward);
00967 cosmicsig[count]=fabs(cosmicval[count])/limw[jjj*nx+iii];
00968 ++count;
00969 cmarr[jjj*nx+iii]=0;
00970 }
00971
00972
00973
00974
00975
00976
00977 }
00978 }
00979 }
00980
00981 }
00982 }
00983 }
00984
00985 }
00986 }
00987 }
00988
00989 }
00990
00991
00992
00993
00994
00995
00996
00997
00998
01000
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050 if (cosmic_flag && kkk >= 1){
01051
01052
01053 if (present[km1] && keyvalue_fsn[km1] >= fsn_first && keyvalue_fsn[km1] <= fsn_last && time_fl[km1] >= tfirst && time_fl[km1] <= tlast)
01054 {
01055 printf("fsn count %d %d\n", keyvalue_fsn[km1], count);
01056 printf("fsn order %d %d %d\n", fsnarr[last], fsnarr[current],fsnarr[next]);
01057
01058 int cosfind, limit_flag;
01059 if (count == -1) cosfind=0; else if (count < limit_cosmic){cosfind=count; limit_flag=0;} else {cosfind=limit_cosmic; limit_flag=1;}
01060
01061
01062 recout = dataout->records[km1];
01063
01064 status=drms_setkey_int(recout, keyfsn, keyvalue_fsn[km1]);
01065 status=drms_setkey_time(recout, keytobs, time_fl[km1]);
01066 status=drms_setkey_int(recout, keycount, count);
01067 status=drms_setkey_int(recout, fidkey, fid);
01068 status=drms_setkey_int(recout, keycamera, cam+1);
01069 status=drms_setkey_int(recout, keyexmax, limit_flag);
01070 status=drms_setkey_float(recout, keylimit, factor[cam][0]);
01071 drms_setkey_float (recout, "CRRMAX", crrmax);
01072
01073 drms_keyword_setdate(recout);
01074
01075 if (keyvalue_cam[km1] == cam_id_front) status=drms_setkey_string(recout, keyinstrument, camera_str_front);
01076 if (keyvalue_cam[km1] == cam_id_side) status=drms_setkey_string(recout, keyinstrument, camera_str_side);
01077
01078 segout = drms_segment_lookup(recout, segmentname_cosmic);
01079 segout_val=drms_segment_lookup(recout, segmentname_val);
01080 segout_sig=drms_segment_lookup(recout, segmentname_sig);
01081
01082
01083
01084 axisbad[0]=cosfind;
01085 arrout=drms_array_create(type_int,1,axisbad,NULL,&status);
01086 cosmic_ray_data=arrout->data;
01087
01088 arrout_val=drms_array_create(type_float,1,axisbad,NULL,&status);
01089 val_data=arrout_val->data;
01090
01091 arrout_sig=drms_array_create(type_float,1,axisbad,NULL,&status);
01092 sig_data=arrout_sig->data;
01093
01094
01095 for (i=0; i<cosfind; ++i){cosmic_ray_data[i]=cosmicarr[i]; val_data[i]=cosmicval[i]; sig_data[i]=cosmicsig[i];}
01096
01097 status=drms_segment_write(segout, arrout, 0);
01098 status=drms_segment_write(segout_val, arrout_val, 0);
01099 status=drms_segment_write(segout_sig, arrout_sig, 0);
01100
01101 drms_free_array(arrout);
01102 drms_free_array(arrout_val);
01103 drms_free_array(arrout_sig);
01104 }
01105 }
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119 if (ccount >= cthreshold[cam] && kkk >= 1){
01120 update_stat=1;
01121
01122 if (present_backward[km1] != 0) ++ccount1;
01123 if (present_forward[km1] != 0) ++ccount2;
01124
01125 #pragma omp parallel
01126 {
01127
01128 if (present_backward[km1] != 0){
01129
01130 #pragma omp for private(jjj,iii)
01131 for (jjj=0; jjj<ny; ++jjj){
01132 for (iii=0; iii<nx; ++iii){
01133
01134 if (!isnan(arrimg[current][jjj][iii]) && arrimg[current][jjj][iii] > 0.0){rhsp[jjj*nx+iii]=rhsp[jjj*nx+iii]+log((double)arrimg[current][jjj][iii]);
01135 count_p[jjj*nx+iii]=count_p[jjj*nx+iii]+1;}
01136 }
01137 }
01138 }
01139
01140
01141 if (present_forward[km1] != 0){
01142
01143 #pragma omp for private(jjj,iii)
01144 for (jjj=0; jjj<ny; ++jjj){
01145 for (iii=0; iii<nx; ++iii){
01146
01147 if (!isnan(arrimg[current][jjj][iii]) && arrimg[current][jjj][iii] > 0.0){rhsm[jjj*nx+iii]=rhsm[jjj*nx+iii]+log((double)arrimg[current][jjj][iii]);
01148 count_m[jjj*nx+iii]=count_m[jjj*nx+iii]+1;}
01149 }
01150 }
01151 }
01152 }
01153
01154
01155
01156 }
01157
01158
01159
01160
01161 }
01162
01163
01164
01165
01166 holder=next;
01167 next=last;
01168 last=current;
01169 current=holder;
01170
01171
01172
01173 }
01174
01175
01176
01177
01178
01179
01180 printtime();
01181 printf("loop done\n");
01182 drms_close_records(dataout, DRMS_INSERT_RECORD);
01183
01184 printf("reference flatfield and limb darkening\n");
01185
01186
01187
01188
01189
01190 limb_darkening((double)R_SUN, (double)XX0, (double)YY0, b_coef, b_order, limb_dark);
01191
01192
01193 #pragma omp parallel for private(jjj,iii)
01194 for (jjj=0; jjj<ny; ++jjj){
01195 for (iii=0; iii<nx; ++iii){
01196
01197 if (count_p[jjj*nx+iii] > 0) rhsp[jjj*nx+iii]=(rhsp[jjj*nx+iii]/(double)count_p[jjj*nx+iii]-log(limb_dark[jjj*nx+iii]));
01198 if (count_m[jjj*nx+iii] > 0) rhsm[jjj*nx+iii]=(rhsm[jjj*nx+iii]/(double)count_m[jjj*nx+iii]-log(limb_dark[jjj*nx+iii]));
01199 }
01200 }
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222 printf("forward / backwards pairs %d %d \n", ccount1, ccount2);
01223
01224 if (ccount1>= cthreshold[cam]){
01225
01226
01227 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) flati[j*nx+i]=0.0;
01228
01229 rotpairs=ccount1;
01230
01231 cpa.norm=normconst[cam];
01232 cpa.omega=omegaconst[cam];
01233 cpa.convergence=convconst[cam];
01234 cpa.maxiter=maxiterconst[cam];
01235
01236 if (flatfield_flag && status_flatfield_relative == 0)
01237 {
01238 status=flatfield(rhsp, rhsm, badpix, ccount, flati, param, cpa, deltat);
01239 }
01240 else
01241 {
01242 status=1;
01243 }
01244
01245
01246 if (status ==0)
01247 {
01248
01249
01250 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i)flat[i][j]=exp(flati[j*nx+i]);
01251 update_stat=0;
01252
01253
01254 }
01255 }
01256
01257
01258
01259 if (status == 0) printf("flatfield for camera %d calculated \n", cam); else printf("could not calcuate flatfield for camera %d \n", cam);
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271 int axisout[2]={nx,ny};
01272 arrout_new = drms_array_create(type_float,2,axisout,NULL,&status);
01273 printf("array size %ld\n", drms_array_size(arrout_new)/sizeof(float));
01274
01275 flatfield_new=arrout_new->data;
01276
01277
01278
01279
01280
01281
01282 apod_circ(R_SUN*cpa.croprad*0.95, R_SUN*cpa.croprad*0.04, XX0-(float)nx/2.0, YY0-(float)ny/2.0, apod);
01283
01284
01285
01286
01287
01288
01289 for (j=0; j<ny; ++j){
01290 for (i=0; i<nx; ++i){
01291 flatfield_new[j*nx+i]=(flat[i][j]-1.0)*apod[j*nx+i]+1.0;
01292 }
01293 }
01294
01295
01296
01297 if (update_stat == 0)rot_new.rotpairs=rotpairs; else rot_new.rotpairs=0;
01298 rot_new.rotcadence=(float)deltat;
01299
01300
01301
01302
01303 printf("update_stat %d\n", update_stat);
01304
01305 printf("query flat %s\n", query_flat);
01306
01307 if (flatfield_flag)
01308 {
01309 status=write_flatfield_fid(arrout_new, cam+1, recnum_offpoint, t_0, focus, fid, rot_new);;
01310 if (status != 0){printf("could not write rotational flatfield\n"); return 1;}
01311 }
01312
01313
01314 drms_free_array(arrout_new);
01315
01316
01317 }
01318 else
01319 {
01320 printf("No data records found\n");
01321 return 1;
01322 }
01323
01324
01325
01326 if (data != NULL)
01327 {
01328 printf("close DRMS session \n");
01329 drms_close_records(data,DRMS_FREE_RECORD);
01330 }
01331
01332
01333
01334
01335
01336
01337
01338 free(param);
01339 free(rr);
01340 free(a1);
01341 free(a2);
01342 free(w1);
01343 free(w2);
01344 free(cmc);
01345 free(cosmicarr);
01346 free(cosmicsig);
01347 free(cosmicval);
01348 free(cmarr);
01349 free(apod);
01350 free(flat_off);
01351 free(flat_relative);
01352 free(badpix);
01353 free(app_flat);
01354 free(flati);
01355
01356 free(rhsp);
01357 free(rhsm);
01358 free(count_p);
01359 free(count_m);
01360 free(limb_dark);
01361
01362 free(limw);
01363
01364
01365 printtime();
01366 printf("COMPLETED!\n");
01367
01368
01369
01370 return 0;
01371
01372 }
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383