/* ****************************
 * *                          *
 * *      IRIS  System        *
 * * Product Printout Utility *
 * *                          *
 * ****************************
 * File: iris/examiners/product_fcast.c
 * 
 *   COPYRIGHT (c) 1991, 1992, 1994, 1996, 1997, 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 <math.h>
#include <stdio.h>
#include <stdlib.h>

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


static void product_fcast_one
(SINT4 iY_a,
 const struct product_hdr *phead_a,
 const struct ndop_results *pResults_a,
 SINT4 iCount_a );  /* Number of points to display horizontally */

/* ================================================================
 * Print out information about a forecast product file.
 * Entry parameters:
 *     phead_a - Product header
 */
void product_fcast
(const struct product_hdr *phead_a,
 SINT4 iLineLength_a)
{
  SINT4 iXSize, iYSize, iY, iWidth, ii;
  const struct ndop_results *pResults;
  /* -------------------------------------------------------------- */
  pResults = (struct ndop_results *)(PRODUCT_HDR_SIZE + (UINT1 *)phead_a );

  iXSize = phead_a->pcf.ixsize;
  iYSize = phead_a->pcf.iysize;

  /* Width is what you can fit on the screen */
  iWidth = (iLineLength_a-3)/14;
  if (iWidth > iXSize) iWidth=iXSize;

  /* Header line: */
  printf( "   " );

  for (ii=0; ii<iWidth; ii++)
    printf( "------%1d------ ", ii );
  printf( "\n" );

  for (iY=iYSize-1; iY>=0; iY-- )
    {
      product_fcast_one( iY, phead_a, &pResults[iY*iXSize], iWidth );
    }
}
/* ================================================================
 * Compose a text description of a forecast point.
 * History:
 *     jmh   26 Dec 1990   Init
 * Entry parameters:
 *     ipoint_a - Point number
 */
static void product_fcast_one
(SINT4 iY_a,
 const struct product_hdr *phead_a,
 const struct ndop_results *pResults_a,
 SINT4 iCount_a )  /* Number of points to display horizontally */
{
  SINT4 iOffset;
  /* -------------------------------------------------------------- */

  printf( "%2d:", iY_a );
  for (iOffset=0; iOffset<iCount_a; iOffset++)
    {
      if (pResults_a[iOffset].iXVel)
	{
	  FLT4 fSpeedEast, fSpeedNorth;
	  fSpeedEast  = fdata_to_user( pResults_a[iOffset].iXVel, DB_VEL2, NULL );
	  fSpeedNorth = fdata_to_user( pResults_a[iOffset].iYVel, DB_VEL2, NULL );
      	  printf( "%5.1fE,%5.1fN ", fSpeedEast, fSpeedNorth );
	}
      else
	{
	  printf( " --No motion- " ) ;
	}
    }
  printf( "\n   " );
  for (iOffset=0; iOffset<iCount_a; iOffset++)
    {
      if (pResults_a[iOffset].iXVel)
	{
	  FLT4 fSpeedEast, fSpeedNorth, fSpeed, fAngle;
	  fSpeedEast  = fdata_to_user( pResults_a[iOffset].iXVel, DB_VEL2, NULL );
	  fSpeedNorth = fdata_to_user( pResults_a[iOffset].iYVel, DB_VEL2, NULL );
      
	  fSpeed = sqrt( fSpeedEast*fSpeedEast + fSpeedNorth*fSpeedNorth );
	  if ((fSpeedEast != 0.0)&&(fSpeedNorth != 0.0) )
	    fAngle = atan2( fSpeedEast, fSpeedNorth );
	  else
	    fAngle = 0.0;
	  if (fAngle<0.0) fAngle+= 2.0*FPI_P;
	  
	  printf( "%3.0fm/s@%3ddeg ", fSpeed, NINT( (180.0/FPI_P) * fAngle) );
	}
      else
	{
	  printf( "              " ) ;
	}
    }
  printf( "\n   " );
  for (iOffset=0; iOffset<iCount_a; iOffset++)
    {
      if (pResults_a[iOffset].iXVel)
	{
	  FLT4 fChange;
	  fChange = fdata_to_user( pResults_a[iOffset].iChangeRate, DB_DBZ2, NULL );
      	  printf( "%5.2fdB/min   ", fChange );
	}
      else
	{
	  printf( "              " ) ;
	}
    }
  printf( "\n\n" ) ;
  return;
  
}
