indexfile = "./datafiles/index_latest.txt"

"""
Explanation: This notebook shows how to use an index file.<br/>
This example uses the index file from the Mediterranean Sea region (INSITU_MED_NRT_OBSERVATIONS_013_035) corresponding to the latest data.<br/>
If you download the same file, the results will be slightly different from what is shown here.
End of explanation
"""


import numpy as np
dataindex = np.genfromtxt(indexfile, skip_header=6, unpack=True, delimiter=',', dtype=None, \
              names=['catalog_id', 'file_name', 'geospatial_lat_min', 'geospatial_lat_max',
                     'geospatial_lon_min', 'geospatial_lon_max',
                     'time_coverage_start', 'time_coverage_end', 
                     'provider', 'date_update', 'data_mode', 'parameters'])

"""
Explanation: To read the index file (comma separated values), we will try with the genfromtxt function.
End of explanation
"""


lon_min = dataindex['geospatial_lon_min']
lon_max = dataindex['geospatial_lon_max']
lat_min = dataindex['geospatial_lat_min']
lat_max = dataindex['geospatial_lat_max']

"""
Explanation: Map of observations
End of explanation
"""


%matplotlib inline
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

"""
Explanation: We import the modules necessary for the plot.
End of explanation
"""


m = Basemap(projection='merc', llcrnrlat=30., urcrnrlat=46.,
            llcrnrlon=-10, urcrnrlon=40., lat_ts=38., resolution='i')
lonmean, latmean = 0.5*(lon_min + lon_max), 0.5*(lat_min + lat_max)
lon2plot, lat2plot = m(lonmean, latmean)

"""
Explanation: We create the projection, centered on the Mediterranean Sea in this case.
End of explanation
"""


fig = plt.figure(figsize=(10,8))
m.plot(lon2plot, lat2plot, 'ko', markersize=2)
m.drawcoastlines(linewidth=0.5, zorder=3)
m.fillcontinents(zorder=2)

m.drawparallels(np.arange(-90.,91.,2.), labels=[1,0,0,0], linewidth=0.5, zorder=1)
m.drawmeridians(np.arange(-180.,181.,3.), labels=[0,0,1,0], linewidth=0.5, zorder=1)
plt.show()

"""
Explanation: And we create a plot showing all the data locations.
End of explanation
"""


box = [12, 15, 32, 34]

"""
Explanation: Selection of a data file based on coordinates
Let's assume we want to have the list of files corresponding to measurements off the northern of Lybia.<br/>
We define a rectangular box containg the data:
End of explanation
"""


import numpy as np
goodcoordinates = np.where( (lonmean>=box[0]) & (lonmean<=box[1]) & (latmean>=box[2]) & (latmean<=box[3]))
print goodcoordinates

"""
Explanation: then we look for the observations within this box:
End of explanation
"""


goodfilelist = dataindex['file_name'][goodcoordinates]
print goodfilelist

"""
Explanation: The generation of the file list is direct:
End of explanation
"""


m2 = Basemap(projection='merc', llcrnrlat=32., urcrnrlat=34.,
            llcrnrlon=12, urcrnrlon=15., lat_ts=38., resolution='h')
lon2plot, lat2plot = m2(lonmean[goodcoordinates], latmean[goodcoordinates])

fig = plt.figure(figsize=(10,8))
m2.plot(lon2plot, lat2plot, 'ko', markersize=4)
m2.drawcoastlines(linewidth=0.5, zorder=3)
m2.fillcontinents(zorder=2)

m2.drawparallels(np.arange(-90.,91.,0.5), labels=[1,0,0,0], linewidth=0.5, zorder=1)
m2.drawmeridians(np.arange(-180.,181.,0.5), labels=[0,0,1,0], linewidth=0.5, zorder=1)
plt.show()

"""
Explanation: According to the file names, we have 7 profiling drifters available in the area. <br/>
To check, we replot the data only in the selected box:
End of explanation
"""


