Exemplo n.º 1
0
 /// <summary>
 /// index位置に行を挿入する
 /// </summary>
 /// <param name="index"></param>
 public void InsertItemAt(int index, bool check, DiffractionProfile dp)
 {
     if (index < this.Rows.Count)
     {
         DataRow dr = this.NewDataTableProfileRow();
         dr[0] = check;
         dr[1] = dp;
         this.Rows.InsertAt(dr, index);
     }
 }
Exemplo n.º 2
0
            public void MoveItem(int srcIndex, int destIndex)
            {
                if (srcIndex < this.Rows.Count && destIndex < this.Rows.Count)
                {
                    bool check = (bool)this.Rows[srcIndex][0];
                    this.Rows[srcIndex][0]  = this.Rows[destIndex][0];
                    this.Rows[destIndex][0] = check;


                    DiffractionProfile c = (DiffractionProfile)this.Rows[srcIndex][1];
                    this.Rows[srcIndex][1]  = this.Rows[destIndex][1];
                    this.Rows[destIndex][1] = c;

                    Bitmap bmp = (Bitmap)this.Rows[srcIndex][2];
                    this.Rows[srcIndex][2]  = this.Rows[destIndex][2];
                    this.Rows[destIndex][2] = bmp;
                }
            }
Exemplo n.º 3
0
        /// <summary>
        /// よく分からないファイルを読み込む
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="separater"></param>
        /// <returns></returns>
        public static DiffractionProfile ConvertUnknownFileToProfileData(string fileName, char separater)
        {
            List <string> strArray = new List <string>();
            StreamReader  reader   = new StreamReader(fileName, Encoding.GetEncoding("Shift_JIS"));
            string        tempstr;

            while ((tempstr = reader.ReadLine()) != null)
            {
                strArray.Add(tempstr);
            }
            reader.Close();
            if (strArray.Count <= 3)
            {
                return(null);
            }

            List <string[]> stringList = new List <string[]>();

            //まず指定されたセパレータで全てを区切る
            for (int i = 0; i < strArray.Count; i++)
            {
                stringList.Add(strArray[i].Split(new char[] { separater }, StringSplitOptions.RemoveEmptyEntries));
            }
            //その全てを数値に変換する
            List <double[]> doubleList = new List <double[]>();
            List <double>   doubleTemp = new List <double>();
            double          result;

            for (int i = 0; i < stringList.Count; i++)
            {
                doubleTemp = new List <double>();
                for (int j = 0; j < stringList[i].Length; j++)
                {
                    var str = Miscellaneous.IsDecimalPointComma ? stringList[i][j].Replace('.', ',') : stringList[i][j].Replace(',', '.');
                    if (double.TryParse(str, out result))
                    {
                        doubleTemp.Add(result);
                    }
                }
                doubleList.Add(doubleTemp.ToArray());
            }
            int count        = 1;
            int beforeLength = 0;
            int countMax     = int.MinValue;
            int startColumn  = 1;
            int endColumn    = 0;

            //doubleList中で2つ以上数値を含み、10個以上連続しているものばしょをえらぶ
            for (int i = 0; i < doubleList.Count; i++)
            {
                count        = 0;
                beforeLength = doubleList[i].Length;
                if (beforeLength >= 2)
                {
                    for (int j = i; j < doubleList.Count && beforeLength == doubleList[j].Length; j++)
                    {
                        count++;
                    }
                    if (countMax < count)
                    {
                        countMax    = count;
                        startColumn = i;
                        endColumn   = i + count - 1;
                    }
                    i += count - 1;
                }
            }
            if (countMax < 10)
            {
                return(null);              //100以下だったらNullをかえす
            }
            if (endColumn + 1 < doubleList.Count)
            {
                doubleList.RemoveRange(endColumn + 1, doubleList.Count - endColumn - 1);
            }
            doubleList.RemoveRange(0, startColumn);

            //X軸をきめる 0.00000001以上のステップで100個以上連続しているものをさがす
            int    xRow = -1;
            double tempStep;

            countMax    = int.MinValue;
            startColumn = 0;
            endColumn   = 0;
            for (int i = 0; i < doubleList[0].Length; i++)
            {
                for (int j = 0; j < doubleList.Count - 1; j++)
                {
                    count    = 0;
                    tempStep = doubleList[j + 1][i] - doubleList[j][i];
                    for (int k = j + 1; k < doubleList.Count - 1 && Math.Abs(doubleList[k][i] + tempStep - doubleList[k + 1][i]) < 10 && tempStep > 0.00001; k++)
                    {
                        count++;
                    }
                    if (countMax < count)
                    {
                        countMax    = count + 2;
                        startColumn = j;
                        endColumn   = startColumn + count + 1;
                        xRow        = i;
                    }
                    j += count;
                }
            }
            if (countMax < 10 || xRow == -1)
            {
                return(null);                            //100以下かXを見つけられなかったらNullをかえす
            }
            if (endColumn + 1 < doubleList.Count)
            {
                doubleList.RemoveRange(endColumn + 1, doubleList.Count - endColumn - 1);
            }
            doubleList.RemoveRange(0, startColumn);

            //y軸を決める 標準偏差が一番大きい数が格納されている
            int    yRow = -1;
            double Sum, SumSquare, Deviation, DeviationMax;

            DeviationMax = double.NegativeInfinity;
            for (int i = 0; i < doubleList[0].Length; i++)
            {
                if (i != xRow)
                {
                    Sum = SumSquare = 0;
                    for (int j = 0; j < doubleList.Count; j++)
                    {
                        Sum       += doubleList[j][i];
                        SumSquare += doubleList[j][i] * doubleList[j][i];
                    }
                    Deviation = (doubleList.Count * SumSquare - Sum * Sum) / doubleList.Count / (doubleList.Count - 1);
                    if (DeviationMax < Deviation)
                    {
                        DeviationMax = Deviation;
                        yRow         = i;
                    }
                }
            }
            if (yRow == -1)
            {
                return(null);
            }

            //最後に値を代入
            DiffractionProfile dif = new DiffractionProfile();

            for (int i = 0; i < doubleList.Count; i++)
            {
                dif.OriginalProfile.Pt.Add(new PointD(doubleList[i][xRow], doubleList[i][yRow]));
            }

            return(dif);
        }
