Exemple #1
0
        //                 E2_P
        //1975           0.74815
        //1976           0.78258

        //                  S1S2
        //1975            .
        //1976           0.72799
        //1977           0.68516
        public static void tspDataUtility(string dataFile, Databank databank, ReadOpenMulbkHelper oRead, Program.ReadInfo readInfo, bool open)
        {
            // Used in READ<tsp>

            if (open)
            {
                G.Writeln2("*** ERROR: Cannot OPEN a tsp file, use READ.");
                throw new GekkoException();
            }

            int allCounter = 0;

            string databankName = databank.aliasName;

            if (!oRead.Merge)
            {
                databank.Clear();
            }

            int min = int.MaxValue;
            int max = int.MinValue;

            if (true)
            {
                List <string> al     = new List <string>(5000);
                List <string> alType = new List <string>(5000);
                List <string> al1    = new List <string>(5000); //contains output
                Program.tokensFromFileToArrayList(dataFile, true, false, al, alType);
                //double[] data = null;
                string varName = "";
                //int start = -12345;
                int year    = -12345;
                int counter = 0;
                //bool firstTime = true;
                bool       minus = false;
                TimeSeries ts    = null;
                for (int i = 0; i < al.Count - Globals.extra; i++)
                {
                    if ((string)alType[i] == "Symbol" && (string)al[i] == "-")
                    {
                        minus = true;
                    }
                    else if ((string)alType[i] == "Word")
                    {
                        //data = new double[4000];   //slack, fix
                        varName = (string)al[i];

                        //HMMM, maybe use much simpler way here... (and remove last parameter in method):
                        ts = Program.FindOrCreateTimeseries(databankName, varName, O.ECreatePossibilities.Can, false, true);
                        allCounter++;

                        //try
                        //{
                        //    start = int.Parse((string)al[i + 2]);
                        //}
                        //catch
                        //{
                        //    G.Writeln2("*** Could not parse '" + (string)al[i + 2] + "' as an integer");
                        //    throw new GekkoException();
                        //}

                        counter = 0;
                        //firstTime = false;
                        minus = false;
                    }
                    else if ((string)alType[i] == "Number" || (string)al[i] == ".")
                    {
                        if (counter % 2 == 0)
                        {
                            try
                            {
                                year = int.Parse((string)al[i]);
                                min  = G.GekkoMin(min, year);
                                max  = G.GekkoMax(max, year);
                            }
                            catch
                            {
                                G.Writeln2("*** Could not parse '" + (string)al[i] + "' as an integer");
                                throw new GekkoException();
                            }
                        }
                        else
                        {
                            if (al[i] == ".")
                            {
                                ts.SetData(new GekkoTime(EFreq.Annual, year, 1), double.NaN);
                                //data[end] = double.NaN;
                            }
                            else
                            {
                                string n1 = (string)al[i];
                                n1 = n1.Replace("d", "E");
                                n1 = n1.Replace("D", "E");

                                try
                                {
                                    double v = double.Parse(n1, System.Globalization.CultureInfo.InvariantCulture);
                                    if (minus)
                                    {
                                        v = -v;
                                    }
                                    ts.SetData(new GekkoTime(EFreq.Annual, year, 1), v);
                                }
                                catch
                                {
                                    G.Writeln2("*** Could not parse '" + n1 + "' as an floating point number");
                                    throw new GekkoException();
                                }
                            }
                        }
                        counter++;
                        minus = false;
                    }
                }

                G.Writeln2("Read " + allCounter + " times series from TSP output file.");
                //G.Writeln2("Note that this utility merges data with any existing data in the primary bank.");
                G.Writeln();
            }
            readInfo.startPerInFile = min;
            readInfo.endPerInFile   = max;
            readInfo.variables      = allCounter;

            if (oRead.Merge)  //See almost identical code in other reads (tsd, pcim, etc...)
            {
                readInfo.startPerResultingBank = G.GekkoMin(readInfo.startPerInFile, databank.yearStart);
                readInfo.endPerResultingBank   = G.GekkoMax(readInfo.endPerInFile, databank.yearEnd);
            }
            else
            {
                readInfo.startPerResultingBank = readInfo.startPerInFile;
                readInfo.endPerResultingBank   = readInfo.endPerInFile;
            }

            databank.Trim();  //should in principle do this after each time series read, but these files are typically small anyway.
        }
        public int offset    = 0; //offset, for instance a lag like x[-2], in that case index = -2.

        public MetaTimeSeries(TimeSeries ts)
        {
            this.ts     = ts;
            this.offset = 0;  //must always be 0 in simple constructor
        }
 private MetaTimeSeries(TimeSeries ts, int offset)
 {
     //ONLY use this in Index() function here
     this.ts     = ts;
     this.offset = offset;
 }
Exemple #4
0
 public void AddVariable(string frequency, TimeSeries ts, bool variableNameCheck)
 {
     AddVariable(false, frequency, ts, variableNameCheck);
 }
Exemple #5
0
 public void AddVariable(string frequency, TimeSeries ts)
 {
     AddVariable(false, frequency, ts, true);
 }
Exemple #6
0
 public void AddVariable(TimeSeries ts, bool variableNameCheck)
 {
     AddVariable(true, null, ts, variableNameCheck);
 }
