Skip to content

Commit

Permalink
Merge pull request #19 from jhamman/feature/history_units
Browse files Browse the repository at this point in the history
Feature/history units
  • Loading branch information
Joe Hamman committed Apr 14, 2014
2 parents 7bcafc1 + eb39939 commit 69ede21
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
13 changes: 9 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ before_install:
- sudo ln -s /run/shm /dev/shm
# Install packages
install:
- conda create --yes -n rvic_env python=$TRAVIS_PYTHON_VERSION
- source activate rvic_env
- pip install -r ci/requireemnts-2.7.txt
- conda create --yes --name=rvic_test_env python=$TRAVIS_PYTHON_VERSION --file=ci/requirements-2.7.txt
- source activate rvic_test_env
- python setup.py install
# Run test
script:
- py.test
- which python
- python --version
- conda list
- pip freeze
- echo $PATH
- python -c 'import rvic; print rvic.__file__'
- py.test # erroring here for unknown path reasons...
45 changes: 36 additions & 9 deletions rvic/core/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, time_ord, caseid, Rvar, tape_num=0,
avgflag='A', units='kg m-2 s-1',
file_format='NETCDF4_CLASSIC', outtype='grid',
grid_lons=False, grid_lats=False, grid_area=None, out_dir='.',
calendar=None, glob_ats=None, zlib=True, complevel=4,
calendar=None, glob_ats=None, zlib=True, complevel=4,
least_significant_digit=None):
self._tape_num = tape_num
self._time_ord = time_ord # Days since basetime
Expand Down Expand Up @@ -95,13 +95,39 @@ def __init__(self, time_ord, caseid, Rvar, tape_num=0,
# ------------------------------------------------------------ #

# ------------------------------------------------------------ #
# Get units multiplier
# Get units multiplier (size of noutlets)
self._units = units
if units in ['kg/m2/s', 'kg m-2 s-1', 'kg m^-2 s^-1',
'kg*m-2*s-1', 'kg s-1 m-2']:
self._units_mult = 1.0
self._units_mult = np.ones_like(self._outlet_y_ind,
dtype=np.float64)
elif units in ['m3/s', 'm^3/s', 'm3 s-1']:
self._units_mult = grid_area / WATERDENSITY
# kg/m2/s --> m3/s
self._units_mult = grid_area[self._outlet_y_ind,
self._outlet_x_ind]
self._units_mult /= WATERDENSITY
elif units in ['mm/day', 'mm d-1', 'mm d^-1', 'mm/day']:
# kg/m2/s --> mm/day over basin area
self._units_mult = grid_area[self._outlet_y_ind,
self._outlet_x_ind]
self._units_mult *= SECSPERDAY
self._units_mult /= WATERDENSITY
self._units_mult /= self._outlet_upstream_area
elif units in ['gal/day', 'gpd', 'gal d-1']:
self._units_mult = grid_area[self._outlet_y_ind,
self._outlet_x_ind]
self._units_mult /= WATERDENSITY
self._units_mult *= 2.28E7
elif units in ['cfs', 'ft^3 s-1', 'f3/s']:
self._units_mult = grid_area[self._outlet_y_ind,
self._outlet_x_ind]
self._units_mult /= WATERDENSITY
self._units_mult *= 35.3
elif units in ['acre-ft/d']:
self._units_mult = grid_area[self._outlet_y_ind,
self._outlet_x_ind]
self._units_mult /= WATERDENSITY
self._units_mult *= 70.0
else:
raise ValueError('{0} is not a valid units string'.format(units))
# ------------------------------------------------------------ #
Expand Down Expand Up @@ -324,10 +350,10 @@ def __update_out_data(self):
# Grid the fields
self._out_data[field][self._out_data_i,
self._outlet_y_ind,
self._outlet_x_ind] = self._temp_data[field][:]
self._outlet_x_ind] = self._temp_data[field][:] * self._units_mult
# ---------------------------------------------------- #
else:
self._out_data[field][self._out_data_i, :] = self._temp_data[field]
self._out_data[field][self._out_data_i, :] = self._temp_data[field] * self._units_mult
# ------------------------------------------------------------ #

self._out_times[self._out_data_i] = self._write_ord
Expand Down Expand Up @@ -368,6 +394,7 @@ def __get_rvar(self, rvar):
self._outlet_lon = rvar.outlet_lon
self._outlet_lat = rvar.outlet_lat
self._outlet_name = rvar.outlet_name
self._outlet_upstream_area = rvar.outlet_upstream_area
# ---------------------------------------------------------------- #

# ---------------------------------------------------------------- #
Expand Down Expand Up @@ -511,8 +538,8 @@ def __write_grid(self):

for field in self._fincl:
var = f.createVariable(field, self._ncprec, tcoords,
**self.ncvaropts))
var[:, :] = self._out_data[field][:self._out_data_i+1] * self._units_mult
**self.ncvaropts)
var[:, :] = self._out_data[field][:self._out_data_i+1]

for key, val in getattr(share, field).__dict__.iteritems():
if val:
Expand Down Expand Up @@ -630,7 +657,7 @@ def __write_array(self):
for field in self._fincl:
var = f.createVariable(field, self._ncprec, tcoords,
**self.ncvaropts)
var[:, :] = self._out_data[field][:self._out_data_i+1] * self._units_mult[self._outlet_y_ind, self._outlet_x_ind]
var[:, :] = self._out_data[field][:self._out_data_i+1]

for key, val in getattr(share, field).__dict__.iteritems():
if val:
Expand Down
4 changes: 4 additions & 0 deletions rvic/core/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ def __init__(self, param_file, case_name, calendar, out_dir, file_format,
self.RvicUHFile = f.RvicUHFile
self.RvicFdrFile = f.RvicFdrFile
self.file_format = file_format
try:
self.outlet_upstream_area = f.variables['outlet_upstream_area'][:]
except:
self.outlet_upstream_area = None
self.glob_atts = NcGlobals(title='RVIC restart file',
RvicPourPointsFile=f.RvicPourPointsFile,
RvicUHFile=f.RvicUHFile,
Expand Down

0 comments on commit 69ede21

Please sign in to comment.