Ejemplo n.º 1
0
        private IExtractResult JPAnaStatAlgorithm(Action <int, string> progressTracker)
        {
            string[]               inputCurrentFiles = null;                             //= ExportManager.GetInstance().List.ToArray();//GetStringArray("RasterCurrentFile"); 由查询那里获得
            string[]               inputHistoryFiles = null;
            List <string>          list1             = ExportManager.GetInstance().List; //得到的是从数据库里查询出来的周期数据
            StatisticResultManager manager           = StatisticResultManager.GetInstance();
            List <StatisticResult> list  = manager.List;
            List <string>          list2 = manager.GetFilePathFromList(); //得到的是基于数据查询的数据又做的统计数;

            #region 获得目标区域
            regionNames  = _argumentProvider.GetArg("regionNames") as string;
            aoiContainer = new GeoDo.RSS.Core.VectorDrawing.AOIContainerLayer();
            string        fieldName;
            string        shapeFilename;
            int           fieldIndex  = -1;
            List <string> fieldValues = new List <string>();
            string        regionsname = "";
            using (frmStatSubRegionTemplates frm = new frmStatSubRegionTemplates())
            {
                frm.listView1.MultiSelect = true;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    Feature[] fets = frm.GetSelectedFeatures();
                    fets = frm.GetStatFeatures(out fieldName, out shapeFilename, out fieldIndex);
                    if (fets == null)
                    {
                        aoiContainer = null;
                        regionsname  = "全国";
                    }
                    else
                    {
                        string chinafieldValue = fets[0].GetFieldValue(fieldIndex);
                        if (chinafieldValue == "中国")
                        {
                            aoiContainer = null;
                            regionsname  = "全国";
                        }
                        else
                        {
                            foreach (Feature fet in fets)
                            {
                                fieldValues.Add(fet.GetFieldValue(fieldIndex)); //获得选择区域名称
                                aoiContainer.AddAOI(fet);
                            }
                            foreach (string region in fieldValues)
                            {
                                regionsname += region;
                            }
                            if (regionsname.Contains("西藏") && regionsname.Contains("青海"))
                            {
                                regionsname = "青藏地区";
                            }
                            if (!string.IsNullOrEmpty(regionNames))
                            {
                                regionsname = regionNames.Trim();
                            }
                        }
                    }
                }
                else
                {
                    return(null);
                }
            }
            #endregion
            Regex DataReg = new Regex(@"(?<year>\d{4})", RegexOptions.Compiled);
            //处理后的数据存储路径
            string savePath   = _argumentProvider.GetArg("HistoryDataSave") as string;
            string jpsavepath = savePath + "\\" + "距平分析";
            if (!System.IO.Directory.Exists(jpsavepath))//如果不存在这个路径
            {
                System.IO.Directory.CreateDirectory(jpsavepath);
            }
            //参数设置
            List <string> paraset = _argumentProvider.GetArg("paraSet") as List <string>;
            string[]      paras   = paraset.ToArray();
            //先裁切分别算每个文件的雪水当量体积后再相减
            string Nostr = "";  //排除非选择的轨道数据
            if (paras[0] == "Ascend")
            {
                Nostr = "_D_";
            }
            if (paras[0] == "Descend")
            {
                Nostr = "_A_";
            }
            if (paras[1] == "winter")   // inputCurrentFiles = manager.GetFilePathFromList() 冬季的数所是统计出来的。
            {                           // inputHistoryFiles  需要再重新计算一次  *** 这是多对一计算距平
                //比如说要算2000-2013年冬季的距平,需要先分别算出每年冬季的值,用这些值算总的均值。再分别用每年冬季减去总冬季值。
                List <string> files = new List <string>();
                foreach (string file in list2)
                {
                    if (!Path.GetFileName(file).Contains(Nostr))
                    {
                        files.Add(file);
                    }
                    if (file.Contains("MWSE"))
                    {
                        prdType = "SWE";
                    }
                    else
                    {
                        prdType = "SD";
                    }
                }
                inputCurrentFiles = files.ToArray();
                string      outHistoryFile = jpsavepath + "\\" + Path.GetFileNameWithoutExtension(inputCurrentFiles[0]).Substring(0, 14) + "_" + paras[2] + ".dat";
                SNWParaStat snwStat        = new SNWParaStat();
                snwStat.SNWParaAvgStat(inputCurrentFiles, 0.1f, outHistoryFile);
                List <string> temphistory = new List <string>();
                temphistory.Add(outHistoryFile);
                inputHistoryFiles = temphistory.ToArray();
                if (inputCurrentFiles == null || inputCurrentFiles.Length < 0 || inputHistoryFiles == null || inputHistoryFiles.Length < 0)
                {
                    PrintInfo("缺少分析文件");
                    return(null);
                }
            }
            else
            {
                inputCurrentFiles = list1.ToArray();
                inputHistoryFiles = list2.ToArray();
                if (inputCurrentFiles[0].Contains("MWSE"))
                {
                    prdType = "SWE";
                }
                else
                {
                    prdType = "SD";
                }
                if (inputCurrentFiles == null || inputCurrentFiles.Length < 0 || inputHistoryFiles == null || inputHistoryFiles.Length < 0)
                {
                    PrintInfo("缺少分析文件");
                    return(null);
                }
                if ((inputCurrentFiles.Length) * 2 != inputHistoryFiles.Length)
                {
                    MessageBox.Show("当年数据与同期数据不对应");
                    return(null);
                }
            }
            #region 裁切文件
            string clipsave = jpsavepath + "\\" + regionsname + "\\" + "裁切";
            if (!System.IO.Directory.Exists(clipsave))//如果不存在这个路径
            {
                System.IO.Directory.CreateDirectory(clipsave);
            }
            //体积文件SVOL存放路径
            string svolPath = jpsavepath + "\\" + regionsname + "\\" + "体积文件";
            if (!System.IO.Directory.Exists(svolPath))
            {
                System.IO.Directory.CreateDirectory(svolPath);
            }
            //统计出的各个文件夹的数据放在txt文本里
            string       statxt = jpsavepath + "\\" + regionsname + "\\" + regionsname + ".txt";
            FileStream   fauto  = new FileStream(statxt, FileMode.Create, FileAccess.Write);
            StreamWriter rauto  = new StreamWriter(fauto);
            if (aoiContainer == null)
            {
                //不用裁切 直接计算
                resultList.Clear();
                if (paras[1] == "winter")
                {
                    #region 冬季
                    //只返回一个体积和
                    double pixelSumWinter; // 总冬季雪水当量的体积和,是被减数
                    if (prdType == "SWE")
                    {
                        title = regionsname + "冬季雪水当量距平" + "(" + paras[2] + "年)";
                        string sumsweVolFile = svolPath + "\\" + Path.GetFileName(inputHistoryFiles[0]).Replace("MSWE", "SVOL");
                        if (!File.Exists(sumsweVolFile))
                        {
                            IFileExtractResult sweVolResult = ComputeSnowSWEVOL(inputHistoryFiles[0], sumsweVolFile);
                            sumsweVolFile = sweVolResult.FileName;
                        }
                        using (IRasterDataProvider inRaster = RasterDataDriver.Open(sumsweVolFile) as IRasterDataProvider)
                        {
                            pixelSumWinter = CompuCurPixel(sumsweVolFile);
                        }
                        foreach (string infile in inputCurrentFiles)
                        {
                            //MWS_JPEA_MSWE_China_Month_SWE_D_2011_2013_1_avg.dat
                            Regex  DataReg2 = new Regex(@"(?<year>\d{4})_(?<month>\d{2})_(?<year>\d{4})_(?<month>\d{1})", RegexOptions.Compiled);
                            Match  m        = DataReg2.Match(infile); //提取每年冬季的时间
                            string year     = "";
                            if (m.Success)
                            {
                                year = m.Value;
                            }
                            string mx = ""; //横轴时间
                            mx = year.Substring(0, 4) + "年";
                            string sweVolFile = svolPath + "\\" + Path.GetFileName(infile).Replace("MSWE", "SVOL");
                            if (!File.Exists(sweVolFile))
                            {
                                IFileExtractResult sweVolResult = ComputeSnowSWEVOL(infile, sweVolFile);
                                sweVolFile = sweVolResult.FileName;
                            }
                            double pixelSum;//只返回一个体积和
                            using (IRasterDataProvider inRaster = RasterDataDriver.Open(sweVolFile) as IRasterDataProvider)
                            {
                                pixelSum = CompuCurPixel(sweVolFile);
                            }
                            resultList.Add(new string[] { mx, ((pixelSum - pixelSumWinter) / pixelSumWinter * 100).ToString() + "%" });
                            rauto.WriteLine(mx + " " + ((pixelSum - pixelSumWinter) / pixelSumWinter * 100).ToString() + "%");
                        }
                    }
                    if (prdType == "SD")
                    {
                        title = regionsname + "冬季雪水当量距平" + "(" + paras[2] + "年)";
                        using (IRasterDataProvider inRaster = RasterDataDriver.Open(inputHistoryFiles[0]) as IRasterDataProvider)
                        {
                            pixelSumWinter = CompuCurPixel(inputHistoryFiles[0]);
                        }
                        foreach (string infile in inputCurrentFiles)
                        {
                            //MWS_JPEA_MSWE_China_Month_SD_D_2011_2013_1_avg.dat
                            Regex  DataReg2 = new Regex(@"(?<year>\d{4})_(?<month>\d{2})_(?<year>\d{4})_(?<month>\d{1})", RegexOptions.Compiled);
                            Match  m        = DataReg2.Match(infile); //提取每年冬季的时间
                            string year     = "";
                            if (m.Success)
                            {
                                year = m.Value;
                            }
                            string mx = ""; //横轴时间
                            mx = year.Substring(0, 4) + "年";
                            string sweVolFile = svolPath + "\\" + Path.GetFileName(infile).Replace("MSWE", "SVOL");
                            //只返回一个体积和
                            double pixelSum;
                            using (IRasterDataProvider inRaster = RasterDataDriver.Open(infile) as IRasterDataProvider)
                            {
                                pixelSum = CompuCurPixel(infile);
                            }
                            resultList.Add(new string[] { mx, ((pixelSum - pixelSumWinter) / pixelSumWinter * 100).ToString() + "%" });
                            rauto.WriteLine(mx + " " + ((pixelSum - pixelSumWinter) / pixelSumWinter * 100).ToString() + "%");
                        }
                    }
                }
                #endregion
                else
                {
                    #region dang nian旬月
                    resultList.Clear();
                    foreach (string infile in inputCurrentFiles)
                    {
                        //先假设选择的是旬数据 MWS_MWSD_China_Xun_0SD_A_2011_1_avg.dat
                        //       则同期旬数据 MWS_MWSD_China_Xun_0SD_A_1989_2011_1_avg.dat
                        string cfile = Path.GetFileNameWithoutExtension(infile);
                        Match  m     = DataReg.Match(cfile);
                        string date  = "";
                        if (m.Success)
                        {
                            date = m.Value;    //根据年4个数字来拆分字符串
                        }
                        string bhalf = cfile.Substring(0, cfile.IndexOf(date));
                        string ahalf = cfile.Substring(cfile.IndexOf(date) + 4, cfile.Length - 4 - cfile.IndexOf(date));
                        foreach (string inputHistoryFile in inputHistoryFiles)
                        {
                            string aa             = Path.GetFileNameWithoutExtension(inputHistoryFile);
                            double infilepixelSum = 0; //只返回一个体积和
                            double hisfilepixelSum;    //只返回一个体积和
                            if (aa.Contains(bhalf) && aa.Contains(ahalf) && aa.Contains(date) && !aa.Contains(Nostr))
                            {
                                #region 当前文件体积计算,并相减
                                if (prdType == "SWE")
                                {
                                    string sweVolFile = svolPath + "\\" + Path.GetFileName(infile).Replace("MSWE", "SVOL");
                                    if (!File.Exists(sweVolFile))
                                    {
                                        IFileExtractResult sweVolResult = ComputeSnowSWEVOL(infile, sweVolFile);
                                        sweVolFile = sweVolResult.FileName;
                                    }
                                    using (IRasterDataProvider inRaster = RasterDataDriver.Open(sweVolFile) as IRasterDataProvider)
                                    {
                                        infilepixelSum = CompuCurPixel(sweVolFile);
                                    }
                                    #region 历史文件做裁切,体积计算,并相减
                                    string hissweVolFile = svolPath + "\\" + Path.GetFileName(inputHistoryFile).Replace("MSWE", "SVOL");
                                    if (!File.Exists(hissweVolFile))
                                    {
                                        IFileExtractResult sweVolResult = ComputeSnowSWEVOL(inputHistoryFile, hissweVolFile);
                                        hissweVolFile = sweVolResult.FileName;
                                    }
                                    using (IRasterDataProvider inRaster = RasterDataDriver.Open(hissweVolFile) as IRasterDataProvider)
                                    {
                                        hisfilepixelSum = CompuCurPixel(hissweVolFile);
                                    }
                                    #endregion
                                }
                                else
                                {
                                    using (IRasterDataProvider inRaster = RasterDataDriver.Open(infile) as IRasterDataProvider)
                                    {
                                        infilepixelSum = CompuCurPixel(infile);
                                    }
                                    using (IRasterDataProvider inRaster = RasterDataDriver.Open(inputHistoryFile) as IRasterDataProvider)
                                    {
                                        hisfilepixelSum = CompuCurPixel(inputHistoryFile);
                                    }
                                }
                                #endregion
                                //提取年至avg之间的字符
                                string   filetime = cfile.Substring(cfile.IndexOf(date), cfile.Length - 3 - cfile.IndexOf(date));
                                string[] mxchars  = filetime.Split(new char[] { '_' });
                                string   mx       = "";//月+旬
                                if (cfile.Contains("Xun"))
                                {
                                    title = regionsname + date + "年冬季雪水当量旬距平" + "(" + paras[2] + "年)";
                                    if (mxchars[2] == "1")
                                    {
                                        mx = mxchars[1] + "月" + "上旬";
                                    }
                                    if (mxchars[2] == "2")
                                    {
                                        mx = mxchars[1] + "月" + "中旬";
                                    }
                                    if (mxchars[2] == "3")
                                    {
                                        mx = mxchars[1] + "月" + "下旬";
                                    }
                                }
                                else if (cfile.Contains("Month"))
                                {
                                    title = regionsname + date + "年冬季雪水当量月距平" + "(" + paras[2] + "年)";
                                    mx    = mxchars[1] + "月";
                                }
                                else
                                {
                                    mx = filetime;
                                }
                                resultList.Add(new string[] { mx, ((infilepixelSum - hisfilepixelSum) / hisfilepixelSum * 100).ToString() + "%" });
                                rauto.WriteLine(mx + " " + ((infilepixelSum - hisfilepixelSum) / hisfilepixelSum * 100).ToString() + "%");
                            }
                        }
                    }
                    #endregion
                }
            }
            else                                                //选择了aois
            {
                MulRegionsClip muticlip = new MulRegionsClip(); //裁切
                if (paras[1] == "winter")
                {
                    resultList.Clear();
                    double pixelSumWinter = 0d; // 总冬季雪水当量的体积和,是被减数
                    #region
                    title = regionsname + "冬季雪水当量距平" + "(" + paras[2] + "年)";
                    //先把总的冬季给裁了
                    string newsumWinter = Path.Combine(clipsave, Path.GetFileName(inputHistoryFiles[0]).Replace("China", regionsname));
                    if (!File.Exists(newsumWinter))
                    {
                        string sumWinter = muticlip.MutiRegionsClip(inputHistoryFiles[0], aoiContainer, clipsave);
                        string hdrfile   = Path.GetDirectoryName(sumWinter) + "\\" + Path.GetFileNameWithoutExtension(sumWinter) + ".hdr";
                        //重命名
                        string   newhdrfile = Path.Combine(Path.GetDirectoryName(sumWinter), Path.GetFileNameWithoutExtension(inputHistoryFiles[0]).Replace("China", regionsname) + ".hdr");
                        FileInfo fi         = new FileInfo(sumWinter);
                        fi.MoveTo(newsumWinter);
                        FileInfo fihdr = new FileInfo(hdrfile);
                        fihdr.MoveTo(newhdrfile);
                    }
                    if (prdType == "SWE")
                    {
                        string sumsweVolFile = svolPath + "\\" + Path.GetFileName(newsumWinter).Replace("MSWE", "SVOL");
                        if (!File.Exists(sumsweVolFile))
                        {
                            IFileExtractResult sweVolResult = ComputeSnowSWEVOL(newsumWinter, sumsweVolFile);
                            sumsweVolFile = sweVolResult.FileName;
                        }
                        using (IRasterDataProvider inRaster = RasterDataDriver.Open(sumsweVolFile) as IRasterDataProvider)
                        {
                            pixelSumWinter = CompuCurPixel(sumsweVolFile);
                        }
                    }
                    if (prdType == "SD")
                    {
                        using (IRasterDataProvider inRaster = RasterDataDriver.Open(inputHistoryFiles[0]) as IRasterDataProvider)
                        {
                            pixelSumWinter = CompuCurPixel(inputHistoryFiles[0]);
                        }
                    }
                    foreach (string infile in inputCurrentFiles)
                    {
                        //MWS_JPEA_MSWE_China_Month_SWE_D_2011_2013_1_avg.dat
                        Regex  DataReg2 = new Regex(@"(?<year>\d{4})_(?<month>\d{2})_(?<year>\d{4})_(?<month>\d{1})", RegexOptions.Compiled);
                        Match  m        = DataReg2.Match(infile); //提取每年冬季的时间
                        string year     = "";
                        if (m.Success)
                        {
                            year = m.Value;
                        }
                        string mx = ""; //横轴时间
                        mx = year.Substring(0, 4) + "年";
                        string newclipfile = Path.Combine(clipsave, Path.GetFileName(infile).Replace("China", regionsname));
                        if (!File.Exists(newclipfile))
                        {
                            string clipfile = muticlip.MutiRegionsClip(infile, aoiContainer, clipsave);
                            string hdrfile  = Path.GetDirectoryName(clipfile) + "\\" + Path.GetFileNameWithoutExtension(clipfile) + ".hdr";
                            //重命名
                            string   newhdrfile = Path.Combine(Path.GetDirectoryName(clipfile), Path.GetFileNameWithoutExtension(infile).Replace("China", regionsname) + ".hdr");
                            FileInfo fi         = new FileInfo(clipfile);
                            fi.MoveTo(newclipfile);
                            FileInfo fihdr = new FileInfo(hdrfile);
                            fihdr.MoveTo(newhdrfile);
                        }
                        //只返回一个体积和
                        double pixelSum = 0d;
                        if (prdType == "SWE")
                        {
                            string sweVolFile = svolPath + "\\" + Path.GetFileName(newclipfile).Replace("MSWE", "SVOL");
                            if (!File.Exists(sweVolFile))
                            {
                                IFileExtractResult sweVolResult = ComputeSnowSWEVOL(newclipfile, sweVolFile);
                                sweVolFile = sweVolResult.FileName;
                            }
                            using (IRasterDataProvider inRaster = RasterDataDriver.Open(sweVolFile) as IRasterDataProvider)
                            {
                                pixelSum = CompuCurPixel(sweVolFile);
                            }
                        }
                        if (prdType == "SD")
                        {
                            using (IRasterDataProvider inRaster = RasterDataDriver.Open(infile) as IRasterDataProvider)
                            {
                                pixelSum = CompuCurPixel(infile);
                            }
                        }
                        resultList.Add(new string[] { mx, ((pixelSum - pixelSumWinter) / pixelSumWinter * 100).ToString() + "%" });
                        rauto.WriteLine(mx + " " + ((pixelSum - pixelSumWinter) / pixelSumWinter).ToString());
                    }
                }
                #endregion
                else                //选择的是旬、月
                {
                    #region
                    resultList.Clear();
                    foreach (string infile in inputCurrentFiles)
                    {
                        //先假设选择的是旬数据 MWS_MWSD_China_Xun_0SD_A_2011_1_avg.dat
                        //       则同期旬数据 MWS_MWSD_China_Xun_0SD_A_1989_2011_1_avg.dat
                        string cfile = Path.GetFileNameWithoutExtension(infile);
                        Match  m     = DataReg.Match(cfile);
                        string date  = "";
                        if (m.Success)
                        {
                            date = m.Value;    //根据年4个数字来拆分字符串
                        }
                        string bhalf = cfile.Substring(0, cfile.IndexOf(date));
                        string ahalf = cfile.Substring(cfile.IndexOf(date) + 4, cfile.Length - 4 - cfile.IndexOf(date));
                        foreach (string inputHistoryFile in inputHistoryFiles)
                        {
                            string aa = Path.GetFileNameWithoutExtension(inputHistoryFile);
                            if (aa.Contains(bhalf) && aa.Contains(ahalf) && aa.Contains(date) && !aa.Contains(Nostr))
                            {
                                //只返回一个体积和
                                double infilepixelSum;
                                //当前文件做裁切,体积计算,并相减
                                #region
                                string newclipfile = Path.Combine(clipsave, Path.GetFileName(infile).Replace("China", regionsname));
                                if (!File.Exists(newclipfile))
                                {
                                    string clipfile = muticlip.MutiRegionsClip(infile, aoiContainer, clipsave);
                                    string hdrfile  = Path.GetDirectoryName(clipfile) + "\\" + Path.GetFileNameWithoutExtension(clipfile) + ".hdr";
                                    //重命名
                                    string   newhdrfile = Path.Combine(Path.GetDirectoryName(clipfile), Path.GetFileNameWithoutExtension(infile).Replace("China", regionsname) + ".hdr");
                                    FileInfo fi         = new FileInfo(clipfile);
                                    fi.MoveTo(newclipfile);
                                    FileInfo fihdr = new FileInfo(hdrfile);
                                    fihdr.MoveTo(newhdrfile);
                                }
                                if (prdType == "SWE")
                                {
                                    string sweVolFile = svolPath + "\\" + Path.GetFileName(newclipfile).Replace("MSWE", "SVOL");
                                    if (!File.Exists(sweVolFile))
                                    {
                                        IFileExtractResult sweVolResult = ComputeSnowSWEVOL(newclipfile, sweVolFile);
                                        sweVolFile = sweVolResult.FileName;
                                    }
                                    using (IRasterDataProvider inRaster = RasterDataDriver.Open(sweVolFile) as IRasterDataProvider)
                                    {
                                        infilepixelSum = CompuCurPixel(sweVolFile);
                                    }
                                }
                                else
                                {
                                    using (IRasterDataProvider inRaster = RasterDataDriver.Open(newclipfile) as IRasterDataProvider)
                                    {
                                        infilepixelSum = CompuCurPixel(newclipfile);
                                    }
                                }
                                #endregion
                                //历史文件做裁切,体积计算,并相减
                                #region
                                //只返回一个体积和
                                double hisfilepixelSum;
                                string newhisclipfile = Path.Combine(clipsave, Path.GetFileName(inputHistoryFile).Replace("China", regionsname));
                                if (!File.Exists(newhisclipfile))
                                {
                                    string clipfile = muticlip.MutiRegionsClip(inputHistoryFile, aoiContainer, clipsave);
                                    string hdrfile  = Path.GetDirectoryName(clipfile) + "\\" + Path.GetFileNameWithoutExtension(clipfile) + ".hdr";
                                    //重命名
                                    string   newhdrfile = Path.Combine(Path.GetDirectoryName(clipfile), Path.GetFileNameWithoutExtension(inputHistoryFile).Replace("China", regionsname) + ".hdr");
                                    FileInfo fi         = new FileInfo(clipfile);
                                    fi.MoveTo(newhisclipfile);
                                    FileInfo fihdr = new FileInfo(hdrfile);
                                    fihdr.MoveTo(newhdrfile);
                                }
                                if (prdType == "SWE")
                                {
                                    string hissweVolFile = svolPath + "\\" + Path.GetFileName(newhisclipfile).Replace("MSWE", "SVOL");
                                    if (!File.Exists(hissweVolFile))
                                    {
                                        IFileExtractResult sweVolResult = ComputeSnowSWEVOL(newhisclipfile, hissweVolFile);
                                        hissweVolFile = sweVolResult.FileName;
                                    }
                                    using (IRasterDataProvider inRaster = RasterDataDriver.Open(hissweVolFile) as IRasterDataProvider)
                                    {
                                        hisfilepixelSum = CompuCurPixel(hissweVolFile);
                                    }
                                }
                                else
                                {
                                    using (IRasterDataProvider inRaster = RasterDataDriver.Open(newhisclipfile) as IRasterDataProvider)
                                    {
                                        hisfilepixelSum = CompuCurPixel(newhisclipfile);
                                    }
                                }
                                #endregion
                                //提取年至avg之间的字符
                                string   filetime = cfile.Substring(cfile.IndexOf(date), cfile.Length - 3 - cfile.IndexOf(date));
                                string[] mxchars  = filetime.Split(new char[] { '_' });
                                string   mx       = "";//月+旬
                                if (cfile.Contains("Xun"))
                                {
                                    title = regionsname + date + "年冬季雪水当量旬距平" + "(" + paras[2] + "年)";
                                    if (mxchars[2] == "1")
                                    {
                                        mx = mxchars[1] + "月" + "上旬";
                                    }
                                    if (mxchars[2] == "2")
                                    {
                                        mx = mxchars[1] + "月" + "中旬";
                                    }
                                    if (mxchars[2] == "3")
                                    {
                                        mx = mxchars[1] + "月" + "下旬";
                                    }
                                }
                                else if (cfile.Contains("Month"))
                                {
                                    title = regionsname + date + "年冬季雪水当量月距平" + "(" + paras[2] + "年)";
                                    mx    = mxchars[1] + "月";
                                }
                                else
                                {
                                    mx = filetime;
                                }
                                resultList.Add(new string[] { mx, ((infilepixelSum - hisfilepixelSum) / hisfilepixelSum * 100).ToString() + "%" });
                                rauto.WriteLine(mx + " " + ((infilepixelSum - hisfilepixelSum) / hisfilepixelSum).ToString());
                            }
                        }
                    }
                    #endregion
                }
            }
            rauto.Close();
            fauto.Close();
            string   coluString = regionsname;
            string   sentitle   = "统计日期:" + DateTime.Now.ToShortDateString();
            string[] columns    = new string[] { coluString, "距平" };
            fresult = new StatResult(sentitle, columns, resultList.ToArray());
            string   outputIdentify = regionsname;
            string   fileexcel      = StatResultToFile(new string[] { inputCurrentFiles[0] }, fresult, "MWS", outputIdentify, title, null, 1, true, 1);
            string   newexcelfile   = Path.Combine(svolPath, title + ".XLSX");//inputCurrentFiles[0].Substring(0,9) + "_"+ regionsname + paras[2]+ ".XLSX");
            FileInfo fi1            = new FileInfo(fileexcel);
            fi1.MoveTo(newexcelfile);
            IFileExtractResult res = new FileExtractResult(_subProductDef.Identify, newexcelfile, false);
            return(new FileExtractResult(outputIdentify, newexcelfile));

            #endregion
        }
