/* ****************************************************
 * *                                                  *
 * *                IRIS  System                      *
 * * Example program showing how to change a product  *
 * *                                                  *
 * ****************************************************
 * File: utils/examples/change_product.c
 * 
 *                  COPYRIGHT (c) 1997, 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 <locale.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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


/* Function prototypes used just within this file
 */
static void convert_to_new_file
(const struct product_hdr *pin_head,
 struct product_hdr *pout_head );

/* ====================================================================
 * This is a program to print out product data.
 */
int main( int iargc_a, char *sargv_a[] )
{
  MESSAGE istatus;
  struct product_hdr *pin_head, *pout_head;
  SINT4 in_size, in_channel, iout_size, iout_channel;
  char sin_path[160], sout_path[160] ;

  /* -------------------------------------------------------------------- */
  setlocale( LC_ALL, "" );

  /* Check that file name exists */
  if (iargc_a < 3)
    {
      printf( "Your must specify two file names.\n") ; exit(1);
    }


  strcpy( sin_path, sargv_a[1] );
  strcpy( sout_path, sargv_a[2] );

  istatus = imapopen( sin_path, 0, (void **)&pin_head, &in_size, &in_channel );
  if ( istatus != SS_NORMAL )
    {
      sig_signal( istatus );
      exit(EXIT_FAILURE);
    }

  /* First check that the data type is reflectivity: */
  switch( pin_head->pcf.idata_out )
    {
    case DB_DBT:
    case DB_DBZ:
      istatus = imapcreate( sout_path, in_size, (void **)&pout_head,
			    &iout_size, &iout_channel );
      if ( istatus != SS_NORMAL )
	sig_signal( istatus );
      else
	{
	  convert_to_new_file( pin_head, pout_head );
	  istatus = imapclose( pout_head, iout_size, iout_channel );
	}
      break;

    default:
      printf("Sorry, change_product will not work on %d data.\n", pin_head->pcf.idata_out);
      break;
    }

  istatus = imapclose( pin_head, in_size, in_channel );

  exit(EXIT_SUCCESS);
}

/* ============================================
 * Function to convert data to the new file.
 */
static void convert_to_new_file
(const struct product_hdr *pin_head,
 struct product_hdr *pout_head )
{
  const UINT1 *pin_data;
  UINT1 *pout_data;
  SINT4 isize, ix, iy;
  FLT4 fdist;

  /* First copy accross the headers and data */
  pin_data  = sizeof(struct product_hdr) + (const UINT1 *)pin_head;
  pout_data = sizeof(struct product_hdr) + (UINT1 *)pout_head;

  *pout_head = *pin_head;

  isize = pin_head->pcf.ixsize*pin_head->pcf.iysize;

  memmove( pout_data, pin_data, isize );

  /* Now add a new feature centered at (30,30) pixels from the lower left corner */

  for (ix=0; ix<pin_head->pcf.ixsize; ix++)
    {
      for (iy=0; iy<pin_head->pcf.iysize; iy++)
	{
	  fdist = (FLT4)((ix-100)*(ix-100) + (iy-100)*(iy-100));
	  if (fdist < (20.0*20.0) )
	    {
	      pout_data[ ix + pin_head->pcf.ixsize*iy ] += 80;
	    }
	  
	}

    }
  /* Change the product name */
  memmove( pout_head->pcf.spname, "NEW_PRODUCT ", 12 );
  return;
}




