Commit c98a6fc6 authored by Philippe Delandmeter's avatar Philippe Delandmeter Committed by Jonathan Lambrechts
Browse files

temporal serie files readed from the interface

parent 36b530bf
Pipeline #1088 passed with stage
in 27 minutes and 39 seconds
......@@ -413,6 +413,89 @@ def netcdf_to_msh(mesh_file_name, nc_file_name, variable_name, export_file_name,
dgpy.function.getTime().set(time)
groups.exportFunctionMsh(f, export_file_name)
def read_temporal_serie(file_name, time=None, output_netcdf=None):
"""Return a temporal serie from a text data file.
If time is given, the function returns a vector with the data interpolated
at the instants of time object.
If time is not given, the function returns a tuple (time, data), where
time is a Time object with the instants of the data file
data is a vector with the values of the data file
keyword arguments:
* file_name
path to the file.
Data must be given in the following format:
1991-06-10 00:00:00, 20.7
1991-06-11 00:00:00, 40.2
1991-06-15 00:00:00, 13.8
Different date must be strictly increasing but the time step
does not have to be constant
* time
time is a slimPre Time object.
if time is given, data will be interpolated to the time instants.
* output_netcdf
output_netcdf = ('netcdf_file_name', 'variable_name')
if output_netcdf is given, the data are written in a netcdf file
"""
f = open(file_name, 'r')
fmt = '%Y-%m-%d %H:%M:%S'
if time:
refTime = time._time
valVec = np.zeros(refTime.shape)
l = f.readline()
(t, val) = l.split(',')
date = slim_private.datetime.datetime.strptime(t, fmt)
t0 = slim_private.calendar.timegm([date.year, date.month, date.day, date.hour, date.minute, date.second])
val0 = float(val)
l = f.readline()
(t, val) = l.split(',')
date = slim_private.datetime.datetime.strptime(t, fmt)
t1 = slim_private.calendar.timegm([date.year, date.month, date.day, date.hour, date.minute, date.second])
val1 = float(val)
if refTime[0] < t0:
dgpy.Msg.Fatal('Minimum time requested is before initial time from data file')
for i in range(refTime.shape[0]):
while refTime[i] > t1:
t1 = t0
val1 = val0
l = f.readline()
if len(l) < 10:
dgpy.Msg.Fatal('Requested time is after final time from data file')
(t, val) = l.split(',')
date = slim_private.datetime.datetime.strptime(t, fmt)
t1 = slim_private.calendar.timegm([date.year, date.month, date.day, date.hour, date.minute, date.second])
val1 = float(val)
xsi = (refTime[i] - t0) / (t1 - t0)
valVec[i] = (1-xsi) * val0 + xsi * val1
if output_netcdf:
write_file(output_netcdf[0], region=None, time=time, data=[(output_netcdf[1], valVec)])
return valVec
else:
num_lines = sum(1 for line in open(file_name))
valVec = np.zeros((num_lines,1))
timeVec = np.zeros((num_lines,1))
l = f.readline()
it = 0
while l != '' and it < 1e7:
(t, val) = l.split(',')
date = slim_private.datetime.datetime.strptime(t, fmt)
timeVec[it] = slim_private.calendar.timegm([date.year, date.month, date.day, date.hour, date.minute, date.second])
valVec[it] = float(val)
it = it+1
l = f.readline()
if it > 1e7-10:
dgpy.Msg.Fatal('temporal serie data file is too long')
time = Time(time_vector=timeVec)
if output_netcdf:
write_file(output_netcdf[0], region=None, time=time, data=[(output_netcdf[1], valVec)])
return (time, valVec)
def tpxo_tide(region, time, data_file_name=None):
"""Give TPXO tides data
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment