<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2017, AGB & GC
# Full license can be found in License.md
#-----------------------------------------------------------------------------
""" Tests the ocb_scaling class and functions
"""
from __future__ import absolute_import, unicode_literals

import datetime as dt
import logging
from io import StringIO
import numpy as np
import os
import unittest

import ocbpy
import ocbpy.instruments.general as ocb_igen

class TestGeneralFileTestMethods(unittest.TestCase):
    def setUp(self):
        """ Initialize the logging and output variables
        """
        self.test_file = os.path.join(os.path.dirname(ocbpy.__file__), "tests",
                                      "test_data", "test_north_circle")
        self.temp_output = os.path.join(os.path.dirname(ocbpy.__file__),
                                        "tests", "test_data", "temp_gen")
        self.rstat = None

        self.lwarn = u""
        self.lout = u""
        self.log_capture = StringIO()
        ocbpy.logger.addHandler(logging.StreamHandler(self.log_capture))
        ocbpy.logger.setLevel(logging.WARNING)

    def tearDown(self):
        if os.path.isfile(self.temp_output):
            os.remove(self.temp_output)

        del self.test_file, self.lwarn, self.lout, self.log_capture, self.rstat
        del self.temp_output

    def test_file_test_success(self):
        """ Test the success condition for one of the test_data files"""
        self.rstat = ocb_igen.test_file(self.test_file)
        self.assertTrue(self.rstat)

    def test_file_test_not_file(self):
        """ Test the general file testing routine with a bad filename """
        self.lwarn = u"name provided is not a file"

        self.rstat = ocb_igen.test_file("/")
        self.lout = self.log_capture.getvalue()

        self.assertTrue(self.lout.find(self.lwarn) >= 0)
        self.assertFalse(self.rstat)

    def test_file_test_empty_file(self):
        """ Test the general file testing routine with a bad filename """
        self.lwarn = u'empty file'

        # Create an empty file and read it in
        open(self.temp_output, 'a').close()
        self.rstat = ocb_igen.test_file(self.temp_output)
        self.lout = self.log_capture.getvalue()

        self.assertTrue(self.lout.find(self.lwarn) >= 0)
        self.assertFalse(self.rstat)

    def test_large_file(self):
        """ Test the file size limit for loading data """
        self.lwarn = u'File size'

        # Create a 2.12 GB file
        with open(self.temp_output, 'wb') as fout:
            fout.truncate(2024 * 1024 * 1024)

        self.rstat = ocb_igen.test_file(self.temp_output)
        self.lout = self.log_capture.getvalue()

        self.assertTrue(self.lout.find(self.lwarn) >= 0)
        self.assertFalse(self.rstat)

class TestGeneralLoadMethods(unittest.TestCase):
    def setUp(self):
        """ Initialize the parameters needed to test the general loading method
        """
        ocb_dir = os.path.dirname(ocbpy.__file__)
        self.test_file_soy = os.path.join(ocb_dir, "tests", "test_data",
                                          "test_north_circle")
        self.test_file_dt = os.path.join(ocb_dir, "tests", "test_data",
                                         "dmsp-ssj_north_out.ocb")
        self.headers = {self.test_file_soy:
                        [u"YEAR SOY NB PHICENT RCENT R A RERR"],
                        self.test_file_dt:
                        [u"#sc date time r x y fom x_1 y_1 x_2 y_2\n"]}
        self.test_out = {self.test_file_soy:
                         {"YEAR": 2000.0, "SOY": 11187202.0, "NB": 9.0,
                          "A": 1.302e+07, "PHICENT": 315.29, "RCENT": 2.67,
                          "R": 18.38, "RERR": 0.47},
                         self.test_file_dt:
                         {"sc": 16.0, "date": u"2010-12-31", "fom": 3.192,
                          "r": 1.268, "time": u"23:24:53", "x": 0.437,
                          "x_1": -7.61, "x_2": 8.485, "y": 6.999, "y_1": 5.564,
                          "y_2": 8.433,
                          "datetime": dt.datetime(2010,12,31,23,24,53)}}
        self.out = None

        self.lwarn = u""
        self.lout = u""
        self.log_capture = StringIO()
        ocbpy.logger.addHandler(logging.StreamHandler(self.log_capture))
        ocbpy.logger.setLevel(logging.WARNING)

    def tearDown(self):
        del self.test_file_soy, self.lwarn, self.lout, self.log_capture
        del self.test_file_dt, self.headers, self.out, self.test_out

    def test_load_ascii_data_badfile(self):
        """ Test the general loading routine for ASCII data with bad input
        """
        self.lwarn = u'name provided is not a file'

        self.out = ocb_igen.load_ascii_data("/", 0)
        self.lout = self.log_capture.getvalue()
        self.assertListEqual(self.out[0], [])
        self.assertDictEqual(self.out[1], {})

        self.assertTrue(self.lout.find(self.lwarn) >= 0)

    def test_load_ascii_data_bad_header(self):
        """ Test the general loading routine for ASCII data with bad header
        """
        self.lwarn = u'unable to find header'

        self.out = ocb_igen.load_ascii_data(self.test_file_soy, 0,
                                            header=list())
        self.lout = self.log_capture.getvalue()
        self.assertListEqual(self.out[0], [])
        self.assertDictEqual(self.out[1], {})

        self.assertTrue(self.lout.find(self.lwarn) >= 0)

    @unittest.skip("Fails when run as part of the class")
    def test_load_ascii_data_w_header(self):
        """ Test the general routine to load ASCII data that has a header
        """
        self.out = ocb_igen.load_ascii_data(self.test_file_dt, 1,
                                            datetime_cols=[1, 2],
                                            datetime_fmt="%Y-%m-%d %H:%M:%S",
                                            header=list())

        # Test to ensure the output header equals the input header
        self.assertListEqual(self.out[0], self.headers[self.test_file_dt])

        # Test to see that the data keys are all in the header
        self.assertListEqual(sorted([kk for kk in self.test_out[
            self.test_file_dt].keys()]),
                             sorted([kk for kk in self.out[1].keys()]))

        # Test the length of the data file
        self.assertTupleEqual(self.out[1]['fom'].shape, (7,))

        # Test the values of the last data line
        for kk in self.test_out[self.test_file_dt].keys():
            self.assertEqual(self.out[1][kk][-1],
                             self.test_out[self.test_file_dt][kk])

    def test_load_ascii_data_wo_header(self):
        """ Test the general routine to load ASCII data by providing a header
        """
        self.out = ocb_igen.load_ascii_data(self.test_file_soy, 0,
                                        header=self.headers[self.test_file_soy])

        # Test to ensure the output header equals the input header
        self.assertListEqual(self.out[0], self.headers[self.test_file_soy])

        # Test to see that the data keys are all in the header
        self.assertListEqual(sorted([kk for kk in self.test_out[
            self.test_file_soy].keys()]),
                             sorted([kk for kk in self.out[1].keys()]))

        # Test the length of the data file
        self.assertTupleEqual(self.out[1]['A'].shape, (75,))

        # Test the values of the last data line
        for kk in self.test_out[self.test_file_soy].keys():
            self.assertEqual(self.out[1][kk][-1],
                             self.test_out[self.test_file_soy][kk])

    def test_load_ascii_data_int_cols(self):
        """ Test the general routine to load ASCII data assigning some
        columns as integers
        """

        int_keys = ["YEAR", "SOY", "NB"]
        self.out = ocb_igen.load_ascii_data(self.test_file_soy, 0,
                    header=self.headers[self.test_file_soy], int_cols=[0, 1, 2])

        # Test to ensure the output header equals the input header
        self.assertListEqual(self.out[0], self.headers[self.test_file_soy])

        # Test to see that the data keys are all in the header
        self.assertListEqual(sorted([kk for kk in self.test_out[
            self.test_file_soy].keys()]),
                             sorted([kk for kk in self.out[1].keys()]))

        # Test the length of the data file
        self.assertTupleEqual(self.out[1]['A'].shape, (75,))

        # Test the values of the last data line
        for kk in self.test_out[self.test_file_soy].keys():
            if kk in int_keys:
                self.assertEqual(self.out[1][kk][-1],
                                 int(self.test_out[self.test_file_soy][kk]))
            else:
                self.assertEqual(self.out[1][kk][-1],
                                 self.test_out[self.test_file_soy][kk])

        del int_keys

    def test_load_ascii_data_str_cols(self):
        """ Test the general routine to load ASCII data assigning some
        columns as strings
        """

        str_keys = ["YEAR", "SOY"]
        self.out = ocb_igen.load_ascii_data(self.test_file_soy, 0,
                    header=self.headers[self.test_file_soy], str_cols=[0, 1])

        # Test to ensure the output header equals the input header
        self.assertListEqual(self.out[0], self.headers[self.test_file_soy])

        # Test to see that the data keys are all in the header
        self.assertListEqual(sorted([kk for kk in self.test_out[
            self.test_file_soy].keys()]),
                             sorted([kk for kk in self.out[1].keys()]))

        # Test the length of the data file
        self.assertEqual(self.out[1]['A'].shape, (75,))

        # Test the values of the last data line
        for kk in self.test_out[self.test_file_soy].keys():
            if kk in str_keys:
                self.assertEqual(self.out[1][kk][-1], "{:.0f}".format(
                                     self.test_out[self.test_file_soy][kk]))
            else:
                self.assertEqual(self.out[1][kk][-1],
                                 self.test_out[self.test_file_soy][kk])

        del str_keys

    def test_load_ascii_data_w_datetime(self):
        """ Test the general routine to load ASCII data
        """

        self.out = ocb_igen.load_ascii_data(self.test_file_soy, 0,
                                            datetime_cols=[0,1],
                                            datetime_fmt="YEAR SOY",
                        header=self.headers[self.test_file_soy])

        # Test to ensure the output header equals the input header
        self.assertListEqual(self.out[0], self.headers[self.test_file_soy])

        # Test to see that the data keys are all in the header
        ktest = [kk for kk in self.test_out[self.test_file_soy].keys()]
        ktest.append("datetime")
        self.assertListEqual(sorted(ktest),
                             sorted([kk for kk in self.out[1].keys()]))

        # Test the length of the data file
        self.assertTupleEqual(self.out[1]['A'].shape, (75,))

        # Test the values of the last data line
        for kk in self.test_out[self.test_file_soy].keys():
            self.assertEqual(self.out[1][kk][-1],
                             self.test_out[self.test_file_soy][kk])

        # Test the datetime
        self.assertEqual(self.out[1]['datetime'][-1],
                         dt.datetime(2000,5,9,11,33,22))

        del ktest

