public static void SwapBankAliases(string old1, string new1, string old2, string new2)
        {
            G.Writeln2("*** ERROR: Swapping is not working at the moment.");
            throw new GekkoException();
            Databank db1 = Program.databanks.GetDatabank(old1);
            Databank db2 = Program.databanks.GetDatabank(old2);

            Program.databanks.RemoveDatabank(old1);
            Program.databanks.RemoveDatabank(old2);
            db1.aliasName = new1;
            db2.aliasName = new2;
        }
        public void ReplaceDatabank(Databank db1, Databank db2)
        {
            bool ok = true;

            for (int i = 0; i < this.storage.Count; i++)
            {
                if (G.equal(db1.aliasName, this.storage[i].aliasName))
                {
                    this.storage[i] = db2; break;
                }
            }
            if (!ok)
            {
                G.Writeln2("*** ERROR: Could not replace databank " + db2.aliasName);
                throw new GekkoException();
            }
        }
        private void RefreshList()
        {
            list.Clear();
            List <string> banks2 = new List <string>();

            foreach (Databank db in Program.databanks.storage)
            {
                banks2.Add(db.aliasName);
            }

            for (int i = 0; i < banks2.Count; i++)
            {
                string   s        = banks2[i];
                Databank databank = Program.databanks.GetDatabank(s);
                if (i == 1)
                {
                    if (G.equal(databank.aliasName, Globals.Ref))
                    {
                        if (databank.storage.Count == 0)
                        {
                            continue;                              //skip it, so that Ref is not shown in the list when it is empty
                        }
                    }
                }
                string c      = "";
                string period = databank.yearStart + "-" + databank.yearEnd;
                if (databank.yearStart == -12345 || databank.yearEnd == -12345)
                {
                    period = "";
                }
                string prot = null;
                if (!databank.protect)
                {
                    prot = Globals.protectSymbol;
                }
                else
                {
                    prot = "";
                }
                list.Add(new Task(s, System.IO.Path.GetFileName(databank.FileNameWithPath), databank.FileNameWithPath, databank.storage.Count.ToString(), period, databank.info1, databank.date, c, prot, i));
            }
            //unswap.IsEnabled = Program.AreDatabanksSwapped();
            //unswap.IsEnabled = false;  //FIXME
        }
        public bool OpenDatabank(ref Databank databank, EOpenType openType, int openPosition)
        {
            if (openType == EOpenType.Pos)
            {
                if (openPosition > this.storage.Count)
                {
                    G.Writeln2("*** ERROR: There are " + (this.storage.Count - 1) + " numbered databanks in the databank list (F2).");
                    G.Writeln("           Opening in position " + openPosition + " is not possible.", Color.Red);
                    throw new GekkoException();
                }
            }

            if (openType == EOpenType.Ref)
            {
                G.Writeln2("+++ WARNING: OPEN<ref> is for advanced users, and will put the existing " + Globals.Ref + " in the list of 'normal' databanks", Globals.warningColor);
            }
            bool readFromFile = false;

            //Does not read the actual bank, but just arranges for the bank to be read into the right 'slot'
            //If <first/edit> or <ref>, the bank in the [0] or [1] slot is pushed down to [2].
            if (G.equal(databank.aliasName, Globals.Work))
            {
                if (openType == EOpenType.Normal || openType == EOpenType.Last)
                {
                    G.Writeln2("*** ERROR: The 'Work' databank cannot be opened or closed (it is always open).");
                    throw new GekkoException();
                }
                else if (openType == EOpenType.First)
                {
                    G.Writeln2("*** ERROR: You cannot use OPEN<first> with the 'Work' databank.");
                    G.Writeln("           If Work is not first and it needs to be, you must CLOSE");
                    G.Writeln("           the present first databank. After that, Work will be first.");
                    throw new GekkoException();
                }
                else if (openType == EOpenType.Edit)
                {
                    G.Writeln2("*** ERROR: You cannot use OPEN<edit> with the 'Work' databank.");
                    G.Writeln("           Work is always editable, and if Work is not first on");
                    G.Writeln("           the F2 databank list, you must CLOSE the present first");
                    G.Writeln("           databank. After that, Work will become first (and editable).");

                    throw new GekkoException();
                }
                else if (openType == EOpenType.Ref)
                {
                    G.Writeln2("*** ERROR: You cannot use OPEN<ref> with the 'Work' databank.");
                    G.Writeln("           It is not legal to set Work as the ref databank.");
                    G.Writeln("           Use the Ref databank for such purposes.");
                    throw new GekkoException();
                }
            }
            else if (G.equal(databank.aliasName, Globals.Ref))  //Ref
            {
                if (openType == EOpenType.Normal)
                {
                    G.Writeln2("*** ERROR: The '" + Globals.Ref + "' databank cannot be opened or closed (it is always open).");
                    throw new GekkoException();
                }
                else if (openType == EOpenType.First)
                {
                    G.Writeln2("*** ERROR: You cannot use OPEN<first> with the '" + Globals.Ref + "' databank.");
                    throw new GekkoException();
                }
                else if (openType == EOpenType.Last)
                {
                    G.Writeln2("*** ERROR: You cannot use OPEN<last> with the '" + Globals.Ref + "' databank.");
                    throw new GekkoException();
                }
                else if (openType == EOpenType.Edit)
                {
                    G.Writeln2("*** ERROR: You cannot use OPEN<edit> with the '" + Globals.Ref + "' databank.");
                    throw new GekkoException();
                }
                else if (openType == EOpenType.Ref)
                {
                    G.Writeln2("*** ERROR: You cannot use OPEN<ref> with the '" + Globals.Ref + "' databank.");
                    throw new GekkoException();
                }
            }
            string name = databank.aliasName;
            int    existI; int WorkI; int BaseI; FindBanksI(name, out existI, out WorkI, out BaseI);

            List <Databank> m = new List <Databank>(this.storage.Count);

            if (existI != -12345)  //the databank name already exists. No actual file reading, just rearrange the banks
            {
                DatabankLogicExistingBank(out databank, openType, openPosition, out readFromFile, name, existI, WorkI, BaseI, m);
            }
            else  //the databank name does not exist, so it is new and will be read from file later on
            {
                readFromFile = true;
                if (G.equal(Program.options.databank_logic, "aremos"))
                {
                    DatabankLogicAREMOS(databank, openType, openPosition, name, m);
                }
                else
                {
                    DatabankLogicDefault(databank, openType, openPosition, name, m);
                }
            }
            this.storage = m;
            return(readFromFile);
        }
        public Databank RemoveDatabank(string name)
        {
            if (G.equal(name, Globals.Work))
            {
                G.Writeln2("*** ERROR: " + Globals.Work + " databank cannot be closed");
                throw new GekkoException();
            }
            if (G.equal(name, Globals.Ref))
            {
                G.Writeln2("*** ERROR: " + Globals.Ref + " databank cannot be closed");
                throw new GekkoException();
            }

            int existI; int WorkI; int BaseI; FindBanksI(name, out existI, out WorkI, out BaseI);

            if (existI == -12345)
            {
                G.Writeln2("*** ERROR: Could not close databank '" + name + "': the bank is not open");
                throw new GekkoException();
            }

            Databank databankToRemove = Program.databanks.storage[existI];

            List <Databank> m = new List <Databank>(this.storage.Count - 1);

            if (existI == 0) //found as first
            {
                if (G.equal(Program.options.databank_logic, "aremos"))
                {
                    //AREMOS jumping
                    m.Add(this.storage[WorkI]); //[0]: Work is put back
                    m.Add(this.storage[1]);     //[1]: not touched
                    for (int i = 2; i < this.storage.Count; i++)
                    {
                        if (i == WorkI)
                        {
                            continue;
                        }
                        m.Add(this.storage[i]);
                    }
                }
                else
                {
                    //Default
                    //Closing a bank in first position (not Work)
                    m.Add(this.storage[2]);  //[0]: gets #2 that is, number 2 on the non-ref databank list.
                    m.Add(this.storage[1]);  //[1]: ref is not touched
                    for (int i = 3; i < this.storage.Count; i++)
                    {
                        //add the rest
                        m.Add(this.storage[i]);
                    }
                }
            }
            else if (existI == 1)           //found as ref
            {
                m.Add(this.storage[0]);     //[0]: not touched
                m.Add(this.storage[BaseI]); //[1]: Base is put back
                for (int i = 2; i < this.storage.Count; i++)
                {
                    if (i == BaseI)
                    {
                        continue;
                    }
                    m.Add(this.storage[i]);
                }
            }
            else //found as a normal open bank (position 2 or larger)
            {
                m.Add(this.storage[0]);  //[0]: not touched
                m.Add(this.storage[1]);  //[1]: not touched
                for (int i = 2; i < this.storage.Count; i++)
                {
                    if (i == existI)
                    {
                        continue;
                    }
                    m.Add(this.storage[i]);
                }
            }
            this.storage = m;
            return(databankToRemove);
        }
        private void DatabankLogicAREMOS(Databank databank, EOpenType openType, int openPosition, string name, List <Databank> m)
        {
            //AREMOS logic
            if (openType == EOpenType.Normal || openType == EOpenType.Sec || (openType == EOpenType.Pos && openPosition == 2))
            {
                m.Add(this.storage[0]);  //first
                m.Add(this.storage[1]);  //ref
                m.Add(databank);
                for (int i = 2; i < this.storage.Count; i++)
                {
                    m.Add(this.storage[i]);
                }
                G.Writeln2("Databank '" + name + "' opened");
            }
            else if (openType == EOpenType.First || openType == EOpenType.Edit || (openType == EOpenType.Pos && openPosition == 1))
            {
                bool edit = false;
                if (openType == EOpenType.Edit)
                {
                    edit = true;
                }
                m.Add(databank);         //first
                m.Add(this.storage[1]);  //ref
                m.Add(this.storage[0]);
                for (int i = 2; i < this.storage.Count; i++)
                {
                    m.Add(this.storage[i]);
                }
                if (openType == EOpenType.Edit)
                {
                    G.Writeln2("Databank '" + name + "' opened as editable in first position");
                }
                else
                {
                    G.Writeln2("Databank '" + name + "' opened in first position");
                }
            }
            else if (openType == EOpenType.Ref)
            {
                m.Add(this.storage[0]);         //first
                m.Add(databank);                //ref
                m.Add(this.storage[1]);
                for (int i = 2; i < this.storage.Count; i++)
                {
                    m.Add(this.storage[i]);
                }
                G.Writeln2("Databank '" + name + "' opened as ref");
            }
            else if (ShouldPutBankLastAREMOS(openType, openPosition))
            {
                m.Add(this.storage[0]);  //first
                m.Add(this.storage[1]);  //ref
                for (int i = 2; i < this.storage.Count; i++)
                {
                    m.Add(this.storage[i]);
                }
                m.Add(databank);
                G.Writeln2("Databank '" + name + "' opened");
            }
            else if (openType == EOpenType.Pos)
            {
                //pos is not 1., 2. or count+1 ===> so 3, 4, ..., up to count.
                if (openPosition < 1)
                {
                    G.Writeln2("*** ERROR: OPEN<pos=...> cannot be 0 or negative");
                    throw new GekkoException();
                }
                m.Add(this.storage[0]);  //first
                m.Add(this.storage[1]);  //ref
                for (int i = 2; i < openPosition; i++)
                {
                    m.Add(this.storage[i]);
                }
                m.Add(databank);
                for (int i = openPosition; i < this.storage.Count; i++)
                {
                    m.Add(this.storage[i]);
                }
                G.Writeln2("Databank '" + name + "' opened in position " + openPosition);
            }
            else
            {
                G.Writeln("*** ERROR: Internal error ¤89435734");
                throw new GekkoException();
            }

            return;
        }
 private void DatabankLogicExistingBank(out Databank databank, EOpenType openType, int openPosition, out bool readFromFile, string name, int existI, int WorkI, int BaseI, List <Databank> m)
 {
     databank     = this.storage[existI]; //now points to the existing databank, and no longer the empty databank the method was called with
     readFromFile = false;
     if (openType == EOpenType.Normal || openType == EOpenType.Last || (openType == EOpenType.Pos && openPosition != 1))
     {
         G.Writeln2("*** ERROR: Databank '" + databank.aliasName + "' is already open. Use CLOSE to close it first.");
         throw new GekkoException();
     }
     else if (openType == EOpenType.Edit || openType == EOpenType.First || (openType == EOpenType.Pos && openPosition == 1))
     {
         if (existI == 0)
         {
             //Note: OPEN<edit> could be used to unlock an OPEN<first>...
             //this.storage[0].protect = false;  //this is set elsewhere
             if (openType == EOpenType.Edit)
             {
                 if (databank.protect == false)
                 {
                     G.Writeln2("Databank '" + databank.aliasName + "' is already editable in first position.");
                 }
                 else
                 {
                     databank.protect = false;
                     G.Writeln2("Databank '" + databank.aliasName + "' set editable.");
                 }
             }
             m.AddRange(this.storage);    //just copied, and put back again later on
         }
         else if (existI == 1)            //Trying an OPEN<edit>db on a db that is already ref (opened with OPEN<ref>db).
         {
             m.Add(this.storage[existI]); //first, = former sec
             m.Add(this.storage[BaseI]);  //ref, = Ref databank, to aviod empty slot
             m.Add(this.storage[0]);      //former first ends here
             for (int i = 2; i < this.storage.Count; i++)
             {
                 if (i == BaseI)
                 {
                     continue;
                 }
                 m.Add(this.storage[i]);
             }
         }
         else  //Trying an OPEN<edit>db on a db that is already there in slot [2] or below
         {
             m.Add(this.storage[existI]);         //first
             m.Add(this.storage[1]);              //ref, same
             m.Add(this.storage[0]);
             for (int i = 2; i < this.storage.Count; i++)
             {
                 if (i == existI)
                 {
                     continue;
                 }
                 m.Add(this.storage[i]);
             }
         }
         if (openType == EOpenType.Edit)
         {
             G.Writeln2("Databank '" + name + "' set as editable databank, put first position.");
         }
         else
         {
             G.Writeln2("Databank '" + name + "' put in first position.");
         }
     }
     else if (openType == EOpenType.Ref)
     {
         if (existI == 0)                 //Trying an OPEN<sec>db on a db that is already first/editable (opened with OPEN<first> or OPEN<edit>)
         {
             m.Add(this.storage[WorkI]);  //first, = Work databank, to aviod empty slot
             m.Add(this.storage[existI]); //ref, = former first
             m.Add(this.storage[1]);      //former ref ends here
             for (int i = 2; i < this.storage.Count; i++)
             {
                 if (i == WorkI)
                 {
                     continue;
                 }
                 m.Add(this.storage[i]);
             }
         }
         else if (existI == 1)
         {
             G.Writeln2("*** ERROR: Databank '" + databank.aliasName + "' is already open as ref bank");
             throw new GekkoException();
         }
         else  //Trying an OPEN<edit/first>db on a db that is already there in slot [2] or below
         {
             m.Add(this.storage[0]);         //first, same
             m.Add(this.storage[existI]);    //ref
             m.Add(this.storage[1]);
             for (int i = 2; i < this.storage.Count; i++)
             {
                 if (i == existI)
                 {
                     continue;
                 }
                 m.Add(this.storage[i]);
             }
         }
         G.Writeln2("Databank '" + name + "' set as ref bank");
     }
 }
