public static List <string> GetScenarioStations(dataClassEnum dataClass, List <string> scenarioList) { int startYear = 0; int endYear = 0; return(GetScenarioStations(dataClass, scenarioList, out startYear, out endYear)); }
public static List <string> GetDiversionList(dataClassEnum dataClass, List <string> scenarioList, List <string> stationList) { // Taken from HSS's UPDATE_diversion_list.c List <string> diversionList = new List <string>(); int indexPtr = 0; // The last element of indices is -1, so if we hit this value, bail out. foreach (string scenBaseName in scenarioList) { /* * Open the .ods file for the current scenario. */ FileInfo fi = new FileInfo(scenBaseName + ".ods"); if (!fi.Exists) { Console.WriteLine(string.Format("{0} does not exist.", fi.FullName)); return(null); } StreamReader sr = new StreamReader(fi.FullName); string scenName = fi.Name; // Read the year from the first line. string input = sr.ReadLine(); // The year is the last two items string[] tokens = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int scenStartYear = int.Parse(tokens[tokens.Length - 2]); int scenEndYear = int.Parse(tokens[tokens.Length - 1]); // The next six lines are skipped. for (int i = 1; i < 6; i++) { sr.ReadLine(); } // The diversions will be accumulated in a list which will // later be ORed with the total list. List <string> scenarioDiversionList = new List <string>(); int currentYear = 0; switch (dataClass) { case dataClassEnum.IFR: while (!sr.EndOfStream && currentYear <= scenStartYear) { string strLine = sr.ReadLine(); string stationName = ""; string ifrName = ""; string title = ""; currentYear = int.Parse(strLine.Substring(0, 4)); int code = int.Parse(strLine.Substring(15, 3)); // Grab all ods records for ifrs. if (code == 500 && currentYear == scenStartYear) { stationName = strLine.Substring(4, 6); ifrName = strLine.Substring(11, 4); title = strLine.Substring(37, 40); // Check to see if the station is in the selected list. if (stationList.IndexOf(stationName) >= 0) { string newIFR = ifrName + "-" + title; if (scenarioDiversionList.IndexOf(newIFR) < 0) { scenarioDiversionList.Add(newIFR); } } } } break; case dataClassEnum.DIVERSION: while (!sr.EndOfStream && currentYear <= scenStartYear) { string strLine = sr.ReadLine(); currentYear = int.Parse(strLine.Substring(0, 4)); int code = int.Parse(strLine.Substring(15, 3)); // Grab all ods records for diversions. if (code == 400 && currentYear == scenStartYear) { string stationName = strLine.Substring(4, 6); string div = strLine.Substring(11, 4); string title = strLine.Substring(84, 40); // Check to see if the station is in the selected list. if (stationList.IndexOf(stationName) >= 0) { string newDiv = div + "-" + title; if (scenarioDiversionList.IndexOf(newDiv) < 0) { scenarioDiversionList.Add(newDiv); } } } } break; } sr.Close(); // Initialize or pare the station list. if (indexPtr > 0) { // OR the total list and the scenario's diversion list. foreach (string station in diversionList) { // Look for this station in the current list. IF it isn't there, // then remove it from the list in order to only keep common // stations. if (scenarioDiversionList.IndexOf(station) < 0) { // The station does not appear in both scenarios, so remove it. int idx = diversionList.IndexOf(station); diversionList.RemoveAt(idx); } } } else { // Copy the scenario's diversion list into the total list. diversionList.AddRange(scenarioDiversionList); } indexPtr++; } return(diversionList); }
public static List <string> GetScenarioStations(dataClassEnum dataClass, List <string> scenarioList, out int startYear, out int endYear) { // Taken from HSS's UPDATE_station_list.c /* * Loop through each scenario. * 1. Open the ns file for the scenario. * 2. Depending on the data class [e.g. ifr], * grab the stations for the scenario and * add to the station_list widget. * 3. Close the ns file. */ List <string> stationList = new List <string>(); startYear = -1; endYear = -1; int indexPtr = 0; // The last element of indices is -1, so if we hit this value, bail out. foreach (string scenBaseName in scenarioList) { // Open the .ods file to get the min and max year for this scenario. FileInfo fi = new FileInfo(scenBaseName + ".ods"); if (!fi.Exists) { Console.WriteLine(string.Format("File {0} does not exist.", fi.FullName)); return(null); } StreamReader sr = new StreamReader(fi.FullName); // Read the year from the first line. string input = sr.ReadLine(); // The year is the last two items string[] tokens = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int scenStartYear = int.Parse(tokens[tokens.Length - 2]); int scenEndYear = int.Parse(tokens[tokens.Length - 1]); if (scenStartYear > startYear) { startYear = scenStartYear; } if (scenEndYear < endYear) { endYear = scenEndYear; } sr.Close(); // Bail out if we are looking for dates. if (dataClass == dataClassEnum.YEARS_ONLY) { return(stationList); } /* * Open the .ns file for the current scenario. */ fi = new FileInfo(scenBaseName + ".ns"); if (!fi.Exists) { Console.WriteLine(string.Format("File {0} does not exist.", fi.FullName)); return(null); } sr = new StreamReader(fi.FullName); // The stations will be accumulated in a list which will later be ORed // with the total list. List <string> scenarioStationList = new List <string>(); /* * Add new stations to the station list. */ switch (dataClass) { /* * For variable groups station, flow, and all, * all station nodes are presented in the * list. */ case dataClassEnum.STATION: case dataClassEnum.FLOW: case dataClassEnum.ALL: { while (!sr.EndOfStream) { string strLine = sr.ReadLine(); if (strLine.IndexOf("STA") == 0) { string id = strLine.Substring(4, 6); string name = strLine.Substring(37, 40); if (scenarioStationList.IndexOf(name) < 0) { scenarioStationList.Add(id + "-" + name); } } } } break; /* * For variable groups reservoir, power, ifr, * and diversion, only the station nodes where the * appropriate subgroups exist are presented in * the list. */ case dataClassEnum.RESERVOIR: case dataClassEnum.POWER: case dataClassEnum.IFR: case dataClassEnum.DIVERSION: { string[] dataClassStringAbrev = { "STA", "RES", "PWR", "DV1", "IFR" }; while (!sr.EndOfStream) { string stationName = ""; string stationID = ""; string strLine = sr.ReadLine(); if (strLine.IndexOf("STA") == 0) { stationID = strLine.Substring(4, 6); stationName = strLine.Substring(37, 40); } else if (strLine.IndexOf(dataClassStringAbrev[(int)dataClass]) == 0) { string name_and_id = stationID + "-" + stationName; if (scenarioStationList.IndexOf(name_and_id) < 0) { scenarioStationList.Add(name_and_id); } } } } break; } sr.Close(); // Initialize or pare the station list. if (indexPtr > 0) { // OR the total list and the scenario list. Save in a temp // list, which will later be saved as the new total list. List <string> templist = new List <string>(); foreach (string station in stationList) { // Look for an occurrance of the station from another scenario. if (scenarioStationList.IndexOf(station) >= 0) { // Save any intersections. templist.Add(station); } } // Update the total list from the temp list. stationList.Clear(); stationList.AddRange(templist); } else { // Copy the scenario list into the total list. stationList.AddRange(scenarioStationList); } indexPtr++; } return(stationList); }