00001 #ident "$Header: /home/cvsuser/cvsroot/JSOC/proj/lev1/apps/load_m3sd.c,v 1.10 2014/08/21 23:56:01 arta Exp $"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00106
00107
00108 #define HKLMS_LONG_KEYWORD_NAME_SIZE 100
00109 #define HKLMS_MAX_AU_OW_NAME 50
00110 #define HKLMS_MAX_DSNAME_STR 100
00111 #define HKLMS_MAX_FILE_NAME 100
00112 #define HKLMS_MAX_INSTR_FILENAME 100
00113 #define HKLMS_MAX_PACKET_DESCRIPTION 100
00114 #define HKLMS_MAX_PKT_SIZE 1000
00115 #define HKLMS_MAX_PVN_SIZE 50
00116 #define HKLMS_PACKET_TIME_STR 100
00117 #define HKLMS_READ_ARRAY_SIZE (25000001)
00118 #define HKLMS_SHORT_KEYWORD_NAME_SIZE 100
00119 #define HKLMS_NEXT_DAYFILE 1
00120 #define HKLMS_PREVIOUS_DAYFILE 0
00121 #define HKLMS_FOUND_DAYFILE 1
00122 #define HKLMS_NOT_FOUND_DAYFILE 0
00123 #define HKLMS_QUERY_STRING_SIZE 500
00124 #define HKLMS_APID_SIZE 5
00125
00126
00127 #define ENVFILE "/home/production/cvs/JSOC/proj/lev1/apps/SOURCE_ENV_FOR_LOAD_M3SD"
00128
00129
00130 #include <stdio.h>
00131 #include <stdlib.h>
00132 #include <string.h>
00133 #include <time.h>
00134 #include <math.h>
00135 #include <sys/stat.h>
00136 #include <unistd.h>
00137 #include <stdarg.h>
00138 #include "drms.h"
00139 #include "drms_names.h"
00140 #include "jsoc_main.h"
00141 #include "load_hk_config_files.h"
00142 #include "write_hk_to_drms.h"
00143 #include "packets.h"
00144 #include "decode_hk.h"
00145 #include "printk.h"
00146
00147 static char datestr[32];
00148
00149 char *do_datestr() {
00150 time_t tval;
00151 struct tm *t_ptr;
00152
00153 tval = time(NULL);
00154 t_ptr = localtime(&tval);
00155 sprintf(datestr, "%d.%02d.%02d_%02d:%02d:%02d",
00156 (t_ptr->tm_year+1900), (t_ptr->tm_mon+1),
00157 t_ptr->tm_mday, t_ptr->tm_hour, t_ptr->tm_min, t_ptr->tm_sec);
00158 return(datestr);
00159 }
00160
00161
00162
00163 ModuleArgs_t module_args[] =
00164 {
00165 {ARG_STRING, "in", "Not Specified", "full path to day file"},
00166 {ARG_STRING, "isf", "Not Specified", "full path to instruction file"},
00167 {ARG_END}
00168 };
00169 ModuleArgs_t *ggModArgs=module_args;
00170 char* module_name = "load_m3sd";
00171
00172
00173
00174
00175 typedef struct Instruction_File_Keywords_struct_t Instruction_File_Keywords_t;
00176 struct Instruction_File_Keywords_struct_t
00177 {
00178 char kw_longname[HKLMS_LONG_KEYWORD_NAME_SIZE];
00179 char kw_shortname[HKLMS_SHORT_KEYWORD_NAME_SIZE];
00180 int dec_apid;
00181 int hex_apid;
00182 Instruction_File_Keywords_t *next;
00183 };
00184
00185
00186 typedef struct Instruction_File_Data_struct_t
00187 {
00188 char inst_filename[HKLMS_MAX_INSTR_FILENAME];
00189 char series_name[HKLMS_MAX_DSNAME_STR];
00190 char author[HKLMS_MAX_AU_OW_NAME];
00191 char owner[HKLMS_MAX_AU_OW_NAME];
00192 char packet_description[HKLMS_MAX_PACKET_DESCRIPTION];
00193 int interval;
00194 Instruction_File_Keywords_t *kw_if;
00195 } Instruction_File_Data_t;
00196
00197
00198 typedef struct HK_Keyword_M3SD_Values_struct_t
00199 {
00200 char kw_longname[HKLMS_LONG_KEYWORD_NAME_SIZE];
00201 char kw_shortname[HKLMS_SHORT_KEYWORD_NAME_SIZE];
00202 float mean;
00203 float min;
00204 float max;
00205 float stdev;
00206 struct HK_Keyword_M3SD_Values_struct_t *next;
00207 } HK_Keyword_M3SD_Values_t;
00208
00209 typedef struct HK_Keyword_M3SD_Data_struct_t
00210 {
00211 TIME start_pkt_time;
00212 int number_points;
00213 int dec_apid;
00214 int hex_apid;
00215 struct HK_Keyword_M3SD_Data_struct_t *next;
00216 struct HK_Keyword_M3SD_Values_struct_t *m3sd_values;
00217 } HK_Keyword_M3SD_Data_t;
00218
00219
00220 typedef struct HK_KW_Data_Values_struct_t
00221 {
00222 TIME pkt_time;
00223 KW_Type_t eng_type;
00224 KW_Type_Value_t eng_value;
00225 struct HK_KW_Data_Values_struct_t *next;
00226 } HK_KW_Data_Values_t;
00227
00228 typedef struct HK_KW_Data_struct_t
00229 {
00230 TIME start_pkt_time;
00231 char longname[MAX_KEYWORD_NAME_SIZE];
00232 char shortname[MAX_FITS_NAME_SIZE];
00233 struct HK_KW_Data_struct_t *next;
00234 struct HK_KW_Data_Values_struct_t *kw_values;
00235 } HK_KW_Data_t;
00236
00237
00238 static void set_env_variables();
00239 static TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss);
00240 void get_next_pkt_time(TIME p_time, int intval, TIME *start, TIME *end);
00241 int get_minute_from_pkttime(double tc_sec);
00242 int get_seconds_from_pkttime(double tc_sec);
00243 static int get_packet_time_for_df(HK_Keyword_t *hk, TIME *ptime);
00244 HK_KW_Data_t * save_kw_data(TIME start_pkt_time, Instruction_File_Data_t *ifs, HK_Keyword_t *kw );
00245 void save_kw_data_values(HK_KW_Data_t *top_kw_data_ptr, TIME pkt_time, HK_Keyword_t *kw );
00246 void free_all_kw_data(HK_KW_Data_t *top_kw_data_ptr);
00247 void save_m3sd_data(HK_KW_Data_t *top_kw_data_ptr, HK_Keyword_M3SD_Data_t **kw_m3sd_head);
00248 void save_m3sd_values(HK_KW_Data_t *top_kw_data_ptr, HK_Keyword_M3SD_Data_t **kw_data_head);
00249 float get_min_value(HK_KW_Data_Values_t *ptr);
00250 float get_max_value(HK_KW_Data_Values_t *ptr);
00251 float get_mean_value(HK_KW_Data_Values_t *ptr);
00252 float get_stdev_value(HK_KW_Data_Values_t *ptr);
00253 void write_m3sd_to_drms(HK_Keyword_M3SD_Data_t *top_m3sd_data_ptr, Instruction_File_Data_t *ifp);
00254 int get_number_points(HK_KW_Data_Values_t *ptr);
00255 Instruction_File_Data_t * read_isf_data( const char *inf);
00256 void get_keyword_values(unsigned char *read_in_buffer, Instruction_File_Data_t *ifp, char *src);
00257 void check_status_drms_set(int status, char *kwn);
00258 int get_day_from_pkttime(double p_time);
00259 int get_month_from_pkttime(double p_time);
00260 int get_yr_from_pkttime(double p_time);
00261 void my_usage (void);
00262 void add_dayfile_data(char *dayfile, TIME start_pkt_time,HK_KW_Data_t *topkwdataptr,int interval);
00263 int get_dayfile_to_process(HK_KW_Data_t *kwdataptr, int apid,int prev_next_flag, char *dayfile_name, char *src);
00264
00265 extern int get_hour_from_pkttime(double p_time);
00266 extern SHCIDS_Version_Number *global_shcids_vn;
00267 extern char * find_file_version_number(GTCIDS_Version_Number *top,char f_version_number[MAX_CHAR_VERSION_NUMBER], int apid);
00268 extern char * find_fvn_from_shcids(SHCIDS_Version_Number *top,char pkt_date[MAX_SIZE_PKT_DATE],int apid);
00269 extern double get_packet_time(unsigned short *word_ptr);
00270 extern int DoIt(void);
00271 extern int nice_intro (void);
00272 extern int check_for_sdo_apid(int apid);
00273 extern void sprint_time (char *at, TIME t, char *zone, int precision);
00274 extern char *get_data_packet_name(int apid ) ;
00275 extern char *get_lookup_filename(int apid);
00276 extern int check_hk_record_exists(char* ds_name, HK_Keyword_t *kw, int apid);
00277 extern int check_hk_record_within_time_range( HK_Keyword_t *kw);
00278
00279
00280 extern GTCIDS_Version_Number *global_gtcids_vn;
00281 extern GTCIDS_Version_Number *global_gtcids_vn;
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 void my_usage (void)
00293 {
00294
00295 printf ("Usage:\nload_m3sd [-h] "
00296 "src=< rtmon | moc > "
00297 "in=<day filename> "
00298 "isf=<instruction file> \n"
00299 " details are:\n"
00300 " -h: help - show this message then exit(optional field)\n"
00301 " src=<source on dayfile> -use to check either moc or rtmon next day's or previous day's dayfile(required field)\n"
00302 " in=<day file name> -use full path to day file(required field)\n"
00303 " isf=<instruction file name> -use full path to instruction file(required field)\n"
00304 " Need data series already created for this to program to load keywords in data series.\n"
00305 " Setup interval time in this file where range of intervals tested are 60 seconds to 7200 seconds.\n"
00306 " Setup keywords to calculate mean, max, min and standard deviation values in this instruction file.\n"
00307 " Setup seriesname in this instruction file for writing keyword min,max,mean and standard deviation values.\n"
00308 " Example of running:\n"
00309 " load_m3sd in=/home/carl/cvs/myprod/JSOC/proj/lev1_hmi/apps/20080918.0x0013 isf=/home/carl/cvs/TBL_JSOC/lev1/instruction_file/hmitest1200_thermal_template.txt\n" );
00310
00311 }
00312
00313
00314
00315
00316
00317
00318
00319 int nice_intro (void)
00320 {
00321 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00322 if (usage == 1)
00323 {
00324 my_usage();
00325 return(1);
00326 }
00327 return (0);
00328 }
00329
00330
00331
00332
00333
00334
00335
00336
00337 int DoIt(void)
00338 {
00339
00340 FILE *file_ptr;
00341 Instruction_File_Data_t *instr_file_ptr;
00342 Instruction_File_Data_t * read_isf_data(const char *inf);
00343 char *hk_df_fn;
00344 char hk_directory_filename[HKLMS_MAX_FILE_NAME];
00345 char source[20];
00346 unsigned char *ptr_read_in_buffer;
00347 unsigned long int i;
00348
00349
00350 set_env_variables();
00351
00352
00353 hk_df_fn= hk_directory_filename;
00354
00355
00356 const char *in = cmdparams_get_str (&cmdparams, "in", NULL);
00357 const char *isf = cmdparams_get_str (&cmdparams, "isf", NULL);
00358 const char *src = cmdparams_get_str (&cmdparams, "src", NULL);
00359
00360
00361 if (nice_intro ()) return (0);
00362
00363
00364 if (in == NULL)
00365 {
00366 printkerr("ERROR at %s, line %d: Need to enter day file name."
00367 "Exiting program.\nExample format for -in- file: "
00368 "$HOME/data/dayfiles/20060719.0x000f\n",
00369 __FILE__,__LINE__);
00370 my_usage();
00371 return (0);
00372 }
00373
00374
00375 instr_file_ptr= read_isf_data(isf);
00376 if (instr_file_ptr == NULL)
00377 {
00378 printkerr("ERROR at %s, line %d: Note need to enter correct path "
00379 "and filename for instruction file <%s>. Example format for -isf- file: "
00380 "isf=/home/data/instruction_file1.txt. Exiting this program.\n ",
00381 __FILE__,__LINE__, isf);
00382 my_usage();
00383 return (0);
00384 }
00385
00386 if (src == NULL)
00387 {
00388
00389
00390
00391
00392
00393
00394
00395 strcpy(source, "moc");
00396 strcat(source,"\0");
00397 }
00398 else if (!strcmp(src,"rtmon"))
00399 {
00400 strcpy(source, src);
00401 strcat(source,"\0");
00402 }
00403 else if (!strcmp(src,"moc"))
00404 {
00405 strcpy(source, src);
00406 strcat(source,"\0");
00407 }
00408 else
00409 {
00410 printf("ERROR: at Note need to enter source value using src=<rtmon | moc>. The current src values enter was <%s>.\n", src);
00411 my_usage();
00412 return (0);
00413 }
00414
00415
00416
00417
00418 #ifdef DEBUG_LM3S
00419 Instruction_File_Keywords_t *ptr_if_kws;
00420 printf("DoIt:Print Structure\n");
00421 printf("DoIt:interval:%d des:%s \n",instr_file_ptr->interval, instr_file_ptr->packet_description);
00422 printf("DoIt:owner:%s author:%s \n",instr_file_ptr->author,instr_file_ptr->owner);
00423 printf("DoIt:seriesname:%s \n",instr_file_ptr->series_name);
00424 printf("DoIt:templatename:%s \n",instr_file_ptr->inst_filename);
00425 for(ptr_if_kws =instr_file_ptr->kw_if;ptr_if_kws;ptr_if_kws=ptr_if_kws->next )
00426 {
00427 printf("DoIt: ptr_if_kws->kw_longname=<%d>\n",ptr_if_kws->dec_apid );
00428 printf("DoIt: ptr_if_kws->kw_longname=<%s>\n",ptr_if_kws->kw_longname );
00429 printf("DoIt: ptr_if_kws->kw_shortname=<%s>\n\n",ptr_if_kws->kw_shortname );
00430 }
00431 #endif
00432
00433
00434 strcpy(hk_df_fn, in);
00435 strcat(hk_df_fn, "\0");
00436 file_ptr=fopen(hk_df_fn,"r");
00437 if (!file_ptr)
00438 {
00439 printkerr("ERROR at %s, line %d: Please check filename and directory is correct. "
00440 " Could not get -in- directory and filename: "
00441 "<%s>. Example format for -in- file: in=/home/rock/20080909.0x013. Exiting execution.\n",
00442 __FILE__,__LINE__, hk_df_fn);
00443 return(0);
00444 }
00445
00446
00447 ptr_read_in_buffer = (unsigned char *) malloc(sizeof(unsigned char) * HKLMS_READ_ARRAY_SIZE);
00448
00449
00450 for(i=0; i < HKLMS_READ_ARRAY_SIZE;i++) ptr_read_in_buffer[i]=0; ;
00451 for(i = 0 ; fread(ptr_read_in_buffer + i,1,1,file_ptr) ; i++)
00452 {
00453 ;
00454 if( i == HKLMS_READ_ARRAY_SIZE - 1)
00455 {
00456 printkerr("ERROR at %s, line %d: Array for reading dayfile is too small. :"
00457 "<%lu>\n", __FILE__,__LINE__, i + 1);
00458 printkerr("Break up large dayfile using dd command. : dd if<large-file> "
00459 " of=<small-file-1> bs=<packet_size + 7> count=<i.e., 1000> skip=<0,1,etc>\n");
00460 return (0);
00461 }
00462 }
00463
00464 *(ptr_read_in_buffer + i) = '\0';
00465
00466
00467 fclose(file_ptr);
00468
00469
00470
00471 (void)get_keyword_values(ptr_read_in_buffer, instr_file_ptr, source);
00472
00473
00474 free(ptr_read_in_buffer);
00475
00476
00477 return 0;
00478 }
00479
00480
00481
00482
00483
00484 static int get_packet_time_for_df(HK_Keyword_t *hk, TIME *ptime)
00485 {
00486
00487 HK_Keyword_t *t_hk;
00488 int sec;
00489 int subsec;
00490 int SEC_FOUND_FLAG=0;
00491 int SUBSEC_FOUND_FLAG=0;
00492
00493
00494 t_hk= hk;
00495
00496
00497 while (t_hk && ( !SEC_FOUND_FLAG || !SUBSEC_FOUND_FLAG ))
00498 {
00499 if(strstr(t_hk->name,"TIMECODE_SECONDS"))
00500 {
00501
00502 SEC_FOUND_FLAG=1;
00503
00504 sec = t_hk->eng_value.uint32_val;
00505 }
00506 if(strstr(t_hk->name,"TIMECODE_SUBSECS"))
00507 {
00508
00509 SUBSEC_FOUND_FLAG=1;
00510
00511 subsec = t_hk->eng_value.uint32_val;
00512 }
00513 t_hk= t_hk->next;
00514 }
00515
00516 if (!SEC_FOUND_FLAG)
00517 {
00518 printkerr("ERROR at %s, line %d: Did not find TIMECODE_SECONDS value for"
00519 "calculating the PACKET_TIME keyword and index. Returning error"
00520 "status.\n",__FILE__,__LINE__);
00521 return 0;
00522 }
00523 else
00524 {
00525 if (!SUBSEC_FOUND_FLAG)
00526 {
00527 printkerr("ERROR at %s, line %d: Did not find TIMECODE_SUBSECS value for"
00528 "calculating the PACKET_TIME keyword and index. Returning error"
00529 "status, but setting time using seconds only.\n",__FILE__,__LINE__);
00530 *ptime =SDO_to_DRMS_time(sec, 0);
00531 return 0;
00532 }
00533 else
00534 {
00535 int shifted_ss=(subsec >> 16) & 0xFFFF;
00536 *ptime =SDO_to_DRMS_time(sec, shifted_ss);
00537 return 1;
00538 }
00539 }
00540 }
00541
00542
00543
00544
00545 static TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss)
00546 {
00547 static int firstcall = 1;
00548 static TIME sdo_epoch;
00549 if (firstcall)
00550 {
00551 firstcall = 0;
00552 sdo_epoch = sscan_time("1958.01.01_00:00:00_TAI");
00553 }
00554 return(sdo_epoch + (TIME)sdo_s + (TIME)(sdo_ss)/65536.0);
00555 }
00556
00557
00558
00559
00560 void get_keyword_values(unsigned char *read_in_buffer, Instruction_File_Data_t *ifp, char *src)
00561 {
00562 CCSDS_Packet_t ccsds;
00563 HK_Keyword_M3SD_Data_t *top_m3sd_data_ptr;
00564 HK_KW_Data_t *top_kw_data_ptr;
00565 HK_Keyword_t *kw_head,*kw;
00566 TIME pkt_time;
00567 TIME next_pkt_time;
00568 TIME start,end;
00569 TIME *st_ptr, *ed_ptr;
00570 char dir_filename_dayfile[HKLMS_MAX_FILE_NAME];
00571 char file_version_number[HKLMS_MAX_PVN_SIZE];
00572 char packet_version_number[HKLMS_MAX_PVN_SIZE];
00573 char pkt_date[MAX_SIZE_PKT_DATE];
00574 char *ptr_fvn;
00575 int apid;
00576 int df_status=0;
00577 int factor;
00578 int interval_count;
00579 int interval;
00580 int i,j,k,y,s;
00581 int packet_length;
00582 static int did_previous_day_flag=0;
00583 unsigned char hk_pkt_buffer[HKLMS_MAX_PKT_SIZE];
00584 unsigned short s_hk_pkt_buffer[HKLMS_MAX_PKT_SIZE];
00585 unsigned short *word_ptr;
00586
00587
00588 top_m3sd_data_ptr=NULL;
00589 top_kw_data_ptr=NULL;
00590 interval_count=0;
00591 st_ptr= &start;
00592 ed_ptr= &end;
00593
00594 #ifdef DEBUG_LM3S
00595 char at[HKLMS_PACKET_TIME_STR];
00596 char atn[HKLMS_PACKET_TIME_STR];
00597 #endif
00598
00599
00600 interval=ifp->interval;
00601
00602
00603 next_pkt_time=0.0;
00604
00605
00606 for(k=0,factor=0,packet_length=0; ; k++)
00607 {
00608
00609 factor = k * (packet_length + 6 + 1 ) ;
00610
00611
00612 if (*(read_in_buffer+5+factor) == '\0')
00613 {
00614 #ifdef DEBUG_LM3S
00615 printf("get_keyword_values:PROCESSING NEXT DAY'S DATA\n");
00616 printf("get_keyword_values:interval case4:\n");
00617 #endif
00618
00619
00620
00621 df_status=get_dayfile_to_process(top_kw_data_ptr,apid,HKLMS_NEXT_DAYFILE,dir_filename_dayfile, src);
00622 if(df_status)
00623 {
00624 #ifdef DEBUG_LM3S
00625 printf("get_keyword_values:Return from get_dayfile_to_process:%s\n",dir_filename_dayfile);
00626 #endif
00627
00628 (void)add_dayfile_data(dir_filename_dayfile, top_kw_data_ptr->start_pkt_time, top_kw_data_ptr,interval);
00629 }
00630
00631
00632
00633
00634 if( !top_m3sd_data_ptr)
00635 {
00636
00637 (void)save_m3sd_data( top_kw_data_ptr,&top_m3sd_data_ptr);
00638
00639
00640 (void)save_m3sd_values(top_kw_data_ptr, &top_m3sd_data_ptr);
00641 }
00642 else
00643 {
00644
00645 (void)save_m3sd_data( top_kw_data_ptr,&top_m3sd_data_ptr);
00646 (void)save_m3sd_values(top_kw_data_ptr, &top_m3sd_data_ptr);
00647 }
00648
00649
00650 free_all_kw_data(top_kw_data_ptr);
00651
00652
00653 break;
00654 }
00655
00656
00657 packet_length= *(read_in_buffer+5+factor);
00658
00659
00660
00661 apid = (unsigned short int)( (*(read_in_buffer+1+factor)) & 0x00FF );
00662
00663 apid |= (unsigned short int)( (*(read_in_buffer+0+factor) << 8) & 0xFF00 );
00664 apid &= 0x07FF;
00665
00666
00667
00668 if(check_for_sdo_apid(apid))
00669 {
00670 sprintf(packet_version_number,"%s","not applicable");
00671 }
00672 else
00673 {
00674
00675 if (( *(read_in_buffer+14+factor) == 0) && (*(read_in_buffer+15+factor) == 0))
00676 {
00677 printkerr("Warning at %s, line %d: Getting 0.0 for packet version number."
00678 "in packet data. Skip processing this packet, don't load to "
00679 "DRMS and get next packet", __FILE__,__LINE__);
00680 continue;
00681 }
00682
00683 if ( *(read_in_buffer+14+factor) == 0 && *(read_in_buffer+15+factor))
00684 {
00685 printkerr("Warning at %s, line %d: Getting 0 for whole number(i.e.,0.1) for "
00686 "packet version number in packet data. Skip processing this packet,"
00687 "don't load to DRMS and get next packet", __FILE__,__LINE__);
00688 continue;
00689 }
00690
00691 sprintf(packet_version_number,"%03d.%03d",*(read_in_buffer+14+factor), *(read_in_buffer+15+factor));
00692 strcat(packet_version_number,"\0");
00693 }
00694
00695
00696 for(i=0; i < HKLMS_MAX_PKT_SIZE;i++) hk_pkt_buffer[i]=0x00;
00697
00698
00699 for (i =0 + factor, j=0 ; i < packet_length + 6 + 1 + factor; i++)
00700 {
00701
00702 hk_pkt_buffer[j++]= *(read_in_buffer+i);
00703 }
00704
00705
00706
00707
00708 for (i=0, y=0 ; i < packet_length + 6 + 1 ; i += 2, y++)
00709 {
00710 s_hk_pkt_buffer[y] = (unsigned short)(hk_pkt_buffer[i + 1] << 8 & 0xFF00 );
00711 s_hk_pkt_buffer[y] = (unsigned short)((hk_pkt_buffer[i] & 0x00FF) + s_hk_pkt_buffer[y]) ;
00712 }
00713
00714 word_ptr = s_hk_pkt_buffer;
00715 s = decode_hk_keywords(word_ptr, apid, &ccsds.keywords);
00716 if (s)
00717 {
00718 printkerr("ERROR at %s, line %d: decode_hk_keyword function returned"
00719 " error status number <%d>\n\n", __FILE__,__LINE__, s);
00720 printf("ERROR at %s, line %d: decode_hk_keyword function returned error status number <%d>\n\n", __FILE__,__LINE__, s);
00721 continue;
00722 }
00723 kw_head= ccsds.keywords;
00724
00725
00726 if(check_for_sdo_apid(apid))
00727 {
00728
00729
00730 (void)sprint_time (pkt_date, get_packet_time(word_ptr), "TAI", 0);
00731
00732 ptr_fvn=find_fvn_from_shcids(global_shcids_vn, pkt_date, apid);
00733 }
00734 else
00735 {
00736
00737 ptr_fvn=find_file_version_number(global_gtcids_vn, packet_version_number,apid);
00738 }
00739
00740 strcpy(file_version_number,ptr_fvn);
00741
00742
00743
00744
00745
00746 kw=kw_head;
00747
00748
00749 (void)get_packet_time_for_df(kw, &pkt_time);
00750
00751 #ifdef DEBUG_LM3S
00752 (void)sprint_time (at, pkt_time, "UTC", 0);
00753 strcat(at,"\0");
00754 (void)sprint_time (atn, next_pkt_time, "UTC", 0);
00755 strcat(atn,"\0");
00756 #endif
00757
00758
00759
00760 if(!(int)next_pkt_time)
00761 {
00762 #ifdef DEBUG_LM3S
00763 printf("get_keyword_values:interval case1:pkt_time is %-12.12f & %-s\n",pkt_time,at);
00764 printf("get_keyword_values:interval case1:next_pkt_time is %-12.12f & %-s\n",next_pkt_time,atn);
00765 #endif
00766
00767 (void)get_next_pkt_time(pkt_time, interval, st_ptr, ed_ptr);
00768
00769
00770 next_pkt_time= *ed_ptr;
00771
00772
00773 interval_count++;
00774
00775 #ifdef DEBUG_LM3S
00776 (void)sprint_time (atn, next_pkt_time, "UTC", 0);
00777 strcat(atn,"\0");
00778 printf("get_keyword_values:interval case1:new next_pkt_time is %-12.12f & %-s\n",next_pkt_time,atn);
00779 #endif
00780
00781
00782 top_kw_data_ptr=save_kw_data(*st_ptr, ifp, kw );
00783
00784
00785 (void) save_kw_data_values(top_kw_data_ptr, pkt_time, kw );
00786 }
00787 else if (pkt_time > next_pkt_time)
00788 {
00789
00790
00791 if(!did_previous_day_flag )
00792 {
00793 #ifdef DEBUG_LM3S
00794 printf("get_keyword_values:DOING PREVIOUS DAY'S DATA PROCESSING\n");
00795 #endif
00796
00797 did_previous_day_flag=1;
00798
00799
00800
00801 df_status=get_dayfile_to_process(top_kw_data_ptr,apid,HKLMS_PREVIOUS_DAYFILE,dir_filename_dayfile,src);
00802
00803
00804 if(df_status)
00805 {
00806 #ifdef DEBUG_LM3S
00807 printf("get_keyword_values:Return from get_dayfile_to_process:%s\n",dir_filename_dayfile);
00808 #endif
00809
00810 (void)add_dayfile_data(dir_filename_dayfile, top_kw_data_ptr->start_pkt_time, top_kw_data_ptr,interval);
00811 }
00812
00813 }
00814
00815 #ifdef DEBUG_LM3S
00816 printf("get_keyword_values:interval case2:k packet:%d pkt_time is %-12.12f & %-s\n",k,pkt_time,at);
00817 printf("get_keyword_values:interval case2:next_pkt_time is %-12.12f & %-s\n",next_pkt_time, atn);
00818 printf("get_keyword_values:interval case2:at end of interval block, so calculate and save m3sd data.\n");
00819 #endif
00820
00821
00822 if( !top_m3sd_data_ptr)
00823 {
00824
00825 (void)save_m3sd_data( top_kw_data_ptr,&top_m3sd_data_ptr);
00826
00827
00828 (void)save_m3sd_values(top_kw_data_ptr, &top_m3sd_data_ptr);
00829 }
00830 else
00831 {
00832
00833 (void)save_m3sd_data( top_kw_data_ptr,&top_m3sd_data_ptr);
00834 (void)save_m3sd_values(top_kw_data_ptr, &top_m3sd_data_ptr);
00835 }
00836
00837
00838 free_all_kw_data(top_kw_data_ptr);
00839
00840
00841 (void)get_next_pkt_time(pkt_time, interval, st_ptr, ed_ptr);
00842
00843
00844 next_pkt_time= *ed_ptr;
00845
00846 #ifdef DEBUG_LM3S
00847 (void)sprint_time (atn, next_pkt_time, "UTC", 0);
00848 strcat(atn,"\0");
00849 printf("get_keyword_values:interval case2:new next_pkt_time is %-12.12f & %-s\n",next_pkt_time,atn);
00850 #endif
00851
00852
00853 interval_count++;
00854
00855
00856 top_kw_data_ptr=save_kw_data(*st_ptr, ifp, kw );
00857
00858
00859 (void) save_kw_data_values(top_kw_data_ptr, pkt_time, kw );
00860 }
00861 else
00862 {
00863 #ifdef DEBUG_LM3S
00864 printf("get_keyword_values:interval case3:k packet:%d pkt_time is %-12.12f & %-s\n",k,pkt_time,at);
00865 printf("get_keyword_values:interval case3:next_pkt_time is %-12.12f & %-s\n",next_pkt_time,atn);
00866 #endif
00867
00868
00869
00870 (void) save_kw_data_values(top_kw_data_ptr, pkt_time, kw );
00871 }
00872
00873
00874 }
00875
00876
00877
00878 #ifdef DEBUG_LM3S
00879 HK_Keyword_M3SD_Values_t *ptr_m3sd_values;
00880 HK_Keyword_M3SD_Data_t *ptr_m3sd_data;
00881 for ( ptr_m3sd_data=top_m3sd_data_ptr;ptr_m3sd_data; ptr_m3sd_data=ptr_m3sd_data->next)
00882 {
00883 printf("HK_Keyword_M3SD_Data_t node start pkt time<%f\n",ptr_m3sd_data->start_pkt_time);
00884 for(ptr_m3sd_values = ptr_m3sd_data->m3sd_values;ptr_m3sd_values;ptr_m3sd_values=ptr_m3sd_values->next)
00885 {
00886 printf("get_keyword_values:HK_Keyword_M3SD_Values_t node longname<%s>\n",ptr_m3sd_values->kw_longname);
00887 printf("get_keyword_values:HK_Keyword_M3SD_Values_t node shortname<%s>\n",ptr_m3sd_values->kw_shortname);
00888 printf("get_keyword_values:HK_Keyword_M3SD_Values_t node MIN:<%f>\n",ptr_m3sd_values->min);
00889 printf("get_keyword_values:HK_Keyword_M3SD_Values_t node MAX:<%f>\n",ptr_m3sd_values->max);
00890 printf("get_keyword_values:HK_Keyword_M3SD_Values_t node MEAN:<%f>\n",ptr_m3sd_values->mean);
00891 printf("get_keyword_values:HK_Keyword_M3SD_Values_t node SD:<%f>\n",ptr_m3sd_values->stdev);
00892 }
00893 }
00894 printf("Interval looped thru: %d\n",interval_count);
00895 #endif
00896
00897
00898 (void)write_m3sd_to_drms(top_m3sd_data_ptr, ifp);
00899
00900 }
00901
00902
00903
00904
00905
00906
00907 Instruction_File_Data_t * read_isf_data(const char *inf)
00908 {
00909 void remove_blanks( char string[HKLMS_SHORT_KEYWORD_NAME_SIZE], char *new_str);
00910 Instruction_File_Data_t *ptr_isf;
00911 Instruction_File_Keywords_t *top_if_kws,*ptr_if_kws;
00912 FILE *file_ptr;
00913 char line[MAXLINE_IN_FILE];
00914 char kwlong[HKLMS_LONG_KEYWORD_NAME_SIZE];
00915 char kwshort[HKLMS_SHORT_KEYWORD_NAME_SIZE];
00916 char new_string[HKLMS_LONG_KEYWORD_NAME_SIZE];
00917 int exit_flag=0;
00918
00919
00920 if(!inf)
00921 {
00922 return ((Instruction_File_Data_t *)(NULL));
00923 }
00924
00925
00926 ptr_isf=(Instruction_File_Data_t*)malloc (sizeof (Instruction_File_Data_t));
00927 ptr_isf->kw_if=NULL;
00928
00929
00930 file_ptr = fopen(inf, "r");
00931 if(!file_ptr)
00932 {
00933
00934 return NULL;
00935 }
00936 top_if_kws=NULL;
00937
00938 while( fgets(line, MAXLINE_IN_FILE, file_ptr) != NULL )
00939 {
00940 if(line[0] == '#')
00941 {
00942 continue;
00943 }
00944 else if (!strncmp(line, "templatename:", 13) || !strncmp(line, "Templatename:", 13))
00945 {
00946 (void)strtok(line, ":");
00947 strcpy( ptr_isf->inst_filename, strtok(NULL,"\n"));
00948 strcat( ptr_isf->inst_filename,"\0");
00949 }
00950 else if (!strncmp(line, "seriesname:", 11) || !strncmp(line, "Seriesname:", 11))
00951 {
00952 (void)strtok(line, ":");
00953 strcpy( ptr_isf->series_name, strtok(NULL,"\n"));
00954 strcat( ptr_isf->inst_filename,"\0");
00955 }
00956 else if (!strncmp(line, "author:", 7) || !strncmp(line, "Author:", 7))
00957 {
00958 (void)strtok(line, ":");
00959 strcpy( ptr_isf->author, strtok(NULL,"\n"));
00960 }
00961 else if (!strncmp(line, "owner:", 6) || !strncmp(line, "Owner:", 6))
00962 {
00963 (void)strtok(line, ":");
00964 strcpy( ptr_isf->owner, strtok(NULL,"\n"));
00965 }
00966 else if (!strncmp(line, "description:", 12) || !strncmp(line, "Description:", 12))
00967 {
00968 (void)strtok(line, ":");
00969 strcpy( ptr_isf->packet_description, strtok(NULL,"\n"));
00970 }
00971 else if (!strncmp(line, "interval:", 9) || !strncmp(line, "Interval:", 9))
00972 {
00973 (void)strtok(line, ":");
00974 sscanf( strtok(NULL,"\n"), "%d", &(ptr_isf->interval));
00975 }
00976 else if (!strncmp(line, "keyword:", 8) || !strncmp(line, "Keyword:", 8))
00977 {
00978 if (top_if_kws == NULL)
00979 {
00980 top_if_kws = ptr_if_kws = malloc(sizeof(Instruction_File_Keywords_t));
00981 ptr_if_kws->next = (Instruction_File_Keywords_t*)NULL;
00982 ptr_isf->kw_if = top_if_kws;
00983 }
00984 else
00985 {
00986 ptr_if_kws->next = malloc(sizeof(Instruction_File_Keywords_t));
00987 ptr_if_kws = ptr_if_kws->next;
00988 ptr_if_kws->next = (Instruction_File_Keywords_t*)NULL;
00989 }
00990 ptr_if_kws->next= NULL;
00991
00992
00993
00994
00995 (void)strtok(line, ":");
00996
00997 sscanf( strtok(NULL,","), "%d", &(ptr_if_kws->dec_apid));
00998
00999
01000 strcpy( kwlong, strtok(NULL,","));
01001
01002 remove_blanks( kwlong, new_string);
01003 strcpy(ptr_if_kws->kw_longname, new_string);
01004 strcat(ptr_if_kws->kw_longname,"\0");
01005
01006
01007 strcpy( kwshort, strtok(NULL,"\n"));
01008
01009 remove_blanks( kwshort, new_string);
01010 strcpy(ptr_if_kws->kw_shortname,new_string);
01011 strcat(ptr_if_kws->kw_shortname,"\0");
01012
01013 #ifdef DEBUG_LM3S
01014 printf("read_isf_data:keywordshort : <%-s>\n", ptr_if_kws->kw_shortname);
01015 printf("read_isf_data:keywordlong : <%-s>\n",ptr_if_kws->kw_longname);
01016 #endif
01017
01018 }
01019 else
01020 {
01021 printkerr("WARNING:Miss parsing line:<%s> from instruction file. Skip line and continue processing.\n",line);
01022 }
01023
01024 }
01025
01026
01027 fclose(file_ptr);
01028
01029
01030 if (!strcmp(ptr_isf->inst_filename,""))
01031 {
01032 printkerr("ERROR:bad templatename setting <%s> from instruction file.\n",ptr_isf->inst_filename);
01033 exit_flag=1;
01034 }
01035 if (!strcmp(ptr_isf->series_name,""))
01036 {
01037 printkerr("ERROR:bad seriesname setting <%s> from instruction file.\n",ptr_isf->series_name);
01038 exit_flag=1;
01039 }
01040 if (!strcmp(ptr_isf->owner,""))
01041 {
01042 printkerr("ERROR:bad owner setting <%s> from instruction file.\n",ptr_isf->owner);
01043 exit_flag=1;
01044 }
01045 if (!strcmp(ptr_isf->author, ""))
01046 {
01047 printkerr("ERROR:bad author setting <%s> from instruction file.\n",ptr_isf->author);
01048 exit_flag=1;
01049 }
01050 if (ptr_isf->interval <= 0)
01051 {
01052 printkerr("ERROR:bad interval setting <%d> from instruction file.\n",ptr_isf->interval);
01053 exit_flag=1;
01054 }
01055
01056
01057 if (exit_flag)
01058 {
01059 printkerr("ERROR: Got exit flag. Exiting from executable.\n",line);
01060 return ((Instruction_File_Data_t *)(NULL));
01061 }
01062 #ifdef DEBUG_LM3S
01063 printf("read_isf_data:templatename : <%s>\n",ptr_isf->inst_filename);
01064 printf("read_isf_data:seriesname : <%s>\n",ptr_isf->series_name);
01065 printf("read_isf_data:owner : <%s>\n", ptr_isf->owner);
01066 printf("read_isf_data:author : <%s>\n",ptr_isf->author);
01067 printf("read_isf_data:description : <%s>\n",ptr_isf->packet_description);
01068 printf("read_isf_data:interval : <%d>\n",ptr_isf->interval);
01069 #endif
01070
01071
01072 return(ptr_isf);
01073 }
01074
01075
01076
01077
01078
01079
01080 void remove_blanks( char string[HKLMS_LONG_KEYWORD_NAME_SIZE], char *new_str)
01081 {
01082 char new_string[HKLMS_LONG_KEYWORD_NAME_SIZE];
01083 int i,j;
01084 for(j=0; j < HKLMS_LONG_KEYWORD_NAME_SIZE; new_string[j]='\0',j++);
01085
01086 for(j=0, i=0;string[i];i++)
01087 {
01088 if(string[i] == ' ')
01089 {
01090 ;
01091 }
01092 else
01093 {
01094 new_string[j]=string[i];
01095 j++;
01096 }
01097 }
01098 strcat(new_string,"\0");
01099 strcpy(new_str,new_string);
01100 }
01101
01102
01103
01104 int get_minute_from_pkttime(double tc_sec)
01105 {
01106
01107 int min;
01108 char at[HKLMS_PACKET_TIME_STR];
01109
01110
01111 (void)sprint_time (at, tc_sec, "UTC", 0);
01112 strcat(at,"\0");
01113
01114
01115
01116 sscanf(at,"%*d.%*d.%*d_%*hd:%d",&min);
01117 return (min);
01118 }
01119
01120
01121
01122 int get_seconds_from_pkttime(double tc_sec)
01123 {
01124
01125 int sec;
01126 char at[HKLMS_PACKET_TIME_STR];
01127
01128
01129 (void)sprint_time (at, tc_sec, "UTC", 0);
01130 strcat(at,"\0");
01131
01132
01133
01134 sscanf(at,"%*d.%*d.%*d_%*hd:%*d:%d",&sec);
01135 return (sec);
01136 }
01137
01138
01139
01140 HK_KW_Data_t * save_kw_data(TIME start_pkt_time, Instruction_File_Data_t *ifs, HK_Keyword_t *kw)
01141 {
01142
01143 HK_KW_Data_t *ptr_kw_data,*top_kw_data;
01144 HK_Keyword_t *hk_kw;
01145 Instruction_File_Keywords_t *kwif;
01146
01147
01148 top_kw_data=NULL;
01149 kwif= ifs->kw_if;
01150
01151
01152 while (kwif)
01153 {
01154 #ifdef DEBUG_LM3S
01155 printf("save_kw_data: longname:%s values in HK_KW_Data_t structure.\n",kwif->kw_longname);
01156 #endif
01157
01158 if(top_kw_data == NULL)
01159 {
01160 top_kw_data = ptr_kw_data = malloc(sizeof(HK_KW_Data_t));
01161 ptr_kw_data->next = (HK_KW_Data_t*)NULL;
01162 ptr_kw_data->kw_values = (HK_KW_Data_Values_t*)NULL;
01163 }
01164 else
01165 {
01166 ptr_kw_data->next = malloc(sizeof(HK_KW_Data_t));
01167 ptr_kw_data = ptr_kw_data->next;
01168 ptr_kw_data->next = (HK_KW_Data_t*)NULL;
01169 ptr_kw_data->kw_values = (HK_KW_Data_Values_t*)NULL;
01170 }
01171
01172
01173 ptr_kw_data->start_pkt_time = start_pkt_time;
01174 strcpy(ptr_kw_data->longname,kwif->kw_longname);
01175 strcpy(ptr_kw_data->shortname,kwif->kw_shortname);
01176
01177
01178 for(hk_kw=kw;hk_kw; hk_kw=hk_kw->next)
01179 {
01180 if (!strcmp(hk_kw->name, kwif->kw_longname))
01181 {
01182 break;
01183 }
01184 }
01185
01186
01187 kwif=kwif->next;
01188 }
01189 return (top_kw_data);
01190 }
01191
01192
01193
01194 void save_kw_data_values(HK_KW_Data_t *top_kw_data, TIME pkt_time, HK_Keyword_t *kw )
01195 {
01196
01197
01198
01199
01200
01201
01202
01203 HK_KW_Data_Values_t *ptr_kw_data_values;
01204 HK_KW_Data_t *ptr_kw_data;
01205 HK_Keyword_t *ptr_kw;
01206
01207
01208 for( ptr_kw_data= top_kw_data; ptr_kw_data;ptr_kw_data=ptr_kw_data->next)
01209 {
01210 for(ptr_kw=kw; ptr_kw; ptr_kw=ptr_kw->next)
01211 {
01212
01213 if(!strcmp(ptr_kw->name, ptr_kw_data->longname))
01214 {
01215 #ifdef DEBUG_LM3S
01216 printf("save_kw_data_values:looking for longname in HK_KW_Data_Values_t: %s \n",ptr_kw_data->longname);
01217 printf("save_kw_data_values:found match in HK_Keyword_t: %s \n",ptr_kw->name);
01218 #endif
01219
01220
01221 ptr_kw_data_values=ptr_kw_data->kw_values;
01222 if(ptr_kw_data_values == NULL)
01223 {
01224 ptr_kw_data_values = malloc(sizeof(HK_KW_Data_Values_t));
01225 ptr_kw_data_values->next = (HK_KW_Data_Values_t*)NULL;
01226 ptr_kw_data->kw_values=ptr_kw_data_values;
01227 }
01228 else
01229 {
01230
01231 while(ptr_kw_data_values->next)
01232 {
01233 ptr_kw_data_values=ptr_kw_data_values->next;
01234 }
01235
01236
01237 ptr_kw_data_values->next = malloc(sizeof(HK_KW_Data_Values_t));
01238 ptr_kw_data_values = ptr_kw_data_values->next;
01239 ptr_kw_data_values->next = (HK_KW_Data_Values_t*)NULL;
01240 }
01241
01242
01243 ptr_kw_data_values->pkt_time=pkt_time;
01244 ptr_kw_data_values->eng_type=ptr_kw->eng_type;
01245
01246 if(ptr_kw_data_values->eng_type == KW_TYPE_DOUBLE)
01247 {
01248 ptr_kw_data_values->eng_value.double_val=ptr_kw->eng_value.double_val;
01249 }
01250 else if(ptr_kw_data_values->eng_type == KW_TYPE_UINT8)
01251 {
01252 ptr_kw_data_values->eng_value.double_val= (double)ptr_kw->eng_value.uint8_val;
01253 }
01254 else if(ptr_kw_data_values->eng_type == KW_TYPE_INT8)
01255 {
01256 ptr_kw_data_values->eng_value.double_val= (double)ptr_kw->eng_value.int8_val;
01257 }
01258 else if(ptr_kw_data_values->eng_type == KW_TYPE_UINT16)
01259 {
01260 ptr_kw_data_values->eng_value.double_val= (double)ptr_kw->eng_value.uint16_val;
01261 }
01262 else if(ptr_kw_data_values->eng_type == KW_TYPE_INT16)
01263 {
01264 ptr_kw_data_values->eng_value.double_val= (double)ptr_kw->eng_value.int16_val;
01265 }
01266 else if(ptr_kw_data_values->eng_type == KW_TYPE_UINT32)
01267 {
01268 ptr_kw_data_values->eng_value.double_val=(double)ptr_kw->eng_value.uint32_val;
01269 }
01270 else if(ptr_kw_data_values->eng_type == KW_TYPE_INT32)
01271 {
01272 ptr_kw_data_values->eng_value.double_val=(double)ptr_kw->eng_value.int32_val;
01273 }
01274 else if(ptr_kw_data_values->eng_type == KW_TYPE_FLOAT)
01275 {
01276 ptr_kw_data_values->eng_value.double_val=(double)ptr_kw->eng_value.float_val;
01277 }
01278 else
01279 {
01280 ptr_kw_data_values->eng_value.double_val=0.00;
01281 printkerr("WARNING at %s, line %d: Type '%d' not handled. Probably String or Time keywords used. This is not allowed.\n",
01282 __FILE__, __LINE__, ptr_kw_data_values->eng_type);
01283 }
01284
01285 #ifdef DEBUG_LM3S
01286 printf("save_kw_data_values:ptr_kw_data_values->pkt_time is %f\n",ptr_kw_data_values->pkt_time);
01287 printf("save_kw_data_values:ptr_kw_data_values->eng.value. is %f\n",ptr_kw_data_values->eng_value.double_val);
01288 #endif
01289
01290
01291 break;
01292
01293 }
01294 }
01295 }
01296 }
01297
01298
01299
01300
01301 void free_all_kw_data(HK_KW_Data_t *top_kw_data_ptr)
01302 {
01303 HK_KW_Data_t *ptr_kw_data, *temp_ptr_kw_data;
01304 HK_KW_Data_Values_t *ptr_kw_data_values, *temp_ptr_kw_data_values;
01305
01306
01307 for ( ptr_kw_data= top_kw_data_ptr; ptr_kw_data;)
01308 {
01309 #ifdef DEBUG_LM3S
01310 printf("free_all_kw_data:1:at HK_Data_t node with start time :%f\n", ptr_kw_data->start_pkt_time);
01311 printf("free_all_kw_data:2:at HK_Data_t node with longname :%s\n", ptr_kw_data->longname);
01312 #endif
01313
01314 for(ptr_kw_data_values = ptr_kw_data->kw_values;ptr_kw_data_values;)
01315 {
01316 #ifdef DEBUG_LM3S
01317 printf("free_all_kw_data:3:will free HK_Data_Values_t node:%p\n",ptr_kw_data_values);
01318 printf("free_all_kw_data:4:where HK_Data_Values_t node pkt time:%f\n", ptr_kw_data_values->pkt_time);
01319 #endif
01320
01321 temp_ptr_kw_data_values = ptr_kw_data_values->next;
01322 free(ptr_kw_data_values);
01323 ptr_kw_data_values=temp_ptr_kw_data_values;
01324 }
01325
01326
01327 temp_ptr_kw_data=ptr_kw_data->next;
01328
01329 #ifdef DEBUG_LM3S
01330 printf("free_all_kw_data:5:will free HK_Data_t node:%p\n",ptr_kw_data);
01331 #endif
01332
01333 free(ptr_kw_data);
01334 ptr_kw_data=temp_ptr_kw_data;
01335 }
01336
01337 }
01338
01339
01340
01341 void save_m3sd_data(HK_KW_Data_t *top_ptr_kw_data ,HK_Keyword_M3SD_Data_t **kw_data_head)
01342 {
01343
01344 HK_KW_Data_t *ptr_kw_data ;
01345 HK_Keyword_M3SD_Data_t *ptr_m3sd_data;
01346 HK_Keyword_M3SD_Data_t *prev_ptr_m3sd_data;
01347
01348
01349 ptr_m3sd_data=*kw_data_head;
01350
01351
01352
01353 for(ptr_kw_data=top_ptr_kw_data; ptr_kw_data; ptr_kw_data= ptr_kw_data->next)
01354 {
01355
01356 if(!ptr_m3sd_data)
01357 {
01358 *kw_data_head = ptr_m3sd_data = malloc(sizeof(HK_Keyword_M3SD_Data_t));
01359 ptr_m3sd_data->next = (HK_Keyword_M3SD_Data_t*)NULL;
01360 ptr_m3sd_data->m3sd_values = (HK_Keyword_M3SD_Values_t*)NULL;
01361 }
01362 else
01363 {
01364
01365 for(prev_ptr_m3sd_data =ptr_m3sd_data = *kw_data_head; ptr_m3sd_data;prev_ptr_m3sd_data=ptr_m3sd_data,ptr_m3sd_data=ptr_m3sd_data->next);
01366 prev_ptr_m3sd_data->next = ptr_m3sd_data = malloc(sizeof(HK_Keyword_M3SD_Data_t));
01367 ptr_m3sd_data->next = (HK_Keyword_M3SD_Data_t*)NULL;
01368 ptr_m3sd_data->m3sd_values = (HK_Keyword_M3SD_Values_t*)NULL;
01369 }
01370
01371
01372 ptr_m3sd_data->start_pkt_time = ptr_kw_data->start_pkt_time;
01373 ptr_m3sd_data->number_points = (int)get_number_points(ptr_kw_data->kw_values);
01374 #ifdef DEBUG_LM3S
01375 printf("save_m3sd_data:set start time %f\n", ptr_m3sd_data->start_pkt_time);
01376 printf("save_m3sd_data:set number of points %d\n", ptr_m3sd_data->number_points);
01377 #endif
01378 break;
01379 }
01380
01381 }
01382
01383
01384
01385 void save_m3sd_values(HK_KW_Data_t *top_ptr_kw_data, HK_Keyword_M3SD_Data_t **kw_data_head)
01386 {
01387 HK_KW_Data_t *ptr_kw_data;
01388 HK_Keyword_M3SD_Data_t *ptr_m3sd_data;
01389 HK_Keyword_M3SD_Values_t *ptr_m3sd_values;
01390
01391
01392 ptr_m3sd_data=*kw_data_head;
01393
01394 for( ptr_kw_data= top_ptr_kw_data; ptr_kw_data;ptr_kw_data=ptr_kw_data->next)
01395 {
01396 for(ptr_m3sd_data=*kw_data_head; ptr_m3sd_data; ptr_m3sd_data=ptr_m3sd_data->next)
01397 {
01398
01399 if(((int)ptr_m3sd_data->start_pkt_time) == ((int)ptr_kw_data->start_pkt_time))
01400 {
01401
01402
01403
01404 ptr_m3sd_values= ptr_m3sd_data->m3sd_values;
01405
01406 if(!ptr_m3sd_values)
01407 {
01408
01409 ptr_m3sd_values = malloc(sizeof(HK_Keyword_M3SD_Values_t));
01410 ptr_m3sd_values->next = (HK_Keyword_M3SD_Values_t*)NULL;
01411
01412
01413 ptr_m3sd_data->m3sd_values= ptr_m3sd_values;
01414 }
01415 else
01416 {
01417
01418 while(ptr_m3sd_values->next)
01419 {
01420 ptr_m3sd_values=ptr_m3sd_values->next;
01421 }
01422
01423 ptr_m3sd_values->next = malloc(sizeof(HK_Keyword_M3SD_Values_t));
01424 ptr_m3sd_values = ptr_m3sd_values->next;
01425 ptr_m3sd_values->next = (HK_Keyword_M3SD_Values_t*)NULL;
01426 }
01427
01428
01429 strcpy(ptr_m3sd_values->kw_longname,ptr_kw_data->longname);
01430 strcpy(ptr_m3sd_values->kw_shortname, ptr_kw_data->shortname);
01431 ptr_m3sd_values->min = get_min_value(ptr_kw_data->kw_values);
01432 ptr_m3sd_values->max = get_max_value(ptr_kw_data->kw_values);
01433 ptr_m3sd_values->mean = get_mean_value(ptr_kw_data->kw_values);
01434 ptr_m3sd_values->stdev = get_stdev_value(ptr_kw_data->kw_values);
01435 break;
01436 }
01437 }
01438 }
01439 }
01440
01441
01442
01443
01444
01445
01446
01447
01448 void set_env_variables()
01449 {
01450 FILE * fp;
01451 char envfile[500], s1[256],s2[256],s3[256], line[256];
01452
01453
01454 strcpy(envfile, ENVFILE );
01455
01456 if(!(fp=fopen(envfile, "r")))
01457 {
01458 printf("ERROR:Can't open environment variable file <%s>. Check setting is correct.\n", envfile);
01459 exit(0);
01460 }
01461
01462 while( fgets(line, MAXLINE_IN_FILE, fp) != NULL )
01463 {
01464 if (!strncmp(line, "#", 1))
01465 {
01466 continue;
01467 }
01468 else
01469 {
01470 sscanf(line, "%s %s %s ", s1,s2,s3);
01471
01472 setenv(s2, s3, 1);
01473 }
01474 }
01475
01476 fclose(fp);
01477 }
01478
01479
01480
01481
01482 int get_number_points(HK_KW_Data_Values_t *ptr_data_values)
01483 {
01484 int num_pts;
01485 for(num_pts=0; ptr_data_values; num_pts++, ptr_data_values=ptr_data_values->next);
01486 return(num_pts);
01487 }
01488
01489
01490
01491
01492 float get_min_value(HK_KW_Data_Values_t *ptr_data_values)
01493 {
01494 float min_value=0.0;
01495 for(min_value=(float)ptr_data_values->eng_value.double_val, ptr_data_values=ptr_data_values->next;
01496 ptr_data_values; ptr_data_values=ptr_data_values->next)
01497 {
01498 if(min_value > ptr_data_values->eng_value.double_val)
01499 {
01500 min_value= (float)ptr_data_values->eng_value.double_val;
01501 }
01502 }
01503 #ifdef DEBUG_LM3S
01504 printf("get_min_value(): MIN->eng.value. is %f\n",min_value);
01505 #endif
01506 return(min_value);
01507 }
01508
01509
01510
01511
01512 float get_max_value(HK_KW_Data_Values_t *ptr_data_values)
01513 {
01514 float max_value=0.0;
01515 for(max_value = (float)ptr_data_values->eng_value.double_val, ptr_data_values=ptr_data_values->next;
01516 ptr_data_values; ptr_data_values=ptr_data_values->next)
01517 {
01518 if( ptr_data_values->eng_value.double_val > max_value)
01519 {
01520 max_value= (float)(ptr_data_values->eng_value.double_val);
01521 }
01522 }
01523 #ifdef DEBUG_LM3S
01524 printf("get_max_value(): MAX->eng.value. is %f\n",max_value);
01525 #endif
01526 return(max_value);
01527 }
01528
01529
01530
01531
01532 float get_mean_value(HK_KW_Data_Values_t *ptr_data_values)
01533 {
01534 double double_mean_value=0.0;
01535 float mean_value=0.0;
01536 float count=0.0;
01537
01538 for(; ptr_data_values; ptr_data_values=ptr_data_values->next)
01539 {
01540 double_mean_value += ptr_data_values->eng_value.double_val;
01541 count++;
01542 #ifdef DEBUG_LM3S
01543 printf("get_mean_value(): HK_KW_Data_Values->eng.value. is %f\n",ptr_data_values->eng_value.double_val);
01544 #endif
01545 }
01546
01547 mean_value= (float)double_mean_value;
01548
01549 #ifdef DEBUG_LM3S
01550 printf("get_mean_value(): MEAN->eng.value. is %f\n",mean_value/count);
01551 #endif
01552
01553
01554 return(mean_value/count);
01555 }
01556
01557
01558
01559
01560 float get_stdev_value(HK_KW_Data_Values_t *top_ptr_data_values)
01561 {
01562 HK_KW_Data_Values_t *ptr_data_values;
01563 double d_cal_value=0.0;
01564 double d_mean_sq_value=0.0;
01565 double d_mean_value=0.0;
01566 double d_sum_sq_value=0.0;
01567 double d_stdev_value=0.0;
01568 double d_sum_value=0.0;
01569 float stdev_value=0.0;
01570 float numpts=0.0;
01571 int int_numpts=0;
01572
01573
01574 for(ptr_data_values=top_ptr_data_values, numpts=0.0; ptr_data_values; ptr_data_values=ptr_data_values->next)
01575 {
01576 d_sum_sq_value += pow(ptr_data_values->eng_value.double_val, 2.0 );
01577 d_sum_value += ptr_data_values->eng_value.double_val;
01578 numpts++;
01579 int_numpts++;
01580 }
01581
01582 d_mean_value= d_sum_value/numpts;
01583
01584
01585 d_mean_sq_value = pow(d_mean_value, 2.0 );
01586
01587
01588 d_cal_value= (((d_sum_sq_value/(numpts - 1.0)) ) - (((numpts/(numpts - 1.0)) * d_mean_sq_value)));
01589 if (d_cal_value < 0.0 || int_numpts == 1)
01590 {
01591 d_stdev_value=0.0;
01592 }
01593 else
01594 {
01595 d_stdev_value = sqrt( d_cal_value );
01596 }
01597
01598
01599 stdev_value=(float)d_stdev_value;
01600
01601 #ifdef DEBUG_LM3S
01602 printf("get_stdev_value(): double:STANDARD-DEV->eng.value. is %15.15f\n",stdev_value);
01603 printf("get_stdev_value(): float:STANDARD-DEV->eng.value. is %15.15f\n",d_stdev_value);
01604 #endif
01605 return(stdev_value);
01606 }
01607
01608
01609
01610
01611
01612
01613 void write_m3sd_to_drms(HK_Keyword_M3SD_Data_t *top_m3sd_data_ptr, Instruction_File_Data_t *ptr_inst_file)
01614 {
01615 DRMS_Type_t keytype;
01616 DRMS_Type_Value_t key_anyval;
01617 DRMS_RecordSet_t *rs;
01618 DRMS_Record_t *rec;
01619 HK_Keyword_M3SD_Data_t *ptr_m3sd_data;
01620 HK_Keyword_M3SD_Values_t *ptr_m3sd_values;
01621 TIME pkt_time;
01622 char keyname[HKLMS_LONG_KEYWORD_NAME_SIZE];
01623 char query[HKLMS_MAX_DSNAME_STR];
01624 int status, status_create, status_closed;
01625 int cntrec;
01626 int i;
01627
01628
01629 if(!ptr_inst_file->series_name)
01630 {
01631 printkerr("ERROR at %s, line %d: Bad value for data series name:"
01632 "<%s>. Exiting program.\n", __FILE__,__LINE__, ptr_inst_file->series_name);
01633 return;
01634 }
01635 else
01636 {
01637 strcpy(query, ptr_inst_file->series_name) ;
01638 #ifdef DEBUG_LM3S
01639 printf("write_m3sd_to_drms:ptr_inst_file->series_name:<%s> query:<%s>\n", ptr_inst_file->series_name,query);
01640 #endif
01641 }
01642
01643
01644 for ( cntrec=0, ptr_m3sd_data=top_m3sd_data_ptr;ptr_m3sd_data; cntrec++,ptr_m3sd_data=ptr_m3sd_data->next);
01645
01646
01647 printf(". . . writing %d records to drms data series <%s>\n",cntrec, query);
01648
01649
01650
01651 rs = drms_create_records( drms_env, cntrec, query, DRMS_PERMANENT, &status_create);
01652 if (status_create)
01653 {
01654 printkerr("ERROR at %s, line %d: drms_create_records "
01655 "returned a bad status. Status returned:<%d>. \n",
01656 __FILE__,__LINE__,status_create);
01657 if (DRMS_ERROR_UNKNOWNSERIES == status_create)
01658 {
01659 printkerr("ERROR at %s, line %d: Unknown DRMS Series. "
01660 "Please create series:<%s> and try again. \n",
01661 __FILE__,__LINE__,query);
01662 }
01663 return;
01664 }
01665
01666 for ( i=0, ptr_m3sd_data=top_m3sd_data_ptr;ptr_m3sd_data; ptr_m3sd_data=ptr_m3sd_data->next, i++)
01667 {
01668 #ifdef DEBUG_LM3S
01669 printf("write_m3sd_to_drms:HK_Keyword_M3SD_Values_t node start_pkt_time<%f>\n",ptr_m3sd_data->start_pkt_time);
01670 #endif
01671
01672
01673 rec = rs->records[i];
01674
01675 pkt_time= ptr_m3sd_data->start_pkt_time;
01676
01677
01678 keytype= DRMS_TYPE_TIME;
01679 strcpy(keyname, "T_START");
01680
01681 key_anyval.time_val= pkt_time;
01682
01683 status = drms_setkey(rec, keyname, keytype, &key_anyval);
01684
01685 check_status_drms_set(status, keyname);
01686
01687
01688 keytype= DRMS_TYPE_TIME;
01689 strcpy(keyname, "DATE");
01690
01691 key_anyval.time_val= CURRENT_SYSTEM_TIME;
01692
01693 status = drms_setkey(rec, keyname, keytype, &key_anyval);
01694
01695
01696
01697
01698
01699 keytype= DRMS_TYPE_INT;
01700 strcpy(keyname, "NUMPTS");
01701
01702 key_anyval.int_val = (int32_t)(ptr_m3sd_data->number_points);
01703
01704 status = drms_setkey(rec, keyname, keytype, &key_anyval);
01705
01706 check_status_drms_set(status, keyname);
01707
01708 #ifdef DEBUG_LM3S
01709 printf("write_m3s_drms: NUMPTS is %d\n",(int32_t)(ptr_m3sd_data->number_points));
01710 #endif
01711
01712
01713 for(ptr_m3sd_values = ptr_m3sd_data->m3sd_values;ptr_m3sd_values;ptr_m3sd_values=ptr_m3sd_values->next)
01714 {
01715
01716
01717 keytype= DRMS_TYPE_FLOAT;
01718
01719
01720 sprintf(keyname,"%s_%s", ptr_m3sd_values->kw_shortname,"MIN");
01721 strcat(keyname,"\0");
01722 key_anyval.float_val = ptr_m3sd_values->min;
01723 status = drms_setkey(rec, keyname, keytype, &key_anyval);
01724
01725 check_status_drms_set(status, keyname);
01726
01727
01728 sprintf(keyname,"%s_%s", ptr_m3sd_values->kw_shortname,"MAX");
01729 strcat(keyname,"\0");
01730 key_anyval.float_val = ptr_m3sd_values->max;
01731 status = drms_setkey(rec, keyname, keytype, &key_anyval);
01732
01733 check_status_drms_set(status, keyname);
01734
01735
01736 sprintf(keyname,"%s_%s", ptr_m3sd_values->kw_shortname,"MEAN");
01737 strcat(keyname,"\0");
01738 key_anyval.float_val = ptr_m3sd_values->mean;
01739 status = drms_setkey(rec, keyname, keytype, &key_anyval);
01740
01741 check_status_drms_set(status, keyname);
01742
01743
01744 sprintf(keyname,"%s_%s", ptr_m3sd_values->kw_shortname,"SD");
01745 strcat(keyname,"\0");
01746 key_anyval.float_val = ptr_m3sd_values->stdev;
01747 status = drms_setkey(rec, keyname, keytype, &key_anyval);
01748
01749 check_status_drms_set(status, keyname);
01750
01751 }
01752
01753 }
01754
01755
01756 status_closed = drms_close_records(rs, DRMS_INSERT_RECORD);
01757 if (status_closed)
01758 {
01759 printkerr("ERROR at %s, line %d: drms_close_record "
01760 "returned a bad status. Status returned:<%d>. \n",
01761 __FILE__,__LINE__,status_closed);
01762 }
01763
01764 }
01765
01766
01767
01768
01769
01770
01771 void check_status_drms_set(int status, char *kwn)
01772 {
01773 if (status)
01774 {
01775 printkerr("ERROR at %s, line %d: drms_setkey for keyword "
01776 "returned a bad status. Status returned:<%d> when setting "
01777 "keyword:<%s>\n", __FILE__,__LINE__,status, kwn);
01778 }
01779 return;
01780 }
01781
01782
01783
01784
01785 void get_next_pkt_time(TIME p_time, int intval , TIME *start, TIME *end)
01786 {
01787 TIME start_day, start_pt_time, end_pt_time, find_time;
01788 char at[HKLMS_PACKET_TIME_STR];
01789 char start_str[HKLMS_PACKET_TIME_STR];
01790 int year, month, day;
01791
01792
01793
01794 year=get_yr_from_pkttime(p_time);
01795 month=get_month_from_pkttime(p_time);
01796 day=get_day_from_pkttime(p_time);
01797
01798
01799 sprintf(start_str,"%4.4d.%02.2d.%02.2d_00:00:00_UTC", year,month,day);
01800 strcat(start_str,"\0");
01801
01802
01803 start_day = sscan_time(start_str);
01804
01805
01806 find_time=start_day;
01807 while( find_time < p_time)
01808 {
01809 find_time += intval;
01810 }
01811
01812
01813 end_pt_time=find_time;
01814 start_pt_time= find_time - intval;
01815 (void)sprint_time (at, start_pt_time, "UTC", 0);
01816 strcat(at,"\0");
01817 (void)sprint_time (at, end_pt_time, "UTC", 0);
01818 strcat(at,"\0");
01819
01820
01821 *start= start_pt_time;
01822 *end=end_pt_time;
01823 }
01824
01825
01826
01827
01828
01829
01830 void add_dayfile_data(char *dayfile, TIME start_pkt_time,HK_KW_Data_t *topkwdataptr,int interval)
01831 {
01832
01833 CCSDS_Packet_t ccsds;
01834 FILE *file_ptr;
01835 HK_Keyword_t *kw_head,*kw;
01836 TIME pkt_time;
01837 char file_version_number[HKLMS_MAX_PVN_SIZE];
01838 char packet_version_number[HKLMS_MAX_PVN_SIZE];
01839 char pkt_date[MAX_SIZE_PKT_DATE];
01840 char *ptr_fvn;
01841 int apid;
01842 int factor;
01843 int i,j,k,y,s;
01844 int packet_length;
01845 unsigned char hk_pkt_buffer[HKLMS_MAX_PKT_SIZE];
01846 unsigned short s_hk_pkt_buffer[HKLMS_MAX_PKT_SIZE];
01847 unsigned short *word_ptr;
01848 unsigned char *pointer_read_in_buffer;
01849 unsigned long int x;
01850
01851 #ifdef DEBUG_LM3S
01852 char at[HKLMS_PACKET_TIME_STR];
01853 char atn[HKLMS_PACKET_TIME_STR];
01854 TIME next_pkt_time;
01855
01856
01857 printf("add_dayfile_data: argument passed: dayfile:%s:\n",dayfile);
01858 printf("add_dayfile_data: argument passed: start_pkt_time:%f:\n",start_pkt_time);
01859 printf("add_dayfile_data: argument passed: interval in seconds is :%d:\n",interval);
01860 #endif
01861
01862
01863 file_ptr=fopen(dayfile,"r");
01864 if (!file_ptr)
01865 {
01866 printkerr("ERROR at %s, line %d: Please check filename and directory is correct. "
01867 " Could not get -in- directory and filename: "
01868 "<%s>. Example format for -in- file: in=/home/rock/20080909.0x013. Exiting execution.\n",
01869 __FILE__,__LINE__, dayfile);
01870 return;
01871 }
01872
01873
01874 pointer_read_in_buffer = (unsigned char *) malloc(sizeof(unsigned char) * HKLMS_READ_ARRAY_SIZE);
01875
01876
01877 for(x=0; x < HKLMS_READ_ARRAY_SIZE;x++) pointer_read_in_buffer[x]=0; ;
01878 for(x = 0 ; fread(pointer_read_in_buffer + x,1,1,file_ptr) ; x++)
01879 {
01880 ;
01881 if( x == HKLMS_READ_ARRAY_SIZE - 1)
01882 {
01883 printkerr("ERROR at %s, line %d: Array for reading dayfile is too small. :"
01884 "<%lu>\n", __FILE__,__LINE__, x + 1);
01885 printkerr("Break up large dayfile using dd command. : dd if<large-file> "
01886 " of=<small-file-1> bs=<packet_size + 7> count=<i.e., 1000> skip=<0,1,etc>\n");
01887 return;
01888 }
01889 }
01890
01891 *(pointer_read_in_buffer + x) = '\0';
01892
01893
01894 fclose(file_ptr);
01895
01896
01897 for(k=0,factor=0,packet_length=0; ; k++)
01898 {
01899
01900 factor = k * (packet_length + 6 + 1 ) ;
01901
01902
01903 if (*(pointer_read_in_buffer+5+factor) == '\0')
01904 {
01905
01906 break;
01907 }
01908
01909
01910 packet_length= *(pointer_read_in_buffer+5+factor);
01911
01912
01913
01914 apid = (unsigned short int)( (*(pointer_read_in_buffer+1+factor)) & 0x00FF );
01915
01916 apid |= (unsigned short int)( (*(pointer_read_in_buffer+0+factor) << 8) & 0xFF00 );
01917 apid &= 0x07FF;
01918
01919
01920
01921 if(check_for_sdo_apid(apid))
01922 {
01923 sprintf(packet_version_number,"%s","not applicable");
01924 }
01925 else
01926 {
01927
01928 if (( *(pointer_read_in_buffer+14+factor) == 0) && (*(pointer_read_in_buffer+15+factor) == 0))
01929 {
01930 printkerr("Warning at %s, line %d: Getting 0.0 for packet version number."
01931 "in packet data. Skip processing this packet, don't load to "
01932 "DRMS and get next packet", __FILE__,__LINE__);
01933 continue;
01934 }
01935
01936 if ( *(pointer_read_in_buffer+14+factor) == 0 && *(pointer_read_in_buffer+15+factor))
01937 {
01938 printkerr("Warning at %s, line %d: Getting 0 for whole number(i.e.,0.1) for "
01939 "packet version number in packet data. Skip processing this packet,"
01940 "don't load to DRMS and get next packet", __FILE__,__LINE__);
01941 continue;
01942 }
01943
01944 sprintf(packet_version_number,"%03d.%03d",*(pointer_read_in_buffer+14+factor), *(pointer_read_in_buffer+15+factor));
01945 strcat(packet_version_number,"\0");
01946 }
01947
01948
01949 for(i=0; i < HKLMS_MAX_PKT_SIZE;i++) hk_pkt_buffer[i]=0x00;
01950
01951
01952 for (i =0 + factor, j=0 ; i < packet_length + 6 + 1 + factor; i++)
01953 {
01954
01955 hk_pkt_buffer[j++]= *(pointer_read_in_buffer+i);
01956 }
01957
01958
01959
01960
01961 for (i=0, y=0 ; i < packet_length + 6 + 1 ; i += 2, y++)
01962 {
01963 s_hk_pkt_buffer[y] = (unsigned short)(hk_pkt_buffer[i + 1] << 8 & 0xFF00 );
01964 s_hk_pkt_buffer[y] = (unsigned short)((hk_pkt_buffer[i] & 0x00FF) + s_hk_pkt_buffer[y]) ;
01965 }
01966
01967 word_ptr = s_hk_pkt_buffer;
01968
01969 s = decode_hk_keywords(word_ptr, apid, &ccsds.keywords);
01970 if (s)
01971 {
01972 printkerr("ERROR at %s, line %d: decode_hk_keyword function returned"
01973 " error status number <%d>\n\n", __FILE__,__LINE__, s);
01974 printf("ERROR at %s, line %d: decode_hk_keyword function returned error status number <%d>\n\n", __FILE__,__LINE__, s);
01975 continue;
01976 }
01977
01978 kw_head= ccsds.keywords;
01979
01980
01981 if(check_for_sdo_apid(apid))
01982 {
01983
01984
01985 (void)sprint_time (pkt_date, get_packet_time(word_ptr), "TAI", 0);
01986
01987 ptr_fvn=find_fvn_from_shcids(global_shcids_vn, pkt_date, apid);
01988 }
01989 else
01990 {
01991
01992 ptr_fvn=find_file_version_number(global_gtcids_vn, packet_version_number,apid);
01993 }
01994
01995 strcpy(file_version_number,ptr_fvn);
01996
01997
01998
01999
02000
02001 kw=kw_head;
02002
02003
02004 (void)get_packet_time_for_df(kw, &pkt_time);
02005
02006 #ifdef DEBUG_LM3S
02007 (void)sprint_time (at, pkt_time, "UTC", 0);
02008 strcat(at,"\0");
02009 (void)sprint_time (atn, next_pkt_time, "UTC", 0);
02010 strcat(atn,"\0");
02011 #endif
02012
02013
02014 if(pkt_time >= start_pkt_time && pkt_time < (start_pkt_time+interval) )
02015 {
02016
02017 #ifdef DEBUG_LM3S
02018
02019 printf("add_dayfile_data:GET PACKET TIME:PACKET_TIME:%s:\n",at);
02020 printf("add_dayfile_data:pkt_time:%f:\n",pkt_time);
02021 (void)sprint_time (at, start_pkt_time, "UTC", 0);
02022 strcat(at,"\0");
02023 printf("add_dayfile_data:start_pkt_time%f:%s:\n",start_pkt_time, at);
02024 #endif
02025
02026
02027
02028 (void)save_kw_data_values(topkwdataptr, pkt_time, kw );
02029 }
02030
02031 }
02032
02033 return;
02034 }
02035
02036
02037
02038
02039
02040
02041 int get_dayfile_to_process(HK_KW_Data_t *kwdataptr, int apid,int prev_next_flag, char *dayfile, char *src)
02042 {
02043
02044 DRMS_RecordSet_t *rs;
02045 DRMS_Record_t *rec;
02046 DRMS_Segment_t *record_segment;
02047 char dayfile_pkttime_str[HKLMS_PACKET_TIME_STR];
02048 char dir_filename_dayfile[HKLMS_MAX_FILE_NAME];
02049 char nquery[HKLMS_QUERY_STRING_SIZE];
02050 char strapid[HKLMS_APID_SIZE];
02051 int drms_status;
02052 int i;
02053
02054 #ifdef DEBUG_LM3S
02055 printf("get_dayfile_to_process:apid:%d is used to create query to lookup dayfile.\n",apid);
02056 printf("get_dayfile_to_process:src:%s is used to create query to lookup dayfile.\n",src);
02057 #endif
02058
02059
02060 for(i=0;i < HKLMS_MAX_FILE_NAME; *(dayfile+i) ='\0', i++);
02061
02062
02063 if (!strcmp(src, "rtmon"))
02064 {
02065 strcpy(dayfile,"");
02066 return(HKLMS_NOT_FOUND_DAYFILE);
02067 }
02068
02069
02070 sprintf(strapid,"%3.3d", apid);
02071
02072
02073 if (prev_next_flag == HKLMS_NEXT_DAYFILE)
02074 {
02075 sprintf(dayfile_pkttime_str,"%4.4d.%02.2d.%02.2d_00:00:00_UTC", get_yr_from_pkttime(kwdataptr->start_pkt_time),
02076 get_month_from_pkttime(kwdataptr->start_pkt_time), get_day_from_pkttime(kwdataptr->start_pkt_time) + HKLMS_NEXT_DAYFILE);
02077 }
02078 else
02079 {
02080 sprintf(dayfile_pkttime_str,"%4.4d.%02.2d.%02.2d_00:00:00_UTC", get_yr_from_pkttime(kwdataptr->start_pkt_time),
02081 get_month_from_pkttime(kwdataptr->start_pkt_time), get_day_from_pkttime(kwdataptr->start_pkt_time) );
02082 }
02083 strcat(dayfile_pkttime_str,"\0");
02084 #ifdef DEBUG_LM3S
02085 printf("get_dayfile_to_process:dayfile_pkttime_str is <%s>. This used to lookup previous or next day's dayfile\n",dayfile_pkttime_str);
02086 #endif
02087
02088
02089 if (apid < 32)
02090 {
02091
02092 sprintf(nquery,"%s[%s][%s][%s]","hmi.hk_dayfile",dayfile_pkttime_str,strapid,src);
02093 }
02094 else if (apid <= 64 && apid > 31)
02095 {
02096
02097 sprintf(nquery,"%s[%s][%s][%s]","aia.hk_dayfile",dayfile_pkttime_str,strapid,src);
02098 }
02099 else if (apid == 129)
02100 {
02101
02102 sprintf(nquery,"%s[%s][%s][%s]","sdo.hk_dayfile",dayfile_pkttime_str,strapid,src);
02103 }
02104 else
02105 {
02106 printkerr("ERROR at %s, line %d: When creating query to get previous or next "
02107 "day's dayfile. Did not find apid to be either hmi,aia or sdo. "
02108 "apid is <%d> src is <%s> \n", __FILE__,__LINE__, apid,src);
02109
02110 strcpy(dayfile,"");
02111 return(HKLMS_NOT_FOUND_DAYFILE);
02112 }
02113 #ifdef DEBUG_LM3S
02114 printf("get_dayfile_to_process:Query created to locate previous day of next day dayfile in drms is <%s>\n", nquery);
02115 #endif
02116
02117
02118 rs = drms_open_records(drms_env, nquery, &drms_status);
02119
02120
02121 if(rs->n < 1 || drms_status < 0)
02122 {
02123
02124 #ifdef DEBUG_LM3S
02125 printf("get_dayfile_to_process:Record query for dataseries is <%s>. Return status of query:<%d>. Did not find previous or next day's dayfile.\n", nquery, drms_status);
02126 #endif
02127
02128 strcpy(dayfile,"");
02129 return(HKLMS_NOT_FOUND_DAYFILE);
02130 }
02131
02132
02133 rec=rs->records[0];
02134
02135
02136 record_segment = drms_segment_lookup (rec, "FILE");
02137
02138
02139 if (!record_segment)
02140 {
02141
02142 printkerr("WARNING at %s, line %d: Record query to "
02143 "dataseries is <%s> for generic file segment. DID not locate "
02144 "segment name <FILE> when did drms_segment_lookup function.\n",
02145 __FILE__,__LINE__, nquery);
02146
02147 strcpy(dayfile,"");
02148 return(HKLMS_NOT_FOUND_DAYFILE);
02149 }
02150
02151
02152 (void)drms_segment_filename(record_segment,dir_filename_dayfile);
02153
02154 #ifdef DEBUG_LM3S
02155 printf("get_dayfile_to_process:absolute filename %s\n", dir_filename_dayfile);
02156 #endif
02157
02158
02159 strcpy(dayfile, dir_filename_dayfile);
02160 strcat(dayfile,"\0");
02161
02162
02163 drms_status = drms_close_records(rs, DRMS_FREE_RECORD);
02164
02165
02166 if (prev_next_flag == HKLMS_NEXT_DAYFILE)
02167 {
02168 printf(". . . reading dayfile <%s> to process data for next day's dayfile.\n",dayfile) ;
02169 }
02170 else if (prev_next_flag == HKLMS_PREVIOUS_DAYFILE)
02171 {
02172 printf(". . . reading dayfile <%s> to process data for previous day's dayfile.\n",dayfile) ;
02173 }
02174
02175
02176 return(HKLMS_FOUND_DAYFILE);
02177 }
02178