/*
 * *********************************************************
 * *                                                       * 
 * *               Configuration Library                   * 
 * *   STCWAVE_LOAD - Load STCWAVE Information From file   * 
 * *                                                       * 
 * *********************************************************
 * File: libs/config/stcwave_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 stcwave file. 
 */
MESSAGE stcwave_load
  (FLT4 fstcwave_a[3]) 
{
  FILE* stream ;
  char spath[PATHNAME_SIZE], *sptr, sline[81] ;

  /* --------------------------------------------------------------------- 
   *Initialize to all zeros in case of early error:
   */     
  fstcwave_a[0] = 0.0 ;  fstcwave_a[1] = 0.0 ;  fstcwave_a[2] = 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, PATHNAME_SIZE-20 ) ;
  spath[PATHNAME_SIZE-20]=0;
  strcat( spath, "COSTCWAVE1.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 ) ;
  fstcwave_a[0] = atof( &sline[ 0] ) ;
  fstcwave_a[1] = atof( &sline[12] ) ;
  fstcwave_a[2] = atof( &sline[24] ) ;

  fclose( stream ) ;
  return SS_NORMAL ;
        	
}
/*
 * =================================================================== 
 * This subroutine writes new values to the stcwave file. 
 */
MESSAGE stcwave_save
(const FLT4 fstcwave_a[3])
{
  FILE* stream ; int 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, PATHNAME_SIZE-20 ) ;
  spath[PATHNAME_SIZE-20]=0;
  strcat( spath, "COSTCWAVE1.DAT" );

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

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

  fclose( stream ) ;
  return SS_NORMAL ;
        	
}

