00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <math.h>
00004 #include <jsoc_main.h>
00005 #include <string.h>
00006 #include <time.h>
00007 #include "module_flatfield.h"
00008
00009 int get_latest_dark(TIME t_0, int camera, const char *fname, const char *segname, float *offpoint, long long *recnum, int *focus)
00010 {
00011 #include "module_flatfield_const.h"
00012
00013 int status, nRec;
00014 int i,j;
00015 DRMS_RecordSet_t *ff_series;
00016 DRMS_Record_t *record_off;
00017 DRMS_Segment_t *segin = NULL;
00018 DRMS_Array_t *arr_off;
00019 float *arr_data;
00020
00021 char t_orig_str[256]="";
00022 sprint_ut(t_orig_str, 0.0);
00023
00024 char t0_str[256]="";
00025 sprint_ut(t0_str, t_0);
00026
00027 char *camstr_side="[1]";
00028 char *camstr_front="[2]";
00029
00030 char *camstr;
00031 if (camera == 1){camstr=camstr_side;}
00032 if (camera == 2){camstr=camstr_front;}
00033
00034
00035 char query_series[256]="";
00036 strcat(query_series, fname);
00037 strcat(query_series, camstr);
00038 strcat(query_series, "[");
00039 strcat(query_series, t_orig_str);
00040 strcat(query_series, "-");
00041 strcat(query_series, t0_str);
00042 strcat(query_series, "]");
00043
00044 printf("query series %s\n", query_series);
00045
00046 ff_series=drms_open_records(drms_env,query_series,&status);
00047
00048 if (status == DRMS_SUCCESS && ff_series != NULL && ff_series->n != 0)
00049 {
00050 nRec=ff_series->n;
00051
00052 record_off=ff_series->records[nRec-1];
00053 segin = drms_segment_lookup(record_off, segname);
00054 arr_off = drms_segment_read(segin, segin->info->type, &status);
00055 arr_data=arr_off->data;
00056 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) offpoint[j*nx+i]=arr_data[j*nx+i];
00057
00058 *recnum=record_off->recnum;
00059 *focus = drms_getkey_int(record_off,keyfocusflat,&status);
00060 printf("record number %ld\n", *recnum);
00061
00062 }
00063 else
00064 {
00065 printf("can not find hmi record\n");
00066 return 1;
00067 }
00068
00069 return 0;
00070 }
00071
00072
00073 int get_latest_flat(TIME t_0, int camera, const char *fname, const char *segname, float *offpoint, long long *recnum, int *focus)
00074 {
00075 #include "module_flatfield_const.h"
00076
00077 int status, nRec;
00078 int i,j;
00079 DRMS_RecordSet_t *ff_series;
00080 DRMS_Record_t *record_off;
00081 DRMS_Segment_t *segin = NULL;
00082 DRMS_Array_t *arr_off;
00083 float *arr_data;
00084
00085 char t_orig_str[256]="";
00086 sprint_ut(t_orig_str, 0.0);
00087
00088 char t0_str[256]="";
00089 sprint_ut(t0_str, t_0);
00090
00091 char *camstr_side="[1]";
00092 char *camstr_front="[2]";
00093
00094 char *camstr;
00095 if (camera == 1){camstr=camstr_side;}
00096 if (camera == 2){camstr=camstr_front;}
00097
00098
00099 char query_series[256]="";
00100 strcat(query_series, fname);
00101 strcat(query_series, camstr);
00102 strcat(query_series, "[");
00103 strcat(query_series, t_orig_str);
00104 strcat(query_series, "-");
00105 strcat(query_series, t0_str);
00106 strcat(query_series, "][?PZTFLAG=1?]");
00107
00108 printf("query series %s\n", query_series);
00109
00110 ff_series=drms_open_records(drms_env,query_series,&status);
00111
00112 if (status == DRMS_SUCCESS && ff_series != NULL && ff_series->n != 0)
00113 {
00114 nRec=ff_series->n;
00115
00116 record_off=ff_series->records[nRec-1];
00117 segin = drms_segment_lookup(record_off, segname);
00118 arr_off = drms_segment_read(segin, segin->info->type, &status);
00119 arr_data=arr_off->data;
00120 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) offpoint[j*nx+i]=arr_data[j*nx+i];
00121
00122 *recnum=record_off->recnum;
00123 *focus = drms_getkey_int(record_off,keyfocusflat,&status);
00124 printf("record number %ld\n", *recnum);
00125
00126 }
00127 else
00128 {
00129 printf("can not find hmi record\n");
00130 return 1;
00131 }
00132
00133 return 0;
00134 }
00135
00136
00137
00138
00139 int get_latest_bad(TIME t_0, int camera, const char *fname, const char *segname, short *bad, long long *recnum)
00140 {
00141 #include "module_flatfield_const.h"
00142
00143 int status, nRec;
00144 int i,j;
00145 DRMS_RecordSet_t *ff_series;
00146 DRMS_Record_t *record_off;
00147 DRMS_Segment_t *segin = NULL;
00148 DRMS_Array_t *arr_off;
00149 int *bad_data;
00150
00151 char t_orig_str[256]="";
00152 sprint_ut(t_orig_str, 0.0);
00153
00154 char t0_str[256]="";
00155 sprint_ut(t0_str, t_0);
00156
00157 char *camstr_side="[1]";
00158 char *camstr_front="[2]";
00159
00160 char *camstr;
00161 if (camera == 1){camstr=camstr_side;}
00162 if (camera == 2){camstr=camstr_front;}
00163
00164
00165 char query_series[256]="";
00166 strcat(query_series, fname);
00167 strcat(query_series, camstr);
00168 strcat(query_series, "[");
00169 strcat(query_series, t_orig_str);
00170 strcat(query_series, "-");
00171 strcat(query_series, t0_str);
00172 strcat(query_series, "]");
00173
00174 printf("query series %s\n", query_series);
00175
00176 ff_series=drms_open_records(drms_env,query_series,&status);
00177
00178 if (status == DRMS_SUCCESS && ff_series != NULL && ff_series->n != 0)
00179 {
00180 nRec=ff_series->n;
00181
00182 record_off=ff_series->records[nRec-1];
00183 segin = drms_segment_lookup(record_off, segname);
00184 arr_off = drms_segment_read(segin, segin->info->type, &status);
00185
00186 for (i=0; i<nx; ++i) for (j=0; j<ny; ++j) bad[j*nx+i]=1;
00187 int nelem=arr_off->axis[0];
00188
00189
00190 if (nelem > 0)
00191 {
00192 bad_data=arr_off->data;
00193 for (i=0; i<nelem; ++i) bad[bad_data[i]]=0;
00194 }
00195
00196 *recnum=record_off->recnum;
00197 printf("bad pix record number %ld\n", *recnum);
00198
00199 }
00200 else
00201 {
00202 printf("can not find hmi record\n");
00203 return 1;
00204 }
00205
00206 return 0;
00207 }
00208
00209
00210 int retrieve_offpoint(char *query, int camera, float *offpoint, long long *recnum, int *focus)
00211 {
00212 #include "module_flatfield_const.h"
00213 DRMS_RecordSet_t *ff;
00214 int status, i, j, cam_id;
00215 DRMS_Record_t *record_off;
00216 DRMS_Segment_t *segin = NULL;
00217 DRMS_Array_t *arr_off;
00218 float *arr_data;
00219
00220 ff = drms_open_records(drms_env,query,&status);
00221 if (status == DRMS_SUCCESS && ff != NULL && ff->n != 0)
00222 {
00223 record_off=ff->records[0];
00224 cam_id = drms_getkey_int(record_off,keycamera,&status);
00225 *focus=drms_getkey_int(record_off,keyfocusflat,&status);
00226
00227 if (cam_id != camera){printf("flatfield record inconsistent with camera\n"); return 1;}
00228
00229 segin = drms_segment_lookup(record_off, segmentname_offpoint);
00230 arr_off = drms_segment_read(segin, segin->info->type, &status);
00231 arr_data=arr_off->data;
00232 *recnum=record_off->recnum;
00233 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) offpoint[j*nx+i]=arr_data[j*nx+i];
00234 }
00235 else
00236 {
00237 printf("can not find reference offpoint flatfield\n");
00238 return 1;
00239 }
00240 return 0;
00241
00242 }
00243
00244
00245
00246 int get_flat(char *query, int camera, float *flatfield, float *offpoint,
00247 short *bad)
00248 {
00249
00250 #include "module_flatfield_const.h"
00251
00252 DRMS_RecordSet_t *ff, *badrecs;
00253 DRMS_Record_t *record_flat;
00254 DRMS_Record_t *reclink_off, *reclink_dark, *reclink_bad;
00255 int status, status_dat;
00256 DRMS_Segment_t *segin = NULL;
00257 DRMS_Array_t *arr_flat;
00258 DRMS_Array_t *arr_offpoint, *arr_bad;
00259 float *arr_data;
00260 int *bad_data;
00261 int i, j;
00262 int cam_id;
00263
00264 for (i=0; i<nx; ++i) for (j=0; j<ny; ++j) bad[j*nx+i]=1;
00265
00266 ff = drms_open_records(drms_env,query,&status);
00267 status_dat=1;
00268
00269 if (status == DRMS_SUCCESS && ff != NULL && ff->n != 0)
00270 {
00271
00272 record_flat=ff->records[0];
00273 segin = drms_segment_lookup(record_flat, segmentname);
00274 arr_flat = drms_segment_read(segin, segin->info->type, &status_dat);
00275 arr_data=arr_flat->data;
00276 }
00277
00278
00279 if (status_dat ==0)
00280 {
00281
00282 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) flatfield[j*nx+i]=arr_data[j*nx+i];
00283
00284
00285 cam_id = drms_getkey_int(record_flat,keycamera,&status);
00286
00287 if (cam_id != camera){printf("flatfield record inconsistent with camera\n"); return 1;}
00288
00289 reclink_off=drms_link_follow(record_flat, linkoff, &status);;
00290 if (reclink_off == NULL){printf("can not follow link to offpoint flatfield\n"); return 1;}
00291
00292
00293 reclink_dark=drms_link_follow(record_flat, linkdark, &status);
00294 if (reclink_dark == NULL){printf("can not follow link to dark image\n"); return 1;}
00295
00296
00297
00298 reclink_bad=drms_link_follow(record_flat, linkbad, &status);
00299 if (reclink_bad == NULL){printf("can not follow link to bad pixel list\n"); return 1;}
00300
00301
00302
00303
00304
00305
00306 segin = drms_segment_lookup(reclink_off, segmentname_offpoint);
00307 arr_offpoint= drms_segment_read(segin, segin->info->type, &status);
00308
00309 arr_data=arr_offpoint->data;
00310 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) offpoint[j*nx+i]=arr_data[j*nx+i];
00311
00312
00313
00314
00315 segin = drms_segment_lookup(reclink_bad, segmentname_badpix);
00316 arr_bad= drms_segment_read(segin, segin->info->type, &status);
00317 int nelem=arr_bad->axis[0];
00318 bad_data=arr_bad->data;
00319
00320 if (nelem > 0)
00321 {
00322 for (i=0; i<nelem; ++i) bad[bad_data[i]]=0;
00323 }
00324
00325 return 0;
00326 }
00327 else
00328 {
00329 printf("Flatfield %s not found\n", query);
00330 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) flatfield[j*nx+i]=1.0;
00331 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) offpoint[j*nx+i]=1.0;
00332
00333
00334 int *bad_data;
00335
00336 char query_bad[256]={""};
00337 strcat(query_bad,filename_badpix);
00338 strcat(query_bad,"[");
00339 char ffnumb[1]={""};
00340 sprintf(ffnumb, "%1.1d", camera);
00341 strcat(query_bad, ffnumb);
00342 strcat(query_bad, "]");
00343
00344 badrecs=drms_open_records(drms_env,query_bad,&status);
00345 int nRec=badrecs->n;
00346 if (nRec > 0)
00347 {
00348 reclink_bad=badrecs->records[nRec-1];
00349
00350 segin = drms_segment_lookup(reclink_bad, segmentname_badpix);
00351 arr_bad= drms_segment_read(segin, segin->info->type, &status);
00352 int nelem=arr_bad->axis[0];
00353 bad_data=arr_bad->data;
00354
00355 if (nelem > 0)
00356 {
00357 for (i=0; i<nelem; ++i) bad[bad_data[i]]=0;
00358 }
00359
00360
00361 }
00362 else
00363 {
00364 printf("can not find any bad pixel array for camera\n");
00365 return 1;
00366 }
00367 return 1;
00368 }
00369
00370
00371 }
00372
00373
00374
00375 int read_flatfield_series(TIME t_0, int camera, float *flatfield, int *focus, TIME tobs_link[2], long long recnum[6],
00376 struct rotpar *rot_cur)
00377 {
00378 #include "module_flatfield_const.h"
00379 DRMS_RecordSet_t *ff_series;
00380 int i, j, k, nRec, status;
00381 DRMS_Record_t *record_flat_series, *record_clone;
00382 DRMS_Record_t *reclink_off, *reclink_dark, *reclink_bad;
00383 double tm1[4096];
00384 double tm2[4096];
00385 int version[4096];
00386 TIME t_start, t_stop_new;
00387 DRMS_Segment_t *segin = NULL;
00388 DRMS_Array_t *arr;
00389 float *arr_data;
00390
00391
00392
00393 char *camstr_side="[1]";
00394 char *camstr_front="[2]";
00395
00396 char *camstr;
00397 if (camera == 1){camstr=camstr_side;}
00398 if (camera == 2){camstr=camstr_front;}
00399
00400
00401 char t_orig_str[256]="";
00402 sprint_ut(t_orig_str, t_0-30.0*24.0*60.0*60.0);
00403
00404 char t0_str[256]="";
00405 sprint_ut(t0_str, t_0);
00406
00407 char query_series[256]="";
00408 strcat(query_series, filename_flatfield_series);
00409 strcat(query_series, camstr);
00410 strcat(query_series, "[");
00411 strcat(query_series, t_orig_str);
00412 strcat(query_series, "-");
00413 strcat(query_series, t0_str);
00414 strcat(query_series, "]");
00415
00416 printf("query series %s\n", query_series);
00417
00418 ff_series=drms_open_records(drms_env,query_series,&status);
00419
00420 if (status == DRMS_SUCCESS && ff_series != NULL && ff_series->n != 0 && ff_series->n < 4096)
00421 {
00422 nRec=ff_series->n;
00423
00424 for (k=0; k<nRec; ++k)
00425 {
00426 record_flat_series=ff_series->records[k];
00427 tm1[k]=drms_getkey_time(record_flat_series, keytstart,&status);
00428 tm2[k]=drms_getkey_time(record_flat_series, keytstop,&status);
00429 version[k]=drms_getkey_int(record_flat_series,keyversion,&status);
00430 }
00431 }
00432 else
00433 {
00434 printf("no flatfield series found\n");
00435 return 1;
00436 }
00437
00438
00439 for (k=0; k< nRec; ++k)
00440 {
00441 if (tm1[k] <= t_0)
00442 {
00443 if (tm2[k] > t_0)
00444 {
00445
00446 if (version[k] > 0)
00447 {
00448 t_stop_new=tm2[k];
00449 t_start=tm1[k];
00450 record_clone=ff_series->records[k];
00451 rot_cur->flatfield_version=version[k];
00452 }
00453 else
00454 {
00455 t_stop_new=0.0;
00456 t_start=tm1[k];
00457 record_clone=ff_series->records[k];
00458 rot_cur->flatfield_version=0;
00459
00460 }
00461 }
00462
00463 }
00464
00465 }
00466
00467
00468 segin = drms_segment_lookup(record_clone, segmentname);
00469 arr = drms_segment_read(segin, segin->info->type, &status);
00470 arr_data=arr->data;
00471 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) flatfield[j*nx+i]=arr_data[j*nx+i];
00472
00473
00474 rot_cur->rotbad=drms_getkey_int(record_clone,keynewpix,&status);
00475 rot_cur->rotpairs=drms_getkey_int(record_clone,keynpairs,&status);
00476 rot_cur->rotcadence=drms_getkey_float(record_clone,keycadence,&status);
00477
00478 *focus=drms_getkey_int(record_clone,keyfocusflat,&status);
00479
00480 reclink_off=drms_link_follow(record_clone, linkoff, &status);;
00481 if (reclink_off == NULL){printf("can not follow link to offpoint flatfield\n"); return 1;}
00482 recnum[0]=reclink_off->recnum;
00483
00484 reclink_dark=drms_link_follow(record_clone, linkdark, &status);
00485 if (reclink_dark == NULL){printf("can not follow link to dark image\n"); return 1;}
00486 recnum[1]=reclink_dark->recnum;
00487
00488
00489 reclink_bad=drms_link_follow(record_clone, linkbad, &status);
00490 if (reclink_bad == NULL){printf("can not follow link to bad pixel list\n"); return 1;}
00491 recnum[2]=reclink_bad->recnum;
00492
00493
00494
00495 tobs_link[0]=t_start;
00496 tobs_link[1]= t_stop_new;
00497
00498 return 0;
00499 }
00500
00501
00502 int write_rot_flatfield(char *filename_flatfield, DRMS_Array_t *arrout_new, int camera,
00503 TIME t_0, int focus, struct rotpar rot_new)
00504 {
00505
00506 #include "module_flatfield_const.h"
00507
00508 DRMS_Segment_t *segout = NULL;
00509 DRMS_RecordSet_t *dataout = NULL, *dataout_new=NULL;
00510 DRMS_Record_t *recout = NULL;
00511
00512
00513 int cam_id;
00514 char *camera_str;
00515
00516 TIME t_stop=t_0+24.0*60.0*60.0;
00517
00518 if (camera == 1){ cam_id=1; camera_str=camera_str_side;}
00519 if (camera == 2){ cam_id=2; camera_str=camera_str_front;}
00520
00521
00522
00523
00524 int status, stat;
00525
00526 dataout_new = drms_create_records(drms_env,1,filename_rot_flatfield,DRMS_PERMANENT,&stat);
00527
00528
00529 if (stat != DRMS_SUCCESS)
00530 {
00531 printf("Could not create a record for the series %s\n",filename_flatfield);
00532 return 1;
00533 }
00534 if (stat == DRMS_SUCCESS)
00535 {
00536 printf("Write rotational flatfield\n");
00537
00538 recout = dataout_new->records[0];
00539
00540 status = drms_setkey_time(recout, keytstart, t_0);
00541 status = drms_setkey_int(recout, keycamera, cam_id);
00542
00543 status=drms_setkey_int(recout, keyfocusflat, focus);
00544 status=drms_setkey_time(recout, keytstop, t_stop);
00545 drms_setkey_string(recout, keyinstrument, camera_str);
00546
00547 status=drms_setkey_int(recout, "COPYFLAG", 0);
00548 status=drms_setkey_int(recout, keynewpix, rot_new.rotbad);
00549 status=drms_setkey_int(recout, keynpairs, rot_new.rotpairs);
00550 status=drms_setkey_float(recout, keycadence, rot_new.rotcadence);
00551
00552 drms_keyword_setdate(recout);
00553
00554
00555 segout = drms_segment_lookup(recout, segmentname);
00556 if (segout == NULL){printf("could not find segment\n"); return 1;}
00557
00558
00559
00560
00561
00562 status=drms_segment_write(segout, arrout_new, 0);
00563 if (status != DRMS_SUCCESS){printf("could not write segment\n"); return 1;}
00564
00565 }
00566
00567 if (dataout_new != NULL) status=drms_close_records(dataout_new, DRMS_INSERT_RECORD);
00568
00569 if (status == DRMS_SUCCESS)
00570 {
00571 printf("done\n");
00572 return 0;
00573 }
00574 else
00575 {
00576 return 1;
00577 }
00578
00579 }
00580
00581
00582
00583 int write_flatfields(char *filename_flatfield, DRMS_Array_t *arr_flat, DRMS_Array_t *arrout_new, int camera,
00584 long long recnum[6], TIME tobs_link[2], TIME t_0, int focus, int focusclone,
00585 struct rotpar rot_new,
00586 struct rotpar rot_cur)
00587 {
00588
00589 #include "module_flatfield_const.h"
00590 TIME t_start=tobs_link[0];
00591 TIME t_stop_new=tobs_link[1];
00592
00593 if (t_stop_new == 0.0) t_stop_new=t_0+365.0*24.0*60.0*60.0;
00594
00595
00596 long long recnum_off=recnum[0];
00597 long long recnum_dark=recnum[1];
00598 long long recnum_bad=recnum[2];
00599 long long recnum2_off=recnum[3];
00600 long long recnum2_dark=recnum[4];
00601 long long recnum2_bad=recnum[5];
00602
00603
00604 DRMS_Segment_t *segout = NULL;
00605 DRMS_RecordSet_t *dataout = NULL, *dataout_new=NULL;
00606 DRMS_Record_t *recout = NULL;
00607
00608
00609 int cam_id;
00610 char *camera_str;
00611
00612 if (camera == 1){ cam_id=1; camera_str=camera_str_side;}
00613 if (camera == 2){ cam_id=2; camera_str=camera_str_front;}
00614
00615
00616
00617
00618 int status, stat;
00619
00620
00621
00622 int flatvers, flatvers_back, flatvers_forward;
00623
00627
00628
00629
00630
00631 flatvers=rot_cur.flatfield_version;
00632 if (flatvers == 0){flatvers_back=1; flatvers_forward=0;}
00633 if (flatvers > 0){flatvers_back=flatvers; flatvers_forward=flatvers+1;}
00634
00635 dataout = drms_create_records(drms_env,1,filename_flatfield,DRMS_PERMANENT,&stat);
00636
00637 if (stat != DRMS_SUCCESS)
00638 {
00639 printf("Could not create a record for the series %s\n",filename_flatfield);
00640 return 1;
00641 }
00642 if (stat == DRMS_SUCCESS)
00643 {
00644 printf("Overwrite current flatfield with definite T_STOP\n");
00645 printf("Writing a record on the DRMS for the series %s\n",filename_flatfield);
00646
00647
00648
00649 recout = dataout->records[0];
00650
00651 status = drms_setkey_time(recout, keytstart, t_start);
00652 status=drms_setkey_int(recout, keycamera, cam_id);
00653
00654 status=drms_setkey_int(recout, keyfocusflat, focusclone);
00655 status=drms_setkey_time(recout, keytstop, t_0);
00656 status=drms_setkey_int(recout, keyversion, flatvers_back);
00657
00658
00659 status = drms_setkey_string(recout, keyinstrument, camera_str);
00660
00661 status=drms_setkey_int(recout, keynewpix, rot_cur.rotbad);
00662 status=drms_setkey_int(recout, keynpairs, rot_cur.rotpairs);
00663 status=drms_setkey_float(recout, keycadence, rot_cur.rotcadence);
00664 printf("set links\n");
00665
00666 drms_keyword_setdate(recout);
00667
00668
00669
00670 {
00671
00672
00673 status=drms_setlink_static(recout, linkoff, recnum_off);
00674 printf("offpoint: link to recum%ld\n", recnum_off);
00675
00676 }
00677 {
00678
00679
00680 status=drms_setlink_static(recout, linkdark, recnum_dark);
00681 printf("dark: link to recum%ld\n", recnum_dark);
00682
00683 }
00684 {
00685
00686
00687 status=drms_setlink_static(recout, linkbad, recnum_bad);
00688 printf("badpix: link to recum%ld\n", recnum_bad);
00689
00690 }
00691
00692
00693
00694 segout = drms_segment_lookup(recout, segmentname);
00695
00696
00697
00698
00699
00700 status=drms_segment_write(segout, arr_flat, 0);
00701
00702
00703 }
00704
00705
00706 drms_close_record(recout, DRMS_INSERT_RECORD);
00707
00708
00709
00710
00711
00712
00713
00714
00718
00719
00720
00721 dataout_new = drms_create_records(drms_env,1,filename_flatfield,DRMS_PERMANENT,&stat);
00722
00723
00724 if (stat != DRMS_SUCCESS)
00725 {
00726 printf("Could not create a record for the series %s\n",filename_flatfield);
00727 return 1;
00728 }
00729 if (stat == DRMS_SUCCESS)
00730 {
00731 printf("Write new flatfield\n");
00732
00733 recout = dataout_new->records[0];
00734
00735 status = drms_setkey_time(recout, keytstart, t_0);
00736 status = drms_setkey_int(recout, keycamera, cam_id);
00737
00738 status=drms_setkey_int(recout, keyfocusflat, focus);
00739 status=drms_setkey_time(recout, keytstop, t_stop_new);
00740 status=drms_setkey_int(recout, keyversion, flatvers_forward);
00741 drms_setkey_string(recout, keyinstrument, camera_str);
00742
00743 status=drms_setkey_int(recout, keynewpix, rot_new.rotbad);
00744 status=drms_setkey_int(recout, keynpairs, rot_new.rotpairs);
00745 status=drms_setkey_float(recout, keycadence, rot_new.rotcadence);
00746
00747 drms_keyword_setdate(recout);
00748
00749
00750
00751 {
00752
00753 status=drms_setlink_static(recout, linkoff, recnum2_off);
00754
00755 if (status != DRMS_SUCCESS){printf("could not set like to offpoint flatfield\n"); return 1;}
00756 else {printf("offpoint: link to recum%ld\n", recnum2_off);}
00757 }
00758 {
00759
00760 status=drms_setlink_static(recout, linkdark, recnum2_dark);
00761
00762 if (status != DRMS_SUCCESS){printf("could not set like to dark\n"); return 1;}
00763 else {printf("dark: link to recum%ld\n", recnum2_dark);}
00764 }
00765
00766 {
00767
00768 status=drms_setlink_static(recout, linkbad, recnum2_bad);
00769 if (status != DRMS_SUCCESS){printf("could not set like to bad pixel list\n"); return 1;}
00770 else {printf("badpix: link to recum%ld\n", recnum2_bad);}
00771 }
00772
00773
00774 segout = drms_segment_lookup(recout, segmentname);
00775 if (segout == NULL){printf("could not find segment\n"); return 1;}
00776
00777
00778
00779
00780 status=drms_segment_write(segout, arrout_new, 0);
00781 if (status != DRMS_SUCCESS){printf("could not write segment\n"); return 1;}
00782
00783 }
00784
00785
00786
00787
00788 status=drms_close_records(dataout_new, DRMS_INSERT_RECORD);
00789
00790 if (status == DRMS_SUCCESS)
00791 {
00792 printf("done\n");
00793 return 0;
00794 }
00795 else
00796 {
00797 return 1;
00798 }
00799
00800 }
00801
00802
00803
00804
00805 int write_flatfield_fid(DRMS_Array_t *arrout_new, int camera, long long recnum_offpoint,
00806 TIME t_0, int focus,int fid,
00807 struct rotpar rot_new)
00808 {
00809
00810 #include "module_flatfield_const.h"
00811
00812
00813
00814 DRMS_Segment_t *segout = NULL;
00815 DRMS_RecordSet_t *dataout = NULL, *dataout_new=NULL;
00816 DRMS_Record_t *recout = NULL;
00817
00818
00819 int cam_id;
00820 char *camera_str;
00821
00822 if (camera == 1){ cam_id=1; camera_str=camera_str_side;}
00823 if (camera == 2){ cam_id=2; camera_str=camera_str_front;}
00824
00825
00826 int status, stat;
00827
00828
00829 const int FLAG_PRELIMINARY=0;
00830 const int FLAG_FINAL=1;
00831
00832 char query_flat[256]="";
00833 strcat(query_flat, filename_offpoint);
00834 strcat(query_flat, "[:#");
00835 char ffnumb[10]={""};
00836 sprintf(ffnumb, "%1.1ld", recnum_offpoint);
00837 strcat(query_flat, ffnumb);
00838 strcat(query_flat, "]");
00839
00840 printf("query offpoint flatfield: %s", query_flat);
00841
00845
00846
00847
00848 dataout_new = drms_create_records(drms_env,1,filename_flatfield_fid,DRMS_PERMANENT,&stat);
00849
00850
00851 if (stat != DRMS_SUCCESS)
00852 {
00853 printf("Could not create a record for the series %s\n",filename_flatfield_fid);
00854 return 1;
00855 }
00856 if (stat == DRMS_SUCCESS)
00857 {
00858 printf("Write new flatfield with fid %d\n", fid);
00859
00860 recout = dataout_new->records[0];
00861
00862 status=drms_setkey_time(recout, keyday, t_0);
00863
00864 status=drms_setkey_int(recout, keycamera, cam_id);
00865 status=drms_setkey_int(recout, fidkey, fid);
00866
00867
00868 status=drms_setkey_int(recout, keyfocusflat, focus);
00869
00870
00871 drms_setkey_string(recout, keyinstrument, camera_str);
00872
00873
00874 status=drms_setkey_int(recout, keynpairs, rot_new.rotpairs);
00875 status=drms_setkey_float(recout, keycadence, rot_new.rotcadence);
00876
00877 status=drms_setkey_string(recout, querykey, query_flat);
00878
00879
00880 drms_keyword_setdate(recout);
00881
00882
00883
00884 segout = drms_segment_lookup(recout, segmentname);
00885 if (segout == NULL){printf("could not find segment\n"); return 1;}
00886
00887
00888
00889
00890
00891 status=drms_segment_write(segout, arrout_new, 0);
00892 if (status != DRMS_SUCCESS){printf("could not write segment\n"); return 1;}
00893 }
00894
00895
00896
00897
00898 status=drms_close_records(dataout_new, DRMS_INSERT_RECORD);
00899
00900 if (status == DRMS_SUCCESS)
00901 {
00902 printf("done\n");
00903 return 0;
00904 }
00905 else
00906 {
00907 return 1;
00908 }
00909 }
00910
00911
00912
00913