00001 #ident "$Header: /home/cvsuser/cvsroot/JSOC/proj/lev0/apps/write_hk_to_drms_iris.c,v 1.3 2015/06/18 17:14:45 jps Exp $"
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "jsoc_main.h"
00011 #include "packets.h"
00012 #include "decode_hk_vcdu.h"
00013 #include "decode_hk.h"
00014 #include "write_hk_to_drms.h"
00015 #include "printk.h"
00016 #include "timeio.h"
00017
00018 int write_hk_to_drms(DRMS_Record_t *record, CCSDS_Packet_t **ccsds_pkt);
00019 static TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss);
00020 char *get_packet_version_number( HK_Keyword_t *kw);
00021 char *lookup_data_series_name(CCSDS_Packet_t *ccsds_ptr, JSOC_Version_Map_t **jmap, char *packet_version_number);
00022 void load_map_data(int apid, JSOC_Version_Map_t *jm, char* packet_version_number);
00023 int find_data_series_name(CCSDS_Packet_t *ccsds_ptr, JSOC_Version_Map_t *jm, char *dsn);
00024 int get_pkt_time_from_timecodes(HK_Keyword_t *hk, TIME *ptime);
00025 int check_for_apid(int apid, JSOC_Version_Map_t *jm);
00026 void free_jsvn_map( JSOC_Version_Map_t *top_jm);
00027 char *get_data_packet_name(int apid ) ;
00028 char *get_hk_source(CCSDS_Packet_t *ccsds_ptr, TIME *pt);
00029 int check_env_variable();
00030 char *get_lookup_filename(int apid ) ;
00031 char * make_strupr (char *in_string);
00032 int check_hk_record_exists(char* ds_name, HK_Keyword_t *kw, int apid);
00033 short create_low_range(HK_Keyword_t *kw_head, long int *low_range);
00034 short check_ds_cache_init(char *dsname);
00035 short check_curr_map_file(char* pv_num, short* current_map_flag);
00036 int get_query_range(int range_type, HK_Keyword_t *kw, char *qr);
00037 int initialize_timecodes_cache(char* ds_name, HK_Keyword_t *kw, int apid);
00038 int check_hk_record_within_time_range( HK_Keyword_t *kw);
00039
00040
00041 extern int get_day_from_pkttime(double tc_sec);
00042 extern int get_month_from_pkttime(double tc_sec);
00043 extern int get_yr_from_pkttime(double tc_sec);
00044 extern int check_for_sdo_apid(int apid);
00045 extern int get_day_from_pkttime(double ptime);
00046 extern int get_hour_from_pkttime(double ptime);
00047 extern int get_month_from_pkttime(double ptime);
00048 extern int get_yr_from_pkttime(double ptime);
00049
00050
00051
00052 extern char ispquery[];
00053 extern DRMS_RecordSet_t *RSISP, *RSISPTO;
00054 extern unsigned int fsnx;
00055 extern int verbose;
00056
00057 HK_DSN_RANGE_t *dsr_head=NULL;
00058 char IRIS_ISP_DS[80];
00059 static DRMS_RecordSet_t *rs = 0;
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 int write_hk_to_drms(DRMS_Record_t *record, CCSDS_Packet_t **ccsds_pkt)
00078 {
00079
00080 DRMS_Type_t keytype;
00081 DRMS_Type_Value_t key_anyval;
00082 DRMS_Record_t *rec;
00083 TIME pkt_time;
00084 char keyname[HK_LEV0_MAX_LONG_KW_NAME];
00085 char query[HK_LEV0_MAX_DSNAME_STR];
00086 char pkt_ver_num[HK_LEV0_MAX_PVN_STR];
00087 char *pvn, *pvtmp;
00088 char *directory ;
00089 char *suffix_filename ;
00090 int status, env_status;
00091 int rec_alreadycreated_flag;
00092 int ck_rec_status;
00093 int ck_rwtr_status;
00094 static short haveNotChecked=1;
00095
00096
00097
00098 CCSDS_Packet_t *ccsds;
00099 HK_Keyword_t *kw;
00100
00101
00102
00103 static JSOC_Version_Map_t *jmap;
00104
00105
00106 TIME *ptime= &pkt_time;
00107 ccsds=*ccsds_pkt;
00108 pvn = pkt_ver_num;
00109
00110
00111
00112 if(record)
00113 {
00114
00115 rec_alreadycreated_flag = 1;
00116
00117 #ifdef DEBUG_WRITE_HK_TO_DRMS
00118 printkerr("DEBUG:Message at %s, line %d: Preparing to write to DRMS "
00119 " for Level 0 Series.\n", __FILE__,__LINE__);
00120 #endif
00121 }
00122 else
00123 {
00124
00125 #ifdef DEBUG_WRITE_HK_TO_DRMS
00126 printkerr("DEBUG:Message at %s, line %d: Preparing to write to DRMS "
00127 " for Level 0 BY APID Series.\n", __FILE__,__LINE__);
00128 #endif
00129
00130 rec_alreadycreated_flag=0;
00131 }
00132
00133
00134 if(haveNotChecked )
00135 {
00136 if(((env_status = check_env_variable()) != 1))
00137 {
00138
00139 return (env_status);
00140 }
00141 else
00142 {
00143
00144 haveNotChecked=0;
00145 }
00146 }
00147
00148
00149 directory = (char *)getenv("HK_JSVNMAP_DIRECTORY");
00150 suffix_filename= (char *)getenv("HK_SUFFIX_JSVNMAP_FILENAME");
00151 if(suffix_filename == NULL)
00152 {
00153 printkerr("ERROR at %s, line %d: Set environment variable <HK_SUFFIX_JSVNMAP_FILENAME>\n",
00154 __FILE__,__LINE__);
00155 return (ERROR_HK_ENVIRONMENT_VARS_NOT_SET);
00156 }
00157 if(directory == NULL)
00158 {
00159 printkerr("ERROR at %s, line %d: Set environment variable <HK_JSVNMAP_DIRECTORY>\n",
00160 __FILE__,__LINE__);
00161 return (ERROR_HK_ENVIRONMENT_VARS_NOT_SET) ;
00162 }
00163
00164
00165 while (ccsds)
00166 {
00167
00168 if (rec_alreadycreated_flag)
00169 {
00170
00171 rec= record;
00172
00173 drms_setkey_double(rec, "DATE", CURRENT_SYSTEM_TIME);
00174 pvtmp = get_packet_version_number(ccsds->keywords);
00175 if(pvtmp) strcpy(pvn, pvtmp);
00176
00177
00178
00179 keytype= DRMS_TYPE_STRING;
00180 strcpy(keyname, "ISPSNAME");
00181
00182
00183 key_anyval.string_val = (char *)malloc(sizeof(char) * 100);
00184
00185
00186 strcpy(key_anyval.string_val, lookup_data_series_name(ccsds,&jmap,pvn));
00187 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00188
00189
00190 free (key_anyval.string_val);
00191 }
00192 else
00193 {
00194
00195 pvtmp = get_packet_version_number(ccsds->keywords);
00196 if(pvtmp) strcpy(pvn, pvtmp);
00197
00198
00199
00200 strcpy(query , lookup_data_series_name(ccsds, &jmap, pvn));
00201 #ifdef DEBUG_WRITE_HK_TO_DRMS
00202 printkerr("DEBUG:Message at %s, line %d: Writing to DRMS data"
00203 " series <%s> for apid <%d>\n", __FILE__,__LINE__, query, ccsds->apid);
00204 #endif
00205
00206
00207
00208 kw = ccsds->keywords;
00209
00210 ck_rwtr_status = check_hk_record_within_time_range(kw);
00211 if (ck_rwtr_status == 1)
00212 {
00213
00214
00215 kw = ccsds->keywords;
00216
00217 ck_rec_status = 0;
00218 #ifdef DEBUG_WRITE_HK_TO_DRMS
00219 printkerr("DEBUG:Message at %s, line %d: After check if record exists. "
00220 "Status:<%d> where 0 means write record and 1 means skip "
00221 "write of record.\n", __FILE__,__LINE__, ck_rec_status);
00222 #endif
00223 }
00224 else
00225 {
00226
00227 ck_rec_status= 1;
00228 }
00229
00230
00231 if (ck_rwtr_status == 0)
00232 {
00233 ;
00234 #ifdef DEBUG_WRITE_HK_TO_DRMS
00235 printkerr("DEBUG:Message at %s, line %d: Check if record within time range. "
00236 " Status:<%d>, so skip writing record\n",
00237 __FILE__,__LINE__, ck_rwtr_status);
00238 #endif
00239
00240
00241 status=SUCCESS_HK_SKIP_WTD_REC_TIME_OOR;
00242
00243
00244 ccsds= ccsds->next;
00245 continue;
00246 }
00247 else if (ck_rec_status == 1)
00248 {
00249 ;
00250 #ifdef DEBUG_WRITE_HK_TO_DRMS
00251 printkerr("DEBUG:Message at %s, line %d: Check if record exists. "
00252 " Status:<%d>, so skip writing record\n",
00253 __FILE__,__LINE__, ck_rec_status);
00254 #else
00255 #endif
00256
00257 status=SUCCESS_HK_SKIP_WTD_REC_EXISTS;
00258
00259
00260 ccsds= ccsds->next;
00261 continue;
00262 }
00263 else
00264 {
00265 #ifdef DEBUG_WRITE_HK_TO_DRMS
00266 printkerr("DEBUG:Message at %s, line %d: Check if record exists. "
00267 "Status:<%d>, Check if record within range.Status:<%d> "
00268 "so writing record\n", __FILE__,__LINE__, ck_rec_status,ck_rwtr_status);
00269 #endif
00270 }
00271
00272
00273 RSISP = rs;
00274 sprintf(ispquery, "%s", query);
00275 rs = drms_create_records( drms_env, 1, query, DRMS_PERMANENT, &status);
00276 RSISPTO = rs;
00277 if (status < 0)
00278 {
00279
00280 rs = 0;
00281
00282
00283
00284
00285 printkerr("ERROR at %s, line %d: Could not create record for data "
00286 "series <%s> because of error code <%d>. Check code in "
00287 "drms_statuscodes.h file. Skipping write to drms of "
00288 "packet data to record in drms data series.\n",
00289 __FILE__ , __LINE__ , query,status);
00290 ccsds= ccsds->next;
00291 continue;
00292 }
00293 else
00294 {
00295
00296 rec = rs->records[0];
00297
00298 drms_setkey_double(rec, "DATE", CURRENT_SYSTEM_TIME);
00299 }
00300 }
00301
00302
00303 kw = ccsds->keywords;
00304 if (!get_pkt_time_from_timecodes(kw, ptime))
00305 {
00306
00307 if (rec_alreadycreated_flag)
00308 {
00309 ;
00310 }
00311 else
00312 {
00313
00314
00315
00316
00317
00318
00319 }
00320
00321 printkerr("ERROR at %s, line %d: Could not set PACKET_TIME keyword. "
00322 "Because the TIME CODE values were not found in keywords. "
00323 "Exiting write to drms of data series <%s> and closing record.\n",
00324 __FILE__ , __LINE__ , query);
00325 return (ERROR_HK_FAILED_TO_FIND_TIMECODES);
00326 }
00327 else
00328 {
00329
00330
00331
00332
00333
00334
00335 keytype= DRMS_TYPE_TIME;
00336 strcpy(keyname, "PACKET_TIME");
00337
00338 key_anyval.time_val= *ptime;
00339
00340 if (rec_alreadycreated_flag)
00341 {
00342 status = drms_setkey(rec, "ISPPKTIM", keytype, &key_anyval);
00343 }
00344 else
00345 {
00346 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00347 }
00348 }
00349
00350
00351
00352
00353 keytype= DRMS_TYPE_STRING;
00354 if (rec_alreadycreated_flag)
00355 {
00356
00357 strcpy(keyname, "ISPPKTVN");
00358 }
00359 else
00360 {
00361
00362 strcpy(keyname, "PACKET_VERSION_NUMBER");
00363 }
00364
00365 key_anyval.string_val = (char *)malloc(sizeof(char) * 100);
00366
00367 strcpy(key_anyval.string_val, pvn);
00368 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00369
00370 free (key_anyval.string_val);
00371
00372
00373
00374
00375 keytype= DRMS_TYPE_STRING;
00376 if (rec_alreadycreated_flag)
00377 {
00378 ;
00379 }
00380 else
00381 {
00382
00383 strcpy(keyname, "HK_SOURCE");
00384
00385 key_anyval.string_val = (char *)malloc(sizeof(char) * 100);
00386
00387 strcpy(key_anyval.string_val, get_hk_source(ccsds, ptime));
00388 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00389
00390 free (key_anyval.string_val);
00391 }
00392
00393
00394 kw = ccsds->keywords;
00395 while (kw)
00396 {
00397 if(kw->eng_type == KW_TYPE_UINT8)
00398 {
00399 if(rec_alreadycreated_flag)
00400 {
00401 strcpy(keyname, kw->fitsname);
00402 }
00403 else
00404 {
00405 strcpy(keyname, kw->name);
00406 }
00407
00408
00409 keytype= DRMS_TYPE_SHORT;
00410
00411
00412 key_anyval.short_val = (int16_t)kw->eng_value.uint8_val;
00414 if (verbose) printf("KEYWORD: %s %d\n", keyname, key_anyval.short_val);
00415 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00416
00417 }
00418 else if(kw->eng_type == KW_TYPE_UINT16)
00419 {
00420 if(rec_alreadycreated_flag)
00421 {
00422 strcpy(keyname, kw->fitsname);
00423 }
00424 else
00425 {
00426 strcpy(keyname, kw->name);
00427 }
00428
00429
00430 keytype= DRMS_TYPE_INT;
00431
00432
00433 key_anyval.int_val = (int32_t)kw->eng_value.uint16_val;
00435 if (verbose) printf("KEYWORD: %s %d\n", keyname, key_anyval.int_val);
00436 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00437
00438 }
00439 else if(kw->eng_type == KW_TYPE_UINT32)
00440 {
00441 if(rec_alreadycreated_flag)
00442 {
00443 strcpy(keyname, kw->fitsname);
00444 }
00445 else
00446 {
00447 strcpy(keyname, kw->name);
00448 }
00449
00450
00451 keytype= DRMS_TYPE_LONGLONG;
00452
00453
00454 key_anyval.longlong_val = (int64_t)kw->eng_value.uint32_val;
00456 if (verbose) printf("KEYWORD: %s %ld\n", keyname, key_anyval.longlong_val);
00457 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00458
00459 }
00460 else if(kw->eng_type == KW_TYPE_INT8)
00461 {
00462 if(rec_alreadycreated_flag)
00463 {
00464 strcpy(keyname, kw->fitsname);
00465 }
00466 else
00467 {
00468 strcpy(keyname, kw->name);
00469 }
00470
00471
00472 keytype= DRMS_TYPE_SHORT;
00473 key_anyval.short_val = (int16_t)kw->eng_value.int8_val;
00475 if (verbose) printf("KEYWORD: %s %d\n", keyname, key_anyval.short_val);
00476 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00477
00478 }
00479 else if(kw->eng_type == KW_TYPE_DOUBLE)
00480 {
00481 if(rec_alreadycreated_flag)
00482 {
00483 strcpy(keyname, kw->fitsname);
00484 }
00485 else
00486 {
00487 strcpy(keyname, kw->name);
00488 }
00489
00490
00491 keytype= DRMS_TYPE_DOUBLE;
00492 key_anyval.double_val = kw->eng_value.double_val;
00494 if (verbose) printf("KEYWORD: %s %g\n", keyname, key_anyval.double_val);
00495 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00496
00497 }
00498 else if(kw->eng_type == KW_TYPE_INT16)
00499 {
00500 if(rec_alreadycreated_flag)
00501 {
00502 strcpy(keyname, kw->fitsname);
00503 }
00504 else
00505 {
00506 strcpy(keyname, kw->name);
00507 }
00508
00509
00510 keytype= DRMS_TYPE_SHORT;
00511 key_anyval.short_val = kw->eng_value.int16_val;
00513 if (verbose) printf("KEYWORD: %s %d\n", keyname, key_anyval.short_val);
00514 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00515
00516 }
00517 else if(kw->eng_type == KW_TYPE_INT32)
00518 {
00519 if(rec_alreadycreated_flag)
00520 {
00521 strcpy(keyname, kw->fitsname);
00522 }
00523 else
00524 {
00525 strcpy(keyname, kw->name);
00526 }
00527
00528 keytype= DRMS_TYPE_INT;
00529 key_anyval.int_val= kw->eng_value.int32_val;
00531 if (verbose) printf("KEYWORD: %s %d\n", keyname, key_anyval.int_val);
00532 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00533
00534 }
00535 else if(kw->eng_type == KW_TYPE_STRING)
00536 {
00537 if(rec_alreadycreated_flag)
00538 {
00539 strcpy(keyname, kw->fitsname);
00540 }
00541 else
00542 {
00543 strcpy(keyname, kw->name);
00544 }
00545
00546
00547 keytype= DRMS_TYPE_STRING;
00548
00549
00550 key_anyval.string_val = (char *)malloc(sizeof(char) * 100);
00551 strcpy(key_anyval.string_val, kw->eng_value.string_val);
00553 if (verbose) printf("KEYWORD: %s %s\n", keyname, key_anyval.string_val);
00554 status = drms_setkey(rec, keyname, keytype, &key_anyval);
00555 free(key_anyval.string_val);
00556
00557 }
00558 else
00559 {
00560 printkerr("Warning at %s, line %d: Found Unknown KW_TYPE for this keyword: "
00561 "<%d>. Check if new KW_TYPE has been created in HKPDF files "
00562 "Skipping recording keyword in structure. Keyword is <%s>.",
00563 __FILE__,__LINE__, (int)kw->eng_type, kw->name);
00564 }
00566
00567
00568
00569
00570
00571 #ifdef DEBUG_WRITE_HK_TO_DRMS_MORE
00572
00573 if (status)
00574 {
00575
00576 if (rec_alreadycreated_flag)
00577 {
00578 printkerr("DEBUG Messsage at %s, line %d: Warning-cannot setkey "
00579 "in drms record when writing to <Level 0 series>. "
00580 "Return status from drms_setkey was <%d>. "
00581 "For keyword <%s>\n", __FILE__,__LINE__, status, keyname);
00582 }
00583 else
00584 {
00585 printkerr("DEBUG Messsage at %s, line %d: Warning-cannot setkey "
00586 "in drms record when writing to <Level 0 By APID series>. "
00587 "Return status from drms_setkey was <%d>. "
00588 "For keyword <%s>\n", __FILE__,__LINE__, status, keyname);
00589 }
00590 }
00591 #else
00592 #endif
00593
00594
00595 kw=kw->next;
00596
00597 }
00598
00599
00600 if (rec_alreadycreated_flag)
00601 {
00602
00603 }
00604 else
00605 {
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616 }
00617
00618
00619
00620 ccsds= ccsds->next;
00621 #ifdef DEBUG_WRITE_HK_TO_DRMS
00622 printkerr("DEBUG:Message at %s, line %d: Complete CCSDS packet. "
00623 "Do next CCSDS packet.\n", __FILE__,__LINE__);
00624 #else
00625 #endif
00626 if (rec_alreadycreated_flag)
00627 {
00628
00629 if (ccsds)
00630 {
00631 printkerr("ERROR at %s, line %d: Getting more than one node in CCSDS_Packet_t "
00632 "link list for Lev0.1 case where one record is passed. "
00633 "To handle not to pass DRMS_RECORD_SET_T as argument.\n",
00634 __FILE__ , __LINE__ );
00635 break;
00636 }
00637 }
00638 }
00639 return status;
00640 }
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650 char *get_packet_version_number( HK_Keyword_t *kw)
00651 {
00652
00653 char vn[HK_LEV0_MAX_PVN_STR];
00654 char *ptr_vn;
00655
00656
00657 ptr_vn = vn;
00658
00659 while (kw)
00660 {
00661
00662 if(strstr(kw->name, "APID56_APID_VERSION"))
00663 {
00664 sprintf(ptr_vn, "%03d.%03d", kw->eng_value.uint16_val >> 8 & 0x00FF,
00665 kw->eng_value.uint16_val & 0x00FF );
00666 strcat(ptr_vn,"\0");
00667 return (ptr_vn);
00668 }
00669 kw= kw->next;
00670 }
00671 return NULL;
00672 }
00673
00674
00675
00676
00677
00678
00679
00680 char *get_data_packet_name(int apid )
00681 {
00682
00683 char *dpn;
00684 char data_packet_name[HK_LEV0_MAX_PACKET_NAME];
00685
00686
00687 dpn = data_packet_name;
00688
00689
00690
00691 switch(apid)
00692 {
00693
00694
00695
00696 case HK_LR_HMI_ISP:
00697 case HK_HSB_HMI_ISP_1:
00698 case HK_HSB_HMI_ISP_2:
00699
00700
00701 strcpy(dpn, HK_HMI_PKT_NAME_ISP);
00702 break;
00703
00704 case HK_HSB_IRIS_ISP:
00705
00706 strcpy(dpn, HK_HSB_IRIS_PKT_NAME_ISP);
00707 break;
00708
00709
00710
00711
00712 case HK_LR_AIA_ISP:
00713 case HK_HSB_AIA_ISP_1:
00714 case HK_HSB_AIA_ISP_2:
00715
00716 strcpy(dpn, HK_AIA_PKT_NAME_ISP);
00717 break;
00718
00719
00720
00721
00722 case HK_LR_HMI_SEQ:
00723 case HK_HSB_HMI_SEQ_1:
00724 case HK_HSB_HMI_SEQ_2:
00725
00726 strcpy(dpn, HK_HMI_PKT_NAME_SEQ);
00727 break;
00728
00729
00730
00731
00732 case HK_LR_AIA_SEQ:
00733 case HK_HSB_AIA_SEQ_1:
00734 case HK_HSB_AIA_SEQ_2:
00735
00736 strcpy(dpn, HK_AIA_PKT_NAME_SEQ);
00737 break;
00738
00739
00740
00741
00742 case HK_LR_HMI_OBT:
00743 case HK_HSB_HMI_OBT_1:
00744 case HK_HSB_HMI_OBT_2:
00745
00746 strcpy(dpn, HK_HMI_PKT_NAME_OBT);
00747 break;
00748
00749
00750
00751
00752 case HK_LR_AIA_OBT:
00753 case HK_HSB_AIA_OBT_1:
00754 case HK_HSB_AIA_OBT_2:
00755
00756 strcpy(dpn, HK_AIA_PKT_NAME_OBT);
00757 break;
00758
00759
00760
00761
00762 case HK_LR_SDO_ASD:
00763
00764 strcpy(dpn, HK_SDO_PKT_NAME_ASD);
00765 break;
00766
00767
00768
00769
00770 default:
00771
00772 sprintf(dpn,"%04d", apid);
00773 break;
00774 }
00775
00776 return( dpn);
00777 }
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789 char *lookup_data_series_name(CCSDS_Packet_t *ccsds_ptr, JSOC_Version_Map_t **jmap_ptr, char *pkt_ver_num)
00790 {
00792
00793 sprintf(IRIS_ISP_DS, "%s", getenv("HK_ISP_IRIS_DSNAME"));
00794 return IRIS_ISP_DS;
00795
00796
00797 JSOC_Version_Map_t *jm, *tmp_jm, *last_jm, *prev_jm;
00798 char data_series_name[HK_LEV0_MAX_DSNAME_STR];
00799 char *dsn;
00800 int apid;
00801 static short curr_mf_flag=0;
00802 short new_mf_flag;
00803
00804
00805 dsn = data_series_name;
00806 jm = *jmap_ptr;
00807
00808
00809 apid = ccsds_ptr->apid;
00810
00811
00812 new_mf_flag = check_curr_map_file(pkt_ver_num, &curr_mf_flag);
00813 #ifdef DEBUG_WRITE_HK_TO_DRMS
00814 printkerr("DEBUG:Message:newmfflag:%d curr:%d \n",new_mf_flag,curr_mf_flag);
00815 #else
00816 #endif
00817
00818
00819 if (!jm || new_mf_flag)
00820 {
00821
00822 jm = (JSOC_Version_Map_t *)malloc(sizeof(JSOC_Version_Map_t));
00823 if (!jm)
00824 {
00825 printkerr("ERROR at %s, line %d: Cannot malloc space!\n", __FILE__ , __LINE__ );
00826 }
00827 *jmap_ptr=jm;
00828 jm->apid=(short)apid;
00829 jm->next=NULL;
00830
00831
00832
00833 load_map_data(apid, jm, pkt_ver_num);
00834
00835
00836
00837 (void)find_data_series_name(ccsds_ptr, jm, dsn);
00838
00839 }
00840 else
00841 {
00842
00843 (void)find_data_series_name(ccsds_ptr, jm, dsn);
00844
00845
00846
00847 if (!strcmp(dsn, "\0"))
00848 {
00849
00850
00851
00852
00853
00854 if (check_for_apid(ccsds_ptr->apid, jm))
00855 {
00856 printkerr("Warning at %s, line %d: Could not find find "
00857 "packet version number and JSOC version number line for "
00858 "APID: <%d> in Data Map structure. Need to reread files.\n",
00859 __FILE__, __LINE__, ccsds_ptr->apid);
00860
00861
00862
00863
00864 (void)free_jsvn_map(jm);
00865
00866
00867 jm = (JSOC_Version_Map_t *)malloc(sizeof(JSOC_Version_Map_t));
00868 if (!jm)
00869 {
00870 printkerr("ERROR at %s, line %d: Cannot malloc space!\n", __FILE__ , __LINE__ );
00871 }
00872 *jmap_ptr=jm;
00873 jm->apid=(short)apid;
00874 jm->next=NULL;
00875
00876
00877
00878 load_map_data(apid, jm, pkt_ver_num);
00879
00880
00881 (void)find_data_series_name(ccsds_ptr, jm, dsn);
00882 }
00883 else
00884 {
00885
00886
00887
00888 tmp_jm = (JSOC_Version_Map_t *)malloc(sizeof(JSOC_Version_Map_t));
00889 if (!tmp_jm)
00890 {
00891 printkerr("ERROR at %s, line %d: Cannot malloc space!\n", __FILE__ , __LINE__ );
00892 }
00893 tmp_jm->apid= (short)apid;
00894 tmp_jm->next= NULL;
00895
00896
00897 load_map_data(apid, tmp_jm, pkt_ver_num);
00898
00899
00900 for( last_jm= *jmap_ptr; last_jm; prev_jm=last_jm, last_jm=last_jm->next);
00901 prev_jm->next =tmp_jm;
00902
00903
00904 (void)find_data_series_name(ccsds_ptr, jm, dsn);
00905 }
00906
00907 }
00908 }
00909 return dsn;
00910 }
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921 void load_map_data(int apid, JSOC_Version_Map_t *jm, char *pvn)
00922 {
00923
00924 FILE *file_ptr;
00925 Map_Data_t *tmp_dm, *prev_dm;
00926 char directory_filename[HK_LEV0_MAX_FILE_NAME];
00927 char dirname[HK_LEV0_MAX_FILE_NAME];
00928 char fn[HK_LEV0_MAX_FILE_NAME];
00929 char *filename;
00930 char *directory;
00931 char sn[HK_LEV0_MAX_FILE_NAME];
00932 char *didn;
00933 char *suffix_filename;
00934 char line[HK_LEV0_MAXLINE_IN_FILE];
00935 char *pn;
00936 int wn, dn;
00937 int curr_pvn_wn,curr_pvn_dn;
00938
00939
00940 filename=fn;
00941 directory=dirname;
00942
00943
00944 suffix_filename=sn;
00945
00946
00947
00948 if(apid <= HK_HSB_HIGHEST_HMI_APID && apid >= HK_HSB_LOWEST_HMI_APID || (apid <= HK_LR_HIGHEST_HMI_APID && apid >= HK_LR_LOWEST_HMI_APID))
00949 {
00950 pn = getenv("HK_LEV0_BY_APID_PROJECT_NAME_HMI");
00951 }
00952 else if((apid <= HK_HSB_HIGHEST_AIA_APID && apid >= HK_HSB_LOWEST_AIA_APID) || (apid <= HK_LR_HIGHEST_AIA_APID && apid >= HK_LR_LOWEST_AIA_APID))
00953 {
00954 pn = getenv("HK_LEV0_BY_APID_PROJECT_NAME_AIA");
00955 }
00956 else if((apid <= HK_LR_HIGHEST_SDO_APID) && (apid >= HK_LR_LOWEST_SDO_APID))
00957 {
00958 pn = getenv("HK_LEV0_BY_APID_PROJECT_NAME_SDO");
00959 }
00960 else
00961 {
00962 printkerr("Warning at %s, line %d: APID is not in range of ",
00963 "HMI, AIA, or SDO. APID: <%d>\n",
00964 __FILE__, __LINE__, apid);
00965 }
00966
00967
00968 didn = getenv("HK_LEV0_BY_APID_DATA_ID_NAME");
00969
00970
00971 directory = (char *)getenv("HK_JSVNMAP_DIRECTORY");
00972 suffix_filename= (char *)getenv("HK_SUFFIX_JSVNMAP_FILENAME");
00973
00974
00975
00976 sscanf(pvn,"%3d.%3d",&curr_pvn_wn, &curr_pvn_dn);
00977
00978
00979 if(curr_pvn_wn >= HK_LEV0_START_MERGED_PVNW && curr_pvn_dn >= HK_LEV0_START_MERGED_PVND)
00980 {
00981 strcpy(filename,get_lookup_filename(apid));
00982 strcat(filename,suffix_filename);
00983 #ifdef DEBUG_WRITE_HK_TO_DRMS
00984 printkerr("DEBUG:Message at %s, line %d: Triggered merge case. "
00985 "Getting JSOC map file with new name: <%s>\n",
00986 __FILE__, __LINE__, filename);
00987 #else
00988 #endif
00989 }
00990 else
00991 {
00992 sprintf(filename,"%4.4d%s",apid,suffix_filename);
00993 #ifdef DEBUG_WRITE_HK_TO_DRMS
00994 printkerr("DEBUG:Message at %s, line %d: Triggered non-merge case. "
00995 "Getting JSOC map file with old name: <%s>\n",
00996 __FILE__, __LINE__, filename);
00997 #else
00998 #endif
00999 }
01000
01001
01002 sprintf(directory_filename, "%s/%s", directory, filename);
01003
01004
01005 jm->apid=(short)apid;
01006
01007
01008 file_ptr=fopen(directory_filename,"r");
01009 if (!file_ptr)
01010 {
01011 printkerr("Error at %s, line %d: Check if directory and filename are",
01012 "correct. Could not open filename: <%s>\n",
01013 __FILE__, __LINE__, directory_filename);
01014 return;
01015 }
01016
01017
01018 tmp_dm=NULL;
01019 while( fgets(line, HK_LEV0_MAXLINE_IN_FILE, file_ptr) != NULL )
01020 {
01021 if(line[0] == '#')
01022 {
01023 continue;
01024 }
01025 else
01026 {
01027
01028
01029 if (!tmp_dm)
01030 {
01031 tmp_dm = (Map_Data_t*)malloc(sizeof(Map_Data_t));
01032 if (!tmp_dm)
01033 {
01034 printkerr("ERROR at %s, line %d: Cannot malloc space!\n", __FILE__ , __LINE__ );
01035 }
01036 tmp_dm->next= NULL;
01037 jm->mdata = tmp_dm;
01038 prev_dm =tmp_dm;
01039 }
01040 else
01041 {
01042 tmp_dm = (Map_Data_t*)malloc(sizeof(Map_Data_t));
01043 if (!tmp_dm)
01044 {
01045 printkerr("ERROR at %s, line %d: Cannot malloc space!\n", __FILE__ , __LINE__ );
01046 }
01047 tmp_dm->next= NULL;
01048 prev_dm->next =tmp_dm;
01049 prev_dm= tmp_dm;
01050 }
01051
01052
01053 sscanf( line,
01054 "%s | %d.%d | %*s | %*s", tmp_dm->jvn, &wn,&dn);
01055
01056
01057 sprintf(tmp_dm->pvn,"%03d.%03d", wn,dn);
01058
01059
01060 if (!strcmp("0000", tmp_dm->jvn))
01061 {
01062
01063 strcpy(tmp_dm->dsn, "\0");
01064 }
01065 else
01066 {
01067
01068
01069 if(curr_pvn_wn >= HK_LEV0_START_MERGED_PVNW && curr_pvn_dn >= HK_LEV0_START_MERGED_PVND)
01070 {
01071
01072 sprintf(tmp_dm->dsn, "%s.%s_%s_%s", pn, didn, get_data_packet_name(apid), tmp_dm->jvn);
01073 }
01074 else
01075 {
01076
01077 sprintf(tmp_dm->dsn, "%s.%s_%4.4d_%s", pn, didn, apid, tmp_dm->jvn);
01078 }
01079 }
01080 }
01081 }
01082
01083 fclose( file_ptr);
01084 return ;
01085 }
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095 int find_data_series_name(CCSDS_Packet_t *ccsds_ptr, JSOC_Version_Map_t *jm, char *dsn)
01096 {
01097
01098 JSOC_Version_Map_t *tmp_jm;
01099 Map_Data_t *dm;
01100 char data_series_name[HK_LEV0_MAX_DSNAME_STR];
01101 char pkt_ver_num[HK_LEV0_MAX_PVN_STR];
01102 char *pvn;
01103
01104
01105 pvn = pkt_ver_num;
01106 tmp_jm= jm;
01107
01108
01109 strcpy (data_series_name,"\0");
01110
01111
01112 strcpy(pvn , get_packet_version_number(ccsds_ptr->keywords));
01113
01114
01115
01116
01117 if (!jm)
01118 {
01119 return 0;
01120 }
01121 else
01122 {
01123 while (tmp_jm)
01124 {
01125
01126 if(tmp_jm->apid == ccsds_ptr->apid)
01127 {
01128
01129
01130
01131 dm= tmp_jm->mdata;
01132 while (dm)
01133 {
01134
01135 if ( !strcmp(dm->pvn, pvn))
01136 {
01137
01138 strcpy (data_series_name, dm->dsn);
01139 break;
01140 }
01141 dm = dm->next;
01142 }
01143 break;
01144 }
01145 else
01146 {
01147
01148 tmp_jm= tmp_jm->next;
01149 }
01150 }
01151 }
01152 strcpy(dsn, data_series_name);
01153 #ifdef DEBUG_WRITE_HK_TO_DRMS
01154 printkerr("DEBUG:Message:FOUND Lookup series name:%s\n",data_series_name);
01155 #else
01156 #endif
01157 return 1;
01158 }
01159
01160
01161
01162
01163
01164
01165
01166
01167 int get_pkt_time_from_timecodes(HK_Keyword_t *hk, TIME *ptime)
01168 {
01169
01170 HK_Keyword_t *t_hk;
01171 int sec;
01172 int subsec;
01173 int SEC_FOUND_FLAG=0;
01174 int SUBSEC_FOUND_FLAG=0;
01175
01176
01177 t_hk= hk;
01178
01179
01180 while (t_hk && ( !SEC_FOUND_FLAG || !SUBSEC_FOUND_FLAG ))
01181 {
01182 if(strstr(t_hk->name,"TIMECODE_SEC"))
01183 {
01184
01185 SEC_FOUND_FLAG=1;
01186
01187 sec = t_hk->eng_value.uint32_val;
01188 }
01189 if(strstr(t_hk->name,"TIMECODE_SSEC"))
01190 {
01191
01192 SUBSEC_FOUND_FLAG=1;
01193
01194 subsec = t_hk->eng_value.uint32_val;
01195 }
01196 t_hk= t_hk->next;
01197 }
01198
01199 if (!SEC_FOUND_FLAG)
01200 {
01201 printkerr("ERROR at %s, line %d: Did not find TIMECODE_SECONDS value for "
01202 "calculating the PACKET_TIME keyword and index. Returning error "
01203 "status.\n",__FILE__,__LINE__);
01204 return 0;
01205 }
01206 else
01207 {
01208 if (!SUBSEC_FOUND_FLAG)
01209 {
01210 printkerr("ERROR at %s, line %d: Did not find TIMECODE_SUBSECS value for "
01211 "calculating the PACKET_TIME keyword and index. Returning error "
01212 "status, but setting time using seconds only.\n",__FILE__,__LINE__);
01213 *ptime =SDO_to_DRMS_time(sec, 0);
01214 return 0;
01215 }
01216 else
01217 {
01218 int shifted_ss=(subsec >> 16) & 0xFFFF;
01219 *ptime =SDO_to_DRMS_time(sec, shifted_ss);
01220 return 1;
01221 }
01222 }
01223 }
01224
01225
01226
01227
01228
01229
01230
01231
01232 int check_for_apid(int apid, JSOC_Version_Map_t *top_jm)
01233 {
01234 JSOC_Version_Map_t *tmp_jm;
01235 tmp_jm= top_jm;
01236
01237 while( tmp_jm)
01238 {
01239 if(tmp_jm->apid == apid)
01240 {
01241 return 1;
01242 }
01243 tmp_jm= tmp_jm->next;
01244 }
01245 return 0;
01246 }
01247
01248
01249
01250
01251
01252
01253 void free_jsvn_map( JSOC_Version_Map_t *top_jm)
01254 {
01255
01256 JSOC_Version_Map_t *tmp_jm, *prev_jm;
01257 Map_Data_t *prev_md, *tmp_md;
01258
01259
01260 tmp_jm= top_jm;
01261
01262 while( tmp_jm)
01263 {
01264 tmp_md= tmp_jm->mdata;
01265 while(tmp_md)
01266 {
01267 prev_md = tmp_md;
01268 tmp_md= tmp_md->next;
01269 free(prev_md);
01270 }
01271 prev_jm = tmp_jm;
01272 tmp_jm= tmp_jm->next;
01273 free(prev_jm);
01274 }
01275 return;
01276 }
01277
01278
01279
01280
01281 static TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss)
01282 {
01283 static int firstcall = 1;
01284 static TIME sdo_epoch;
01285 if (firstcall)
01286 {
01287 firstcall = 0;
01288 sdo_epoch = sscan_time("1958.01.01_00:00:00_TAI");
01289 }
01290 return(sdo_epoch + (TIME)sdo_s + (TIME)(sdo_ss)/65536.0);
01291 }
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306 char *get_hk_source(CCSDS_Packet_t *ccsds_ptr, TIME *pt)
01307 {
01308
01309 char date[HK_LEV0_MAX_HKS_DATE];
01310 char df_datatype_name[HK_LEV0_MAX_DATATYPE_NAME];
01311 char df_project_name[HK_LEV0_MAX_PROJECT_NAME];
01312 char *df_dtname, *df_pjname;
01313 char hk_source[HK_LEV0_MAX_HKS];
01314 char *hks;
01315 int apid;
01316 int len_hk_source;
01317 int pkt_yr, pkt_month, pkt_day;
01318
01319
01320 df_dtname=df_datatype_name;
01321 df_pjname=df_project_name;
01322 hks = hk_source;
01323
01324
01325 apid = ccsds_ptr->apid;
01326
01327
01328 if(apid <= HK_HSB_HIGHEST_HMI_APID && apid >= HK_HSB_LOWEST_HMI_APID || (apid <= HK_LR_HIGHEST_HMI_APID && apid >= HK_LR_LOWEST_HMI_APID))
01329 {
01330 df_pjname = getenv("HK_LEV0_DF_PROJECT_NAME_HMI");
01331 }
01332 else if((apid <= HK_HSB_HIGHEST_AIA_APID && apid >= HK_HSB_LOWEST_AIA_APID) || (apid <= HK_LR_HIGHEST_AIA_APID && apid >= HK_LR_LOWEST_AIA_APID))
01333 {
01334 df_pjname = getenv("HK_LEV0_DF_PROJECT_NAME_AIA");
01335 }
01336 else if((apid <= HK_LR_HIGHEST_SDO_APID) && (apid >= HK_LR_LOWEST_SDO_APID))
01337 {
01338 df_pjname = getenv("HK_LEV0_DF_PROJECT_NAME_SDO");
01339 }
01340 else
01341 {
01342 printkerr("Warning at %s, line %d: APID is not in range of "
01343 "HMI, AIA, or SDO. APID: <%d>\n",
01344 __FILE__, __LINE__, apid);
01345 }
01346 df_pjname = getenv("HK_LEV0_DF_PROJECT_NAME_IRIS");
01347
01348
01349 df_dtname = getenv("HK_LEV0_DF_DATA_ID_NAME");
01350
01351
01352 pkt_yr=get_yr_from_pkttime(*pt);
01353 pkt_month=get_month_from_pkttime(*pt);
01354 pkt_day=get_day_from_pkttime(*pt);
01355
01356
01357 sprintf(date,"%d.%-02.2d.%-02.2d_00:00:00.00_TAI",pkt_yr, pkt_month, pkt_day);
01358
01359
01360 len_hk_source=strlen(df_pjname) + strlen(df_dtname) + strlen(date) + strlen("hsb") + strlen("475");
01361 if(len_hk_source >= HK_LEV0_MAX_HKS - 1)
01362 {
01363 printkerr("ERROR at %s, line %d: HK_SOURCE keyword value exceeds maximum size of array. "
01364 "Length is %d. Setting source value to null so that we do not exceed hks array.\n",
01365 __FILE__, __LINE__, len_hk_source);
01366 strcpy(hks,"" );
01367 }
01368 else
01369 {
01370 sprintf(hks,"%s.%s[%s][%d][%s]\0", df_pjname, df_dtname, date, apid, "hsb");
01371 }
01372
01373 return(hks);
01374 }
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385 int check_env_variable()
01386 {
01387
01388 char datatype_name[HK_LEV0_MAX_DATATYPE_NAME];
01389 char df_project_name[HK_LEV0_MAX_PROJECT_NAME];
01390 char df_datatype_name[HK_LEV0_MAX_DATATYPE_NAME];
01391 char project_name[HK_LEV0_MAX_PROJECT_NAME];
01392 char jvn_lu_hmi_prefix[HK_LEV0_MAX_LU_PREFIX_FILENAME];
01393 char jvn_lu_aia_prefix[HK_LEV0_MAX_LU_PREFIX_FILENAME];
01394 char jvn_lu_sdo_prefix[HK_LEV0_MAX_LU_PREFIX_FILENAME];
01395 char *dtname, *pjname, *df_dtname, *df_pjname;
01396 char *hmiprefix,*sdoprefix,*aiaprefix;
01397
01398
01399 pjname=project_name;
01400 df_dtname=df_datatype_name;
01401 df_pjname=df_project_name;
01402 dtname=datatype_name;
01403 hmiprefix=jvn_lu_hmi_prefix;
01404 aiaprefix=jvn_lu_aia_prefix;
01405 sdoprefix=jvn_lu_sdo_prefix;
01406
01407
01408
01409 pjname = getenv("HK_LEV0_BY_APID_PROJECT_NAME_HMI");
01410 if(pjname == NULL)
01411 {
01412 printkerr("ERROR at %s, line %d: Could not get project name environment "
01413 "variable:<HK_LEV0_BY_APID_PROJECT_NAME_HMI>. Set the env variable "
01414 "HK_LEV0_BY_APID_PROJECT_NAME_HMI to data series project name"
01415 "(hmi, hmi_ground, su_carl,etc.) for a existing data series name.\n",
01416 __FILE__,__LINE__);
01417 return ERROR_HK_ENVIRONMENT_VARS_NOT_SET ;
01418 }
01419 pjname = getenv("HK_LEV0_BY_APID_PROJECT_NAME_AIA");
01420 if(pjname == NULL)
01421 {
01422 printkerr("ERROR at %s, line %d: Could not get project name environment "
01423 "variable:<HK_LEV0_BY_APID_PROJECT_NAME_AIA>. Set the env variable "
01424 "HK_LEV0_BY_APID_PROJECT_NAME_HMI to data series project name"
01425 "(aia, aia_ground, su_carl,etc.) for a existing data series name.\n",
01426 __FILE__,__LINE__);
01427 return ERROR_HK_ENVIRONMENT_VARS_NOT_SET ;
01428 }
01429 pjname = getenv("HK_LEV0_BY_APID_PROJECT_NAME_SDO");
01430 if(pjname == NULL)
01431 {
01432 printkerr("ERROR at %s, line %d: Could not get project name environment "
01433 "variable:<HK_LEV0_BY_APID_PROJECT_NAME_SDO>. Set the env variable "
01434 "HK_LEV0_BY_APID_PROJECT_NAME_SDO to data series project name"
01435 "(sdo, sdo_ground, su_carl,etc.) for a existing data series name.\n",
01436 __FILE__,__LINE__);
01437 return ERROR_HK_ENVIRONMENT_VARS_NOT_SET ;
01438 }
01439
01440
01441 dtname = getenv("HK_LEV0_BY_APID_DATA_ID_NAME");
01442 if(dtname == NULL)
01443 {
01444 printkerr("ERROR at %s, line %d: Could not get data type name environment "
01445 "variable:<HK_LEV0_BY_APID_DATA_ID_NAME>. Set the env variable "
01446 "HK_LEV0_BY_APID_DATA_ID_NAME to data series data type name"
01447 "(lev0,lev0test, etc.) for a existing data series name\n",
01448 __FILE__ , __LINE__ );
01449 return (ERROR_HK_ENVIRONMENT_VARS_NOT_SET) ;
01450 }
01451
01452
01453
01454
01455 df_pjname = getenv("HK_LEV0_DF_PROJECT_NAME_HMI");
01456 if(df_pjname == NULL)
01457 {
01458 printkerr("ERROR at %s, line %d: Could not get project name environment "
01459 "variable:<HK_LEV0_DF_PROJECT_NAME_HMI>. Set the env variable "
01460 "HK_LEV0_DF_PROJECT_NAME_HMI to dayfile series project name"
01461 "(hmi, hmi_ground, su_carl,etc.) for a existing dayfile series name.\n",
01462 __FILE__,__LINE__);
01463 return ERROR_HK_ENVIRONMENT_VARS_NOT_SET ;
01464 }
01465 df_pjname = getenv("HK_LEV0_DF_PROJECT_NAME_AIA");
01466 if(df_pjname == NULL)
01467 {
01468 printkerr("ERROR at %s, line %d: Could not get project name environment "
01469 "variable:<HK_LEV0_DF_PROJECT_NAME_AIA>. Set the env variable "
01470 "HK_LEV0_DF_PROJECT_NAME_AIA to dayfile series project name"
01471 "(hmi, hmi_ground, su_carl,etc.) for a existing dayfile series name.\n",
01472 __FILE__,__LINE__);
01473 return ERROR_HK_ENVIRONMENT_VARS_NOT_SET ;
01474 }
01475 df_pjname = getenv("HK_LEV0_DF_PROJECT_NAME_SDO");
01476 if(df_pjname == NULL)
01477 {
01478 printkerr("ERROR at %s, line %d: Could not get project name environment "
01479 "variable:<HK_LEV0_DF_PROJECT_NAME_SDO>. Set the env variable "
01480 "HK_LEV0_DF_PROJECT_NAME_SDO to dayfile series project name"
01481 "(hmi, hmi_ground, su_carl,etc.) for a existing dayfile series name.\n",
01482 __FILE__,__LINE__);
01483 return ERROR_HK_ENVIRONMENT_VARS_NOT_SET ;
01484 }
01485
01486
01487 df_dtname = getenv("HK_LEV0_DF_DATA_ID_NAME");
01488 if(df_dtname == NULL)
01489 {
01490 printkerr("ERROR at %s, line %d: Could not get data type name environment "
01491 "variable:<HK_LEV0_DF_DATA_ID_NAME>. Set the env variable "
01492 "HK_LEV0_DF_DATA_ID_NAME to data series data type name"
01493 "(lev0,lev0test, etc.) for a existing data series name\n",
01494 __FILE__ , __LINE__ );
01495 return (ERROR_HK_ENVIRONMENT_VARS_NOT_SET) ;
01496 }
01497
01498
01499 hmiprefix=getenv("HK_PREFIX_JSVNMAP_FILENAME_HMI");
01500 if(hmiprefix == NULL)
01501 {
01502 printkerr("ERROR at %s, line %d: Could not get lookup file environment "
01503 "variable:<HK_PREFIX_JSVNMAP_FILENAME_HMI>. Set the env variable "
01504 "HK_PREFIX_JSVNMAP_FILENAME_HMI to correct prefix name"
01505 "(HMI-ISP or HMI-SEQ, etc.). Current value is null. \n",
01506 __FILE__ , __LINE__ );
01507 return (ERROR_HK_ENVIRONMENT_VARS_NOT_SET) ;
01508 }
01509
01510 aiaprefix=getenv("HK_PREFIX_JSVNMAP_FILENAME_AIA");
01511 if(aiaprefix == NULL)
01512 {
01513 printkerr("ERROR at %s, line %d: Could not get lookup file environment "
01514 "variable:<HK_PREFIX_JSVNMAP_FILENAME_AIA>. Set the env variable "
01515 "HK_PREFIX_JSVNMAP_FILENAME_AIA to correct prefix name"
01516 "(AIA-ISP or AIA-SEQ, etc.). Current value is null.\n",
01517 __FILE__ , __LINE__ );
01518 return (ERROR_HK_ENVIRONMENT_VARS_NOT_SET) ;
01519 }
01520
01521 sdoprefix=getenv("HK_PREFIX_JSVNMAP_FILENAME_SDO");
01522 if(sdoprefix == NULL)
01523 {
01524 printkerr("ERROR at %s, line %d: Could not get lookup file environment "
01525 "variable:<HK_PREFIX_JSVNMAP_FILENAME_SDO>. Set the env variable "
01526 "HK_PREFIX_JSVNMAP_FILENAME_SDO to correct prefix name"
01527 "(SDO-ADP). Current value is null.\n",
01528 __FILE__ , __LINE__ );
01529 return (ERROR_HK_ENVIRONMENT_VARS_NOT_SET) ;
01530 }
01531
01532
01533 return (1);
01534 }
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546 char *get_lookup_filename(int apid )
01547 {
01548
01549 char *lufn;
01550 char lookup_filename[HK_LEV0_MAX_DSNAME_STR];
01551 char pkt_name[HK_LEV0_MAX_PACKET_NAME];
01552
01553
01554 lufn = lookup_filename;
01555
01556
01557
01558 switch(apid)
01559 {
01560
01561
01562
01563 case HK_LR_HMI_ISP:
01564 case HK_HSB_HMI_ISP_1:
01565 case HK_HSB_HMI_ISP_2:
01566
01567
01568 strcpy(pkt_name, HK_HMI_PKT_NAME_ISP);
01569 sprintf(lufn,"%s-%s",getenv("HK_PREFIX_JSVNMAP_FILENAME_HMI"),make_strupr(pkt_name));
01570 strcat(lufn,"\0");
01571 break;
01572
01573 case HK_HSB_IRIS_ISP:
01574
01575 strcpy(pkt_name, HK_HSB_IRIS_PKT_NAME_ISP);
01576 sprintf(lufn,"%s-%s",getenv("HK_PREFIX_JSVNMAP_FILENAME_IRIS"),make_strupr(pkt_name));
01577 strcat(lufn,"\0");
01578 break;
01579
01580
01581
01582
01583 case HK_LR_AIA_ISP:
01584 case HK_HSB_AIA_ISP_1:
01585 case HK_HSB_AIA_ISP_2:
01586
01587
01588 strcpy(pkt_name,HK_AIA_PKT_NAME_ISP);
01589 sprintf(lufn,"%s-%s",getenv("HK_PREFIX_JSVNMAP_FILENAME_AIA"),make_strupr(pkt_name));
01590 strcat(lufn,"\0");
01591 break;
01592
01593
01594
01595
01596 case HK_LR_HMI_SEQ:
01597 case HK_HSB_HMI_SEQ_1:
01598 case HK_HSB_HMI_SEQ_2:
01599
01600
01601 strcpy(pkt_name,HK_HMI_PKT_NAME_SEQ);
01602 sprintf(lufn,"%s-%s",getenv("HK_PREFIX_JSVNMAP_FILENAME_HMI"),make_strupr(pkt_name));
01603 strcat(lufn,"\0");
01604 break;
01605
01606
01607
01608
01609 case HK_LR_AIA_SEQ:
01610 case HK_HSB_AIA_SEQ_1:
01611 case HK_HSB_AIA_SEQ_2:
01612
01613
01614 strcpy(pkt_name,HK_AIA_PKT_NAME_SEQ);
01615 sprintf(lufn,"%s-%s",getenv("HK_PREFIX_JSVNMAP_FILENAME_AIA"),make_strupr(pkt_name));
01616 strcat(lufn,"\0");
01617 break;
01618
01619
01620
01621
01622 case HK_LR_HMI_OBT:
01623 case HK_HSB_HMI_OBT_1:
01624 case HK_HSB_HMI_OBT_2:
01625
01626
01627 strcpy(pkt_name,HK_HMI_PKT_NAME_OBT);
01628 sprintf(lufn,"%s-%s",getenv("HK_PREFIX_JSVNMAP_FILENAME_HMI"),make_strupr(pkt_name));
01629 strcat(lufn,"\0");
01630 break;
01631
01632
01633
01634
01635 case HK_LR_AIA_OBT:
01636 case HK_HSB_AIA_OBT_1:
01637 case HK_HSB_AIA_OBT_2:
01638
01639
01640 strcpy(pkt_name,HK_AIA_PKT_NAME_OBT);
01641 sprintf(lufn,"%s-%s",getenv("HK_PREFIX_JSVNMAP_FILENAME_AIA"),make_strupr(pkt_name));
01642 strcat(lufn,"\0");
01643 break;
01644
01645
01646
01647
01648 case HK_LR_SDO_ASD:
01649
01650
01651 strcpy(pkt_name,HK_SDO_PKT_NAME_ASD);
01652 sprintf(lufn,"%s-%s",getenv("HK_PREFIX_JSVNMAP_FILENAME_SDO"),make_strupr(pkt_name));
01653 strcat(lufn,"\0");
01654 break;
01655
01656
01657
01658
01659 default:
01660
01661 if(apid <= HK_HSB_HIGHEST_HMI_APID && apid >= HK_HSB_LOWEST_HMI_APID || (apid <= HK_LR_HIGHEST_HMI_APID && apid >= HK_LR_LOWEST_HMI_APID))
01662 {
01663 sprintf(lufn,"%s-%-03.3d",getenv("HK_PREFIX_JSVNMAP_FILENAME_HMI"),apid);
01664 strcat(lufn,"\0");
01665 }
01666 else if((apid <= HK_HSB_HIGHEST_AIA_APID && apid >= HK_HSB_LOWEST_AIA_APID) || (apid <= HK_LR_HIGHEST_AIA_APID && apid >= HK_LR_LOWEST_AIA_APID))
01667 {
01668 sprintf(lufn,"%s-%-03.3d",getenv("HK_PREFIX_JSVNMAP_FILENAME_AIA"),apid);
01669 strcat(lufn,"\0");
01670 }
01671 else if((apid <= HK_LR_HIGHEST_SDO_APID) && (apid >= HK_LR_LOWEST_SDO_APID))
01672 {
01673 sprintf(lufn,"%s-%-03.3d",getenv("HK_PREFIX_JSVNMAP_FILENAME_SDO"),apid);
01674 strcat(lufn,"\0");
01675 }
01676 else
01677 {
01678 printkerr("Warning at %s, line %d: APID is not in range of "
01679 "HMI, AIA, or SDO. APID: <%d>\n",
01680 __FILE__, __LINE__, apid);
01681 strcpy(lufn,"\0");
01682 }
01683 break;
01684
01685 }
01686
01687 return(lufn);
01688 }
01689
01690
01691
01692
01693
01694
01695
01696
01697
01698 char * make_strupr (char *a)
01699 {
01700 char *ret = a;
01701
01702 while (*a != '\0')
01703 {
01704 if (islower (*a))
01705 *a = (char)toupper (*a);
01706 ++a;
01707 }
01708
01709 return ret;
01710 }
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723 int check_hk_record_exists(char* ds_name, HK_Keyword_t *kw, int apid)
01724 {
01725
01726 HK_DSN_RANGE_t *dsr;
01727 HK_DSN_RANGE_t *pdsr;
01728 HK_DSN_RANGE_t *dsrn;
01729 HK_Keyword_t *tkw;
01730 HK_Timecode_t *tc;
01731 HK_Timecode_t *ptc;
01732 HK_Timecode_t *tcn;
01733 int ck_status;
01734 int found_status=0;
01735 int j;
01736 long int sec_pkt, subsec_pkt;
01737 long int trigger_lr, trigger_hr;
01738 static int tc_last_index;
01739 int found_dsr_node=0;
01740 int ckinit=0;
01741 int istatus=0;
01742
01743
01744 ckinit= check_ds_cache_init(ds_name);
01745
01746
01747
01748 if(ckinit)
01749 {
01750 istatus=initialize_timecodes_cache(ds_name, kw, apid);
01751 if(istatus == 0)
01752 {
01753 return (0);
01754 }
01755 }
01756
01757
01758
01759 tkw=kw;
01760 while (tkw)
01761 {
01762 if(strstr(tkw->name,"TIMECODE_SECONDS"))
01763 {
01764
01765
01766 sec_pkt = tkw->eng_value.uint32_val;
01767 }
01768 if(strstr(tkw->name,"TIMECODE_SUBSECS"))
01769 {
01770
01771
01772 subsec_pkt = tkw->eng_value.uint32_val;
01773 break;
01774 }
01775 tkw= tkw->next;
01776 }
01777
01778
01779
01780
01781 for(dsr=dsr_head; dsr;dsr=dsr->next)
01782 {
01783
01784 if (!strcmp(dsr->dsname, ds_name))
01785 {
01786
01787
01788
01789 trigger_lr=dsr->timecode_lrsec + 3600;
01790 trigger_hr=dsr->timecode_hrsec - 3600;
01791 if ( (trigger_lr <= sec_pkt) && (trigger_hr >= sec_pkt) )
01792 {
01793
01794
01795 break;
01796 }
01797 else
01798 {
01799
01800
01801 #ifdef DEBUG_WRITE_HK_TO_DRMS
01802 printkerr("Warning Monitor Message:check_hk_rec_exist:Watch this does "
01803 "not occur too often. The low and high range of timecode cache needs "
01804 "to be reset and reloaded.This warning message should occur only about four "
01805 "times every 24 hours per HK by APID series.\n");
01806 #endif
01807
01808
01809 dsr->timecode_lrsec= sec_pkt - (HK_SECONDS_RANGE);
01810 dsr->timecode_hrsec= sec_pkt + (HK_SECONDS_RANGE);
01811
01812
01813 for(j=0,tc=dsr->tcnode;tc;j++)
01814 {
01815 ptc=tc;
01816 tc=tc->next;
01817 free(ptc);
01818 }
01819 dsr->tcnode=NULL;
01820 #ifdef DEBUG_WRITE_HK_TO_DRMS
01821 printkerr("DEBUG:MESSAGE: Freed <%d> TIMECODE nodes in cache for <%s> before reloading cache TIMECODE values\n", j,ds_name);
01822 #endif
01823 tc_last_index=0;
01824
01825
01826 istatus=initialize_timecodes_cache(ds_name, kw, apid);
01827 if(istatus == 0)
01828 {
01829 return (0);
01830 }
01831 break;
01832 }
01833 }
01834 }
01835
01836
01837
01838
01839
01840
01841
01842
01843 for(ck_status=found_status=0, dsr=dsr_head; dsr; dsr=dsr->next)
01844 {
01845
01846 if(!strcmp(dsr->dsname, ds_name))
01847 {
01848
01849
01850
01851 for(found_status=0, j=0, tc=dsr->tcnode; tc;j++)
01852 {
01853
01854 if( sec_pkt == tc->sec)
01855 {
01856
01857 if( subsec_pkt == tc->subsec)
01858 {
01859
01860 ck_status=1;
01861 found_status=1;
01862 tc=tcn;
01863 break;
01864 }
01865 else
01866 {
01867
01868
01869 found_status=0;
01870 ck_status=0;
01871 tcn=tc->next;
01872 tc=tcn;
01873 }
01874 }
01875 else
01876 {
01877
01878 found_status=0;
01879 ck_status=0;
01880 tcn=tc->next;
01881 tc=tcn;
01882 }
01883 }
01884
01885 }
01886 }
01887
01888
01889
01890
01891 if(!found_status)
01892 {
01893
01894
01895 ck_status=found_status;
01896
01897
01898 for(dsr=dsr_head; dsr; dsr=dsr->next)
01899 {
01900 if(!strcmp(dsr->dsname, ds_name))
01901 {
01902
01903 found_dsr_node=1;
01904 break;
01905 }
01906 }
01907
01908
01909 if (!found_dsr_node)
01910 {
01911
01912
01913 dsrn= (HK_DSN_RANGE_t *)malloc(sizeof(HK_DSN_RANGE_t));
01914
01915
01916 strcpy(dsrn->dsname, ds_name);
01917 dsrn->timecode_lrsec= sec_pkt - HK_SECONDS_RANGE;
01918 dsrn->timecode_hrsec= sec_pkt + HK_SECONDS_RANGE;
01919 dsrn->next=NULL;
01920 dsrn->tcnode=NULL;
01921
01922
01923 for(dsr=dsr_head; dsr; pdsr=dsr, dsr=dsr->next);
01924
01925
01926 if(dsr_head)
01927 {
01928
01929 pdsr->next= dsrn;
01930 }
01931 else
01932 {
01933
01934 dsr_head=dsrn;
01935 }
01936 tc=dsrn->tcnode;
01937
01938 }
01939 else
01940 {
01941
01942 tc=dsr->tcnode;
01943 dsrn=dsr;
01944 }
01945
01946
01947 if (!tc)
01948 {
01949
01950 tc= (HK_Timecode_t *)malloc(sizeof(HK_Timecode_t));
01951 tc->sec = sec_pkt;
01952 tc->subsec = subsec_pkt;
01953 tc->next = NULL;
01954 dsrn->tcnode=tc;
01955 tc_last_index++;
01956 }
01957 else
01958 {
01959
01960 while(tc)
01961 {
01962 ptc=tc;
01963 tcn=tc->next;
01964 tc=tcn;
01965 }
01966
01967
01968 tc= (HK_Timecode_t *)malloc(sizeof(HK_Timecode_t)) ;
01969 tc->sec = sec_pkt;
01970 tc->subsec = subsec_pkt;
01971 tc->next = NULL;
01972 ptc->next=tc;
01973 tc_last_index++;
01974 }
01975 }
01976 else
01977 {
01978
01979 ck_status=found_status;
01980 }
01981
01982 return (ck_status);
01983 }
01984
01985
01986
01987
01988
01989
01990
01991
01992
01993
01994
01995
01996
01997 short create_low_range(HK_Keyword_t *kw_head, long int *lr)
01998 {
01999 HK_Keyword_t *tkw;
02000 tkw=kw_head;
02001
02002
02003 while (tkw)
02004 {
02005 if(strstr(tkw->name,"TIMECODE_SECONDS"))
02006 {
02007
02008
02009 *lr = tkw->eng_value.uint32_val - HK_SECONDS_RANGE;
02010 return(1);
02011 }
02012 tkw= tkw->next;
02013 }
02014
02015 return(0);
02016 }
02017
02018
02019
02020
02021
02022
02023
02024
02025
02026
02027 short create_high_range(HK_Keyword_t *kw_head, long int *hr)
02028 {
02029 HK_Keyword_t *tkw;
02030 tkw=kw_head;
02031
02032
02033 while (tkw)
02034 {
02035 if(strstr(tkw->name,"TIMECODE_SECONDS"))
02036 {
02037
02038
02039 *hr = tkw->eng_value.uint32_val + (HK_SECONDS_RANGE);
02040 return(1);
02041 }
02042 tkw= tkw->next;
02043 }
02044
02045 return(0);
02046 }
02047
02048
02049
02050
02051
02052
02053
02054
02055
02056
02057
02058 short check_ds_cache_init(char *dsname)
02059 {
02060 short i;
02061 static char init_cache[100][50];
02062 static short init_count=0;
02063 short should_init=0;
02064
02065
02066 if (init_count == 0)
02067 {
02068 should_init=1;
02069 }
02070 else
02071 {
02072
02073 for (i=0,should_init=1; i < init_count;i++)
02074 {
02075
02076 if (!strcmp(init_cache[i], dsname))
02077 {
02078
02079 should_init=0;
02080 break;
02081 }
02082 }
02083 }
02084
02085 if (should_init)
02086 {
02087 strcpy(init_cache[init_count],dsname);
02088 init_count++;
02089 }
02090 return(should_init);
02091 }
02092
02093
02094
02095
02096
02097
02098
02099
02100
02101
02102
02103
02104 short check_curr_map_file(char* pv_str, short *curr_map_flag)
02105 {
02106 float pv_num;
02107 short new_map_flag;
02108 float merge_threshold;
02109
02110
02111 merge_threshold= (float)HK_LEV0_START_MERGED_PVNW;
02112 merge_threshold += (float)(HK_LEV0_START_MERGED_PVND/1000.0);
02113
02114
02115 sscanf(pv_str,"%f",&pv_num);
02116
02117 if(pv_num < merge_threshold)
02118 {
02119 new_map_flag= HK_NON_MERGE_MAP_FILE_FLAG;
02120 }
02121
02122 else if (pv_num >= merge_threshold)
02123 {
02124 new_map_flag= HK_MERGE_MAP_FILE_FLAG;
02125 }
02126 else
02127 {
02128 printkerr("DEBUG:Warning Message:check_curr_map_file: Should not have reached else case:\n");
02129 }
02130
02131
02132 if (new_map_flag == *curr_map_flag)
02133 {
02134
02135 return(0);
02136 }
02137 else
02138 {
02139 *curr_map_flag = new_map_flag;
02140 return(1);
02141 }
02142 }
02143
02144
02145
02146
02147
02148
02149
02150
02151
02152
02153 int get_query_range(int range_type, HK_Keyword_t *kw, char qr[HK_MAX_SIZE_RANGE_TIME])
02154 {
02155 int gpt_status;
02156 TIME pkt_time;
02157 TIME *ptime=&pkt_time;
02158 TIME pkt_time1;
02159 TIME *ptime1=&pkt_time1;
02160 int yr,mr,dr,hr;
02161
02162
02163 gpt_status = get_pkt_time_from_timecodes(kw, ptime);
02164 if(!gpt_status)
02165 {
02166 printkerr("WARNING at %s, line %d: Could not find timecode inorder "
02167 "to get packet time. gpt_status:<%d>.\n", __FILE__,__LINE__, gpt_status);
02168 }
02169
02170 if(range_type == HK_LOW_QUERY_RANGE)
02171 {
02172 *ptime1= *ptime - (HK_SECONDS_RANGE);
02173 }
02174 else if (range_type == HK_HIGH_QUERY_RANGE)
02175 {
02176 *ptime1= *ptime + (HK_SECONDS_RANGE);
02177 }
02178 else
02179 {
02180 sprintf(qr, "0000.00.00_00:00:00.00_UTC");
02181
02182 #ifdef DEBUG_WRITE_HK_TO_DRMS
02183 printkerr("DEBUG:Message at %s, line %d: Bad Query range "
02184 " is <%s>.\n", __FILE__,__LINE__, qr);
02185 #endif
02186
02187 return (0);
02188 }
02189 hr=get_hour_from_pkttime(*ptime1);
02190 dr=get_day_from_pkttime( *ptime1);
02191 mr=get_month_from_pkttime(*ptime1);
02192 yr=get_yr_from_pkttime(*ptime1);
02193 sprintf(qr, "%04d.%02d.%02d_%02d:00:00.00_UTC",yr,mr,dr,hr);
02194
02195 #ifdef DEBUG_WRITE_HK_TO_DRMS
02196 printkerr("DEBUG:Message at %s, line %d: Query range "
02197 " is <%s>.\n", __FILE__,__LINE__, qr);
02198 #endif
02199
02200 return (1);
02201 }
02202
02203
02204
02205
02206 int initialize_timecodes_cache(char* ds_name, HK_Keyword_t *kw, int apid)
02207 {
02208
02209 HK_DSN_RANGE_t *dsr;
02210 HK_DSN_RANGE_t *pdsr;
02211 HK_Timecode_t *tc;
02212 HK_Timecode_t *tcn;
02213 char pkt_ver_num[HK_LEV0_MAX_PVN_STR];
02214 char tc_sec[HK_LEV0_MAX_LONG_KW_NAME];
02215 char tc_subsec[HK_LEV0_MAX_LONG_KW_NAME];
02216 char strapid[HK_LEV0_MAX_PACKET_NAME];
02217 char nquery[HK_MAX_SIZE_QUERY];
02218 char qr1[HK_MAX_SIZE_RANGE_TIME];
02219 char qr2[HK_MAX_SIZE_RANGE_TIME];
02220 char *pvn;
02221 int curr_pvn_dn;
02222 int curr_pvn_wn;
02223 int drms_status;
02224 int j;
02225 int lr_status;
02226 int hr_status;
02227 int gqr1_status;
02228 int gqr2_status;
02229 static long int tclr=0;
02230 static long int tchr=0;
02231
02232 DRMS_RecordSet_t *rs;
02233 DRMS_Record_t *rec;
02234
02235
02236 rs=NULL;
02237 rec=NULL;
02238 pvn= pkt_ver_num;
02239
02240
02241 if(check_for_sdo_apid(apid))
02242 {
02243
02244
02245 curr_pvn_wn= HK_LEV0_START_MERGED_PVNW;
02246 curr_pvn_dn= HK_LEV0_START_MERGED_PVND;
02247 }
02248 else
02249 {
02250 pvn=get_packet_version_number(kw);
02251 sscanf(pvn,"%3d.%3d",&curr_pvn_wn, &curr_pvn_dn);
02252 }
02253
02254
02255
02256 lr_status=create_low_range( kw, &tclr);
02257 hr_status=create_high_range( kw, &tchr);
02258
02259
02260
02261
02262
02263
02264
02265
02266
02267
02268
02269
02270
02271 gqr1_status=get_query_range(HK_LOW_QUERY_RANGE,kw,qr1);
02272 gqr2_status=get_query_range(HK_HIGH_QUERY_RANGE,kw,qr2);
02273 if(!gqr1_status || !gqr2_status)
02274 {
02275 printkerr("Warning at %s, line %d: Could not create query range successfully. "
02276 "qr1:<%s> qr2:<%s>\n", __FILE__, __LINE__);
02277 }
02278
02279 sprintf(nquery,"%s[? PACKET_TIME < $(%s) AND PACKET_TIME > $(%s) ?]",ds_name,qr2,qr1);
02280
02281
02282 rs = drms_open_records(drms_env, nquery, &drms_status);
02283 if(drms_status)
02284 {
02285 printkerr("ERROR at %s, line %d: Can't open records using drms_open_records for drms query <%s> "
02286 "This series may not exist. Please create series and rerun\n",
02287 __FILE__,__LINE__, nquery);
02288 return(0);
02289 }
02290
02291 #ifdef DEBUG_WRITE_HK_TO_DRMS
02292 if(!rs)
02293 {
02294 printkerr("DEBUG:Message at %s, line %d: Record query to "
02295 "dataseries is <%s>. Return status of query:<%d> \n",
02296 __FILE__,__LINE__, nquery, drms_status);
02297 }
02298 else
02299 {
02300 printkerr("DEBUG:Message at %s, line %d: Record query to "
02301 "dataseries is <%s>. Return status of query:<%d> "
02302 "Return count:<%d>\n", __FILE__,__LINE__, nquery, drms_status, rs->n);
02303 }
02304 #endif
02305
02306
02307
02308 if (!rs)
02309 {
02310
02311 printkerr("Warning at %s, line %d: There are no records for series. "
02312 "Therefore returning 0 for check if hk record exists. When return "
02313 "zero then the records will get written to series. \n", __FILE__, __LINE__);
02314
02315
02316 printkerr("Warning at %s, line %d: DRMS data series <%s> returned status<%d>.Query "
02317 "could be too big and reached limit. The query was <%s>\n",
02318 __FILE__,__LINE__, ds_name, drms_status, nquery);
02319 return(0);
02320 }
02321
02322
02323 if(rs->n)
02324 {
02325
02326
02327
02328 if(dsr_head)
02329 {
02330
02331 for(dsr=dsr_head;dsr; pdsr=dsr,dsr=dsr->next)
02332 {
02333
02334
02335 if(!strcmp(dsr->dsname, ds_name))
02336 {
02337
02338 tc=dsr->tcnode;
02339 break;
02340 }
02341 }
02342 }
02343 else
02344 {
02345
02346 dsr=dsr_head;
02347 }
02348
02349
02350 if(curr_pvn_wn >= HK_LEV0_START_MERGED_PVNW && curr_pvn_dn >= HK_LEV0_START_MERGED_PVND)
02351 {
02352
02353 if(apid == HK_HSB_HMI_ISP_1 || apid == HK_HSB_HMI_ISP_2 || apid == HK_LR_HMI_ISP)
02354 {
02355 strcpy(strapid, HK_HMI_PKT_NAME_ISP);
02356 }
02357 else if(apid == HK_HSB_IRIS_ISP) {
02358 strcpy(strapid, HK_HSB_IRIS_PKT_NAME_ISP);
02359 }
02360 else if(apid == HK_HSB_AIA_ISP_1 || apid == HK_HSB_AIA_ISP_2 || apid == HK_LR_AIA_ISP )
02361 {
02362 strcpy(strapid, HK_AIA_PKT_NAME_ISP);
02363 }
02364 else if(apid == HK_HSB_HMI_SEQ_1 || apid == HK_HSB_HMI_SEQ_2 || apid ==HK_LR_HMI_SEQ )
02365 {
02366 strcpy(strapid, HK_HMI_PKT_NAME_SEQ);
02367 }
02368 else if(apid == HK_HSB_AIA_SEQ_1 || apid == HK_HSB_AIA_SEQ_2 || apid == HK_LR_AIA_SEQ )
02369 {
02370 strcpy(strapid, HK_AIA_PKT_NAME_SEQ);
02371 }
02372 else if(apid == HK_HSB_HMI_OBT_1 || apid == HK_HSB_HMI_OBT_2 || apid ==HK_LR_HMI_OBT )
02373 {
02374 strcpy(strapid, HK_HMI_PKT_NAME_OBT);
02375 }
02376 else if(apid == HK_HSB_AIA_OBT_1 || apid == HK_HSB_AIA_OBT_2 || apid == HK_LR_AIA_OBT )
02377 {
02378 strcpy(strapid, HK_AIA_PKT_NAME_OBT);
02379 }
02380 else if(apid == HK_LR_SDO_ASD)
02381 {
02382 strcpy(strapid, HK_SDO_PKT_NAME_ASD);
02383 }
02384 else
02385 {
02386
02387 sprintf(strapid,"%03.3x", apid);
02388 }
02389 }
02390 else
02391 {
02392
02393 sprintf(strapid,"%03.3x", apid);
02394 }
02395
02396
02397 for(j=0; j < rs->n;j++)
02398 {
02399
02400
02401 if(!dsr)
02402 {
02403
02404 dsr= (HK_DSN_RANGE_t *)malloc(sizeof(HK_DSN_RANGE_t));
02405
02406
02407 tc= (HK_Timecode_t *)malloc(sizeof(HK_Timecode_t));
02408
02409
02410 if(!dsr_head)
02411 {
02412
02413 dsr_head=dsr;
02414 }
02415 else
02416 {
02417
02418 pdsr->next=dsr;
02419 }
02420
02421
02422 strcpy(dsr->dsname, ds_name);
02423 dsr->next=NULL;
02424 dsr->tcnode=tc;
02425 dsr->timecode_lrsec=tclr;
02426 dsr->timecode_hrsec=tchr;
02427
02428
02429 rec = rs->records[j];
02430
02431
02432 sprintf(tc_sec, "%s%s_%s","APID",make_strupr(strapid),"TIMECODE_SECONDS");
02433 sprintf(tc_subsec, "%s%s_%s","APID",make_strupr(strapid),"TIMECODE_SUBSECS");
02434
02435
02436 tc->sec = drms_getkey_longlong(rec, tc_sec, &drms_status);
02437 tc->subsec = drms_getkey_longlong(rec,tc_subsec , &drms_status);
02438
02439
02440 tc->next = NULL;
02441
02442 }
02443 else
02444 {
02445
02446 tcn= (HK_Timecode_t *)malloc(sizeof(HK_Timecode_t));
02447
02448
02449
02450 if(dsr->tcnode == NULL)
02451 {
02452
02453 dsr->tcnode=tcn;
02454 }
02455 else
02456 {
02457
02458 tc->next=tcn;
02459 }
02460
02461
02462 tc=tcn;
02463
02464
02465 rec = rs->records[j];
02466
02467
02468 sprintf(tc_sec, "%s%s_%s","APID",make_strupr(strapid),"TIMECODE_SECONDS");
02469 sprintf(tc_subsec, "%s%s_%s","APID",make_strupr(strapid),"TIMECODE_SUBSECS");
02470
02471
02472 tc->sec = drms_getkey_longlong(rec, tc_sec, &drms_status);
02473 tc->subsec = drms_getkey_longlong(rec,tc_subsec , &drms_status);
02474
02475
02476 tc->next = NULL;
02477 }
02478 }
02479 }
02480
02481
02482
02483 drms_status = drms_close_records(rs, DRMS_FREE_RECORD);
02484
02485 return (1);
02486
02487 }
02488
02489
02490
02491
02492
02493
02494
02495
02496 int check_hk_record_within_time_range( HK_Keyword_t *kw)
02497 {
02498 TIME pkt_time;
02499 TIME *ptime= &pkt_time;
02500 HK_Keyword_t *tkw;
02501 int curr_packet_number;
02502 int curr_time_range;
02503 int curr_hr_range;
02504 int y,m,d,h;
02505 int gpt_status;
02506 struct tm timestorage;
02507 struct tm *time_ptr;
02508 time_t tvalue;
02509 time_ptr= ×torage;
02510
02511
02512 tkw=kw;
02513 gpt_status = get_pkt_time_from_timecodes(tkw, ptime);
02514 if(!gpt_status)
02515 {
02516 printkerr("WARNING at %s, line %d: Could not find timecode inorder "
02517 "to get packet time. gpt_status:<%d>.\n", __FILE__,__LINE__, gpt_status);
02518 }
02519 d=get_day_from_pkttime(*ptime);
02520 h=get_hour_from_pkttime(*ptime);
02521 m=get_month_from_pkttime(*ptime);
02522 y=get_yr_from_pkttime(*ptime);
02523
02524
02525 curr_packet_number = (y * 10000) + (m * 100) + d ;
02526 #ifdef DEBUG_WRITE_HK_TO_DRMS
02527 printkerr("DEBUG:Message at %s, line %d: Packet time is::: y:<%d>m:<%d>d:<%d>h<%d>.\n",__FILE__,__LINE__,y,m,d,h);
02528 #endif
02529
02530
02531 tvalue = time(NULL);
02532 time_ptr = gmtime(&tvalue);
02533 #ifdef DEBUG_WRITE_HK_TO_DRMS
02534 printkerr("DEBUG:Message at %s, line %d: Current time is:::y:<%d>m:<%d>d:<%d>h<%d>.\n",
02535 __FILE__,__LINE__, time_ptr->tm_year+1900,time_ptr->tm_mon+1,time_ptr->tm_mday,time_ptr->tm_hour);
02536 #endif
02537
02538
02539 if((time_ptr->tm_mon+1) == 12 && (time_ptr->tm_mday == 31) && (time_ptr->tm_hour >= 12))
02540 {
02541
02542 curr_time_range = (((time_ptr->tm_year+1900+1) * 10000) + ( 1 * 100) + 1);
02543 curr_hr_range=time_ptr->tm_hour - 12;
02544 }
02545 else if( ( (time_ptr->tm_mon+1) == 1 || (time_ptr->tm_mon+1) == 3 || (time_ptr->tm_mon+1) == 5 || (time_ptr->tm_mon+1) == 7 || (time_ptr->tm_mon+1) == 8 || (time_ptr->tm_mon+1) == 10 ) && time_ptr->tm_mday == 31 && (time_ptr->tm_hour >= 12))
02546 {
02547
02548 curr_time_range = (((time_ptr->tm_year+1900) * 10000) + ((time_ptr->tm_mon+2) * 100) + 1);
02549 curr_hr_range=time_ptr->tm_hour - 12;
02550 }
02551 else if( ( (time_ptr->tm_mon+1) == 4 || (time_ptr->tm_mon+1) == 6 || (time_ptr->tm_mon+1) == 5 || (time_ptr->tm_mon+1) == 9 || (time_ptr->tm_mon+1) == 11 ) && time_ptr->tm_mday == 30 && (time_ptr->tm_hour >= 12))
02552 {
02553
02554 curr_time_range = ((time_ptr->tm_year+1900) * 10000) + ((time_ptr->tm_mon+2) * 100) + 1;
02555 curr_hr_range=time_ptr->tm_hour - 12;
02556 }
02557 else if( ( (time_ptr->tm_mon+1) == 2 && (time_ptr->tm_year+1900 == 2009) && time_ptr->tm_mday == 28) && (time_ptr->tm_hour >= 12))
02558 {
02559
02560 curr_time_range = ((time_ptr->tm_year+1900) * 10000) + ((time_ptr->tm_mon+2) * 100) + 1;
02561 curr_hr_range=time_ptr->tm_hour - 12;
02562 }
02563 else if( ( (time_ptr->tm_mon+1) == 2 && (time_ptr->tm_year+1900 == 2010) && time_ptr->tm_mday == 29) && (time_ptr->tm_hour >= 12))
02564 {
02565
02566 curr_time_range = ((time_ptr->tm_year+1900) * 10000) + ((time_ptr->tm_mon+2) * 100) + 1;
02567 curr_hr_range=time_ptr->tm_hour - 12;
02568 }
02569 else if((time_ptr->tm_hour >= 12))
02570 {
02571
02572 curr_time_range= ((time_ptr->tm_year+1900) * 10000) + ((time_ptr->tm_mon+1) * 100) + ( time_ptr->tm_mday + 1);
02573 curr_hr_range=time_ptr->tm_hour - 12;
02574 }
02575 else if((time_ptr->tm_hour < 12))
02576 {
02577
02578 curr_time_range= ((time_ptr->tm_year+1900) * 10000) + ((time_ptr->tm_mon+1) * 100) + ( time_ptr->tm_mday);
02579 curr_hr_range=time_ptr->tm_hour + 12;
02580 }
02581 else
02582 {
02583 printkerr("ERROR:check_hk_record_within_time_range:Bad else case in function\n");
02584 }
02585
02586
02587 if ( curr_packet_number < curr_time_range)
02588 {
02589 #ifdef DEBUG_WRITE_HK_TO_DRMS
02590 printkerr("DEBUG:MESSAGE:check_hk_record_within_range:Received packets --within-- range(yyyy.mm.dd_hh):%d.%02d.%02d_%02d\n", y,m,d,h);
02591 #endif
02592 return (1);
02593 }
02594 else if ( curr_packet_number == curr_time_range && h < curr_hr_range)
02595 {
02596
02597 #ifdef DEBUG_WRITE_HK_TO_DRMS
02598 printkerr("DEBUG:MESSAGE:check_hk_record_within_range:Received packets --within-- range(yyyy.mm.dd_hh):%d.%02d.%02d_%02d\n", y,m,d,h);
02599 #endif
02600 return (1);
02601 }
02602 else
02603 {
02604 #ifdef DEBUG_WRITE_HK_TO_DRMS
02605 printkerr("DEBUG:WARNING MESSAGE:check_hk_record_within_range:Received packets not within range(yyyy.mm.dd_hh):%d.%02d.%02d_%02d\n", y,m,d,h);
02606 #endif
02607 return (0);
02608 }
02609
02610 }