/* ****************************
 * *                          *
 * *      IRIS  System        *
 * * Product Printout Utility *
 * *                          *
 * ****************************
 * File: iris/examiners/product_vvp.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 <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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


/* ==============================================================
 * Print VVP results.  Only display the parameters that were actually recorded.
 */
void product_vvp
(SINT4 iswitch_a,           /* Mask of data recorded */
 SINT4 iIntervals_a,        /* Number of intervals in the data */
 const struct vvp_results *results_a )
{
  const struct vvp_results *pvvp ; SINT4 index ;
  char sline[80], *sresult;
  UINT4 iPrintMask;

  sline[0] = 0;
  if( iswitch_a & (VVP_HVEL|VVP_HDIR)  )    strcat( sline, "HWIND " );
  if( iswitch_a &  VVP_VVEL   )             strcat( sline, "VWIND " );
  if( iswitch_a & (VVP_LINDBZ|VVP_LOGDBZ) ) strcat( sline, "Z " );
  if( iswitch_a & (VVP_DIVERG|VVP_DEFORM) ) strcat( sline, "DIV " );
  if( iswitch_a &  VVP_AXDIL  )             strcat( sline, "DIL " );
  
  
  printf("The file contains the following data: %s \n", sline ) ;
  printf("What shall we display? ") ;
  
  sresult = fgets( sline, 70, stdin );
  if (NULL == sresult)
    iPrintMask = iswitch_a;
  else
    {
      char *sptr; int iLen;
      iPrintMask = 0;
      iLen = strlen( sline );
      STR_UPPER( sline, iLen );
      sptr = strtok( sline, " ,\n\t\v\r\f" );
      if (NULL == sptr) iPrintMask = iswitch_a;
      while(sptr)
	{
	  if ( 0 == strcmp( sptr, "HWIND" ))
	    {
	      iPrintMask |= VVP_HVEL|VVP_HDIR;
	    }
	  else if ( 0 == strcmp( sptr, "VWIND" ))
	    {
	      iPrintMask |= VVP_VVEL;
	    }
	  else if ( 0 == strcmp( sptr, "Z" ))
	    {
	      iPrintMask |= VVP_LINDBZ|VVP_LOGDBZ;
	    }
	  else if ( 0 == strcmp( sptr, "DIV" ))
	    {
	      iPrintMask |= VVP_DIVERG|VVP_DEFORM;
	    }
	  else if ( 0 == strcmp( sptr, "DIL" ))
	    {
	      iPrintMask |= VVP_AXDIL;
	    }
	  else
	    {
	      printf( "Unrecognized data: '%s'\n", sptr );
	    }

	  sptr = strtok( NULL, " ,\n\t\v\r\f" );
	}     
    }
  
  
  printf( "     Height  Count" ) ;
  /* Generally print the column if either data or standard deviation is wanted */
  /* dBZ is the only case where these should be separately turned on. */
  if( iPrintMask & (VVP_LINDBZ|VVP_LOGDBZ)    ) printf( "  ZCnt" );
  if( iPrintMask & (VVP_HVEL  |VVP_HVEL_STD)  ) printf( "  Wind-Speed"   ) ;
  if( iPrintMask & (VVP_HDIR  |VVP_HDIR_STD)  ) printf( "  Direction "   ) ;
  if( iPrintMask & (VVP_VVEL  |VVP_VVEL_STD)  ) printf( "    Vertical  " ) ;
  if( iPrintMask & (VVP_LINDBZ|VVP_LINDBZ_STD)) printf( "  Linear-dBZ" ) ;
  if( iPrintMask & (VVP_LOGDBZ|VVP_LOGDBZ_STD)) printf( "   Log-dBZ  " ) ;
  if( iPrintMask & (VVP_DIVERG|VVP_DIVERG_STD)) printf( "   Divergence " ) ;
  if( iPrintMask & (VVP_DEFORM|VVP_DEFORM_STD)) printf( "  Deformation " ) ;
  if( iPrintMask & (VVP_AXDIL |VVP_AXDIL_STD) ) printf( " Axis-of-Dil"   ) ;
  printf( "\n" ) ;

  for( index=0 ; index < iIntervals_a ; index++ ) {
    pvvp = &results_a[index] ;

    printf( "%3d: %6.3f %6d", index+1,
	   ((double)pvvp->iheight) / 100000.0, pvvp->icount ) ;
    if( iPrintMask & (VVP_LINDBZ|VVP_LOGDBZ) ) printf( "%6d", pvvp->iZCount );

    if( iPrintMask & (VVP_HVEL    + VVP_HVEL_STD  ) ) {
      if( pvvp->icount != 0 ) {
	printf( " %5.1f,%5.1f",
	       ((double)pvvp->ihvel    ) / 100.0,
	       ((double)pvvp->ihvel_std) / 100.0 ) ;
      } else {
	printf( "  ----, ----" ) ;
      }
    }    
    if( iPrintMask & (VVP_HDIR    + VVP_HDIR_STD  ) ) {
      if( pvvp->icount != 0 ) {
	printf( " %5.1f,%5.1f",
	       ((double)pvvp->ihdir    ) / 10.0,
	       ((double)pvvp->ihdir_std) / 10.0 ) ;
      } else {
	printf( "  ----, ----" ) ;
      }
    }    
    if( iPrintMask & (VVP_VVEL    + VVP_VVEL_STD  ) ) {
      if( pvvp->icount != 0 ) {
	printf( " %6.2f,%6.2f",
	       ((double)pvvp->ivvel    ) / 100.0,
	       ((double)pvvp->ivvel_std) / 100.0 ) ;
      } else {
	printf( "  -----, -----" ) ;
      }
    }    
    if( iPrintMask & (VVP_LINDBZ|VVP_LINDBZ_STD) ) {
      if( (pvvp->iZCount != 0) && (iswitch_a&VVP_LINDBZ)&&(pvvp->iLindBZ != 0) ) {
	printf( " %5.1f", (double)fdata_to_user( pvvp->iLindBZ    , DB_DBZ2, NULL ) );
      } else {
	printf( "  ----" ) ;
      }
      if( (pvvp->iZCount != 0) && (iswitch_a&VVP_LINDBZ_STD) && (pvvp->iLindBZ != 0) ) {
	printf( ",%5.1f",
		(double)fdata_to_user( pvvp->iLindBZ_std, DB_DBZ2, NULL ) ) ;
      } else {
	printf( ", ----" ) ;
      }
    }
    if( iPrintMask & (VVP_LOGDBZ    + VVP_LOGDBZ_STD  ) ) {
      if( (pvvp->iZCount != 0) && (iswitch_a&VVP_LOGDBZ) && (pvvp->iLogdBZ != 0) ) {
	printf( " %5.1f",
	       (double)fdata_to_user( pvvp->iLogdBZ    , DB_DBZ2, NULL ) );
      } else {
	printf( "  ----" ) ;
      }
      if( (pvvp->iZCount != 0) && (iswitch_a&VVP_LOGDBZ_STD) && (pvvp->iLogdBZ_std != 0)) {
	printf( ",%5.1f",
	       (double)fdata_to_user( pvvp->iLogdBZ_std, DB_DBZ2, NULL ) ) ;
      } else {
	printf( ", ----" ) ;
      }
    }    
    if( iPrintMask & (VVP_DIVERG + VVP_DIVERG_STD) ) {
      if( pvvp->icount != 0 ) {
	printf( " %6.2f,%6.2f",
	       ((double)pvvp->idiverg    ) / 1000.0,
	       ((double)pvvp->idiverg_std) / 1000.0 ) ;
      } else {
	printf( "  -----, -----" ) ;
      }
    }    
    if( iPrintMask & (VVP_DEFORM + VVP_DEFORM_STD) ) {
      if( pvvp->icount != 0 ) {
	printf( " %6.2f,%6.2f",
	       ((double)pvvp->ideform    ) / 1000.0,
	       ((double)pvvp->ideform_std) / 1000.0 ) ;
      } else {
	printf( "  -----, -----" ) ;
      }
    }    
    if( iPrintMask & (VVP_AXDIL  + VVP_AXDIL_STD ) ) {
      if( pvvp->icount != 0 ) {
	printf( " %5.1f,%5.1f",
	       ((double)pvvp->iaxdil    ) / 10.0,
	       ((double)pvvp->iaxdil_std) / 10.0 ) ;
      } else {
	printf( "  ----, ----" ) ;
      }
    }    
    printf( "\n" ) ;
  }
}
