/* ***********************************************************************
 * *                                                                     *
 * *  Ericsson EWIS format, Convert local structures to/from VAX format  *
 * *                                                                     *
 * ***********************************************************************
 * File: utils/ewis/FileConvert
 *
 *                       COPYRIGHT (c) 2005 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.
 */

/* This program takes the filename of a ewis file and prints out diagnostic
 * information.
 */

#ifndef LINUX
#include <arpa/nameser.h>  /* Defines BYTE_ORDER on HP and SGI */
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sigtypes.h"
#include "ericsson_ewis.h"

#include "user_lib.h"

/* Local function prototypes
 */

static void HeaderPolarFromFile
( struct ewis_header_polar_volume *pHeader_a,
  const struct ewis_header_polar_volume_file *pHeaderFile_a );

static void HeaderXyzFromFile
( struct ewis_header_xyz_volume *pHeader_a,
  const struct ewis_header_xyz_volume_file *pHeaderFile_a );

static void HeaderHorizFromFile
( struct ewis_header_horiz *pHeader_a,
  const struct ewis_header_horiz_file *pHeaderFile_a );

static void FileFromHeaderPolar
( struct ewis_header_polar_volume_file *pHeaderFile_a,
  const struct ewis_header_polar_volume *pHeader_a );

static void FileFromHeaderXyz
( struct ewis_header_xyz_volume_file *pHeaderFile_a,
  const struct ewis_header_xyz_volume *pHeader_a );

static void FileFromHeaderHoriz
( struct ewis_header_horiz_file *pHeaderFile_a,
  const struct ewis_header_horiz *pHeader_a );

/* =====================================================
 * Convert the header
 */
void HeaderFromFile
( struct ewis_header *pHeader_a,  /* Strings are byte swapped correctly */
  const struct ewis_header_file *pHeaderFile_a )
{
  memset( pHeader_a, 0, sizeof(*pHeader_a) );
  memcpy( pHeader_a->ident, pHeaderFile_a->ident, 8 );
  pHeader_a->header_len = GetVaxUint4( pHeaderFile_a->header_len );
  memcpy( pHeader_a->date_time, pHeaderFile_a->date_time, 12 );
  /* sys_time is skipped because we are not sure of the format */
  pHeader_a->scale      = pHeaderFile_a->scale;
  pHeader_a->pic_type   = pHeaderFile_a->pic_type;
  pHeader_a->quant      = pHeaderFile_a->quant;
  pHeader_a->weath      = pHeaderFile_a->weath;

  memcpy( pHeader_a->comment1,  pHeaderFile_a->comment1, 10 );
  memcpy( pHeader_a->comment2,  pHeaderFile_a->comment2, 30 );
  memcpy( pHeader_a->signature, pHeaderFile_a->signature, 2 );
  memcpy( pHeader_a->place,     pHeaderFile_a->place, 20 );

  pHeader_a->radar_type   = pHeaderFile_a->radar_type;
  pHeader_a->compressed   = pHeaderFile_a->compressed;

  pHeader_a->geo_lat      = GetVaxFlt4( pHeaderFile_a->geo_lat );
  pHeader_a->geo_lon      = GetVaxFlt4( pHeaderFile_a->geo_lon );

  pHeader_a->radar_x      = GetVaxFlt4( pHeaderFile_a->radar_x );
  pHeader_a->radar_y      = GetVaxFlt4( pHeaderFile_a->radar_y );

  pHeader_a->ne           = GetVaxUint4( pHeaderFile_a->ne );
  pHeader_a->e_inc        = GetVaxFlt4( pHeaderFile_a->e_inc );
  pHeader_a->nn           = GetVaxUint4( pHeaderFile_a->nn );
  pHeader_a->n_inc        = GetVaxFlt4( pHeaderFile_a->n_inc );
  pHeader_a->nv           = GetVaxUint4( pHeaderFile_a->nv );
  pHeader_a->v_inc        = GetVaxFlt4( pHeaderFile_a->v_inc );

  pHeader_a->pixel_cnt    = GetVaxUint4( pHeaderFile_a->pixel_cnt );
  pHeader_a->store_min    = GetVaxUint4( pHeaderFile_a->store_min );
  pHeader_a->store_max    = GetVaxUint4( pHeaderFile_a->store_max );
  pHeader_a->store_slope  = GetVaxFlt4( pHeaderFile_a->store_slope );
  pHeader_a->store_ord    = GetVaxFlt4( pHeaderFile_a->store_ord );
  pHeader_a->store_offset = GetVaxUint4( pHeaderFile_a->store_offset );
  pHeader_a->store_bits   = GetVaxUint4( pHeaderFile_a->store_bits );
  pHeader_a->store_align  = GetVaxUint4( pHeaderFile_a->store_align );
  memcpy( pHeader_a->store_quant,     pHeaderFile_a->store_quant, 8 );

  pHeader_a->rain_const1  = GetVaxFlt4( pHeaderFile_a->rain_const1 );
  pHeader_a->rain_const2  = GetVaxFlt4( pHeaderFile_a->rain_const2 );
  pHeader_a->geo_height   = GetVaxFlt4( pHeaderFile_a->geo_height );
  pHeader_a->map_proj     = GetVaxUint4( pHeaderFile_a->map_proj );
  pHeader_a->sub_proj     = GetVaxUint4( pHeaderFile_a->sub_proj );

  pHeader_a->map_cen_e    = GetVaxFlt4( pHeaderFile_a->map_cen_e );
  pHeader_a->map_cen_n    = GetVaxFlt4( pHeaderFile_a->map_cen_n );
  for (int ii=0;ii<5;ii++)
    {
      pHeader_a->e_const[ii] = GetVaxFlt4( pHeaderFile_a->e_const[ii] );
      pHeader_a->n_const[ii] = GetVaxFlt4( pHeaderFile_a->n_const[ii] );
    }
  pHeader_a->volume_params  = GetVaxUint2( pHeaderFile_a->volume_params );
  pHeader_a->pic_fil_typ  = pHeaderFile_a->pic_fil_typ;

  switch( pHeader_a->pic_fil_typ )
    {
    case EWIS_PIC_FIL_POLAR_VOLUME:
      HeaderPolarFromFile( &pHeader_a->Type.polar, &pHeaderFile_a->Type.polar );
      break;
    case EWIS_PIC_FIL_XYZ_VOLUME:
      HeaderXyzFromFile( &pHeader_a->Type.xyz, &pHeaderFile_a->Type.xyz );
      break;
    case EWIS_PIC_FIL_HORIZ_PICTURE:
      HeaderHorizFromFile( &pHeader_a->Type.horiz, &pHeaderFile_a->Type.horiz );
      break;
    case EWIS_PIC_FIL_VERT_PICTURE:
      break;
    case EWIS_PIC_FIL_COLOR_IMAGE:
      break;
    case EWIS_PIC_FIL_VERT_OBLIQUE_PICTURE:
      break;
    }

  return;
}
/* =====================================================
 * Convert the polar volume header
 */