if __name__ == '__main__':
    unittest.main()

<commit_msg>TST: generalized input specification<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2017, AGB & GC
# Full license can be found in License.md
#-----------------------------------------------------------------------------
""" Tests the ocb_scaling class and functions
"""
from __future__ import absolute_import, unicode_literals

import datetime as dt
import logging
from io import StringIO
import numpy as np
import os
import unittest

import ocbpy
import ocbpy.instruments.general as ocb_igen

class TestGeneralFileTestMethods(unittest.TestCase):
    def setUp(self):
        """ Initialize the logging and output variables
        """
        self.test_file = os.path.join(os.path.dirname(ocbpy.__file__), "tests",
                                      "test_data", "test_north_circle")
        self.temp_output = os.path.join(os.path.dirname(ocbpy.__file__),
                                        "tests", "test_data", "temp_gen")
        self.rstat = None

        self.lwarn = u""
        self.lout = u""
        self.log_capture = StringIO()
        ocbpy.logger.addHandler(logging.StreamHandler(self.log_capture))
        ocbpy.logger.setLevel(logging.WARNING)

    def tearDown(self):
        if os.path.isfile(self.temp_output):
            os.remove(self.temp_output)

        del self.test_file, self.lwarn, self.lout, self.log_capture, self.rstat
        del self.temp_output

    def test_file_test_success(self):
        """ Test the success condition for one of the test_data files"""
        self.rstat = ocb_igen.test_file(self.test_file)
        self.assertTrue(self.rstat)

    def test_file_test_not_file(self):
        """ Test the general file testing routine with a bad filename """
        self.lwarn = u"name provided is not a file"

        self.rstat = ocb_igen.test_file("/")
        self.lout = self.log_capture.getvalue()

        self.assertTrue(self.lout.find(self.lwarn) >= 0)
        self.assertFalse(self.rstat)

    def test_file_test_empty_file(self):
        """ Test the general file testing routine with a bad filename """
        self.lwarn = u'empty file'

        # Create an empty file and read it in
        open(self.temp_output, 'a').close()
        self.rstat = ocb_igen.test_file(self.temp_output)
        self.lout = self.log_capture.getvalue()

        self.assertTrue(self.lout.find(self.lwarn) >= 0)
        self.assertFalse(self.rstat)

    def test_large_file(self):
        """ Test the file size limit for loading data """
        self.lwarn = u'File size'

        # Create a 2.12 GB file
        with open(self.temp_output, 'wb') as fout:
            fout.truncate(2024 * 1024 * 1024)

        self.rstat = ocb_igen.test_file(self.temp_output)
        self.lout = self.log_capture.getvalue()

        self.assertTrue(self.lout.find(self.lwarn) >= 0)
        self.assertFalse(self.rstat)

class TestGeneralLoadMethods(unittest.TestCase):
    def setUp(self):
        """ Initialize the parameters needed to test the general loading method
        """
        ocb_dir = os.path.dirname(ocbpy.__file__)
        self.test_file_soy = os.path.join(ocb_dir, "tests", "test_data",
                                          "test_north_circle")
        self.test_file_dt = os.path.join(ocb_dir, "tests", "test_data",
                                         "dmsp-ssj_north_out.ocb")
        self.headers = {self.test_file_soy:
                        [u"YEAR SOY NB PHICENT RCENT R A RERR"],
                        self.test_file_dt:
                        [u"#sc date time r x y fom x_1 y_1 x_2 y_2"]}
        self.test_out = {self.test_file_soy:
                         {"YEAR": 2000.0, "SOY": 11187202.0, "NB": 9.0,
                          "A": 1.302e+07, "PHICENT": 315.29, "RCENT": 2.67,
                          "R": 18.38, "RERR": 0.47},
                         self.test_file_dt:
                         {"sc": 16.0, "date": u"2010-12-31", "fom": 3.192,
                          "r": 1.268, "time": u"23:24:53", "x": 0.437,
                          "x_1": -7.61, "x_2": 8.485, "y": 6.999, "y_1": 5.564,
                          "y_2": 8.433,
                          "datetime": dt.datetime(2010,12,31,23,24,53)}}
        self.load_kwargs = {'gft_kwargs': dict(), 'hsplit': None,
                            'datetime_cols': list(), 'datetime_fmt': None,
                            'int_cols': list(), 'str_cols': list(),
                            'max_str_length': 50, 'header': list()}
        self.out = None

        self.lwarn = u""
        self.lout = u""
        self.log_capture = StringIO()
        ocbpy.logger.addHandler(logging.StreamHandler(self.log_capture))
        ocbpy.logger.setLevel(logging.WARNING)

    def tearDown(self):
        del self.test_file_soy, self.lwarn, self.lout, self.log_capture
        del self.test_file_dt, self.headers, self.out, self.test_out
        del self.load_kwargs

    def test_load_ascii_data_badfile(self):
        """ Test the general loading routine for ASCII data with bad input
        """
        self.lwarn = u'name provided is not a file'

        self.out = ocb_igen.load_ascii_data("/", 0)
        self.lout = self.log_capture.getvalue()
        self.assertListEqual(self.out[0], [])
        self.assertDictEqual(self.out[1], {})

        self.assertTrue(self.lout.find(self.lwarn) >= 0)

    def test_load_ascii_data_bad_header(self):
        """ Test the general loading routine for ASCII data with bad header
        """
        self.lwarn = u'unable to find header'

        self.out = ocb_igen.load_ascii_data(self.test_file_soy, 0,
                                            **self.load_kwargs)
        self.lout = self.log_capture.getvalue()
        self.assertListEqual(self.out[0], [])
        self.assertDictEqual(self.out[1], {})

        self.assertTrue(self.lout.find(self.lwarn) >= 0)

    def test_load_ascii_data_w_header(self):
        """ Test the general routine to load ASCII data that has a header
        """
        self.load_kwargs['datetime_cols'] = [1, 2]
        self.load_kwargs['datetime_fmt'] = "%Y-%m-%d %H:%M:%S"

        self.out = ocb_igen.load_ascii_data(self.test_file_dt, 1,
                                            **self.load_kwargs)

        # Test to ensure the output header equals the input header
        self.assertListEqual(self.out[0], self.headers[self.test_file_dt])

        # Test to see that the data keys are all in the header
        self.assertListEqual(sorted([kk for kk in self.test_out[
            self.test_file_dt].keys()]),
                             sorted([kk for kk in self.out[1].keys()]))

        # Test the length of the data file
        self.assertTupleEqual(self.out[1]['fom'].shape, (7,))

        # Test the values of the last data line
        for kk in self.test_out[self.test_file_dt].keys():
            self.assertEqual(self.out[1][kk][-1],
                             self.test_out[self.test_file_dt][kk])

    def test_load_ascii_data_wo_header(self):
        """ Test the general routine to load ASCII data by providing a header
        """
        self.load_kwargs['header'] = self.headers[self.test_file_soy]
        self.out = ocb_igen.load_ascii_data(self.test_file_soy, 0,
                                            **self.load_kwargs)

        # Test to ensure the output header equals the input header
        self.assertListEqual(self.out[0], self.headers[self.test_file_soy])

        # Test to see that the data keys are all in the header
        self.assertListEqual(sorted([kk for kk in self.test_out[
            self.test_file_soy].keys()]),
                             sorted([kk for kk in self.out[1].keys()]))

        # Test the length of the data file
        self.assertTupleEqual(self.out[1]['A'].shape, (75,))

        # Test the values of the last data line
        for kk in self.test_out[self.test_file_soy].keys():
            self.assertEqual(self.out[1][kk][-1],
                             self.test_out[self.test_file_soy][kk])

    def test_load_ascii_data_int_cols(self):
        """ Test the general routine to load ASCII data assigning some
        columns as integers
        """

        int_keys = ["YEAR", "SOY", "NB"]
        self.load_kwargs['header'] = self.headers[self.test_file_soy]
        self.load_kwargs['int_cols'] = [0, 1, 2]
        self.out = ocb_igen.load_ascii_data(self.test_file_soy, 0,
                                            **self.load_kwargs)

        # Test to ensure the output header equals the input header
        self.assertListEqual(self.out[0], self.headers[self.test_file_soy])

        # Test to see that the data keys are all in the header
        self.assertListEqual(sorted([kk for kk in self.test_out[
            self.test_file_soy].keys()]),
                             sorted([kk for kk in self.out[1].keys()]))

        # Test the length of the data file
        self.assertTupleEqual(self.out[1]['A'].shape, (75,))

        # Test the values of the last data line
        for kk in self.test_out[self.test_file_soy].keys():
            if kk in int_keys:
                self.assertEqual(self.out[1][kk][-1],
                                 int(self.test_out[self.test_file_soy][kk]))
            else:
                self.assertEqual(self.out[1][kk][-1],
                                 self.test_out[self.test_file_soy][kk])

        del int_keys

    def test_load_ascii_data_str_cols(self):
        """ Test the general routine to load ASCII data assigning some
        columns as strings
        """

        str_keys = ["YEAR", "SOY"]
        self.load_kwargs['header'] = self.headers[self.test_file_soy]
        self.load_kwargs['str_cols'] = [0, 1]
        self.out = ocb_igen.load_ascii_data(self.test_file_soy, 0,
                                            **self.load_kwargs)

        # Test to ensure the output header equals the input header
        self.assertListEqual(self.out[0], self.headers[self.test_file_soy])

        # Test to see that the data keys are all in the header
        self.assertListEqual(sorted([kk for kk in self.test_out[
            self.test_file_soy].keys()]),
                             sorted([kk for kk in self.out[1].keys()]))

        # Test the length of the data file
        self.assertEqual(self.out[1]['A'].shape, (75,))

        # Test the values of the last data line
        for kk in self.test_out[self.test_file_soy].keys():
            if kk in str_keys:
                self.assertEqual(self.out[1][kk][-1], "{:.0f}".format(
                                     self.test_out[self.test_file_soy][kk]))
            else:
                self.assertEqual(self.out[1][kk][-1],
                                 self.test_out[self.test_file_soy][kk])

        del str_keys

    def test_load_ascii_data_w_datetime(self):
        """ Test the general routine to load ASCII data
        """

        self.load_kwargs['header'] = self.headers[self.test_file_soy]
        self.load_kwargs['datetime_cols'] = [0, 1]
        self.load_kwargs['datetime_fmt'] = "YEAR SOY"
        self.out = ocb_igen.load_ascii_data(self.test_file_soy, 0,
                                            **self.load_kwargs)

        # Test to ensure the output header equals the input header
        self.assertListEqual(self.out[0], self.headers[self.test_file_soy])

        # Test to see that the data keys are all in the header
        ktest = [kk for kk in self.test_out[self.test_file_soy].keys()]
        ktest.append("datetime")
        self.assertListEqual(sorted(ktest),
                             sorted([kk for kk in self.out[1].keys()]))

        # Test the length of the data file
        self.assertTupleEqual(self.out[1]['A'].shape, (75,))

        # Test the values of the last data line
        for kk in self.test_out[self.test_file_soy].keys():
            self.assertEqual(self.out[1][kk][-1],
                             self.test_out[self.test_file_soy][kk])

        # Test the datetime
        self.assertEqual(self.out[1]['datetime'][-1],
                         dt.datetime(2000,5,9,11,33,22))

        del ktest

