Пример #1
0
        public virtual DataTable ToDataTable(ListTimeSeries <double> sourcedata)
        {
            DataTable  dt       = new DataTable();
            DataColumn date_col = new DataColumn("Date", Type.GetType("System.DateTime"));

            dt.Columns.Add(date_col);
            DataColumn value_col = new DataColumn(this.Name, Type.GetType("System.Double"));

            dt.Columns.Add(value_col);
            double[] buf = null;
            if (Derivable)
            {
                if (this.DerivedValues == null)
                {
                    this.DerivedValues = this.Derive(sourcedata);
                }
                buf = this.DerivedValues;
            }
            else
            {
                buf = sourcedata.Values[this.VariableIndex].ToArray();
            }
            for (int i = 0; i < buf.Length; i++)
            {
                var dr = dt.NewRow();
                dr[0] = sourcedata.Dates[i];
                dr[1] = buf[i];
                dt.Rows.Add(dr);
            }
            return(dt);
        }
Пример #2
0
        public override void Update()
        {
            if (State == RunningState.Stopped)
            {
                return;
            }
            var line = _StreamReader.ReadLine();

            if (TypeConverterEx.IsNull(line))
            {
                return;
            }
            if (line.Contains("Date"))
            {
                var ss = TypeConverterEx.Split <string>(line, TypeConverterEx.Comma);
                _DataSource = new ListTimeSeries <double>(ss.Length - 1);
                return;
            }
            var strs = TypeConverterEx.SkipSplit <string>(line);
            var buf  = TypeConverterEx.SkipSplit <double>(line, 1);

            _WatchObject.Current = DateTime.Parse(strs[0]);
            _WatchObject.Values  = buf;
            _DataSource.Add(_WatchObject.Current, buf);
            OnUpdated(this, _WatchObject);
        }
Пример #3
0
        public override void Load(string filename)
        {
            if (this.State == RunningState.Busy)
            {
                return;
            }
            if (File.Exists(filename))
            {
                var    fs   = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                var    sr   = new StreamReader(fs, Encoding.Default);
                string line = sr.ReadLine();
                int    nvar = TypeConverterEx.Split <string>(line, TypeConverterEx.Comma).Length - 1;
                _DataSource = new ListTimeSeries <double>(nvar);

                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine();
                    if (!TypeConverterEx.IsNull(line))
                    {
                        var strs = TypeConverterEx.Split <string>(line);
                        var buf  = TypeConverterEx.SkipSplit <double>(line, 1);
                        _DataSource.Add(DateTime.Parse(strs[0]), buf);
                    }
                }
                fs.Close();
                sr.Close();
            }
        }
Пример #4
0
 public virtual double [] Derive(ListTimeSeries <double> source)
 {
     if (DerivedIndex != null)
     {
         double[] values = new double[source.Dates.Count];
         for (int i = 0; i < source.Dates.Count; i++)
         {
             foreach (var index in DerivedIndex)
             {
                 values[i] += source.Values[index][i];
             }
         }
         return(values);
     }
     else
     {
         return(null);
     }
 }
Пример #5
0
        public override double[] Derive(ListTimeSeries <double> sourcedata)
        {
            if (Source != null)
            {
                if (Source.Derivable && Source.DerivedValues == null)
                {
                    Source.DerivedValues = Source.Derive(sourcedata);
                }
                double[] values = new double[sourcedata.Dates.Count];

                for (int i = 1; i < sourcedata.Dates.Count; i++)
                {
                    values[i] = Source.DerivedValues[i] - Source.DerivedValues[i - 1];
                }
                return(values);
            }
            else
            {
                return(null);
            }
        }
Пример #6
0
        public override double[] Derive(ListTimeSeries <double> sourcedata)
        {
            int c = 0;

            if (Source.Count > 0)
            {
                foreach (var item in Source)
                {
                    if (item.Derivable && item.DerivedValues == null)
                    {
                        item.DerivedValues = item.Derive(sourcedata);
                    }
                }

                double[] values = new double[sourcedata.Dates.Count];

                for (int i = 0; i < sourcedata.Dates.Count; i++)
                {
                    c = 0;
                    foreach (var item in Source)
                    {
                        if (item.Derivable)
                        {
                            values[i] += item.DerivedValues[i] * SourceSign[c];
                        }
                        else
                        {
                            values[i] += sourcedata.Values[item.VariableIndex][i] * SourceSign[c];
                        }
                        c++;
                    }
                }
                return(values);
            }
            else
            {
                return(null);
            }
        }
        public override System.Data.DataTable ToDataTable(ListTimeSeries <double> sourcedata)
        {
            int        c        = 0;
            DataTable  dt       = new DataTable();
            DataColumn date_col = new DataColumn("Date", Type.GetType("System.DateTime"));

            dt.Columns.Add(date_col);
            foreach (var item in Children)
            {
                DataColumn value_col = new DataColumn(item.Name, Type.GetType("System.Double"));
                dt.Columns.Add(value_col);
                if (item.Derivable && item.DerivedValues == null)
                {
                    item.DerivedValues = item.Derive(sourcedata);
                }
            }

            for (int i = 0; i < sourcedata.Dates.Count; i++)
            {
                var dr = dt.NewRow();
                dr[0] = sourcedata.Dates[i];
                c     = 1;
                foreach (var item in Children)
                {
                    if (item.Derivable)
                    {
                        dr[c] = item.DerivedValues[i];
                    }
                    else
                    {
                        dr[c] = sourcedata.Values[item.VariableIndex][i];
                    }
                    c++;
                }

                dt.Rows.Add(dr);
            }
            return(dt);
        }
