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

   LST_TEMP.C

   Within a time range, this program produces a list of profile
   times, transducer temperture, last_temperature and sound speed.
   It is a minimal conversion from LIST_BT.C.

                       Willa Zhu
                       03-02-1990
--------------------------------------------------------------------

control file structure:

     dbname:    (e.g. WEP)
     output: (file name)
     step_size=  (number of profiles to advance)
     year_base=
     time ranges:
     (list of YMDHMS time pairs:)
        (yy/mm/dd hh:mm:ss) to (yy/mm/dd hh:mm:ss)
        .
        .
****************************************************************/

#include "geninc.h"
#include "ioserv.h"
#include "use_db.h"
#include "adcp.h"
#include "data_id.h"
#include "matfile.h"
#define  TRUE  1
#define  FALSE 0

#define  sqr(x)  (x)*(x)
#define  good_float(x)  ( (x) < ADJ_BADFLOAT )

#if PROTOTYPE_ALLOWED
void write_ts_indices(void);
#else
void write_ts_indices();
#endif

/* matlab output of the time-functions */
#define N_T_VAR 6
   static MAT_ARRAY_FP_TYPE *mat_time_ptr;
   static double            time_samples[N_T_VAR];
   static FILE *fp_mat;



#if PROTOTYPE_ALLOWED
void write_ts_indices(void)
#else
void write_ts_indices()
#endif
{
   double id;

   id = (double)1;
   savemat(fp_mat, 'd', "i_day", 1,1,0,(char *)&id, (char *)NULL);
   id = (double)2;
   savemat(fp_mat, 'd', "i_x", 1,1,0,(char *)&id, (char *)NULL);
   id = (double)3;
   savemat(fp_mat, 'd', "i_y", 1,1,0,(char *)&id, (char *)NULL);
   id = (double)4;
   savemat(fp_mat, 'd', "i_tm", 1,1,0,(char *)&id, (char *)NULL);
   id = (double)5;
   savemat(fp_mat, 'd', "i_tl", 1,1,0,(char *)&id, (char *)NULL);
   id = (double)6;
   savemat(fp_mat, 'd', "i_ss", 1,1,0,(char *)&id, (char *)NULL);
}




#if PROTOTYPE_ALLOWED
void do_it(FILE *fp_cnt)
#else
void do_it(fp_cnt)
FILE *fp_cnt;
#endif
{
   FILE *fp_out;

   int      IERR;

   YMDHMS_TIME_TYPE start, end,            /* for time range */
                    this_time;             /*  current profile time */
   ANCILLARY_1_TYPE ancil1;
   ANCILLARY_2_TYPE ancil2;
   DOUBLE_LL_TYPE pos;
   double day;
   int gap_flagged;


   /*---------- end of auto declarations for do_it -----------------*/

/***************************************************************
   static variables needed for initialization of
   the parameter array

*/

static   FILE_NAME_TYPE  dbname, out_filename, mat_filename;
static   int   STEPS;
static   int   year_base;

static   PARAMETER_LIST_ENTRY_TYPE parameters[] =
   {
      { " dbname:",     " %79s", dbname,                 TYPE_STRING },
      { " output:",     " %79s", out_filename,           TYPE_STRING },
      { " step_size=",  " %d",   &STEPS,                 TYPE_INT    },
      { " year_base=",  " %d",   &year_base,             TYPE_INT    },
      { NULL,           NULL,    NULL,                   0           }
   };                   /* parameters[] */


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

   sin(0.0);       /* to force linking of float routines */

   if(get_parameters(fp_cnt, parameters, NULL))  exit(-1);

   check_dbopen(1, dbname, READ_ONLY, DIR_IN_MEMORY);

   fp_out = check_fopen(out_filename,"w");
   new_extension(mat_filename, out_filename, ".mat");
   fp_mat = check_fopen(mat_filename,"wb");
   write_ts_indices();
   mat_time_ptr = start_mat_array(fp_mat, 'd', "ts", 'c', N_T_VAR);
   check_error( (fscanf(fp_cnt, " time_ranges:") != 0),
         "reading time range start");
/*
---> Print the header.
*/
   fprintf(fp_out, "%%");
   print_parameters(fp_out, parameters, NULL, " ");
   fprintf(fp_out,
      "\n%%\n%%       Time         tr_temp    last_temp   snd_spd_used\n");
/*
---> Start the loop through time ranges listed in the control file.
*/
   while (get_time_range(fp_cnt, &start, &end) == 1)
   {
      check_dbsrch(TIME_SEARCH, (char *)&start);

      print_time_range(stdout, &start, &end);
      puts("");
      fprintf(fp_out, "\n");
      gap_flagged = 0;
      IERR = 0;

/*
---> Start the loop through profiles within the time range.
*/
      while (!IERR && check_time(&this_time, &start, &end))
      {
         if ((check_dbget(ANCILLARY_1, (char *)&ancil1, sizeof(ancil1),
            "getting ANCILLARY_1") == sizeof(ancil1))
            && (check_dbget(ANCILLARY_2, (char *)&ancil2, sizeof(ancil2),
            "getting ANCILLARY_2") == sizeof(ancil2)))
         {
            day = year_day(&this_time, year_base);
            get_latlon(&pos);
            time_samples[0] = day;
            time_samples[1] = pos.lon;
            time_samples[2] = pos.lat;
            time_samples[3] = ancil1.tr_temp;
            time_samples[4] = ancil2.last_temp;
            time_samples[5] = ancil1.snd_spd_used;
            tonan_d(time_samples, time_samples, N_T_VAR);
            add_to_mat_array(mat_time_ptr, (char *)time_samples);

            fprintf(fp_out, "%12.6f  %7.2f  %7.4g  %8.3f\n",
                     year_day(&this_time, year_base),
                     ancil1.tr_temp, ancil2.last_temp, ancil1.snd_spd_used);
                     /* The g format is used for last_temp
                     because Transect has none; with the g
                     format, the 1e38 is nicely formatted.
                     Normal last_temp numbers are not
                     formatted as nicely as with the f
                     specification, but still adequately. */
            gap_flagged = 0;
         }
         else if (!gap_flagged)
         {
            fprintf(fp_out, "%%\n");
            gap_flagged = 1;
         }
         DBMOVE(&STEPS, &IERR);   /* IERR is checked at the start of the loop */

      }  /* end of loop through profiles within a time range */

   }  /* end of loop through time ranges read from control file */

   fclose(fp_out);
   end_mat_array(mat_time_ptr);
   fclose(fp_mat);
   DBCLOSE(&IERR);
   check_error(IERR, "DBCLOSE");
   printf("\nLST_TEMP completed.\n");
   return;
}