static void HeaderPolarFromFile
( struct ewis_header_polar_volume *pHeader_a,
  const struct ewis_header_polar_volume_file *pHeaderFile_a )
{
  int ii;
  pHeader_a->elev_uppb  = GetVaxUint4( pHeaderFile_a->elev_uppb );
  pHeader_a->azim_uppb  = GetVaxUint4( pHeaderFile_a->azim_uppb );
  pHeader_a->range_uppb = GetVaxUint4( pHeaderFile_a->range_uppb );
  for (ii=0; ii<3; ii++)
    {
      pHeader_a->range_lim[ii] = GetVaxUint4( pHeaderFile_a->range_lim[ii] );
      pHeader_a->range_siz[ii] = GetVaxFlt4( pHeaderFile_a->range_siz[ii] );
    }
  pHeader_a->scan_size = GetVaxUint4( pHeaderFile_a->scan_size );
  for (ii=0; ii<EWIS_POLAR_MAX_SWEEPS; ii++)
    {
      pHeader_a->elev[ii] = GetVaxFlt4( pHeaderFile_a->elev[ii] );
      pHeader_a->elev_block_nr[ii] = GetVaxUint4( pHeaderFile_a->elev_block_nr[ii] );
    }
}

/* =====================================================
 * Convert the xyz volume header
 */
static void HeaderXyzFromFile
( struct ewis_header_xyz_volume *pHeader_a,
  const struct ewis_header_xyz_volume_file *pHeaderFile_a )
{
  for (int ii=0; ii<EWIS_XYZ_MAX_SLICES+1; ii++)
    {
      pHeader_a->block_no[ii] = GetVaxUint4( pHeaderFile_a->block_no[ii] );
      pHeader_a->pic_level[ii] = GetVaxFlt4( pHeaderFile_a->pic_level[ii] );
    }
}
/* =====================================================
 * Convert the horiz volume header
 */
