faysalmirmd

Gage Download By State

Mar 22nd, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1.  public void DownloadStationsByState(string[] stateCodes, string localFilePath, StationFields stationFields = null)
  2.         {
  3.             this.localFilePath = localFilePath;
  4.             DownloadedStations = new List<Station>();
  5.             List<string> columnNames = stationFields == null ? StationFields.DefaultStationFields : stationFields;
  6.  
  7.             string url =
  8.                 "http://nwis.waterdata.usgs.gov/nwis/peak?" +
  9.                 string.Join("&", stateCodes.Select(stateCode => "state_cd=" + stateCode).ToArray()) +  // e.g. state_cd=va&state_cd=wa
  10.                 "&coordinate_format=decimal_degrees" +
  11.                 "&sort_key=site_no&group_key=NONE&format=sitefile_output" +
  12.                 "&sitefile_output_format=rdb" + // rdb = plain text in CSV
  13.  
  14.                 // Column names
  15.                 string.Concat(columnNames.Select(field => "&column_name=" + field).ToArray()) +
  16.  
  17.                 "&set_logscale_y=1&date_format=YYYY-MM-DD&rdb_compression=file&hn2_compression=file" +
  18.                 "&list_of_search_criteria=lat_long_bounding_box";
  19.  
  20.            DownloadStations(url, localFilePath);
  21.         }
  22.  
  23.         private void DownloadStations(string url, string localFilePath)
  24.         {
  25.             webClient = new WebClient();
  26.             webClient.DownloadFileCompleted -= WebClientOnStationDownloadCompleted;
  27.             webClient.DownloadFileCompleted += WebClientOnStationDownloadCompleted;
  28.  
  29.             IsWebClientCancelled = false;
  30.             IsWebClientBusy = true;
  31.  
  32.             webClient.DownloadFileAsync(new Uri(url), localFilePath);
  33.  
  34.         }
Add Comment
Please, Sign In to add comment