/**---------------------------------------------------------------------------
 ** 
 ** sbe.c -- Reading SBE data files
 ** 
 ** Author          : Pierre Jaccard
 ** Created On      : 1999/07/15 16:17:06
 ** Last Modified By: Pierre Jaccard
 ** Last Modified On: 1999/07/16 20:09:14
 ** Update Count    : 6
 ** Directory       : /home/pego/pcd1/codas3c/gfi/src/libs/ctd/
 ** Version         : 0.0
 ** Status          : Unknown
 ** ---------------------------------------------------------------------- ** 
 ** DESCRIPTION: 
 ** 
 **    This file provides functions to read data from ASCII CTD SBE files.
 ** 
 ** ---------------------------------------------------------------------- ** 
 ** REVISIONS: 
 ** ---------------------------------------------------------------------- ** 
 ** CHANGES: 
 **------------------------------------------------------------------------**/

#define GFI_CTD_HEADER

#include "gfimisc.h"
#include "gfi.h"

/*
 * Find a header line.
 */
char *find_line(
  /*
   * Pointer to file
   */
  FILE *fp,
  /*
   * Pointer to header string
   */
  char *str,
  /*
   * Pointer to header line
   */
  char *line
  )
  /*
   * NULL pointer if header line is not found, or pointer to start of data in
   * header line, which is filled up.
   */
{

  char *c;

  /*
   * Initializations
   */
  c = (char *) NULL;
  line[0] = '\0';
  
  fseek(fp, 0L, 0L);
  while( (c = strstr(line, str)) == NULL){
    if(error_found(fgets(line, 85, fp) == NULL,
                   "Cannot find time"))
      return(c);
  }

  c += strlen(str);

  return(c);
}
     
/* ------------------------------------------------------------------
	 Read the header file and fills appropriate variables

   RETURNS: non-zero on error
	 ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int get_gfi_header(/* Pointer xto ASCII header file */
                   FILE *fpasc, 
                   /* Place for configuration data */
                   CONFIGURATION_2_TYPE *conf, 
									 /* Place for ancillary data */
                   ANCILLARY_1_TYPE *anc, 
                   /* Place for time data */
                   YMDHMS_TIME_TYPE *time, 
									 /* Place for position data */
                   DMSH_POSITION_TYPE position[], 
                   /* Place for profile comments */
                   char profile_comment[])
#else
     int get_gfi_header(fpasc, conf, anc, time, position, profile_comment)
     FILE                 *fpasc;
     CONFIGURATION_2_TYPE *conf;
     ANCILLARY_1_TYPE     *anc;
     YMDHMS_TIME_TYPE     *time;
     DMSH_POSITION_TYPE   position[];
     char                 profile_comment[];
