/**---------------------------------------------------------------------------
 ** 
 ** load_ctd.c -- 
 ** 
 ** Author          : Pierre Jaccard
 ** Created On      : 1999/08/04 10:37:45
 ** Last Modified By: Pierre Jaccard
 ** Last Modified On: 1999/08/04 10:45:27
 ** Update Count    : 2
 ** Directory       : /home/pego/pcd1/codas3c/gfi/src/plugs/
 ** Version         : 0.0
 ** Status          : Unknown
 ** ---------------------------------------------------------------------- ** 
 ** DESCRIPTION: 
 ** 
 **    Pluggin for reading CTD data in C78 format. It is turned on with
 **    compile time option -D USE_GFI_C78_CTD_FORMAT.
 ** 
 ** ---------------------------------------------------------------------- ** 
 ** REVISIONS: 
 ** ---------------------------------------------------------------------- ** 
 ** CHANGES: 
 **------------------------------------------------------------------------**/


/*
  C78 Format - First pluggin
  do_it()
  Added new test to allow the C78 format
*/
#ifdef gfiplug_c78_ctd_format_1
#ifndef gfiplug_c78_ctd_format_1_already_included
#define gfiplug_c78_ctd_format_1_already_included

   if ( stricmp(FORMAT, "NODC") 
				&& stricmp(FORMAT, "WHOI") 
				&& stricmp(FORMAT, "WOCE")
				&& stricmp(FORMAT, "C78") )
     {
      printf("\n ERROR: Invalid value for FORMAT flag %s\n\n", FORMAT);
      return;
   }

#endif /* gfiplug_c78_ctd_format_1_already_included */
#endif /* gfiplug_c78_ctd_format_1 */

/* 
   C78 Format - Second pluggin
   get_header()
   Additionnal variables required for reading C78 format
*/
#ifdef gfiplug_c78_ctd_format_2
#ifndef gfiplug_c78_ctd_format_2_already_included
#define gfiplug_c78_ctd_format_2_already_included

	 /* 98.03.19 PJ
			Added variables for C78 */
	 char tmp[10];
	 char syear[3], smonth[3], sday[3], shour[3], sminute[3];
	 char slatdeg[5], slondeg[5], slatmin[3], slonmin[3], slathun[3], slonhun[3];

#endif /* gfiplug_c78_ctd_format_2_already_included */
#endif /* gfiplug_c78_ctd_format_2 */

/*
  C78 Format - Third pluggin
  get_header()
  Reading header from C78 CTD files
*/

