00001
00002
00003
00004
00005
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <math.h>
00043 #include <jsoc_main.h>
00044 #include <string.h>
00045 #include <time.h>
00046 #include <omp.h>
00047 #include <fresize.h>
00048 #include <module_flatfield.h>
00049
00050 char *module_name = "module_flatfield_combine";
00051
00052 #define kRecSetIn "input_series" //name of the series containing the input filtergrams
00053 #define datumn "datum"
00054 #define cameran "camera"
00055 #define fsnname "fsn"
00056
00057
00058 ModuleArgs_t module_args[] =
00059 {
00060 {ARG_STRING, kRecSetIn, "", "Input data series."},
00061 {ARG_STRING, datumn, "", "Datum string"},
00062 {ARG_END}
00063 };
00064
00065
00066 int write_flatfields(char *filename_flatfield_out, DRMS_Array_t *arr_flat, DRMS_Array_t *arrout_new, int camera,
00067 long long recnum[6], TIME tobs_link[2], TIME t_0, int focus, int focusclone,
00068 struct rotpar rot_new,
00069 struct rotpar rot_cur);
00070
00071
00072 int write_rot_flatfield(char *filename_flatfield, DRMS_Array_t *arrout_new, int camera,
00073 TIME t_0, int focus, struct rotpar rot_new);
00074
00075 int retrieve_offpoint(char *query, int camera, float *offpoint, long long *recnum, int *focus);
00076
00077 int get_latest_dark(TIME t_0, int camera, const char *fname, const char *segname, float *offpoint, long long *recnum, int *focus);
00078
00079 int get_latest_flat(TIME t_0, int camera, const char *fname, const char *segname, float *offpoint, long long *recnum, int *focus);
00080
00081 int get_latest_bad(TIME t_0, int camera, const char *fname, const char *segname, short *bad, long long *recnum);
00082
00083 int read_flatfield_series(TIME t_0, int camera, float *flatfield, int *focus, TIME tobs_link[2], long long recnum[6],
00084 struct rotpar *rot_cur);
00085
00086 void highpass_2d(int M, int N, double fwhm1, double fwhm2, double phi, double a[nx][ny]);
00087 int DoIt(void)
00088 {
00089
00090 #include "module_flatfield_const.h"
00091
00092
00093
00094
00095
00096 const char *inRecQuery = cmdparams_get_str(&cmdparams, kRecSetIn, NULL);
00097 const char *datum = cmdparams_get_str(&cmdparams, datumn, NULL);
00098 int cameraint = cmdparams_get_int(&cmdparams, cameran, NULL);
00099
00100
00101
00102
00103
00104 int status, stat, status_bad_s,status_latest_off_s, status_off_s, status_flat_s, status_latest_s;
00105
00106 DRMS_Segment_t *segin = NULL;
00107
00108
00109 DRMS_Type_t type_time = DRMS_TYPE_TIME;
00110 DRMS_Type_t type_int = DRMS_TYPE_INT;
00111 DRMS_Type_t type_float = DRMS_TYPE_FLOAT;
00112 DRMS_Type_t type_double = DRMS_TYPE_DOUBLE;
00113
00114 DRMS_RecordSet_t *data, *dataout;
00115 DRMS_Record_t *ff_last_front=NULL, *ff_last_side=NULL;
00116 DRMS_Array_t *arr_flat, *arr_flat_new, *arr_flat_rel;
00117
00118 int axisout[2]={nx,ny};
00119
00120 TIME t_0;
00121 int focus, focusclone, camera;
00122 int fvers, foc;
00123 int cadence;
00124 int npairstot=0;
00125
00126 int i,j,k;
00127 float h1,h2;
00128
00129
00130 float *flatfield, *offpoint, *flat, *ffhp, *flathp, *dummyarr;
00131 short *bad;
00132 float *dark;
00133
00134 float *flatfield_new;
00135 float *ff, *ff_new, *ff_rel;
00136
00137 long long recnum[6];
00138 long long recnum_offpoint, recnum_dark, recnum_badpix, dummy;
00139 TIME tobs_link[2];
00140
00141 struct rotpar rot_cur, rot_new, rot_rel;
00142 struct fresize_struct fresizes;
00143
00144
00145
00146 flatfield=(float *)(malloc(nx*ny*sizeof(float)));
00147 flatfield_new=(float *)(malloc(nx*ny*sizeof(float)));
00148 offpoint=(float *)(malloc(nx*ny*sizeof(float)));
00149 dummyarr=(float *)(malloc(nx*ny*sizeof(float)));
00150 bad=(short *)(malloc(nx*ny*sizeof(short)));
00151 dark=(float *)(malloc(nx*ny*sizeof(float)));
00152
00153 flat=(float *)(calloc(nx*ny, sizeof(float)));
00154 ffhp=(float *)(malloc(nx*ny*sizeof(float)));
00155 flathp=(float *)(malloc(nx*ny*sizeof(float)));
00156
00157
00158
00159
00160
00161 int nthreads;
00162 nthreads=omp_get_num_threads();
00163
00164
00165 printf("Number of threads run in parallel = %d \n",nthreads);
00166
00167
00168
00169
00170
00171
00172
00173 drms_series_exists(drms_env, filename_flatfield_out, &status);
00174 if (status == DRMS_ERROR_UNKNOWNSERIES)
00175 {
00176 printf("Output series %s doesn't exist\n",filename_flatfield_out);
00177 return 1;
00178 }
00179 if (status == DRMS_SUCCESS)
00180 {
00181 printf("Output series %s exists.\n",filename_flatfield_out);
00182 }
00183
00184
00185
00186
00187
00188 char query[256]="";
00189 strcat(query, inRecQuery);
00190 strcat(query, "[");
00191 char cami[1]={""};
00192 sprintf(cami, "%d", cameraint);
00193 strcat(query, cami);
00194 strcat(query, "][");
00195 strcat(query, datum);
00196 strcat(query, "_00:00:00_TAI-");
00197 strcat(query, datum);
00198 strcat(query, "_23:59:59_TAI");
00199 strcat(query, "]");
00200
00201 printf("%s\n", query);
00202
00203
00204
00205
00206
00207 data = drms_open_records(drms_env,query,&stat);
00208
00209 if (data == NULL){printf("can not open records\n"); return 1;}
00210
00211 int nRecs=data->n;
00212 printf("number of records %d\n", nRecs);
00213
00214
00215
00216
00217
00218
00219 if (stat == DRMS_SUCCESS && data != NULL && nRecs > 0)
00220 {
00221 int keyvalue_fid[nRecs];
00222 int keyvalue_cam[nRecs];
00223 int keyvalue_cadence[nRecs];
00224 DRMS_Record_t *rec0[nRecs];
00225 float *flatfield_fid[nRecs];
00226 char *keyvalue_query[nRecs];
00227 DRMS_Array_t *arrin0[nRecs];
00228 short wave[nRecs];
00229 short pol[nRecs];
00230
00231 float pang[nRecs];
00232 int npairs[nRecs];
00233
00234 float nwav[6];
00235 for (i=0; i<6; ++i) nwav[i]=0.0;
00236
00237
00238 for (k=0; k<nRecs; ++k)
00239 {
00240 rec0[k]=data->records[k];
00241 pang[k]=drms_getkey_float(rec0[k],"PANG",&status);
00242 npairs[k]= drms_getkey_int(rec0[k],keynpairs,&status);
00243 keyvalue_query[k]=drms_getkey_string(rec0[k], querykey,&status);
00244 keyvalue_cam[k]=drms_getkey_int(rec0[k],keycamera,&status);
00245 keyvalue_cadence[k]=drms_getkey_int(rec0[k], keycadence,&status);
00246
00247 keyvalue_fid[k] = drms_getkey_int(rec0[k],fidkey,&status);
00248 wave[k]=((keyvalue_fid[k]-10000)/10-5)/2;
00249 pol[k]=(keyvalue_fid[k] % 10);
00250
00251 segin = drms_segment_lookupnum(rec0[k], 0);
00252 arrin0[k]= drms_segment_read(segin, type_float, &status);
00253 flatfield_fid[k] = arrin0[k]->data;
00254
00255
00256 if (isnan(pang[k]) || fabs(pang[k] - pang[0]) > 0.01){printf("Inconsistent P-angle in FID flatfields %d %f\n", k, pang[k]); return 1;}
00257
00258 }
00259
00260
00261
00262
00263
00264
00265 char timefirst[256]="";
00266 strcat(timefirst, datum);
00267 strcat(timefirst, "_00:00:00.00_TAI");
00268
00269 TIME t_0=sscan_time(timefirst)+24.0*60.0*60.0;
00270
00271
00272
00273
00274
00275 camera= drms_getkey_int(rec0[nRecs-1],keycamera,&status);
00276 cadence=drms_getkey_int(rec0[nRecs-1],keycadence,&status);
00277 char *qstr_refflat=keyvalue_query[nRecs-1];
00278
00279
00280
00281 for (k=0; k<nRecs; ++k)
00282 {
00283 if (npairs[k] > 0 && camera == keyvalue_cam[k] && cadence == keyvalue_cadence[k] && strcmp(qstr_refflat, keyvalue_query[k]) ==0 && wave[k] >= 0 && wave[k] <=5)
00284 {
00285
00286 npairstot=npairstot+npairs[k];
00287 printf("%d %d %d %d\n", k, keyvalue_fid[k], pol[k], wave[k]);
00288 nwav[wave[k]]=nwav[wave[k]]+1.0;
00289 }
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299 float sum=0.0;
00300 for (k=0; k<nRecs; ++k)
00301 {
00302
00303 if (nwav[wave[k]] > 0.0 && npairs[k] > 0 && camera == keyvalue_cam[k] && cadence == keyvalue_cadence[k] && strcmp(qstr_refflat, keyvalue_query[k]) ==0)
00304 {
00305 ff=flatfield_fid[k];
00306 sum=sum+cof[cameraint-1][wave[k]]/nwav[wave[k]];
00307 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) flat[j*nx+i]=flat[j*nx+i]+cof[cameraint-1][wave[k]]*(float)(ff[j*nx+i]-1.0)/nwav[wave[k]];
00308 }
00309 }
00310
00311 printf("query flat %s\n", qstr_refflat);
00312
00313
00314
00315
00316
00317
00318 status_off_s=retrieve_offpoint(qstr_refflat, cameraint, offpoint, &dummy, &focus);
00319 if (status_off_s != 0){printf("could not find offpoint flatfield\n"); return 1;}
00320
00321 status_latest_off_s=get_latest_flat(t_0, cameraint, filename_offpoint, segmentname_offpoint, dummyarr, &recnum_offpoint, &foc);
00322 if (status_latest_off_s != 0){printf("could not find latest offpoint flatfield\n"); return 1;}
00323 printf("recnum offpoint %ld\n", recnum_offpoint);
00324
00325 status_latest_s=get_latest_dark(t_0, cameraint, filename_dark, segmentname_dark, dark, &recnum_dark, &foc);
00326 if (status_latest_s != 0){printf("could not find dark\n"); return 1;}
00327 printf("recnum_dark %ld\n", recnum_dark);
00328
00329 status_bad_s=get_latest_bad(t_0, cameraint, filename_badpix, segmentname_badpix, bad, &recnum_badpix);
00330 if (status_bad_s != 0){printf("could not find bad pixel list\n"); return 1;}
00331 printf("recnum_badpix %ld\n", recnum_badpix);
00332
00333 status_flat_s=read_flatfield_series(t_0, cameraint, flatfield, &focusclone, tobs_link, recnum, &rot_cur);
00334 if (status_flat_s != 0){printf("could not find latest flatfield\n"); return 1;}
00335 printf("recnum flatfield %ld %ld %ld\n", recnum[0], recnum[1], recnum[2]);
00336
00337
00338 recnum[3]=recnum_offpoint;
00339 recnum[4]=recnum_dark;
00340 recnum[5]=recnum_badpix;
00341
00342
00343 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) if (bad[j*nx+i] == 0) flat[j*nx+i]=0.0;
00344
00345
00346
00347
00348
00349
00350
00351 double ffr[nx][ny];
00352 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) ffr[i][j]=flat[j*nx+i];
00353 highpass_2d(nx, ny, fwhm_x, fwhm_y, pang[0], ffr);
00354 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i)flathp[j*nx+i]=ffr[i][j];
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372 if (sum != 0.0 && sum !=1.0)
00373 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) flathp[j*nx+i]=flathp[j*nx+i]/sum;
00374
00375
00376
00377
00378
00379 drms_close_records(data,DRMS_FREE_RECORD);
00380
00381
00382
00383
00384
00385 int count=0;
00386
00387 if (update_flag == 2)
00388 {
00389 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i)
00390 {
00391 if (bad[j*nx+i])
00392 {
00393 flatfield_new[j*nx+i]=(1.0+flathp[j*nx+i])*offpoint[j*nx+i];
00394 if (flathp[j*nx+i] != 0.0) ++count;
00395 }
00396 else
00397 {
00398 flatfield_new[j*nx+i]=offpoint[j*nx+i];
00399 }
00400 }
00401 }
00402
00403
00404
00405
00406
00407 if (update_flag == 1)
00408 {
00409 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i)
00410 if (bad[j*nx+i] && (flat[j*nx+i] < threshold_lower|| flat[j*nx+i] > threshold_upper))
00411 {
00412 ++count;
00413 flatfield_new[j*nx+i]=(1.0f+flathp[j*nx+i])*offpoint[j*nx+i];
00414 }
00415 else
00416 {
00417 flatfield_new[j*nx+i]=offpoint[j*nx+i];
00418 }
00419 }
00420
00421
00422
00423
00424
00425 if (update_flag == 0)
00426 {
00427
00428 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i)
00429 flatfield_new[j*nx+i]=offpoint[j*nx+i];
00430 }
00431
00432
00433
00434
00435 arr_flat=drms_array_create(type_float,2,axisout,NULL,&status);
00436 arr_flat_new=drms_array_create(type_float,2,axisout,NULL,&status);
00437
00438
00439 ff_new=arr_flat_new->data;
00440 ff=arr_flat->data;
00441
00442
00443
00444
00445 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i)ff[j*nx+i]=flatfield[j*nx+i];
00446 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i)ff_new[j*nx+i]=flatfield_new[j*nx+i];
00447
00448
00449 rot_new.rotbad=count;
00450 rot_new.rotpairs=npairstot;
00451 rot_new.rotcadence=cadence;
00452
00453
00454
00455
00456
00457
00458 arr_flat_rel=drms_array_create(type_float,2,axisout,NULL,&status);
00459 ff_rel=arr_flat_rel->data;
00460
00461
00462
00463
00464
00465 int count_rel=0;
00466 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i){
00467 if (bad[j*nx+i] && npairstot >= 11520)
00468 {
00469 ff_rel[j*nx+i]=(1.0+flathp[j*nx+i])*offpoint[j*nx+i];
00470 if (flathp[j*nx+i] != 0.0) ++count_rel;
00471 }
00472 else
00473 {
00474 ff_rel[j*nx+i]=offpoint[j*nx+i];
00475 }
00476 }
00477
00478
00479 rot_rel.rotbad=count_rel;
00480 rot_rel.rotpairs=npairstot;
00481 rot_rel.rotcadence=cadence;
00482
00483 printf("rot_pairs / mod. pixels %d %d\n", npairstot, count_rel);
00484
00485
00486
00487
00488
00489 status=write_flatfields(filename_flatfield_out, arr_flat, arr_flat_new, camera, recnum, tobs_link, t_0, focus, focusclone, rot_new, rot_cur);
00490 if (status != 0){printf("could not write out flatfield to hmi series\n"); return 1;}
00491
00492
00493
00494
00495 if (count_rel == 0){printf("not enough data for rotational flatfield\n"); return 1;}
00496 status=write_rot_flatfield(filename_rot_flatfield, arr_flat_rel, camera, t_0, focus, rot_rel);
00497
00498
00499 if (status != 0){printf("could not write out flatfield to intermediate series\n"); return 1;}
00500
00501
00502
00503 }
00504 else
00505
00506 {
00507 printf("No data records found\n");
00508 return 1;
00509 }
00510
00511
00512
00513
00514 free(flatfield);
00515 free(flatfield_new);
00516 free(offpoint);
00517 free(dummyarr);
00518 free(bad);
00519 free(dark);
00520 free(flat);
00521 free(flathp);
00522 free(ffhp);
00523 free_fresize(&fresizes);
00524
00525
00526
00527
00528
00529
00530
00531
00532 printf("COMLETED!\n");
00533
00534 return 0;
00535
00536 }
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547