/// <summary>
        /// Create a dictionary for looking up a GhcnSite object based on its site code
        /// </summary>
        /// <returns>SiteCode - GhcnSite object lookup dictionary</returns>
        private Dictionary <string, GhcnSite> GetSiteLookup()
        {
            Dictionary <string, GhcnSite> lookup = new Dictionary <string, GhcnSite>();

            string connString = ConfigurationManager.ConnectionStrings["OdmConnection"].ConnectionString;

            using (SqlConnection connection = new SqlConnection(connString))
            {
                string sql = "SELECT SiteID, SiteCode, SiteName FROM dbo.Sites";
                using (SqlCommand cmd = new SqlCommand(sql, connection))
                {
                    cmd.Connection.Open();

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        string   code = reader.GetString(1);
                        GhcnSite site = new GhcnSite
                        {
                            SiteID   = reader.GetInt32(0),
                            SiteCode = code,
                            SiteName = reader.GetString(2),
                        };
                        lookup.Add(code, site);
                    }
                    reader.Close();
                    cmd.Connection.Close();
                }
            }
            return(lookup);
        }
示例#2
0
        public List <GhcnSite> ReadSitesFromWeb()
        {
            var countries = ReadCountriesFromWeb();
            var states    = ReadStatesFromWeb();

            string sitesUrl = "https://www1.ncdc.noaa.gov/pub/data/ghcn/daily/ghcnd-stations.txt";

            Console.WriteLine("Reading Sites from URL: " + sitesUrl);
            _log.LogWrite("Reading sites from URL: " + sitesUrl);

            // positions of columns in the ghcnd-stations file
            Dictionary <string, TextFileColumn> colPos = new Dictionary <string, TextFileColumn>();

            colPos.Add("code", new TextFileColumn(1, 11));
            colPos.Add("lat", new TextFileColumn(13, 20));
            colPos.Add("lon", new TextFileColumn(22, 30));
            colPos.Add("elevation", new TextFileColumn(32, 37));
            colPos.Add("state", new TextFileColumn(39, 40));
            colPos.Add("name", new TextFileColumn(42, 71));
            colPos.Add("gsnflag", new TextFileColumn(73, 75));
            colPos.Add("hcnflag", new TextFileColumn(77, 79));
            colPos.Add("wmo", new TextFileColumn(81, 85));
            colPos.Add("sitecode", new TextFileColumn(1, 11));
            colPos.Add("varcode", new TextFileColumn(32, 35));
            colPos.Add("firstyear", new TextFileColumn(37, 40));
            colPos.Add("lastyear", new TextFileColumn(42, 45));

            List <GhcnSite> siteList = new List <GhcnSite>();

            try
            {
                var client = new WebClient();
                using (var stream = client.OpenRead(sitesUrl))
                    using (var reader = new StreamReader(stream))
                    {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            string code        = line.Substring(colPos["code"].Start, colPos["code"].Length);
                            string countryCode = code.Substring(0, 2);
                            string countryName = countries[countryCode];
                            string networkCode = code.Substring(2, 1);

                            float  lat       = Convert.ToSingle(line.Substring(colPos["lat"].Start, colPos["lat"].Length), CultureInfo.InvariantCulture);
                            float  lon       = Convert.ToSingle(line.Substring(colPos["lon"].Start, colPos["lon"].Length), CultureInfo.InvariantCulture);
                            float  elev      = Convert.ToSingle(line.Substring(colPos["elevation"].Start, colPos["elevation"].Length), CultureInfo.InvariantCulture);
                            string stateCode = (line.Substring(colPos["state"].Start, colPos["state"].Length)).Trim();

                            string stateName = String.Empty;
                            if (!string.IsNullOrEmpty(stateCode))
                            {
                                if (states.ContainsKey(stateCode))
                                {
                                    stateName = states[stateCode];
                                }
                            }

                            string name    = line.Substring(colPos["name"].Start, colPos["name"].Length);
                            string gsnflag = (line.Substring(colPos["gsnflag"].Start, colPos["gsnflag"].Length)).Trim();
                            string hcnflag = (line.Substring(colPos["hcnflag"].Start, colPos["hcnflag"].Length)).Trim();
                            string wmo     = String.Empty;
                            if (line.Length > colPos["wmo"].Start + colPos["wmo"].Length)
                            {
                                wmo = (line.Substring(colPos["wmo"].Start, colPos["wmo"].Length)).Trim();
                            }
                            int?wmoID = null;
                            if (!string.IsNullOrEmpty(wmo))
                            {
                                wmoID = Convert.ToInt32(wmo);
                            }

                            GhcnSite site = new GhcnSite
                            {
                                SiteCode    = code,
                                SiteName    = name,
                                Latitude    = lat,
                                Longitude   = lon,
                                Elevation   = elev,
                                WmoID       = wmoID,
                                State       = stateName,
                                Country     = countryName,
                                NetworkFlag = networkCode,

                                GSN      = (gsnflag == "GSN"),
                                HCNFlag  = hcnflag,
                                CoCoRaHS = (networkCode == "1"),
                                Snotel   = (networkCode == "S"),
                            };
                            siteList.Add(site);
                        }
                    }
                Console.WriteLine(String.Format("found {0} sites", siteList.Count));
                _log.LogWrite(String.Format("found {0} sites", siteList.Count));
            }
            catch (Exception ex)
            {
                _log.LogWrite("ReadSites ERROR: " + ex.Message);
            }
            return(siteList);
        }