csv_filepath = ""

sound_directory = ""

source_directory = os.path.dirname(os.path.dirname(working_directory))

"""
Explanation: Organize recording files by site
This notebook copies sound files (recordings) into the pumilio directory structure based on a csv file containing information about the time and location of recording.
Required packages
<a href="https://github.com/pydata/pandas">pandas</a> <br />
Variable declarations
csv_filepath – path to a csv file containing information about the time and location of each recording <br />
sound_directory – path to the directory that will contain the recordings <br />
source_directory – path to the directory containing the unorganized recordings
End of explanation
"""


import pandas
import os.path
from datetime import datetime
import subprocess

"""
Explanation: Import statements
End of explanation
"""


visits = pandas.read_csv(csv_filepath)

visits = visits.sort_values(by=['Time'])

visits['ID'] = visits['ID'].map('{:g}'.format)

sites = visits['ID'].drop_duplicates().dropna().as_matrix()

for site in sites:
    path = os.path.join(working_directory, str(site))
    if os.path.exists(path):
        os.rmdir(path)
    os.mkdir(path)

for index, visit in visits.iterrows():
    try:
        dt = datetime.strptime(visit['Time'], '%Y-%m-%d %X')
    except TypeError:
        print('Time was not a string, but had a value of: "{0}"'.format(visit['Time']))
        continue
    source_file = os.path.join(source_directory, dt.strftime('%Y-%m-%d'), 'converted', '{0}.flac'.format(dt.strftime('%y%m%d-%H%M%S')))
    destination_file = os.path.join(working_directory, str(visit['ID']), '{0}.flac'.format(dt.strftime('%y%m%d-%H%M%S')))
    if os.path.exists(source_file):
        subprocess.check_output(["cp", source_file, destination_file])
        print('copying {0}'.format(dt.strftime('%y%m%d-%H%M%S')))
    else:
        print('\n')
        print('{0} does not exist!'.format(dt.strftime('%y%m%d-%H%M%S')))
        print(visit['Name'])
        print('\n')
    print('\n')
    print('done')

"""
Explanation: Organize recordings
End of explanation
"""


