Skip to content

Commit

Permalink
Adapt to titanlib 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
trygveasp committed Jan 14, 2021
1 parent 042c01d commit 4cd087d
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 30 deletions.
4 changes: 2 additions & 2 deletions bin/oi2soda
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import surfex

if __name__ == "__main__":

args = surfex.parse_args_oi2soda(sys.argv[1:])
surfex.run_oi2soda(args)
kwargs = surfex.parse_args_oi2soda(sys.argv[1:])
surfex.run_oi2soda(**kwargs)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def get_version(rel_path):
'bin/perturbed_offline',
'bin/pgd',
'bin/plot_field',
'bin/plot_points',
'bin/plot_timeseries',
'bin/prep',
'bin/set_geo_from_obs_set',
Expand Down
2 changes: 1 addition & 1 deletion surfex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.0.1a12'
__version__ = '0.0.1a13'

from .file import *
from .geo import *
Expand Down
30 changes: 19 additions & 11 deletions surfex/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def run_masterodb(**kwargs):
if kwargs["config"] is not None:
config_exp = kwargs["config"]
if config_exp is None:
config_exp = surfex.__path__[0] + "/../scheduler/config/config_exp_surfex.toml"
config_exp = surfex.__path__[0] + "/cfg/config_exp_surfex.toml"
print("Using default config from: " + config_exp)
input_data = toml.load(open(config_exp, "r"))
config = surfex.ConfigurationFromHarmonie(os.environ, input_data)
Expand Down Expand Up @@ -1202,8 +1202,8 @@ def run_bufr2json(**kwargs):
bufr_set.write_json_file(output, indent=indent)


def parse_args_plot_field(argv):
parser = ArgumentParser("Plot field")
def parse_args_plot_points(argv):
parser = ArgumentParser("Plot points")
parser.add_argument('-g', '--geo', dest="geo", type=str, help="Domain/points json geometry definition file",
default=None, required=False)
parser.add_argument('-v', '--variable', dest="variable", type=str, help="Variable name", required=False)
Expand All @@ -1215,7 +1215,7 @@ def parse_args_plot_field(argv):
required=False)
parser.add_argument('-o', '--output', dest="output", type=str, help="Output file", default=None,
required=False)

parser.add_argument("--interpolator", type=str, default="nearest", required=False, help="Interpolator")
grib = parser.add_argument_group('grib', 'Grib1/2 settings (-it grib1 or -it grib2)')
grib.add_argument('--indicatorOfParameter', type=int, help="Indicator of parameter [grib1]", default=None)
grib.add_argument('--timeRangeIndicator', type=int, help="Time range indicator [grib1]", default=0)
Expand Down Expand Up @@ -1257,7 +1257,7 @@ def parse_args_plot_field(argv):
return kwargs


def run_plot_field(**kwargs):
def run_plot_points(**kwargs):

debug = False
if "debug" in kwargs:
Expand All @@ -1277,13 +1277,15 @@ def run_plot_field(**kwargs):
filepattern = kwargs["inputfile"]
inputtype = kwargs["inputtype"]
output = kwargs["output"]
interpolator = "nearest"
if "interpolator" in kwargs:
interpolator = kwargs["interpolator"]

geo = None
if geo_file is not None:
domain_json = json.load(open(geo_file, "r"))
geo = surfex.geo.get_geo_object(domain_json)

geo_input = None
contour = True
var = "field_to_read"
if inputtype == "grib1":
Expand All @@ -1306,7 +1308,8 @@ def run_plot_field(**kwargs):
"parameter": par,
"type": lt,
"level": lev,
"tri": tri
"tri": tri,
"interpolator": interpolator
}

elif inputtype == "grib2":
Expand Down Expand Up @@ -1354,7 +1357,8 @@ def run_plot_field(**kwargs):
"filepattern": filepattern,
"fcint": 10800,
"file_inc": 10800,
"offset": 0
"offset": 0,
"interpolator": interpolator
}

elif inputtype == "surfex":
Expand Down Expand Up @@ -1390,7 +1394,8 @@ def run_plot_field(**kwargs):
"geo_input": geo_input,
"fcint": 10800,
"file_inc": 10800,
"offset": 0
"offset": 0,
"interpolator": interpolator
}

elif inputtype == "obs":
Expand Down Expand Up @@ -1439,7 +1444,6 @@ def run_plot_field(**kwargs):
converter = "none"
converter = surfex.read.Converter(converter, validtime, defs, converter_conf, inputtype, validtime, debug=debug)
field = surfex.ConvertedInput(geo, var, converter).read_time_step(validtime, cache)
# field = np.reshape(field, [geo.nlons, geo.nlats])

if field is None:
raise Exception("No field read")
Expand Down Expand Up @@ -1789,7 +1793,11 @@ def run_timeseries2json(**kwargs):
obs_set = kwargs["obs_set"]
start = datetime.strptime(starttime, "%Y%m%d%H")
end = datetime.strptime(endtime, "%Y%m%d%H")
geo_in = kwargs["geo_in"]
geo_in = None
if "geo_in" in kwargs:
geo_in = kwargs["geo_in"]
if isinstance(geo_in, str):
geo_in = json.load(open(geo_in, "r"))

# Get lon and lats from station list
if lons is None and lats is None:
Expand Down
1 change: 1 addition & 0 deletions surfex/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,7 @@ def points(self, var, geo_out, validtime=None, interpolation=None, cache=None):

points, interpolator = SurfexIO.interpolate_field(self, field, geo_in, geo_out, interpolation=interpolation,
cache=cache)

return points, interpolator


