Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating tomsl3 and cdtoms #131

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/PseudoNetCDF/test/test_toms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__all__ = ['test_tomsl3']


from PseudoNetCDF.toms.level3 import test_tomsl3
5 changes: 4 additions & 1 deletion src/PseudoNetCDF/testcase/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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__ +
Expand Down
File renamed without changes.
32 changes: 26 additions & 6 deletions src/PseudoNetCDF/toms/level3.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from re import compile
import numpy as np
from datetime import datetime
import unittest


dayre = compile(' Day:\s+(?P<jday>\d+) (?P<daystring>.{12})\s+EP/' +
Expand All @@ -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)

Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'
Expand All @@ -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')
Expand Down