// extract data from the line read in str with order gave by hash map_var
// values:  1318 81.583900 3.000 2.983 29.5431 29.5464 5 ...
// map_var: PRES:2 DEPTH:3 PSAL:21 DOX2:18 ...
func DecodeData(nc *lib.Nc, m *config.Map, str string, profile float64, file string, line int) {

	var timestop string
	var Poslatstop string
	var Poslongstop string

	// split the string str using coma characters
	values := strings.Split(str, ",")
	nb_value := len(values)

	//extract time
	date := values[2]
	time := values[3]

	//extract pos
	lat_s := values[4]
	lat_deg := values[5]
	lat_min := values[6]
	long_s := values[7]
	long_deg := values[8]
	long_min := values[9]

	timestop = lib.ConvertDate(date + " " + time)
	Poslatstop = lat_deg + " " + lat_min + " " + lat_s
	Poslongstop = long_deg + " " + long_min + " " + long_s

	// for each physical parameter, extract its data from the rigth column
	// and save it in map data
	for key, column := range m.Map_var {

		if column > nb_value {
			log.Fatal(fmt.Sprintf("Error in func DecodeData() "+
				"configuration mismatch\nFound %d values, and we try to use column %d",
				nb_value, column))
		}

		if v, err := strconv.ParseFloat(values[column], 64); err == nil {
			m.Data[key] = v
		} else {
			log.Printf("Can't parse line: %d in file: %s\n", line, file)
			log.Fatal(err)
		}
	}
	if line == 0 {
		nc.Extras_s[fmt.Sprintf("Starttime:%d", int(profile))] = timestop
		nc.Extras_s[fmt.Sprintf("Startlatpos:%d", int(profile))] = Poslatstop
		nc.Extras_s[fmt.Sprintf("Startlongpos:%d", int(profile))] = Poslongstop
	}

	m.Data["PRFL"] = profile
	if v, err := lib.Position2Decimal(Poslatstop); err == nil {
		m.Data["LAT"] = v
	}
	if v, err := lib.Position2Decimal(Poslongstop); err == nil {
		m.Data["LONG"] = v
	}

	nc.Extras_s[fmt.Sprintf("Stopttime:%d", int(profile))] = timestop
	nc.Extras_s[fmt.Sprintf("Stoplatpos:%d", int(profile))] = Poslatstop
	nc.Extras_s[fmt.Sprintf("Stoplongpos:%d", int(profile))] = Poslongstop
}
func secondPassLADCP(nc *lib.Nc, m *config.Map, cfg toml.Configtoml, files []string, optDebug *bool) {

	regIsHeader := regexp.MustCompile(cfg.Ifm.Header)

	fmt.Fprintf(lib.Echo, "Second pass ...\n")

	// initialize profile
	var nbProfile int = 0

	// loop over each files passed throw command line
	for _, file := range files {
		var line int = 0

		fid, err := os.Open(file)
		if err != nil {
			log.Fatal(err)
		}
		defer fid.Close()
		// fmt.Printf("Read %s\n", file)

		profile := GetProfileNumber(nc, cfg, file)
		nc.Variables_1D["PROFILE"] = append(nc.Variables_1D["PROFILE"].([]float64), profile)
		scanner := bufio.NewScanner(fid)
		downcast := true
		for scanner.Scan() {
			str := scanner.Text()
			match := regIsHeader.MatchString(str)
			if match {
				DecodeHeader(nc, cfg, str, profile, optDebug)
			} else {
				// fill map data with information contain in read line str
				DecodeData(nc, m, str, profile, file, line)

				if downcast {
					// fill 2D slice
					for _, key := range m.Hdr {
						if key != "PRFL" {
							//fmt.Println("Line: ", line, "key: ", key, " data: ", data[key])
							lib.SetData(nc.Variables_2D[key], nbProfile, line, config.GetData(m.Data[key]))
						}
					}
					// exit loop if reach maximum pressure for the profile
					if m.Data["PRES"] == nc.Extras_f[fmt.Sprintf("PRES:%d", int(profile))] {
						downcast = false
					}
				} else {
					// store last julian day for end profile
					//nc.Extras_f[fmt.Sprintf("ETDD:%d", int(profile))] = m.Data["ETDD"].(float64)
					//fmt.Println(presMax)
				}
				line++
			}
		}
		if err := scanner.Err(); err != nil {
			log.Fatal(err)
		}

		// increment sclice index
		nbProfile += 1

		// store last julian day for end profile
		//nc.Extras_f[fmt.Sprintf("ETDD:%d", int(profile))] = m.Data["ETDD"].(float64)
		//fmt.Println(presMax)
		value := nc.Extras_s["DATE"] + " " + nc.Extras_s["HEURE"]
		value = lib.ConvertDate(value)
		var t = lib.NewTimeFromString("Jan 02 2006 15:04:05", value)
		v := t.Time2JulianDec()
		nc.Variables_1D["TIME"] = append(nc.Variables_1D["TIME"].([]float64), v)
	}

	fmt.Fprintln(lib.Debug, nc.Variables_1D["PROFILE"])
}