if __name__ == '__main__':
    unittest.main()

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# datasets.py
#
# purpose:  Functions to handle datasets.
# author:   Filipe P. A. Fernandes
# e-mail:   ocefpaf@gmail
# web:      http://ocefpaf.tiddlyspot.com/
# created:  09-Sep-2011
# modified: Fri 05 Jul 2013 02:26:49 PM BRT
#
# obs: some Functions were based on:
# http://www.trondkristiansen.com/?page_id=1071

from __future__ import division

import numpy as np
import matplotlib.pyplot as plt

from pandas import Panel
from netCDF4 import Dataset, num2date
from oceans.ff_tools import get_profile

__all__ = ['woa_subset',
           'etopo_subset',
           'laplace_X',
           'laplace_Y',
           'laplace_filter',
           'get_depth',
           'get_isobath']

def woa_subset(bbox=[-43, -29.5, -22.5, -17], var='temperature',
               clim_type='monthly', resolution='1deg'):
    r"""Get World Ocean Atlas variables at a given lon, lat bounding box.
    Choose variable from:
        `dissolved_oxygen`, `salinity`, `temperature`, `oxygen_saturation`,
        `apparent_oxygen_utilization`, `phosphate`, `silicate`, or `nitrate`.
    Choose clim_type averages from:
        `monthly`, `seasonal`, or `annual`.
    Choose resolution from:
        `1deg` or `5deg`

    Example
    -------
    >>> import matplotlib.pyplot as plt
    >>> bbox = [-59, -25, -38, 9]
    >>> dataset = woa_subset(bbox=bbox, var='temperature', clim_type='annual',
    ...                      resolution='5deg')
    >>> dataset = dataset['annual']['Statistical Mean']
    >>> lon, lat = dataset.minor_axis, dataset.major_axis
    >>> surface_temp = dataset.ix[0]
    >>> fig, ax = plt.subplots()
    >>> cs = ax.pcolormesh(lon, lat, ma.masked_invalid(surface_temp.values))
    >>> fig.colorbar(cs)
    """

    uri = "http://data.nodc.noaa.gov/thredds/dodsC/woa/WOA09/NetCDFdata/"
    dataset = "%s_%s_%s.nc" % (var, clim_type, resolution)

    nc = Dataset(uri + dataset)
    latStart, latEnd = bbox[2], bbox[3]
    lonStart, lonEnd = bbox[0], bbox[1]

    v = dict(temperature='t', dissolved_oxygen='o', salinity='s',
             oxygen_saturation='O', apparent_oxygen_utilization='A',
             phosphate='p', silicate='p', nitrate='n')

    d = dict({'%s_an' % v[var]: 'OA Climatology',
              '%s_mn' % v[var]: 'Statistical Mean',
              '%s_dd' % v[var]: 'N. of Observations',
              '%s_se' % v[var]: 'Std Error of the Statistical Mean',
              '%s_sd' % v[var]: 'Std Deviation from Statistical Mean',
              '%s_oa' % v[var]: 'Statistical Mean minus OA Climatology',
              '%s_ma' % v[var]: 'Seasonal/Monthly minus Annual Climatology',
              '%s_gp' % v[var]: 'N. of Mean Values within Influence Radius'})

    lon = nc.variables.pop('lon')[:] - 360
    lat = nc.variables.pop('lat')[:]
    depth = nc.variables.pop('depth')[:]
    time = nc.variables.pop('time')
    time = num2date(time[:], time.units, calendar='365_day')
    months = [t.strftime('%b') for t in time]

    # Select data subset.
    maskx = np.logical_and(lon >= lonStart, lon <= lonEnd)
    masky = np.logical_and(lat >= latStart, lat <= latEnd)
    lon, lat = lon[maskx], lat[masky]

    start = '%s_' % v[var]
    dataset, clim = dict(), dict()
    for k, month in enumerate(months):
        for data in nc.variables.keys():
            if data.startswith(start):
                subset = nc.variables[data][..., masky, maskx].squeeze()
                if clim_type == 'annual':
                    panel = Panel(subset[...], items=depth, major_axis=lat,
                                  minor_axis=lon)
                    month = clim_type
                else:
                    panel = Panel(subset[k, ...], items=depth, major_axis=lat,
                                  minor_axis=lon)
                clim.update({d[data]: panel})
        dataset.update({month: clim})
    return dataset

def get_indices(min_lat, max_lat, min_lon, max_lon, lons, lats):
    r"""Return the data indices for a lon, lat square."""

    distances1, distances2, indices = [], [], []
    index = 1
    for point in lats:
        s1 = max_lat - point
        s2 = min_lat - point
        distances1.append((np.dot(s1, s1), point, index))
        distances2.append((np.dot(s2, s2), point, index - 1))
        index = index + 1

    distances1.sort()
    distances2.sort()
    indices.append(distances1[0])
    indices.append(distances2[0])

    distances1, distances2 = [], []
    index = 1
    for point in lons:
        s1 = max_lon - point
        s2 = min_lon - point
        distances1.append((np.dot(s1, s1), point, index))
        distances2.append((np.dot(s2, s2), point, index - 1))
        index = index + 1

    distances1.sort()
    distances2.sort()
    indices.append(distances1[0])
    indices.append(distances2[0])

    # max_lat_indices, min_lat_indices, max_lon_indices, min_lon_indices.
    res = np.zeros((4), dtype=np.float64)
    res[0] = indices[3][2]
    res[1] = indices[2][2]
    res[2] = indices[1][2]
    res[3] = indices[0][2]
    return res

def etopo_subset(llcrnrlon=None, urcrnrlon=None, llcrnrlat=None,
                 urcrnrlat=None, tfile='dap', smoo=False):
    r"""Get a etopo subset.
    Should work on any netCDF with x, y, data
    http://www.trondkristiansen.com/wp-content/uploads/downloads/
    2011/07/contourICEMaps.py

    Example
    -------
    >>> import matplotlib.pyplot as plt
    >>> offset = 5
    >>> tfile = '/home/filipe/00-NOBKP/OcFisData/ETOPO1_Bed_g_gmt4.grd'
    >>> toponame = basename(tfile)[0]
    >>> llcrnrlon, urcrnrlon, llcrnrlat, urcrnrlat = -43, -30, -22, -17
    >>> lons, lats, bathy = etopo_subset(llcrnrlon - offset,
    ...                                  urcrnrlon + offset,
    ...                                  llcrnrlat - offset,
    ...                                  urcrnrlat + offset,
    ...                                  smoo=True, tfile=tfile)
    >>> fig, ax = plt.subplots()
    >>> ax.pcolormesh(lons, lats, bathy)
    >>> ax.axis([-42, -28, -23, -15])
    >>> ax.set_title(toponame)
    >>> plt.show()
    """

    if tfile == 'dap':
        tfile = 'http://opendap.ccst.inpe.br/Misc/etopo2/ETOPO2v2c_f4.nc'

    etopo = Dataset(tfile, 'r')

    lons = etopo.variables["x"][:]
    lats = etopo.variables["y"][:]

    res = get_indices(llcrnrlat, urcrnrlat, llcrnrlon, urcrnrlon, lons, lats)

    lon, lat = np.meshgrid(lons[res[0]:res[1]], lats[res[2]:res[3]])

    bathy = etopo.variables["z"][int(res[2]):int(res[3]),
                                 int(res[0]):int(res[1])]

    if smoo:
        bathy = laplace_filter(bathy, M=None)

    return lon, lat, bathy

