private bool downloadOneStation(ECStationInfo station, out string message)
 {
     message = "";
     try
     {
         bool status = station.save(_fields, _startYear, _endYear, _path, Format, TimeInterval);
         if (!status)
         {
             message = station.Name + ": There is no data between "
                       + _startYear.ToString() + " and " + _endYear.ToString() + ". Please check output messages.";
         }
         else
         {
             message = station.WarningMessage;
             if (message.Length == 0)
             {
                 message = station.Name + ": Finished.";
             }
         }
         return(status);
     }
     catch (System.Exception e)
     {
         message = station.Name + ": " + e.Message;
         return(false);
     }
 }
 public Station(ECStationInfo info)
 {
     _id = info.ID;
     _name = info.Name;
     _province = info.Province;
     _latitude = info.Latitude.ToString();
     _longitude = info.Longitude.ToString();
     _elevation = info.Elevation.ToString();
     _hasTryToGetBasicInformation = true;
 }
 public Station(ECStationInfo info)
 {
     _id        = info.ID;
     _name      = info.Name;
     _province  = info.Province;
     _latitude  = info.Latitude.ToString();
     _longitude = info.Longitude.ToString();
     _elevation = info.Elevation.ToString();
     _hasTryToGetBasicInformation = true;
 }
Пример #4
0
 /// <summary>
 /// check if one station is already selected
 /// </summary>
 /// <param name="info"></param>
 /// <returns></returns>
 private bool isSelected(ECStationInfo info)
 {
     foreach (object b in lstSelectedStations.Items)
     {
         ECStationInfo selected = b as ECStationInfo;
         if (info.Equals(selected))
         {
             return(true);
         }
     }
     return(false);
 }
