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