/* *********************************************************
 * *                                                       * 
 * *                Configuration Library                  * 
 * *       DSP_LOAD - Load DSP Information From file       * 
 * *                                                       * 
 * *********************************************************
 * File: libs/config/dsp_load.c
 *
 * COPYRIGHT (c) 1988, 1989, 1994, 1997, 1999, 2000, 2001, 2002, 2003  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 <string.h>

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

/* =================================================================== 
 * This subroutine writes new values to the dsp file. 
 */
MESSAGE dsp_save_setup
( const char *sRemoteHostname_a, 
  volatile const struct dsp_manual_setup *pDspSetup_a )
{
  struct dsp_manual_setup DspSetup = *pDspSetup_a ;

  strncpy( DspSetup.sVersion, IRIS_VERSION, 8 ) ; DspSetup.sVersion[7] = '\0' ;

  return( lim_saveFile( sRemoteHostname_a, "setup_dsp.conf", "dsp_manual_setup",
			&DspSetup, setup_limits ) ) ;
}

/* =================================================================== 
 * Convert dsp_manual_setup structure from legacy format into current
 * format.  This routine is made public because the legacy structure
 * appears in certain user data files.
 */
void dspManualSetup_from_legacy
( volatile       struct dsp_manual_setup    *current_a,
  volatile const struct dsp_manual_setup722 *legacy_a )
{
  SINT4 ii ;
  memset( (char *)current_a, 0, sizeof(*current_a) ) ;

  current_a->ldsp_available = legacy_a->ldsp_available ;
  current_a->itype = legacy_a->itype ;
  current_a->iwidcnt = legacy_a->iwidcnt ;

  for( ii=0 ; ii < 4 ; ii++ )
    current_a->ipwidths[ii] = legacy_a->ipwidths[ii] ;

  for( ii=0 ; ii < 4 ; ii++ )
    current_a->ipwbits[ii] = legacy_a->ipwbits[ii] ;

  current_a->iwavelength = legacy_a->iwavelength ;
  current_a->isync_fuzz = legacy_a->isync_fuzz ;
  current_a->interface = legacy_a->interface ;
  current_a->lCheckByteOrder = legacy_a->lCheckByteOrder ;
  current_a->idspprf = legacy_a->idspprf ;

  for( ii=0 ; ii < 4 ; ii++ )
    current_a->iminprf[ii] = legacy_a->iminprf[ii] ;

  for( ii=0 ; ii < 4 ; ii++ )
    current_a->imaxprf[ii] = legacy_a->imaxprf[ii] ;

  current_a->inoiseprf = legacy_a->inoiseprf ;
  current_a->ipw_wait = legacy_a->ipw_wait ;
  current_a->ihorizwidth = legacy_a->ihorizwidth ;
  current_a->ivertwidth = legacy_a->ivertwidth ;
  current_a->iantgain = legacy_a->iantgain ;
  current_a->Horiz.iXmtLossSingle = legacy_a->itrloss ;
  current_a->Horiz.iXmtLossHAndV = legacy_a->itrloss ;
  current_a->Horiz.iRcvLoss = legacy_a->irecloss ;
  current_a->Horiz.iTestLoss = legacy_a->itestloss ;
  current_a->idspagcode = legacy_a->idspagcode ;
  current_a->idspdualdelay = legacy_a->idspdualdelay ;
  current_a->lPolarAvailable = legacy_a->lPolarAvailable ;
  current_a->lDualReceiver = legacy_a->lPolSimultaneous ;
  current_a->lXmtSimultaneous = legacy_a->lPolSimultaneous;
  strncpy( (char *)current_a->strid, (char *)legacy_a->strid, 40 ) ;

  current_a->izdr_bias = legacy_a->izdr_bias ;

  for( ii=0 ; ii < 4 ; ii++ )
    {
      current_a->itpower[ii] = legacy_a->itpower[ii] ;
      current_a->iRangeMaskSpacing[ii] = legacy_a->imask_res ;
    }
  current_a->iwindow = legacy_a->iwindow ;
  current_a->linvert_velocities = legacy_a->linvert_velocities ;
  current_a->ldigital_gain = legacy_a->ldigital_gain ;
  current_a->iDataWidths = legacy_a->idata_widths ;
  if (current_a->iDataWidths>DSP_DATA_8AND16) current_a->iDataWidths=DSP_DATA_8AND16;
  current_a->iaqclock = legacy_a->iaqclock ;

  strncpy( (char *)current_a->sdevice, (char *)legacy_a->sdevice, 40 ) ;

  current_a->lPhidpAvailable = legacy_a->lPhidpAvailable ;
  current_a->lRhohvAvailable = legacy_a->lRhohvAvailable ;
  current_a->lSqiAvailable = legacy_a->lSqiAvailable ;

  strncpy( (char *)current_a->sVersion, (char *)legacy_a->sVersion, 8 ) ;

  current_a->iGasAtten = legacy_a->iGasAtten ;
}