Пример #5
0
        private void updateDataAvailability(ECStationInfo info)
        {
            if (info != null)
            {
                lblStationName.Text = info.ToString();
            }
            else
            {
                lblStationName.Text = "No station is selected.";
            }

            if (info != null && info.HourlyAvailability.IsAvailable)
            {
                lblHourlyDataAvailability.Text = info.HourlyAvailability.ToTimeRangeString();
            }
            else
            {
                lblHourlyDataAvailability.Text = "HOURLY: Not Available.";
            }

            if (info != null && info.DailyAvailability.IsAvailable)
            {
                this.lblDailyDataAvailability.Text = info.DailyAvailability.ToTimeRangeString();
            }
            else
            {
                lblDailyDataAvailability.Text = "DAILY: Not Available.";
            }

            if (info != null && info.MonthlyAvailability.IsAvailable)
            {
                lblMonthlyDataAvailability.Text = info.MonthlyAvailability.ToTimeRangeString();
            }
            else
            {
                lblMonthlyDataAvailability.Text = "MONTHLY: Not Available.";
            }
        }
        private void updateDataAvailability(ECStationInfo info)
        {
            if (info != null)
                lblStationName.Text = info.ToString();
            else
                lblStationName.Text = "No station is selected.";

            if (info != null && info.HourlyAvailability.IsAvailable)
                lblHourlyDataAvailability.Text = info.HourlyAvailability.ToTimeRangeString();
            else
                lblHourlyDataAvailability.Text = "HOURLY: Not Available.";

            if (info != null && info.DailyAvailability.IsAvailable)
                this.lblDailyDataAvailability.Text = info.DailyAvailability.ToTimeRangeString();
            else
                lblDailyDataAvailability.Text = "DAILY: Not Available.";

            if (info != null && info.MonthlyAvailability.IsAvailable)
                lblMonthlyDataAvailability.Text = info.MonthlyAvailability.ToTimeRangeString();
            else
                lblMonthlyDataAvailability.Text = "MONTHLY: Not Available.";
        }
 /// <summary>
 /// check if one station is already selected
 /// </summary>
 /// <param name="info"></param>
 /// <returns></returns>
 private bool isSelected(ECStationInfo info)
 {
     foreach (object b in lstSelectedStations.Items)
     {
         ECStationInfo selected = b as ECStationInfo;
         if (info.Equals(selected)) return true;
     }
     return false;
 }
        public FrmDownloadEnvrionmentCanadaClimateData()
        {
            InitializeComponent();

            //time
            txtStartYear.TextChanged += (s, ee) => { int.TryParse(txtStartYear.Text, out _startYear); };
            txtEndYear.TextChanged   += (s, ee) => { int.TryParse(txtEndYear.Text, out _endYear); };

            //stations
            bDefineStations.Click += (s, ee) =>
            {
                FrmDefineStations frm = new FrmDefineStations();

                //remove handler
                if (_stations != null && _stations.Count > 0)
                {
                    foreach (ECStationInfo info in _stations)
                    {
                        info.ProgressChanged -= onStaionClimateDataDownloadingProgressChanged;
                    }
                }

                //set to frm
                frm.SelectedStations = _stations;
                frm.DataType         = TimeInterval;

                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    SelectedStations = frm.SelectedStations;
                }
                else
                {
                    SelectedStations = SelectedStations; //for case when user doesn't click OK to add event handler.
                }
            };
            bLoadSavedStations.Click += (s, ee) =>
            {
                if (dlgLoadSaveStations.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    SelectedStations = EC.ReadStations(dlgLoadSaveStations.FileName);
                    MessageBox.Show(SelectedStations.Count.ToString() + " stations are loaded.", "ECReader");
                }
            };

            lblLatestVersion.LinkClicked   += (s, ee) => { System.Diagnostics.Process.Start("http://wp.me/s2CzBq-325"); };
            lblStationLocation.LinkClicked += (s, ee) => { System.Diagnostics.Process.Start("http://wp.me/p2CzBq-68"); };
            lblFeedback.LinkClicked        += (s, ee) =>
            {
                try
                {
                    System.Diagnostics.Process.Start("mailto:[email protected]");
                }
                catch
                {
                    System.Windows.Forms.Clipboard.SetText("*****@*****.**");
                    showInformationWindow("My email address has been copyied to clipboard.");
                }
            };

            //the output time interval
            this.rdbTimeIntervalDaily.CheckedChanged   += (s, ee) => { this.updateTimeIntervalSelectionControl(); };
            this.rdbTimeIntervalHourly.CheckedChanged  += (s, ee) => { this.updateTimeIntervalSelectionControl(); };
            this.rdbTimeIntervalMonthly.CheckedChanged += (s, ee) => { this.updateTimeIntervalSelectionControl(); };

            //the output format
            this.rdbFormatArcSWATDbf.CheckedChanged += (s, ee) => { updateFormatSelectionControl(); };
            this.rdbFormatArcSWATTxt.CheckedChanged += (s, ee) => { updateFormatSelectionControl(); };
            this.rdbFormatFreeCSV.CheckedChanged    += (s, ee) => { updateFormatSelectionControl(); };
            this.rdbFormatFreeText.CheckedChanged   += (s, ee) => { updateFormatSelectionControl(); };
            this.rdbFormatSWATInput.CheckedChanged  += (s, ee) => { updateFormatSelectionControl(); };

            //select output fields
            listFields.ItemCheck += (s, ee) =>
            {
                List <int> f = new List <int>();

                foreach (int index in listFields.CheckedIndices)
                {
                    if (index != ee.Index)
                    {
                        f.Add(DataFieldIndex[index]);
                    }
                }
                if (ee.NewValue == CheckState.Checked)
                {
                    f.Add(DataFieldIndex[ee.Index]);
                }

                _fields = null;

                if (f.Count > 0)
                {
                    _fields = f.ToArray();
                }
            };
            bSelectAll.Click += (s, ee) =>
            {
                for (int i = 0; i < listFields.Items.Count; i++)
                {
                    listFields.SetItemCheckState(i, CheckState.Checked);
                }
            };

            //select output folder
            bBrowseOutput.Click +=
                (s, ee) =>
            {
                FolderBrowserDialog dlg = new FolderBrowserDialog();
                dlg.SelectedPath = _path;
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    txtPath.Text = dlg.SelectedPath;
                    _path        = dlg.SelectedPath;
                }
            };
            txtPath.TextChanged += (s, ee) => { if (System.IO.Directory.Exists(txtPath.Text))
                                                {
                                                    _path = txtPath.Text;
                                                }
                                                else
                                                {
                                                    txtPath.Text = _path;
                                                } };


            //download button
            bDownload.Click += (s, ee) =>
            {
                if (backgroundWorker1.IsBusy)
                {
                    showInformationWindow("Please wait current process to finish.");
                    return;
                }

                //check stations
                if (_stations == null || _stations.Count == 0)
                {
                    showInformationWindow("Please define stations first.");
                    return;
                }

                //check field list
                if (listFields.Enabled && _fields == null)
                {
                    showInformationWindow("Please select output fields.");
                    return;
                }

                if (Format == FormatType.SWAT_TEXT)
                {
                    showInformationWindow("Coming soon. Stay tuned:)");
                    return;
                }

                if (_endYear < _startYear)
                {
                    showInformationWindow("The end year couldn't be earlier than start year.");
                    return;
                }

                progressBar1.Maximum   = (_endYear - _startYear + 1) * 2;
                _maxValueofProgressBar = progressBar1.Maximum;

                backgroundWorker1.RunWorkerAsync();
            };

            //worker
            backgroundWorker1.ProgressChanged += (s, ee) =>
            {
                if (richTextBox1.Text.Length > 0)
                {
                    richTextBox1.AppendText(Environment.NewLine);
                }
                richTextBox1.AppendText(ee.UserState.ToString());
                richTextBox1.SelectionStart = richTextBox1.Text.Length;
                richTextBox1.ScrollToCaret();

                progressBar1.Value =
                    ee.ProgressPercentage > progressBar1.Maximum ? progressBar1.Maximum : ee.ProgressPercentage;
            };
            backgroundWorker1.RunWorkerCompleted += (s, ee) =>
            {
                //this.progressBar1.Value = this.progressBar1.Maximum;
                //this.richTextBox1.Text += "finished";
            };
            backgroundWorker1.DoWork +=
                (s, ee) =>
            {
                backgroundWorker1.ReportProgress(0, "--------------------------------------------");

                if (_isDownloadAllStations)     //download all stations, update ecstations.csv
                {
                    backgroundWorker1.ReportProgress(0, "Downloading all EC stations.");
                    EC.RetrieveAndSaveAllStations(
                        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\ecstations.csv",
                        backgroundWorker1);
                    backgroundWorker1.ReportProgress(100,
                                                     "Downloading all EC stations finished. Location: " + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\ecstations.csv");
                }
                else    //download daily, hourly data
                {
                    createGageLocationFile();
                    string msg = "";
                    if (_stations.Count == 1)            //one station
                    {
                        ECStationInfo station = _stations[0];
                        downloadOneStation(station, out msg);
                        backgroundWorker1.ReportProgress(_maxValueofProgressBar, msg);
                        showInformationWindow(msg);
                    }
                    else                        //multiple stations
                    {
                        //needs to write into one file
                        if (Format == FormatType.SWAT_TEXT)
                        {
                        }
                        else
                        {
                            bool          totalStatus = false;
                            StringBuilder sb          = new StringBuilder();
                            sb.AppendLine("------------ Summary ------------");
                            foreach (ECStationInfo onestation in _stations)
                            {
                                bool status = downloadOneStation(onestation, out msg);
                                totalStatus |= status;

                                if (msg.Length > 0)
                                {
                                    sb.AppendLine(msg);
                                }
                            }

                            showInformationWindow("Finished." + (totalStatus ? "" : "Please check messages in the message window."));
                            backgroundWorker1.ReportProgress(_maxValueofProgressBar, sb.ToString());
                        }
                    }
                }
            };

            //open the output folder
            bOpen.Click += (s, ee) => { if (System.IO.Directory.Exists(_path))
                                        {
                                            System.Diagnostics.Process.Start(_path);
                                        }
            };

            this.FormClosing += (s, ee) =>
            {
                EC.SaveStations(SelectedStations);
            };
        }
 private bool downloadOneStation(ECStationInfo station, out string message)
 {
     message = "";
     try
     {
         bool status = station.save(_fields, _startYear, _endYear, _path, Format,TimeInterval);
         if (!status)
             message = station.Name + ": There is no data between "
                 + _startYear.ToString() + " and " + _endYear.ToString() + ". Please check output messages.";
         else
         {
             message = station.WarningMessage;
             if (message.Length == 0)
                 message = station.Name + ": Finished.";
         }
         return status;
     }
     catch (System.Exception e)
     {
         message = station.Name + ": " + e.Message;
         return false;
     }
 }
