/***************************************************************

   CONFILE.C       module of ADCPSECT
   Routines for printing out a file in the format required
   as input by the contour program.

*/

#include <stdio.h>
#include <math.h>
#include "dbext.h"
#include "ioserv.h"
#include "vector.h"
#include "use_db.h"
#include "vstat.h"
#include "data_id.h"
#include "pos_to_m.h"
#include "adcp.h"
#include "adcpsect.h"


int contour = FALSE;            /* flag */

extern char out_filename[];
extern int regrid_var;            /* this use of global variables might */
extern int regrid_method;         /*   be better done otherwise */
extern NAME_LIST_ENTRY_TYPE lat_lon_names[];  /* Is this used elsewhere? */
extern UNISTAT_TYPE    adcp_pos_stat;   /* should pass these explicitly? */
extern double  mid_adcp_pos[];  /* avg of min and max */


static int contour_x;                  /* LATITUDE or LONGITUDE */
static double con_units;
static int con_min_npts;               /* minimum number for contour output */
static char  con_filename[NPATHMAX];
static FILE *fp_con;

static int position_source = CONPS_MEAN;        /* how to calculate position */
NAME_LIST_ENTRY_TYPE pos_sources[] =
{
   {"middle"   , CONPS_MIDDLE},
   {"mean"     , CONPS_MEAN},
   {"ctd"      , CONPS_CTD},      /* This option is not implemented. */
   {NULL       , 0}
};
NAME_LIST_TYPE contour_name_lists[] =
{
   lat_lon_names,
   pos_sources
};
PARAMETER_LIST_ENTRY_TYPE contour_params[] =
{
   {""               , " %s"     , &contour_x         , TYPE_TRANS},
   {""               , " %s"     , &position_source   , TYPE_TRANS + 256},
   {" minimum_npts=" , " %d"     , &con_min_npts      , TYPE_INT},
   {" units= "       , " %lf"    , &con_units         , TYPE_DOUBLE},
   {NULL             , NULL      , NULL               , 0}
};

#if PROTOTYPE_ALLOWED
int init_contour(FILE *fp_cnt)
#else
int init_contour(fp_cnt)
FILE *fp_cnt;
#endif
{
   contour = TRUE;
   check_error(get_parameters(fp_cnt, contour_params, contour_name_lists),
                  "contour parameters");

   /* Make a contour output file name, and open the file. */
   new_extension(con_filename, out_filename, ".con");
   fp_con = check_fopen(con_filename, "w");
   return(0);
}

#if PROTOTYPE_ALLOWED
void write_confile(UNISTAT_TYPE *u_stat, UNISTAT_TYPE *v_stat, float *y, int minus)
#else
void write_confile(u_stat, v_stat, y, minus)
UNISTAT_TYPE *u_stat, *v_stat;
float *y;
int minus;
#endif
{           /* We might add options for other position values to be used */
   double x;         /* the x-coordinate for contour output: lat or lon */
   int i;
   double sign = 1.0;

   if (minus == TRUE) sign = -1.0;
   if (position_source == CONPS_MEAN)
      x = adcp_pos_stat.sum[contour_x];
   else
      x = mid_adcp_pos[contour_x];

   for (i=0; i<u_stat->n; i++)
      if (u_stat->npts[i] >= con_min_npts)
         fprintf(fp_con, "%9.4f  %6.1f  %9.3f  %9.3f\n",
                     x, y[i]*sign,
                     u_stat->sum[i]/con_units,
                     v_stat->sum[i]/con_units);
      /* We have assumed that if u is bad, so is v, and vice versa. */
}  /* end of contour printing section */

#if PROTOTYPE_ALLOWED
void close_contour(void)
#else
void close_contour()
#endif
{
   fclose(fp_con);
}