def laplace_X(F, M):
    r"""1D Laplace Filter in X-direction (axis=1)
    http://www.trondkristiansen.com/wp-content/uploads/downloads/
    2010/09/laplaceFilter.py"""

    jmax, imax = F.shape

    # Add strips of land
    F2 = np.zeros((jmax, imax + 2), dtype=F.dtype)
    F2[:, 1:-1] = F
    M2 = np.zeros((jmax, imax + 2), dtype=M.dtype)
    M2[:, 1:-1] = M

    MS = M2[:, 2:] + M2[:, :-2]
    FS = F2[:, 2:] * M2[:, 2:] + F2[:, :-2] * M2[:, :-2]

    return np.where(M > 0.5, (1 - 0.25 * MS) * F + 0.25 * FS, F)

def laplace_Y(F, M):
    r"""1D Laplace Filter in Y-direction (axis=1)
    http://www.trondkristiansen.com/wp-content/uploads/downloads/
    2010/09/laplaceFilter.py"""

    jmax, imax = F.shape

    # Add strips of land
    F2 = np.zeros((jmax + 2, imax), dtype=F.dtype)
    F2[1:-1, :] = F
    M2 = np.zeros((jmax + 2, imax), dtype=M.dtype)
    M2[1:-1, :] = M

    MS = M2[2:, :] + M2[:-2, :]
    FS = F2[2:, :] * M2[2:, :] + F2[:-2, :] * M2[:-2, :]

    return np.where(M > 0.5, (1 - 0.25 * MS) * F + 0.25 * FS, F)

def laplace_filter(F, M=None):
    r"""Laplace filter a 2D field with mask.  The mask may cause laplace_X and
    laplace_Y to not commute. Take average of both directions.
    http://www.trondkristiansen.com/wp-content/uploads/downloads/
    2010/09/laplaceFilter.py"""

    if not M:
        M = np.ones_like(F)

    return 0.5 * (laplace_X(laplace_Y(F, M), M) +
                  laplace_Y(laplace_X(F, M), M))

def get_depth(lon, lat, tfile='dap'):
    r"""Find the depths for each station on the etopo2 database."""
    lon, lat = map(np.atleast_1d, (lon, lat))

    lons, lats, bathy = etopo_subset(lat.min() - 5, lat.max() + 5,
                                     lon.min() - 5, lon.max() + 5,
                                     tfile=tfile, smoo=False)

    return get_profile(lons, lats, bathy, lon, lat, mode='nearest', order=3)

def get_isobath(lon, lat, iso=-200., tfile='dap'):
    r"""Find isobath."""
    plt.ioff()
    topo = get_depth(lon, lat, tfile=tfile)

    fig, ax = plt.subplots()
    cs = ax.contour(lon, lat, topo, [iso])
    path = cs.collections[0].get_paths()[0]
    del fig
    del ax
    del cs
    plt.ion()
    return path.vertices[:, 0], path.vertices[:, 1]
<commit_msg>Added a subsample keyword to etopo_subset.<commit_after># -*- coding: utf-8 -*-
#
# datasets.py
#
# purpose:  Functions to handle datasets.
# author:   Filipe P. A. Fernandes
# e-mail:   ocefpaf@gmail
# web:      http://ocefpaf.tiddlyspot.com/
# created:  09-Sep-2011
# modified: Fri 05 Jul 2013 02:26:49 PM BRT
#
# obs: some Functions were based on:
# http://www.trondkristiansen.com/?page_id=1071

from __future__ import division

import numpy as np
import matplotlib.pyplot as plt

from pandas import Panel
from netCDF4 import Dataset, num2date
from oceans.ff_tools import get_profile

__all__ = ['woa_subset',
           'etopo_subset',
           'laplace_X',
           'laplace_Y',
           'laplace_filter',
           'get_depth',
           'get_isobath']

def woa_subset(bbox=[-43, -29.5, -22.5, -17], var='temperature',
               clim_type='monthly', resolution='1deg'):
    r"""Get World Ocean Atlas variables at a given lon, lat bounding box.
    Choose variable from:
        `dissolved_oxygen`, `salinity`, `temperature`, `oxygen_saturation`,
        `apparent_oxygen_utilization`, `phosphate`, `silicate`, or `nitrate`.
    Choose clim_type averages from:
        `monthly`, `seasonal`, or `annual`.
    Choose resolution from:
        `1deg` or `5deg`

    Example
    -------
    >>> import matplotlib.pyplot as plt
    >>> bbox = [-59, -25, -38, 9]
    >>> dataset = woa_subset(bbox=bbox, var='temperature', clim_type='annual',
    ...                      resolution='5deg')
    >>> dataset = dataset['annual']['Statistical Mean']
    >>> lon, lat = dataset.minor_axis, dataset.major_axis
    >>> surface_temp = dataset.ix[0]
    >>> fig, ax = plt.subplots()
    >>> cs = ax.pcolormesh(lon, lat, ma.masked_invalid(surface_temp.values))
    >>> fig.colorbar(cs)
    """

    uri = "http://data.nodc.noaa.gov/thredds/dodsC/woa/WOA09/NetCDFdata/"
    dataset = "%s_%s_%s.nc" % (var, clim_type, resolution)

    nc = Dataset(uri + dataset)
    latStart, latEnd = bbox[2], bbox[3]
    lonStart, lonEnd = bbox[0], bbox[1]

    v = dict(temperature='t', dissolved_oxygen='o', salinity='s',
             oxygen_saturation='O', apparent_oxygen_utilization='A',
             phosphate='p', silicate='p', nitrate='n')

    d = dict({'%s_an' % v[var]: 'OA Climatology',
              '%s_mn' % v[var]: 'Statistical Mean',
              '%s_dd' % v[var]: 'N. of Observations',
              '%s_se' % v[var]: 'Std Error of the Statistical Mean',
              '%s_sd' % v[var]: 'Std Deviation from Statistical Mean',
              '%s_oa' % v[var]: 'Statistical Mean minus OA Climatology',
              '%s_ma' % v[var]: 'Seasonal/Monthly minus Annual Climatology',
              '%s_gp' % v[var]: 'N. of Mean Values within Influence Radius'})

    lon = nc.variables.pop('lon')[:] - 360
    lat = nc.variables.pop('lat')[:]
    depth = nc.variables.pop('depth')[:]
    time = nc.variables.pop('time')
    time = num2date(time[:], time.units, calendar='365_day')
    months = [t.strftime('%b') for t in time]

    # Select data subset.
    maskx = np.logical_and(lon >= lonStart, lon <= lonEnd)
    masky = np.logical_and(lat >= latStart, lat <= latEnd)
    lon, lat = lon[maskx], lat[masky]

    start = '%s_' % v[var]
    dataset, clim = dict(), dict()
    for k, month in enumerate(months):
        for data in nc.variables.keys():
            if data.startswith(start):
                subset = nc.variables[data][..., masky, maskx].squeeze()
                if clim_type == 'annual':
                    panel = Panel(subset[...], items=depth, major_axis=lat,
                                  minor_axis=lon)
                    month = clim_type
                else:
                    panel = Panel(subset[k, ...], items=depth, major_axis=lat,
                                  minor_axis=lon)
                clim.update({d[data]: panel})
        dataset.update({month: clim})
    return dataset

def get_indices(min_lat, max_lat, min_lon, max_lon, lons, lats):
    r"""Return the data indices for a lon, lat square."""

    distances1, distances2, indices = [], [], []
    index = 1
    for point in lats:
        s1 = max_lat - point
        s2 = min_lat - point
        distances1.append((np.dot(s1, s1), point, index))
        distances2.append((np.dot(s2, s2), point, index - 1))
        index = index + 1

    distances1.sort()
    distances2.sort()
    indices.append(distances1[0])
    indices.append(distances2[0])

    distances1, distances2 = [], []
    index = 1
    for point in lons:
        s1 = max_lon - point
        s2 = min_lon - point
        distances1.append((np.dot(s1, s1), point, index))
        distances2.append((np.dot(s2, s2), point, index - 1))
        index = index + 1

    distances1.sort()
    distances2.sort()
    indices.append(distances1[0])
    indices.append(distances2[0])

    # max_lat_indices, min_lat_indices, max_lon_indices, min_lon_indices.
    res = np.zeros((4), dtype=np.float64)
    res[0] = indices[3][2]
    res[1] = indices[2][2]
    res[2] = indices[1][2]
    res[3] = indices[0][2]
    return res

