/* *****************************************************
 * *                                                   *
 * *                  IRIS  System                     *
 * *  Product Printout Utility, GAGE product details   *
 * *                                                   *
 * *****************************************************
 * File: iris/examiners/product_gage.c
 * 
 *                 COPYRIGHT (c) 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 <math.h>
#include <stdio.h>
#include <stdlib.h>

#include "sigtypes.h"
#include "headers.h"
#include "product.h"
#include "user_lib.h"
#include "misc_lib.h"

#include "examiners.h"

static void product_gage_one
(const struct product_hdr *phead_a,
 const struct gage_results *pResults_a,
 const struct data_convert *pConvert_a,  /* Number of points to display horizontally */
 SINT4 iLineLength_a );

/* ================================================================
 * Print out information about a forecast product file.
 * Entry parameters:
 *     phead_a - Product header
 */
void product_gage
(const struct product_hdr *phead_a,
 SINT4 iLineLength_a)
{
  SINT4 iGage;
  const struct gage_results *pResults;
  const struct gage_psi_struct *pPsi;
  struct data_convert Convert;
  /* -------------------------------------------------------------- */
  pResults = (struct gage_results *)(PRODUCT_HDR_SIZE + (UINT1 *)phead_a );
  pPsi     = &phead_a->pcf.psi.Gage;

  printf( "Gage count: %d, Data span:%d minutes, ",
	  phead_a->end.iresults_count, (pPsi->iSpan+30)/60 );

  if (!(pPsi->iFlag&GAGE_PFLG_NORAIN))
    {
      if (pPsi->iFlag&GAGE_PFLG_DISDRO)
	printf( "Types:Rain&Disdro\n" );
      else
	printf( "Types:Rain\n" );
    }
  else
    {
      if (pPsi->iFlag&GAGE_PFLG_DISDRO)
	printf( "Types:Disdro\n" );
      else
	printf( "Types:None\n" );
    }

  /* Header line: */
  PendFillDataConvert( &Convert, &phead_a->end, CONVERT_FLAGS_DEFAULT );

  for (iGage=0; iGage<phead_a->end.iresults_count; iGage++)
    {
      product_gage_one( phead_a, &pResults[iGage], &Convert, iLineLength_a );
    }
}
/* ================================================================
 * Compose a text description of a gage_results structure.
 */
static void product_gage_one
(const struct product_hdr *phead_a,
 const struct gage_results *pResults_a,
 const struct data_convert *pConvert_a,
 SINT4 iLineLength_a )
{
  FLT4 fLat, fLon;
  /* -------------------------------------------------------------- */

  printf( "Gage:'%s', ", pResults_a->sName );
  fLat = fDegFromBin4(pResults_a->iLabelLat);
  fLon = fDegFromBin4(pResults_a->iLabelLon);
  printf( "Loc:%s, %s, ", srdeg_to_lat(fLat), srdeg_to_lon(fLon) );
  if (!(pResults_a->iFlag&GAGE_FLG_NORAIN))
    {
      if (pResults_a->iFlag&GAGE_FLG_DISDRO)
	printf( "Type:Rain&Disdro" );
      else
	printf( "Type:Rain" );
    }
  else
    {
      if (pResults_a->iFlag&GAGE_FLG_DISDRO)
	printf( "Type:Disdro" );
      else
	printf( "Type:None" );
    }
  printf("\n     " );
  if (!(pResults_a->iFlag&GAGE_FLG_NORAIN))
    {
      printf( "%d,Rate:%s mm/hr., ", 
	      pResults_a->iRainRate, sStringFromData( pResults_a->iRainRate, DB_RAINRATE2, pConvert_a ));
      if ((DV_NOSCAN2==pResults_a->iCorrection)||(DV_THRESH2==pResults_a->iCorrection))
	printf( "Cor: None" );
      else
	{
	  FLT4 fLogValue, fValue;
	  fLogValue = fdata_to_user( pResults_a->iCorrection, DB_DBZ2, pConvert_a );
	  fValue = pow( 10.0, fLogValue/10.0 );
	  printf( "Cor: %5.2f, ", fValue );
	}
    }
  if (pResults_a->iFlag&GAGE_FLG_DISDRO)
    {
      printf( "Z = %05.1f R**%3.2f, ", 
	      ((FLT4)pResults_a->iZrConst)/10.0, ((FLT4)pResults_a->iZrExp)/1000.0 );
    }

  printf( "Qual: %d\n", pResults_a->iDataQuality );


  return;
  
}