Ejemplo n.º 2
0
        private void DoRetrievalAndComputeMonthly(StringBuilder sql)
        {
            StringBuilder monthsql           = null;
            string        outAvgRetrievFname = null;
            string        outputdir          = Path.Combine(db.Output, "MWS\\纵向同期_横向连续统计\\");

            if (!Directory.Exists(outputdir))
            {
                Directory.CreateDirectory(outputdir);
            }
            if (_retrievAvgFiles != null)
            {
                _retrievAvgFiles.Clear();
            }
            for (int fm = _startMonth; fm <= _endMonth; fm++)
            {
                try
                {
                    monthsql = new StringBuilder(sql.ToString());
                    monthsql.Append("ExtractMonth=").Append(Convert.ToInt32(fm));
                    if (_retrievFiles != null)
                    {
                        _retrievFiles.Clear();
                    }
                    else
                    {
                        _retrievFiles = new List <string>();
                    }
                    db.Retrieval(monthsql.ToString(), SaveRetrieveHistory);
                    if (_retrievFiles == null || _retrievFiles.Count == 0)
                    {
                        monthsql = GenerateSQL(_sensor[1]);
                        monthsql.Append("ExtractMonth=").Append(Convert.ToInt32(fm));
                        if (_retrievFiles != null)
                        {
                            _retrievFiles.Clear();
                        }
                        else
                        {
                            _retrievFiles = new List <string>();
                        }
                        db.Retrieval(monthsql.ToString(), SaveRetrieveHistory);
                    }
                    //历史同期平均
                    if (_retrievFiles != null && _retrievFiles.Count > 0)
                    {
                        outAvgRetrievFname = Path.Combine(outputdir, CreateSamePeriodAvgOutName(_retrievFiles, "Month"));
                        _snwParaStat.SNWParaAvgStat(_retrievFiles.ToArray(), 0.1f, outAvgRetrievFname);
                    }
                    else
                    {
                        continue;
                    }
                    if (_retrievAvgFiles == null)
                    {
                        _retrievAvgFiles = new Dictionary <string, List <string> >();
                        if (File.Exists(outAvgRetrievFname))
                        {
                            _retrievAvgFiles.Add(outAvgRetrievFname, new List <string>(_retrievFiles.ToArray()));
                        }
                    }
                    else if (!_retrievAvgFiles.ContainsKey(outAvgRetrievFname))
                    {
                        if (File.Exists(outAvgRetrievFname))
                        {
                            _retrievAvgFiles.Add(outAvgRetrievFname, new List <string>(_retrievFiles.ToArray()));
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Ejemplo n.º 3
0
        private IExtractResult JuPingAnalysisAlgorithm(Action <int, string> progressTracker)
        {
            string[]               inputCurrentFiles = null;                             //= ExportManager.GetInstance().List.ToArray();//GetStringArray("RasterCurrentFile"); 由查询那里获得
            string[]               inputHistoryFiles = null;
            List <string>          list1             = ExportManager.GetInstance().List; //得到的是从数据库里查询出来的周期数据
            StatisticResultManager manager           = StatisticResultManager.GetInstance();
            List <StatisticResult> list  = manager.List;
            List <string>          list2 = manager.GetFilePathFromList(); //得到的是基于数据查询的数据又做的统计数据

            regionNames = _argumentProvider.GetArg("regionNames") as string;
            //处理后的数据,用它来出专题图
            List <string> afterProcessfiles = new List <string>();

            #region 获得目标区域
            aoiContainer = new GeoDo.RSS.Core.VectorDrawing.AOIContainerLayer();
            string        fieldName;
            string        shapeFilename;
            int           fieldIndex  = -1;
            List <string> fieldValues = new List <string>();
            string        regionsname = "";
            using (frmStatSubRegionTemplates frm = new frmStatSubRegionTemplates())
            {
                frm.listView1.MultiSelect = true;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    Feature[] fets = frm.GetSelectedFeatures();
                    fets = frm.GetStatFeatures(out fieldName, out shapeFilename, out fieldIndex);
                    if (fets == null)
                    {
                        aoiContainer = null;
                        regionsname  = "全国";
                    }
                    else
                    {
                        string chinafieldValue = fets[0].GetFieldValue(fieldIndex);
                        if (chinafieldValue == "中国")
                        {
                            aoiContainer = null;
                            regionsname  = "全国";
                        }
                        else
                        {
                            foreach (Feature fet in fets)
                            {
                                fieldValues.Add(fet.GetFieldValue(fieldIndex)); //获得选择区域名称
                                aoiContainer.AddAOI(fet);
                            }
                            foreach (string region in fieldValues)
                            {
                                regionsname += region;
                            }
                            if (regionsname.Contains("西藏") && regionsname.Contains("青海"))
                            {
                                regionsname = "青藏地区";
                            }
                            if (!string.IsNullOrEmpty(regionNames))
                            {
                                regionsname = regionNames.Trim();
                            }
                        }
                    }
                }
                else
                {
                    return(null);
                }
            }
            #endregion
            Regex DataReg = new Regex(@"(?<year>\d{4})", RegexOptions.Compiled);
            //处理后的数据存储路径
            string savePath   = _argumentProvider.GetArg("HistoryDataSave") as string;
            string jpsavepath = savePath + "\\" + "距平分析";
            if (!System.IO.Directory.Exists(jpsavepath))//如果不存在这个路径
            {
                System.IO.Directory.CreateDirectory(jpsavepath);
            }
            //参数设置
            List <string>       paraset    = _argumentProvider.GetArg("paraSet") as List <string>;
            string[]            paras      = paraset.ToArray();
            List <string>       jpfiles    = new List <string>();//存放相减的距平分析文件
            string              date       = "";
            string              jupingfile = "";
            IExtractResultArray array      = new ExtractResultArray("距平分析");
            string              Nostr      = ""; //排除非选择的轨道数据
            if (paras[0] == "Ascend")
            {
                Nostr = "_D_";
            }
            if (paras[0] == "Descend")
            {
                Nostr = "_A_";
            }
            if (paras[1] == "winter")   // inputCurrentFiles = manager.GetFilePathFromList() 冬季的数所是统计出来的。
            {                           // inputHistoryFiles  需要再重新计算一次  *** 这是多对一计算距平
                //比如说要算2000-2013年冬季的距平,需要先分别算出每年冬季的值,用这些值算总的均值。再分别用每年冬季减去总冬季值。
                List <string> files = new List <string>();
                foreach (string file in list2)
                {
                    if (!Path.GetFileName(file).Contains(Nostr))
                    {
                        files.Add(file);
                    }
                }
                inputCurrentFiles = files.ToArray();
                string      outHistoryFile = jpsavepath + "\\" + Path.GetFileNameWithoutExtension(inputCurrentFiles[0]).Substring(0, 14) + "_" + paras[2] + ".dat";
                SNWParaStat snwStat        = new SNWParaStat();
                snwStat.SNWParaAvgStat(inputCurrentFiles, 0.1f, outHistoryFile);
                List <string> temphistory = new List <string>();
                temphistory.Add(outHistoryFile);
                inputHistoryFiles = temphistory.ToArray();
                if (inputCurrentFiles == null || inputCurrentFiles.Length < 0 || inputHistoryFiles == null || inputHistoryFiles.Length < 0)
                {
                    PrintInfo("缺少分析文件");
                    return(null);
                }
                foreach (string inputCurrentFile in inputCurrentFiles)
                {
                    Regex  DataReg2 = new Regex(@"(?<year>\d{4})_(?<month>\d{2})_(?<year>\d{4})_(?<month>\d{1})", RegexOptions.Compiled);
                    Match  m        = DataReg2.Match(inputCurrentFile); //提取每年冬季的时间
                    string year     = "";
                    if (m.Success)
                    {
                        year = m.Value;
                    }
                    jupingfile = JuPinComputer(inputCurrentFile, inputHistoryFiles[0], jpsavepath);
                    string   hdrfile    = Path.GetDirectoryName(jupingfile) + "\\" + Path.GetFileNameWithoutExtension(jupingfile) + ".hdr";
                    FileInfo fi         = new FileInfo(jupingfile);
                    string   newjpfile  = Path.Combine(jpsavepath, Path.GetFileNameWithoutExtension(jupingfile) + "_" + year.Substring(0, 4) + ".dat");
                    string   newhdrfile = Path.Combine(jpsavepath, Path.GetFileNameWithoutExtension(jupingfile) + "_" + year.Substring(0, 4) + ".hdr");
                    fi.MoveTo(newjpfile);
                    FileInfo fihdr = new FileInfo(hdrfile);
                    fihdr.MoveTo(newhdrfile);
                    jpfiles.Add(newjpfile);
                }
            }
            else
            {                                        //例 2013年11月上旬-2013年11月下旬分别与1987-2013年11月上中下旬 同期比较   一对一计算距平
                inputCurrentFiles = list1.ToArray();
                inputHistoryFiles = list2.ToArray();
                if (inputCurrentFiles == null || inputCurrentFiles.Length < 0 || inputHistoryFiles == null || inputHistoryFiles.Length < 0)
                {
                    PrintInfo("缺少分析文件");
                    return(null);
                }
                if ((inputCurrentFiles.Length) * 2 != inputHistoryFiles.Length)
                {
                    MessageBox.Show("当年数据与同期数据不对应");
                    return(null);
                }
                foreach (string inputCurrentFile in inputCurrentFiles)
                {
                    //先假设选择的是旬数据 MWS_MWSD_China_Xun_0SD_A_2011_1_avg.dat
                    //       则同期旬数据 MWS_MWSD_China_Xun_0SD_A_1989_2011_1_avg.dat
                    string cfile = Path.GetFileNameWithoutExtension(inputCurrentFile);
                    Match  m     = DataReg.Match(cfile);
                    if (m.Success)
                    {
                        date = m.Value;    //根据年4个数字来拆分字符串
                    }
                    string bhalf = cfile.Substring(0, cfile.IndexOf(date));
                    string ahalf = cfile.Substring(cfile.IndexOf(date) + 4, cfile.Length - 4 - cfile.IndexOf(date));
                    foreach (string inputHistoryFile in inputHistoryFiles)
                    {
                        string aa = Path.GetFileNameWithoutExtension(inputHistoryFile);
                        if (aa.Contains(bhalf) && aa.Contains(ahalf) && aa.Contains(date) && !aa.Contains(Nostr))
                        {
                            jupingfile = JuPinComputer(inputCurrentFile, inputHistoryFile, jpsavepath);
                            jpfiles.Add(jupingfile);
                            break;
                        }
                    }
                }
            }
            #region  距平的文件在jpfiles 中,下面是裁切和平滑处理
            MulRegionsClip muticlip = new MulRegionsClip(); //裁切
            MWSSmoothHelp  smooth   = new MWSSmoothHelp();  //平滑
            //创建专题图路径
            gxdsave = jpsavepath + "\\" + regionsname + "\\" + "专题图";
            if (!System.IO.Directory.Exists(gxdsave))//如果不存在这个路径
            {
                System.IO.Directory.CreateDirectory(gxdsave);
            }
            //创建平滑路径
            string smoothsave = jpsavepath + "\\" + regionsname + "\\" + "平滑";
            if (!System.IO.Directory.Exists(smoothsave))
            {
                System.IO.Directory.CreateDirectory(smoothsave);
            }
            //创建平滑\\中值
            string filtersave = smoothsave + "\\" + "中值";
            if (!System.IO.Directory.Exists(filtersave))
            {
                System.IO.Directory.CreateDirectory(filtersave);
            }
            if (aoiContainer == null)
            {
                //只做中值处理
                foreach (string infile in jpfiles)
                {
                    // 先判断这个文件是否存在,存在了就不做
                    string filterfile = filtersave + "\\" + Path.GetFileName(infile);
                    if (!File.Exists(filterfile))
                    {
                        filterfile = smooth.ComputerMid(infile, 5, filtersave);
                    }
                    afterProcessfiles.Add(filterfile);
                    toBarfiles.Add(infile);
                }
            }
            else
            {
                // 裁切 // 中值 // 插值
                string clipsave = jpsavepath + "\\" + regionsname + "\\" + "裁切";
                if (!System.IO.Directory.Exists(clipsave))//如果不存在这个路径
                {
                    System.IO.Directory.CreateDirectory(clipsave);
                }
                string bilisave = smoothsave + "\\" + "插值";
                if (!System.IO.Directory.Exists(bilisave))
                {
                    System.IO.Directory.CreateDirectory(bilisave);
                }
                foreach (string infile in jpfiles)
                {
                    string newclipfile = clipsave + "\\" + Path.GetFileName(infile).Replace("China", regionsname);
                    if (!File.Exists(newclipfile))
                    {
                        string clipfile = muticlip.MutiRegionsClip(infile, aoiContainer, clipsave);
                        string hdrfile  = Path.GetDirectoryName(clipfile) + "\\" + Path.GetFileNameWithoutExtension(clipfile) + ".hdr";
                        //重命名
                        string   newhdrfile = Path.Combine(Path.GetDirectoryName(clipfile), Path.GetFileNameWithoutExtension(infile).Replace("China", regionsname) + ".hdr");
                        FileInfo fi         = new FileInfo(clipfile);
                        fi.MoveTo(newclipfile);
                        FileInfo fihdr = new FileInfo(hdrfile);
                        fihdr.MoveTo(newhdrfile);
                    }
                    toBarfiles.Add(newclipfile); //加上裁切后的文件
                    //中值、插值
                    string filterfile = smooth.ComputerMid(newclipfile, 5, filtersave);
                    string bilifile   = smooth.Bilinear(filterfile, 10, bilisave);
                    afterProcessfiles.Add(bilifile);
                }
            }
            #endregion
            //出专题图
            #region
            IExtractResultArray results = new ExtractResultArray("");
            foreach (string arguments in afterProcessfiles.ToArray())
            {
                string product = "";
                if (arguments.Contains("MWSD"))
                {
                    product = "雪深";
                    _argumentProvider.SetArg("OutFileIdentify", "JPDI");
                }
                if (arguments.Contains("MSWE"))
                {
                    product = "雪水当量";
                    _argumentProvider.SetArg("OutFileIdentify", "JPEI");
                }
                //解析文件名确定专题图名称
                string filename = Path.GetFileNameWithoutExtension(arguments);
                if (paras[1] == "usual")
                {
                    Regex  DataReg1 = new Regex(@"(?<year>\d{4})_(?<year>\d{4})", RegexOptions.Compiled);
                    Match  m        = DataReg1.Match(filename);
                    string year     = "";
                    if (m.Success)
                    {
                        year = m.Value;
                    }
                    //提取年至avg之间的字符
                    string   filetime = filename.Substring(filename.IndexOf(year), filename.Length - 3 - filename.IndexOf(year));
                    string[] mxchars  = filetime.Split(new char[] { '_' });
                    string   mx       = "";//月+旬
                    if (filename.Contains("Xun"))
                    {
                        if (mxchars[3] == "1")
                        {
                            mx = mxchars[2] + "月" + "上旬";
                        }
                        if (mxchars[3] == "2")
                        {
                            mx = mxchars[2] + "月" + "中旬";
                        }
                        if (mxchars[3] == "3")
                        {
                            mx = mxchars[2] + "月" + "下旬";
                        }
                    }
                    if (filename.Contains("Month"))
                    {
                        mx = mxchars[2] + "月";
                    }
                    imgname = year.Substring(5, 4) + "年" + mx + regionsname + product + "距平分布图(" + paras[2] + "年)";
                }
                if (paras[1] == "winter")
                {
                    imgname = Path.GetFileNameWithoutExtension(arguments).Substring(Path.GetFileNameWithoutExtension(arguments).Length - 4, 4) + "年" + "冬季" + regionsname + product + "距平分布图(" + paras[2] + "年)";
                }
                _argumentProvider.SetArg("SelectedPrimaryFiles", arguments);
                _argumentProvider.SetArg("fileOpenArgs", arguments);
                FileExtractResult result = ThemeGraphyResult(null) as FileExtractResult;
                //增加矢量
                string autopath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"SystemData\ProductArgs\MWS\shppath.txt");
                string shpFile  = "";
                if (File.Exists(autopath))
                {
                    FileStream   fauto = new FileStream(autopath, FileMode.Open, FileAccess.Read);
                    StreamReader rauto = new StreamReader(fauto, Encoding.GetEncoding("gb2312"));
                    shpFile = rauto.ReadLine();
                    rauto.Close();
                    fauto.Close();
                }
                else
                {
                    shpFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"数据引用\基础矢量\行政区划\线\中国边界_线.shp");
                }
                CreateMcd(shpFile);
                _shpFile = shpFile;
                _gxdFile = (result as FileExtractResult).FileName;
                AddShpToGxd();
                string   newresultfile = Path.Combine(gxdsave, Path.GetFileNameWithoutExtension(arguments) + ".gxd");
                FileInfo fi            = new FileInfo(result.FileName);
                if (!File.Exists(newresultfile))
                {
                    fi.MoveTo(newresultfile);
                }
                else
                {
                    FileInfo fi1 = new FileInfo(newresultfile);
                    fi1.Delete();
                }

                IFileExtractResult res = new FileExtractResult(_subProductDef.Identify, newresultfile, false);
                results.Add(res);
            }
            #endregion
            return(results);
        }