static void HeaderHorizFromFile
( struct ewis_header_horiz *pHeader_a,
  const struct ewis_header_horiz_file *pHeaderFile_a )
{
  pHeader_a->hei_nr      = GetVaxUint4( pHeaderFile_a->hei_nr );
  pHeader_a->hei         = GetVaxFlt4( pHeaderFile_a->hei );
  pHeader_a->acc_int     = GetVaxFlt4( pHeaderFile_a->acc_int );
  pHeader_a->acc_lost    = GetVaxFlt4( pHeaderFile_a->acc_lost );
  pHeader_a->fcst_spd    = GetVaxFlt4( pHeaderFile_a->fcst_spd );
  pHeader_a->fcst_cor    = GetVaxFlt4( pHeaderFile_a->fcst_cor );
  pHeader_a->cappi_range = GetVaxFlt4( pHeaderFile_a->cappi_range );
  pHeader_a->threshold   = GetVaxFlt4( pHeaderFile_a->threshold );
  pHeader_a->store_type  = pHeaderFile_a->store_type ;
  for (int ii=0; ii<16; ii++)
    {
      pHeader_a->thresh[ii] = GetVaxFlt4( pHeaderFile_a->thresh[ii] );
    }
}
/* =====================================================
 * Convert the header
 */
void FileFromHeader
( struct ewis_header_file *pHeaderFile_a,
  const struct ewis_header *pHeader_a )
{
  memset( pHeaderFile_a, 0, sizeof(*pHeaderFile_a) );
  memcpy( pHeaderFile_a->ident          , pHeader_a->ident, 8 );
  PutVaxUint4( pHeaderFile_a->header_len, pHeader_a->header_len );
  memcpy( pHeaderFile_a->date_time      , pHeader_a->date_time, 12 );
  /* sys_time is skipped because we are not sure of the format */
  pHeaderFile_a->scale      = pHeader_a->scale;
  pHeaderFile_a->pic_type   = pHeader_a->pic_type;
  pHeaderFile_a->quant      = pHeader_a->quant;
  pHeaderFile_a->weath      = pHeader_a->weath;

  memcpy( pHeaderFile_a->comment1,    pHeader_a->comment1, 10 );
  memcpy( pHeaderFile_a->comment2,    pHeader_a->comment2, 30 ); 
  memcpy( pHeaderFile_a->signature,   pHeader_a->signature, 2 );
  memcpy( pHeaderFile_a->place,       pHeader_a->place, 20 );

  pHeaderFile_a->radar_type         = pHeader_a->radar_type;
  pHeaderFile_a->compressed         = pHeader_a->compressed;

  PutVaxFlt4( pHeaderFile_a->geo_lat,  pHeader_a->geo_lat );
  PutVaxFlt4( pHeaderFile_a->geo_lon,  pHeader_a->geo_lon );
  PutVaxFlt4( pHeaderFile_a->radar_x,  pHeader_a->radar_x );
  PutVaxFlt4( pHeaderFile_a->radar_y,  pHeader_a->radar_y );

  PutVaxUint4( pHeaderFile_a->ne   ,   pHeader_a->ne );
  PutVaxFlt4 ( pHeaderFile_a->e_inc,   pHeader_a->e_inc );
  PutVaxUint4( pHeaderFile_a->nn   ,   pHeader_a->nn );
  PutVaxFlt4 ( pHeaderFile_a->n_inc,   pHeader_a->n_inc );
  PutVaxUint4( pHeaderFile_a->nv   ,   pHeader_a->nv );
  PutVaxFlt4 ( pHeaderFile_a->v_inc,   pHeader_a->v_inc );

  PutVaxUint4( pHeaderFile_a->pixel_cnt    , pHeader_a->pixel_cnt );
  PutVaxUint4( pHeaderFile_a->store_min    , pHeader_a->store_min );
  PutVaxUint4( pHeaderFile_a->store_max    , pHeader_a->store_max );
  PutVaxFlt4 ( pHeaderFile_a->store_slope  , pHeader_a->store_slope );
  PutVaxFlt4 ( pHeaderFile_a->store_ord    , pHeader_a->store_ord );
  PutVaxUint4( pHeaderFile_a->store_offset , pHeader_a->store_offset );
  PutVaxUint4( pHeaderFile_a->store_bits   , pHeader_a->store_bits );
  PutVaxUint4( pHeaderFile_a->store_align  , pHeader_a->store_align );
  memcpy( pHeaderFile_a->store_quant       , pHeader_a->store_quant, 8 );

  PutVaxFlt4 ( pHeaderFile_a->rain_const1  , pHeader_a->rain_const1 );
  PutVaxFlt4 ( pHeaderFile_a->rain_const2  , pHeader_a->rain_const2 );
  PutVaxFlt4 ( pHeaderFile_a->geo_height   , pHeader_a->geo_height );
  PutVaxUint4( pHeaderFile_a->map_proj     , pHeader_a->map_proj );
  PutVaxUint4( pHeaderFile_a->sub_proj     , pHeader_a->sub_proj );

  PutVaxFlt4 ( pHeaderFile_a->map_cen_e    , pHeader_a->map_cen_e );
  PutVaxFlt4 ( pHeaderFile_a->map_cen_n    , pHeader_a->map_cen_n );
  for (int ii=0;ii<5;ii++)
    {
      PutVaxFlt4( pHeaderFile_a->e_const[ii] , pHeader_a->e_const[ii] );
      PutVaxFlt4( pHeaderFile_a->n_const[ii] , pHeader_a->n_const[ii] );
    }
  PutVaxUint2( pHeaderFile_a->volume_params  , pHeader_a->volume_params );
  pHeaderFile_a->pic_fil_typ  = pHeader_a->pic_fil_typ;

  switch( pHeader_a->pic_fil_typ )
    {
    case EWIS_PIC_FIL_POLAR_VOLUME:
      FileFromHeaderPolar( &pHeaderFile_a->Type.polar, &pHeader_a->Type.polar );
      break;
    case EWIS_PIC_FIL_XYZ_VOLUME:
      FileFromHeaderXyz( &pHeaderFile_a->Type.xyz, &pHeader_a->Type.xyz );
      break;
    case EWIS_PIC_FIL_HORIZ_PICTURE:
      FileFromHeaderHoriz( &pHeaderFile_a->Type.horiz, &pHeader_a->Type.horiz );
      break;
    case EWIS_PIC_FIL_VERT_PICTURE:
      break;
    case EWIS_PIC_FIL_COLOR_IMAGE:
      break;
    case EWIS_PIC_FIL_VERT_OBLIQUE_PICTURE:
      break;
    }

  return;
}
/* =====================================================
 * Convert the polar volume header
 */
