Skip to content

Commit

Permalink
Merge branch 'example_files' into pyparticleprobe_merge
Browse files Browse the repository at this point in the history
ENH: ParsivelReader functional test. Template for further reader tests.
  • Loading branch information
nguy committed Sep 7, 2016
2 parents cbf311e + 026a2bc commit e5ae077
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 37 deletions.
3 changes: 2 additions & 1 deletion pydisdrometer/DSDProcessor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import division
import numpy as np
from pytmatrix.tmatrix import Scatterer
from pytmatrix.psd import PSDIntegrator, GammaPSD
from pytmatrix import orientation, radar, tmatrix_aux, refractive
import DSR
from . import DSR

class DSDProcessor:

Expand Down
6 changes: 4 additions & 2 deletions pydisdrometer/DSR.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import division
import numpy as np

'''
The DSR module contains different drop shape relationships used in
pydisdrometer for the scattering calculations.
PyDisdrometer for the scattering calculations.
'''

import numpy as np


def tb(D_eq):
Expand Down
20 changes: 7 additions & 13 deletions pydisdrometer/DropSizeDistribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

import numpy as np
import pytmatrix
import scipy
from scipy.optimize import curve_fit

from pytmatrix.tmatrix import Scatterer
from pytmatrix.psd import PSDIntegrator
from pytmatrix import orientation, radar, tmatrix_aux, refractive
from . import DSR
from datetime import date
from expfit import expfit, expfit2
from scipy.optimize import curve_fit
import scipy
from .expfit import expfit, expfit2

from . import DSR
from .utility import dielectric

SPEED_OF_LIGHT=299792458
Expand Down Expand Up @@ -128,7 +129,7 @@ def __init__(self, reader, time_start = None, location=None,):
self.diameter = reader.diameter
except:
self.diameter = None
self.fields = {}
self.fields = reader.fields
self.time_start = time_start

self.numt = len(reader.time['data'])
Expand Down Expand Up @@ -332,7 +333,7 @@ def __get_last_nonzero(self, N):
last nonzero entry in an array.
'''

if np.count_nonzero(N):
if np.ma.count(N):
return self.diameter['data'][np.max(N.nonzero())]
else:
return 0
Expand Down Expand Up @@ -545,10 +546,3 @@ def _mu_cost(self, mu, idx):
return np.sqrt(np.nansum(np.power(np.abs(self.Nd['data'][idx] - gdsd(self.diameter['data'])),2)))









30 changes: 15 additions & 15 deletions pydisdrometer/io/ParsivelReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def read_parsivel(filename):
reader = ParsivelReader(filename)
dsd = DropSizeDistribution(reader)

dsd.fields['raw_matrix'] = {'data': reader.raw}
dsd.fields['filtered_raw_matrix'] = {'data': reader.filtered_raw_matrix}
#dsd.fields['raw_matrix'] = {'data': reader.raw}
#dsd.fields['filtered_raw_matrix'] = {'data': reader.filtered_raw_matrix}
return dsd


Expand All @@ -47,16 +47,22 @@ def __init__(self, filename):

self.ndt = []

#pcm_matrix_file = open('parsivel_conditional_matrix.txt')
self.pcm = np.reshape(self.pcm_matrix, (32, 32))

self._read_file()
self._prep_data()

self.bin_edges = np.hstack(
(0, self.diameter + np.array(self.spread) / 2))
(0, self.diameter['data'] + np.array(self.spread['data']) / 2))

self.bin_edges = common.var_to_dict(
'bin_edges',
self.bin_edges,
'mm', 'Bin Edges')

self._apply_pcm_matrix()


def _read_file(self):
with open(self.filename) as f:
for line in f:
Expand Down Expand Up @@ -98,11 +104,11 @@ def _prep_data(self):
self.fields['reflectivity'] = common.var_to_dict(
'Reflectivity', np.ma.masked_equal(self.Z, -9.999), 'dBZ',
'Equivalent reflectivity factor')
self.nd[self.nd == -9.999] = 0
self.nd[self.nd == -9.999] = 0
self.fields['Nd'] = common.var_to_dict(
'Nd', np.ma.array(self.nd), 'm^-3 mm^-1',
'Nd', np.ma.masked_equal(self.nd, np.power(10, -9.999)), 'm^-3 mm^-1',
'Liquid water particle concentration')
self.fields['Nd']['data'].set_fill_value(0)

self.fields['num_particles'] = common.var_to_dict(
'Number of Particles', np.ma.array(self.num_particles),
'', 'Number of particles')
Expand All @@ -111,7 +117,7 @@ def _prep_data(self):
'm/s', 'Terminal fall velocity for each bin')

try:
self.time = self._get_epoch_time(self.time)
self.time = self._get_epoch_time()
except:
raise ValueError('Conversion to Epoch did not work!')
self.time = {'data': np.array(self.time), 'units': None,
Expand All @@ -124,13 +130,7 @@ def _get_epoch_time(self):
'''
Convert the time to an Epoch time using package standard.
'''
# Convert the time array into a datetime instance
dt_units = 'minutes since ' + StartDate + '00:00:00+0:00'
dtminute = num2date(self.time, dt_units)
# Convert this datetime instance into a number of seconds since Epoch
timesec = date2num(dtminute, common.EPOCH_UNITS)
# Once again convert this data into a datetime instance
time_unaware = num2date(timesec, common.EPOCH_UNITS)
time_unaware = num2date(self.time, common.EPOCH_UNITS)
eptime = {'data': time_unaware, 'units': common.EPOCH_UNITS,
'title': 'Time', 'full_name': 'Time (UTC)'}
return eptime
Expand Down
35 changes: 35 additions & 0 deletions pydisdrometer/tests/test_ParsivelReader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np
import unittest

from ..io import ParsivelReader

class TestParsivelReader(unittest.TestCase):
'Test module for the ParsivelReader class in pydisdrometer.io.ParsivelReader'

def setUp(self):
filename = 'testdata/parsivel_telegraph_testfile.mis'
self.dsd = ParsivelReader.read_parsivel(filename)

def test_can_read_sample_file(self):
self.assertIsNotNone(self.dsd, 'File did not read in correctly, returned None')

def test_dsd_nd_exists(self):
self.assertIsNotNone(self.dsd.fields['Nd'], 'DSD Object has no Nd field')

def test_dsd_nd_is_dict(self):
self.assertIsInstance(self.dsd.fields['Nd'], dict, 'Nd was not a dictionary.')

def test_RR_works_on_Parsivel(self):
self.dsd.calculate_RR()
self.assertIsNotNone(self.dsd.fields['rain_rate'], 'Rain Rate is not in fields after calculate_RR()')
self.assertEqual(len(self.dsd.fields['rain_rate']['data']), 6, 'Wrong number of time samples in rain rate')

def test_can_run_calc_dsd_params(self):
self.dsd.calculate_dsd_parameterization()
self.assertIsNotNone(self.dsd.fields['D0'], 'The Field D0 did not exist after dsd_parameterization check')
self.assertEqual(len(self.dsd.fields['D0']['data']), 6, 'Wrong number of samples in D0')

def test_time_same_length_as_Nd(self):
self.assertEqual(len(self.dsd.time['data']), self.dsd.fields['Nd']['data'].shape[0], 'Different number of samples for time and Nd')


Loading

0 comments on commit e5ae077

Please sign in to comment.