OpenTemp.WriteStationResults()

        /// <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;
        }