/* *****************************************************
 * *                                                   *
 * *  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 "bitio.h"
}

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

#include "sigbufr.h"

/* ==================================================================
 * Load BUFR descriptor file.
 */
void ReadBufrTables
( char * tables_dir_a, const struct sig_logging_spec *pLogging_a,
  const char *sec_1_buff_a, size_t sec_1_len_a )
{
  char sLog[200];
  sect_1_t s1;
  int h;
  unsigned long l;

  h = bitio_i_open ((void*)sec_1_buff_a, sec_1_len_a);
  if (h == -1)
    exit(EXIT_FAILURE);

  bitio_i_input (h, &l, 24);                 /* length of section */

  bitio_i_input (h, &l, 8);  s1.mtab = l;    /* master table used */
  bitio_i_input (h, &l, 8);  s1.subcent = l; /* originating sub-center */
  bitio_i_input (h, &l, 8);  s1.gencent = l; /* generating-center */
  bitio_i_input (h, &l, 8);  s1.updsequ = l; /* original BUFR message */
  bitio_i_input (h, &l, 8);  s1.opsec = l;   /* no optional section */
  bitio_i_input (h, &l, 8);  s1.dcat = l;    /* message type */
  bitio_i_input (h, &l, 8);  s1.dcatst = l;  /* message subtype */
  bitio_i_input (h, &l, 8);  s1.vmtab = l;   /* version number of master table used */
  bitio_i_input (h, &l, 8);  s1.vltab = l;   /* version number of local table used */
  bitio_i_input (h, &l, 8);  s1.year = l;    /* year */
  bitio_i_input (h, &l, 8);  s1.mon = l;     /* month */
  bitio_i_input (h, &l, 8);  s1.day = l;     /* day */
  bitio_i_input (h, &l, 8);  s1.hour = l;    /* hour */
  bitio_i_input (h, &l, 8);  s1.min = l;     /* minute */
  bitio_i_close (h);

 
  if (read_tables (tables_dir_a, s1.vmtab, s1.vltab, s1.subcent, s1.gencent))
    {
      sprintf( sLog, "Error reading BUFR tables in %s\n", tables_dir_a );
      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 );
    }
}

