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

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



/* ================================================================
 * Print information about an NDOP product file
 */
void product_ndop
( const struct product_hdr *phead_a,
  SINT4 ilinelength_a )
{
  SINT4 ixsize = phead_a->pcf.ixsize ;
  SINT4 iysize = phead_a->pcf.iysize ;
  SINT4 izsize = phead_a->pcf.izsize ;
  UINT2 ldoublespace = (ixsize < (ilinelength_a / 2)) ;
  struct ndop_results *pzresults ;
  SINT4 ii, isurface, ix, iy ;
  FLT4 fx ; char line[80] ;

  /* Print information about the PSI parameters and each INGEST
   * input source.
   */
  for( ii=0 ; ii < WARN_MAX_INPUTS ; ii++ ) {
    const struct ndop_input *pin = &phead_a->pcf.psi.ndop.Inputs[ii] ;

    printf( "Input #%d", ii+1 ) ;

    if( pin->sSiteId[0] ) printf( ",  Site:%3s", nullify( pin->sSiteId, 3 ) ) ;
    else                { printf( ",  -Unused-\n" ) ; continue ; }

    printf( ",  Task:%12s", nullify( pin->sTName, 12 ) ) ;

    if( pin->iflags & NDOP_INFLG_VC ) printf( ",  Data:Vc" ) ;
    else                              printf( ",  Data:V " ) ;

    if( pin->iflags & NDOP_INFLG_INFILE ) printf( " (Present)" ) ;
    else                                  printf( " (Absent) " ) ;

    printf( "\n" ) ;
  }
  printf( "\nHeights (km):" ) ;
  for( ii=0 ; ii < izsize ; ii++ ) {
    printf( " %.1f ", 1E-5 * (FLT4)( phead_a->pcf.psi.ndop.iCappiHeight +
				     phead_a->pcf.izscale * ii ) ) ;
  }
  printf( "\nXY-Res: %.1f km,  Min-Angle: %.1f deg,  Time Window: %.1f min\n",
	  ((FLT4)phead_a->pcf.psi.ndop.iOutputResolution) / 1E5,
	  fDegFromBin4( phead_a->pcf.psi.ndop.iMinCrossingAngle ),
	  ((FLT4)phead_a->pcf.psi.ndop.iTimeWindow ) / 60.0 ) ;

  /* Ask which height we will display.  Round to the nearest
   * available surface.
   */
  printf( "\nWhich height would you like to display? " ) ;
  fgets( line, 80, stdin ) ; sscanf( line, "%f", &fx ) ;

  isurface = NINT( ((fx * 1E5) - (FLT4)phead_a->pcf.psi.ndop.iCappiHeight) /
		   (FLT4)phead_a->pcf.izscale ) ;
  isurface = MIN( (izsize - 1), MAX( 0, isurface ) ) ;
  printf( "Displaying data from %.1f km...\n",
	  1E-5 * (FLT4)( phead_a->pcf.psi.ndop.iCappiHeight +
			 phead_a->pcf.izscale * isurface ) ) ;

  pzresults = &((struct ndop_results *)(void*)(phead_a+1))
    [ isurface * ixsize * iysize ] ;

  /* Print character grid showing a crude display of the active
   * areas of the product.
   */
  printf( "\n" ) ;
  printf( "XY-Area-Map  NoScan:'.'  Thresh:'+'  Valid:'X'\n" ) ;
  printf( "---------------------------------------------\n" ) ;

  printf( "   " ) ;
  for( ix=1 ; ix <= ixsize ; ix++ ) {
    if( ix > 10 ) printf( "%d", (ix / 10) % 10 ) ;
    else          printf( " " ) ;
    if( ldoublespace ) printf( " " ) ;
  } ;
  printf( "\n   " ) ;
  for( ix=1 ; ix <= ixsize ; ix++ ) {
    printf( "%d",  ix       % 10 ) ; if( ldoublespace ) printf( " " ) ;
  } ;
  printf( "\n" ) ;

  for( iy = (iysize-1) ; iy >= 0 ; iy-- ) {
    printf( "%2d ", (iy+1) % 100 ) ;
    for( ix=0 ; ix < ixsize ; ix++ ) {
      struct ndop_results *pres = &pzresults[ ix + (iy * ixsize) ] ;

      if     ( pres->iXVel == 0xFFFF ) printf( "." ) ;
      else if( pres->iXVel == 0x0000 ) printf( "+" ) ;
      else                             printf( "X" ) ;

      if( ldoublespace ) printf( " " ) ;
    }
    printf( "\n" ) ;
  }

  /* Check whether more details should be shown
   */
  printf( "\nShow details of valid points? " ) ;
  if( (NULL != fgets( line, 80, stdin )) &&
      ( 'Y' == toupper(line[0]) ) ) {

    SINT4 ichars = 0 ;
    
    printf( "\n" ) ;
    for( iy=0 ; iy < iysize ; iy++ ) {
      for( ix=0 ; ix < ixsize ; ix++ ) {
	struct ndop_results *pres = &pzresults[ ix + (iy * ixsize) ] ;

	if( (pres->iXVel != 0x0000) && (pres->iXVel != 0xFFFF) &&
	    (pres->iYVel != 0x0000) && (pres->iYVel != 0xFFFF) ) {

	  FLT4 fxvel = fdata_to_user( pres->iXVel, DB_VEL2, NULL ) ;
	  FLT4 fyvel = fdata_to_user( pres->iYVel, DB_VEL2, NULL ) ;
	  FLT4  fsqi = ((FLT4)pres->iSqi) / 255.0 ;

	  printf( "(%2d,%2d)%6.1fU%6.1fV%6.2f",
		  ix+1, iy+1, fxvel, fyvel, fsqi ) ; ichars += 27 ;

	  if( (ichars + (5+27)) > ilinelength_a ) {
	    printf( "\n" )    ; ichars  = 0 ;
	  } else {
	    printf( "  |  " ) ; ichars += 5 ;
	  }
	}
      }
    }
    if( ichars > 0 ) printf( "\n" ) ;
  }
}
