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

import os
import json
import unittest
from collections import namedtuple

import numpy as np
import netCDF4 as nc4

from gutils.gbdr import (
    GliderBDReader,
    MergedGliderBDReader
)

from gutils.nc import open_glider_netcdf
from gutils.scripts.create_glider_netcdf import process_dataset

import logging
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)


def decoder(x):
    return str(x.decode('utf-8'))


def resource(nm):
    return os.path.join(
        os.path.dirname(__file__),
        'resources',
        'usf-bass',
        nm
    )


def output(*args):
    return os.path.join(
        os.path.dirname(__file__),
        'output',
        *args
    )


class TestCreateGliderScript(unittest.TestCase):

    def test_script(self):
        nt = namedtuple('Arguments', [
            'flight',
            'science',
            'time',
            'depth',
            'gps_prefix',
            'segment_id',
            'mode',
            'glider_config_path',
            'output_path'
        ])

        args = nt(
            flight=resource('usf-bass-2014-061-1-0.sbd'),
            science=resource('usf-bass-2014-061-1-0.tbd'),
            time='timestamp',
            depth='m_depth-m',
            gps_prefix='m_gps_',
            segment_id=None,
            mode='rt',
            glider_config_path=resource('.'),
            output_path=os.path.join(os.path.dirname(__file__), 'output')
        )

        process_dataset(args)

        assert len(os.listdir(output('bass-20150407T1300Z'))) == 5
        assert os.path.isfile(output('bass-20150407T1300Z', 'bass_20140303T095556Z_rt.nc'))
        assert os.path.isfile(output('bass-20150407T1300Z', 'bass_20140303T100510Z_rt.nc'))
        assert os.path.isfile(output('bass-20150407T1300Z', 'bass_20140303T101015Z_rt.nc'))
        assert os.path.isfile(output('bass-20150407T1300Z', 'bass_20140303T101624Z_rt.nc'))
        assert os.path.isfile(output('bass-20150407T1300Z', 'bass_20140303T102040Z_rt.nc'))


class TestMergedGliderDataReader(unittest.TestCase):

    def setUp(self):
        # Load NetCDF Configs
        with open(resource('deployment.json'), 'r') as f:
            self.deployment = json.loads(f.read())

        with open(resource('global_attributes.json'), 'r') as f:
            self.global_attributes = json.loads(f.read())
        self.global_attributes.update(self.deployment['global_attributes'])

        with open(resource('instruments.json'), 'r') as f:
            self.instruments = json.loads(f.read())

        self.test_path = os.path.join(
            os.path.dirname(__file__),
            'output',
            'test.nc'
        )
        try:
            os.makedirs(os.path.dirname(self.test_path))<commit_msg>Remove output files after testing<commit_after>#!/usr/bin/env python

import os
import json
import shutil
import unittest
from collections import namedtuple

import numpy as np
import netCDF4 as nc4

from gutils.gbdr import (
    GliderBDReader,
    MergedGliderBDReader
)

from gutils.nc import open_glider_netcdf
from gutils.scripts.create_glider_netcdf import process_dataset

import logging
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)


def decoder(x):
    return str(x.decode('utf-8'))


def resource(*args):
    return os.path.join(
        os.path.dirname(__file__),
        'resources',
        'usf-bass',
        *args
    )


def output(*args):
    return os.path.join(
        os.path.dirname(__file__),
        'output',
        *args
    )


class TestCreateGliderScript(unittest.TestCase):

    def tearDown(self):
        shutil.rmtree(output())

    def test_script(self):
        nt = namedtuple('Arguments', [
            'flight',
            'science',
            'time',
            'depth',
            'gps_prefix',
            'segment_id',
            'mode',
            'glider_config_path',
            'output_path'
        ])

        args = nt(
            flight=resource('usf-bass-2014-061-1-0.sbd'),
            science=resource('usf-bass-2014-061-1-0.tbd'),
            time='timestamp',
            depth='m_depth-m',
            gps_prefix='m_gps_',
            segment_id=None,
            mode='rt',
            glider_config_path=resource(),
            output_path=output(),
        )

        process_dataset(args)

        assert len(os.listdir(output('bass-20150407T1300Z'))) == 5
        assert os.path.isfile(output('bass-20150407T1300Z', 'bass_20140303T095556Z_rt.nc'))
        assert os.path.isfile(output('bass-20150407T1300Z', 'bass_20140303T100510Z_rt.nc'))
        assert os.path.isfile(output('bass-20150407T1300Z', 'bass_20140303T101015Z_rt.nc'))
        assert os.path.isfile(output('bass-20150407T1300Z', 'bass_20140303T101624Z_rt.nc'))
        assert os.path.isfile(output('bass-20150407T1300Z', 'bass_20140303T102040Z_rt.nc'))


class TestMergedGliderDataReader(unittest.TestCase):

    def tearDown(self):
        shutil.rmtree(output())

    def setUp(self):
        # Load NetCDF Configs
        with open(resource('deployment.json'), 'r') as f:
            self.deployment = json.loads(f.read())

        with open(resource('global_attributes.json'), 'r') as f:
            self.global_attributes = json.loads(f.read())
        self.global_attributes.update(self.deployment['global_attributes'])

        with open(resource('instruments.json'), 'r') as f:
            self.instruments = json.loads(f.read())

        self.test_path = os.path.join(
            os.path.dirname(__file__),
            'output',
            'test.nc'
        )
        try:
            os.makedirs(os.path.dirname(self.test_path))
{'fraction_non_alphanumeric': 0.1266724587315378, 'fraction_numerical': 0.056993918331885315, 'mean_word_length': 2.4653822998193857, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '759085', 'n_tokens_mistral': 2111, 'n_tokens_neox': 1901, 'n_words': 290}