Exemple #8
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.
        }
        // Performs custom drop logic for the top ListView.
        void dragMgr_ProcessDrop(object sender, ProcessDropEventArgs <Task> e)
        {
            // This shows how to customize the behavior of a drop.
            // Here we perform a swap, instead of just moving the dropped item.

            if (!Program.options.interface_databank_swap)
            {
                MessageBox.Show("*** ERROR: Databank swapping not allowed, since 'OPTION interface databank swap = no'");
                return;
            }

            string text = "";

            int higherIdx = Math.Max(e.OldIndex, e.NewIndex);
            int lowerIdx  = Math.Min(e.OldIndex, e.NewIndex);

            Task t_from = list[e.OldIndex];
            Task t_to   = list[e.NewIndex];

            string aliasFromOld = t_from.AliasName;
            string aliasToOld   = t_to.AliasName;

            string s = null;

            if (lowerIdx < 0)
            {
                // The item came from the lower ListView
                // so just insert it.
                e.ItemsSource.Insert(higherIdx, e.DataItem);
            }
            else
            {
                // null values will cause an error when calling Move.
                // It looks like a bug in ObservableCollection to me.
                if (e.ItemsSource[lowerIdx] == null ||
                    e.ItemsSource[higherIdx] == null)
                {
                    Program.ShowPeriodInStatusField("");
                    return;
                }

                // The item came from the ListView into which
                // it was dropped, so swap it with the item
                // at the target index.
                e.ItemsSource.Move(lowerIdx, higherIdx);
                e.ItemsSource.Move(higherIdx - 1, lowerIdx);

                Databank lower  = Program.databanks.storage[lowerIdx];
                Databank higher = Program.databanks.storage[higherIdx];
                Program.databanks.storage[lowerIdx]  = higher;
                Program.databanks.storage[higherIdx] = lower;
                //remember that higher is at lowerIdx and vice versa!
                if ((lowerIdx == 0 || lowerIdx == 1) && !(G.equal(higher.aliasName, Globals.Work) || G.equal(higher.aliasName, Globals.Ref)))
                {
                    if (!higher.protect)
                    {
                        higher.protect = true;
                        s += "Note that the databank '" + higher.aliasName + "' has been set non-editable. ";
                        list[lowerIdx].Prot = Globals.protectSymbol;
                    }
                }
                //remember that higher is at lowerIdx and vice versa!
                if ((higherIdx == 0 || higherIdx == 1) && !(G.equal(lower.aliasName, Globals.Work) || G.equal(lower.aliasName, Globals.Ref)))
                {
                    if (!lower.protect)
                    {
                        lower.protect        = true;
                        s                   += "Note that the databank '" + lower.aliasName + "' has been set non-editable. ";
                        list[higherIdx].Prot = Globals.protectSymbol;
                    }
                }

                int counter = 0;
                foreach (var x in e.ItemsSource)
                {
                    counter++;
                    x.Number = counter.ToString();
                    if (x.Number == "2")
                    {
                        x.LineColor = "Black";                   //these numbers are 1-based and are strings!
                    }
                    else
                    {
                        x.LineColor = "LightGray";
                    }
                }
            }

            // Set this to 'Move' so that the OnListViewDrop knows to
            // remove the item from the other ListView.
            e.Effects = DragDropEffects.Move;
            //unswap.IsEnabled = Program.AreDatabanksSwapped();
            //unswap.IsEnabled = true;  //fixme

            yellow.Text = "Databanks were swapped. " + s;
            Program.ShowPeriodInStatusField("");
        }