<commit_before># -*- coding: utf-8 -*-

import csv, json, os, time
try:
    import requests
except ImportError:
    pass

def set_default(obj):
    if isinstance(obj, set):
        return list(obj)
    raise TypeError

def touch(f_path='index.html', times=None):
    with open(f_path, 'a'):
        os.utime(f_path, times)

def get_filenames(f_dir, prefix='', suffix=''):
    f_names = []
    for r,d,files in os.walk(f_dir):
        for f in files:
            if f.startswith(prefix) and f.endswith(suffix):
                f_names.append('{}/{}'.format(r, f))
    return f_names

def file_exists(f_path):
    if os.path.isfile(f_path):
        return True
    return False

def get_json(url, verbose=True):
    if verbose:
        print('Fetching: {}'.format(url))
    r = requests.get(url)
    if r.status_code != 200:
        print('Error: {}.'.format(r.status_code))
    return r.json()

def read_csv(f_path, fieldnames=[], strip_header=False):
    with open(f_path) as f:
        # optionally strip whitespace from header values (e.g. '  depth' -> 'depth')
        if not fieldnames and strip_header:
            fieldnames = [h.strip() for h in f.next().split(',')]
        if not fieldnames:
            reader = csv.DictReader(f)
        else:
            reader = csv.DictReader(f, fieldnames=fieldnames)
        rows = [ row for row in reader ]
    return rows

def read_json(f_path):
    with open(f_path, 'r') as infile:
        data = json.load(infile)
    print('Read {}.'.format(f_path))
    return data

def write_json(data, f_path):
    with open(f_path, 'w') as outfile:
        json.dump(
            data,
            outfile,
            indent=2,
            sort_keys=True
        )
    print('{} written.'.format(f_path))

def list_to_csv(data, f_path):
    with open(f_path, 'wb') as f:
        writer = csv.writer(f)
        writer.writerows(data)
    print('Wrote {}.'.format(f_path))

class Farmer:
    def __init__(self):
        self.verbose = False

    def get(self, url):
        if self.verbose:
            print('Fetching: {}'.format(url))
        r = requests.get(url)
        if r.status_code != 200:
            print('Error: {}.'.format(r.status_code))
        return r

    def get_json(self, url):
        if self.verbose:
            print('Fetching: {}'.format(url))
        r = requests.get(url)
        if r.status_code != 200:
            print('Error: {}.'.format(r.status_code))
        return r.json()

    def post_json(self, payload, url):
        headers = {'content-type': 'application/json'}
        r = requests.post(
            url,
            data=json.dumps(payload),
            headers=headers
        )
        if r.status_code in [200, 201, 202]:
            if self.verbose:
                print('Status: {}.'.format(r.status_code))
            return r
        else:
            print('Error: {}.'.format(r.status_code))

    def read_json(self, f_path):
        with open(f_path, 'r') as infile:
            self.documents = json.load(infile)
        if self.verbose:
            print('Read {}.'.format(f_path))
        return self.documents

    def write_json(self, data, f_path, ensure_ascii=True):
        with open(f_path, 'w') as outfile:
            json.dump(
                data,
                outfile,
                indent=2,
                default=set_default,
                ensure_ascii=ensure_ascii,
                sort_keys=True
            )
        if self.verbose:
            print f_path, 'written.'

    def read_csv(self, f_path, fieldnames=[]):
        with open(f_path) as f:
            if not fieldnames:
                reader = csv.DictReader(f)
            else:
                reader = csv.DictReader(f, fieldnames=fieldnames)
            rows = [ row for row in reader]
        return rows

    def list_to_csv(self, data, f_path):
        with open(f_path, 'wb') as f:
            writer = csv.writer(f)
            writer.writerows(data)
        if self.verbose:
            print('Wrote {}.'.format(f_path))

    def get_filenames(self, f_dir, suffix=''):
        f_paths = []
        for r,d,files in os.walk(f_dir):
            for f in files:
                if f.endswith(suffix):
                    f_paths.append('%s/%s' % (r, f))
        return f_paths

    def file_exists(self, f_path):
        if os.path.isfile(f_path):
            if self.verbose:
                print(' * Skipping item {} exists...'.format(f_path))
            return True
        return False
<commit_msg>added write_text()<commit_after># -*- coding: utf-8 -*-

import csv, json, os, time
try:
    import requests
except ImportError:
    pass

def set_default(obj):
    if isinstance(obj, set):
        return list(obj)
    raise TypeError

def touch(f_path='index.html', times=None):
    with open(f_path, 'a'):
        os.utime(f_path, times)

def get_filenames(f_dir, prefix='', suffix=''):
    f_names = []
    for r,d,files in os.walk(f_dir):
        for f in files:
            if f.startswith(prefix) and f.endswith(suffix):
                f_names.append('{}/{}'.format(r, f))
    return f_names

def file_exists(f_path):
    if os.path.isfile(f_path):
        return True
    return False

def get_json(url, verbose=True):
    if verbose:
        print('Fetching: {}'.format(url))
    r = requests.get(url)
    if r.status_code != 200:
        print('Error: {}.'.format(r.status_code))
    return r.json()

def read_csv(f_path, fieldnames=[], strip_header=False):
    with open(f_path) as f:
        # optionally strip whitespace from header values (e.g. '  depth' -> 'depth')
        if not fieldnames and strip_header:
            fieldnames = [h.strip() for h in f.next().split(',')]
        if not fieldnames:
            reader = csv.DictReader(f)
        else:
            reader = csv.DictReader(f, fieldnames=fieldnames)
        rows = [ row for row in reader ]
    return rows

def read_json(f_path):
    with open(f_path, 'r') as infile:
        data = json.load(infile)
    print('Read {}.'.format(f_path))
    return data

def write_json(data, f_path):
    with open(f_path, 'w') as outfile:
        json.dump(
            data,
            outfile,
            indent=2,
            sort_keys=True
        )
    print('{} written.'.format(f_path))

def write_text(data, f_path):
    with open(f_path, 'w') as f:
        f.write(data.encode('utf8'))

def list_to_csv(data, f_path):
    with open(f_path, 'wb') as f:
        writer = csv.writer(f)
        writer.writerows(data)
    print('Wrote {}.'.format(f_path))

class Farmer:
    def __init__(self):
        self.verbose = False

    def get(self, url):
        if self.verbose:
            print('Fetching: {}'.format(url))
        r = requests.get(url)
        if r.status_code != 200:
            print('Error: {}.'.format(r.status_code))
        return r

    def get_json(self, url):
        if self.verbose:
            print('Fetching: {}'.format(url))
        r = requests.get(url)
        if r.status_code != 200:
            print('Error: {}.'.format(r.status_code))
        return r.json()

    def post_json(self, payload, url):
        headers = {'content-type': 'application/json'}
        r = requests.post(
            url,
            data=json.dumps(payload),
            headers=headers
        )
        if r.status_code in [200, 201, 202]:
            if self.verbose:
                print('Status: {}.'.format(r.status_code))
            return r
        else:
            print('Error: {}.'.format(r.status_code))

    def read_json(self, f_path):
        with open(f_path, 'r') as infile:
            self.documents = json.load(infile)
        if self.verbose:
            print('Read {}.'.format(f_path))
        return self.documents

    def write_json(self, data, f_path, ensure_ascii=True):
        with open(f_path, 'w') as outfile:
            json.dump(
                data,
                outfile,
                indent=2,
                default=set_default,
                ensure_ascii=ensure_ascii,
                sort_keys=True
            )
        if self.verbose:
            print f_path, 'written.'

    def read_csv(self, f_path, fieldnames=[]):
        with open(f_path) as f:
            if not fieldnames:
                reader = csv.DictReader(f)
            else:
                reader = csv.DictReader(f, fieldnames=fieldnames)
            rows = [ row for row in reader]
        return rows

    def list_to_csv(self, data, f_path):
        with open(f_path, 'wb') as f:
            writer = csv.writer(f)
            writer.writerows(data)
        if self.verbose:
            print('Wrote {}.'.format(f_path))

    def get_filenames(self, f_dir, suffix=''):
        f_paths = []
        for r,d,files in os.walk(f_dir):
            for f in files:
                if f.endswith(suffix):
                    f_paths.append('%s/%s' % (r, f))
        return f_paths

    def file_exists(self, f_path):
        if os.path.isfile(f_path):
            if self.verbose:
                print(' * Skipping item {} exists...'.format(f_path))
            return True
        return False
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import unittest
from contextlib import contextmanager
from heapq import heapify, heappush, heappop, heapreplace, heappushpop
from string import ascii_uppercase

from xheap import Heap, RemovalHeap, OrderHeap, XHeap

repeat = 100

class BaseTimeTestCaseMixin(object):

    def tearDown(self):
        print('-'*40)

    def test_init_time(self):
        with print_time('heapq.init'):
            for _ in range(repeat):
                heapify(list(reversed(ascii_uppercase)))
        with print_time('Heap.init'):
            for _ in range(repeat):
                Heap(reversed(ascii_uppercase))

    def test_peek_time(self):
        with print_time('heapq[0]'):
            for _ in range(repeat):
                self.hqs[0]
        with print_time('Heap.peek'):
            for _ in range(repeat):
                self.hs[0].peek()

    def test_pop_time(self):
        with print_time('heapq.heappop'):
            for i in range(repeat):
                heappop(self.hqs[i])
        with print_time('Heap.pop'):
            for i in range(repeat):
                self.hs[i].pop()

    def test_push_time(self):
        with print_time('heapq.heappush'):
            for i in range(repeat):
                heappush(self.hqs[i], 10000000)
        with print_time('Heap.push'):
            for i in range(repeat):
                self.hs[i].push(10000000)

    def test_replace_time(self):
        with print_time('heapq.heapreplace'):
            for i in range(repeat):
                heapreplace(self.hqs[i], 10000000)
        with print_time('Heap.replace'):
            for i in range(repeat):
                self.hs[i].replace(10000000)

    def test_pushpop_time(self):
        with print_time('heapq.heappushpop'):
            for i in range(repeat):
                heappushpop(self.hqs[i], 10000000)
        with print_time('Heap.pushpop'):
            for i in range(repeat):
                self.hs[i].pushpop(10000000)

    def test_heapify_time(self):
        with print_time('heapq.heapify'):
            for i in range(repeat):
                heapify(self.hqs[i])
        with print_time('Heap.heapify'):
            for i in range(repeat):
                self.hs[i].heapify()

