from planet4 import plotting, catalog_production

rm = catalog_production.ReleaseManager('v1.0b4')

fans = rm.read_fan_file()
blotches = rm.read_blotch_file()

cols = ['angle', 'distance', 'tile_id', 'marking_id',
       'obsid', 'spread', 
       'l_s', 'map_scale', 'north_azimuth',
       'PlanetographicLatitude',
       'PositiveEast360Longitude']

fans.head()

fans[cols].rename(dict(PlanetographicLatitude='Latitude',
                       PositiveEast360Longitude='Longitude'),
                  axis=1).head()

fans.columns

fan_counts = fans.groupby('tile_id').size()
blotch_counts = blotches.groupby('tile_id').size()

ids = fan_counts[fan_counts > 4][fan_counts < 10].index

pure_fans = list(set(ids) - set(blotches.tile_id))

len(ids)

len(pure_fans)

rm.savefolder

%matplotlib ipympl

plt.close('all')

from ipywidgets import interact

id_ = pure_fans[51]

def do_plot(i):
    id_ = pure_fans[i]
    plotting.plot_image_id_pipeline(id_, datapath=rm.savefolder, via_obsid=False,
                                    save=True, figsize=(8,4),
                                    saveroot='/Users/klay6683/Dropbox/src/p4_paper1/figures')

interact(do_plot, i=48)

from planet4 import markings

def do_plot(i=0):
    plt.close('all')
    fig, ax = plt.subplots()
    markings.ImageID(pure_fans[i]).show_subframe(ax=ax)
    ax.set_title(pure_fans[i])

interact(do_plot, i=(0,len(pure_fans),1))

markings.ImageID('6n3').image_name

from planet4 import markings

markings.ImageID(pure_fans[15]).image_name

plotting.plot_raw_fans(id_)

plotting.plot_finals(id_, datapath=rm.savefolder)

"""
Explanation: <h1>Table of Contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#largest-number-of-fans-and-blotches" data-toc-modified-id="largest-number-of-fans-and-blotches-1"><span class="toc-item-num">1&nbsp;&nbsp;</span>largest number of fans and blotches</a></span></li><li><span><a href="#parameter_scan" data-toc-modified-id="parameter_scan-2"><span class="toc-item-num">2&nbsp;&nbsp;</span>parameter_scan</a></span></li><li><span><a href="#pipeline-examples" data-toc-modified-id="pipeline-examples-3"><span class="toc-item-num">3&nbsp;&nbsp;</span>pipeline examples</a></span></li><li><span><a href="#ROIs-map" data-toc-modified-id="ROIs-map-4"><span class="toc-item-num">4&nbsp;&nbsp;</span>ROIs map</a></span></li></ul></div>
End of explanation
"""


g_id = fans.groupby('tile_id')

g_id.size().sort_values(ascending=False).head()

blotches.groupby('tile_id').size().sort_values(ascending=False).head()

plotting.plot_finals('6mr', datapath=rm.savefolder)

plotting.plot_finals('7t9', datapath=rm.savefolder)

"""
Explanation: largest number of fans and blotches
End of explanation
"""


from planet4 import dbscan

db = dbscan.DBScanner()

import seaborn as sns
sns.set_context('paper')

db.parameter_scan(id_, 'fan', [0.13, 0.2], [10, 20, 30], size_to_scan='small')

"""
Explanation: parameter_scan
End of explanation
"""


plotting.plot_image_id_pipeline('bk7', datapath=rm.savefolder, via_obsid=False,
                                    save=True, figsize=(8,4), do_title=False,
                                    saveroot='/Users/klay6683/Dropbox/src/p4_paper1/figures')

plotting.plot_image_id_pipeline('ops', datapath=rm.savefolder, via_obsid=False,
                                    save=True, figsize=(8,4), do_title=False,
                                    saveroot='/Users/klay6683/Dropbox/src/p4_paper1/figures')

plotting.plot_image_id_pipeline('b0t', datapath=rm.savefolder, via_obsid=False,
                                    save=True, figsize=(8,4), do_title=False,
                                    saveroot='/Users/klay6683/Dropbox/src/p4_paper1/figures')

"""
Explanation: pipeline examples
End of explanation
"""


from astropy.table import Table
tab = Table.read('/Users/klay6683/Dropbox/src/p4_paper1/rois_table.tex')

rois = tab.to_pandas()

rois.drop(0, inplace=True)

rois.head()

rois.columns = ['Latitude', 'Longitude', 'Informal Name', '# Images (MY29)', '# Images (MY30)'] 

rois.head()

rois.to_csv('/Users/klay6683/Dropbox/data/planet4/p4_analysis/rois.csv')

"""
Explanation: ROIs map
End of explanation
"""


