00001 00002 /* 00003 * THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE 00004 * CHECKED IT OUT USING THE COMMAND CHECKOUT. 00005 * 00006 * $Id: make__triglist_8c-source.html 2161 2006-05-19 16:55:03Z paulf $ 00007 * 00008 * Revision history: 00009 * $Log$ 00009 * Revision 1.1 2006/05/19 16:55:02 paulf 00009 * first inclusion 00009 * 00010 * Revision 1.3 2001/07/01 22:11:36 davidk 00011 * Added a comment about another comment. 00012 * 00013 * Revision 1.2 2000/05/02 19:45:32 lucky 00014 * Cosmetic fixes (define extern fns) to make NT compile without warnings. 00015 * 00016 * Revision 1.1 2000/02/14 18:51:48 lucky 00017 * Initial revision 00018 * 00019 * 00020 */ 00021 00022 /************************************************************ 00023 * writetrig.c * 00024 * * 00025 * Functions for maintaining trigger files. * 00026 * (based on logit.c) * 00027 * * 00028 * First, call writetrig_Init. Then, call writetrig. * 00029 * Call writetrig_close before exitting! * 00030 * * 00031 * These functions are NOT MT-Safe, since they store * 00032 * data in static buffers: PNL, 12/8/98 * 00033 ************************************************************/ 00034 #include <stdio.h> 00035 #include <stdlib.h> 00036 #include <string.h> 00037 #include <stdarg.h> 00038 #include <time.h> 00039 #include <time_ew.h> 00040 #include <earthworm.h> 00041 #include <transport.h> 00042 00043 00044 static FILE *fp; 00045 static char date[9]; 00046 static char date_prev[9]; 00047 static time_t now; 00048 static char trigName[100]; 00049 static char trigfilepath[70]; 00050 static char template[25]; 00051 static char *buf; 00052 static struct tm res; 00053 static int Init = 0; /* 1 if writetrig_Init has been called */ 00054 static int Disk = 1; /* 1 if output goes to Disk file */ 00055 void logit( char *, char *, ... ); /* logit.c sys-independent */ 00056 00057 00058 extern void date17 (double, char *); 00059 00060 /* Functions in make_triglist.c */ 00061 int writetrig_init( char*, char* ); 00062 int writetrig( char*, char*, char* ); 00063 void writetrig_close(void); 00064 void bldtrig_head( char*, double , char* , char* ); 00065 void bldtrig_phs( char *trigmsg, char* sta, char* comp, char* net, 00066 char ph, double pickTime, double saveStart, double durSave); 00067 char *make_datestr( double, char * ); 00068 00069 00070 /************************************************************************* 00071 * writetrig_Init * 00072 * * 00073 * Call this function once before the other writetrig routines. * 00074 * * 00075 *************************************************************************/ 00076 int writetrig_init( char* trigFileBase, char* outputDir ) 00077 { 00078 char *str; 00079 char baseName[50]; 00080 int lastchar; 00081 00082 /* Set time zone using the TZ environmental variable. 00083 This is not required under Solaris. 00084 In OS2 v2 or v3.0, use _tzset(). 00085 In OS2 v3.0, use tzset(). 00086 *************************************************/ 00087 #if defined(_OS2) || defined(_WINNT) 00088 if ( getenv( "TZ" ) != NULL ) _tzset(); 00089 #endif 00090 00091 /* Make sure we should write a trigger file 00092 ****************************************/ 00093 if ( strncmp(trigFileBase, "none",4)==0 ) Disk=0; 00094 else if( strncmp(trigFileBase, "NONE",4)==0 ) Disk=0; 00095 else if( strncmp(trigFileBase, "None",4)==0 ) Disk=0; 00096 else if( strncmp(outputDir, "none",4)==0 ) Disk=0; 00097 else if( strncmp(outputDir, "NONE",4)==0 ) Disk=0; 00098 else if( strncmp(outputDir, "None",4)==0 ) Disk=0; 00099 if( Disk==0 ) return( 0 ); 00100 00101 /* Truncate everything beyond and 00102 including "." in the base file name 00103 ***********************************/ 00104 strcpy( baseName, trigFileBase ); 00105 str = strchr( baseName, '.' ); 00106 if ( str != NULL ) *str = '\0'; 00107 00108 /* Check Init flag 00109 ***************/ 00110 if( Init ) return( 0 ); 00111 Init = 1; 00112 00113 /* Get path & base file name from config-file parameters 00114 *****************************************************/ 00115 strcpy ( trigfilepath, outputDir ); 00116 lastchar = strlen(outputDir)-1; 00117 00118 #if defined(_OS2) || defined(_WINNT) 00119 if( outputDir[lastchar] != '\\' && outputDir[lastchar] != '/' ) 00120 strcat( trigfilepath, "\\" ); 00121 #endif 00122 #ifdef _SOLARIS 00123 if( outputDir[lastchar] != '/' ) strcat( trigfilepath, "/" ); 00124 #endif 00125 00126 sprintf( template, "%s.trg_", baseName ); 00127 00128 /* Build trigger file name by appending time 00129 *****************************************/ 00130 time( &now ); 00131 gmtime_ew( &now, &res ); 00132 sprintf( date, "%04d%02d%02d", (res.tm_year+1900), (res.tm_mon+1), 00133 res.tm_mday ); 00134 00135 strcpy( trigName, trigfilepath ); 00136 strcat( trigName, template ); 00137 strcat( trigName, date ); 00138 strcpy( date_prev, date ); 00139 00140 /* Open trigger list file 00141 **********************/ 00142 fp = fopen( trigName, "a" ); 00143 if ( fp == NULL ) 00144 { 00145 logit("e", 00146 "make_triglist: Error opening triglist file <%s>\n", 00147 trigName ); 00148 return( -1 ); 00149 } 00150 00151 /* Print startup message to trigger file 00152 *************************************/ 00153 fprintf( fp, "\n-------------------------------------------------\n" ); 00154 fprintf( fp, "make_triglist: startup at UTC_%s_%02d:%02d:%02d", 00155 date, res.tm_hour, res.tm_min, res.tm_sec ); 00156 fprintf( fp, "\n-------------------------------------------------\n" ); 00157 fflush ( fp ); 00158 00159 /* Log a warning message 00160 *********************/ 00161 #if defined(_OS2) || defined(_WINNT) 00162 if ( getenv( "TZ" ) == NULL ) 00163 { 00164 writetrig("WARNING: The TZ environmental variable is not set.\n", trigFileBase, outputDir ); 00165 writetrig(" Roll-over dates of trigger files may be bogus.\n", trigFileBase, trigFileBase ); 00166 } 00167 #endif 00168 00169 return( 0 ); 00170 } 00171 00172 00173 /***************************************************************** 00174 * writetrig * 00175 * * 00176 * Function to log a message to a Disk file. * 00177 * * 00178 * flag: A string controlling where output is written: * 00179 * If any character is 'e', output is written to stderr. * 00180 * If any character is 'o', output is written to stdout. * 00181 * If any character is 't', output is time stamped. * 00182 * * 00183 * The rest of calling sequence is identical to printf. * 00184 *****************************************************************/ 00185 /* The comment above doesn't seem to have any relevance to writetrig() 00186 DK 06/28/2001 */ 00187 00188 00189 int writetrig( char *note, char* filename, char* outDir ) 00190 { 00191 int rc; 00192 00193 /* Check Init flag 00194 ***************/ 00195 if ( !Init ) 00196 { 00197 rc = writetrig_init(filename, outDir); 00198 if( rc != 0 ) return( rc ); 00199 } 00200 if ( !Disk ) return( 0 ); 00201 00202 /* Get current system time 00203 ***********************/ 00204 time( &now ); 00205 gmtime_ew( &now, &res ); 00206 00207 /* See if the date has changed. 00208 If so, create a new trigger file. 00209 *********************************/ 00210 sprintf( date, "%04d%02d%02d", (res.tm_year+1900), (res.tm_mon+1), 00211 res.tm_mday ); 00212 00213 if ( strcmp( date, date_prev ) != 0 ) 00214 { 00215 fprintf( fp, 00216 "UTC date changed; trigger output continues in file <%s%s>\n", 00217 template, date ); 00218 fclose( fp ); 00219 strcpy( trigName, trigfilepath ); 00220 strcat( trigName, template ); 00221 strcat( trigName, date ); 00222 fp = fopen( trigName, "a" ); 00223 if ( fp == NULL ) 00224 { 00225 fprintf( stderr, "Error opening trigger file <%s%s>!\n", 00226 template, date ); 00227 return( -1 ); 00228 } 00229 fprintf( fp, 00230 "UTC date changed; trigger output continues from file <%s%s>\n", 00231 template, date_prev ); 00232 strcpy( date_prev, date ); 00233 00234 /* Send a warning message to the new log file 00235 ******************************************/ 00236 #if defined(_OS2) || defined(_WINNT) 00237 if ( getenv( "TZ" ) == NULL ) 00238 { 00239 fprintf( fp, "WARNING: The TZ environmental variable is not set.\n" ); 00240 fprintf( fp, " Roll-over dates of trigger files may be bogus.\n" ); 00241 } 00242 #endif 00243 } 00244 00245 /* write the message to the trigger file 00246 ***************************************/ 00247 fprintf( fp, "%s", note ); 00248 fflush( fp ); 00249 00250 return( 0 ); 00251 } 00252 00253 void writetrig_close() 00254 { 00255 fclose( fp ); 00256 return; 00257 } 00258 /***************************************************************************/ 00259 00260 /* Routines for creating a trig_list message */ 00261 00262 /************************************************************** 00263 * bldtrig_head() builds the EVENT line of a trigger message * 00264 * Modified for author id by alex 7/10/98 * 00265 **************************************************************/ 00267 #define DATESTR_LEN 22 00268 #define PHASE_STR 200 00269 void bldtrig_head( char* trigmsg, double otime, char* evId, char* author) 00270 { 00271 char datestr[DATESTR_LEN]; 00272 00273 /* Sample EVENT line for trigger message: 00274 EVENT DETECTED 970729 03:01:13.22 UTC EVENT ID:123456 AUTHOR: asdf:asdf\n 00275 0123456789 123456789 123456789 123456789 123456789 123456789 00276 ************************************************************/ 00277 make_datestr( otime, datestr ); 00278 sprintf( trigmsg, "EVENT DETECTED %s UTC EVENT ID: %s AUTHOR: %s \n\n", 00279 datestr, evId, author); 00280 strcat ( trigmsg, "Sta/Cmp/Net Date Time start save duration in sec.\n" ); 00281 strcat ( trigmsg, "----------- ------ --------------- ------------------------------------------\n"); 00282 00283 return; 00284 } 00285 00286 /**************************************************************** 00287 * bldtrig_phs() builds the "phase" lines of a trigger message * 00288 ****************************************************************/ 00289 void bldtrig_phs( char *trigmsg, char* sta, char* comp, char* net, char ph, double pickTime, 00290 double saveStart, double durSave) 00291 { 00292 char str[PHASE_STR]; 00293 char pckt_str[DATESTR_LEN]; 00294 char savet_str[DATESTR_LEN]; 00295 00296 /* Convert times in seconds since 1600 to character strings 00297 **********************************************************/ 00298 make_datestr( pickTime, pckt_str ); 00299 make_datestr( saveStart, savet_str ); 00300 00301 /* Build the "phase" line! Here's a sample: 00302 MCM VHZ NC N 19970729 03:01:13.34 UTC save: yyyymmdd 03:00:12.34 120\n 00303 0123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 00304 ********************************************************************************/ 00305 /*alex 11/1/97: changed format to be variable lenth <station> <comp> <net> 00306 separated by spaces: */ 00307 sprintf( str, " %s %s %s %c %s UTC save: %s %8ld\n", 00308 sta, comp, net, ph, pckt_str, savet_str, (long)durSave ); 00309 00310 strcat( trigmsg, str ); 00311 00312 return; 00313 } 00314 00315 /********************************************************************* 00316 * make_datestr() takes a time in seconds since 1600 and converts * 00317 * it into a character string in the form of: * 00318 * "19880123 12:34:12.21" * 00319 * It returns a pointer to the new character string * 00320 * * 00321 * NOTE: this requires an output buffer >=21 characters long * 00322 * * 00323 * Y2K compliance: * 00324 * date format changed to YYYYMMDD * 00325 * date15() changed to date17() * 00326 * * 00327 *********************************************************************/ 00328 00329 char *make_datestr( double t, char *datestr ) 00330 { 00331 char str17[18]; /* temporary date string */ 00332 00333 /* Convert time to a pick-format character string */ 00334 date17( t, str17 ); 00335 00336 /* Convert a date character string in the form of: 00337 "19880123123412.21" to one in the form of: 00338 "19880123 12:34:12.21" 00339 0123456789 123456789 00340 Requires a character string at least 21 characters long 00341 */ 00342 strncpy( datestr, str17, 8 ); /*yyyymmdd*/ 00343 datestr[8] = '\0'; 00344 strcat ( datestr, " " ); 00345 strncat( datestr, str17+8, 2 ); /*hr*/ 00346 strcat ( datestr, ":" ); 00347 strncat( datestr, str17+10, 2 ); /*min*/ 00348 strcat ( datestr, ":" ); 00349 strncat( datestr, str17+12, 5 ); /*seconds*/ 00350 00351 00352 return( datestr ); 00353 } 00354