00001
00002
00003
00004
00005 #include <stdlib.h>
00006 #include <stdio.h>
00007 #include <math.h>
00008 #include <jsoc_main.h>
00009 #include <string.h>
00010 #include <time.h>
00011
00012
00013
00014 char *module_name = "write_flatfield";
00015
00016
00017 ModuleArgs_t module_args[] =
00018 {
00019 {ARG_STRING, "instrument", ""},
00020 {ARG_STRING, "series_offpoint", ""},
00021 {ARG_STRING, "series_badpix", ""},
00022 {ARG_STRING, "series_dark", ""},
00023 {ARG_STRING, "series_flatfield", ""},
00024 {ARG_STRING,"file_flatfield"},
00025 {ARG_INT, "camera", "0", "1-2"},
00026 {ARG_STRING, "wave_str", "dd"},
00027 {ARG_INT, "focus", "0"},
00028 {ARG_TIME, "t_start"},
00029 {ARG_TIME, "t_stop", "DRMS_MISSING_VALUE"},
00030 {ARG_TIME, "t_obs_dark"},
00031 {ARG_TIME, "t_obs_badpix"},
00032 {ARG_TIME, "t_obs_offpoint"},
00033 {ARG_INT, "flatfield_version", "0"},
00034 {ARG_INT, "nx", "4096"},
00035 {ARG_INT, "ny", "4096"},
00036 {ARG_END}
00037 };
00038
00039
00040
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 int DoIt(void)
00052 {
00053
00054
00055 int i,j,k;
00056 int status=0;
00057
00058 const char *instrument;
00059 instrument=cmdparams_get_str(&cmdparams, "instrument", &status);
00060
00061 if (strcmp(instrument,"HMI") != 0 && strcmp(instrument,"AIA") != 0){printf("Instrument invalid\n"); exit(EXIT_FAILURE);}
00062
00063 int inst_hmi=!strcmp(instrument,"HMI");
00064 int inst_aia=!strcmp(instrument,"AIA");
00065
00066
00067
00068 const char *input_flatfield;
00069 input_flatfield = cmdparams_get_str(&cmdparams, "file_flatfield", &status);
00070
00071
00072 int foc_id;
00073 if (inst_hmi){
00074 foc_id=cmdparams_get_int(&cmdparams, "focus", &status);
00075 }
00076
00077
00078 const char *wavelength;
00079 int vvd=0, vvv=-1;
00080 short aia_cam[23]={1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4};
00081
00082 if (inst_aia)
00083 {
00084 wavelength=cmdparams_get_str(&cmdparams, "wave_str", &status);
00085 char **wavestrs=(char **)(malloc(23*sizeof(char *)));
00086
00087
00088 wavestrs[0]="131_THIN";
00089 wavestrs[1]="131_THICK";
00090 wavestrs[2]="131_OPEN";
00091 wavestrs[3]="335_THIN";
00092 {char *wv="335_THICK"; wavestrs[4]=wv;}
00093 {char *wv="335_OPEN"; wavestrs[5]=wv;}
00094 {char *wv="193_THIN"; wavestrs[6]=wv;}
00095 {char *wv="193_THICK"; wavestrs[7]=wv;}
00096 {char *wv="193_OPEN"; wavestrs[8]=wv;}
00097 {char *wv="211_THIN"; wavestrs[9]=wv;}
00098 {char *wv="211_THICK"; wavestrs[10]=wv;}
00099 {char *wv="211_OPEN"; wavestrs[11]=wv;}
00100 {char *wv="171_THIN"; wavestrs[12]=wv;}
00101 {char *wv="171_THICK"; wavestrs[13]=wv;}
00102 {char *wv="1600"; wavestrs[14]=wv;}
00103 {char *wv="1700"; wavestrs[15]=wv;}
00104 {char *wv="4500"; wavestrs[16]=wv;}
00105 {char *wv="94_THIN"; wavestrs[17]=wv;}
00106 {char *wv="94_THICK"; wavestrs[18]=wv;}
00107 {char *wv="94_OPEN"; wavestrs[19]=wv;}
00108 {char *wv="304_THIN"; wavestrs[20]=wv;}
00109 {char *wv="304_THICK"; wavestrs[21]=wv;}
00110 {char *wv="304_OPEN"; wavestrs[22]=wv;}
00111
00112
00113
00114
00115
00116 for (i=0; i<23; ++i){vvd += !strcmp(wavestrs[i], wavelength); if (!strcmp(wavestrs[i], wavelength)) vvv=i;}
00117 if (vvd == 0){printf("nonexisting wavelength id\n"); exit(EXIT_FAILURE);} else {printf("number %d\n", vvv);}
00118 }
00119
00120
00121
00122
00123
00124 int cam_id;
00125 if (inst_hmi) cam_id=cmdparams_get_int(&cmdparams, "camera", &status);
00126
00127 char *camera_string;
00128
00129 if (inst_hmi)
00130 {
00131 if (cam_id == 1) camera_string="HMI_SIDE1";
00132 if (cam_id == 2) camera_string="HMI_FRONT2";
00133 if (cam_id < 1 || cam_id > 2){printf("wrong camera id for HMI\n"); exit(EXIT_FAILURE);}
00134 }
00135
00136 if (inst_aia){
00137 if (aia_cam[vvv] == 1) camera_string="AIA_ATA1";
00138 if (aia_cam[vvv] == 2) camera_string="AIA_ATA2";
00139 if (aia_cam[vvv] == 3) camera_string="AIA_ATA3";
00140 if (aia_cam[vvv] == 4) camera_string="AIA_ATA4";
00141 }
00142
00143
00144 TIME t_start=cmdparams_get_time(&cmdparams, "t_start", NULL);
00145 TIME t_stop_preliminary=cmdparams_get_time(&cmdparams, "t_stop", &status);
00146
00147 if (status != 0) {printf("in here\n"); t_stop_preliminary=t_start+365.0*24.0*60.0*60.0;}
00148
00149 TIME t_dark=cmdparams_get_time(&cmdparams, "t_obs_dark", NULL);
00150 TIME t_bad=cmdparams_get_time(&cmdparams, "t_obs_badpix", NULL);
00151 TIME t_offpoint=cmdparams_get_time(&cmdparams, "t_obs_offpoint", NULL);
00152
00153
00154 const char *series_name_offpoint;
00155 series_name_offpoint=cmdparams_get_str(&cmdparams, "series_offpoint", NULL);
00156
00157 drms_series_exists(drms_env, series_name_offpoint, &status);
00158 if (status == DRMS_ERROR_UNKNOWNSERIES)
00159 {
00160 printf("Output series %s doesn't exist\n",series_name_offpoint);
00161 exit(EXIT_FAILURE);
00162 }
00163
00164 const char *series_name_dark;
00165 series_name_dark=cmdparams_get_str(&cmdparams, "series_dark", NULL);
00166 if (status == DRMS_ERROR_UNKNOWNSERIES)
00167 {
00168 printf("Output series %s doesn't exist\n",series_name_dark);
00169 exit(EXIT_FAILURE);
00170 }
00171
00172
00173
00174 const char *series_name_bad;
00175 series_name_bad=cmdparams_get_str(&cmdparams, "series_badpix", NULL);
00176 if (status == DRMS_ERROR_UNKNOWNSERIES)
00177 {
00178 printf("Output series %s doesn't exist\n",series_name_bad);
00179 exit(EXIT_FAILURE);
00180 }
00181
00182
00183 const char *series_name_flatfield;
00184 series_name_flatfield=cmdparams_get_str(&cmdparams,"series_flatfield", NULL);
00185 if (status == DRMS_ERROR_UNKNOWNSERIES)
00186 {
00187 printf("Output series %s doesn't exist\n",series_name_flatfield);
00188 exit(EXIT_FAILURE);
00189 }
00190
00191 int version;
00192 version=cmdparams_get_int(&cmdparams, "flatfield_version", NULL);
00193
00194
00195 int stat = DRMS_SUCCESS;
00196 int error = 0;
00197
00198
00199 DRMS_RecordSet_t *dataout, *dataout_off, *dataout_dark, *dataout_bad;
00200 DRMS_RecordSet_t *ff_offpoint, *ff_bad, *ff_dark, *ff_flat;
00201 DRMS_Record_t *recout = NULL;
00202 DRMS_Record_t *record_off, *record_dark, *record_bad, *record_flat;
00203 DRMS_Segment_t *segout = NULL, *segin = NULL;
00204 DRMS_Array_t *arrout, *arr_bad;
00205 long long recnum_off, recnum_dark, recnum_bad;
00206 DRMS_Type_t type;
00207 DRMS_Type_t type_time = DRMS_TYPE_TIME;
00208 size_t bytes_read;
00209 FILE *fgram;
00210 float *gout1;
00211 int *gout_bad;
00212 int nbad=0;
00213 int *badpix;
00214
00215 const int nx=cmdparams_get_int(&cmdparams, "nx", NULL);
00216 const int ny=cmdparams_get_int(&cmdparams, "ny", NULL);
00217
00218 int axisin[2];
00219 int axisout[2]={nx,ny};
00220
00221
00222
00223
00224 const char *primekey1 = "CAMERA";
00225 const char *primekey2 = "T_OBS";
00226 const char *primekey2a="T_START";
00227 const char *primekey3 = "HMI_SEQ_ID_FOCUS";
00228
00229
00230 const char *key1 = "FSN_FIRST";
00231 const char *key2 = "FSN_LAST";
00232 const char *key3 = "FSN_INPUT";
00233
00234 const char *key4= "T_STOP";
00235 const char *key5= "FLATFIELD_VERSION";
00236 const char *key6="INSTRUME";
00237
00238 const char *key1aia="WAVE_STR";
00239
00240 char *camstr_side="[1]";
00241 char *camstr_front="[2]";
00242
00243 char *camstr;
00244
00245 char t0_off[256];
00246 sprint_ut(t0_off, t_offpoint);
00247
00248 char t0_flat[256];
00249 sprint_ut(t0_flat, t_start);
00250
00251 char query[256]={""}, query_flat[256]={""}, query_dark[256]={""}, query_bad[256]={""};
00252 char q1_hmi[256]={""};
00253 char q1_aia[256]={""};
00254 char t_orig_str[256];
00255 sprint_ut(t_orig_str, 0.0);
00256
00257
00258 strcat(query,series_name_offpoint);
00259 strcat(query_flat, series_name_flatfield);
00260
00261 if (inst_hmi)
00262 {
00263 if (cam_id == 1){camstr=camstr_side;}
00264 if (cam_id == 2){camstr=camstr_front;}
00265 strcat(q1_hmi, camstr);
00266 strcat(query, q1_hmi);
00267 strcat(query_flat, q1_hmi);
00268 }
00269
00270 if (inst_aia)
00271 {
00272
00273 strcat(q1_aia,"[");
00274 strcat(q1_aia, wavelength);
00275 strcat(q1_aia, "]");
00276
00277 strcat(query, q1_aia);
00278 }
00279
00280 strcat(query, "[");
00281 strcat(query,t0_off);
00282 strcat(query,"]");
00283
00284 if (inst_hmi)
00285 {
00286 strcat(query_flat, "[");
00287 strcat(query_flat,t_orig_str);
00288 strcat(query_flat,"-");
00289 strcat(query_flat,t0_flat);
00290 strcat(query_flat,"]");
00291 }
00292
00293 if (inst_hmi)
00294 {
00295 strcat(query,"[");
00296
00297 char ffnumb[2]={""};
00298 sprintf(ffnumb, "%2.2d", foc_id);
00299
00300 strcat(query, ffnumb);
00301 strcat(query, "]");
00302
00303 }
00304
00305
00306
00307
00308 printf("query offpoint %s\n", query);
00309 ff_offpoint = drms_open_records(drms_env,query,&status);
00310
00311 if (status == DRMS_SUCCESS && ff_offpoint!= NULL && ff_offpoint->n > 0)
00312 {
00313 record_off=ff_offpoint->records[0];
00314 recnum_off=record_off->recnum;
00315 }
00316 else
00317 {
00318
00319 printf("Offpoint flatfield not found\n");
00320 exit(EXIT_FAILURE);
00321 }
00322
00323
00324
00325 char t0_dark[256];
00326 sprint_ut(t0_dark, t_dark);
00327
00328 if (inst_hmi)
00329 {
00330 strcat(query_dark,series_name_dark);
00331 strcat(query_dark,q1_hmi);
00332 strcat(query_dark,"[");
00333 strcat(query_dark,t0_dark);
00334 strcat(query_dark,"]");
00335 printf("query string %s\n", query_dark);
00336 }
00337
00338 if (inst_aia)
00339 {strcat(query_dark,series_name_dark);
00340 strcat(query_dark,q1_aia);
00341 strcat(query_dark,"[");
00342 strcat(query_dark,t0_dark);
00343 strcat(query_dark,"]");
00344 printf("query string %s\n", query_dark);
00345 }
00346
00347
00348 ff_dark = drms_open_records(drms_env,query_dark,&status);
00349
00350
00351
00352
00353 if (status == DRMS_SUCCESS && ff_dark != NULL && ff_dark->n > 0)
00354 {
00355 record_dark = ff_dark->records[0];
00356 recnum_dark=record_dark->recnum;
00357 }
00358 else
00359 {
00360
00361 printf("Dark record not found\n");
00362 exit(EXIT_FAILURE);
00363 }
00364
00365
00366
00367 char t0_bad[256];
00368 sprint_ut(t0_bad, t_bad);
00369
00370 if (inst_hmi)
00371 {
00372 strcat(query_bad,series_name_bad);
00373 strcat(query_bad,q1_hmi);
00374 strcat(query_bad,"[");
00375 strcat(query_bad,t0_bad);
00376 strcat(query_bad,"]");
00377 printf("query string %s\n", query_bad);
00378 }
00379
00380 if (inst_aia)
00381 {
00382 strcat(query_bad,series_name_bad);
00383 strcat(query_bad,q1_aia);
00384 strcat(query_bad,"[");
00385 strcat(query_bad,t0_bad);
00386 strcat(query_bad,"]");
00387 printf("query string %s\n", query_bad);
00388 }
00389
00390
00391 ff_bad = drms_open_records(drms_env,query_bad,&status);
00392
00393
00394
00395
00396 if (status == DRMS_SUCCESS && ff_bad != NULL && ff_bad->n > 0)
00397 {
00398 record_bad = ff_bad->records[0];
00399 recnum_bad=record_bad->recnum;
00400
00401 segin = drms_segment_lookup(record_bad, "bad_pixel_list");
00402 arr_bad = drms_segment_read(segin, segin->info->type, &status);
00403 nbad=arr_bad->axis[0];
00404 badpix=arr_bad->data;
00405
00406 }
00407 else
00408 {
00409
00410 printf("Bad pixel record not found\n");
00411 exit(EXIT_FAILURE);
00412 }
00413
00414 type = DRMS_TYPE_FLOAT;
00415 float *f_flatfield;
00416
00417
00418 fgram=fopen(input_flatfield, "rb");
00419
00420 if (fgram==NULL){fputs("Input file not found", stderr); printf("Input file not found\n"); exit(EXIT_FAILURE);}
00421 f_flatfield=(float *)(malloc(nx*ny*sizeof(float)));
00422 bytes_read=fread(f_flatfield,sizeof(float),nx*ny,fgram);
00423
00424 fclose(fgram);
00425
00426
00427 arrout = drms_array_create(type,2,axisout,NULL,&status);
00428 gout1=arrout->data;
00429
00430
00431 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) gout1[j*nx+i]=f_flatfield[j*nx+i];
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 drms_series_exists(drms_env, (char *)series_name_flatfield, &status);
00442 if (status == DRMS_ERROR_UNKNOWNSERIES)
00443 {
00444 printf("Flatfield series %s doesn't exist\n",series_name_flatfield);
00445 exit(EXIT_FAILURE);
00446 }
00447
00448
00449
00450
00451
00452
00453
00454
00455 dataout = drms_create_records(drms_env,1,(char *)series_name_flatfield,DRMS_PERMANENT,&stat);
00456
00457
00458
00459 if (stat != DRMS_SUCCESS)
00460 {
00461 printf("Could not create a record for the series %s\n",series_name_flatfield);
00462 exit(EXIT_FAILURE);
00463 }
00464 if (stat == DRMS_SUCCESS)
00465 {
00466 printf("Writing a record on the DRMS for the series %s\n",series_name_flatfield);
00467 recout = dataout->records[0];
00468
00469 status=0;
00470 status += drms_setkey_time(recout, primekey2a, t_start);
00471 status += drms_setkey_time(recout,key4,t_stop_preliminary);
00472 status += drms_setkey_int(recout,key5,version);
00473
00474 if (inst_hmi) status += drms_setkey_int(recout, primekey1,cam_id);
00475 if (inst_hmi) status += drms_setkey_int(recout,primekey3,foc_id);
00476
00477 status += drms_setkey_string(recout, key6, camera_string);
00478
00479 if (inst_hmi)
00480 {
00481 status += drms_setkey_int(recout, "ROTF_FLATFIELD", 0);
00482 status += drms_setkey_int(recout, "ROTF_N_PAIRS", 0);
00483 status += drms_setkey_float(recout, "ROTF_CADENCE", 0.0);
00484 }
00485
00486 if(inst_aia)
00487 {
00488 status += drms_setkey_string(recout, key1aia, wavelength);
00489
00490 }
00491
00492 if (status != 0){printf("error setting keywords"); exit(EXIT_FAILURE);}
00493 drms_keyword_setdate(recout);
00494
00495
00496
00497
00498 status=0;
00499 {
00500 const char *linkname = "OFFPOINT_FLAT";
00501 status+=drms_setlink_static(recout, linkname, recnum_off);
00502 printf("link status offpoint %d\n", status);
00503 }
00504 {
00505 const char *linkname = "DARK";
00506 status+=drms_setlink_static(recout, linkname, recnum_dark);
00507 printf("link status dark %d\n", status);
00508
00509 }
00510 {
00511 const char *linkname = "BAD_PIXEL";
00512 status+=drms_setlink_static(recout, linkname, recnum_bad);
00513 printf("link status badpix %d\n", status);
00514 if (status != 0){printf("error setting links\n"); exit(EXIT_FAILURE);}
00515 }
00516
00517
00518
00519
00520
00521 printf("write out segment\n");
00522 segout = drms_segment_lookup(recout, "flatfield");
00523 if (segout == NULL){printf("could not find segment\n"); exit(EXIT_FAILURE);}
00524
00525 status=drms_segment_write(segout, arrout, 0);
00526 if (status != 0){printf("could not write segment\n"); exit(EXIT_FAILURE);}
00527 }
00528
00529 printf("done\n");
00530
00531
00532 drms_close_records(dataout, DRMS_INSERT_RECORD);
00533
00534
00535 drms_free_array(arrout);
00536
00537
00538
00539
00540 printf("COMPLETED!\n");
00541
00542
00543
00544
00545
00546 return 0;
00547
00548
00549
00550 }
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573