#endif
{
   char   header_line[85], *c, hemi;
   int    status;
   int    ideg;
   float  fmin;
   long   hundredths;

   set_byte((UBYTE *) header_line, (UBYTE) '\0', sizeof(header_line));
   set_byte((UBYTE *) profile_comment, (UBYTE) '\0', 40);

	 /* Get the time */
   if( error_found( (c = find_line(fpasc, "* Time =", header_line)) == NULL,
                    "Cannot find time"))
     return(EOF);
	 status = sscanf(c, "%4hu/%2hu/%2hu %2hu:%2hu:%2hu", 
									 &(time->year), &(time->month), &(time->day),
									 &(time->hour), &(time->minute), &(time->second));
	 if(error_found(status != 6, "Reading time"))
		 return(1);   
	 if(error_found(invalid_time(time), "Invalid time"))
		 return(1);
	 
	 /* Get latitude */
   if( error_found( (c = find_line(fpasc, "* Latitude =", header_line)) == NULL,
                    "Cannot find latitude"))
     return(EOF);
   status = sscanf(c, "%d %f %c", &ideg, &fmin, &hemi);
	 if(error_found(status != 3, "Reading latitude"))
		 return(1);   
   if(error_found((hemi != 'N') && (hemi != 'S'), 
                  "Bad hemisphere for latitude"))
     return(1);
	 hundredths = ((long) (ideg * 360000)) + ((long) (fmin * 6000.0));
	 if(hemi == 'S')
		 hundredths *= -1;
	 /* Latitude corresponds to `y' and must be the second element in
			the position array */
	 HUNPOS(position+1, &hundredths);
	 if(error_found(invalid_position(position+1, 1), "Invalid latitude"))
		 return(1);

	 /* Get longitude */
   if( error_found( (c = find_line(fpasc, "* Longitude =", header_line)) 
                    == NULL,
                    "Cannot find longitude"))
     return(EOF);
   status = sscanf(c, "%d %f %c", &ideg, &fmin, &hemi);
	 if(error_found(status != 3, "Reading longitude"))
		 return(1);   
   if(error_found((hemi != 'W') && (hemi != 'E'), 
                  "Bad hemisphere for longitude"))
     return(1);
	 hundredths = ((long) (ideg * 360000)) + ((long) (fmin * 6000.0));
	 if(hemi == 'W')
		 hundredths *= -1;
	 /* Longitude corresponds to `x' and must be the first element in
			the position array */
	 HUNPOS(position, &hundredths);
	 if(error_found(invalid_position(position, 1), "Invalid longitude"))
		 return(1);
  
	 /* Get station number */
   if( error_found( (c = find_line(fpasc, "* Station =", header_line)) == NULL,
                    "Cannot find station number"))
     return(EOF);
	 status = sscanf(c, "%hu", &(anc->station));
	 if(error_found( status != 1, "Reading station number"))
		 return(1);
	 
	 /* Get depth */
   if( error_found( (c = find_line(fpasc, "* Depth =", header_line)) == NULL,
                    "Cannot find ocean depth"))
     return(EOF);
	 status = sscanf(c, "%f", &(anc->ocean_depth));
	 if(error_found( status != 1, "Reading ocean depth"))
		 return(1);
	 
	 /* Skipping profile comments */
	 
   /* Get ship name */
   if( error_found( (c = find_line(fpasc, "* Ship =", header_line)) == NULL,
                    "Cannot find ship name"))
     return(EOF);
   c = strtrim_lt(c);
   strncpy(conf->ship_name, c, 12-1);
   conf->ship_name[11] = '\0';

   /* Get cruise id */
   if( error_found( (c = find_line(fpasc, "* Cruise =", header_line)) == NULL,
                    "Cannot find cruise name"))
     return(EOF);
   c = strtrim_lt(c);
   strncpy(conf->cruise_id, c, 12-1);
   conf->cruise_id[11] = '\0';

   /* Get project */
   if( error_found( (c = find_line(fpasc, "* Project =", header_line)) == NULL,
                    "Cannot find project name"))
     return(EOF);
   c = strtrim_lt(c);
   strncpy(conf->project, c, 12-1);
   conf->project[11] = '\0';

   /* Get area */
   if( error_found( (c = find_line(fpasc, "* Area =", header_line)) == NULL,
                    "Cannot find area name"))
     return(EOF);
   c = strtrim_lt(c);
   strncpy(conf->area, c, 12-1);
   conf->area[11] = '\0';

   /* Get Conductivity sensor */
   if( error_found( (c = find_line(fpasc, "* Conductivity SN =", header_line))
                    == NULL,
                    "Cannot find conductivity sensor"))
     return(EOF);
   c = strtrim_lt(c);
   strncpy(conf->conductivity_sn, c, 12-1);
   conf->conductivity_sn[11] = '\0';

   /* Get Temperature sensor */
   if( error_found( (c = find_line(fpasc, "* Temperature SN =", header_line))
                    == NULL,
                    "Cannot find temperature sensor"))
     return(EOF);
   c = strtrim_lt(c);
   strncpy(conf->temperature_sn, c, 12-1);
   conf->temperature_sn[11] = '\0';

   /* Get Pressure sensor */
   if( error_found( (c = find_line(fpasc, "* Pressure SN =", header_line))
                    == NULL,
                    "Cannot find pressure sensor"))
     return(EOF);
   c = strtrim_lt(c);
   strncpy(conf->pressure_sn, c, 12-1);
   conf->pressure_sn[11] = '\0';

	 /* Filling the rest: no comments at present */
	 anc->cast_num = 1;
	 anc->end_time = BADUSHORT;
	 anc->aver = anc->rate = BADFLOAT;
	 strcpy(profile_comment, "NO COMMENT");

	 return(0);

}

	
	
	
	







