/*
 * *********************************************************
 * *                                                       * 
 * *              Configuration Library                    * 
 * *   TRIGGER_LOAD - Load TRIGGER Information From file   * 
 * *                                                       * 
 * *********************************************************
 * File: libs/config/trigger_load.c
 *
 *            COPYRIGHT (c) 1988, 1989, 1994, 1999, 2002  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. 
 */     

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

/*
 * =================================================================== 
 * This subroutine reads the old values from the trigger file. 
 */
MESSAGE trigger_load
  (volatile FLT4 fstart_a[4][6],
   volatile FLT4 fwidth_a[4][6],
   volatile FLT4 fpol_time_a[4],
   volatile UINT2 *ilevel_a,
   volatile SINT2 *icount_a ) 
{
  FILE* stream ; int ii, iwidth ;
  char spath[PATHNAME_SIZE], *sptr, sline[81] ;

  /* --------------------------------------------------------------------- 
   *Initialize to all zeros in case of early error:
   */     
  *ilevel_a = 0 ;
  *icount_a = 0 ;
  for( iwidth=0 ; iwidth<4 ; iwidth++ ) {
    fpol_time_a[iwidth] = 0.0 ;
    for( ii=0 ; ii<6 ; ii++ ) {
      fstart_a[iwidth][ii] = 0.0 ;
      fwidth_a[iwidth][ii] = 0.0 ;
    }
  }
      
  sptr = getenv( "IRIS_CONFIG" ) ;
  if (NULL == sptr)
    {
      sig_signal_va( USER_NO_ENV, 1, "IRIS_CONFIG");
      return USER_NO_ENV ;
    }

  strncpy( spath, sptr, DIRECTORY_SIZE-1 ) ;
  spath[DIRECTORY_SIZE-1]=0;
  strcat( spath, "TRIGGER1.DAT" );

  stream = fopen( spath, "r" ) ;
  if (NULL == stream) return errno_to_message_va( errno, 1, spath ) ;

  sptr = fgets( sline, sizeof(sline), stream ) ;
  if (NULL == sptr) return errno_to_message_va( errno, 1, spath ) ;
  *icount_a = atoi( &sline[0] ) ;

  sptr = fgets( sline, sizeof(sline), stream ) ;
  if (NULL == sptr) return errno_to_message_va( errno, 1, spath ) ;
  *ilevel_a = atoi( &sline[0] ) ;

  for( iwidth=0 ; iwidth<4 ; iwidth++ ) {
    sptr = fgets( sline, sizeof(sline), stream ) ;
    if (NULL == sptr) return errno_to_message_va( errno, 1, spath ) ;
    for( ii=0 ; ii<6 ; ii++ ) fstart_a[iwidth][ii] = atof( &sline[12*ii] ) ;

    sptr = fgets( sline, sizeof(sline), stream ) ;
    if (NULL == sptr) return errno_to_message_va( errno, 1, spath ) ;
    for( ii=0 ; ii<6 ; ii++ ) fwidth_a[iwidth][ii] = atof( &sline[12*ii] ) ;
  }

  sptr = fgets( sline, sizeof(sline), stream ) ;
  if (NULL == sptr) return errno_to_message_va( errno, 1, spath ) ;
  for( iwidth=0 ; iwidth<4 ; iwidth++ )
    fpol_time_a[iwidth] = atof( &sline[12*iwidth] ) ;

  fclose( stream ) ;
  return SS_NORMAL ;
        	
}
/*
 * =================================================================== 
 * This subroutine writes new values to the trigger file. 
 */
MESSAGE trigger_save
  (const FLT4 fstart_a[4][6],
   const FLT4 fwidth_a[4][6],
   const FLT4 fpol_time_a[4],
   const UINT2 *ilevel_a,
   const SINT2 *icount_a )
{
  FILE* stream ; int iwidth, err ;
  char spath[PATHNAME_SIZE], *sptr ;

  sptr = getenv( "IRIS_CONFIG" ) ;
  if (NULL == sptr)
    {
      sig_signal_va( USER_NO_ENV, 1, "IRIS_CONFIG");
      return USER_NO_ENV ;
    }

  strncpy( spath, sptr, DIRECTORY_SIZE-1 ) ;
  spath[DIRECTORY_SIZE-1] = 0;
  strcat( spath, "TRIGGER1.DAT" );

  stream = fopen( spath, "w" ) ;
  if (NULL == stream) return errno_to_message_va( errno, 1, spath ) ;

  err = fprintf( stream, "%8d\n", (int)*icount_a ) ;
  if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;

  err = fprintf( stream, "%8d\n", (int)*ilevel_a ) ;
  if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;

  for( iwidth=0 ; iwidth<4 ; iwidth++ ) {
    err = fprintf( stream, "%12.5f%12.5f%12.5f%12.5f%12.5f%12.5f\n",
		  fstart_a[iwidth][0],
		  fstart_a[iwidth][1],
		  fstart_a[iwidth][2],
		  fstart_a[iwidth][3],
		  fstart_a[iwidth][4],
		  fstart_a[iwidth][5] ) ;
    if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;
    err = fprintf( stream, "%12.5f%12.5f%12.5f%12.5f%12.5f%12.5f\n",
		  fwidth_a[iwidth][0],
		  fwidth_a[iwidth][1],
		  fwidth_a[iwidth][2],
		  fwidth_a[iwidth][3],
		  fwidth_a[iwidth][4],
		  fwidth_a[iwidth][5] ) ;
    if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;
  }  

  err = fprintf( stream, "%12.5f%12.5f%12.5f%12.5f\n",
		fpol_time_a[0],
		fpol_time_a[1],
		fpol_time_a[2],
		fpol_time_a[3] ) ;
  if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;

  fclose( stream ) ;
  return SS_NORMAL ;
        	
}
