Esempio n. 1
0
        /// <summary>
        /// Processes all BigMatrix worksheets in a given Workbook
        /// </summary>
        /// <param name="activeWorkbook"></param>
        /// <returns></returns>
        public static bool ProcessAll(IBIGMatrixStatus bmStatus)
        {
            DateTime start = DateTime.Now;

            HydroSharedAddIn.TransitLoss2 tl2 = new HydroSharedAddIn.TransitLoss2();
            tl2.DoLock(true);
            _inputParameterArrays.Clear();
            bmStatus.log(Globals.ThisWorkbook.Name + " begin simulation.");

            List<string> wrkBookNames = GetWorksheetNames();

            // Determine if we will use the new Matrix_Workbook algorithm (new) or the BIG Matrix algorithm (legacy)
            if (HydroSharedAddIn.Simulation.Matrix_Workbook.ProcessAll(bmStatus))
            {
                return true;
            }

            bmStatus.log("found j349 worksheet, using BIG Matrix technique.");

            foreach (string wrkBookName in wrkBookNames)
            {
                string lowername = wrkBookName.ToLower();
                if (lowername.IndexOf("big matrix") == 0)
                {
                    string bigMatrixSuffix = wrkBookName.Replace("BIG Matrix", "");
                    bigMatrixSuffix = bigMatrixSuffix.Replace("BIG matrix", "");
                    string j349WBName = "j349" + bigMatrixSuffix;
                    string j349LogWBName = "j349" + bigMatrixSuffix + "_log";

                    Worksheet bigMatrixWS = GetWorksheet(wrkBookName);
                    Worksheet j349WS = GetWorksheet(j349WBName);
                    Worksheet j349LogWS = GetWorksheet(j349LogWBName);
                    try
                    {
                        if (!ProcessBigMatrix(bigMatrixWS, j349WS, j349LogWS, bmStatus))
                        {
                            StringCollection sc = bmStatus.getInfo();
                            foreach (string info in sc)
                            {
                                bmStatus.log(info);
                            }
                            tl2.DoLock(false);
                            return false;
                        }
                    }
                    catch (Exception e)
                    {
                        bmStatus.log("Error: Exception thrown");
                        bmStatus.log(e.ToString());
                        StringCollection sc = bmStatus.getInfo();
                        foreach (string info in sc)
                        {
                            bmStatus.log(info);
                        }
                        tl2.DoLock(false);
                        return false;
                    }
                }
            }

            TimeSpan ts = DateTime.Now - start;
            bmStatus.log(Globals.ThisWorkbook.Name + "  simulations completed in " +
                         ts.Seconds.ToString() + " secs.");
            bmStatus.setBigMatrixStatus(Globals.ThisWorkbook.Name, "successfully completed all simulations.");
            ExcelHelper.flush();
            tl2.DoLock(false);
            Globals.ThisWorkbook.Application.CalculateFull();
            return true;
        }