Expand Down
5 changes: 3 additions & 2 deletions surfex/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def __init__(self, geo_in, geo_out, cache=None, distance_check=True, distance_l
cached_interpolator = cache.get_interpolator("nearest", geo_in, geo_out)

print(geo_in.identifier())
if geo_out is None:
geo_out = geo_in
print(geo_out.identifier())
if cached_interpolator is not None:
print("Using cached interpolator")
Expand Down Expand Up @@ -156,8 +158,7 @@ def __init__(self, geo_in, geo_out, cache=None, distance_check=True, distance_l
values_vec = np.arange(nx * ny)
x = np.floor_divide(values_vec, ny)
y = np.mod(values_vec, ny)
grid_points = np.column_stack((x, y))

grid_points = np.column_stack((x, y))

Interpolation.__init__(self, "nearest", nx, ny, var_lons, var_lats)
self.index = grid_points
Expand Down
17 changes: 15 additions & 2 deletions surfex/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ def print_info(self):
raise NotImplementedError('users must define read_time_step to use this base class')


class Points(object):
def __init__(self, values, interpolator):
self.values = values
self.interpolator = interpolator


class TwoDField(object):
def __init__(self, values, geo):
self.values = values
self.geo = geo


# Direct data can be ead with this class with converter = None
class ConvertedInput(ReadData):
"""
Expand Down Expand Up @@ -105,7 +117,7 @@ def remove_existing_file(f_in, f_out):
#######################################################


class Converter:
class Converter(object):
"""
Main interface to read a field is done through a converter.
The converter name is default "None" to read a plain field without any conversion.
Expand Down Expand Up @@ -229,7 +241,8 @@ def read_time_step(self, geo, validtime, cache):
# print("Time in converter: "+self.name+" "+validtime.strftime('%Y%m%d%H'))

gravity = 9.81
field = np.empty(geo.npoints)
field = None
# field = np.empty(geo.npoints)
# Specific reading for each converter
if self.name == "none":
field = self.var.read_variable(geo, validtime, cache)
Expand Down
12 changes: 8 additions & 4 deletions surfex/titan.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ def test(self, dataset, mask, code=105):
lons = np.asarray(lons)
elevs = np.asarray(elevs)
values = np.asarray(values)
answer = tit.sct(lats, lons, elevs, values, self.num_min, self.num_max, self.inner_radius,
points = tit.Points(lats, lons, elevs)
answer = tit.sct(points, values, self.num_min, self.num_max, self.inner_radius,
self.outer_radius, self.num_iterations, self.num_min_prof, self.min_elev_diff,
self.min_horizonal_scale, self.vertical_scale, self.pos, self.neg, self.eps2)

Expand Down Expand Up @@ -492,7 +493,8 @@ def test(self, dataset, mask, code=104):
elevs.append(dataset.elevs[i])
values.append(dataset.values[i])

status, flags = tit.buddy_check(lats, lons, elevs, values,
points = tit.Points(lats, lons, elevs)
status, flags = tit.buddy_check(points, values,
self.distance_lim, self.priorities,
self.buddies_min, self.thresholds, self.diff_elev_max,
self.adjust_for_elev_diff, self.obs_to_check)
Expand Down Expand Up @@ -563,7 +565,8 @@ def test(self, dataset, mask, code=103):
val = dataset.values[mask[o]] + self.offset[o]
values.append(val)

flags = tit.range_check_climatology(lats, lons, elevs, values, self.unixtime, self.maxvals, self.minvals)
points = tit.Points(lats, lons, elevs)
flags = tit.range_check_climatology(points, values, self.unixtime, self.maxvals, self.minvals)

global_flags = dataset.flags
for i in range(0, len(mask)):
Expand Down Expand Up @@ -1094,7 +1097,8 @@ def __init__(self, var, settings, tests, datasources, an_time, debug=False, core
for i in range(0, len(lons)):
observations.append(surfex.Observation(obstimes[i], lons[i], lats[i], values[i], elev=elevs[i],
stid=stids[i], varname=varnames[i]))
self.titan_dataset = tit.Dataset(lats, lons, elevs, values)
points = tit.Points(lats, lons, elevs)
self.titan_dataset = tit.Dataset(points, values)
if passed_tests is None:
passed_tests = []
for i in range(0, len(lons)):
Expand Down
16 changes: 8 additions & 8 deletions test/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def test_plot_grib1(self):
"-o", "unittest_output_plot_grib1.png",
"--debug"
]
kwargs = surfex.parse_args_plot_field(argv)
surfex.run_plot_field(**kwargs)
kwargs = surfex.parse_args_plot_points(argv)
surfex.run_plot_points(**kwargs)

def test_plot_grib2(self):
argv = [
Expand All @@ -36,8 +36,8 @@ def test_plot_grib2(self):
"-o", "unittest_output_plot_grib2.png",
"--debug"
]
kwargs = surfex.parse_args_plot_field(argv)
surfex.run_plot_field(**kwargs)
kwargs = surfex.parse_args_plot_points(argv)
surfex.run_plot_points(**kwargs)

def test_plot_netcdf(self):
argv = [
Expand All @@ -49,8 +49,8 @@ def test_plot_netcdf(self):
"-o", "unittest_output_plot_nc.png",
"--debug"
]
kwargs = surfex.parse_args_plot_field(argv)
surfex.run_plot_field(**kwargs)
kwargs = surfex.parse_args_plot_points(argv)
surfex.run_plot_points(**kwargs)

def test_plot_obs_frost_json(self):
argv = [
Expand All @@ -62,5 +62,5 @@ def test_plot_obs_frost_json(self):
"-o", "unittest_output_plot_obs_frost_json.png",
"--debug"
]
kwargs = surfex.parse_args_plot_field(argv)
surfex.run_plot_field(**kwargs)
kwargs = surfex.parse_args_plot_points(argv)
surfex.run_plot_points(**kwargs)

0 comments on commit 4cd087d

Please sign in to comment.