Пример #10
0
        private void FrmDefineStations_Load(object sender, EventArgs e)
        {
            lsvResultByName.SelectedIndexChanged += (s, ee) =>
            {
                if (lsvResultByName.SelectedItems.Count == 0)
                {
                    return;
                }
                ECStationInfo info = lsvResultByName.SelectedItems[0].Tag as ECStationInfo;
                updateDataAvailability(info);
            };
            lsvResultByName.MouseDoubleClick += (s, ee) =>
            {
                if (lsvResultByName.SelectedItems.Count == 0)
                {
                    return;
                }
                ECStationInfo info = lsvResultByName.SelectedItems[0].Tag as ECStationInfo;
                if (!isSelected(info))
                {
                    lstSelectedStations.Items.Add(info);
                }
                bRemoveAllSelected.Enabled = lstSelectedStations.Items.Count > 0;
            };

            lstSelectedStations.SelectedIndexChanged += (s, ee) =>
            {
                if (lstSelectedStations.SelectedItem == null)
                {
                    return;
                }
                updateDataAvailability(lstSelectedStations.SelectedItem as ECStationInfo);
            };
            lstSelectedStations.MouseDoubleClick += (s, ee) =>
            {
                if (lstSelectedStations.SelectedItem == null)
                {
                    return;
                }
                lstSelectedStations.Items.RemoveAt(lstSelectedStations.SelectedIndex);
                bRemoveAllSelected.Enabled = lstSelectedStations.Items.Count > 0;
            };
            bAddAll.Click += (s, ee) =>
            {
                if (lsvResultByName.Items.Count == 0)
                {
                    return;
                }
                foreach (ListViewItem item in lsvResultByName.Items)
                {
                    ECStationInfo info = item.Tag as ECStationInfo;
                    if (!isSelected(info))
                    {
                        lstSelectedStations.Items.Add(info);
                    }
                }
                bRemoveAllSelected.Enabled = lstSelectedStations.Items.Count > 0;
            };

            bRemoveAllSelected.Click += (s, ee) => { lstSelectedStations.Items.Clear(); };
            bMoveDown.Click          += (s, ee) => { moveSelectedStation(false); };
            bMoveUp.Click            += (s, ee) => { moveSelectedStation(true); };

            bSearch.Click += (s, ee) =>
            {
                lsvResultByName.Items.Clear();

                string sql = SearchSQL;
                if (sql.Length == 0)
                {
                    bAddAll.Text    = "Use All";
                    bAddAll.Enabled = false;
                    return;
                }

                List <ECStationInfo> stations = EC.Search(sql);
                foreach (ECStationInfo info in stations)
                {
                    ListViewItem item = lsvResultByName.Items.Add(info.Name);
                    item.SubItems.Add(info.Province);
                    item.Tag = info;
                }

                bAddAll.Enabled = stations.Count > 0;
                bAddAll.Text    = "Use All" + (stations.Count > 0 ? " " + stations.Count.ToString() + " stations" : "");
            };
            bSaveSelectedStations.Click += (s, ee) =>
            {
                dlgSaveSelectedStations.FileName = "ECReader_Stations_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv";
                if (dlgSaveSelectedStations.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    try
                    {
                        EC.SaveStations(dlgSaveSelectedStations.FileName, SelectedStations);
                        MessageBox.Show("Saved!", "ECReader");
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.Message, "ECReader");
                    }
                }
            };

            updateDataAvailability(null);

            if (cmbDataType.SelectedIndex == -1)
            {
                cmbDataType.SelectedIndex = 0;
            }
            cmbProvince.SelectedIndex = 0;
            nudEndYear.Minimum        = 1840;
            nudEndYear.Maximum        = DateTime.Now.Year;
            nudEndYear.Value          = nudEndYear.Maximum;
            nudStartYear.Minimum      = 1840;
            nudStartYear.Maximum      = DateTime.Now.Year;
            nudStartYear.Value        = 1840;
        }