Esempio n. 2
0
        /// <summary>
        /// Processes a specific BIG Matrix worksheet in an Excel file.
        /// </summary>
        /// <returns></returns>
        public static bool ProcessBigMatrix(Microsoft.Office.Interop.Excel.Worksheet bigMatrixWorksheet,
                                              Microsoft.Office.Interop.Excel.Worksheet j349Worksheet,
                                              Microsoft.Office.Interop.Excel.Worksheet j349LogWorksheet,
                                              IBIGMatrixStatus bmStatus)
        {
            System.DateTime start = System.DateTime.Now;
            /*
            if (activeWorkbook == null)
            {
                bmStatus.log("Error: active workbook is null.");
                return false;
            }
             */
            if (bigMatrixWorksheet == null)
            {
                bmStatus.log("Error: bigMatrixWorksheet is null.");
                return false;
            }
            if (j349Worksheet == null)
            {
                bmStatus.log("Error: j349Worksheet is null.");
                return false;
            }
            if (j349LogWorksheet == null)
            {
                bmStatus.log("Error: j349LogWorksheet is null.");
                return false;
            }

            bmStatus.logInfo(bigMatrixWorksheet.Name + " begin processing.");
            // Zero out all output values in j349 worksheet
            bmStatus.setBigMatrixStatus(bigMatrixWorksheet.Name, "Zeroing data.");
            if( !UpdateExcelWithZeroData(j349Worksheet,j349LogWorksheet,bmStatus) )
            {
                bmStatus.setBigMatrixStatus(bigMatrixWorksheet.Name, "Error: UpdateExcelWithZeroData() failed.");
                bmStatus.log("Error: UpdateExcelWithZeroData() failed.");
                return false;
            }

            // The dataDirectory is a directory used to place j349 data files.
            // For an excel spreadsheet named Calibration2005.xls, the data
            // directory would be Calibration2005
            string dataDirectory = Globals.ThisWorkbook.FullName;
            dataDirectory = dataDirectory.Replace(".xls", "");
            if (!Directory.Exists(dataDirectory)) Directory.CreateDirectory(dataDirectory);
            dataDirectory = dataDirectory + "\\" + bigMatrixWorksheet.Name.Replace(" ", "_");
            if (!Directory.Exists(dataDirectory)) Directory.CreateDirectory(dataDirectory);
            bmStatus.logInfo("  worksheet data directory: " + dataDirectory);

            bool errorOccurred = false;
            string errorStatus = "OK";
            int lastGoodSr = 0;
            bmStatus.setupBigMatrixProgress(12, 1);
            // Loop through each subreach (SR), build SR#.in, produce SR#.out, place results back into worksheet j349
            for (int i = 0; i < 12 && !errorOccurred; ++i)
            {
                System.DateTime srStart = System.DateTime.Now;
                bmStatus.logInfo(bigMatrixWorksheet.Name + " SR" + (i+1).ToString() + ": processing." );
                bmStatus.stepBigMatrixProgress();

                lastGoodSr = i;
                int sr = i + 1;
                bmStatus.setBigMatrixStatus(bigMatrixWorksheet.Name, "Simulating SR" + sr.ToString());
                try
                {
                    //activeWorkbook.Application.CalculateFull();
                    //System.Threading.Thread.Sleep(500);

                    errorStatus = "OK";
                    float[] outData = null;

                    // Name the inFile and outFile to match the excel filename
                    string filename = Globals.ThisWorkbook.FullName;
                    FileInfo fi = new FileInfo(filename);
                    string inFile = dataDirectory + "\\SR" + sr.ToString() + ".in";
                    string outFile = dataDirectory + "\\SR" + sr.ToString() + ".out";

                    bmStatus.logInfo("   in file: " + inFile);
                    bmStatus.logInfo("  out file: " + outFile);

                    // --Generate the .in file--------
                    if (GenerateInFile(j349Worksheet, sr, inFile,bmStatus))
                    {
                        // Run the simulation
                        if (j349.Simulate(inFile, outFile, bmStatus))
                        {
                            // Extract the resulting hydrograph from outFile
                            outData = ExtractOutputHydrograph(outFile,bmStatus);
                            if (outData == null)
                            {
                                errorOccurred = true;
                                errorStatus = "Error: Unable to extract output hydrograph from " + outFile;
                                bmStatus.setBigMatrixStatus(bigMatrixWorksheet.Name, errorStatus);
                                bmStatus.log(errorStatus);
                                return false;
                            }
                        }
                        else
                        {
                            bmStatus.logInfo("j349 simulation failed.");
                            return false; // any errors should have been logged by the method that failed.
                        }
                    }
                    else
                    {
                        bmStatus.logInfo("GenerateInFile failed.");
                        return false; // any errors should have been logged by the method that failed.
                    }

                    // Update the Excel spreadsheet j349_log worksheet with what happened for current subreach
                    try
                    {
                        if (!UpdateExcelWithSubreachData(j349Worksheet,
                                            j349LogWorksheet, sr, inFile,
                                            outFile, errorStatus, outData, _nDays,
                                            bmStatus))
                        {
                            errorOccurred = true;
                            bmStatus.setBigMatrixStatus(bigMatrixWorksheet.Name, "Error updating Worksheet with simulation data on SR " + sr.ToString());
                            bmStatus.log("Error updating Worksheet with simulation data on SR " + sr.ToString());
                            return false;
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("Exception occurred on SR " + sr.ToString());
                        Debug.WriteLine(e.ToString());
                        errorOccurred = true;
                        bmStatus.setBigMatrixStatus(bigMatrixWorksheet.Name, "Exception occurred on SR " + sr.ToString());
                        bmStatus.log("Exception occurred on SR " + sr.ToString());
                        bmStatus.log(e.ToString());
                        return false;
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Exception occurred on SR " + sr.ToString());
                    Debug.WriteLine(e.ToString());
                    bmStatus.log("Exception occurred on SR " + sr.ToString());
                    bmStatus.log(e.ToString());
                    errorOccurred = true;
                }

                // SR completed
                System.TimeSpan ts = System.DateTime.Now - srStart;
                bmStatus.log("  SR" + sr.ToString() + " completed in " + ts.Seconds.ToString() + " secs");

                // Allow workbook to update it's calculations
                //activeWorkbook.RefreshAll();
            }

            // Format a status string
            // Status will be of the form "SR1-SR7 OK, SR8: Error: ..."
            string status = "OK";
            if (lastGoodSr == 0)
            {
                // nothing ran
                status = "SR1 failed to run correctly";
                bmStatus.setBigMatrixStatus(bigMatrixWorksheet.Name, status);
                bmStatus.log(status);
                return false;
            }
            if (lastGoodSr == 1)
            {
                status = "SR1 OK, SR2 " + errorStatus;
                bmStatus.setBigMatrixStatus(bigMatrixWorksheet.Name, status);
                bmStatus.log(status);
                return false;
            }
            if (lastGoodSr > 1)
            {
                status = "SR1-SR" + lastGoodSr.ToString() + " OK, SR" + (lastGoodSr + 1).ToString() + " " + errorStatus;
            }

            System.TimeSpan ts2 = System.DateTime.Now - start;
            bmStatus.log(bigMatrixWorksheet.Name +
                         " completed in " + ts2.Seconds.ToString() + " secs.");
            bmStatus.setBigMatrixStatus(bigMatrixWorksheet.Name,status);

            // Allow
            GC.Collect();

            return true;
        }