/* * * * To compile, * prompt> gcc -o ti2utc_from_file ti2utc_from_file.c -lsctimecal * * To run the command, * prompt> ti2utc_from_file ti_list.txt 20170301000000 * * The file "ti_list.txt" should contain a list of TIs in the hexadecimal form: * prompt> cat ti_list.txt * 1FD5B1CE * 1FD5B1ED * 1FD5B20C * 1FD5B22B * .... * ... * * The converted UTC times are dumped to a file "ti_list.txt_out" with the error status: * prompt> cat ti_list.txt_out * 20170326235950079306 0 * 20170326235950563687 0 * 20170326235951048067 0 * .... * ... * Here, the first column gives a converted UTC time in string and the second column * shows the status returned by ti2es(). 0 means that ti2es() converted normally. */ #include #include #include #include "TimeCal_type.h" #include //added by Kazuhiro Yamamoto on 2024Sep06 #define SEARCH_RANGE_IN_DAY 5 int fexist( const char *fpath ) { FILE *fp ; if ( (fp = fopen(fpath,"r") ) == NULL ) { return(0) ; } ; fclose(fp) ; return(1) ; } int main( int argc, char *argv[] ) { int ret, ret1, ret2 ; int i ; char s_est_utc[20] ; char tti[30] ; char *cdmy ; uint64_t llint ; uint32_t lint ; uint32_t ti, ext ; double elpsd_sec ; char s_utc[21] ; char fpath_in[256], fpath_out[256] ; FILE *fp_in, *fp_out ; char line[256] ; char s_ti[10] ; //, s_est_utc[30] ; char s_tihex[12] ; //char tcal_f[100]= "/home/ergsc-cdf/data_org/erg/sys/TIMECAL/time_cal_sa49" ; //char leaps_f[100]="/home/ergsc-cdf/data_org/erg/sys/TIMECAL/leapsec.dat" ; //char tcal_f[100]= "/home/ergsc/ergsc-cdf/work/data_org/erg/sys/TIMECAL/time_cal_sa49" ; //chaged by Kazuhiro Yamamoto on 2024Jun29 //char leaps_f[100]="/home/ergsc/ergsc-cdf/work/data_org/erg/sys/TIMECAL/leapsec.dat" ; //chaged by Kazuhiro Yamamoto on 2024Jun29 char tcal_f[100]= "/home/ergsc/ergsc-cdf/work_local/data_org/erg/sys/TIMECAL/time_cal_sa49" ; //chaged by Kazuhiro Yamamoto on 2024Sep05 char leaps_f[100]="/home/ergsc/ergsc-cdf/work_local/data_org/erg/sys/TIMECAL/leapsec.dat" ; //chaged by Kazuhiro Yamamoto on 2024Sep05 //Check the arguments if ( argc != 3 ) { printf("Usage: ti2utc_from_file ti_list.txt estimated_utc\n") ; printf(" ti_list.txt: path of a file containing a list of TIs in hex to be converted\n") ; printf(" estimated_utc: estimated UTC string (YYYYMMDDhhmmss)\n") ; printf("Example:\n prompt> ti2utc_from_file ti_list.txt 20161220000000\n") ; return(1) ; } ; strcpy( fpath_in, argv[1] ) ; strcpy( s_est_utc, argv[2] ) ; if ( fexist( fpath_in ) == 0 ) { fprintf(stderr, "Cannot open the TI list: %s\n", fpath_in ) ; return(1) ; } ; // Open files if ( ( fp_in = fopen( fpath_in, "r" ) ) == NULL ) { fprintf(stderr, "Cannot open the TI list file: %s\n", fpath_in ) ; return(1) ; } ; sprintf( fpath_out, "%s_out", fpath_in ) ; if ( ( fp_out = fopen( fpath_out, "w" ) ) == NULL ) { fprintf(stderr, "Cannot open the resultant UTC file in the write mode: %s\n", fpath_out ) ; return(1) ; } ; // Read the table files for TIMCAL ret = setTimeCalFile(tcal_f) ; ret = setLeapSecFile(leaps_f) ; // Loop for reading TIs and writing UTCs while( feof(fp_in) == 0 ) { if ( NULL == fgets( line, sizeof(line), fp_in ) ) continue ; if ( 1 != sscanf( line, "%s", s_ti ) ) continue ; sprintf( s_tihex, "0x%s", s_ti ) ; llint = strtoll( s_tihex, &cdmy, 0 ) ; ti = (uint32_t)llint ; // TI --> elapsed second from 2000-01-01T00:00:00Z ret1 = ti2es( s_est_utc, ti, &elpsd_sec, &ext, SEARCH_RANGE_IN_DAY ) ; if ( ret1 < 0 ) ret1 = ti2es( s_est_utc, ti, &elpsd_sec, &ext ) ; //Retry unless ended nomally // elapsed second --> UTC in string if ( ret1 >= 0 ) { ret2 = es2utc( 1, elpsd_sec, s_utc ) ; } else { strcpy( s_utc, "00000000000000000000" ) ; // Padding with 0s if ti2es() failed } ; fprintf( fp_out, "%s %d\n", s_utc, ret1 ) ; } ; // Close files and terminate fclose(fp_in) ; fclose(fp_out) ; return(0) ; }