def etopo_subset(llcrnrlon=None, urcrnrlon=None, llcrnrlat=None,
                 urcrnrlat=None, tfile='dap', smoo=False, subsample=False):
    r"""Get a etopo subset.
    Should work on any netCDF with x, y, data
    http://www.trondkristiansen.com/wp-content/uploads/downloads/
    2011/07/contourICEMaps.py

    Example
    -------
    >>> import matplotlib.pyplot as plt
    >>> offset = 5
    >>> tfile = '/home/filipe/00-NOBKP/OcFisData/ETOPO1_Bed_g_gmt4.grd'
    >>> toponame = basename(tfile)[0]
    >>> llcrnrlon, urcrnrlon, llcrnrlat, urcrnrlat = -43, -30, -22, -17
    >>> lons, lats, bathy = etopo_subset(llcrnrlon - offset,
    ...                                  urcrnrlon + offset,
    ...                                  llcrnrlat - offset,
    ...                                  urcrnrlat + offset,
    ...                                  smoo=True, tfile=tfile)
    >>> fig, ax = plt.subplots()
    >>> ax.pcolormesh(lons, lats, bathy)
    >>> ax.axis([-42, -28, -23, -15])
    >>> ax.set_title(toponame)
    >>> plt.show()
    """

    if tfile == 'dap':
        tfile = 'http://opendap.ccst.inpe.br/Misc/etopo2/ETOPO2v2c_f4.nc'

    etopo = Dataset(tfile, 'r')

    lons = etopo.variables["x"][:]
    lats = etopo.variables["y"][:]

    res = get_indices(llcrnrlat, urcrnrlat, llcrnrlon, urcrnrlon, lons, lats)

    lon, lat = np.meshgrid(lons[res[0]:res[1]], lats[res[2]:res[3]])

    bathy = etopo.variables["z"][int(res[2]):int(res[3]),
                                 int(res[0]):int(res[1])]

    if smoo:
        bathy = laplace_filter(bathy, M=None)

    if subsample:
        lon, lat, bathy = lon[::subsample], lat[::subsample], bathy[::subsample]
    return lon, lat, bathy

def laplace_X(F, M):
    r"""1D Laplace Filter in X-direction (axis=1)
    http://www.trondkristiansen.com/wp-content/uploads/downloads/
    2010/09/laplaceFilter.py"""

    jmax, imax = F.shape

    # Add strips of land
    F2 = np.zeros((jmax, imax + 2), dtype=F.dtype)
    F2[:, 1:-1] = F
    M2 = np.zeros((jmax, imax + 2), dtype=M.dtype)
    M2[:, 1:-1] = M

    MS = M2[:, 2:] + M2[:, :-2]
    FS = F2[:, 2:] * M2[:, 2:] + F2[:, :-2] * M2[:, :-2]

    return np.where(M > 0.5, (1 - 0.25 * MS) * F + 0.25 * FS, F)

def laplace_Y(F, M):
    r"""1D Laplace Filter in Y-direction (axis=1)
    http://www.trondkristiansen.com/wp-content/uploads/downloads/
    2010/09/laplaceFilter.py"""

    jmax, imax = F.shape

    # Add strips of land
    F2 = np.zeros((jmax + 2, imax), dtype=F.dtype)
    F2[1:-1, :] = F
    M2 = np.zeros((jmax + 2, imax), dtype=M.dtype)
    M2[1:-1, :] = M

    MS = M2[2:, :] + M2[:-2, :]
    FS = F2[2:, :] * M2[2:, :] + F2[:-2, :] * M2[:-2, :]

    return np.where(M > 0.5, (1 - 0.25 * MS) * F + 0.25 * FS, F)

def laplace_filter(F, M=None):
    r"""Laplace filter a 2D field with mask.  The mask may cause laplace_X and
    laplace_Y to not commute. Take average of both directions.
    http://www.trondkristiansen.com/wp-content/uploads/downloads/
    2010/09/laplaceFilter.py"""

    if not M:
        M = np.ones_like(F)

    return 0.5 * (laplace_X(laplace_Y(F, M), M) +
                  laplace_Y(laplace_X(F, M), M))

def get_depth(lon, lat, tfile='dap'):
    r"""Find the depths for each station on the etopo2 database."""
    lon, lat = map(np.atleast_1d, (lon, lat))

    lons, lats, bathy = etopo_subset(lat.min() - 5, lat.max() + 5,
                                     lon.min() - 5, lon.max() + 5,
                                     tfile=tfile, smoo=False)

    return get_profile(lons, lats, bathy, lon, lat, mode='nearest', order=3)

def get_isobath(lon, lat, iso=-200., tfile='dap'):
    r"""Find isobath."""
    plt.ioff()
    topo = get_depth(lon, lat, tfile=tfile)

    fig, ax = plt.subplots()
    cs = ax.contour(lon, lat, topo, [iso])
    path = cs.collections[0].get_paths()[0]
    del fig
    del ax
    del cs
    plt.ion()
    return path.vertices[:, 0], path.vertices[:, 1]
<|endoftext|>


<commit_before>import sys, os, os.path
from ofxhome import OFXHome, Institution, InstitutionList
import unittest
import datetime

class InstitutionTestCase(unittest.TestCase):

    def testGoodParse(self):
        with testfile('scottrade.xml') as f:
            xml = f.read()
        i = Institution(xml)
        self.assertEquals(i.id,'623')
        self.assertEquals(i.name,'Scottrade, Inc.')
        self.assertEquals(i.fid,'777')
        self.assertEquals(i.org,'Scottrade')
        self.assertEquals(i.brokerid,'www.scottrade.com')
        self.assertEquals(i.url,'https://ofxstl.scottsave.com')
        self.assertEquals(i.ofxfail,'0')
        self.assertEquals(i.sslfail,'4')
        self.assertEquals(i.lastofxvalidation,datetime.datetime(2012,8,13,22,28,10))
        self.assertEquals(i.lastsslvalidation,datetime.datetime(2011,9,28,22,22,22))
        self.assertEquals(i.xml, xml)

    def testOptionalBroker(self):
        with testfile('jpmorgan.xml') as f:
            xml = f.read()
        i = Institution(xml)
        self.assertEquals(i.id,'435')
        self.assertEquals(i.name,'JPMorgan Chase Bank')
        self.assertEquals(i.fid,'1601')
        self.assertEquals(i.org,'Chase Bank')
        self.assertEquals(i.brokerid,'')
        self.assertEquals(i.url,'https://www.oasis.cfree.com/1601.ofxgp')
        self.assertEquals(i.ofxfail,'0')
        self.assertEquals(i.sslfail,'0')
        self.assertEquals(i.lastofxvalidation,datetime.datetime(2014,8,17,22,23,35))
        self.assertEquals(i.lastsslvalidation,datetime.datetime(2014,8,17,22,23,34))
        self.assertEquals(i.xml, xml)

    def testFromFile(self):
        i = Institution.from_file( testfile_name('scottrade.xml') )
        self.assertEquals(i.id,'623')
        self.assertEquals(i['id'],'623')

    def testDictKeys(self):
        with testfile('scottrade.xml') as f:
            xml = f.read()
        i = Institution(xml)
        self.assertEquals(i['id'],'623')
        self.assertEquals(i['name'],'Scottrade, Inc.')

        i['id'] = '123'
        self.assertEquals(i['id'],'123')

    def testBadParse(self):
        with testfile('badxml_bank.xml') as f:
            xml = f.read()
        try:
            l = Institution(xml)
            self.assertFalse(0)
        except Exception:
            self.assertTrue(1)

class InstitutionListTestCase(unittest.TestCase):

    def testFromFile(self):
        l = InstitutionList.from_file( testfile_name('search_america.xml') )
        self.assertEquals(len(l),15)

    def testGoodResult(self):
        with testfile('search_america.xml') as f:
            xml = f.read()
        l = InstitutionList(xml)
        self.assertEquals(len(l),15)
        self.assertEquals(l.xml,xml)
        self.assertEquals(l[0]['id'],'533')
        self.assertEquals(l[0]['name'],'America First Credit Union')

    def testResultWithPHPError(self):
        with testfile('search_noexist.xml') as f:
            xml = f.read()
        l = InstitutionList(xml)
        self.assertEquals(len(l),0)
        self.assertEquals(l.xml,xml)

    def testIterator(self):
        count = 0
        with testfile('search_america.xml') as f:
            xml = f.read()
        l = InstitutionList(xml)
        for i in l:
            count += 1
        self.assertEquals(count,15)

    def testBadXML(self):
        with testfile('badxml_search.xml') as f:
            xml = f.read()
        try:
            l = InstitutionList(xml)
            self.assertFalse(0)
        except Exception:
            self.assertTrue(1)

def testfile_name(filename):
    base_path = os.path.dirname(os.path.abspath(__file__))

    path = os.path.join(base_path,'testfiles',filename)
    if ('tests' in os.listdir('.')):
        path = os.path.join('tests',path)
    return path

def testfile(filename):
    return open(testfile_name(filename))

if __name__ == '__main__':
    unittest.main()
<commit_msg>assertEquals is deprecated<commit_after>import sys, os, os.path
from ofxhome import OFXHome, Institution, InstitutionList
import unittest
import datetime

