/* *****************************************************
 * *                                                   *
 * *  Support routines for IrisToBufr and BufrToIris   *
 * *                                                   *
 * *****************************************************
 * File: utils/bufr/BufrSubs.C
 *
 *                 COPYRIGHT (c) 2002, 2004, 2005 BY
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */

/* This program is a pipe which takes an IRIS product in the standard input
 * and writes a modified product out the standard output.
 *
 * Note if you are modifying this program, and wish to insert a diagnostic
 * printf, be sure to write it to the error output.
 */

#include <stdio.h>
#define INCLUDE_STDIO_PROTOTYPES 1
#include <stdlib.h>
#include <string.h>

extern "C" {
#include "desc.h"
}

#include "sigtypes.h"
#include "user_lib.h"

#include "sigbufr.h"

/* ==================================================================
 * Load BUFR descriptor file.
 */
void LoadBufrDescriptor
( const struct sig_logging_spec *pLogging_a )
{
  char *sptr;
  char sLog[200];
  char sPath[PATHNAME_SIZE];
  sptr = getenv( "IRIS_CONFIG" );
  if (sptr)
    {
      strncpy( sPath, sptr, PATHNAME_SIZE-20 );
      sPath[PATHNAME_SIZE-20] = 0;
    }
  else
    sPath[0] = 0;
  strcat( sPath, "descr.txt" );
  if (!read_desc(sPath))
    {
      sprintf( sLog, "Error reading %s\n", sPath );
      Lprintf( pLogging_a, sLog );
      exit(EXIT_FAILURE);
    } 
  return;
}


/* ===========================================
 * Print string to log and terminal, if desired.
 */
void Lprintf
( const struct sig_logging_spec *pLogging_a,
  const char *sString_a )
{
  char sTimeBuf[TIMENAME_SIZE];
  struct ymds_time Ymds;
  GetTimeYmds( &Ymds, TIME_FLG_LOCAL );

  if (pLogging_a->lLogToTerm)
    {
      shhmmssmmm_ddmonyyyy( &Ymds, sTimeBuf );
      fprintf( stderr, "%s:", sTimeBuf );
      fputs( sString_a, stderr );
    }
  if (pLogging_a->lLogToFile)
    {
      shhmmssmmm_ddmonyyyy( &Ymds, sTimeBuf );
      fprintf( pLogging_a->iLogFile, "%s:", sTimeBuf );
      fputs( sString_a, pLogging_a->iLogFile );
      fflush( pLogging_a->iLogFile );
    }
}

