Пример #1
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            StreamReader sr   = new StreamReader(DataFileName);
            var          line = sr.ReadLine();

            line = sr.ReadLine();
            var buf       = TypeConverterEx.Split <string>(line);
            int ncell     = int.Parse(buf[1]);
            int nvar      = int.Parse(buf[3]);
            var variables = new string[nvar + 2];

            variables[0] = "x";
            variables[1] = "y";
            for (int i = 0; i < nvar; i++)
            {
                variables[i + 2] = buf[4 + i];
            }
            var mat_out = new DataCube <float>(nvar + 2, 1, ncell);

            mat_out.Name      = OutputDataCube;
            mat_out.Variables = variables;
            for (int i = 0; i < ncell; i++)
            {
                line = sr.ReadLine().Trim();
                var vec = TypeConverterEx.Split <float>(line);
                for (int j = 0; j < nvar + 2; j++)
                {
                    mat_out[j, 0, i] = vec[j];
                }
            }
            sr.Close();
            Workspace.Add(mat_out);
            cancelProgressHandler.Progress("Package_Tool", 100, "Finished");
            return(true);
        }
Пример #2
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            var vec = GetVector(Matrix);

            if (vec != null)
            {
                var    dou_vec  = MatrixOperation.ToDouble(vec);
                string vec_name = "CDF of " + GetName(Matrix);
                int    nlen     = dou_vec.Length;
                cancelProgressHandler.Progress("Package_Tool", 10, "Calculating...");
                Array.Sort <double>(dou_vec);
                var cdf       = MathNet.Numerics.Statistics.Statistics.EmpiricalCDFFunc(dou_vec);
                var cdf_value = new double[nlen];
                for (int i = 0; i < nlen; i++)
                {
                    cdf_value[i] = cdf(dou_vec[i]);
                }
                WorkspaceView.Plot <double>(dou_vec, cdf_value, vec_name, Models.UI.MySeriesChartType.FastLine);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #3
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            var vec      = GetVector(Matrix);
            var var_name = GetName(Matrix);

            if (vec != null)
            {
                if (Number <= 0)
                {
                    Number = 10;
                }
                if (Number >= 100)
                {
                    Number = 100;
                }
                var vec_name = "Histogram of " + GetName(Matrix);
                cancelProgressHandler.Progress("Package_Tool", 10, "Calculating...");
                var   dou_vec = MatrixOperation.ToDouble(vec);
                var   hist    = new Histogram(dou_vec, Number);
                int   nhist   = hist.BucketCount;
                int[] xx      = new int[nhist];
                int[] yy      = new int[nhist];
                for (int i = 0; i < nhist; i++)
                {
                    xx[i] = (int)((hist[i].LowerBound + hist[i].UpperBound) * 0.5);
                    yy[i] = (int)hist[i].Count;
                }
                WorkspaceView.Plot <int>(xx, yy, vec_name, Models.UI.MySeriesChartType.Column);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #4
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            var vec_src = GetVector(SourceDataCube);
            var mat_tar = Get3DMat(TargetDataCube);

            if (vec_src != null)
            {
                var dims = GetDims(TargetDataCube);
                mat_tar.ILArrays[int.Parse(dims[0])][dims[1], dims[2]] = vec_src;
                cancelProgressHandler.Progress("Package_Tool", 100, "Successful");
                return(true);
            }
            else
            {
                cancelProgressHandler.Progress("Package_Tool", 100, "Failed. The source or target matrix style is wrong.");
                return(false);
            }
        }
Пример #5
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            int    var_indexA = 0;
            var    matA       = Get3DMat(InputDataCube, ref var_indexA);
            double prg        = 0;
            int    count      = 1;

            if (matA != null)
            {
                int nstep = matA.Size[1];
                int ncell = matA.Size[2];

                var mat_out = new DataCube <float>(1, 1, ncell);
                mat_out.Name      = OutputDataCube;
                mat_out.Variables = new string[] { "Slope" };

                for (int c = 0; c < ncell; c++)
                {
                    var    vec = matA.GetVector(var_indexA, ":", c.ToString());
                    var    dou_vec = MatrixOperation.ToDouble(vec);
                    var    steps = new double[nstep];
                    double rs, slope, yint;
                    for (int t = 1; t < nstep; t++)
                    {
                        steps[t] = t + 1;
                    }
                    MyStatisticsMath.LinearRegression(steps, dou_vec, 0, nstep, out rs, out yint, out slope);
                    mat_out[0, 0, c] = (float)slope;
                    prg = (c + 1) * 100.0 / ncell;
                    if (prg > count)
                    {
                        cancelProgressHandler.Progress("Package_Tool", (int)prg, "Caculating Cell: " + (c + 1));
                        count++;
                    }
                }
                Workspace.Add(mat_out);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #6
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            int    var_indexA = 0;
            int    var_indexB = 0;
            var    matA       = Get3DMat(MatrixA, ref var_indexA);
            var    matB       = Get3DMat(MatrixB, ref var_indexB);
            double prg        = 0;

            if (matA != null && matB != null)
            {
                int nstep   = System.Math.Min(matA.Size[1], matB.Size[1]);
                int ncell   = matA.Size[2];
                var mat_out = new DataCube <float>(1, 1, ncell);
                mat_out.Name      = OutputMatrix;
                mat_out.Variables = new string[] { "CorrelationCoefficients" };
                for (int c = 0; c < ncell; c++)
                {
                    var vecA     = matA.GetVector(var_indexA, ":", c.ToString());
                    var dou_vecA = MatrixOperation.ToDouble(vecA);
                    var vecB     = matB.GetVector(var_indexB, ":", c.ToString());;
                    var dou_vecB = MatrixOperation.ToDouble(vecB);
                    var len      = System.Math.Min(vecA.Length, vecB.Length);
                    //   var cor = MyStatisticsMath.Correlation(dou_vecA, dou_vecB);
                    var cor = Heiflow.Core.Alglib.alglib.basestat.pearsoncorrelation(dou_vecA, dou_vecB, len);
                    if (double.IsNaN(cor) || double.IsInfinity(cor))
                    {
                        cor = 0;
                    }
                    mat_out[0, 0, c] = (float)cor;
                    prg = (c + 1) * 100.0 / ncell;
                    if (prg % 10 == 0)
                    {
                        cancelProgressHandler.Progress("Package_Tool", (int)prg, "Caculating Cell: " + (c + 1));
                    }
                }
                Workspace.Add(mat_out);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #7
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            int var_index = 0;
            var mat       = Get3DMat(InputDataCube, ref var_index);
            int prg       = 0;
            int count     = 1;

            if (mat != null)
            {
                int nstep   = mat.Size[1];
                int ncell   = mat.Size[2];
                var mat_out = new DataCube <float>(4, 1, ncell);
                mat_out.Name      = OutputDataCube;
                mat_out.Variables = new string[] { "Mean", "Variance", "Skewness", "kurtosis" };
                for (int c = 0; c < ncell; c++)
                {
                    double mean = 0, variance = 0, skewness = 0, kurtosis = 0;
                    var    vec     = mat.GetVector(var_index, ":", c.ToString());
                    var    dou_vec = MatrixOperation.ToDouble(vec);
                    Heiflow.Core.Alglib.alglib.basestat.samplemoments(dou_vec, vec.Length, ref mean, ref variance, ref skewness, ref kurtosis);
                    mat_out[0, 0, c] = (float)mean;
                    mat_out[1, 0, c] = (float)variance;
                    mat_out[2, 0, c] = (float)skewness;
                    mat_out[3, 0, c] = (float)kurtosis;
                    prg = (c + 1) * 100 / ncell;
                    if (prg > count)
                    {
                        cancelProgressHandler.Progress("Package_Tool", prg, "Caculating Cell: " + (c + 1));
                        count++;
                    }
                }
                Workspace.Add(mat_out);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #8
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            var vector = GetVector(Matrix);

            if (vector != null)
            {
                cancelProgressHandler.Progress("Package_Tool", 10, "Calculating...");
                var dt = ProjectService.Project.Model.Grid.FeatureSet.DataTable;
                if (vector.Length == dt.Rows.Count)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i][RegularGrid.ParaValueField] = vector[i];
                    }
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            DataCubeStreamReader ds = new DataCubeStreamReader(CloudCoverFileName);

            ds.LoadDataCube();
            var           cloudcover  = ds.DataCube;
            CSVFileStream locationcsv = new CSVFileStream(LocationFileName);
            var           locations   = locationcsv.Load <double>();
            StreamReader  sr_dates    = new StreamReader(DateTimeFileName);
            int           ndays       = cloudcover.Size[1];
            int           nlocation   = cloudcover.Size[2];
            var           dates       = new DateTime[ndays];
            int           progress    = 0;
            int           np          = 1;
            var           swmat       = new DataCube <float>(cloudcover.Size[0], cloudcover.Size[1], cloudcover.Size[2]);

            swmat.Name = OutputName;
            for (int i = 0; i < ndays; i++)
            {
                var line = sr_dates.ReadLine();
                dates[i] = DateTime.Parse(line);
            }
            sr_dates.Close();
            for (int i = 0; i < ndays; i++)
            {
                for (int j = 0; j < nlocation; j++)
                {
                    swmat[0, i, j] = (float)_ShortWaveRadiation.DailyIncomingRadiationIntensity(locations.GetValue(j, 1), locations.GetValue(j, 0), dates[i], cloudcover[0, i, j]);
                }
                progress = i * 100 / ndays;
                if (progress == 5 * np)
                {
                    cancelProgressHandler.Progress("Package_Tool", progress, "Processing step: " + progress + "%");
                    np++;
                }
            }
            Workspace.Add(swmat);
            return(true);
        }
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            int    var_index = 0;
            var    mat       = Get3DMat(Source, ref var_index);
            double prg       = 0;
            int    count     = 1;

            if (mat != null)
            {
                DataTable dt = new DataTable();

                DataColumn dc_time = new DataColumn("DateTime", Type.GetType("System.DateTime"));
                dt.Columns.Add(dc_time);
                DataColumn dc = new DataColumn("Mean", Type.GetType("System.Double"));
                dt.Columns.Add(dc);
                dc = new DataColumn("Variance", Type.GetType("System.Double"));
                dt.Columns.Add(dc);
                dc = new DataColumn("Skewness", Type.GetType("System.Double"));
                dt.Columns.Add(dc);
                dc = new DataColumn("Kurtosis", Type.GetType("System.Double"));
                dt.Columns.Add(dc);

                int        nstep = mat.Size[1];
                double     mean = 0, variance = 0, skewness = 0, kurtosis = 0;
                List <int> list_selected = new List <int>();
                bool       use_selected  = false;
                //   selected_index = null;

                if (BaseTimeStep >= 0 && BaseTimeStep < mat.Size[0])
                {
                    var vec = mat[var_index, BaseTimeStep.ToString(), ":"];
                    for (int i = 0; i < vec.Length; i++)
                    {
                        if (vec[i] != NoDataValue)
                        {
                            list_selected.Add(i);
                        }
                    }
                    use_selected = true;
                }
                for (int i = 0; i < nstep; i++)
                {
                    var      dr           = dt.NewRow();
                    var      vec          = mat[var_index, i.ToString(), ":"];
                    double[] vec_selected = null;
                    if (use_selected)
                    {
                        vec_selected = new double[list_selected.Count];
                        for (int j = 0; j < list_selected.Count; j++)
                        {
                            vec_selected[j] = vec[list_selected[j]];
                        }
                    }
                    else
                    {
                        var dou_vec = Array.ConvertAll <float, double>(vec, s => s);
                        vec_selected = dou_vec.Where(s => s != NoDataValue).ToArray();
                    }
                    Heiflow.Core.Alglib.alglib.basestat.samplemoments(vec_selected, vec_selected.Length, ref mean, ref variance, ref skewness, ref kurtosis);
                    dr[0] = DateTime.Now;
                    dr[1] = mean;
                    dr[2] = variance;
                    dr[3] = skewness;
                    dr[4] = kurtosis;
                    dt.Rows.Add(dr);
                    prg = (i + 1) * 100.0 / nstep;
                    if (prg > count)
                    {
                        cancelProgressHandler.Progress("Package_Tool", (int)prg, "Time Step:" + dr[0].ToString());
                        count++;
                    }
                }

                if (mat.DateTimes != null)
                {
                    for (int i = 0; i < nstep; i++)
                    {
                        dt.Rows[i][0] = mat.DateTimes[i];
                    }
                }
                else
                {
                    for (int i = 0; i < nstep; i++)
                    {
                        dt.Rows[i][0] = DateTime.Now.AddDays(i);
                    }
                }
                WorkspaceView.OutputTo(dt);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #11
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            var    var_index = 0;
            var    mat       = Get3DMat(Source, ref var_index);
            double prg       = 0;
            int    count     = 1;

            if (mat != null)
            {
                int nstep   = mat.Size[1];
                int ncell   = mat.Size[2];
                var vec     = mat[var_index, ":", "0"];
                var dou_vec = MatrixOperation.ToDouble(vec);

                var date_source = new DateTime[nstep];
                if (mat.DateTimes != null && mat.DateTimes.Length >= nstep)
                {
                    for (int i = 0; i < nstep; i++)
                    {
                        date_source[i] = mat.DateTimes[i];
                    }
                }
                else
                {
                    for (int i = 0; i < nstep; i++)
                    {
                        date_source[i] = ModelService.Start.AddDays(i);
                    }
                }
                var ts             = new DataCube <float>(vec, date_source);
                var derieved_ts    = TimeSeriesAnalyzer.Derieve(ts, NumericalDataType, TimeUnits);
                var derieved_steps = derieved_ts.DateTimes.Length;
                var mat_out        = new DataCube <float>(1, derieved_steps, ncell);

                mat_out.Name          = Derived;
                mat_out.Variables     = new string[] { "Derived" };
                mat_out.DateTimes     = derieved_ts.DateTimes.ToArray();
                mat_out.TimeBrowsable = true;
                for (int c = 0; c < ncell; c++)
                {
                    vec         = mat[var_index, ":", c.ToString()];
                    ts          = new DataCube <float>(vec, date_source);
                    derieved_ts = TimeSeriesAnalyzer.Derieve(ts, NumericalDataType, TimeUnits);
                    for (int t = 0; t < derieved_steps; t++)
                    {
                        mat_out[0, t, c] = derieved_ts[0, t, 0];
                    }
                    prg = (c + 1) * 100.0 / ncell;
                    if (prg > count)
                    {
                        cancelProgressHandler.Progress("Package_Tool", (int)prg, "Caculating Cell: " + (c + 1));
                        count++;
                    }
                }

                Workspace.Add(mat_out);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #12
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            var    var_index = 0;
            var    mat       = Get3DMat(Source, ref var_index);
            double prg       = 0;
            int    count     = 1;
            var    grid      = ProjectService.Project.Model.Grid as RegularGrid;

            if (mat != null && grid != null)
            {
                int nstep = mat.Size[1];
                int ncell = mat.Size[2];

                var mat_out = new DataCube <float>(1, nstep, ncell);
                mat_out.Name      = Output;
                mat_out.Variables = mat.Variables;
                mat_out.DateTimes = mat.DateTimes;
                mat.CopyTo(mat_out);
                List <float[]> neibor                = new List <float[]>();
                int            num_dep_modified      = 0;
                int            num_head_modified     = 0;
                int            num_no_neighbor_found = 0;
                for (int c = 0; c < ncell; c++)
                {
                    var vec = mat[var_index, ":", c.ToString()];
                    var buf = (from vv in vec select vv - grid.Elevations[0, 0, c]).ToArray();
                    var max = buf.Maximum();
                    if (max > MaxPositiveDepth)
                    {
                        var scale = max / MaxPositiveDepth;
                        for (int i = 0; i < nstep; i++)
                        {
                            if (buf[i] > 0)
                            {
                                buf[i] /= scale;
                                mat_out[var_index, i, c] = grid.Elevations[0, 0, c] + buf[i];
                            }
                        }
                        num_dep_modified++;
                    }
                }

                for (int c = 0; c < ncell; c++)
                {
                    var  vec    = mat_out[var_index, ":", c.ToString()];
                    var  buf    = (from vv in vec select vv - vec[0]).ToArray();
                    var  max    = buf.Maximum();
                    var  min    = buf.Minimum();
                    bool modify = false;
                    if (max > MaxPositiveDeviation)
                    {
                        var scale = max / MaxPositiveDeviation;
                        for (int i = 1; i < nstep; i++)
                        {
                            if (buf[i] > 0)
                            {
                                buf[i] /= scale;
                            }
                        }
                        modify = true;
                    }

                    if (min < MinPositiveDeviation)
                    {
                        var scale = min / MinPositiveDeviation;
                        for (int i = 1; i < nstep; i++)
                        {
                            if (buf[i] < 0)
                            {
                                buf[i] /= scale;
                            }
                        }
                        modify = true;
                    }

                    if (modify)
                    {
                        //var cell_id = grid.Topology.ActiveCellIDs[c];
                        int[] loc = grid.Topology.ActiveCell[c];
                        neibor.Clear();
                        for (int ii = loc[0] - NeighborCount; ii <= loc[0] + NeighborCount; ii++)
                        {
                            for (int jj = loc[1] - NeighborCount; jj <= loc[1] + NeighborCount; jj++)
                            {
                                if (ii >= 0 && ii < grid.RowCount && jj >= 0 && jj < grid.ColumnCount && grid.IBound[0, ii, jj] > 0)
                                {
                                    var cell_index = grid.Topology.GetSerialIndex(ii, jj);
                                    if (!RequireModify(mat_out, var_index, cell_index))
                                    {
                                        var neibor_vec = mat_out[var_index, ":", cell_index.ToString()];
                                        var buf_nei    = (from vv in neibor_vec select vv - neibor_vec[0]).ToArray();
                                        neibor.Add(buf_nei);
                                    }
                                }
                            }
                        }
                        if (neibor.Count > 0)
                        {
                            for (int i = 1; i < nstep; i++)
                            {
                                // float av_value = 0;
                                var sds          = (from vv in neibor select vv.StandardDeviation()).ToArray();
                                var min_sd       = sds.Min();
                                var min_sd_index = 0;
                                for (int n = 0; n < sds.Count(); n++)
                                {
                                    if (min_sd == sds[n])
                                    {
                                        min_sd_index = n;
                                        break;
                                    }
                                }
                                //foreach (var ss in neibor)
                                //{
                                //    av_value += ss[i];
                                //}
                                //av_value /= neibor.Count;
                                //mat_out.Value[var_index][i][c] = mat_out.Value[var_index][0][c] + av_value;
                                mat_out[var_index, i, c] = mat_out[var_index, 0, c] + neibor[min_sd_index][i];
                            }
                        }
                        else
                        {
                            for (int i = 1; i < nstep; i++)
                            {
                                vec[i] = vec[0] + buf[i] * 0.5f;
                                mat_out[var_index, i, c] = vec[i];
                            }
                            num_no_neighbor_found++;
                        }
                        num_head_modified++;
                    }

                    prg = (c + 1) * 100.0 / ncell;
                    if (prg > count)
                    {
                        cancelProgressHandler.Progress("Package_Tool", (int)prg, "Caculating Cell: " + (c + 1));
                        count++;
                    }
                }
                cancelProgressHandler.Progress("Package_Tool", 100, string.Format(" \num_dep_modified: {0};\n num_head_modified: {1};\n num_no_neighbor_found:{2}",
                                                                                  num_dep_modified, num_head_modified, num_no_neighbor_found));

                Workspace.Add(mat_out);
                return(true);
            }
            else
            {
                return(false);
            }
        }