class InstitutionTestCase(unittest.TestCase):

    def testGoodParse(self):
        with testfile('scottrade.xml') as f:
            xml = f.read()
        i = Institution(xml)
        self.assertEqual(i.id,'623')
        self.assertEqual(i.name,'Scottrade, Inc.')
        self.assertEqual(i.fid,'777')
        self.assertEqual(i.org,'Scottrade')
        self.assertEqual(i.brokerid,'www.scottrade.com')
        self.assertEqual(i.url,'https://ofxstl.scottsave.com')
        self.assertEqual(i.ofxfail,'0')
        self.assertEqual(i.sslfail,'4')
        self.assertEqual(i.lastofxvalidation,datetime.datetime(2012,8,13,22,28,10))
        self.assertEqual(i.lastsslvalidation,datetime.datetime(2011,9,28,22,22,22))
        self.assertEqual(i.xml, xml)

    def testOptionalBroker(self):
        with testfile('jpmorgan.xml') as f:
            xml = f.read()
        i = Institution(xml)
        self.assertEqual(i.id,'435')
        self.assertEqual(i.name,'JPMorgan Chase Bank')
        self.assertEqual(i.fid,'1601')
        self.assertEqual(i.org,'Chase Bank')
        self.assertEqual(i.brokerid,'')
        self.assertEqual(i.url,'https://www.oasis.cfree.com/1601.ofxgp')
        self.assertEqual(i.ofxfail,'0')
        self.assertEqual(i.sslfail,'0')
        self.assertEqual(i.lastofxvalidation,datetime.datetime(2014,8,17,22,23,35))
        self.assertEqual(i.lastsslvalidation,datetime.datetime(2014,8,17,22,23,34))
        self.assertEqual(i.xml, xml)

    def testFromFile(self):
        i = Institution.from_file( testfile_name('scottrade.xml') )
        self.assertEqual(i.id,'623')
        self.assertEqual(i['id'],'623')

    def testDictKeys(self):
        with testfile('scottrade.xml') as f:
            xml = f.read()
        i = Institution(xml)
        self.assertEqual(i['id'],'623')
        self.assertEqual(i['name'],'Scottrade, Inc.')

        i['id'] = '123'
        self.assertEqual(i['id'],'123')

    def testBadParse(self):
        with testfile('badxml_bank.xml') as f:
            xml = f.read()
        try:
            l = Institution(xml)
            self.assertFalse(0)
        except Exception:
            self.assertTrue(1)

class InstitutionListTestCase(unittest.TestCase):

    def testFromFile(self):
        l = InstitutionList.from_file( testfile_name('search_america.xml') )
        self.assertEqual(len(l),15)

    def testGoodResult(self):
        with testfile('search_america.xml') as f:
            xml = f.read()
        l = InstitutionList(xml)
        self.assertEqual(len(l),15)
        self.assertEqual(l.xml,xml)
        self.assertEqual(l[0]['id'],'533')
        self.assertEqual(l[0]['name'],'America First Credit Union')

    def testResultWithPHPError(self):
        with testfile('search_noexist.xml') as f:
            xml = f.read()
        l = InstitutionList(xml)
        self.assertEqual(len(l),0)
        self.assertEqual(l.xml,xml)

    def testIterator(self):
        count = 0
        with testfile('search_america.xml') as f:
            xml = f.read()
        l = InstitutionList(xml)
        for i in l:
            count += 1
        self.assertEqual(count,15)

    def testBadXML(self):
        with testfile('badxml_search.xml') as f:
            xml = f.read()
        try:
            l = InstitutionList(xml)
            self.assertFalse(0)
        except Exception:
            self.assertTrue(1)

def testfile_name(filename):
    base_path = os.path.dirname(os.path.abspath(__file__))

    path = os.path.join(base_path,'testfiles',filename)
    if ('tests' in os.listdir('.')):
        path = os.path.join('tests',path)
    return path

def testfile(filename):
    return open(testfile_name(filename))

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>import os
import sys
from django.contrib.messages import constants as messages

# Directory that contains this file.
PROJECT_SETTINGS_DIRECTORY = os.path.dirname(globals()['__file__'])
# Root directory. Contains manage.py
PROJECT_ROOT_DIRECTORY = os.path.join(PROJECT_SETTINGS_DIRECTORY, '..', '..')
#PROJECT_ROOT_DIRECTORY = os.path.dirname(os.path.dirname(__file__))

TEST_RUNNER = "django_nose.NoseTestSuiteRunner"
NOSE_ARGS = ['--with-coverage', '--cover-package=apps']

DEBUG = False
TEMPLATE_DEBUG = DEBUG

TEST_RUNNER = "django_nose.NoseTestSuiteRunner"

ADMINS = (
    ('dotKom', 'dotkom@online.ntnu.no'),
)
MANAGERS = ADMINS

# Email settings
DEFAULT_FROM_EMAIL = 'online@online.ntnu.no'
EMAIL_ARRKOM = 'arrkom@online.ntnu.no'
EMAIL_BEDKOM = 'bedkom@online.ntnu.no'
EMAIL_DOTKOM = 'dotkom@online.ntnu.no'
EMAIL_FAGKOM = 'fagkom@online.ntnu.no'
EMAIL_PROKOM = 'prokom@online.ntnu.no'
EMAIL_TRIKOM = 'trikom@online.ntnu.no'

# We will receive errors and other django messages from this email
SERVER_EMAIL = 'onlineweb4-error@online.ntnu.no'

TIME_ZONE = 'Europe/Oslo'
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'nb'
LANGUAGES = (
                ('nb', 'Norwegian'),
                ('en_US', 'English'),
            )

SITE_ID = 1
USE_I18N = True
USE_L10N = True
USE_TZ = False
DATETIME_FORMAT = 'N j, Y, H:i'
SECRET_KEY = 'q#wy0df(7&$ucfrxa1j72%do7ko*-6(g!8f$tc2$3x@3cq5@6c'

AUTH_USER_MODEL = 'authentication.OnlineUser'
LOGIN_URL = '/auth/login/'

MEDIA_ROOT = os.path.join(PROJECT_ROOT_DIRECTORY, 'uploaded_media') # Override this in local.py in prod.
MEDIA_URL = '/media/'

STATIC_ROOT = os.path.join(PROJECT_ROOT_DIRECTORY, 'static')
STATIC_URL = '/static/'

#Url of default profile picture
DEFAULT_PROFILE_PICTURE_URL = os.path.join(STATIC_URL, "img", "profile_default.png")

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT_DIRECTORY, 'files/static'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
    'compressor.finders.CompressorFinder',
)

COMPRESS_OUTPUT_DIR = 'cache'
COMPRESS_PRECOMPILERS = (
    ('text/less', 'lessc {infile} {outfile}'),
)

COMPRESS_CSS_FILTERS = [
    'compressor.filters.css_default.CssAbsoluteFilter',
    'compressor-filters.cssmin.CSSMinFilter',
]
COMPRESS_JS_FILTERS = [
    'compressor.filters.jsmin.JSMinFilter',
]

COMPRESS_PRECOMPILERS = (
    ('text/less', 'lessc -x {infile} {outfile}'),
)

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'middleware.http.Http403Middleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'onlineweb4.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'onlineweb4.wsgi.application'

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT_DIRECTORY, 'templates/')
)

# Grappelli settings
GRAPPELLI_ADMIN_TITLE = 'Onlineweb'

INSTALLED_APPS = (
    # Third party dependencies
    'django.contrib.humanize',
    'django_nose',
    'south',
    'django_notify',
    'mptt',
    'sekizai',
    'sorl.thumbnail',
    'grappelli',
    'filebrowser',
    'chunks',
    'crispy_forms',
    'django_extensions',
    'django_dynamic_fixture',
    'captcha',
    'compressor',
    'watson',

    # Django apps
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.markup',
    'django.contrib.messages',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.staticfiles',

    # Onlineweb 4 apps
    'apps.article',
    'apps.authentication',
    'apps.autoconfig',
    'apps.careeropportunity',
    'apps.companyprofile',
    'apps.events',
    'apps.marks',
    'apps.offline',
    'apps.feedback',
    'apps.mommy',
    'apps.profiles',

    # Wiki-related stuff
    'wiki',
    'wiki.plugins.attachments',
    'wiki.plugins.notifications',
    'wiki.plugins.images',
    'wiki.plugins.macros',
)

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'console':{
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'apscheduler.scheduler': {
            'handlers': ['console'],
            'level': 'INFO',
        },
    }
}

# crispy forms settings
CRISPY_TEMPLATE_PACK = 'bootstrap3'

# bootstrap messages classes
MESSAGE_TAGS = {messages.DEBUG: 'alert-debug',
                messages.INFO: 'alert-info',
                messages.SUCCESS: 'alert-success',
                messages.WARNING: 'alert-warning',
                messages.ERROR: 'alert-error'}

# Not really sure what this does.
# Has something to do with django-dynamic-fixture bumped from 1.6.4 to 1.6.5 in order to run a syncdb with mysql/postgres (OptimusCrime)
IMPORT_DDF_MODELS = False

# Required by the Wiki
TEMPLATE_CONTEXT_PROCESSORS = ("django.core.context_processors.request",
    "django.contrib.auth.context_processors.auth",)

# Remember to keep 'local' last, so it can override any setting.
for settings_module in ['filebrowser', 'local']:  # local last
    if not os.path.exists(os.path.join(PROJECT_SETTINGS_DIRECTORY,
            settings_module + ".py")):
        sys.stderr.write("Could not find settings module '%s'.\n" %
                settings_module)
        if settings_module == 'local':
            sys.stderr.write("You need to copy the settings file "
                             "'onlineweb4/settings/example-local.py' to "
                             "'onlineweb4/settings/local.py'.\n")
        sys.exit(1)
    try:
        exec('from %s import *' % settings_module)
    except ImportError, e:
        print "Could not import settings for '%s' : %s" % (settings_module,
                str(e))
<commit_msg>Forgot to add the default template context processors<commit_after>import os
import sys
from django.contrib.messages import constants as messages

# Directory that contains this file.
PROJECT_SETTINGS_DIRECTORY = os.path.dirname(globals()['__file__'])
# Root directory. Contains manage.py
PROJECT_ROOT_DIRECTORY = os.path.join(PROJECT_SETTINGS_DIRECTORY, '..', '..')
#PROJECT_ROOT_DIRECTORY = os.path.dirname(os.path.dirname(__file__))

