/*
 * *********************************************************
 * *                                                       * 
 * *              Configuration Library                    * 
 * *      GAIN_LOAD - Load GAIN Information From file      * 
 * *                                                       * 
 * *********************************************************
 * File: libs/config/gain_load.c
 *
 *                 COPYRIGHT (c) 1988, 1989, 1994, 1999  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 "setup.h"
#include "config_lib.h"

/*
 * =================================================================== 
 * This subroutine reads the old values from the gain file. 
 */
MESSAGE gain_load
  (const char *sRemoteHostname_a, struct dsp_gain_cal *pgain_a)
{
  UINT2 lremote_host; 
  FILE* stream ; int ii ;
  char spath[160], *sptr, sline[81] ;

  /* --------------------------------------------------------------------- 
   *Initialize to all zeros in case of early error:
   */     
  for( ii=0 ; ii<101 ; ii++ ) pgain_a->fanalog_curve[ii] = 0.0 ;
  for( ii=0 ; ii< 64 ; ii++ ) {
    pgain_a->fdigital_nominal_gain[ii] = 0.0 ;
    pgain_a->fdigital_actual_gain[ii]  = 0.0 ;
    pgain_a->idigital_flags[ii]        = 0   ;
  }
      
  /* Translate environment variable and make the file name
   */
  if (sRemoteHostname_a == NULL)
     lremote_host = FALSE;
  else if (sRemoteHostname_a[0] == '\0')
     lremote_host = FALSE;
  else
     lremote_host = TRUE;

  if (lremote_host)
  {
     strcpy(spath, "${IRIS_CONFIG}COGAIN1.DAT");
     stream = sigrfopen(sRemoteHostname_a, spath, "r");
  }
  else
  {
     sptr = getenv( "IRIS_CONFIG" ) ;
     if (NULL == sptr)
       {
         sig_signal_va( USER_NO_ENV, 1, "IRIS_CONFIG");
         return USER_NO_ENV ;
       }

     strncpy( spath, sptr, 140 ) ;
     strcat ( spath, "COGAIN1.DAT" ) ;

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

  /* Read in the analog gain table (101 points) and the digital gain info
     (64 points).
     */
  for( ii=0 ; ii<101 ; ii++ ) {
    sptr = fgets( sline, sizeof(sline), stream ) ;
    if (NULL == sptr) return errno_to_message_va( errno, 1, spath ) ;
    pgain_a->fanalog_curve[ii] = atof( &sline[8] ) ;
  }

  for( ii=0 ; ii<64 ; ii++ ) {
    sptr = fgets( sline, sizeof(sline), stream ) ;
    if (NULL == sptr) return errno_to_message_va( errno, 1, spath ) ;
    pgain_a->idigital_flags[ii]        = atoi( &sline[ 8] ) ;
    pgain_a->fdigital_nominal_gain[ii] = atof( &sline[16] ) ;
    pgain_a->fdigital_actual_gain[ii]  = atof( &sline[24] ) ;
  }

  if (lremote_host)
     sigrclose( stream ) ;
  else
     fclose( stream ) ; 
  return SS_NORMAL ;
        	
}
/*
 * =================================================================== 
 * This subroutine writes new values to the gain file. 
 */
MESSAGE gain_save
  (const char *sRemoteHostname_a, const struct dsp_gain_cal *pgain_a )
{
  UINT2 lremote_host;
  FILE* stream ; int ii, err ;
  char spath[160], *sptr ;

  if (sRemoteHostname_a == NULL)
     lremote_host = FALSE;
  else if (sRemoteHostname_a[0] == '\0')
     lremote_host = FALSE;
  else
     lremote_host = TRUE;
 
  if (lremote_host)
  {
     strcpy(spath, "${IRIS_CONFIG}COGAIN1.DAT");
     stream = sigrfopen(sRemoteHostname_a, spath, "w");
  }
  else
  {         
     sptr = getenv( "IRIS_CONFIG" ) ;
     if (NULL == sptr)
       {
         sig_signal_va( USER_NO_ENV, 1, "IRIS_CONFIG");
         return USER_NO_ENV ;
       }

     strncpy( spath, sptr, 140 ) ;
     strcat ( spath, "COGAIN1.DAT" ) ;

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

  /* Write out the analog gain table (101 points) and the digital gain info
     (64 points).
     */
  for( ii=0 ; ii<101 ; ii++ ) {
    err = fprintf( stream, "%8d%8.3f\n", ii, pgain_a->fanalog_curve[ii] ) ;
    if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;
  }

  for( ii=0 ; ii<64 ; ii++ ) {
    err = fprintf( stream, "%8d%8d%8.3f%8.3f\n", ii,
		  pgain_a->idigital_flags[ii],
		  pgain_a->fdigital_nominal_gain[ii],
		  pgain_a->fdigital_actual_gain[ii] ) ;
    if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;
  }
  if (lremote_host)
     sigrclose( stream ) ;
  else
     fclose( stream ) ;   
  return SS_NORMAL ;
}

