diff --git a/src/PseudoNetCDF/test/test_toms.py b/src/PseudoNetCDF/test/test_toms.py new file mode 100644 index 00000000..7722f342 --- /dev/null +++ b/src/PseudoNetCDF/test/test_toms.py @@ -0,0 +1,4 @@ +__all__ = ['test_tomsl3'] + + +from PseudoNetCDF.toms.level3 import test_tomsl3 diff --git a/src/PseudoNetCDF/testcase/__init__.py b/src/PseudoNetCDF/testcase/__init__.py index 11c8f6fb..f51faabe 100644 --- a/src/PseudoNetCDF/testcase/__init__.py +++ b/src/PseudoNetCDF/testcase/__init__.py @@ -1,6 +1,6 @@ __all__ = ['camxfiles_paths', 'net_balance_paths', 'geoschemfiles_paths', 'icarttfiles_paths', 'all_paths', 'self_described_paths', - 'cmaqfiles_paths'] + 'cmaqfiles_paths', 'toms_paths'] from os.path import join, abspath @@ -44,6 +44,9 @@ icarttfiles_paths = dict(ffi1001=join( *__path__ + ['icarttfiles', 'test.ffi1001'])) +toms_paths = dict(tomsl3=join( + *__path__ + ['toms', 'test.txt'])) + ceilometerfiles_paths = dict( vaisala=join( *__path__ + diff --git a/src/PseudoNetCDF/toms/test.txt b/src/PseudoNetCDF/testcase/toms/test.txt similarity index 100% rename from src/PseudoNetCDF/toms/test.txt rename to src/PseudoNetCDF/testcase/toms/test.txt diff --git a/src/PseudoNetCDF/toms/level3.py b/src/PseudoNetCDF/toms/level3.py index 92eea2e4..15a6be74 100644 --- a/src/PseudoNetCDF/toms/level3.py +++ b/src/PseudoNetCDF/toms/level3.py @@ -7,6 +7,7 @@ from re import compile import numpy as np from datetime import datetime +import unittest dayre = compile(' Day:\s+(?P\d+) (?P.{12})\s+EP/' + @@ -30,7 +31,10 @@ def _groupdict(reo, line): def cdtoms(path, outfile=None): if outfile is None: outfile = PseudoNetCDFFile() - inlines = open(path, 'r').readlines() + if hasattr(path, 'readlines'): + inlines = path.readlines() + else: + inlines = open(path, 'r').readlines() dayline = inlines[0] daygrp = _groupdict(dayre, dayline) @@ -45,10 +49,10 @@ def cdtoms(path, outfile=None): for i in [3, 2, 1]: daystr = ' '.join(dayparts[:i]) try: - date = pd.to_datetime(daystr, box=False) + date = pd.to_datetime(daystr).to_numpy() break except Exception as e: - print(e) + warn(e) else: date = np.datetime64('1970-01-01') rdate = np.datetime64('1970-01-01') @@ -93,7 +97,7 @@ def cdtoms(path, outfile=None): lats.append(lat.strip()) nlats = len(lats) - datablock = ''.join(datalines).replace(' ', '0') + datablock = ''.join(datalines).replace(' ', '0').replace('***', '999') nlons = len(datablock) // 3 // nlats outfile.createDimension('time', 1) outfile.createDimension('latitude', nlats) @@ -128,12 +132,12 @@ def cdtoms(path, outfile=None): var[:, 1] = lon + lonstep / 2. var = outfile.createVariable( - 'ozone', 'f', ('latitude', 'longitude'), + 'ozone', 'f', ('time', 'latitude', 'longitude'), missing_value=999 ) var.units = 'matm-cm' var.long_name = var.var_desc = 'ozone'.ljust(16) - var[:] = np.ma.masked_values( + var[0] = np.ma.masked_values( np.array( [i for i in datablock], dtype='S1' @@ -149,6 +153,22 @@ def __init__(self, path): cdtoms(path, self) +class test_tomsl3(unittest.TestCase): + def setUp(self): + from PseudoNetCDF.testcase import toms_paths + self.tomspath = toms_paths['tomsl3'] + + def testTOMS2NCF(self): + import os + vfile = tomsl3(self.tomspath) + outpath = self.tomspath + '.nc' + ncfile = vfile.save(outpath) + for k, ncv in ncfile.variables.items(): + vpv = vfile.variables[k] + np.testing.assert_allclose(ncv[...], vpv[...]) + os.remove(outpath) + + if __name__ == '__main__': from PseudoNetCDF.pncdump import pncdump pfile = cdtoms('test.txt')