#GUI_WeatherPrep.py created by Jay Heppler (Davey Tree) with assist from Li Zhang and Ted Endreny (SUNY ESF)
#Code provides a GUI to orchestrate key steps in i-Tree Tools HydroPlus weather retrieval and processing:
#   0. Code designed to work with legacy NOAA NCEI ISD (Integrated Surface Dataset) and active GHCNh (Global Historical Climatology Network hourly) data
#   1. Find NOAA NCEI weather stations based on country, state, county, place and proximity search of database latitude and longitude
#   2. Use station associated 5-digit WBAN or 6-digit WMO or USAF to generate GHCN ID or find ISD
#   2a. GHCN ID for sites with WBAN codes: #<COUNTRY CODE>W000<WBAN>, For Naples, Italy with WBAN = 34113, ITW00034113
#   2b. GHCN ID for for sites without WBAN codes but with WMO or USAF codes: #<COUNTRY CODE>0000<WMO>, For Cagliari, Italy with WMO 16560, GIT000016560
#   3. Retrieve NOAA NCEI weather data from GHCNh via HTTMS or from ISD via FTP
#   4. Convert NOAA NCEI weather data from GHCNh psv to csv or from ISD gunzip to txt with ishapp2.exe
#   5. Process NOAA NCEI weather data to data formatted for HydroPlus tools




How to Use: 
1. Launch WeatherPrep_GUI.exe
2. Enter the Nation first, followed by the State, County, and Place. Locations are retrieved periodically from https://database.itreetools.org/#/locations
3. Select a weather station based on your years of interest
4. Select Hourly or Sub-Hourly format 
--For Hourly: Enter Start and End YYYY format 
--For Sub-Hourly: Enter Start and End YYYYMMDDHH. The start and end YYYY must be the same. 
5. Click 'Process Data'
6. Check your WeatherPrep/Outputs/ folder for results
7. To process more locations or start over, click 'Reset Location'

How to Run WeatherPrep_GUI.py: 
1. (Optional) create python venv in WeatherPrep_GUI folder 
2. Enter in python: "pip install -r path/to/requirements.txt" to install necessary libraries 
3. Launch WeatherPrep_GUI.py
& "C:\iTree\WeatherPrep_GUI_build\venv_py38\Scripts\python.exe" WeatherPrep_GUI.py

Required Libraries:
lxml==4.9.1
numpy==1.22.4
numpy==1.20.1
pandas==1.4.4
PyQt5==5.15.10
PyQt5_sip==12.12.1


Editing the Data:
1. Location - Locations are retrieved from resources/itree-locations.csv. To add a location that is in the i-Tree database but not in the document, the following fields are required: tertiary partition (place), secondary partition (county), primary partition (state), nation, latitude, and longitude. 
2. Weather Station - Weather stations are retrieved from resources/isd-history.csv. The most recent isd-history.csv can be obtained from https://noaa-isd-pds.s3.amazonaws.com/index.html

Troubleshooting:
If WeatherPrep does not run successfully, attempt to run from your command prompt to examine the output from WeatherPrep. A different station may need to be selected, especially if the file size is <1,000 KB, due to poor data completeness. 

Contact: For issues or bug reports with WeatherPrep_GUI, email jay.heppler@davey.com