class HeapTimeTestCase(BaseTimeTestCaseMixin, unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print('### Heap ###')

    def setUp(self):
        self.hqs = []
        for i in range(repeat):
            hq = list(reversed(range(2600)))
            heapify(hq)
            self.hqs.append(hq)
        self.hs = []
        for i in range(repeat):
            h = Heap(reversed(range(2600)))
            self.hs.append(h)

class OrderHeapTimeTestCase(BaseTimeTestCaseMixin, unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print('### OrderHeap ###')

    @staticmethod
    def key(x):
        return -x

    def setUp(self):
        self.hqs = []
        for i in range(repeat):
            hq = list(reversed(range(2600)))
            heapify(hq)
            self.hqs.append(hq)
        self.hs = []
        for i in range(repeat):
            h = OrderHeap(reversed(range(2600)), key=self.key)
            self.hs.append(h)

class RemovalHeapTimeTestCase(BaseTimeTestCaseMixin, unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print('### RemovalHeap ###')

    def setUp(self):
        self.hqs = []
        for i in range(repeat):
            hq = list(reversed(range(2600)))
            heapify(hq)
            self.hqs.append(hq)
        self.hs = []
        for i in range(repeat):
            h = RemovalHeap(reversed(range(2600)))
            self.hs.append(h)

class XHeapTimeTestCase(BaseTimeTestCaseMixin, unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print('### OrderHeap ###')

    @staticmethod
    def key(x):
        return -x

    def setUp(self):
        self.hqs = []
        for i in range(repeat):
            hq = list(reversed(range(2600)))
            heapify(hq)
            self.hqs.append(hq)
        self.hs = []
        for i in range(repeat):
            h = XHeap(reversed(range(2600)), key=self.key)
            self.hs.append(h)

@contextmanager
def print_time(label):
    import time
    start = time.time()
    yield
    end = time.time()
    print('{: <18} - {:8.4f}usec'.format(label, (end - start) * repeat))<commit_msg>first version of new time testing<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals

from timeit import repeat

class HeapTimeCase(object):

    def time_init(self):
        return [
            (
                'heapq.heapify',
                'from heapq import heapify',
                'heapify(list(reversed(range({size}))))',
                1,
            ),
            (
                'Heap.__init__',
                'from xheap import Heap',
                'Heap(reversed(range({size})))',
                1,
            ),
        ]

    def time_pop(self):
        return [
            (
                'heapq.heappop',
                'from heapq import heapify, heappop; heap = list(reversed(range({size}))); heapify(heap)',
                'heappop(heap)',
                None,
            ),
            (
                'Heap.pop',
                'from xheap import Heap; heap = Heap(reversed(range({size})))',
                'heap.pop()',
                None,
            ),
        ]

    def time_push(self):
        return [
            (
                'heapq.heappush',
                'from heapq import heapify, heappush; heap = list(reversed(range({size}))); heapify(heap); i=0',
                'heappush(heap, i); i += 1',
                None,
            ),
            (
                'Heap.push',
                'from xheap import Heap; heap = Heap(reversed(range({size}))); i=0',
                'heap.push(i); i += 1',
                None,
            ),
        ]

initial_sizes = [10**2, 10**3, 10**4, 10**5, 10**6]
repetitions = 10000
def perform_time_configs(configs):
    for _, setup, stmt, number in configs:
        yield [min(repeat(stmt.format(size=size), setup.format(size=size), number=(number or size), repeat=repetitions)) for size in initial_sizes]

htc = HeapTimeCase()
config_methods = [getattr(htc, method) for method in dir(htc) if method.startswith('time_') and callable(getattr(htc, method))]
configs_list = [config_method() for config_method in config_methods]
align = max(len(c[0]) for cs in configs_list for c in cs)

for configs in configs_list:
    results = list(perform_time_configs(configs))

    baseline_config = configs[0]
    baseline_results = results[0]

    print(baseline_config[0].ljust(align), ' '.join('{:f}'.format(result) for result in baseline_results))

    for config, results in zip(configs[1:], results[1:]):
        print(config[0].ljust(align), ' '.join('{:f}'.format(result) for result in results))
        print('ratio'.ljust(align), ' '.join('{:7.4f}x'.format(result_after/result_before) for result_before, result_after in zip(baseline_results, results)))

    print('---------------------------------')<|endoftext|>


<commit_before>import sys
import os
import argparse
import logging
import pdb
import numpy as np

from intermol.system import System
import helper_functions
import desmond_driver
import gromacs_driver
import lammps_driver

# global constants

DES_POS = 0
GRO_POS = 1
LMP_POS = 2
N_FORMATS = 3

# Make a global logging object.
logger = logging.getLogger('InterMolLog')
if __name__ == '__main__':
    logger.setLevel(logging.DEBUG) # specifies lowest severity log messages to handle
    h = logging.StreamHandler()
    h.setLevel(logging.INFO) # ignores DEBUG level for now
    f = logging.Formatter("%(levelname)s %(asctime)s %(message)s", "%Y-%m-%d %H:%M:%S")
    h.setFormatter(f)
    logger.addHandler(h)

def get_parser():
    parser = argparse.ArgumentParser(description = 'Perform a file conversion')

    # input arguments
    group_in = parser.add_argument_group('Choose input conversion format')
    group_in.add_argument('--des_in', nargs=1, metavar='file',
            help='.cms file for conversion from DESMOND file format')
    group_in.add_argument('--gro_in', nargs=2, metavar='file',
            help='.gro and .top file for conversion from GROMACS file format')
    group_in.add_argument('--lmp_in', nargs=1, metavar='file',
            help='input file for conversion from LAMMPS file format (expects'
                    + ' data file in same directory and a read_data call)')

    # output arguments
    group_out = parser.add_argument_group('Choose output conversion format(s)')
    group_out.add_argument('--desmond', action='store_true',
            help='convert to DESMOND')
    group_out.add_argument('--gromacs', action='store_true',
            help='convert to GROMACS')
    group_out.add_argument('--lammps', action='store_true',
            help='convert to LAMMPS')

    # other
    group_misc = parser.add_argument_group('Other optional arguments')
    group_misc.add_argument('--odir', metavar='directory', default='.',
            help='specification of output directory (default: ./)')
    group_misc.add_argument('--oname', metavar='prefix', default='',
            help='specification of prefix for output filenames '
                    + '(default: inputprefix_converted)')
    group_misc.add_argument('-e', '--energy', dest='energy', action='store_true',
            help='evaluate energy of input and output files for comparison')
    group_misc.add_argument('--efile', default='',
            help='optional run file for energy evaluation (e.g. .cfg, .mdp, .input)')
    group_misc.add_argument('-d', '--despath', dest='despath',
            metavar='path', default='',
            help='path for DESMOND binary, needed for energy evaluation')
    group_misc.add_argument('-g', '--gropath', dest='gropath',
            metavar='path', default='',
            help='path for GROMACS binary, needed for energy evaluation')
    group_misc.add_argument('-l', '--lmppath', dest='lmppath',
            metavar='path', default='lmp_openmpi',
            help='path for LAMMPS binary, needed for energy evaluation')
    group_misc.add_argument('-v', '--verbose', dest='verbose', action='store_true',
            help='high verbosity, includes DEBUG level output')

    # prints help if no arguments given
    if len(sys.argv)==1:
        parser.print_help()
        sys.exit(1)

    return parser

def main(args=''):
    parser = get_parser() # returns parser object
    if not args: # args automatically obtained from sys.argv
        args = parser.parse_args()
    else: # flexibility to call main() from other scripts
        args = parser.parse_args(args)

    if args.verbose:
        h.setLevel(logging.DEBUG)
    System._sys = System('InterMol')

    # --------------- PROCESS INPUTS ----------------- #

    if args.des_in:
        prefix = args.des_in[0][args.des_in[0].rfind('/') + 1:-4]
        desmond_driver.readFile(args.des_in[0])
    elif args.gro_in:
        prefix = args.gro_in[0][args.gro_in[0].rfind('/') + 1:-4]
        # find the top file since order of inputs is not enforced
        top_in = [x for x in args.gro_in if x.endswith('.top')]
        assert(len(top_in)==1)
        top_in = top_in[0] # now just string instead of list
        # find the gro file since order of inputs is not enforced
        gro_in = [x for x in args.gro_in if x.endswith('.gro')]
        assert(len(gro_in)==1)
        gro_in = gro_in[0]
        gromacs_driver.readFile(top_in, gro_in, args.gropath)
    elif args.lmp_in:
        prefix = args.lmp_in[0][args.lmp_in[0].rfind('/') + 1:-4]
        lammps_driver.readFile(args.lmp_in[0])
    else:
        logger.error('No input file')
        sys.exit(1)

    # --------------- WRITE OUTPUTS ----------------- #

    if not args.oname: # default is to append _converted to the input prefix
        oname = '%s_converted' % prefix
    else:
        oname = args.oname
    oname = os.path.join(args.odir, oname) # prepend output directory to oname

    status = [Exception('Failed on write')]*N_FORMATS # records status of conversion
    if args.desmond:
        try:
            desmond_driver.writeFile('{0}.cms'.format(oname))
            status[DES_POS] = 0
        except Exception as e: # other output types will still be attempted
            logger.exception(e) # reports the supressed exception
            status[DES_POS] = e
    if args.gromacs:
        try:
            gromacs_driver.writeFile('{0}.top'.format(oname), '{0}.gro'.format(oname))
            status[GRO_POS] = 0
        except Exception as e:
            logger.exception(e)
            status[GRO_POS] = e
    if args.lammps:
        try:
            lammps_driver.writeFile('{0}.lmp'.format(oname))
            status[LMP_POS] = 0
        except Exception as e:
            logger.exception(e)
            status[LMP_POS] = e

    if args.energy:
        # evaluate input energy
        if args.des_in:
            input_type = 'Desmond' # used for displaying results
            e_in, e_infile = desmond_driver.desmond_energies(args.des_in[0],
                    'inputs/Desmond/onepoint.cfg', args.despath)
        elif args.gro_in:
            input_type = 'Gromacs'
            e_in, e_infile = gromacs_driver.gromacs_energies(top_in, gro_in,
                    'inputs/Gromacs/grompp.mdp', args.gropath, '')
        elif args.lmp_in:
            input_type = 'Lammps'
            # TODO: fix this when --lmp_in gets changed to read input files
            temp = args.lmp_in[0].split('.')[0] + '.input'
            e_in, e_infile = lammps_driver.lammps_energies(temp, args.lmppath)
        else:
            logger.warn('Something weird went wrong! Code should have never made it here.')
            pass # should never reach here

        # evaluate output energy
        output_type = []
        e_outfile = []
        e_out = []
        if args.desmond and status[DES_POS] == 0:
            output_type.append('Desmond')
            try:
                out, outfile = desmond_driver.desmond_energies('%s.cms' % oname,
                        'inputs/Desmond/onepoint.cfg', args.despath)
                status[DES_POS] = helper_functions.get_diff(e_in, out)
                e_out.append(out)
                e_outfile.append(outfile)
            except Exception as e:
                status[DES_POS] = e
                logger.exception(e) #reports the supressed exception
                e_out.append(-1)
                e_outfile.append(-1)

        if args.gromacs and status[GRO_POS] == 0:
            output_type.append('Gromacs')
            try:
                out, outfile = gromacs_driver.gromacs_energies('%s.top' % oname,
                        '%s.gro' % oname, 'inputs/Gromacs/grompp.mdp', args.gropath, '')
                status[GRO_POS] = helper_functions.get_diff(e_in, out)
                e_out.append(out)
                e_outfile.append(outfile)
            except Exception as e:
                status[GRO_POS] = e
                logger.exception(e)
                e_out.append(-1)
                e_outfile.append(-1)

        if args.lammps and status[LMP_POS] == 0:
            output_type.append('Lammps')
            try:
                out, outfile = lammps_driver.lammps_energies('%s.input' % oname,
                        args.lmppath)
                status[LMP_POS] = helper_functions.get_diff(e_in, out)
                e_out.append(out)
                e_outfile.append(outfile)
            except Exception as e:
                status[LMP_POS] = e
                logger.exception(e)
                e_out.append(-1)
                e_outfile.append(-1)

        # display energy comparison results
        out = ['InterMol Conversion Energy Comparison Results','']
        out.append('{0} input energy file: {1}'.format(input_type, e_infile))
        for type, file in zip(output_type, e_outfile):
            out.append('{0} output energy file: {1}'.format(type, file))
        out += helper_functions.summarize_energy_results(e_in, e_out, input_type, output_type)
        logger.info('\n'.join(out))
    status = ['Converted' if x is 0 else x for x in status] # changes 0 to 'Converted'
    return status

if __name__ == '__main__':
    logger.info('Beginning InterMol conversion')
    main()
    logger.info('Finished!')
<commit_msg>cleanup arg parsing<commit_after>import sys
import os
import argparse
import logging
import pdb
import numpy as np

from intermol.system import System
import helper_functions
import desmond_driver
import gromacs_driver
import lammps_driver

# global constants

DES_POS = 0
GRO_POS = 1
LMP_POS = 2
N_FORMATS = 3

# Make a global logging object.
logger = logging.getLogger('InterMolLog')
if __name__ == '__main__':
    logger.setLevel(logging.DEBUG) # specifies lowest severity log messages to handle
    h = logging.StreamHandler()
    h.setLevel(logging.INFO) # ignores DEBUG level for now
    f = logging.Formatter("%(levelname)s %(asctime)s %(message)s", "%Y-%m-%d %H:%M:%S")
    h.setFormatter(f)
    logger.addHandler(h)

def parse_args(args):
    parser = argparse.ArgumentParser(description = 'Perform a file conversion')

    # input arguments
    group_in = parser.add_argument_group('Choose input conversion format')
    group_in.add_argument('--des_in', nargs=1, metavar='file',
            help='.cms file for conversion from DESMOND file format')
    group_in.add_argument('--gro_in', nargs=2, metavar='file',
            help='.gro and .top file for conversion from GROMACS file format')
    group_in.add_argument('--lmp_in', nargs=1, metavar='file',
            help='input file for conversion from LAMMPS file format (expects'
                    + ' data file in same directory and a read_data call)')

    # output arguments
    group_out = parser.add_argument_group('Choose output conversion format(s)')
    group_out.add_argument('--desmond', action='store_true',
            help='convert to DESMOND')
    group_out.add_argument('--gromacs', action='store_true',
            help='convert to GROMACS')
    group_out.add_argument('--lammps', action='store_true',
            help='convert to LAMMPS')

    # other
    group_misc = parser.add_argument_group('Other optional arguments')
    group_misc.add_argument('--odir', metavar='directory', default='.',
            help='specification of output directory (default: ./)')
    group_misc.add_argument('--oname', metavar='prefix', default='',
            help='specification of prefix for output filenames '
                    + '(default: inputprefix_converted)')
    group_misc.add_argument('-e', '--energy', dest='energy', action='store_true',
            help='evaluate energy of input and output files for comparison')
    group_misc.add_argument('--efile', default='',
            help='optional run file for energy evaluation (e.g. .cfg, .mdp, .input)')
    group_misc.add_argument('-d', '--despath', dest='despath',
            metavar='path', default='',
            help='path for DESMOND binary, needed for energy evaluation')
    group_misc.add_argument('-g', '--gropath', dest='gropath',
            metavar='path', default='',
            help='path for GROMACS binary, needed for energy evaluation')
    group_misc.add_argument('-l', '--lmppath', dest='lmppath',
            metavar='path', default='lmp_openmpi',
            help='path for LAMMPS binary, needed for energy evaluation')
    group_misc.add_argument('-v', '--verbose', dest='verbose', action='store_true',
            help='high verbosity, includes DEBUG level output')

    # prints help if no arguments given
    if len(sys.argv)==1:
        parser.print_help()
        sys.exit(1)
    return parser.parse_args(args)

def main(args=None):
    args = parse_args(args)

    if args.verbose:
        h.setLevel(logging.DEBUG)
    System._sys = System('InterMol')

    # --------------- PROCESS INPUTS ----------------- #

    if args.des_in:
        prefix = args.des_in[0][args.des_in[0].rfind('/') + 1:-4]
        desmond_driver.readFile(args.des_in[0])
    elif args.gro_in:
        prefix = args.gro_in[0][args.gro_in[0].rfind('/') + 1:-4]
        # find the top file since order of inputs is not enforced
        top_in = [x for x in args.gro_in if x.endswith('.top')]
        assert(len(top_in)==1)
        top_in = top_in[0] # now just string instead of list
        # find the gro file since order of inputs is not enforced
        gro_in = [x for x in args.gro_in if x.endswith('.gro')]
        assert(len(gro_in)==1)
        gro_in = gro_in[0]
        gromacs_driver.readFile(top_in, gro_in, args.gropath)
    elif args.lmp_in:
        prefix = args.lmp_in[0][args.lmp_in[0].rfind('/') + 1:-4]
        lammps_driver.readFile(args.lmp_in[0])
    else:
        logger.error('No input file')
        sys.exit(1)

    # --------------- WRITE OUTPUTS ----------------- #

    if not args.oname: # default is to append _converted to the input prefix
        oname = '%s_converted' % prefix
    else:
        oname = args.oname
    oname = os.path.join(args.odir, oname) # prepend output directory to oname

    status = [Exception('Failed on write')]*N_FORMATS # records status of conversion
    if args.desmond:
        try:
            desmond_driver.writeFile('{0}.cms'.format(oname))
            status[DES_POS] = 0
        except Exception as e: # other output types will still be attempted
            logger.exception(e) # reports the supressed exception
            status[DES_POS] = e
    if args.gromacs:
        try:
            gromacs_driver.writeFile('{0}.top'.format(oname), '{0}.gro'.format(oname))
            status[GRO_POS] = 0
        except Exception as e:
            logger.exception(e)
            status[GRO_POS] = e
    if args.lammps:
        try:
            lammps_driver.writeFile('{0}.lmp'.format(oname))
            status[LMP_POS] = 0
        except Exception as e:
            logger.exception(e)
            status[LMP_POS] = e

    if args.energy:
        # evaluate input energy
        if args.des_in:
            input_type = 'Desmond' # used for displaying results
            e_in, e_infile = desmond_driver.desmond_energies(args.des_in[0],
                    'inputs/Desmond/onepoint.cfg', args.despath)
        elif args.gro_in:
            input_type = 'Gromacs'
            e_in, e_infile = gromacs_driver.gromacs_energies(top_in, gro_in,
                    'inputs/Gromacs/grompp.mdp', args.gropath, '')
        elif args.lmp_in:
            input_type = 'Lammps'
            # TODO: fix this when --lmp_in gets changed to read input files
            temp = args.lmp_in[0].split('.')[0] + '.input'
            e_in, e_infile = lammps_driver.lammps_energies(temp, args.lmppath)
        else:
            logger.warn('Something weird went wrong! Code should have never made it here.')
            pass # should never reach here

        # evaluate output energy
        output_type = []
        e_outfile = []
        e_out = []
        if args.desmond and status[DES_POS] == 0:
            output_type.append('Desmond')
            try:
                out, outfile = desmond_driver.desmond_energies('%s.cms' % oname,
                        'inputs/Desmond/onepoint.cfg', args.despath)
                status[DES_POS] = helper_functions.get_diff(e_in, out)
                e_out.append(out)
                e_outfile.append(outfile)
            except Exception as e:
                status[DES_POS] = e
                logger.exception(e) #reports the supressed exception
                e_out.append(-1)
                e_outfile.append(-1)

        if args.gromacs and status[GRO_POS] == 0:
            output_type.append('Gromacs')
            try:
                out, outfile = gromacs_driver.gromacs_energies('%s.top' % oname,
                        '%s.gro' % oname, 'inputs/Gromacs/grompp.mdp', args.gropath, '')
                status[GRO_POS] = helper_functions.get_diff(e_in, out)
                e_out.append(out)
                e_outfile.append(outfile)
            except Exception as e:
                status[GRO_POS] = e
                logger.exception(e)
                e_out.append(-1)
                e_outfile.append(-1)

        if args.lammps and status[LMP_POS] == 0:
            output_type.append('Lammps')
            try:
                out, outfile = lammps_driver.lammps_energies('%s.input' % oname,
                        args.lmppath)
                status[LMP_POS] = helper_functions.get_diff(e_in, out)
                e_out.append(out)
                e_outfile.append(outfile)
            except Exception as e:
                status[LMP_POS] = e
                logger.exception(e)
                e_out.append(-1)
                e_outfile.append(-1)

        # display energy comparison results
        out = ['InterMol Conversion Energy Comparison Results','']
        out.append('{0} input energy file: {1}'.format(input_type, e_infile))
        for type, file in zip(output_type, e_outfile):
            out.append('{0} output energy file: {1}'.format(type, file))
        out += helper_functions.summarize_energy_results(e_in, e_out, input_type, output_type)
        logger.info('\n'.join(out))
    status = ['Converted' if x is 0 else x for x in status] # changes 0 to 'Converted'
    return status # for systems_test.py

if __name__ == '__main__':
    logger.info('Beginning InterMol conversion')
    main()
    logger.info('Finished!')
<|endoftext|>


<commit_before>#!/usr/bin/env python

# Copyright 2017 Stanford University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from __future__ import print_function
import hashlib, multiprocessing, os, platform, subprocess, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
import install # from ./install.py

def check_sha1(file_path, sha1):
    with open(file_path, 'rb') as f:
        assert hashlib.sha1(f.read()).hexdigest() == sha1

def download(dest_path, url, sha1):
    dest_dir = os.path.dirname(dest_path)
    dest_file = os.path.basename(dest_path)
    subprocess.check_call(['wget', '-O', dest_path, url])
    check_sha1(dest_path, sha1)

def extract(dest_dir, archive_path, format):
    if format == 'gz':
        subprocess.check_call(['tar', 'xfz', archive_path], cwd=dest_dir)
    elif format == 'xz':
        subprocess.check_call(['tar', 'xfJ', archive_path], cwd=dest_dir)
    else:
        raise Exception('Unknown format %s' % format)

def git_clone(repo_dir, url, branch=None):
    if branch is not None:
        subprocess.check_call(['git', 'clone', '-b', branch, url, repo_dir])
    else:
        subprocess.check_call(['git', 'clone', url, repo_dir])

def git_update(repo_dir):
    subprocess.check_call(
        ['git', 'pull', '--ff-only'],
        cwd=repo_dir)

def discover_conduit():
    if 'CONDUIT' in os.environ:
        return os.environ['CONDUIT']
    elif platform.node().startswith('cori'):
        return 'aries'
    elif platform.node().startswith('daint'):
        return 'aries'
    elif platform.node().startswith('excalibur'):
        return 'aries'
    elif platform.node().startswith('quartz'):
        return 'psm'
    else:
        raise Exception('Please set CONDUIT in your environment')

def build_gasnet(gasnet_dir, conduit):
    subprocess.check_call(['make', 'CONDUIT=%s' % conduit], cwd=gasnet_dir)

def build_llvm(source_dir, build_dir, install_dir, cmake_exe, thread_count):
    env = None
    if is_cray:
        env = dict(list(os.environ.items()) + [
            ('CC', os.environ['HOST_CC']),
            ('CXX', os.environ['HOST_CXX']),
        ])
    subprocess.check_call(
        [cmake_exe,
         '-DCMAKE_INSTALL_PREFIX=%s' % install_dir,
         '-DCMAKE_BUILD_TYPE=Release',
         '-DLLVM_ENABLE_ZLIB=OFF',
         '-DLLVM_ENABLE_TERMINFO=OFF',
         source_dir],
        cwd=build_dir,
        env=env)
    subprocess.check_call(['make', '-j', str(thread_count)], cwd=build_dir)
    subprocess.check_call(['make', 'install'], cwd=build_dir)

def build_terra(terra_dir, llvm_dir, is_cray, thread_count):
    env = None
    if is_cray:
        env = dict(list(os.environ.items()) + [
            ('CC', os.environ['HOST_CC']),
            ('CXX', os.environ['HOST_CXX']),
        ])

    subprocess.check_call(
        ['make',
         'LLVM_CONFIG=%s' % os.path.join(llvm_dir, 'bin', 'llvm-config'),
         'CLANG=%s' % os.path.join(llvm_dir, 'bin', 'clang'),
         '-j', str(thread_count),
        ],
        cwd=terra_dir,
        env=env)

if __name__ == '__main__':
    if 'CC' not in os.environ:
        raise Exception('Please set CC in your environment')
    if 'CXX' not in os.environ:
        raise Exception('Please set CXX in your environment')
    if 'LG_RT_DIR' in os.environ:
        raise Exception('Please unset LG_RT_DIR in your environment')

    is_cray = 'CRAYPE_VERSION' in os.environ

    if is_cray:
        print('This system has been detected as a Cray system.')
        print()
        print('Note: The Cray wrappers are broken for various purposes')
        print('(particularly, dynamically linked libraries). For this')
        print('reason this script requires that HOST_CC and HOST_CXX')
        print('be set to the underlying compilers (GCC and G++, etc.).')
        print()
        if 'HOST_CC' not in os.environ:
            raise Exception('Please set HOST_CC in your environment')
        if 'HOST_CXX' not in os.environ:
            raise Exception('Please set HOST_CXX in your environment')

    root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    legion_dir = os.path.dirname(root_dir)

    thread_count = multiprocessing.cpu_count()

    conduit = discover_conduit()
    gasnet_dir = os.path.realpath(os.path.join(root_dir, 'gasnet'))
    gasnet_release_dir = os.path.join(gasnet_dir, 'release')
    if not os.path.exists(gasnet_dir):
        git_clone(gasnet_dir, 'https://github.com/StanfordLegion/gasnet.git')
        build_gasnet(gasnet_dir, conduit)
    assert os.path.exists(gasnet_release_dir)

    cmake_dir = os.path.realpath(os.path.join(root_dir, 'cmake'))
    cmake_install_dir = os.path.join(cmake_dir, 'cmake-3.7.2-Linux-x86_64')
    if not os.path.exists(cmake_dir):
        os.mkdir(cmake_dir)

        cmake_tarball = os.path.join(cmake_dir, 'cmake-3.7.2-Linux-x86_64.tar.gz')
        download(cmake_tarball, 'https://cmake.org/files/v3.7/cmake-3.7.2-Linux-x86_64.tar.gz', '915bc981aab354821fb9fd28374a720fdb3aa180')
        extract(cmake_dir, cmake_tarball, 'gz')
    assert os.path.exists(cmake_install_dir)
    cmake_exe = os.path.join(cmake_install_dir, 'bin', 'cmake')

    llvm_dir = os.path.realpath(os.path.join(root_dir, 'llvm'))
    llvm_install_dir = os.path.join(llvm_dir, 'install')
    if not os.path.exists(llvm_dir):
        os.mkdir(llvm_dir)

        llvm_tarball = os.path.join(llvm_dir, 'llvm-3.9.1.src.tar.xz')
        llvm_source_dir = os.path.join(llvm_dir, 'llvm-3.9.1.src')
        clang_tarball = os.path.join(llvm_dir, 'cfe-3.9.1.src.tar.xz')
        clang_source_dir = os.path.join(llvm_dir, 'cfe-3.9.1.src')
        download(llvm_tarball, 'http://llvm.org/releases/3.9.1/llvm-3.9.1.src.tar.xz', 'ce801cf456b8dacd565ce8df8288b4d90e7317ff')
        download(clang_tarball, 'http://llvm.org/releases/3.9.1/cfe-3.9.1.src.tar.xz', '95e4be54b70f32cf98a8de36821ea5495b84add8')
        extract(llvm_dir, llvm_tarball, 'xz')
        extract(llvm_dir, clang_tarball, 'xz')
        os.rename(clang_source_dir, os.path.join(llvm_source_dir, 'tools', 'clang'))

        llvm_build_dir = os.path.join(llvm_dir, 'build')
        os.mkdir(llvm_build_dir)
        os.mkdir(llvm_install_dir)
        build_llvm(llvm_source_dir, llvm_build_dir, llvm_install_dir, cmake_exe, thread_count)
    assert os.path.exists(llvm_install_dir)

    terra_dir = os.path.join(root_dir, 'terra.build')
    if not os.path.exists(terra_dir):
        git_clone(terra_dir, 'https://github.com/elliottslaughter/terra.git', 'compiler-sc17-snapshot')
        build_terra(terra_dir, llvm_install_dir, is_cray, thread_count)

    use_cuda = 'USE_CUDA' in os.environ and os.environ['USE_CUDA'] == '1'
    use_openmp = 'USE_OPENMP' in os.environ and os.environ['USE_OPENMP'] == '1'
    use_hdf = 'USE_HDF' in os.environ and os.environ['USE_HDF'] == '1'
    install.install(
        gasnet=True, cuda=use_cuda, openmp=use_openmp, hdf=use_hdf,
        external_terra_dir=terra_dir, gasnet_dir=gasnet_release_dir, conduit=conduit,
        debug=False, thread_count=thread_count)
<commit_msg>regent: Add debug mode to the setup script<commit_after>#!/usr/bin/env python

# Copyright 2017 Stanford University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from __future__ import print_function
import hashlib, multiprocessing, os, platform, subprocess, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
import install # from ./install.py

def check_sha1(file_path, sha1):
    with open(file_path, 'rb') as f:
        assert hashlib.sha1(f.read()).hexdigest() == sha1

def download(dest_path, url, sha1):
    dest_dir = os.path.dirname(dest_path)
    dest_file = os.path.basename(dest_path)
    subprocess.check_call(['wget', '-O', dest_path, url])
    check_sha1(dest_path, sha1)

def extract(dest_dir, archive_path, format):
    if format == 'gz':
        subprocess.check_call(['tar', 'xfz', archive_path], cwd=dest_dir)
    elif format == 'xz':
        subprocess.check_call(['tar', 'xfJ', archive_path], cwd=dest_dir)
    else:
        raise Exception('Unknown format %s' % format)

def git_clone(repo_dir, url, branch=None):
    if branch is not None:
        subprocess.check_call(['git', 'clone', '-b', branch, url, repo_dir])
    else:
        subprocess.check_call(['git', 'clone', url, repo_dir])

def git_update(repo_dir):
    subprocess.check_call(
        ['git', 'pull', '--ff-only'],
        cwd=repo_dir)

def discover_conduit():
    if 'CONDUIT' in os.environ:
        return os.environ['CONDUIT']
    elif platform.node().startswith('cori'):
        return 'aries'
    elif platform.node().startswith('daint'):
        return 'aries'
    elif platform.node().startswith('excalibur'):
        return 'aries'
    elif platform.node().startswith('quartz'):
        return 'psm'
    else:
        raise Exception('Please set CONDUIT in your environment')

def build_gasnet(gasnet_dir, conduit):
    subprocess.check_call(['make', 'CONDUIT=%s' % conduit], cwd=gasnet_dir)

def build_llvm(source_dir, build_dir, install_dir, cmake_exe, thread_count):
    env = None
    if is_cray:
        env = dict(list(os.environ.items()) + [
            ('CC', os.environ['HOST_CC']),
            ('CXX', os.environ['HOST_CXX']),
        ])
    subprocess.check_call(
        [cmake_exe,
         '-DCMAKE_INSTALL_PREFIX=%s' % install_dir,
         '-DCMAKE_BUILD_TYPE=Release',
         '-DLLVM_ENABLE_ZLIB=OFF',
         '-DLLVM_ENABLE_TERMINFO=OFF',
         source_dir],
        cwd=build_dir,
        env=env)
    subprocess.check_call(['make', '-j', str(thread_count)], cwd=build_dir)
    subprocess.check_call(['make', 'install'], cwd=build_dir)

def build_terra(terra_dir, llvm_dir, is_cray, thread_count):
    env = None
    if is_cray:
        env = dict(list(os.environ.items()) + [
            ('CC', os.environ['HOST_CC']),
            ('CXX', os.environ['HOST_CXX']),
        ])

    subprocess.check_call(
        ['make',
         'LLVM_CONFIG=%s' % os.path.join(llvm_dir, 'bin', 'llvm-config'),
         'CLANG=%s' % os.path.join(llvm_dir, 'bin', 'clang'),
         '-j', str(thread_count),
        ],
        cwd=terra_dir,
        env=env)

if __name__ == '__main__':
    if 'CC' not in os.environ:
        raise Exception('Please set CC in your environment')
    if 'CXX' not in os.environ:
        raise Exception('Please set CXX in your environment')
    if 'LG_RT_DIR' in os.environ:
        raise Exception('Please unset LG_RT_DIR in your environment')

    is_cray = 'CRAYPE_VERSION' in os.environ

    if is_cray:
        print('This system has been detected as a Cray system.')
        print()
        print('Note: The Cray wrappers are broken for various purposes')
        print('(particularly, dynamically linked libraries). For this')
        print('reason this script requires that HOST_CC and HOST_CXX')
        print('be set to the underlying compilers (GCC and G++, etc.).')
        print()
        if 'HOST_CC' not in os.environ:
            raise Exception('Please set HOST_CC in your environment')
        if 'HOST_CXX' not in os.environ:
            raise Exception('Please set HOST_CXX in your environment')

    root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    legion_dir = os.path.dirname(root_dir)

    thread_count = multiprocessing.cpu_count()

    conduit = discover_conduit()
    gasnet_dir = os.path.realpath(os.path.join(root_dir, 'gasnet'))
    gasnet_release_dir = os.path.join(gasnet_dir, 'release')
    if not os.path.exists(gasnet_dir):
        git_clone(gasnet_dir, 'https://github.com/StanfordLegion/gasnet.git')
        build_gasnet(gasnet_dir, conduit)
    assert os.path.exists(gasnet_release_dir)

    cmake_dir = os.path.realpath(os.path.join(root_dir, 'cmake'))
    cmake_install_dir = os.path.join(cmake_dir, 'cmake-3.7.2-Linux-x86_64')
    if not os.path.exists(cmake_dir):
        os.mkdir(cmake_dir)

        cmake_tarball = os.path.join(cmake_dir, 'cmake-3.7.2-Linux-x86_64.tar.gz')
        download(cmake_tarball, 'https://cmake.org/files/v3.7/cmake-3.7.2-Linux-x86_64.tar.gz', '915bc981aab354821fb9fd28374a720fdb3aa180')
        extract(cmake_dir, cmake_tarball, 'gz')
    assert os.path.exists(cmake_install_dir)
    cmake_exe = os.path.join(cmake_install_dir, 'bin', 'cmake')

    llvm_dir = os.path.realpath(os.path.join(root_dir, 'llvm'))
    llvm_install_dir = os.path.join(llvm_dir, 'install')
    if not os.path.exists(llvm_dir):
        os.mkdir(llvm_dir)

        llvm_tarball = os.path.join(llvm_dir, 'llvm-3.9.1.src.tar.xz')
        llvm_source_dir = os.path.join(llvm_dir, 'llvm-3.9.1.src')
        clang_tarball = os.path.join(llvm_dir, 'cfe-3.9.1.src.tar.xz')
        clang_source_dir = os.path.join(llvm_dir, 'cfe-3.9.1.src')
        download(llvm_tarball, 'http://llvm.org/releases/3.9.1/llvm-3.9.1.src.tar.xz', 'ce801cf456b8dacd565ce8df8288b4d90e7317ff')
        download(clang_tarball, 'http://llvm.org/releases/3.9.1/cfe-3.9.1.src.tar.xz', '95e4be54b70f32cf98a8de36821ea5495b84add8')
        extract(llvm_dir, llvm_tarball, 'xz')
        extract(llvm_dir, clang_tarball, 'xz')
        os.rename(clang_source_dir, os.path.join(llvm_source_dir, 'tools', 'clang'))

        llvm_build_dir = os.path.join(llvm_dir, 'build')
        os.mkdir(llvm_build_dir)
        os.mkdir(llvm_install_dir)
        build_llvm(llvm_source_dir, llvm_build_dir, llvm_install_dir, cmake_exe, thread_count)
    assert os.path.exists(llvm_install_dir)

    terra_dir = os.path.join(root_dir, 'terra.build')
    if not os.path.exists(terra_dir):
        git_clone(terra_dir, 'https://github.com/elliottslaughter/terra.git', 'compiler-sc17-snapshot')
        build_terra(terra_dir, llvm_install_dir, is_cray, thread_count)

    use_cuda = 'USE_CUDA' in os.environ and os.environ['USE_CUDA'] == '1'
    use_openmp = 'USE_OPENMP' in os.environ and os.environ['USE_OPENMP'] == '1'
    use_hdf = 'USE_HDF' in os.environ and os.environ['USE_HDF'] == '1'
    debug = 'DEBUG' in os.environ and os.environ['DEBUG'] == '1'
    install.install(
        gasnet=True, cuda=use_cuda, openmp=use_openmp, hdf=use_hdf,
        external_terra_dir=terra_dir, gasnet_dir=gasnet_release_dir, conduit=conduit,
        debug=debug, thread_count=thread_count)
<|endoftext|>


<commit_before>import argparse
import glob
import os
import re
import shutil

# get filenames with matching pattern in given path
def get_filenames(path, pattern):
    os.chdir(path)
    return glob.glob(pattern)

# parse filename and return dictionary
def parse_filename(name):
    search = re.search('(^\d+)\.?(.*)-(.*).mp3', name, re.IGNORECASE)
    if search:
        return {
            'rank': search.group(1),
            'singer': search.group(2).strip(),
            'title': search.group(3).strip()
        }
    else:
        return {'title': name}

# Remove ranking from filename
def remove_ranking(name):
    info = parse_filename(name)
    if info['singer']:
        return "%s - %s.mp3" % (info['singer'], info['title'])
    else:
        return name

# Normalize string
# Removes whitespace, special characters
def normailze(name):
    return re.sub('[^A-Za-z0-9가-힣]', '', name)

# get normalized filenames in given path
def get_normalized_filenames(path):
    filenames = get_filenames(path, "*.mp3")
    normailzed = map(lambda x: normailze(x), filenames)
    return set(normailzed)

# Copy or move mp3 files from src to dest directory
def copy_mp3_files(src, dest, move):
    header = "MOVE" if move else "COPY"
    print("[%s] %s => %s" % (header, src, dest))

    dest_filenames = get_normalized_filenames(dest)

    for filename in get_filenames(src, "*.mp3"):
        new_filename = remove_ranking(filename)
        normalized = normailze(new_filename)
        src_path = os.path.join(src, filename)

        # skip copy/move if exists
        if normalized in dest_filenames:
            print("[SKIP] %s" % filename)
            continue

        # copy/move
        dest_path = os.path.join(dest, new_filename)
        print("[%s] %s => %s" % (header, filename, new_filename))
        if move:
            shutil.move(src_path, dest_path)
        else:
            shutil.copyfile(src_path, dest_path)

parser = argparse.ArgumentParser(description='Move mp3 files to another directory')
parser.add_argument('--src', help='source directory', default='./')
parser.add_argument('--dest', help='destination directory', required=True)
parser.add_argument('--move', help='move file', action='store_true')
parser.add_argument('--regex', help='source directory regular expression')
args = parser.parse_args()

dest_dir = os.path.abspath(args.dest)
if args.regex:
    for d in get_filenames(args.src, args.regex):
        copy_mp3_files(os.path.abspath(os.path.join(args.src, d)),
                       dest_dir,
                       args.move)
else:
    copy_mp3_files(os.path.abspath(args.src),
                   dest_dir,
                   args.move)
<commit_msg>[MOD] '--src' option supports wildcard. ('--regex' option removed)<commit_after>import argparse
import glob
import os
import re
import shutil

# get filenames with matching pattern in given path
def get_filenames(path, pattern):
    os.chdir(path)
    return glob.glob(pattern)

# parse filename and return dictionary
def parse_filename(name):
    search = re.search('(^\d+)\.?(.*)-(.*).mp3', name, re.IGNORECASE)
    if search:
        return {
            'rank': search.group(1),
            'singer': search.group(2).strip(),
            'title': search.group(3).strip()
        }
    else:
        return {'title': name}

# Remove ranking from filename
def remove_ranking(name):
    info = parse_filename(name)
    if info['singer']:
        return "%s - %s.mp3" % (info['singer'], info['title'])
    else:
        return name

# Normalize string
# Removes whitespace, special characters
def normailze(name):
    return re.sub('[^A-Za-z0-9가-힣]', '', name)

# get normalized filenames in given path
def get_normalized_filenames(path):
    filenames = get_filenames(path, "*.mp3")
    normailzed = map(lambda x: normailze(x), filenames)
    return set(normailzed)

# Copy or move mp3 files from src to dest directory
def copy_mp3_files(src, dest, move):
    header = "MOVE" if move else "COPY"
    print("%s => %s" % (src, dest))

    dest_filenames = get_normalized_filenames(dest)

    for filename in get_filenames(src, "*.mp3"):
        new_filename = remove_ranking(filename)
        normalized = normailze(new_filename)
        src_path = os.path.join(src, filename)

        # skip copy/move if exists
        if normalized in dest_filenames:
            continue

        # copy/move
        dest_path = os.path.join(dest, new_filename)
        print("[%s] %s => %s" % (header, filename, new_filename))
        if move:
            shutil.move(src_path, dest_path)
        else:
            shutil.copyfile(src_path, dest_path)

parser = argparse.ArgumentParser(description='Move mp3 files to another directory')
parser.add_argument('--src', help='source directory', default='./')
parser.add_argument('--dest', help='destination directory', required=True)
parser.add_argument('--move', help='move file', action='store_true')
args = parser.parse_args()

dest_dir = os.path.abspath(args.dest)
wildcard_src = True if any(ch in args.src for ch in ['?', '*']) else False

if wildcard_src:
    src_parent = os.path.dirname(args.src)
    src_pattern = os.path.basename(args.src)
    for d in get_filenames(src_parent, src_pattern):
        copy_mp3_files(os.path.abspath(os.path.join(src_parent, d)),
                       dest_dir,
                       args.move)
else:
    copy_mp3_files(os.path.abspath(args.src),
                   dest_dir,
                   args.move)
<|endoftext|>


<commit_before># TensorFlow external dependencies that can be loaded in WORKSPACE files.

load("//third_party/gpus:cuda_configure.bzl", "cuda_configure")

# If TensorFlow is linked as a submodule.
# path_prefix and tf_repo_name are no longer used.
def tf_workspace(path_prefix = "", tf_repo_name = ""):
  cuda_configure(name = "local_config_cuda")
  if path_prefix:
    print("path_prefix was specified to tf_workspace but is no longer used and will be removed in the future.")
  if tf_repo_name:
    print("tf_repo_name was specified to tf_workspace but is no longer used and will be removed in the future.")

  # These lines need to be changed when updating Eigen. They are parsed from
  # this file by the cmake and make builds to determine the eigen version and
  # hash.
  eigen_version = "22b492048b2f"
  eigen_sha256 = "8b9bd14a037c1a3fe37dc5e4a71504ebe48148cf2498fd8eb6848165a7a0538f"

  native.new_http_archive(
    name = "eigen_archive",
    url = "http://bitbucket.org/eigen/eigen/get/" + eigen_version + ".tar.gz",
    sha256 = eigen_sha256,
    strip_prefix = "eigen-eigen-" + eigen_version,
    build_file = str(Label("//:eigen.BUILD")),
  )

  native.http_archive(
    name = "com_googlesource_code_re2",
    url = "http://github.com/google/re2/archive/b94b7cd42e9f02673cd748c1ac1d16db4052514c.tar.gz",
    sha256 = "bd63550101e056427c9e7ff12a408c1c8b74e9803f393ca916b2926fc2c4906f",
    strip_prefix = "re2-b94b7cd42e9f02673cd748c1ac1d16db4052514c",
  )

  native.http_archive(
    name = "gemmlowp",
    url = "http://github.com/google/gemmlowp/archive/a6f29d8ac48d63293f845f2253eccbf86bc28321.tar.gz",
    sha256 = "75d40ea8e68b0d1644f052fffe8f14a410b2a73d40ccb859a95c0578d194ec26",
    strip_prefix = "gemmlowp-a6f29d8ac48d63293f845f2253eccbf86bc28321",
  )

  native.new_http_archive(
    name = "farmhash_archive",
    url = "http://github.com/google/farmhash/archive/71a777924015693c69bc3c8c6492fb8d5372c636.zip",
    sha256 = "99190108fb96a5e38e183f6a23fb7742948214fc96a746a50c79eb09a255a298",
    strip_prefix = "farmhash-71a777924015693c69bc3c8c6492fb8d5372c636/src",
    build_file = str(Label("//:farmhash.BUILD")),
  )

  native.bind(
    name = "farmhash",
    actual = "@farmhash//:farmhash",
  )

  native.http_archive(
    name = "highwayhash",
    url = "http://github.com/google/highwayhash/archive/4bce8fc6a9ca454d9d377dbc4c4d33488bbab78f.tar.gz",
    sha256 = "b159a62fb05e5f6a6be20aa0df6a951ebf44a7bb96ed2e819e4e35e17f56854d",
    strip_prefix = "highwayhash-4bce8fc6a9ca454d9d377dbc4c4d33488bbab78f",
  )

  native.new_http_archive(
    name = "jpeg_archive",
    url = "http://www.ijg.org/files/jpegsrc.v9a.tar.gz",
    sha256 = "3a753ea48d917945dd54a2d97de388aa06ca2eb1066cbfdc6652036349fe05a7",
    strip_prefix = "jpeg-9a",
    build_file = str(Label("//:jpeg.BUILD")),
  )

  native.new_http_archive(
    name = "png_archive",
    url = "http://github.com/glennrp/libpng/archive/v1.2.53.zip",
    sha256 = "c35bcc6387495ee6e757507a68ba036d38ad05b415c2553b3debe2a57647a692",
    strip_prefix = "libpng-1.2.53",
    build_file = str(Label("//:png.BUILD")),
  )

  native.new_http_archive(
    name = "gif_archive",
    url = "http://cdimage.debian.org/mirror/xbmc.org/build-deps/sources/giflib-5.1.4.tar.gz",
    sha256 = "34a7377ba834397db019e8eb122e551a49c98f49df75ec3fcc92b9a794a4f6d1",
    strip_prefix = "giflib-5.1.4/lib",
    build_file = str(Label("//:gif.BUILD")),
  )

  native.new_http_archive(
    name = "six_archive",
    url = "http://pypi.python.org/packages/source/s/six/six-1.10.0.tar.gz",
    sha256 = "105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a",
    strip_prefix = "six-1.10.0",
    build_file = str(Label("//:six.BUILD")),
  )

  native.bind(
    name = "six",
    actual = "@six_archive//:six",
  )

  native.http_archive(
    name = "protobuf",
    url = "http://github.com/google/protobuf/archive/008b5a228b37c054f46ba478ccafa5e855cb16db.tar.gz",
    sha256 = "2737ad055eb8a9bc63ed068e32c4ea280b62d8236578cb4d4120eb5543f759ab",
    strip_prefix = "protobuf-008b5a228b37c054f46ba478ccafa5e855cb16db",
  )

  native.new_http_archive(
    name = "gmock_archive",
    url = "http://pkgs.fedoraproject.org/repo/pkgs/gmock/gmock-1.7.0.zip/073b984d8798ea1594f5e44d85b20d66/gmock-1.7.0.zip",
    sha256 = "26fcbb5925b74ad5fc8c26b0495dfc96353f4d553492eb97e85a8a6d2f43095b",
    strip_prefix = "gmock-1.7.0",
    build_file = str(Label("//:gmock.BUILD")),
  )

  native.bind(
    name = "gtest",
    actual = "@gmock_archive//:gtest",
  )

  native.bind(
    name = "gtest_main",
    actual = "@gmock_archive//:gtest_main",
  )

  native.bind(
    name = "python_headers",
    actual = str(Label("//util/python:python_headers")),
  )

  native.new_http_archive(
    name = "pcre",
    sha256 = "ccdf7e788769838f8285b3ee672ed573358202305ee361cfec7a4a4fb005bbc7",
    url = "http://ftp.exim.org/pub/pcre/pcre-8.39.tar.gz",
    strip_prefix = "pcre-8.39",
    build_file = str(Label("//third_party:pcre.BUILD")),
  )

  native.new_http_archive(
    name = "swig",
    sha256 = "58a475dbbd4a4d7075e5fe86d4e54c9edde39847cdb96a3053d87cb64a23a453",
    url = "http://cdimage.debian.org/mirror/xbmc.org/build-deps/sources/swig-3.0.8.tar.gz",
    strip_prefix = "swig-3.0.8",
    build_file = str(Label("//third_party:swig.BUILD")),
  )

  # grpc expects //external:protobuf_clib and //external:protobuf_compiler
  # to point to the protobuf's compiler library.
  native.bind(
    name = "protobuf_clib",
    actual = "@protobuf//:protoc_lib",
  )

  native.bind(
    name = "protobuf_compiler",
    actual = "@protobuf//:protoc_lib",
  )

  native.new_http_archive(
    name = "grpc",
    url = "http://github.com/grpc/grpc/archive/d7ff4ff40071d2b486a052183e3e9f9382afb745.tar.gz",
    sha256 = "a15f352436ab92c521b1ac11e729e155ace38d0856380cf25048c5d1d9ba8e31",
    strip_prefix = "grpc-d7ff4ff40071d2b486a052183e3e9f9382afb745",
    build_file = str(Label("//:grpc.BUILD")),
  )

  # protobuf expects //external:grpc_cpp_plugin to point to grpc's
  # C++ plugin code generator.
  native.bind(
    name = "grpc_cpp_plugin",
    actual = "@grpc//:grpc_cpp_plugin",
  )

  native.bind(
    name = "grpc_lib",
    actual = "@grpc//:grpc++_unsecure",
  )

  native.new_git_repository(
    name = "linenoise",
    commit = "c894b9e59f02203dbe4e2be657572cf88c4230c3",
    init_submodules = True,
    remote = "https://github.com/antirez/linenoise.git",
    build_file = str(Label("//:linenoise.BUILD")),
  )

  native.new_http_archive(
    name = "jsoncpp_git",
    url = "http://github.com/open-source-parsers/jsoncpp/archive/11086dd6a7eba04289944367ca82cea71299ed70.tar.gz",
    sha256 = "07d34db40593d257324ec5fb9debc4dc33f29f8fb44e33a2eeb35503e61d0fe2",
    strip_prefix = "jsoncpp-11086dd6a7eba04289944367ca82cea71299ed70",
    build_file = str(Label("//:jsoncpp.BUILD")),
  )

  native.bind(
    name = "jsoncpp",
    actual = "@jsoncpp_git//:jsoncpp",
  )

  native.http_archive(
    name = "boringssl",
    url = "http://github.com/google/boringssl/archive/bbcaa15b0647816b9a1a9b9e0d209cd6712f0105.tar.gz",  # 2016-07-11
    sha256 = "025264d6e9a7ad371f2f66d17a28b6627de0c9592dc2eb54afd062f68f1f9aa3",
    strip_prefix = "boringssl-bbcaa15b0647816b9a1a9b9e0d209cd6712f0105",
  )

  native.new_http_archive(
    name = "nanopb_git",
    url = "http://github.com/nanopb/nanopb/archive/1251fa1065afc0d62f635e0f63fec8276e14e13c.tar.gz",
    sha256 = "ab1455c8edff855f4f55b68480991559e51c11e7dab060bbab7cffb12dd3af33",
    strip_prefix = "nanopb-1251fa1065afc0d62f635e0f63fec8276e14e13c",
    build_file = str(Label("//:nanopb.BUILD")),
  )

  native.bind(
    name = "nanopb",
    actual = "@nanopb_git//:nanopb",
  )

  native.new_http_archive(
    name = "zlib_archive",
    url = "http://zlib.net/zlib-1.2.8.tar.gz",
    sha256 = "36658cb768a54c1d4dec43c3116c27ed893e88b02ecfcb44f2166f9c0b7f2a0d",
    strip_prefix = "zlib-1.2.8",
    build_file = str(Label("//:zlib.BUILD")),
  )

  native.bind(
    name = "zlib",
    actual = "@zlib_archive//:zlib",
  )
<commit_msg>Upgraded to the latest version of Eigen that adds support for AVX512 instructions Change: 138212280<commit_after># TensorFlow external dependencies that can be loaded in WORKSPACE files.

load("//third_party/gpus:cuda_configure.bzl", "cuda_configure")

# If TensorFlow is linked as a submodule.
# path_prefix and tf_repo_name are no longer used.
def tf_workspace(path_prefix = "", tf_repo_name = ""):
  cuda_configure(name = "local_config_cuda")
  if path_prefix:
    print("path_prefix was specified to tf_workspace but is no longer used and will be removed in the future.")
  if tf_repo_name:
    print("tf_repo_name was specified to tf_workspace but is no longer used and will be removed in the future.")

  # These lines need to be changed when updating Eigen. They are parsed from
  # this file by the cmake and make builds to determine the eigen version and
  # hash.
  eigen_version = "3d41a24add9b"
  eigen_sha256 = "ce91f8db04493096c0f8bd5ebca7f5a295c88874cda99b3e9d99c9ed10154f99"

  native.new_http_archive(
    name = "eigen_archive",
    url = "http://bitbucket.org/eigen/eigen/get/" + eigen_version + ".tar.gz",
    sha256 = eigen_sha256,
    strip_prefix = "eigen-eigen-" + eigen_version,
    build_file = str(Label("//:eigen.BUILD")),
  )

  native.http_archive(
    name = "com_googlesource_code_re2",
    url = "http://github.com/google/re2/archive/b94b7cd42e9f02673cd748c1ac1d16db4052514c.tar.gz",
    sha256 = "bd63550101e056427c9e7ff12a408c1c8b74e9803f393ca916b2926fc2c4906f",
    strip_prefix = "re2-b94b7cd42e9f02673cd748c1ac1d16db4052514c",
  )

  native.http_archive(
    name = "gemmlowp",
    url = "http://github.com/google/gemmlowp/archive/a6f29d8ac48d63293f845f2253eccbf86bc28321.tar.gz",
    sha256 = "75d40ea8e68b0d1644f052fffe8f14a410b2a73d40ccb859a95c0578d194ec26",
    strip_prefix = "gemmlowp-a6f29d8ac48d63293f845f2253eccbf86bc28321",
  )

  native.new_http_archive(
    name = "farmhash_archive",
    url = "http://github.com/google/farmhash/archive/71a777924015693c69bc3c8c6492fb8d5372c636.zip",
    sha256 = "99190108fb96a5e38e183f6a23fb7742948214fc96a746a50c79eb09a255a298",
    strip_prefix = "farmhash-71a777924015693c69bc3c8c6492fb8d5372c636/src",
    build_file = str(Label("//:farmhash.BUILD")),
  )

  native.bind(
    name = "farmhash",
    actual = "@farmhash//:farmhash",
  )

  native.http_archive(
    name = "highwayhash",
    url = "http://github.com/google/highwayhash/archive/4bce8fc6a9ca454d9d377dbc4c4d33488bbab78f.tar.gz",
    sha256 = "b159a62fb05e5f6a6be20aa0df6a951ebf44a7bb96ed2e819e4e35e17f56854d",
    strip_prefix = "highwayhash-4bce8fc6a9ca454d9d377dbc4c4d33488bbab78f",
  )

  native.new_http_archive(
    name = "jpeg_archive",
    url = "http://www.ijg.org/files/jpegsrc.v9a.tar.gz",
    sha256 = "3a753ea48d917945dd54a2d97de388aa06ca2eb1066cbfdc6652036349fe05a7",
    strip_prefix = "jpeg-9a",
    build_file = str(Label("//:jpeg.BUILD")),
  )

  native.new_http_archive(
    name = "png_archive",
    url = "http://github.com/glennrp/libpng/archive/v1.2.53.zip",
    sha256 = "c35bcc6387495ee6e757507a68ba036d38ad05b415c2553b3debe2a57647a692",
    strip_prefix = "libpng-1.2.53",
    build_file = str(Label("//:png.BUILD")),
  )

  native.new_http_archive(
    name = "gif_archive",
    url = "http://cdimage.debian.org/mirror/xbmc.org/build-deps/sources/giflib-5.1.4.tar.gz",
    sha256 = "34a7377ba834397db019e8eb122e551a49c98f49df75ec3fcc92b9a794a4f6d1",
    strip_prefix = "giflib-5.1.4/lib",
    build_file = str(Label("//:gif.BUILD")),
  )

  native.new_http_archive(
    name = "six_archive",
    url = "http://pypi.python.org/packages/source/s/six/six-1.10.0.tar.gz",
    sha256 = "105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a",
    strip_prefix = "six-1.10.0",
    build_file = str(Label("//:six.BUILD")),
  )

  native.bind(
    name = "six",
    actual = "@six_archive//:six",
  )

  native.http_archive(
    name = "protobuf",
    url = "http://github.com/google/protobuf/archive/008b5a228b37c054f46ba478ccafa5e855cb16db.tar.gz",
    sha256 = "2737ad055eb8a9bc63ed068e32c4ea280b62d8236578cb4d4120eb5543f759ab",
    strip_prefix = "protobuf-008b5a228b37c054f46ba478ccafa5e855cb16db",
  )

  native.new_http_archive(
    name = "gmock_archive",
    url = "http://pkgs.fedoraproject.org/repo/pkgs/gmock/gmock-1.7.0.zip/073b984d8798ea1594f5e44d85b20d66/gmock-1.7.0.zip",
    sha256 = "26fcbb5925b74ad5fc8c26b0495dfc96353f4d553492eb97e85a8a6d2f43095b",
    strip_prefix = "gmock-1.7.0",
    build_file = str(Label("//:gmock.BUILD")),
  )

  native.bind(
    name = "gtest",
    actual = "@gmock_archive//:gtest",
  )

  native.bind(
    name = "gtest_main",
    actual = "@gmock_archive//:gtest_main",
  )

  native.bind(
    name = "python_headers",
    actual = str(Label("//util/python:python_headers")),
  )

  native.new_http_archive(
    name = "pcre",
    sha256 = "ccdf7e788769838f8285b3ee672ed573358202305ee361cfec7a4a4fb005bbc7",
    url = "http://ftp.exim.org/pub/pcre/pcre-8.39.tar.gz",
    strip_prefix = "pcre-8.39",
    build_file = str(Label("//third_party:pcre.BUILD")),
  )

  native.new_http_archive(
    name = "swig",
    sha256 = "58a475dbbd4a4d7075e5fe86d4e54c9edde39847cdb96a3053d87cb64a23a453",
    url = "http://cdimage.debian.org/mirror/xbmc.org/build-deps/sources/swig-3.0.8.tar.gz",
    strip_prefix = "swig-3.0.8",
    build_file = str(Label("//third_party:swig.BUILD")),
  )

  # grpc expects //external:protobuf_clib and //external:protobuf_compiler
  # to point to the protobuf's compiler library.
  native.bind(
    name = "protobuf_clib",
    actual = "@protobuf//:protoc_lib",
  )

  native.bind(
    name = "protobuf_compiler",
    actual = "@protobuf//:protoc_lib",
  )

  native.new_http_archive(
    name = "grpc",
    url = "http://github.com/grpc/grpc/archive/d7ff4ff40071d2b486a052183e3e9f9382afb745.tar.gz",
    sha256 = "a15f352436ab92c521b1ac11e729e155ace38d0856380cf25048c5d1d9ba8e31",
    strip_prefix = "grpc-d7ff4ff40071d2b486a052183e3e9f9382afb745",
    build_file = str(Label("//:grpc.BUILD")),
  )

  # protobuf expects //external:grpc_cpp_plugin to point to grpc's
  # C++ plugin code generator.
  native.bind(
    name = "grpc_cpp_plugin",
    actual = "@grpc//:grpc_cpp_plugin",
  )

  native.bind(
    name = "grpc_lib",
    actual = "@grpc//:grpc++_unsecure",
  )

  native.new_git_repository(
    name = "linenoise",
    commit = "c894b9e59f02203dbe4e2be657572cf88c4230c3",
    init_submodules = True,
    remote = "https://github.com/antirez/linenoise.git",
    build_file = str(Label("//:linenoise.BUILD")),
  )

  native.new_http_archive(
    name = "jsoncpp_git",
    url = "http://github.com/open-source-parsers/jsoncpp/archive/11086dd6a7eba04289944367ca82cea71299ed70.tar.gz",
    sha256 = "07d34db40593d257324ec5fb9debc4dc33f29f8fb44e33a2eeb35503e61d0fe2",
    strip_prefix = "jsoncpp-11086dd6a7eba04289944367ca82cea71299ed70",
    build_file = str(Label("//:jsoncpp.BUILD")),
  )

  native.bind(
    name = "jsoncpp",
    actual = "@jsoncpp_git//:jsoncpp",
  )

  native.http_archive(
    name = "boringssl",
    url = "http://github.com/google/boringssl/archive/bbcaa15b0647816b9a1a9b9e0d209cd6712f0105.tar.gz",  # 2016-07-11
    sha256 = "025264d6e9a7ad371f2f66d17a28b6627de0c9592dc2eb54afd062f68f1f9aa3",
    strip_prefix = "boringssl-bbcaa15b0647816b9a1a9b9e0d209cd6712f0105",
  )

  native.new_http_archive(
    name = "nanopb_git",
    url = "http://github.com/nanopb/nanopb/archive/1251fa1065afc0d62f635e0f63fec8276e14e13c.tar.gz",
    sha256 = "ab1455c8edff855f4f55b68480991559e51c11e7dab060bbab7cffb12dd3af33",
    strip_prefix = "nanopb-1251fa1065afc0d62f635e0f63fec8276e14e13c",
    build_file = str(Label("//:nanopb.BUILD")),
  )

  native.bind(
    name = "nanopb",
    actual = "@nanopb_git//:nanopb",
  )

  native.new_http_archive(
    name = "zlib_archive",
    url = "http://zlib.net/zlib-1.2.8.tar.gz",
    sha256 = "36658cb768a54c1d4dec43c3116c27ed893e88b02ecfcb44f2166f9c0b7f2a0d",
    strip_prefix = "zlib-1.2.8",
    build_file = str(Label("//:zlib.BUILD")),
  )

  native.bind(
    name = "zlib",
    actual = "@zlib_archive//:zlib",
  )
<|endoftext|>


<commit_before>#FHT-METAR
#Author FHT
#2022-02-22

""" Import Libraries """
import xml.etree.ElementTree as ET
import urllib.request
import time
import datetime
import neopixel
import board
try:
    import astral
except ImportError:
    astral = None

""" Import Airport List """
with open("/home/pi/METARmap/Airports") as f:
    airports = f.readlines()
airports = [x.strip() for x in airports]

""" LED Colors """
COLOR_VFR = (0,255,0) # Green
COLOR_MVFR = (0, 0, 255) # Blue
COLOR_IFR = (255,0,0) # Red
COLOR_LIFR = (125,0,125) # Magenta
COLOR_CLEAR = (0,0,0) # Clear

""" Setup Dimming Based on Time """
brightLED = 0.5 # Brightness ratio for bright LEDs (0 - 1)
dimmingRatio = 0.2 # Brightness ratio for dim LEDs (0 - 1)
cityNearest = "Boston" # Set to city location, refer to https://astral.readthedocs.io/en/latest/#cities)
timeBright = datetime.time(7,0) # Set default bright time
#timeDim = datetime.time(19,0) # Set default dim time
timeDim = datetime.time(11,0) # Set default dim time

# Determine sunrise/sunset times for bright/dim adjustment)
if astral is not None:
    try:
        ast = astral.Astral()
        try:
            city = ast[cityNearest]
        except KeyError:
            print("Error: Location not recognized")
        else:
            print(city)
            sun = city.sun(date = datetime.datetime.now(), local = True)
            timeBright = sun['sunrise'].time()
            timeDim = sun['sunset'].time()
    except AttributeError:
        import astral.geocoder
        import astral.sun
        try:
            city = astral.geocoder.lookup(cityNearest, astral.geocoder.database())
        except KeyError:
            print("Error: Location not recognized")
        else:
            print(city)
            sun = astral.sun.sun(city.observer, date = datetime.datetime.now().date(), tzinfo=city.timezone)
            timeBright = sun['sunrise'].time()
            timeDim = sun['sunset'].time()
    print("Current time is: " + str(datetime.datetime.now()))
    print("Sunrise:" + timeBright.strftime('%H:%M') + " Sunset:" + timeDim.strftime('%H:%M'))

"""LED Configuration"""
bright = timeBright < datetime.datetime.now().time() < timeDim
LED_COUNT = len(airports) # Number of LEDS --> Equals # of airports in list
LED_PIN = board.D18 # GPIO pin connected to neopixels (18 uses PWM)
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(
    LED_PIN, LED_COUNT, brightness = dimmingRatio if (bright == False) else brightLED,
    auto_write=False, pixel_order=ORDER)

""" Get METAR Info """
url = 'https://aviationweather.gov/adds/dataserver_current/httpparam?datasource=metars&requestType=retrieve&format=xml&mostRecentForEachStation=constraint&hoursBeforeNow=1.25&stationString='
for airport in airports:
    url += (airport + ",")
url = url[:-1]

try:
    with urllib.request.urlopen(url) as response:
            xml_metar = response.read()
    metar = ET.fromstring(xml_metar)
except Exceptions:
    time.sleep(60)
    pass

conditionDict = {"NULL": {"flightCategory" : ""}}
conditionDict.pop("NULL")

stationList = []
for metarInfo in metar.iter('METAR'):
    stationID = metarInfo.find('station_id').text
    if metarInfo.find('flight_category') is None:
            continue
    flightCategory = metarInfo.find('flight_category').text
    conditionDict[stationID] = flightCategory

""" Organize METAR Info """
organizedWeather = {}
for airport in airports:
    organizedWeather[airport] = "NONE"

for key in organizedWeather:
    try:
        organizedWeather[key] = conditionDict[key]
    except Exception as e:
        organizedWeather[key] = "NONE"
""" Set LED Colors """
i=0
for key in organizedWeather:
    if organizedWeather[key] == "VFR":
        pixels[i] = COLOR_VFR
    elif organizedWeather[key] == "MVFR":
        pixels[i] = COLOR_MVFR
    elif organizedWeather[key] == "IFR":
        pixels[i] = COLOR_IFR
    elif organizedWeather[key] == "LIFR":
        pixels[i] = COLOR_LIFR
    else:
        pixels[i] = COLOR_CLEAR
    i+=1

for key in organizedWeather:
    print("At " + key + " the current weather is " + organizedWeather[key])
pixels.show()
<commit_msg>Nevermind<commit_after>#FHT-METAR
#Author FHT
#2022-02-22

""" Import Libraries """
import xml.etree.ElementTree as ET
import urllib.request
import time
import datetime
import neopixel
import board
try:
    import astral
except ImportError:
    astral = None

""" Import Airport List """
with open("/home/pi/METARmap/Airports") as f:
    airports = f.readlines()
airports = [x.strip() for x in airports]

""" LED Colors """
COLOR_VFR = (0,255,0) # Green
COLOR_MVFR = (0, 0, 255) # Blue
COLOR_IFR = (255,0,0) # Red
COLOR_LIFR = (125,0,125) # Magenta
COLOR_CLEAR = (0,0,0) # Clear

""" Setup Dimming Based on Time """
brightLED = 0.5 # Brightness ratio for bright LEDs (0 - 1)
dimmingRatio = 0.2 # Brightness ratio for dim LEDs (0 - 1)
cityNearest = "Boston" # Set to city location, refer to https://astral.readthedocs.io/en/latest/#cities)
timeBright = datetime.time(7,0) # Set default bright time
timeDim = datetime.time(19,0) # Set default dim time

# Determine sunrise/sunset times for bright/dim adjustment)
if astral is not None:
    try:
        ast = astral.Astral()
        try:
            city = ast[cityNearest]
        except KeyError:
            print("Error: Location not recognized")
        else:
            print(city)
            sun = city.sun(date = datetime.datetime.now(), local = True)
            timeBright = sun['sunrise'].time()
            timeDim = sun['sunset'].time()
    except AttributeError:
        import astral.geocoder
        import astral.sun
        try:
            city = astral.geocoder.lookup(cityNearest, astral.geocoder.database())
        except KeyError:
            print("Error: Location not recognized")
        else:
            print(city)
            sun = astral.sun.sun(city.observer, date = datetime.datetime.now().date(), tzinfo=city.timezone)
            timeBright = sun['sunrise'].time()
            timeDim = sun['sunset'].time()
    print("Current time is: " + str(datetime.datetime.now()))
    print("Sunrise:" + timeBright.strftime('%H:%M') + " Sunset:" + timeDim.strftime('%H:%M'))

"""LED Configuration"""
bright = timeBright < datetime.datetime.now().time() < timeDim
LED_COUNT = len(airports) # Number of LEDS --> Equals # of airports in list
LED_PIN = board.D18 # GPIO pin connected to neopixels (18 uses PWM)
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(
    LED_PIN, LED_COUNT, brightness = dimmingRatio if (bright == False) else brightLED,
    auto_write=False, pixel_order=ORDER)

""" Get METAR Info """
url = 'https://aviationweather.gov/adds/dataserver_current/httpparam?datasource=metars&requestType=retrieve&format=xml&mostRecentForEachStation=constraint&hoursBeforeNow=1.25&stationString='
for airport in airports:
    url += (airport + ",")
url = url[:-1]

try:
    with urllib.request.urlopen(url) as response:
            xml_metar = response.read()
    metar = ET.fromstring(xml_metar)
except Exceptions:
    time.sleep(60)
    pass

conditionDict = {"NULL": {"flightCategory" : ""}}
conditionDict.pop("NULL")

stationList = []
for metarInfo in metar.iter('METAR'):
    stationID = metarInfo.find('station_id').text
    if metarInfo.find('flight_category') is None:
            continue
    flightCategory = metarInfo.find('flight_category').text
    conditionDict[stationID] = flightCategory

""" Organize METAR Info """
organizedWeather = {}
for airport in airports:
    organizedWeather[airport] = "NONE"

for key in organizedWeather:
    try:
        organizedWeather[key] = conditionDict[key]
    except Exception as e:
        organizedWeather[key] = "NONE"
""" Set LED Colors """
i=0
for key in organizedWeather:
    if organizedWeather[key] == "VFR":
        pixels[i] = COLOR_VFR
    elif organizedWeather[key] == "MVFR":
        pixels[i] = COLOR_MVFR
    elif organizedWeather[key] == "IFR":
        pixels[i] = COLOR_IFR
    elif organizedWeather[key] == "LIFR":
        pixels[i] = COLOR_LIFR
    else:
        pixels[i] = COLOR_CLEAR
    i+=1

for key in organizedWeather:
    print("At " + key + " the current weather is " + organizedWeather[key])
pixels.show()
<|endoftext|>