MissionPlanner.MagCalib.getOffsetsLog C# (CSharp) Method

getOffsetsLog() public static method

public static getOffsetsLog ( string fn ) : double[]
fn string
return double[]
        public static double[] getOffsetsLog(string fn)
        {
            // this is for a dxf
            PolylineVertex vertex;
            List<PolylineVertex> vertexes = new List<PolylineVertex>();

            List<Tuple<float, float, float>> data = new List<Tuple<float, float, float>>();

            List<Tuple<float, float, float>> data2 = new List<Tuple<float, float, float>>();

            List<Tuple<float, float, float>> data3 = new List<Tuple<float, float, float>>();

            double[] ofsDoubles = new double[3];
            double[] ofsDoubles2 = new double[3];
            double[] ofsDoubles3 = new double[3];

            CollectionBuffer logdata = new CollectionBuffer(File.OpenRead(fn));

            var dflog = logdata.dflog;
            
            foreach (var line in logdata.GetEnumeratorType(new[]{"MAG","MAG2","MAG3"}))
            {
                if (line.msgtype == "MAG" || line.msgtype == "MAG2" || line.msgtype == "MAG3")
                {
                    int indexmagx = dflog.FindMessageOffset(line.msgtype, "MagX");
                    int indexmagy = dflog.FindMessageOffset(line.msgtype, "MagY");
                    int indexmagz = dflog.FindMessageOffset(line.msgtype, "MagZ");

                    int indexoffsetx = dflog.FindMessageOffset(line.msgtype, "OfsX");
                    int indexoffsety = dflog.FindMessageOffset(line.msgtype, "OfsY");
                    int indexoffsetz = dflog.FindMessageOffset(line.msgtype, "OfsZ");

                    if (indexmagx != -1 && indexoffsetx != -1)
                    {
                        float magx = float.Parse(line.items[indexmagx]);
                        float magy = float.Parse(line.items[indexmagy]);
                        float magz = float.Parse(line.items[indexmagz]);

                        float offsetx = float.Parse(line.items[indexoffsetx]);
                        float offsety = float.Parse(line.items[indexoffsety]);
                        float offsetz = float.Parse(line.items[indexoffsetz]);

                        //offsetx = offsety = offsetz = 0;

                        if (line.msgtype == "MAG")
                        {
                            data.Add(new Tuple<float, float, float>(
                                magx - offsetx,
                                magy - offsety,
                                magz - offsetz));

                            ofsDoubles[0] = offsetx;
                            ofsDoubles[1] = offsety;
                            ofsDoubles[2] = offsetz;

                            // fox dxf
                            vertex = new PolylineVertex(new netDxf.Vector3(magx - offsetx,
                                magy - offsety,
                                magz - offsetz)
                                );
                            vertexes.Add(vertex);
                        }
                        else if (line.msgtype == "MAG2")
                        {
                            data2.Add(new Tuple<float, float, float>(
                                magx - offsetx,
                                magy - offsety,
                                magz - offsetz));

                            ofsDoubles2[0] = offsetx;
                            ofsDoubles2[1] = offsety;
                            ofsDoubles2[2] = offsetz;
                        }
                        else if (line.msgtype == "MAG3")
                        {
                            data3.Add(new Tuple<float, float, float>(
                                magx - offsetx,
                                magy - offsety,
                                magz - offsetz));

                            ofsDoubles3[0] = offsetx;
                            ofsDoubles3[1] = offsety;
                            ofsDoubles3[2] = offsetz;
                        }
                    }
                }
            }

            double[] x = LeastSq(data, false);

            log.InfoFormat("magcal 1 ofs {0},{1},{2} strength {3} old ofs {4},{5},{6}", x[0], x[1], x[2], x[3], ofsDoubles[0], ofsDoubles[1], ofsDoubles[2]);
            
            x = LeastSq(data,true);

            log.InfoFormat("magcalel 1 ofs {0},{1},{2} strength {3} old ofs {4},{5},{6}", x[0], x[1], x[2], x[3], ofsDoubles[0], ofsDoubles[1], ofsDoubles[2]);

            if (data2.Count > 0)
            {
                double[] x2 = LeastSq(data2, false);
                log.InfoFormat("magcal 2 ofs {0},{1},{2} strength {3} old ofs {4},{5},{6}", x2[0], x2[1], x2[2], x2[3], ofsDoubles2[0], ofsDoubles2[1], ofsDoubles2[2]);
                x2 = LeastSq(data2, true);
                log.InfoFormat("magcalel 2 ofs {0},{1},{2} strength {3} old ofs {4},{5},{6}", x2[0], x2[1], x2[2], x2[3], ofsDoubles2[0], ofsDoubles2[1], ofsDoubles2[2]);
            }

            if (data3.Count > 0)
            {
                double[] x3 = LeastSq(data3, false);
                log.InfoFormat("magcal 3 ofs {0},{1},{2} strength {3} old ofs {4},{5},{6}", x3[0], x3[1], x3[2], x3[3], ofsDoubles3[0], ofsDoubles3[1], ofsDoubles3[2]);
                x3 = LeastSq(data3, true);
                log.InfoFormat("magcalel 3 ofs {0},{1},{2} strength {3} old ofs {4},{5},{6}", x3[0], x3[1], x3[2], x3[3], ofsDoubles3[0], ofsDoubles3[1], ofsDoubles3[2]);
            }


            log.Info("Least Sq Done " + DateTime.Now);

            doDXF(vertexes, x);

            Array.Resize<double>(ref x, 3);

            return x;
        }