def get_cross_section(Alat, Alon, Blat, Blon):
    # Now find cross-sections
    Ax, Ay = gridded_data.getXY(lats,lons,Alat,Alon)
    Bx, By = gridded_data.getXY(lats,lons,Blat,Blon)

    # Number of points along cross-section
    xspt = int(N.hypot(Bx-Ax,By-Ay))
    xx = N.linspace(Ay,By,xspt).astype(int)
    yy = N.linspace(Ax,Bx,xspt).astype(int)

    # Get terrain heights along this transect
    heights = topodata[xx,yy]

    # Work out distance along this cross-section in m
    xsdistance = gridded_data.xs_distance(Alat,Alon,Blat,Blon)

    xvals = N.linspace(0,xsdistance,xspt)
    xlabels = ['%3.1f' %(x/1000) for x in xvals]
    # Now plot cross-sections
    fig = plt.figure(figsize=(width,height))
    plt.plot(xvals,heights)
    delta = xspt/10
    plt.xticks(xvals[::delta],xlabels[::delta])
    plt.xlabel('Distance along cross-section (km)')
    fname = 'test1.png'
    plt.savefig(outdir+fname,bbox_inches='tight',pad_inches=0.3)
    plt.clf()
def get_map(Nlim,Elim,Slim,Wlim):
    ymax,xmax = gridded_data.getXY(lats,lons,Nlim,Elim) #Here, x is lat, y is lon
    ymin,xmin = gridded_data.getXY(lats,lons,Slim,Wlim) # Not sure why!
    terrain = topodata[xmin:xmax,ymin:ymax]
    xlat = lats[xmin:xmax]
    xlon = lons[ymin:ymax]
    #pdb.set_trace()
    return terrain,xlat,xlon
def return_shear(lev1,lev2,ncpath,ens,loc,timetuple,lats,lons,dom):
    nc = Dataset(ncpath,'r')
    time = convert_time(dom,timetuple)
    x,y,exactlat,exactlon = gridded_data.getXY(lats,lons,loc[0],loc[1])
    u = nc.variables['U'][time,:,y,x][:]
    v = nc.variables['V'][time,:,y,x][:]
    #wind = N.sqrt(u**2 + v**2)
    geopot = nc.variables['PH'][time,:,y,x][:] + nc.variables['PHB'][time,:,y,x][:]
    Z = geopot/9.81
    zAGL = Z - nc.variables['HGT'][time,y,x] # Height AGL
    
    # Compute the height, AGL, of the u/v surfaces
    zAGL_wind = N.array([(z0+z1)/2.0 for z0,z1 in zip(zAGL[:-1],zAGL[1:])])
    # Find wind at lev2 via interpolation
    lev2 *= 1000 # get into metres
    u2 = N.interp(lev2,zAGL_wind,u)
    v2 = N.interp(lev2,zAGL_wind,v)

    if lev1 == 0:
        u1 = nc.variables['U10'][time,y,x]
        v1 = nc.variables['V10'][time,y,x]
    else:
        # Find wind at lev1 via interpolation
        pass 
    shear = N.sqrt((u2-u1)**2 + (v2-v1)**2)
    return shear
def plot_T2(loc,timetuple,lats,lons,dom):
    fig = plt.figure()
    for i,ens in enumerate(ensnames):
        # Load data from nc file
        time = convert_time(dom,timetuple)
        x,y,exactlat,exactlon = gridded_data.getXY(lats,lons,loc[0],loc[1])
        ncpath = ncdir+ens+'/'+ncfname
        nc = Dataset(ncpath,'r')
        ens_T2 = nc.variables['T2'][time,y,x] - 273.15
        plt.bar(i,ens_T2)
#    if obs == 1:
#        # Include observed shear
#        D = load_sounding_data(obspath)
#        obs_T2 = D['thta'][0]
#        plt.bar(len(ensnames),obs_T2,color='black')
#        xlabelnames = ensnames + ['OBS']
#        xticklocs = [0.5+n for n in range(len(ensnames)+1)]
#    else:
    xlabelnames = ensnames
    xticklocs = [0.5+n for n in range(len(ensnames))]
    fname = '_'.join(('T2',nicetime))
    plt.gca().autoscale(enable=True,axis='y',tight=True)
    plt.xticks(xticklocs,xlabelnames)
    plt.ylabel('2m potential temperature (C)')
    fig.savefig(outdir+fname, bbox_inches='tight', pad_inches=0.5)
    fig.clf()
def plot_Q2(loc,timetuple,lats,lons,dom):
    fig = plt.figure()
    for i,ens in enumerate(ensnames):
        # Load data from nc file
        time = convert_time(dom,timetuple)
        x,y,exactlat,exactlon = gridded_data.getXY(lats,lons,loc[0],loc[1])
        ncpath = ncdir+ens+'/'+ncfname
        nc = Dataset(ncpath,'r')
        ens_Q2 = nc.variables['Q2'][time,y,x]*1000
        plt.bar(i,ens_Q2)
    if obs == 1:
        # Include observed shear
        D = load_sounding_data(obspath)
        obs_Q2 = D['mixr'][0]
        plt.bar(len(ensnames),obs_Q2,color='black')
        xlabelnames = ensnames + ['OBS']
        xticklocs = [0.5+n for n in range(len(ensnames)+1)]
    else:
        xlabelnames = ensnames
        xticklocs = [0.5+n for n in range(len(ensnames))]
    fname = '_'.join(('Q2',nicetime))
    plt.xticks(xticklocs,xlabelnames)
    plt.ylabel('Mixing ratio at surface (g kg$^{-1}$)')
    fig.savefig(outdir+fname, bbox_inches='tight', pad_inches=0.5)
    fig.clf()
ax = plt.gca()
handles, labels = ax.get_legend_handles_labels()
obsArt = plt.Line2D((0, 1), (0, 0), color="black")
wrfArt = plt.Line2D((0, 1), (0, 0), color="blue")
plt.legend([obsArt, wrfArt], ["Observed", "WRF Output"])

# plt.savefig(outdir+'kslcSkewT.png',bbox_inches='tight')

### ADD MODEL DATA
nc = openWRF(dom)  # Pick domain here
Nx, Ny, Nz, lons, lats, dx, dy, x, y = getDimensions(nc)

del x, y  # We want our own locations

time = gettime()
x, y, exactlat, exactlon = gridded_data.getXY(lats[:, Nx / 2], lons[Ny / 2, :], obs_latlon[0], obs_latlon[1])
P = nc.variables["P"][time, :, y, x] + nc.variables["PB"][time, :, y, x]
elev = nc.variables["HGT"][0, y, x]

thin_locs = gridded_data.thinned_barbs(P)

ST._windbarbs(nc, time, y, x, P, thin_locs, n=45, color="blue")
ST._temperature(nc, time, y, x, P, linestyle="solid", color="blue")
ST._dewpoint(nc, time, y, x, P, linestyle="dashed", color="blue")

plt.xticks(xticks, ["" if tick % 10 != 0 else str(tick) for tick in xticks])
plt.yticks(yticks, ytix)

plt.savefig(outdirfull + "verifSkewT_" + ens + "_" + nicetime + ".png", bbox_inches="tight", pad_inches=0.1)
plt.clf()