# Load the needed packages
import numpy as np
import matplotlib.pyplot as plt

import awot
from awot.graph.common import create_basemap
from awot.graph import FlightLevel

%matplotlib inline

"""
Explanation: <h2>Load and plot a T-28 NetCDF file</h2>
<br>
<i>Files can be found here
<br>
They follow RAF nimbus conventions</i>
End of explanation
"""


# Set the path for data file
flname="/Users/guy/data/t28/jpole/T28_JPOLE2003_800.nc"

"""
Explanation: Supply user information
End of explanation
"""


proj = 'cea'
Wbarb_Spacing = 300 # Spacing of wind barbs along flight path (sec)

# Choose the X-axis time step (in seconds) where major labels will be
XlabStride = 3600

# Should landmarks be plotted? [If yes, then modify the section below
Lmarks=False

# Optional variables that can be included with AWOT
# Start and end times for track in Datetime instance format
#start_time = "2003-08-06 00:00:00"
#end_time = "2003-08-06 23:50:00"

corners = [-96., 34., -98., 36.,]

"""
Explanation: <li>Set up some characteristics for plotting. 
<li>Use Cylindrical Equidistant Area map projection.
<li>Set the spacing of the barbs and X-axis time step for labels.
<li>Set the start and end times for subsetting.
End of explanation
"""


fl = awot.io.read_netcdf(fname=flname, platform='t28', time_var="Time")

fl.keys()

"""
Explanation: Read in flight data<br>
NOTE: At time or writing this it is required that the time_var argument be provided to make the read function work properly. This may change in the future, but time variables are not standard even among RAF Nimbus guidelines.
End of explanation
"""


print(fl['latitude']['data'].min(), fl['latitude']['data'].max())
fl['latitude']['data'][:] = np.ma.masked_equal(fl['latitude']['data'][:], 0.)
fl['longitude']['data'][:] = np.ma.masked_equal(fl['longitude']['data'][:], 0.)
print(fl['latitude']['data'].min(), fl['latitude']['data'].max())
print(fl['longitude']['data'].min(), fl['longitude']['data'].max())
print(fl['altitude']['data'].max())

fig, ax = plt.subplots(1, 1, figsize=(9, 9))

# Create the basemap
bm = create_basemap(corners=corners, proj=proj, resolution='l', area_thresh=1.,ax=ax)
bm.drawcounties()
# Instantiate the Flight plotting routines
flp = FlightLevel(fl, basemap=bm)
flp.plot_trackmap(
#                 start_time=start_time, end_time=end_time,
                 color_by_altitude=True, track_cmap='spectral',
                 min_altitude=50., max_altitude= 5000.,
                 addlegend=True, addtitle=False)

"""
Explanation: Create the track figure for this flight, there appear to be some bunk data values in lat/lon
End of explanation
"""