/* =================================================================== 
 * Convert dsp_manual_setup structure from current format into legacy
 * format.  This routine is made public because the legacy structure
 * appears in certain user data files.
 */
void dspManualSetup_to_legacy
( volatile       struct dsp_manual_setup722 *legacy_a,
  volatile const struct dsp_manual_setup    *current_a )
{
  SINT4 ii ;
  memset( (char *)legacy_a, 0, sizeof(*legacy_a) ) ;

  legacy_a->ldsp_available = current_a->ldsp_available ;
  legacy_a->itype = current_a->itype ;
  legacy_a->iwidcnt = current_a->iwidcnt ;

  for( ii=0 ; ii < 4 ; ii++ )
    legacy_a->ipwidths[ii] = current_a->ipwidths[ii] ;

  for( ii=0 ; ii < 4 ; ii++ )
    legacy_a->ipwbits[ii] = current_a->ipwbits[ii] ;

  legacy_a->iwavelength = current_a->iwavelength ;
  legacy_a->isync_fuzz = current_a->isync_fuzz ;
  legacy_a->interface = current_a->interface ;
  legacy_a->lCheckByteOrder = current_a->lCheckByteOrder ;
  legacy_a->idspprf = current_a->idspprf ;

  for( ii=0 ; ii < 4 ; ii++ )
    legacy_a->iminprf[ii] = current_a->iminprf[ii] ;

  for( ii=0 ; ii < 4 ; ii++ )
    legacy_a->imaxprf[ii] = current_a->imaxprf[ii] ;

  legacy_a->inoiseprf = current_a->inoiseprf ;
  legacy_a->ipw_wait = current_a->ipw_wait ;
  legacy_a->ihorizwidth = current_a->ihorizwidth ;
  legacy_a->ivertwidth = current_a->ivertwidth ;
  legacy_a->iantgain = current_a->iantgain ;
  legacy_a->itrloss = current_a->Horiz.iXmtLossSingle ;
  legacy_a->irecloss = current_a->Horiz.iRcvLoss ;
  legacy_a->itestloss = current_a->Horiz.iTestLoss ;
  legacy_a->idspagcode = current_a->idspagcode ;
  legacy_a->idspdualdelay = current_a->idspdualdelay ;
  legacy_a->lPolarAvailable = current_a->lPolarAvailable ;
  legacy_a->lPolSimultaneous = current_a->lDualReceiver;

  strncpy( (char *)legacy_a->strid, (char *)current_a->strid, 40 ) ;

  legacy_a->izdr_bias = current_a->izdr_bias ;

  for( ii=0 ; ii < 4 ; ii++ )
    legacy_a->itpower[ii] = current_a->itpower[ii] ;

  legacy_a->imask_res = current_a->iRangeMaskSpacing[0];
  legacy_a->iwindow = current_a->iwindow ;
  legacy_a->linvert_velocities = current_a->linvert_velocities ;
  legacy_a->ldigital_gain = current_a->ldigital_gain ;
  legacy_a->idata_widths = current_a->iDataWidths ;
  legacy_a->iaqclock = current_a->iaqclock ;

  strncpy( (char *)legacy_a->sdevice, (char *)current_a->sdevice, 40 ) ;

  legacy_a->lPhidpAvailable = current_a->lPhidpAvailable ;
  legacy_a->lRhohvAvailable = current_a->lRhohvAvailable ;
  legacy_a->lSqiAvailable = current_a->lSqiAvailable ;

  strncpy( (char *)legacy_a->sVersion, (char *)current_a->sVersion, 8 ) ;

  legacy_a->iGasAtten = current_a->iGasAtten ;
}
