/*
 * *********************************************************
 * *                                                       * 
 * *              Configuration Library                    * 
 * *     AGCAL_LOAD - Load AGCAL Information From file     * 
 * *                                                       * 
 * *********************************************************
 * File: libs/config/agcal_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 "setup.h"
#include "user_lib.h"
#include "config_lib.h"

/*
 * =================================================================== 
 * This subroutine reads the old values from the agcal file. 
 */
MESSAGE agc_load
  (const char *sRemoteHostname_a, 
   volatile struct dsp_agc_cal *pcal_a )
{
  UINT2 lremote_host; 
  FILE* stream ; int ii, ij ;
  char spath[PATHNAME_SIZE], *sptr, sline[81] ;

  /* --------------------------------------------------------------------- 
   *Initialize to all zeros in case of early error:
   */     
  memset( (void *)pcal_a, 0, sizeof( struct dsp_agc_cal) ) ;
      
  /* 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)
    {
      sprintf(spath, "${IRIS_CONFIG}AGC1.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, DIRECTORY_SIZE-1 ) ;
      spath[DIRECTORY_SIZE-1]=0;
      strcat( spath, "AGC1.DAT" );
      
      stream = fopen( spath, "r" ) ;
    }
  if (NULL == stream) return errno_to_message_va( errno, 1, spath ) ;

  /* Read in the analog gain feedback table, and the two original numbers that
     were used to create it.
   */
  for( ii=0 ; ii<256 ; ii+=8 ) {
    sptr = fgets( sline, sizeof(sline), stream ) ;
    if (NULL == sptr) return errno_to_message_va( errno, 1, spath ) ;

    for( ij=0 ; ij<8 ; ij++ ) {
      pcal_a->iagctable[ii+ij] = atoi( &sline[7*(1+ij)] ) ;
    }
  }

  sptr = fgets( sline, sizeof(sline), stream ) ;
  if (NULL == sptr) return errno_to_message_va( errno, 1, spath ) ;
  pcal_a->fsaturation = atof( &sline[0] ) ;
  pcal_a->fmargin     = atof( &sline[8] ) ;

  /* Read in the digital gain definitions
   */
  sptr = fgets( sline, sizeof(sline), stream ) ;
  if (NULL == sptr) return errno_to_message_va( errno, 1, spath ) ;
  pcal_a->fiagc_low  = atof( &sline[ 0] ) ;
  pcal_a->fiagc_goal = atof( &sline[ 8] ) ;
  pcal_a->fiagc_hi   = atof( &sline[16] ) ;

  if (lremote_host)
     sigrclose( stream ) ;
  else
     fclose( stream ) ;
  return SS_NORMAL ;
}
/*
 * =================================================================== 
 * This subroutine writes new values to the agcal file. 
 */
MESSAGE agc_save
  (const char *sRemoteHostname_a, 
   const struct dsp_agc_cal *pcal_a )
{
  UINT2 lremote_host; 
  FILE* stream ; int ii, ij, err ;
  char spath[PATHNAME_SIZE], *sptr ;

  /* Translate environment variable, make the file name, and open it
   * for writing.
   */
 
  if (sRemoteHostname_a == NULL)
     lremote_host = FALSE;
  else if (sRemoteHostname_a[0] == '\0')
     lremote_host = FALSE;
  else
     lremote_host = TRUE;
 
  if (lremote_host)
  {
     sprintf(spath, "${IRIS_CONFIG}AGC1.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, DIRECTORY_SIZE-1 ) ;
     spath[DIRECTORY_SIZE-1]=0;
     strcat( spath, "AGC1.DAT" );
     stream = fopen( spath, "w" ) ;
  }
  if (NULL == stream) return errno_to_message_va( errno, 1, spath ) ;

  /* Write out the analog gain feedback table, and the two original numbers that
     were used to create it.
   */
  for( ii=0 ; ii<256 ; ii+=8 ) {
    err = fprintf( stream, "%7d", ii ) ;
    if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;

    for( ij=0 ; ij<8 ; ij++ ) {
      err = fprintf( stream, "%7d", pcal_a->iagctable[ii+ij] ) ;
      if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;
    }
    err = fprintf( stream, "\n" ) ;
    if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;
  }

  err = fprintf( stream, "%8.3f%8.3f\n", pcal_a->fsaturation, pcal_a->fmargin ) ;
  if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;

  /* Write out the digital gain definitions
   */
  err = fprintf( stream, "%8.3f%8.3f%8.3f\n", pcal_a->fiagc_low, pcal_a->fiagc_goal,
		 pcal_a->fiagc_hi ) ;
  if( err < 0 ) return errno_to_message_va( errno, 1, spath ) ;
  
  if (lremote_host)
     sigrclose( stream ) ;
  else
     fclose( stream ) ;

  return SS_NORMAL ;
        	
}

