"""
This program was written by Remy. It seems to iterate over each observation
for each user. It's trying to get a AWS key and generate zip the files but 
must be failing since there are no zip files.

Since it is unclear what the program does and the process runs well without
it the program has been deactivated. - mgb 240227

"""

import os
import boto3
import pymongo
import botocore
import string
import zipfile
import datetime

# connect to S3
s3 = boto3.resource('s3')

client = pymongo.MongoClient()

observations = client.seo.observations

# walk over usernames
for user in next(os.walk('.'))[1]:

    if 'gladders' in user:
        continue

    if 'galax' in user:
        continue

    print('Processing user = %s' % user)
    # walk over their observations
    for obs in next(os.walk(user))[1]:
        print('    Processing observation = %s' % obs)

        # key to store in s3
        key = (f'{user}/{obs}').encode('ascii', errors='ignore').decode()

        # check for old file format
        if obs[0:4] == '2018':
            pass
        else:
            pass

        try:
            s3.Object('seo-queue', f'{key}.zip'.replace('+', '')).load()
            print(f'{key}.zip exists. Skipping...')
            continue
        except botocore.exceptions.ClientError as e:
            if e.response['Error']['Code'] == "404":
                # file not found
                pass
            else:
                print(f'Unable to load key {key}')
                continue

        try:
            key.encode('ascii')
        except:
            print(f'Unable to ascii convert key {key}')
            continue

        # check that observation is valid
        if not os.path.isdir(key+'/raw/science'):
            print(f'Unable to find {key}/raw/science')
            continue

        print(f'Processing {key}')

        # create a zip file for this observation
        zfile = zipfile.ZipFile(f'{key}.zip', 'w')

        # iterate over science, dark, bias
        try:
            for imtype in ['science', 'dark', 'bias']:
                # iterate over files in directory
                directory = os.path.join(key, 'raw', imtype)
                files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
                for f in files:
                    zfile.write('/'.join([key, 'raw', imtype, f]), '/'.join([obs, 'raw', imtype, f]))
        except:
            continue

        # close and transfer
        zfile.close()
        s3.Object('seo-queue', f'{key}.zip'.replace('+', '')).put(Body=open(f'{key}.zip', 'rb'))
