/* Here are some functions for filling CODAS ADCP structures from the
   NBP structures.  Elements that are not filled in these functions must
   be initialized to BADxxx by an appropriate initialization function.

   At this point I am not making any provision for averaging processed
   ensembles; we are just making a straight transfer.  Presumably averaging
   could be done after this, with the CODAS structures as input.
*/

#include <common.h>    /* PROTOTYPE_ALLOWED, etc. */
#include <adcp.h>      /* ANCILLARY_1_TYPE, etc. */
#include "tran_nbp.h"  /* NBP_ENSEMBLE_TYPE, etc. */

#if PROTOTYPE_ALLOWED
void fill_ancil1(ANCILLARY_1_TYPE *a, NBP_ENSEMBLE_TYPE *ens)
#else
void fill_ancil1(a, ens)
   ANCILLARY_1_TYPE *a;
   NBP_ENSEMBLE_TYPE *ens;
#endif
{
   long n_raw;
   NBP_LEADER_TYPE *ld;

   ld = &(ens->leader);

   a->tr_temp          = ld->avg_temperature * 0.01;
   /* It is not clear from the documentation whether this test
      is necessary; we could probably find out with a little experimentation.
   */
   if (ens->conf.use_default_sound_speed)
      a->snd_spd_used   = 1536.0;
   else
      a->snd_spd_used   = ens->conf.sound_speed * 0.01;
   a->mn_heading        = ld->avg_heading * 0.01;
   a->pgs_sample        = ld->n_raw_ens * ens->conf.pings_per_ens;
}

/* I don't see much in ancillary_2 that can be filled from the NBP
   data types.
*/
#if PROTOTYPE_ALLOWED
void fill_ancil2(ANCILLARY_2_TYPE *a, NBP_ENSEMBLE_TYPE *ens)
#else
void fill_ancil2(a, ens)
   ANCILLARY_2_TYPE *a;
   NBP_ENSEMBLE_TYPE *ens;
#endif
{
   a->mn_pitch =      ens->leader.avg_pitch * 0.01;
   a->mn_roll  =      ens->leader.avg_roll  * 0.01;
   a->last_good_bin = ens->conf.n_bins;
}

/* Configuration should need to be filled only once per input file,
   so a single function might as well handle all fields, whether there
   is information available or not.
*/
#if PROTOTYPE_ALLOWED
void fill_config1(CONFIGURATION_1_TYPE *c, NBP_CONFIGURATION_TYPE *nbp_conf)
#else
void fill_config1(c, nbp_conf)
   CONFIGURATION_1_TYPE *c;
   NBP_CONFIGURATION_TYPE *nbp_conf;
#endif
{
   char *fhstr;
   int fhval;

   if (nbp_conf->avg_method == 0)
      c->avg_interval  = (float)(nbp_conf->avg_interval / 100);
   else
      c->avg_interval  = BADFLOAT;

   c->compensation = 1;      /* always bit 0, for heading */
   if (nbp_conf->no_PR_compensation == 0)
      c->compensation |= 6;  /* turn on bits 1 and 2 */
   c->num_bins     = nbp_conf->n_bins;
   c->tr_depth     = nbp_conf->adcp_depth * 0.01;
   /* I suspect the following 3 may need multipliers; units may not
      actually be m as specified in the Transect documentation */
   c->bin_length   = nbp_conf->bin_length;
   c->pls_length   = nbp_conf->pulse_length;
   c->blank_length = nbp_conf->blank;
   c->ping_interval= nbp_conf->ping_interval * 0.01;
   /* c->bot_track  is available only if there is an FH command in
      the direct commands list.  For consistency with loadping, we
      set bot_track = 0 if there is no FH command or if it is FH00255,
      else set it to the no. of profiling pings per BT ping */
   if (nbp_conf->direct_cmd_size == 0 || 
      (fhstr = strstr(nbp_conf->direct_cmd, "FH")) == NULL ||
      (fhval = atoi(fhstr+2)) == 255) 
      c->bot_track  = 0;
   else
      c->bot_track = fhval;
   c->pgs_ensemble  = nbp_conf->pings_per_ens;
   c->ens_threshold = BADSHORT; /* (%     ) threshold: %good in ens.   */
   c->ev_threshold  = BADSHORT; /* ( mm/s ) Error velocity threshold   */
   c->hd_offset     = nbp_conf->transducer_misalignment * 0.001;
   c->pit_offset    = BADFLOAT;
   c->rol_offset    = BADFLOAT;
   c->unused1       = BADFLOAT;
   c->unused2       = BADFLOAT;
   c->unused3       = BADFLOAT;
   c->freq_transmit = nbp_conf->frequency * 1000;
   c->top_ref_bin   = BADSHORT;
   c->bot_ref_bin   = BADSHORT;
   c->unused4       = BADFLOAT;
   /* I am not sure about the signs on the following.  Fortunately,
      this field does not get used for anything. */
   c->heading_bias  = (nbp_conf->compass_offset + nbp_conf->magnetic_variation) * 0.001;
}

/* Our present bt structure is minimal; perhaps some time we should expand it,
   by adding additional fields.  No rush, though.

   Mon  99/06/07 EF
   Based on data from the Kaiyo and a report by John Gunn,
   it appears that contrary to the manual, BT ranges are
   recorded in meters, not cm.  This makes sense, because
   the raw files record it in meters, and the dynamic range
   of the short ints in cm is inadequate, so it would be
   silly to use cm--and maybe in this instance it is not
   Transect itself that is silly, but only its
   documentation.  In any case, I am now changing the depth
   multiplier below from 0.0025 to 0.25.
*/
#if PROTOTYPE_ALLOWED
void fill_bt(BOTTOM_TRACK_TYPE *bt, NBP_LEADER_TYPE *ld)
#else
void fill_bt(bt, ld)
   BOTTOM_TRACK_TYPE *bt;
   NBP_LEADER_TYPE *ld;
#endif
{
   if (ld->bt_u == BADSHORT)
   {
      bt->u = bt->v = bt->depth = BADFLOAT;
   }
   else
   {
      bt->depth = 0.25 * (ld->bt_range1 + ld->bt_range2
                           + ld->bt_range3 + ld->bt_range4);
      bt->u = ld->bt_u * 0.001;
      bt->v = ld->bt_v * 0.001;
   }
}

