/*
 * *********************************************************
 * *                                                       * 
 * *              Configuration Library                    * 
 * *  ZCAL_LOAD - Load reflectivity calibration from file  * 
 * *                                                       * 
 * *********************************************************
 * File: libs/config/zcal_load.c
 *
 *     COPYRIGHT (c) 1988, 1989, 1994, 1995, 1998, 1999, 2000, 2001  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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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

static UINT2 process_line
(char *sline_a,
 struct dsp_siggen_cal *pcal_a,
 UINT2 *lfirst_a
 );

/* =================================================================== 
 * This subroutine reads the signal generator calibration values from the
 * file (if any).
 */
MESSAGE siggen_cal_load
( struct dsp_siggen_cal *pcal_a ) 
{
  FILE* stream;
  char spath[PATHNAME_SIZE], sline[81], *sptr;
  SINT4 index, iline;
  UINT2 lfirst;

  /* --------------------------------------------------------------------- 
   *Initialize to all zeros:
   */     
  memset( pcal_a, 0, sizeof( struct dsp_siggen_cal) );
  strcpy( pcal_a->siggen_id, "No siggen file" );
  for (index=0;index<128; index++)
    pcal_a->factual[index] = (FLT4)-index;

  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 );
  strcat( spath, "SIGGEN_CAL.DAT" );

  stream = fopen( spath, "r" );
  /* It is OK if the file does not exist */
  if (NULL == stream) return SS_NORMAL;

  strcpy( pcal_a->siggen_id, "No ID in file" );
  /* now loop through the file reading and parsing lines */
  iline = 1;
  lfirst = TRUE;
  sptr = fgets( sline, 81, stream );
  while (NULL != sptr)
    {
      process_line( sptr, pcal_a, &lfirst );
      iline +=1;
      sptr = fgets( sline, 81, stream );
    }

  fclose( stream );
  return SS_NORMAL;
        	
}

/* ===============================================================
 * Process one line from the configuration file.  Returns nonzero if there
 * is a syntax error.
 */
static UINT2 process_line
( char *sline_a,
  struct dsp_siggen_cal *pcal_a,
  UINT2 *lfirst_a
  )
{
  char *stoken1, *stoken2, *sptr;
  SINT4 ivalue, index;
  FLT4 fvalue;

  /* Skip commented lines */
  if (sline_a[0] == '#') return FALSE;

  /* Note that because of the initialization, the token pointer list must be
   * one longer than the number of tokens needed
   */
  if (0 == strncmp( sline_a, "Date: ", 6) )
    {
      /* Read time */
      shmsdmy_to_ymds( &sline_a[6], &(pcal_a->siggen_time) );
    }

  else if (0 == strncmp( sline_a, "ID: ", 4) )
    {
      /* Read ID string, remove trailing CR or NL */
      stoken2 = strtok(&sline_a[4], "\r\n");
      if (NULL == stoken2) 
	pcal_a->siggen_id[0] = 0;
      else
	{
	  strncpy( pcal_a->siggen_id, stoken2, 39 );
	  pcal_a->siggen_id[39] = 0;
	}
    }

  else
    {
      stoken1 = strtok(sline_a, " \t\v\r\n\f");
      /* Blank lines are allowed */
      if (NULL == stoken1) return FALSE;

      ivalue = strtol( stoken1, &sptr, 10 );
      if (sptr == stoken1) return TRUE;
      stoken2 = strtok(NULL, " \t\v\r\n\f");
      fvalue = strtod( stoken2, &sptr );
      if (sptr == stoken2) return TRUE;

      /* First number is assumed to be the maximum */
      if (*lfirst_a)
	{
	  *lfirst_a = FALSE;
	  pcal_a->ical_max_value = ivalue;
	  for (index=0;index<128; index++)
	    pcal_a->factual[index] = (FLT4)(ivalue-index);
	}
      /* Look up value in table, and fill it in */
      index = pcal_a->ical_max_value - ivalue;
      if (index>=0 && index<128)
	pcal_a->factual[index] = fvalue;

    }
  return FALSE;
}

/* =================================================================== 
 * This subroutine reads the signal generator calibration values from the
 * file (if any).
 */
FLT4 fsiggen_cal_lookup
( const struct dsp_siggen_cal *pcal_a,
  SINT4 ivalue_a
  )
{
  SINT4 index;
  FLT4 fresult;

  /* --------------------------------------------------------------------- 
   */     
  index = pcal_a->ical_max_value - ivalue_a;

  if (index>=0 && index<128)
    fresult = pcal_a->factual[index];
  else
    fresult = (FLT4)ivalue_a;

  return fresult;
}
