00001
00002
00003
00004
00005
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <math.h>
00042 #include <jsoc_main.h>
00043 #include <string.h>
00044 #include <time.h>
00045 #include <omp.h>
00046 #include <fresize.h>
00047 #include <module_flatfield.h>
00048
00049 char *module_name = "module_flatfield_combine";
00050
00051 #define kRecSetIn "input_series" //name of the series containing the input filtergrams
00052 #define datumn "datum"
00053 #define cameran "camera"
00054 #define fsnname "fsn"
00055 #define fsnf_name "fsn_first"
00056 #define fsnl_name "fsn_last"
00057
00058 #define minval(x,y) (((x) < (y)) ? (x) : (y))
00059
00060 ModuleArgs_t module_args[] =
00061 {
00062 {ARG_STRING, kRecSetIn, "", "Input data series."},
00063 {ARG_STRING, "output_series", "hmi.cosmic_rays", "Output data series"},
00064 {ARG_STRING, datumn, "yyyy.mm.dd", "Datum string"},
00065 {ARG_INT, "hour", "00", "Hour"},
00066 {ARG_INT, cameran, 0, "Camera"},
00067 {ARG_INT, fsnf_name, "0"},
00068 {ARG_INT, fsnl_name, "2147483647"},
00069 {ARG_END}
00070 };
00071
00072
00073
00074 void printtime();
00075 int is_element(int value, int *array, int n);
00076
00077 int DoIt(void)
00078 {
00079
00080 #include "module_flatfield_const.h"
00081
00082
00083
00084
00085 const char *inRecQuery = cmdparams_get_str(&cmdparams, kRecSetIn, NULL);
00086 const char *datum = cmdparams_get_str(&cmdparams, datumn, NULL);
00087 const char *output_series = cmdparams_get_str (&cmdparams, "output_series", NULL);
00088
00089 int cameraint = cmdparams_get_int(&cmdparams, cameran, NULL);
00090 int hour= cmdparams_get_int(&cmdparams, "hour", NULL);
00091 int fsn_first=cmdparams_get_int(&cmdparams, fsnf_name, NULL);
00092 int fsn_last=cmdparams_get_int(&cmdparams, fsnl_name, NULL);
00093
00094
00095
00096
00097
00098
00099 int status= DRMS_SUCCESS, stat=DRMS_SUCCESS;
00100
00101 DRMS_Segment_t *seg, *seg_val, *seg_sig;
00102 DRMS_Segment_t *segout, *segout_val, *segout_sig;
00103
00104 DRMS_Type_t type_time = DRMS_TYPE_TIME;
00105 DRMS_Type_t type_int = DRMS_TYPE_INT;
00106 DRMS_Type_t type_float = DRMS_TYPE_FLOAT;
00107 DRMS_Type_t type_double = DRMS_TYPE_DOUBLE;
00108
00109 DRMS_RecordSet_t *data, *dataout;
00110 DRMS_Record_t *recout, *rec0;
00111 DRMS_Array_t *arrout, *arrout_val, *arrout_sig;
00112
00113 int *cosmic_ray_data;
00114 float *val_data, *sig_data;
00115
00116 DRMS_Type_Value_t keyvalue_time;
00117
00118 int ct,ct_prev,ct_next,ctr;
00119 float *sig, *lev;
00120 int *hits;
00121 int axisbad[1];
00122
00123 int i, j, k, c;
00124
00125
00126 int axisout[2]={nx,ny};
00127
00128
00129
00130
00131
00132
00133 drms_series_exists(drms_env, output_series, &status);
00134 if (status == DRMS_ERROR_UNKNOWNSERIES)
00135 {
00136 printf("Output series %s doesn't exist\n", output_series);
00137 exit(EXIT_FAILURE);
00138 }
00139
00140
00141 printf("START!\n");
00142 printtime();
00143
00144
00145
00146
00147 char fnumb[2]={""};
00148 char ffnumb[2]={""};
00149 char timefirst[256]="";
00150
00151 strcat(timefirst, datum);
00152 strcat(timefirst, "_");
00153 sprintf(fnumb, "%2.2d", hour);
00154 strcat(timefirst, fnumb);
00155 strcat(timefirst, ":00:00.00_TAI");
00156
00157
00158
00159 char query0[256]="";
00160 TIME tfirst, tlast;
00161
00162 printf("fsnfs %d %d\n", fsn_first, fsn_last);
00163
00164
00165 if (fsn_first == 0 || fsn_last == 2147483647)
00166 {
00167
00168 char timelast[256]="";
00169 strcat(timelast, datum);
00170 strcat(timelast, "_");
00171 sprintf(ffnumb, "%2.2d", hour+1);
00172 strcat(timelast, ffnumb);
00173 strcat(timelast, ":59:59.99_TAI");
00174
00175
00176 tfirst=sscan_time(timefirst);
00177 tlast=sscan_time(timelast);
00178
00179 char datefirst[256]="";
00180 sprint_ut(datefirst, tfirst-5.0);
00181
00182 char datelast[256]="";
00183 sprint_ut(datelast, tlast+5.0);
00184
00185
00186 strcat(query0, inRecQuery);
00187 strcat(query0, "[");
00188 strcat(query0, datefirst);
00189 strcat(query0, "-");
00190 strcat(query0, datelast);
00191 strcat(query0, "][?CAMERA=");
00192 sprintf(fnumb, "%1.1d", cameraint);
00193 strcat(query0, fnumb);
00194 strcat(query0, "?]");
00195
00196 printf("query string time: %s\n", query0);
00197
00198 }
00199
00200
00201 if (fsn_first != 0 && fsn_last != 2147483647)
00202 {
00203
00204 tfirst=0;
00205 tlast=3881520000.0;
00206
00207 strcat(query0, inRecQuery);
00208 strcat(query0, "[][");
00209 char fsnf[10]={""};
00210 sprintf(fsnf, "%d", fsn_first-2);
00211 strcat(query0, fsnf);
00212 strcat(query0, "-");
00213 char fsnl[10]={""};
00214 sprintf(fsnl, "%d", fsn_last+2);
00215 strcat(query0, fsnl);
00216 strcat(query0, "][?CAMERA=");
00217 char fnumb[2]={""};
00218 sprintf(fnumb, "%1.1d", cameraint);
00219 strcat(query0, fnumb);
00220 strcat(query0, "?]");
00221 printf("query string fsn: %s\n", query0);
00222 }
00223
00224
00225
00226
00227
00228
00229 data = drms_open_records(drms_env,query0,&stat);
00230
00231 if (data == NULL){printf("can not open records\n"); exit(EXIT_FAILURE);}
00232
00233 drms_stage_records(data, 1, 0);
00234
00235 int nRecs=data->n;
00236 printf("number of records %d\n", nRecs);
00237 printtime();
00238
00239
00240
00241
00242
00243
00244 if (stat == DRMS_SUCCESS && data != NULL && nRecs > 0)
00245 {
00246 int keyvalue_fid[nRecs];
00247 int keyvalue_cam[nRecs];
00248 DRMS_Record_t *rec0[nRecs];
00249 int *cosmic_rays[nRecs];
00250 int keyvalue_recnum[nRecs];
00251 DRMS_Array_t *arr_cosmic[nRecs], *arr_level[nRecs], *arr_sig[nRecs];
00252 short wave[nRecs];
00253 short pol[nRecs];
00254 int count[nRecs];
00255 double time_fl[nRecs];
00256 int keyvalue_fsn[nRecs];
00257 int keyvalue_flag[nRecs];
00258 float keyvalue_factor[nRecs];
00259 float *significance[nRecs], *level[nRecs];
00260 int new_count[nRecs];
00261 int npairs[nRecs];
00262 float crrmax[nRecs];
00263
00264
00265 printtime();
00266 printf("create_records ...");
00267 dataout = drms_create_records (drms_env, nRecs, output_series,
00268 DRMS_PERMANENT, &stat);
00269 printf("done\n");
00270 printtime();
00271
00272 printf("begin keyword reading loop\n");
00273 for (k=0; k<nRecs; ++k)
00274 {
00275 rec0[k]=data->records[k];
00276
00277
00278
00279 count[k]= drms_getkey_int(rec0[k],keycount,&status);
00280 keyvalue_fid[k] = drms_getkey_int(rec0[k],fidkey,&status);
00281 keyvalue_cam[k] = drms_getkey_int(rec0[k],keycamera,&status);
00282 keyvalue_fsn[k]= drms_getkey_int(rec0[k],keyfsn,&status);
00283 keyvalue_flag[k] = drms_getkey_int(rec0[k],keyexmax,&status);
00284 keyvalue_factor[k] = drms_getkey_float(rec0[k],keylimit,&status);
00285
00286 keyvalue_time=drms_getkey(rec0[k], keytobs, &type_time, &status); ;
00287 time_fl[k] = keyvalue_time.time_val;
00288
00289 wave[k]=((keyvalue_fid[k]-10000)/10-5)/2;
00290 pol[k]=(keyvalue_fid[k] % 10);
00291
00292 keyvalue_recnum[k]=drms_getkey_int(rec0[k], recnumkey,&status);
00293 crrmax[k] = drms_getkey_float (rec0[k], "CRRMAX", &status);
00294
00295 }
00296 printtime();
00297
00298
00299
00300
00301
00302
00303
00304 printf("begin data reading loop\n");
00305
00306 for (k=0; k<nRecs; ++k)
00307 {
00308
00309 seg = drms_segment_lookup(rec0[k], segmentname_cosmic);
00310 seg_val=drms_segment_lookup(rec0[k], segmentname_val);
00311 seg_sig=drms_segment_lookup(rec0[k], segmentname_sig);
00312
00313 arr_cosmic[k]= drms_segment_read(seg, type_int, &status);
00314 arr_level[k]=drms_segment_read(seg_val, type_float, &status);
00315 arr_sig[k]=drms_segment_read(seg_sig, type_float, &status);
00316
00317
00318 cosmic_rays[k] = arr_cosmic[k]->data;
00319 significance[k]=arr_sig[k]->data;
00320 level[k]=arr_level[k]->data;
00321
00322 }
00323
00324 printtime();
00325
00326
00327
00328
00329 int elem_prev, elem_next;
00330
00331 int *cosmic_new;
00332 float *val_new;
00333 float *sig_new;
00334
00335 int counter=0;
00336 int falsecounter=0, goodcounter=0;
00337
00338 cosmic_new=(int *)(malloc(limit_cosmic*sizeof(int)));
00339 val_new=(float *)(malloc(limit_cosmic*sizeof(float)));
00340 sig_new=(float *)(malloc(limit_cosmic*sizeof(float)));
00341
00342
00343
00344
00345 printf("begin test loop\n");
00346
00347 for (k=0; k<nRecs; ++k)
00348 {
00349
00350
00351 if (time_fl[k] >= tfirst && time_fl[k] <= tlast && keyvalue_fsn[k] >= fsn_first && keyvalue_fsn[k] <= fsn_last)
00352 {
00353
00354 hits=cosmic_rays[k];
00355 sig=significance[k];
00356 lev=level[k];
00357
00358 ct=minval(count[k], limit_cosmic);
00359
00360 if (count[k] > 0 && k >=1 && k < (nRecs-1))
00361 {
00362
00363 ct_prev=minval(count[k-1], limit_cosmic);
00364 ct_next=minval(count[k+1], limit_cosmic);
00365
00366
00367 ctr=0;
00368
00369 for (c=0; c<ct; ++c)
00370 {
00371
00372 ++counter;
00373 elem_prev=is_element(hits[c], cosmic_rays[k-1], ct_prev);
00374 elem_next=is_element(hits[c], cosmic_rays[k+1], ct_next);
00375
00376 if (elem_prev == 0 && elem_next == 0)
00377 {
00378
00379 cosmic_new[ctr]=hits[c];
00380 val_new[ctr]=lev[c];
00381 sig_new[ctr]=sig[c];
00382 ++ctr;
00383 ++goodcounter;
00384 }
00385 else
00386 {
00387 ++falsecounter;
00388 }
00389 }
00390
00391 new_count[k]=ctr;
00392 }
00393 else
00394 {
00395 ctr=count[k];
00396
00397 for (c=0; c<ct; ++c)
00398 {
00399
00400 cosmic_new[c]=hits[c];
00401 val_new[c]=lev[c];
00402 sig_new[c]=sig[c];
00403
00404 }
00405
00406 new_count[k]=ctr;
00407 }
00408
00409
00410
00411
00412
00413 recout = dataout->records[k];
00414
00415 status=drms_setkey_int(recout, keyfsn, keyvalue_fsn[k]);
00416 status=drms_setkey_time(recout, keytobs, time_fl[k]);
00417
00418 status=drms_setkey_int(recout, fidkey, keyvalue_fid[k]);
00419 status=drms_setkey_int(recout, keycamera, keyvalue_cam[k]);
00420 status=drms_setkey_int(recout, keyexmax, keyvalue_flag[k]);
00421 status=drms_setkey_float(recout, keylimit, keyvalue_factor[k]);
00422 drms_setkey_float (recout, "CRRMAX", crrmax[k]);
00423
00424 drms_keyword_setdate(recout);
00425
00426 status=drms_setkey_int(recout, keycount, new_count[k]);
00427
00428 if (keyvalue_cam[k] == 2) status=drms_setkey_string(recout, keyinstrument, camera_str_front);
00429 if (keyvalue_cam[k] == 1) status=drms_setkey_string(recout, keyinstrument, camera_str_side);
00430
00431
00432
00433
00434
00435
00436
00437 if (new_count[k] >= 0)
00438 {
00439
00440 segout = drms_segment_lookup(recout, segmentname_cosmic);
00441 segout_val=drms_segment_lookup(recout, segmentname_val);
00442 segout_sig=drms_segment_lookup(recout, segmentname_sig);
00443
00444 axisbad[0]=new_count[k];
00445 arrout=drms_array_create(type_int,1,axisbad,NULL,&status);
00446 cosmic_ray_data=arrout->data;
00447
00448 arrout_val=drms_array_create(type_float,1,axisbad,NULL,&status);
00449 val_data=arrout_val->data;
00450
00451 arrout_sig=drms_array_create(type_float,1,axisbad,NULL,&status);
00452 sig_data=arrout_sig->data;
00453
00454
00455 for (i=0; i<new_count[k]; ++i){cosmic_ray_data[i]=cosmic_new[i]; val_data[i]=val_new[i]; sig_data[i]=sig_new[i];}
00456
00457 status=drms_segment_write(segout, arrout, 0);
00458 status=drms_segment_write(segout_val, arrout_val, 0);
00459 status=drms_segment_write(segout_sig, arrout_sig, 0);
00460
00461 drms_free_array(arrout);
00462 drms_free_array(arrout_val);
00463 drms_free_array(arrout_sig);
00464 }
00465 }
00466
00467 }
00468
00469
00470
00471
00472 for (k=0; k<nRecs; ++k)
00473 {
00474 drms_free_array(arr_level[k]);
00475 drms_free_array(arr_cosmic[k]);
00476 drms_free_array(arr_sig[k]);
00477 }
00478
00479
00480 drms_close_records(dataout, DRMS_INSERT_RECORD);
00481
00482
00483 drms_close_records(data,DRMS_FREE_RECORD);
00484
00485 free(cosmic_new);
00486 free(sig_new);
00487 free(val_new);
00488
00489
00490 printf("correctly identified cosmic rays %d out of %d %f\n", goodcounter, (goodcounter+falsecounter), (float)goodcounter/(float)(falsecounter+goodcounter));
00491
00492 }
00493 else
00494 {
00495 printf("No data records found\n");
00496 }
00497
00498
00499
00500
00501
00502
00503 printtime();
00504
00505 printf("COMLETED!\n");
00506 return 0;
00507
00508 }
00509
00510
00511
00512
00513
00514 int is_element(int value, int *array, int n)
00515 {
00516
00517 int isel, ctr;
00518
00519 ctr=0;
00520 isel=0;
00521 while (ctr < n && isel == 0)
00522 {
00523 isel=(value == array[ctr]);
00524 ++ctr;
00525 }
00526
00527 return isel;
00528 }
00529
00530
00531 void printtime()
00532
00533 {
00534 time_t timer, timerd;
00535 char *timestring;
00536 int i;
00537
00538 timerd=time(&timer);
00539 timestring=ctime(&timer);
00540 for (i=0; i<24; ++i) printf("%c", *(timestring+i));
00541 printf("\n");
00542 }
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554