Exemplo n.º 4
0
        public static DiffractionProfile[] ReadPdiFile(string fileName)
        {
            //OriginalFormatType  -> SrcAxisMode
            //OriginalWaveLength  -> SrcWaveLength
            //OriginalTakeoffAngle -> SrcTakeoffAngle
            try//まずXMLのタグを変更
            {
                using (StreamReader reader = new StreamReader(fileName, Encoding.GetEncoding("Shift_JIS")))
                {
                    List <string> strList = new List <string>();
                    string        tempstr;
                    while ((tempstr = reader.ReadLine()) != null)
                    {
                        tempstr = tempstr.Replace("OriginalFormatType", "SrcAxisMode");
                        tempstr = tempstr.Replace("OriginalWaveLength", "SrcWaveLength");
                        tempstr = tempstr.Replace("OriginalTakeoffAngle", "SrcTakeoffAngle");
                        tempstr = tempstr.Replace("pt", "Pt");
                        strList.Add(tempstr);
                    }

                    reader.Close();

                    using (StreamWriter writer = new StreamWriter(fileName, false, Encoding.GetEncoding("Shift_JIS")))
                    {
                        for (int i = 0; i < strList.Count; i++)
                        {
                            writer.WriteLine(strList[i]);
                        }
                        writer.Flush();
                        writer.Close();
                    }
                }
            }
            catch { };

            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(DiffractionProfile[]));
            System.IO.FileStream fs = null;
            try
            {
                fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
                DiffractionProfile[] dp = (DiffractionProfile[])serializer.Deserialize(fs);
                fs.Close();
                if (dp.Length > 0)
                {
                    return(dp);
                }
                else
                {
                    return(new DiffractionProfile[0]);
                }
            }
            catch//もしシリアライズできなかったら、name部分に間違った日本語が書かれている可能性あり。
            {
                if (fs != null)
                {
                    fs.Close();
                }
                try
                {
                    StreamReader  reader  = new StreamReader(fileName, Encoding.UTF8);
                    List <string> strList = new List <string>();
                    string        tempstr;
                    while ((tempstr = reader.ReadLine()) != null)
                    {
                        strList.Add(tempstr);
                    }
                    reader.Close();
                    StreamWriter writer = new StreamWriter(fileName, false, Encoding.UTF8);
                    for (int i = 0; i < strList.Count; i++)
                    {
                        if (strList[i].EndsWith("\0"))
                        {
                            writer.WriteLine(strList[i].TrimEnd('\0'));
                        }
                        else
                        {
                            writer.WriteLine(strList[i]);
                        }
                    }

                    writer.Flush();
                    writer.Close();
                    fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
                    DiffractionProfile[] dp = (DiffractionProfile[])serializer.Deserialize(fs);
                    fs.Close();
                    if (dp.Length > 0)
                    {
                        return(dp);
                    }
                    else
                    {
                        return(new DiffractionProfile[0]);
                    }
                }
                catch
                {
                    try
                    {
                        if (fs != null)
                        {
                            fs.Close();
                        }
                        StreamReader  reader  = new StreamReader(fileName, Encoding.GetEncoding("Shift_JIS"));
                        List <string> strList = new List <string>();
                        string        tempstr;
                        while ((tempstr = reader.ReadLine()) != null)
                        {
                            strList.Add(tempstr);
                        }
                        reader.Close();
                        if (strList.Count <= 3)
                        {
                            return(new DiffractionProfile[0]);
                        }

                        DiffractionProfile diffProf = new DiffractionProfile();

                        //古いヘッダの書式
                        //Wave Length (0.1nm):0.4176811455              0
                        //Camera Length (mm):445.8055943768             1
                        //Pixel Size Horizontal(mm):0.10080533          2
                        //AspectRatio(Vertical/Horizontal):1            3
                        //Mode:Angle                                    4
                        //4,733.102005852614                            5

                        if (strList[0].IndexOf("Wave Length") >= 0)
                        {
                            if (strList[0].IndexOf("(nm)") >= 0)
                            {
                                diffProf.SrcWaveLength = Convert.ToDouble((strList[0].Split(':'))[1]);
                            }
                            else if (strList[0].IndexOf("(0.1nm)") >= 0)
                            {
                                diffProf.SrcWaveLength = Convert.ToDouble((strList[0].Split(':'))[1]) / 10.0;
                            }
                        }

                        if ((strList[4].Split(':'))[1] == "Angle")
                        {
                            diffProf.SrcAxisMode = HorizontalAxis.Angle;
                        }
                        else if ((strList[4].Split(':'))[1] == "d-spacing")
                        {
                            diffProf.SrcAxisMode = HorizontalAxis.d;
                        }
                        else if ((strList[4].Split(':'))[1] == "Energy")
                        {
                            diffProf.SrcAxisMode = HorizontalAxis.EnergyXray;
                        }
                        else
                        {
                            return(new DiffractionProfile[0]);
                        }

                        for (int i = 5; i < strList.Count; i++)
                        {
                            string[] str = strList[i].Split(',');
                            diffProf.OriginalProfile.Pt.Add(new PointD(Convert.ToDouble(str[0]), Convert.ToDouble(str[1])));
                        }
                        diffProf.Name = fileName.Remove(0, fileName.LastIndexOf('\\') + 1);
                        return(new DiffractionProfile[] { diffProf });
                    }
                    catch { return(new DiffractionProfile[0]); }
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 複数のプロファイルを含むCSV形式(PDI独自形式)
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static DiffractionProfile[] ReadCSVFile(string fileName)
        {
            var strArray = new List <string>();

            using (var reader = new StreamReader(fileName, Encoding.GetEncoding("Shift_JIS")))
            {
                string tempstr;
                while ((tempstr = reader.ReadLine()) != null)
                {
                    strArray.Add(tempstr);
                }
            }

            if (strArray.Count <= 3)
            {
                return(new DiffractionProfile[0]);
            }

            if (!strArray[1].StartsWith("X,Y,"))
            {
                return(new DiffractionProfile[0]);
            }

            try
            {
                string[] title = strArray[0].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string[] axis  = strArray[1].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string[] value = strArray[2].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (title.Length * 2 != axis.Length || axis.Length != value.Length)
                {
                    return(new DiffractionProfile[0]);
                }

                var dp = new DiffractionProfile[title.Length];
                for (int i = 0; i < dp.Length; i++)
                {
                    dp[i]      = new DiffractionProfile();
                    dp[i].Name = title[i];
                }
                for (int i = 2; i < strArray.Count; i++)
                {
                    value = strArray[i].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int j = 0; j < value.Length / 2; j++)
                    {
                        double x = Convert.ToDouble(value[j * 2]);
                        double y = Convert.ToDouble(value[j * 2 + 1]);
                        dp[j].OriginalProfile.Pt.Add(new PointD(x, y));
                    }
                }

                for (int i = 0; i < dp.Length; i++)
                {
                    if (title[i] == "")
                    {
                        dp[i].Name = $"{Path.GetFileName(fileName)}{(dp.Length > 1 ? $" -{i}" : "")}";
                    }
                    else
                    {
                        dp[i].Name = title[i];
                    }
                }
                return(dp);
            }
            catch
            {
                return(new DiffractionProfile[0]);
            }
        }
Exemplo n.º 6
0
        private void setGsasFileContents()
        {
            double div = 100;

            if (formMain.AxisMode == HorizontalAxis.NeutronTOF)
            {
                div = 1;
            }

            StringBuilder      sb = new StringBuilder();
            DiffractionProfile dp = (DiffractionProfile)((DataRowView)formMain.bindingSourceProfile.Current).Row[1];;

            //一行目
            string str = dp.Name;

            sb.Append(str + "\r\n");

            //二行目
            int    ptCount    = dp.Profile.Pt.Count;
            double startAngle = dp.Profile.Pt[0].X * div;
            double stepAngle  = (dp.Profile.Pt[1].X - dp.Profile.Pt[0].X) * div;

            str = "BANK 1 " + ptCount.ToString() + " " + ptCount.ToString() + " CONST " + startAngle.ToString("f2") + " " + stepAngle.ToString("f2") + " 0 0 FXYE";
            sb.Append(str + "\r\n");
            str = "";
            bool validErr = false;

            if (dp.Profile.Err != null && dp.Profile.Err.Count == dp.Profile.Pt.Count)
            {
                for (int i = 0; i < ptCount; i++)
                {
                    if (dp.Profile.Err[i].IsNaN && dp.Profile.Err[i].Y != 0)
                    {
                        validErr = true;
                        break;
                    }
                }
            }
            for (int i = 0; i < ptCount; i++)
            {
                string[] value = new string[3];
                value[0] = (dp.Profile.Pt[i].X * div).ToString("g12");
                value[1] = dp.Profile.Pt[i].Y.ToString("g12");
                if (validErr)
                {
                    value[2] = dp.Profile.Err[i].Y.ToString("g12");
                }
                else
                {
                    value[2] = Math.Sqrt(dp.Profile.Pt[i].Y).ToString("g12");
                }

                string y = dp.Profile.Pt[i].Y.ToString("g7");

                for (int j = 0; j < value.Length; j++)
                {
                    if (value[j].Length > 11)
                    {
                        value[j] = value[j].Substring(0, 11);
                    }
                    if (value[j].EndsWith("."))
                    {
                        value[j] = " " + y.Substring(0, 10);
                    }
                    while (value[j].Length < 11)
                    {
                        value[j] = " " + value[j];
                    }
                }
                sb.Append(" " + value[0] + " " + value[1] + " " + value[2] + "\r\n");
            }
            richTextBoxGsa.Text = sb.ToString();
            #region

            /*         StringBuilder sb = new StringBuilder();
             *       DiffractionProfile dp = (DiffractionProfile)((DataRowView)formMain.bindingSourceProfile.Current).Row[1]; ;
             *
             *       //一行目
             *       string str = dp.Name;
             *       while (str.Length < 80)
             *           str += " ";
             *       sb.Append(str+"\r\n");
             *
             *       //二行目
             *       int ptCount = dp.Profile.Pt.Count;
             *       double startAngle = dp.Profile.Pt[0].X * 100;
             *       double stepAngle = (dp.Profile.Pt[1].X - dp.Profile.Pt[0].X) * 100;
             *       str = "BANK 1 " + ptCount.ToString() + " " + (ptCount / 5).ToString() + " CONST " + startAngle.ToString("f2") + " " + stepAngle.ToString("f2") + " 0 0 ESD";
             *       while (str.Length < 80)
             *           str += " ";
             *       sb.Append(str + "\r\n");
             *       str = "";
             *       int n = 0;
             *       for (int i = 0; i < ptCount; i++)
             *       {
             *           n++;
             *           string y = dp.Profile.Pt[i].Y.ToString("g7");
             *           string yErr = Math.Sqrt(dp.Profile.Pt[i].Y).ToString("g8");
             *           if (y.Length > 7)
             *               y = y.Substring(0, 7);
             *           if (y.EndsWith("."))
             *               y = " " + y.Substring(0, 6);
             *           while (y.Length < 7)
             *               y = " " + y;
             *
             *           if (yErr.Length > 7)
             *               yErr = yErr.Substring(0, 7);
             *           if (yErr.EndsWith("."))
             *               yErr = " " + yErr.Substring(0, 6);
             *           while (yErr.Length < 7)
             *               yErr = " " + yErr;
             *
             *           str += " " + y + " " + yErr;
             *           if (n == 5)
             *           {
             *               sb.Append(str + "\r\n");
             *               str = "";
             *               n = 0;
             *           }
             *           else if (i == ptCount - 1)
             *           {
             *               while (str.Length < 80)
             *                   str += " ";
             *               sb.Append(str + "\r\n");
             *           }
             *       }
             *       richTextBoxGsa.Text = sb.ToString();*/
            #endregion
        }
Exemplo n.º 7
0
        private void buttonCalibrate_Click(object sender, EventArgs e)
        {
            //まず、現在のcoeffを保存
            DiffractionProfile dp = (DiffractionProfile)((DataRowView)formMain.bindingSourceProfile.Current).Row[1];

            coeff0_old = dp.TwoThetaOffsetCoeff0;
            coeff1_old = dp.TwoThetaOffsetCoeff1;
            coeff2_old = dp.TwoThetaOffsetCoeff2;

            buttonCalibrate.Text = "Now calibrationg...";
            this.Enabled         = false;

            for (int n = 0; n < 8; n++)
            {
                formMain.formFitting.Fitting();

                List <double> twoThetaObs  = new List <double>();
                List <double> twoThetaCalc = new List <double>();

                foreach (var p in formMain.formFitting.TargetCrystal.Plane)
                {
                    if (p.IsFittingChecked && !double.IsNaN(p.XObs) && !double.IsNaN(p.XCalc))
                    {
                        twoThetaObs.Add(p.XObs);
                        twoThetaCalc.Add(p.XCalc);
                    }
                }
                int count = twoThetaCalc.Count;
                if (count == 0)
                {
                    return;
                }

                int order = Math.Min((int)numericUpDownOrder.Value, count);

                var X = new DenseMatrix(count, order);
                var Y = new DenseMatrix(count, 1);
                for (int i = 0; i < count; i++)
                {
                    double tan = Math.Tan(twoThetaCalc[i] / 360.0 * Math.PI);
                    for (int j = 0; j < order; j++)
                    {
                        X[i, j] = Math.Pow(tan, j);
                    }
                    Y[i, 0] = Math.Tan(twoThetaCalc[i] - twoThetaObs[i]);
                }
                var inv = (X.Transpose() * X).TryInverse();
                if (inv != null)
                {
                    var A = inv * X.Transpose() * Y;
                    if (order > 0)
                    {
                        formMain.formProfile.TwoThetaOffsetCoeff0 += A[0, 0] * 0.9;
                    }
                    else
                    {
                        formMain.formProfile.TwoThetaOffsetCoeff0 = 0;
                    }
                    if (order > 1)
                    {
                        formMain.formProfile.TwoThetaOffsetCoeff1 += A[1, 0] * 0.9;
                    }
                    else
                    {
                        formMain.formProfile.TwoThetaOffsetCoeff1 = 0;
                    }

                    if (order > 2)
                    {
                        formMain.formProfile.TwoThetaOffsetCoeff2 += A[2, 0] * 0.9;
                    }
                    else
                    {
                        formMain.formProfile.TwoThetaOffsetCoeff2 = 0;
                    }
                }
            }

            buttonCalibrate.Text = "Calibrate";
            this.Enabled         = true;
        }