#ifdef gfiplug_c78_ctd_format_3
#ifndef gfiplug_c78_ctd_format_3_already_included
#define gfiplug_c78_ctd_format_3_already_included

	 /* 98.03.19 PJ
			Reading the header from C78 files.
			
			NOTES: +no cast is available
			       +no cruise_id is available
						 +SeaSoar sets ship_id to cruise_id */
	 if(stricmp(FORMAT, "C78") == 0){
		 /* Read master card record */
		 if(fgets(header_line, 85, fpasc) == NULL) return(EOF);
		 if(error_found( (!(strstr(header_line, "MASTER CARD RECORD:"))),
										 "Cannot find MASTER CARD RECORD"))
			 return(1);
		 if(fgets(header_line, 85, fpasc) == NULL) return(EOF);
		 status = sscanf(header_line, 
		 "%2c%2c%4hu%4c%2c%2c%4c%2c%2c%2c%2c%2c%2c%2c%4f",
										 tmp, conf->cruise_id, &(anc->station),
										 slatdeg, slatmin, slathun, slondeg, slonmin, slonhun,
										 syear, smonth, sday, shour, sminute,
										 &(anc->ocean_depth));
		 if(error_found( (status != 15), "Scanning header"))
			 return(1);
		 /* Time Conversions */
		 time->year = atoi(syear);
		 time->year += 1900;
     if(time->year < 1970)
       time->year += 100;
		 time->month = atoi(smonth);
		 time->day = atoi(sday);
		 time->hour = atoi(shour);
		 time->minute = atoi(sminute);
		 time->second = 0;
		 /* Longitude Conversions */
		 slondeg[4] = slonmin[2] = slonhun[2] = '\0';
		 hundredths = abs_val(atoi(slondeg)) * 360000 + 
			 atoi(slonmin) * 6000 + atoi(slonhun) * 60;
		 if(strchr(slondeg, '-'))
			 hundredths *= -1;
		 HUNPOS(position, &hundredths);
		 /* Latitude Conversions */
		 slatdeg[4] = slatmin[2] = slathun[2] = '\0';
		 hundredths = abs_val(atoi(slatdeg)) * 360000 + 
			 atoi(slatmin) * 6000 + atoi(slathun) * 60;
		 if(strchr(slatdeg, '-'))
			 hundredths *= -1;
		 HUNPOS(position+1, &hundredths);
		 /* Controling time and positions */
		 if(error_found(invalid_time(time), "Invalid time"))
			 return(1);
		 if(error_found(invalid_position(position, 0), "Invalid longitude"))
			 return(1);
		 if(error_found(invalid_position(position+1, 1), "Invalid latitude"))
			 return(1);
		 /* Filling the rest of the structure */
		 anc->cast_num = 1;
		 anc->inst = BADSHORT;
		 anc->end_time = BADUSHORT;
		 anc->aver = anc->rate = BADFLOAT;
		 strcpy(conf->ship_name, SHIP_NAME);
		 /* Read profile comments */
		 if(fgets(header_line, 85, fpasc) == NULL) return(EOF);
		 if(error_found( (!(strstr(header_line, "COMMENT RECORD:"))),
										 "Cannot find COMMENT RECORD"))
			 return(1);
		 if(fgets(header_line, 85, fpasc) == NULL) return(EOF);
		 status = sscanf(header_line, "%s", profile_comment);
		 if(error_found( (status != 1), "Scanning profile comment"))
			 return(1);
		 /* Reading until beginning of data */
		 if(fgets(header_line, 85, fpasc) == NULL) return(EOF);/* CALIB. RECORD */
		 if(fgets(header_line, 85, fpasc) == NULL) return(EOF);/* Calib. data   */ 
		 if(fgets(header_line, 85, fpasc) == NULL) return(EOF);/* Column titles */
		 /* Done */
		 return(0);
	 }

#endif /* gfiplug_c78_ctd_format_3_already_included */
#endif /* gfiplug_c78_ctd_format_3 */

/*
  C78 Format - Fourth pluggin
  get_temperature()
  Reading header from C78 CTD files
*/

#ifdef gfiplug_c78_ctd_format_4
#ifndef gfiplug_c78_ctd_format_4_already_included
#define gfiplug_c78_ctd_format_4_already_included

	
	 /* 98.03.19 PJ
			Added for reading C78 files */
	 if (stricmp(FORMAT, "C78") == 0){
		 /*-----------------------------------------------
			 SCAN AVAILABLE PROFILE DATA FROM ASCII FILE
			 -----------------------------------------------*/
		 while (fgets(line, 80, fpasc) != NULL)
			 {
         if (sscanf(line, " %*d %f %*f %f %*f %*f", &p, &t) < 2)
         {
            printf("\n ERROR: Scanning temperature for n = %d", *nvalues+1);
            return(1);
         }
				 if (p <= MAX_PRESSURE){ /* store only up to MAX_PRESSURE */
					 /* I don't know anything about BAD data coding in C78 format, so I
							do not perform the test for positive temperature */
					 j = ((int) p - START_PRESSURE) / PRESSURE_INCREMENT; 
					 /* may be missing data at initial pressures */
					 temp[j] = t;
				 }
			 }
		 *nvalues = j + 1;
		 *anc_max_pressure = p;  /* from last data line in file */
		 /* Done */
		 return(0);
	 }

#endif /* gfiplug_c78_ctd_format_4_already_included */
#endif /* gfiplug_c78_ctd_format_4 */
/*
  C78 Format - Fifth pluggin
  get_salinity()
  Reading salinity from C78 CTD files
*/

#ifdef gfiplug_c78_ctd_format_5
#ifndef gfiplug_c78_ctd_format_5_already_included
#define gfiplug_c78_ctd_format_5_already_included

	 /* 98.03.19 PJ
			Added to read C78 format */
	 else if(stricmp(FORMAT, "C78") == 0)
		 strcpy(scan_format, " %*d %f %*f %*f %*f %f");

#endif /* gfiplug_c78_ctd_format_5_already_included */
#endif /* gfiplug_c78_ctd_format_5 */