static void FileFromHeaderPolar
( struct ewis_header_polar_volume_file *pHeaderFile_a,
  const struct ewis_header_polar_volume *pHeader_a )
{
  int ii;
  PutVaxUint4( pHeaderFile_a->elev_uppb  , pHeader_a->elev_uppb );
  PutVaxUint4( pHeaderFile_a->azim_uppb  , pHeader_a->azim_uppb );
  PutVaxUint4( pHeaderFile_a->range_uppb , pHeader_a->range_uppb );
  for (ii=0; ii<3; ii++)
    {
      PutVaxUint4( pHeaderFile_a->range_lim[ii] , pHeader_a->range_lim[ii] );
      PutVaxFlt4 ( pHeaderFile_a->range_siz[ii] , pHeader_a->range_siz[ii] );
    }
  PutVaxUint4( pHeaderFile_a->scan_size , pHeader_a->scan_size );
  for (ii=0; ii<EWIS_POLAR_MAX_SWEEPS; ii++)
    {
      PutVaxFlt4 ( pHeaderFile_a->elev[ii] , pHeader_a->elev[ii] );
      PutVaxUint4( pHeaderFile_a->elev_block_nr[ii] , pHeader_a->elev_block_nr[ii] );
    }
}

/* =====================================================
 * Convert the xyz volume header
 */
static void FileFromHeaderXyz
( struct ewis_header_xyz_volume_file *pHeaderFile_a,
  const struct ewis_header_xyz_volume *pHeader_a )
{
  for (int ii=0; ii<EWIS_XYZ_MAX_SLICES+1; ii++)
    {
      PutVaxUint4( pHeaderFile_a->block_no[ii] , pHeader_a->block_no[ii] );
      PutVaxFlt4 ( pHeaderFile_a->pic_level[ii], pHeader_a->pic_level[ii] );
    }
}
/* =====================================================
 * Convert the horiz volume header
 */
static void FileFromHeaderHoriz
( struct ewis_header_horiz_file *pHeaderFile_a,
  const struct ewis_header_horiz *pHeader_a )
{
  PutVaxUint4( pHeaderFile_a->hei_nr      , pHeader_a->hei_nr );
  PutVaxFlt4 ( pHeaderFile_a->hei         , pHeader_a->hei );
  PutVaxFlt4 ( pHeaderFile_a->acc_int     , pHeader_a->acc_int );
  PutVaxFlt4 ( pHeaderFile_a->acc_lost    , pHeader_a->acc_lost );
  PutVaxFlt4 ( pHeaderFile_a->fcst_spd    , pHeader_a->fcst_spd );
  PutVaxFlt4 ( pHeaderFile_a->fcst_cor    , pHeader_a->fcst_cor );
  PutVaxFlt4 ( pHeaderFile_a->cappi_range , pHeader_a->cappi_range );
  PutVaxFlt4 ( pHeaderFile_a->threshold   , pHeader_a->threshold );
  pHeaderFile_a->store_type               = pHeader_a->store_type ;
  for (int ii=0; ii<16; ii++)
    {
      PutVaxFlt4( pHeaderFile_a->thresh[ii] , pHeader_a->thresh[ii] );
    }
}
