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

fix: adjust alignment of values to soil headers #33

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions src/tradssat/tmpl/vals.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,10 @@ def write(self, lines):
"""
self.check_dims()
self.check_vals()

lines.append('@' + ''.join([vr.write() for vr in self]))
headers = ''.join([vr.write() for vr in self])
# Hack for soil SALB header
headers = '@' + headers[1:] if headers.startswith(" SLB") else '@' + headers
lines.append(headers)
for i in range(self.n_data()):
written = [vr.write(i) for vr in self]
for i, (x, vr) in enumerate(zip(list(written), self)):
Expand Down Expand Up @@ -444,6 +446,15 @@ class VariableValue(object):
"""

def __init__(self, var, val):
"""

Parameters
----------
var : Variable object
Class for all variable types
val : str, int, or floats
Value pertaining to variable
"""

self.changed = False

Expand Down
52 changes: 48 additions & 4 deletions src/tradssat/tmpl/var.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ class Variable(object):
type_ = None

def __init__(self, name, size, spc, header_fill, float_r, miss, sect, info):
"""

Parameters
----------
name : str
A DSSAT variable name
size : int
Allowed maximum width of the variable
spc : int
Number of required leading spaces
header_fill : str
A character with which to fill a header
float_r : bool
Flag to indicate whether a value is a float
miss : str
A substitute to indicate missing data, ie '-99'
sect : str
A section or subsection to which a variable corresponds
info : str
Description for the name
"""
self.name = name
self.size = size
self.spc = spc
Expand All @@ -33,8 +54,8 @@ def write(self, val=None):
txt = self._write(val)

if self.float_r:
return ' ' * self.spc + txt.ljust(self.size, fill)
return ' ' * self.spc + txt.rjust(self.size, fill)
return ' ' * self.spc + txt.rjust(self.size, fill)
return ' ' * self.spc + txt.ljust(self.size, fill)

def check_val(self, val):
raise NotImplementedError
Expand Down Expand Up @@ -75,7 +96,7 @@ class NumericVar(Variable):

def __init__(self, name, size, lims, spc, header_fill, miss, sect, info):

super().__init__(name, size, spc, header_fill, sect=sect, float_r=True, miss=miss, info=info)
super().__init__(name, size=size, spc=spc, header_fill=header_fill, sect=sect, float_r=True, miss=miss, info=info)

if lims is None:
lims = (-np.inf, np.inf)
Expand Down Expand Up @@ -103,7 +124,30 @@ class FloatVar(NumericVar):
type_ = float

def __init__(self, name, size, dec, lims=None, spc=1, sect=None, header_fill=' ', miss='-99', info=''):
super().__init__(name, size, lims, spc, header_fill, miss=miss, sect=sect, info=info)
"""

Parameters
----------
name : str
A DSSAT variable name
size : int
Allowed maximum width of the variable
dec : int
The number of allowed decimal spaces
lims : tuple, optional
Describes the minimum and maximum boundaries for a value, by default None
spc : int, optional
Number of required leading spaces, by default 1
sect : str, optional
A section or subsection to which a variable corresponds, by default None
header_fill : str, optional
Character with which to fill width of the field, by default ' '
miss : str, optional
A substitute to indicate missing data, by default '-99'
info : str, optional
Description for the name, by default ''
"""
super().__init__(name, size=size, lims=lims, spc=spc, header_fill=header_fill, miss=miss, sect=sect, info=info)
self.dec = dec

def _write(self, val):
Expand Down
Loading