TEST_RUNNER = "django_nose.NoseTestSuiteRunner"
NOSE_ARGS = ['--with-coverage', '--cover-package=apps']

DEBUG = False
TEMPLATE_DEBUG = DEBUG

TEST_RUNNER = "django_nose.NoseTestSuiteRunner"

ADMINS = (
    ('dotKom', 'dotkom@online.ntnu.no'),
)
MANAGERS = ADMINS

# Email settings
DEFAULT_FROM_EMAIL = 'online@online.ntnu.no'
EMAIL_ARRKOM = 'arrkom@online.ntnu.no'
EMAIL_BEDKOM = 'bedkom@online.ntnu.no'
EMAIL_DOTKOM = 'dotkom@online.ntnu.no'
EMAIL_FAGKOM = 'fagkom@online.ntnu.no'
EMAIL_PROKOM = 'prokom@online.ntnu.no'
EMAIL_TRIKOM = 'trikom@online.ntnu.no'

# We will receive errors and other django messages from this email
SERVER_EMAIL = 'onlineweb4-error@online.ntnu.no'

TIME_ZONE = 'Europe/Oslo'
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'nb'
LANGUAGES = (
                ('nb', 'Norwegian'),
                ('en_US', 'English'),
            )

SITE_ID = 1
USE_I18N = True
USE_L10N = True
USE_TZ = False
DATETIME_FORMAT = 'N j, Y, H:i'
SECRET_KEY = 'q#wy0df(7&$ucfrxa1j72%do7ko*-6(g!8f$tc2$3x@3cq5@6c'

AUTH_USER_MODEL = 'authentication.OnlineUser'
LOGIN_URL = '/auth/login/'

MEDIA_ROOT = os.path.join(PROJECT_ROOT_DIRECTORY, 'uploaded_media') # Override this in local.py in prod.
MEDIA_URL = '/media/'

STATIC_ROOT = os.path.join(PROJECT_ROOT_DIRECTORY, 'static')
STATIC_URL = '/static/'

#Url of default profile picture
DEFAULT_PROFILE_PICTURE_URL = os.path.join(STATIC_URL, "img", "profile_default.png")

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT_DIRECTORY, 'files/static'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
    'compressor.finders.CompressorFinder',
)

COMPRESS_OUTPUT_DIR = 'cache'
COMPRESS_PRECOMPILERS = (
    ('text/less', 'lessc {infile} {outfile}'),
)

COMPRESS_CSS_FILTERS = [
    'compressor.filters.css_default.CssAbsoluteFilter',
    'compressor-filters.cssmin.CSSMinFilter',
]
COMPRESS_JS_FILTERS = [
    'compressor.filters.jsmin.JSMinFilter',
]

COMPRESS_PRECOMPILERS = (
    ('text/less', 'lessc -x {infile} {outfile}'),
)

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'middleware.http.Http403Middleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'onlineweb4.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'onlineweb4.wsgi.application'

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT_DIRECTORY, 'templates/')
)

# Grappelli settings
GRAPPELLI_ADMIN_TITLE = 'Onlineweb'

INSTALLED_APPS = (
    # Third party dependencies
    'django.contrib.humanize',
    'django_nose',
    'south',
    'django_notify',
    'mptt',
    'sekizai',
    'sorl.thumbnail',
    'grappelli',
    'filebrowser',
    'chunks',
    'crispy_forms',
    'django_extensions',
    'django_dynamic_fixture',
    'captcha',
    'compressor',
    'watson',

    # Django apps
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.markup',
    'django.contrib.messages',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.staticfiles',

    # Onlineweb 4 apps
    'apps.article',
    'apps.authentication',
    'apps.autoconfig',
    'apps.careeropportunity',
    'apps.companyprofile',
    'apps.events',
    'apps.marks',
    'apps.offline',
    'apps.feedback',
    'apps.mommy',
    'apps.profiles',

    # Wiki-related stuff
    'wiki',
    'wiki.plugins.attachments',
    'wiki.plugins.notifications',
    'wiki.plugins.images',
    'wiki.plugins.macros',
)

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'console':{
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'apscheduler.scheduler': {
            'handlers': ['console'],
            'level': 'INFO',
        },
    }
}

# crispy forms settings
CRISPY_TEMPLATE_PACK = 'bootstrap3'

# bootstrap messages classes
MESSAGE_TAGS = {messages.DEBUG: 'alert-debug',
                messages.INFO: 'alert-info',
                messages.SUCCESS: 'alert-success',
                messages.WARNING: 'alert-warning',
                messages.ERROR: 'alert-error'}

# Not really sure what this does.
# Has something to do with django-dynamic-fixture bumped from 1.6.4 to 1.6.5 in order to run a syncdb with mysql/postgres (OptimusCrime)
IMPORT_DDF_MODELS = False

# Required by the Wiki
TEMPLATE_CONTEXT_PROCESSORS = ("django.core.context_processors.request",
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages")
USE_TZ = True

# Remember to keep 'local' last, so it can override any setting.
for settings_module in ['filebrowser', 'local']:  # local last
    if not os.path.exists(os.path.join(PROJECT_SETTINGS_DIRECTORY,
            settings_module + ".py")):
        sys.stderr.write("Could not find settings module '%s'.\n" %
                settings_module)
        if settings_module == 'local':
            sys.stderr.write("You need to copy the settings file "
                             "'onlineweb4/settings/example-local.py' to "
                             "'onlineweb4/settings/local.py'.\n")
        sys.exit(1)
    try:
        exec('from %s import *' % settings_module)
    except ImportError, e:
        print "Could not import settings for '%s' : %s" % (settings_module,
                str(e))
<|endoftext|>


<commit_before>import os

DEBUG = True
TEMPLATE_DEBUG = DEBUG

SECRET_KEY = 'pvh9d)+7aui4=evh$yv!qgbr3oyz-4=^oj_%6g8+v57b=de5)7'
SETTINGS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.abspath(os.path.dirname(SETTINGS_ROOT))
WSGI_APPLICATION = 'openbudget.wsgi.application'

ROOT_URLCONF = 'openbudget.ui.urls'
SUBDOMAIN_URLCONFS = {
    '': 'openbudget.ui.urls',
    'www': 'openbudget.ui.urls',
    'he': 'openbudget.ui.urls',
    'en': 'openbudget.ui.urls',
    'ru': 'openbudget.ui.urls',
    'ar': 'openbudget.ui.urls',
    'api': 'openbudget.api.urls',
}

SITE_ID = 1
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.abspath(os.path.join(os.path.dirname(PROJECT_ROOT), 'local.db')),
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

MEDIA_ROOT = os.path.abspath(
    os.path.join(os.path.dirname(PROJECT_ROOT),
        'static',
        'media'
    )
)

MEDIA_URL = '/static/media/'

STATIC_ROOT = os.path.abspath(
    os.path.join(os.path.dirname(PROJECT_ROOT),
        'static'
    )
)

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.abspath(
        os.path.join(PROJECT_ROOT, 'commons', 'static')
    ),
)

TEMPLATE_DIRS = (
    os.path.abspath(
        os.path.join(PROJECT_ROOT, 'commons', 'templates')
    ),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'subdomains.middleware.SubdomainURLRoutingMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'openbudget.apps.international.middleware.InterfaceLanguage',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.comments',
    'grappelli.dashboard',
    'grappelli',
    'grappelli_modeltranslation',
    'django.contrib.admin',
    'django.contrib.sitemaps',
    'gunicorn',
    'south',
    'haystack',
    'djcelery',
    'subdomains',
    'registration',
    'rest_framework',
    'rosetta_grappelli',
    'rosetta',
    'modeltranslation',
    'taggit',
    'openbudget.apps.accounts',
    'openbudget.apps.budgets',
    'openbudget.apps.contexts',
    'openbudget.apps.entities',
    'openbudget.apps.interactions',
    'openbudget.apps.international',
    'openbudget.apps.pages',
    'openbudget.apps.sources',
    'openbudget.apps.taxonomies',
    'openbudget.apps.transport',
    'openbudget.api',
    'openbudget.commons',
)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
    'openbudget.commons.context_processors.get_site',
)

# FIXTURE CONF
FIXTURE_DIRS = (
    os.path.abspath(
        os.path.join(PROJECT_ROOT, 'fixtures')
    ),
)

# LANGUAGE CONF
LANGUAGE_CODE = 'en'
MODELTRANSLATION_DEFAULT_LANGUAGE = LANGUAGE_CODE
gettext = lambda s: s
LANGUAGES = (
    ('en', gettext('English')),
    ('he', gettext('Hebrew')),
    ('ar', gettext('Arabic')),
    ('ru', gettext('Russian')),
)

# UNICODE SLUG CONF
AUTOSLUG_SLUGIFY_FUNCTION = 'slugify.slugify'

# USER ACCOUNT CONF
ACCOUNT_ACTIVATION_DAYS = 7
AUTH_PROFILE_MODULE = 'accounts.UserProfile'
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_URL = '/accounts/logout/'
ABSOLUTE_URL_OVERRIDES = {
    'auth.user': lambda u: "/accounts/%s/" % u.get_profile.uuid,
}

# GRAPPELLI CONF
GRAPPELLI_ADMIN_TITLE = 'Open Budget'
GRAPPELLI_INDEX_DASHBOARD = 'openbudget.dashboard.OpenBudgetDashboard'

# DJANGO REST FRAMEWORK CONF
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.AllowAny',
    ),
    'PAGINATE_BY': 10
}