Exemple #7
0
 public void AddVariable(TimeSeries ts)
 {
     AddVariable(true, null, ts, true);
 }
        public IVariable Indexer(GekkoTime t, bool isLhs, params IVariable[] indexes)
        {
            //Check if any indexer elements are LIST type, if not lists = null.
            List <int> lists = null;

            for (int i = 0; i < indexes.Length; i++)
            {
                IVariable iv = indexes[i];
                if (iv.Type() == EVariableType.List)
                {
                    if (lists == null)
                    {
                        lists = new List <int>();
                    }
                    lists.Add(i);
                }
            }

            if (lists != null)
            {
                List <string> temp = new List <string>();
                if (lists.Count == 1)
                {
                    List <string> m = ((MetaList)indexes[lists[0]]).list;
                    foreach (string s in m)
                    {
                        string ss = null;
                        for (int i = 0; i < indexes.Length; i++)
                        {
                            if (i == lists[0])
                            {
                                ss += Globals.symbolTurtle + s;
                            }
                            else
                            {
                                ss += Globals.symbolTurtle + O.GetString(indexes[i]);
                            }
                        }
                        ss = this.ts.variableName + ss;
                        string bankname = null;
                        if (!G.equal(Program.databanks.GetFirst().aliasName, this.ts.parentDatabank.aliasName))
                        {
                            bankname = this.ts.parentDatabank.aliasName + ":";
                        }
                        temp.Add(bankname + ss);
                    }
                }
                else if (lists.Count == 2)
                {
                    List <string> m1 = ((MetaList)indexes[lists[0]]).list;
                    List <string> m2 = ((MetaList)indexes[lists[1]]).list;
                    foreach (string s1 in m1)
                    {
                        foreach (string s2 in m2)
                        {
                            string ss = null;
                            for (int i = 0; i < indexes.Length; i++)
                            {
                                if (i == lists[0])
                                {
                                    ss += Globals.symbolTurtle + s1;
                                }
                                else if (i == lists[1])
                                {
                                    ss += Globals.symbolTurtle + s2;
                                }
                                else
                                {
                                    ss += Globals.symbolTurtle + O.GetString(indexes[i]);
                                }
                            }
                            ss = this.ts.variableName + ss;
                            string bankname = null;
                            if (!G.equal(Program.databanks.GetFirst().aliasName, this.ts.parentDatabank.aliasName))
                            {
                                bankname = this.ts.parentDatabank.aliasName + ":";
                            }
                            temp.Add(bankname + ss);
                        }
                    }
                }
                else
                {
                    G.Writeln2("*** ERROR: Sorry, unfolding of > 2 dimension not supported at the moment");
                    throw new GekkoException();
                }
                MetaList ml = new Gekko.MetaList(temp);
                return(ml);
            }
            else if (indexes.Length > 0 && indexes[0].Type() == EVariableType.String)
            {
                string hash = TimeSeries.GetHashCodeFromIvariables(indexes);
                O.ECreatePossibilities canCreate = O.ECreatePossibilities.None;
                if (isLhs)
                {
                    canCreate = O.ECreatePossibilities.Can;
                }
                string     varHash = this.ts.variableName + Globals.symbolTurtle + hash;
                TimeSeries ts      = this.ts.parentDatabank.GetVariable(this.ts.freqEnum, varHash);
                if (ts == null)
                {
                    if (canCreate == O.ECreatePossibilities.None)
                    {
                        if (Program.options.series_array_ignoremissing)
                        {
                            return(new ScalarVal(0d));
                        }
                        else
                        {
                            string prettyName = this.ts.parentDatabank.aliasName + ":" + this.ts.variableName + "[" + G.PrettifyTimeseriesHash(hash, false, false) + "]";
                            G.Writeln2("*** ERROR: Cannot find " + prettyName);
                            if (prettyName.Contains("["))
                            {
                                Program.ArrayTimeseriesTip(this.ts.variableName);
                            }
                            G.Writeln("+++ NOTE: Set 'OPTION series array ignoremissing = yes;' to ignore this error.");
                            throw new GekkoException();
                        }
                    }
                    else
                    {
                        ts = new TimeSeries(this.ts.freqEnum, varHash);
                        this.ts.parentDatabank.AddVariable(ts);
                    }
                }
                this.ts.SetDirtyGhost(true, true);  //otherwise, an ASER x['a'] = ... will not register 'x' as a ghost.
                return(new MetaTimeSeries(ts));
            }
            else
            {
                //y[2010] or y[-1]
                IVariable index = indexes[0];
                if (index.Type() == EVariableType.Val)
                {
                    int ival = O.GetInt(index);
                    if (ival >= 1900)
                    {
                        return(new ScalarVal(this.ts.GetData(new GekkoTime(EFreq.Annual, ival + this.offset, 1))));
                    }
                    else
                    {
                        //typically ival numerically < 10 here
                        //return new MetaTimeSeries(this.ts, ival, this.bank, this.variable);
                        return(new MetaTimeSeries(this.ts, ival + this.offset));
                        //10% faster, but maybe more error prone...
                        //this.offset = ival;
                        //return this;
                    }
                }
                else if (index.Type() == EVariableType.Date)
                {
                    return(new ScalarVal(this.ts.GetData(((ScalarDate)index).date.Add(this.offset))));
                }
                else
                {
                    //should not be possible
                    G.Writeln2("*** ERROR: SERIES uses []-indexer with wrong variable type");
                    throw new GekkoException();
                }
            }
        }