Пример #8
0
        public override void Load(string filename)
        {
            if (this.State == RunningState.Busy)
            {
                return;
            }

            _cache_file = filename + ".csv";
            if (File.Exists(_cache_file) && File.Exists(filename))
            {
                InitMonitor(filename);
                _DataSource = new ListTimeSeries <double>(_MFMonitor.Root[0].Children.Count);
                var fs = new FileStream(_cache_file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                var sr = new StreamReader(fs, Encoding.Default);

                string line = sr.ReadLine();
                int    t    = 0;
                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine();
                    if (!string.IsNullOrEmpty(line))
                    {
                        var buf  = TypeConverterEx.SkipSplit <double>(line, 1);
                        var date = ModelService.Start.AddDays(t);
                        _DataSource.Add(date, buf);
                        t++;
                    }
                }
                fs.Close();
                sr.Close();
            }
            else
            {
                if (File.Exists(filename))
                {
                    InitMonitor(filename);
                    int total_var = _MFMonitor.Root[0].Children.Count;
                    _DataSource = new ListTimeSeries <double>(total_var);

                    var    fs   = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    var    sr   = new StreamReader(fs, Encoding.Default);
                    string line = "";
                    int    t    = 0;
                    int    nvar = (total_var - 4) / 2;
                    while (!sr.EndOfStream)
                    {
                        line = sr.ReadLine();
                        if (!string.IsNullOrEmpty(line))
                        {
                            if (line.Contains("VOLUMETRIC BUDGET FOR ENTIRE MODEL AT END OF TIME STEP"))
                            {
                                break;
                            }
                        }
                    }
                    while (!sr.EndOfStream)
                    {
                        line = sr.ReadLine();
                        if (!string.IsNullOrEmpty(line))
                        {
                            if (line.Contains("VOLUMETRIC BUDGET FOR ENTIRE MODEL AT END OF TIME STEP"))
                            {
                                var    vector = new double[total_var];
                                double total_in = 0, total_out = 0, ds = 0, error = 0;
                                for (int i = 0; i < 7; i++)
                                {
                                    sr.ReadLine();
                                }

                                for (int i = 0; i < nvar; i++)
                                {
                                    line = sr.ReadLine();
                                    var buf = TypeConverterEx.Split <string>(line);
                                    if (buf.Length == 6)
                                    {
                                        vector[i] = double.Parse(buf[5]);
                                    }
                                    else if (buf.Length == 8)
                                    {
                                        vector[i] = double.Parse(buf[7]);
                                    }
                                    total_in += vector[i];
                                }
                                for (int i = 0; i < 5; i++)
                                {
                                    sr.ReadLine();
                                }
                                for (int i = nvar; i < nvar * 2; i++)
                                {
                                    line = sr.ReadLine();
                                    var buf = TypeConverterEx.Split <string>(line);
                                    if (buf.Length == 6)
                                    {
                                        vector[i] = double.Parse(buf[5]);
                                    }
                                    else if (buf.Length == 8)
                                    {
                                        vector[i] = double.Parse(buf[7]);
                                    }
                                    total_out += vector[i];
                                }

                                ds    = vector[2 * nvar] - vector[0];
                                error = total_in - total_out;

                                vector[2 * nvar]     = total_in;
                                vector[2 * nvar + 1] = total_out;
                                vector[2 * nvar + 2] = ds;
                                vector[2 * nvar + 3] = error;

                                if (t > 0)
                                {
                                    var date = ModelService.Start.AddDays(t - 1);
                                    _DataSource.Add(date, vector);
                                }
                                t++;
                            }
                        }
                    }
                    fs.Close();
                    sr.Close();

                    var          csv_file = filename + ".csv";
                    StreamWriter sw       = new StreamWriter(csv_file);
                    var          head     = from item in _MFMonitor.Root[0].Children select item.Name;
                    line = "Date" + "," + string.Join(",", head);
                    sw.WriteLine(line);
                    var row = new double[total_var];
                    for (int i = 0; i < _DataSource.Dates.Count; i++)
                    {
                        for (int n = 0; n < total_var; n++)
                        {
                            row[n] = _DataSource.Values[n][i];
                        }
                        line = _DataSource.Dates[i].ToString("yyyy-MM-dd") + "," + string.Join(",", row);
                        sw.WriteLine(line);
                    }
                    sw.Close();
                }
            }
        }