# HAYSTACK CONF

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
        'PATH': os.path.join(PROJECT_ROOT, 'commons', 'search', 'index'),
    },
}

# CELERY CONF
from celery.schedules import crontab
import djcelery
djcelery.setup_loader()
BROKER_URL = 'redis://127.0.0.1:6379/'
CELERYBEAT_SCHEDULE = {
    "update_index": {
        "task": "tasks.update_index",
        "schedule": crontab(
           minute=0,
            hour=0
        ),
    },
    "rebuild_index": {
        "task": "tasks.rebuild_index",
        "schedule": crontab(
            day_of_week='saturday',
            minute=0,
            hour=0
        ),
    }
}

# EMAIL CONF
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''

# SENTRY CONF
SENTRY_DSN = ''

# DEVELOPER ADMINS CONF
ADMINS = (
    ('', ''),
    ('', ''),
)

# OPEN BUDGET CUSTOM CONF
TEMP_FILES_DIR = os.path.abspath(os.path.join(os.path.dirname(PROJECT_ROOT), 'tmp'))
<commit_msg>Defauly LANGUAGE_CODE is now always the first item in the LANGUAGES tuple.<commit_after>import os

DEBUG = True
TEMPLATE_DEBUG = DEBUG

SECRET_KEY = 'pvh9d)+7aui4=evh$yv!qgbr3oyz-4=^oj_%6g8+v57b=de5)7'
SETTINGS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.abspath(os.path.dirname(SETTINGS_ROOT))
WSGI_APPLICATION = 'openbudget.wsgi.application'

ROOT_URLCONF = 'openbudget.ui.urls'
SUBDOMAIN_URLCONFS = {
    '': 'openbudget.ui.urls',
    'www': 'openbudget.ui.urls',
    'he': 'openbudget.ui.urls',
    'en': 'openbudget.ui.urls',
    'ru': 'openbudget.ui.urls',
    'ar': 'openbudget.ui.urls',
    'api': 'openbudget.api.urls',
}

SITE_ID = 1
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.abspath(os.path.join(os.path.dirname(PROJECT_ROOT), 'local.db')),
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

MEDIA_ROOT = os.path.abspath(
    os.path.join(os.path.dirname(PROJECT_ROOT),
        'static',
        'media'
    )
)

MEDIA_URL = '/static/media/'

STATIC_ROOT = os.path.abspath(
    os.path.join(os.path.dirname(PROJECT_ROOT),
        'static'
    )
)

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.abspath(
        os.path.join(PROJECT_ROOT, 'commons', 'static')
    ),
)

TEMPLATE_DIRS = (
    os.path.abspath(
        os.path.join(PROJECT_ROOT, 'commons', 'templates')
    ),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'subdomains.middleware.SubdomainURLRoutingMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'openbudget.apps.international.middleware.InterfaceLanguage',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.comments',
    'grappelli.dashboard',
    'grappelli',
    'grappelli_modeltranslation',
    'django.contrib.admin',
    'django.contrib.sitemaps',
    'gunicorn',
    'south',
    'haystack',
    'djcelery',
    'subdomains',
    'registration',
    'rest_framework',
    'rosetta_grappelli',
    'rosetta',
    'modeltranslation',
    'taggit',
    'openbudget.apps.accounts',
    'openbudget.apps.budgets',
    'openbudget.apps.contexts',
    'openbudget.apps.entities',
    'openbudget.apps.interactions',
    'openbudget.apps.international',
    'openbudget.apps.pages',
    'openbudget.apps.sources',
    'openbudget.apps.taxonomies',
    'openbudget.apps.transport',
    'openbudget.api',
    'openbudget.commons',
)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
    'openbudget.commons.context_processors.get_site',
)

# FIXTURE CONF
FIXTURE_DIRS = (
    os.path.abspath(
        os.path.join(PROJECT_ROOT, 'fixtures')
    ),
)

# LANGUAGE CONF
gettext = lambda s: s
LANGUAGES = (
    ('en', gettext('English')),
    ('he', gettext('Hebrew')),
    ('ar', gettext('Arabic')),
    ('ru', gettext('Russian')),
)
LANGUAGE_CODE = LANGUAGES[0][0]
MODELTRANSLATION_DEFAULT_LANGUAGE = LANGUAGE_CODE

# UNICODE SLUG CONF
AUTOSLUG_SLUGIFY_FUNCTION = 'slugify.slugify'

# USER ACCOUNT CONF
ACCOUNT_ACTIVATION_DAYS = 7
AUTH_PROFILE_MODULE = 'accounts.UserProfile'
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_URL = '/accounts/logout/'
ABSOLUTE_URL_OVERRIDES = {
    'auth.user': lambda u: "/accounts/%s/" % u.get_profile.uuid,
}

# GRAPPELLI CONF
GRAPPELLI_ADMIN_TITLE = 'Open Budget'
GRAPPELLI_INDEX_DASHBOARD = 'openbudget.dashboard.OpenBudgetDashboard'

# DJANGO REST FRAMEWORK CONF
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.AllowAny',
    ),
    'PAGINATE_BY': 10
}

# HAYSTACK CONF

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
        'PATH': os.path.join(PROJECT_ROOT, 'commons', 'search', 'index'),
    },
}

# CELERY CONF
from celery.schedules import crontab
import djcelery
djcelery.setup_loader()
BROKER_URL = 'redis://127.0.0.1:6379/'
CELERYBEAT_SCHEDULE = {
    "update_index": {
        "task": "tasks.update_index",
        "schedule": crontab(
           minute=0,
            hour=0
        ),
    },
    "rebuild_index": {
        "task": "tasks.rebuild_index",
        "schedule": crontab(
            day_of_week='saturday',
            minute=0,
            hour=0
        ),
    }
}

# EMAIL CONF
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''

# SENTRY CONF
SENTRY_DSN = ''

# DEVELOPER ADMINS CONF
ADMINS = (
    ('', ''),
    ('', ''),
)

# OPEN BUDGET CUSTOM CONF
TEMP_FILES_DIR = os.path.abspath(os.path.join(os.path.dirname(PROJECT_ROOT), 'tmp'))
<|endoftext|>


<commit_before>import argparse
import traceback
import sys
import os
import cv2
import signal
from opencv_home_cam import HomeCamManager, HomeCamException

description = "OpenCV home cam test app"

def signal_handler(signal, frame):

    global hcm

    sys.stderr.write('Signal received!')
    hcm.stop()

def load_options():

    global parsed_args
    parser = argparse.ArgumentParser(prog="home_cam_tester",
                                     description=description)

    parser.add_argument('-a', '--cascade-file', nargs='+', type=str,
                             help="Cascade file(s) for OpenCV.")
    parser.add_argument('-c', '--config-file',
                             help="Configuraton file.")

    parsed_args = parser.parse_args()

def main():
    global parsed_args
    global hcm
    load_options()
    hcm = None

    try:

        if not parsed_args.cascade_file:
            sys.stderr.write('Missing input cascade file(s)\n')
            exit(1)

        if not parsed_args.config_file:
            sys.stderr.write('Missing input config file\n')
            exit(1)

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        hcm = HomeCamManager(config_file=parsed_args.config_file,
                             cascade_files=parsed_args.cascade_file)

        hcm.start()
        hcm.wait()

    except IOError as err:
        sys.stderr.write('{}\n'.format(err))
    except HomeCamException as err:
        sys.stderr.write('{}\n'.format(err))
    except:
        traceback.print_exc()

if __name__ == "__main__":
    main()
<commit_msg>add logging option<commit_after>import argparse
import traceback
import sys
import os
import cv2
import signal
import logging
import logging.config
from opencv_home_cam import HomeCamManager, HomeCamException

description = "OpenCV home cam test app"

def signal_handler(signal, frame):

    global hcm

    sys.stderr.write('Signal received!')
    hcm.stop()

def load_options():

    global parsed_args
    parser = argparse.ArgumentParser(prog="home_cam_tester",
                                     description=description)

    parser.add_argument('-a', '--cascade-file', nargs='+', type=str,
                             help="Cascade file(s) for OpenCV.")
    parser.add_argument('-c', '--config-file',
                             help="Configuraton file.")
    parser.add_argument('-l', '--log-config-file',
                             help="Logging configuraton file. This is the "
                                  "configuration for the Python logging "
                                  "facility. See the Python documentation "
                                  "for more details.")

    parsed_args = parser.parse_args()

def main():
    global parsed_args
    global hcm
    load_options()
    hcm = None

    try:

        if not parsed_args.cascade_file:
            sys.stderr.write('Missing input cascade file(s)\n')
            exit(1)

        if not parsed_args.config_file:
            sys.stderr.write('Missing input config file\n')
            exit(1)

        if parsed_args.log_config_file:
            logging.config.fileConfig(parsed_args.log_config_file)
        else:
            sys.stderr.write("Missing logger configuration.\n"
                             "Default configuration will be used.\n")
            logging.basicConfig(stream=sys.stderr,
                                level=logging.DEBUG,
                                format='%(asctime)s %(message)s')

        logger = logging.getLogger(__name__)

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        hcm = HomeCamManager(config_file=parsed_args.config_file,
                             cascade_files=parsed_args.cascade_file)

        hcm.start()
        logger.info("Waiting for HomeCamManager to finish\n")
        hcm.wait()
        logger.info("HomeCamManager finished\n")

    except IOError as err:
        sys.stderr.write('{}\n'.format(err))
    except HomeCamException as err:
        sys.stderr.write('{}\n'.format(err))
    except:
        traceback.print_exc()

if __name__ == "__main__":
    main()
<|endoftext|>