/* *****************************************************
 * *                                                   *
 * *                  IRIS  System                     *
 * *  Product Printout Utility, CATCH product details  *
 * *                                                   *
 * *****************************************************
 * File: iris/examiners/product_catch.C
 * 
 *           COPYRIGHT (c) 2000, 2001, 2002, 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. 
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

#include "examiners.h"

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

/* ================================================================
 * Print out information about a CATCH product file.
 */
void product_catch
(const struct catch_product *pCatch_a,  /* Pointer to CATCH product */
 SINT4 iLineLength_a)
{
  SINT4 iCatch;
  const struct catch_results *pResults;
  const struct catch_psi_struct *pPsi;
  struct data_convert Convert;
  /* -------------------------------------------------------------- */
  pResults = pCatch_a->Results;
  pPsi     = &pCatch_a->Header.pcf.psi.Catch;

  printf( "Catchment file:'%s', RAIN1 input:'%s', Hours: %d\n",
	  pPsi->sSubCatchment, nullify(pPsi->sProduct, 12 ), pPsi->iHours );
  printf( "SubCatchment count: %d, Threshold offset:%5.2f, factor:%5.2f\n",
	  pCatch_a->Header.end.iresults_count, ((FLT4)pPsi->iThresholdOffset)/1000.0,
	  ((FLT4)pPsi->iThresholdFactor)/1000.0 );

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

  for (iCatch=0; iCatch<pCatch_a->Header.end.iresults_count; iCatch++)
    {
      product_catch_one( &(pCatch_a->Header), &pResults[iCatch], &Convert, iLineLength_a );
    }
}
/* ================================================================
 * Compose a text description of a catch_results structure.
 */
static void product_catch_one
(const struct product_hdr *phead_a,
 const struct catch_results *pResults_a,
 const struct data_convert *pConvert_a,
 SINT4 iLineLength_a )
{
  FLT4 fLat, fLon;
  UINT4 iSkip, iIndex, iCount, iOutSpan;
  /* -------------------------------------------------------------- */

  printf( "\nSubcatchment #%d:'%s', ", pResults_a->iNumber, pResults_a->sName );
  fLat = fDegFromBin4(pResults_a->iLabelLat);
  fLon = fDegFromBin4(pResults_a->iLabelLon);
  printf( "Location: %s, %s\n", srdeg_to_lat(fLat), srdeg_to_lon(fLon) );
  printf( "Area: %5.2f sq km, Pixels: %d, Covered:%d, Rain:%s mm.\n", 
	  ((FLT4)pResults_a->iArea)/100.0, pResults_a->iPixelCount, pResults_a->iCoverCount,
	  sStringFromData( pResults_a->iRain, DB_FLIQUID2, pConvert_a ));
  

  iSkip = (iLineLength_a-7)/8;
  if (iSkip<1) iSkip = 1;

  iOutSpan  = 3600*phead_a->pcf.psi.Catch.iHours;
  iOutSpan += 0xffff&phead_a->pcf.psi.Catch.iSpanSeconds;
  if (phead_a->pcf.psi.Catch.iRain1Span)
    iCount = iOutSpan/phead_a->pcf.psi.Catch.iRain1Span;
  else
    iCount = 1;

  for (iIndex=0; iIndex<iCount; iIndex++)
    {
      if (0 == (iIndex%iSkip))
	{
	  if (0==iIndex)
	    printf("Hourly:");
	  else
	    printf("\n       ");
	}
      else
	printf(",");
      printf("%s", sStringFromData( pResults_a->iRainHourly[iIndex], DB_FLIQUID2, pConvert_a));
    }
  printf( "\n" );

  return;
  
}
/* ====================================================================
 * Convert the data to a string, using "-" if it is area not scanned.
 */
char *sStringFromData
( UINT2 iData_a,
  UINT2 iDataType_a,
  const struct data_convert *pConvert_a )
{
  UINT2 iRes, iNoScan;
  static char sString[20];
  iRes = idata_resolution( iDataType_a );
  if (iRes==16)
    iNoScan = DV_NOSCAN2;
  else
    iNoScan = DV_NOSCAN1;

  if (iData_a == iNoScan)
    {
      strcpy( sString, "  --.--" );
    }
  else
    {
      sprintf( sString, "%7.2f", fdata_to_user( iData_a, iDataType_a, pConvert_a ) );
    }

  return sString;
}
