#target_point_density = np.array([5., 10., 20., 30., 40.])
target_pulse_density = np.array([25])
keys_2 = ['25']
#target_points = area*target_point_density
target_shots = (area * target_pulse_density).astype('int')

#---------------------------------------------------------------------------------------------------------------
# Load the data etc.
mask = plot_coordinates['plot'] == plot
affine = lstsq.least_squares_affine_matrix(plot_coordinates['x'][mask],
                                           plot_coordinates['y'][mask],
                                           plot_coordinates['x_prime'][mask],
                                           plot_coordinates['y_prime'][mask])
plot_bbox = np.array(
    lstsq.apply_affine_transformation(np.array(
        [-20., 120.,
         120., -20.]), np.array([120., 120., -20., -20.]), affine)).transpose(
         )  # simple square bounding box applied for all sensitivity analyses

pts, starting_ids, trees = io.load_lidar_file_by_polygon(
    las_file, plot_bbox, filter_by_first_return_location=True)
pts[pts[:, 2] < 0, 2] = 0
n_returns = pts.shape[0]
shots = np.unique(pts[:, 6])
#n_shots=shots.size
#target_shots = (np.ceil(target_points*n_shots/float(n_returns))).astype('int')

subplots = {}
PAD_profiles_MH = {}
PAD_profiles_MH_wt = {}
PAD_profiles_rad1 = {}
#plot = 'Belian'
#plot = 'E'
#plot = 'B North'
plot = sys.argv[1]
if plot == 'BNorth':
    plot = 'B North'
print(plot)
plot_width = 100.
sample_res = 0.5

#---------------------------------------------------------------------------------------------------------------
# Load the data etc.
print(plot_coordinates['plot'])
mask = plot_coordinates['plot']==plot
affine=lstsq.least_squares_affine_matrix(plot_coordinates['x'][mask],plot_coordinates['y'][mask],plot_coordinates['x_prime'][mask],plot_coordinates['y_prime'][mask])
plot_bbox = np.array(lstsq.apply_affine_transformation(np.array([0.,100.,100.,0.]),np.array([100.,100.,0.,0.]),affine)).transpose() # simple square bounding box applied for all sensitivity analyses

pts, starting_ids, trees = io.load_lidar_file_by_polygon(las_file,plot_bbox,filter_by_first_return_location=True)
pts[pts[:,2]<0,2]=0
n_returns = pts.shape[0]


# Loop through all the spatial scales of interest
print("generating sample grid")
# Now create the subplot grids
rows = int(plot_width/sample_res)
cols = int(plot_width/sample_res)
x=np.arange(0,plot_width+sample_res,sample_res)
y=np.arange(0,plot_width+sample_res,sample_res)

xv,yv=np.asarray(np.meshgrid(x,y))
datatype = {'names': ('plot', 'x', 'y', 'x_prime', 'y_prime'), 'formats': ('<U16','f16','f16','f16','f16')}
plot_coordinates = np.genfromtxt(gps_pts_file, skip_header = 0, delimiter = ',',dtype=datatype)

heights = np.arange(0.,80)+1
plot_width = 100.

#---------------------------------------------------------------------------------------------------------------
# Generate North arrows
n_arrow={}
n_arrow_text={}
for plot in (plots):
    if plot in plot_coordinates['plot']:
        mask = plot_coordinates['plot']==plot
        affine=lstsq.least_squares_affine_matrix(plot_coordinates['x_prime'][mask],plot_coordinates['y_prime'][mask],
                                                plot_coordinates['x'][mask],plot_coordinates['y'][mask])
        n_arrow[plot] = np.array(lstsq.apply_affine_transformation(np.array([0.,0.]),
                                            np.array([0.,15.]),affine)).transpose() # simple square bounding box applied for all sensitivity analyses
        n_arrow_text[plot] = np.array(lstsq.apply_affine_transformation(np.array([0.,0.]),
                                            np.array([0.,18.]),affine)).transpose() # simple square bounding box applied for all sensitivity analyses
    else:
        n_arrow[plot] = np.array(([0.,0.],[0.,15.])) # simple square bounding box applied for all sensitivity analyses
        n_arrow_text[plot] = np.array(([0.,0.],[0.,18]))

    shift_x = np.max(n_arrow[plot][:,0])-90
    shift_y = np.max(n_arrow[plot][:,1])-90
    n_arrow[plot][:,0]-=shift_x
    n_arrow[plot][:,1]-=shift_y
    n_arrow_text[plot][:,0]-=shift_x
    n_arrow_text[plot][:,1]-=shift_y

# Plot up the figure
labels = ['OGF (MLA-01)','MLF (SAF-03)','HLF (SAF-02)','OP']
area = 10.**4
target_point_density = np.array([5., 10., 15., 20., 25., 30., 40.])
keys_2 = ['5', '10', '15', '20', '25', '30', '40']
target_points = area * target_point_density

#---------------------------------------------------------------------------------------------------------------
# Load the data etc.
mask = plot_coordinates['plot'] == alt_plot
affine = lstsq.least_squares_affine_matrix(plot_coordinates['x'][mask],
                                           plot_coordinates['y'][mask],
                                           plot_coordinates['x_prime'][mask],
                                           plot_coordinates['y_prime'][mask])
plot_bbox = np.array(
    lstsq.apply_affine_transformation(plot_coordinates['x'][mask],
                                      plot_coordinates['y'][mask],
                                      affine)).transpose()

pts, starting_ids, trees = io.load_lidar_file_by_polygon(
    las_file, plot_bbox, filter_by_first_return_location=True)
n_returns = pts.shape[0]
shots = np.unique(pts[:, -1])
n_shots = shots.size
target_shots = (np.ceil(target_points * n_shots /
                        float(n_returns))).astype('int')
shots = np.unique(pts[:, -1])

subplots = {}
PAD_profiles_MH = {}
PAD_profiles_rad1 = {}
PAD_profiles_rad2 = {}