/// <summary>
/// Write the station results to a data file in CSV format.
/// </summary>
/// <returns>0 if successful; -ve for error code</returns>
protected int WriteStationResults()
{
OpenTemp.WriteLine(string.Format("Writing the station results file '{0}'...", _outputStationFile)); // Open the file
StreamWriter sw;
try
{
sw = new StreamWriter(_outputStationFile, false); // Overwrite (do not append)
OpenTemp.WriteLine(" File opened for writing");
}
catch (Exception ex)
{
OpenTemp.WriteLine(string.Format(" Exception thrown when opening file: {0}", ex.ToString()));
return -1;
}
if (sw == null)
{
OpenTemp.WriteLine(" Failed to open file (check that it exists and the path is correct)");
return -2;
}
// Write header
sw.Write("YEAR,MONTH");
foreach (string countryStation in _stations.Keys)
{
sw.Write(",");
sw.Write(countryStation);
}
sw.WriteLine();
// Write one line at a time (starting in 1880)
DateTime date = new DateTime(1880, 1, 1);
DateTime today = DateTime.Today;
int nMonths = 0;
while (date < today)
{
// Any values for this month?
int nValues = 0;
foreach (Station station in _stations.Values)
{
foreach (byte series in station.SeriesNumbers)
{
SortedList<DateTime, float?> values = station.GetReadings(series);
if (values.ContainsKey(date))
{
if (values[date].HasValue) nValues++;
}
}
}
// Write output if values found
if (nValues > 0)
{
// Write month label
sw.Write(date.ToString("yyyy"));
sw.Write(",");
sw.Write(date.ToString("MM"));
// Write values for this month
foreach (Station station in _stations.Values)
{
foreach (byte series in station.SeriesNumbers)
{
sw.Write(",");
SortedList<DateTime, float?> values = station.GetReadings(series);
if (values.ContainsKey(date))
{
if (!values[date].HasValue) continue;
sw.Write(string.Format("{0:F2}", values[date].Value));
}
}
}
sw.WriteLine();
nMonths++;
}
// Next month
date = date.AddMonths(1);
}
// Done
sw.Close();
// Done
OpenTemp.WriteLine(string.Format(" Writing finished: {0} months written", nMonths));
return 0;
}
John V | 20-Sep-07 at 11:36 am | Permalink
Should always write a row for the month, even if there are no values (just write blanks for every month). To do this, move the code that writes the month and the Console.WriteLine() outside of the loop.
John V | 20-Sep-07 at 11:52 am | Permalink
BUG:
The header must show every series — not just every station. Causes problems when there are multiple series per station.
There is an additional problem. The data written seems to include one column for every series name in all stations (instead of just a column for the current station).
Clayton B. | 21-Sep-07 at 11:02 pm | Permalink
Aren’t those the same problem? You only want to write one set of data per station, correct?
I think the multiple series should be corrected (combined) as soon as possible to make the data easy to manage throughout the rest of the program.
Clayton B. | 22-Sep-07 at 2:32 pm | Permalink
Regarding my previous comment: From discussions at climateaudit (http://www.climateaudit.org/?p=2069 #369) it may not be a good idea to combine series for a single station.
John V | 24-Sep-07 at 6:54 am | Permalink
Clayton B, I’m glad we agree.
If possible I would prefer to avoid merging any station series. If merging is advantageous, it should be done as a seperate step and the post-merge results should be written to a new data file.