def get_avg_conc_and_times_from_polyline(task_id, task_root, single_file): """ Parameters ---------- task_id task_root single_file Yields ------ avg_concentration : time series of averaged concentration for a polyline times : coresponding times time_steps : number of time steps nodes : number of nodes along the polyline """ tec = readtec_polyline(task_id=task_id, task_root=task_root, single_file=single_file) time_steps = tec[var_name].shape[0] nodes = tec[var_name].shape[1] times = tec["TIME"] avg_concentration = [] for i in range(0, time_steps): # get the node values of the velocities for ith time step node_concentration = tec[var_name][i, :] avg_concentration.append(np.mean(node_concentration)) return avg_concentration, times, time_steps, nodes
def get_baseflow_from_line( path_to_file="/Users/houben/PhD/modelling/transect/ogs/confined/transient/rectangular/Groundwater@UFZ/Model_Setup_D_day_EVE/homogeneous/D18-D30_whitenoise/Groundwater@UFZ_eve_HOMO_276_D_25_results/transect_01_ply_obs_1000_t17_GROUNDWATER_FLOW.tec" ): """ - Polyline has to be vertical! Only x-component of node-velocity is used. - All points in the line have to have the exact same x-coordinate (e.g. on a vertical domain border) """ from ogs5py.reader import readtec_polyline import numpy as np import os # read only 1 tec file for polyline for which the flow should be calculated tecs = readtec_polyline(single_file=path_to_file) # time_steps = number of timesteps + initial values try: time_steps = tecs["VELOCITY_X1"].shape[0] - 1 # nodes = number of nodes along polyline nodes = tecs["VELOCITY_X1"].shape[1] flow_array = np.zeros((nodes, time_steps)) flow_timeseries = [] for i in range(0, time_steps): # get the node values of the velocities for ith time step node_velocity = tecs["VELOCITY_X1"][i, :] # get the node values of the distances measured from starting point of polyline # (i.e. y-value) for ith time step node_dist = tecs["DIST"][i, :] flow_per_timestep = [] # add flow for first node, calculate the distance between # 1st and 2nd node and multiply with first node value flow_per_timestep.append( (node_dist[1] - node_dist[0]) / 2 * node_velocity[0]) for j in range(0, len(node_velocity) - 2): # 1st loop: calculate the distance between first and second node diff_low = node_dist[j + 1] - node_dist[j] # 1st loop: calculate the distance between second and third node diff_up = node_dist[j + 2] - node_dist[j + 1] # 1st loop: multiply second node velocity with half of distance between # 1st and 2nd node and half of distance between 2nd and 3rd node flow_per_timestep.append( (diff_low + diff_up) / 2 * node_velocity[j + 1]) # add flow for last node, multiply node velocity with half of distance between 2nd last and last node flow_per_timestep.append( ((node_dist[-1] - node_dist[-2]) / 2 * node_velocity[-1])) flow_per_timestep = np.asarray(flow_per_timestep) flow_array[:, i] = flow_per_timestep flow_timeseries = flow_array.sum(axis=0) np.savetxt( str(path_to_file)[:-len(os.path.basename(str(path_to_file)))] + "/baseflow.txt", flow_timeseries, ) return flow_timeseries except KeyError: print( "ERROR: There is no VELOCITY_X1 time series in the given tec-file." )
# ============================================================================= recharge = [] if __name__ == "__main__": try: """ Loading tecs from file if file exists """ tecs = np.load(str(path_to_project) + "/" + "tecs.npy").item() print("Reading heads from file...") if tecs == {}: print( "No saved tecs.npy available, reading heads from .tec-files..." ) tecs = readtec_polyline(task_id=name_of_project_ogs, task_root=path_to_project) print("Reading of .tec-files finished.") # Save the dict print("Saving data...") np.save(str(path_to_project) + "/" + "tecs.npy", tecs) print("Saving finished.") except IOError: print("Failed to load input data.") tecs = readtec_polyline(task_id=name_of_project_ogs, task_root=path_to_project) #print(tecs) #print(name_of_project_ogs, path_to_project) time_s = tecs[process][obs_per_plot[0]]["TIME"] time_d = time_s / 86400
def get_fft_data_from_simulation( path_to_project="/Users/houben/PhD/modelling/transect/ogs/confined/transient/rectangular/Groundwater@UFZ/Model_Setup_D_day_EVE/homogeneous/D18-D30/testing2/Groundwater@UFZ_eve_HOMO_276_D_4_results", single_file="/Users/houben/PhD/modelling/transect/ogs/confined/transient/rectangular/Groundwater@UFZ/Model_Setup_D_day_EVE/homogeneous/D18-D30/testing2/Groundwater@UFZ_eve_HOMO_276_D_4_results/transect_01_ply_obs_0400_t6_GROUNDWATER_FLOW.tec", which_data_to_plot=2, name_of_project_gw_model="", name_of_project_ogs="transect_01", process="GROUNDWATER_FLOW", which="mean", time_steps=8401, obs_point="NA", ): """ which_data_to_plot = 1 # 1: ogs, 2: gw_model 3: recharge which = 'max' # min, max, mean time_steps = 8400 # this is the value which is given in the ogs input file .tim. It will result in a total of 101 times because the initial time is added. methods = ['scipyfftnormt', 'scipyfftnormn', 'scipyfft', 'scipywelch', 'pyplotwelch', 'scipyperio', 'spectrumperio'] """ # ============================================================================= # initialize the file for output # ============================================================================= with open(str(path_to_project) + "/PSD_output.txt", "a") as file: file.write( "date time method T_l[m2/s] kf_l[m/s] Ss_l[1/m] D_l[m2/s] a_l t_l[s] T_d[m2/s] kf_d[m/s] Ss_d[1/m] D_d[m2/s] a_d t_d[s] path_to_project observation_point\n" ) file.close() # ============================================================================= # global variables set automatically # ============================================================================= rfd_x, recharge = get_curve( path_to_project=path_to_project, name_of_project_ogs=name_of_project_ogs, curve=1, mm_d=False, time_steps=time_steps, ) try: fft_data = np.loadtxt( str(path_to_project) + "/" + "head_ogs_" + str(obs_point) + "_" + str(which) + ".txt") print("Loaded heads from file: " + str("head_ogs_" + str(obs_point) + "_" + str(which) + ".txt")) except IOError: print("Reading .tec-file...") print(single_file[-40:]) tecs = readtec_polyline( task_id=name_of_project_ogs, task_root=path_to_project, single_file=single_file, ) print("Finished reading.") # ============================================================================= # get data dependent on which_data_to_plot # ============================================================================= if which_data_to_plot == 2: fft_data = gethead_ogs_each_obs( process, obs_point, which, time_steps, tecs=tecs, path_to_project=path_to_project, single_file=True, save_heads=True, ) # recharge = getrecharge(path_to_project=path_to_project, name_of_project_ogs=name_of_project_ogs, time_steps=time_steps) elif which_data_to_plot == 1: fft_data = gethead_gw_model_each_obs( make_array_gw_model( split_gw_model( getlist_gw_model( str(path_to_project) + str(name_of_project_gw_model) + "/H.OUT"), index=2, )), convert_obs_list_to_index("obs_0990"), save_heads=False, ) # recharge = getrecharge(path_to_project=path_to_project, name_of_project_ogs=name_of_project_ogs, time_steps=time_steps) elif which_data_to_plot == 3: # recharge = getrecharge(path_to_project=path_to_project, name_of_project_ogs=name_of_project_ogs, time_steps=time_steps) fft_data = recharge # convert recharge from list to array recharge = np.asarray([float(i) for i in recharge]) return fft_data, recharge
def get_baseflow_from_polyline(task_id, task_root, single_file, orientation="vertical", save_flow=True): """ Calculates the horizontal flow through a vertical polyline from an OGS run. The file type should be .tec and it must contain VELOCITY_X1 values. Parameters ---------- task_id : string Name of the ogs task. task_root : string Directory of ogs model run. single_file : string Path to .tec file from the polyline. orientation : string (vertical, horizontal) NO FUNCTION YET save_flow : bool True: A txt-file will be saved in the root directoy. Yields ------ flow_timeseries : 1D array A value for the outflow over the whole polyline for each time step. """ from ogs5py.reader import readtec_polyline import numpy as np import os # read only 1 tec file for polyline for which the flow should be calculated tec = readtec_polyline(task_id=task_id, task_root=task_root, single_file=single_file) # time_steps = number of timesteps + initial values try: time_steps = tec["VELOCITY_X1"].shape[0] # nodes = number of nodes along polyline nodes = tec["VELOCITY_X1"].shape[1] flow_array = np.zeros((nodes, time_steps)) flow_timeseries = [] for i in range(0, time_steps): # print("Time step " + str(i) + " of " + str(time_steps) + "...") # get the node values of the velocities for ith time step node_velocity = tec["VELOCITY_X1"][i, :] # get the node values of the distances measured from starting point of polyline # (i.e. y-value) for ith time step node_dist = tec["DIST"][i, :] flow_per_timestep = [] # add flow for first node, calculate the distance between # 1st and 2nd node and multiply with first node value flow_per_timestep.append( (node_dist[1] - node_dist[0]) / 2 * node_velocity[0]) for j in range(0, len(node_velocity) - 2): # 1st loop: calculate the distance between first and second node diff_low = node_dist[j + 1] - node_dist[j] # 1st loop: calculate the distance between second and third node diff_up = node_dist[j + 2] - node_dist[j + 1] # 1st loop: multiply second node velocity with half of distance between # 1st and 2nd node and half of distance between 2nd and 3rd node flow_per_timestep.append( (diff_low + diff_up) / 2 * node_velocity[j + 1]) # add flow for last node, multiply node velocity with half of distance between 2nd last and last node flow_per_timestep.append( ((node_dist[-1] - node_dist[-2]) / 2 * node_velocity[-1])) flow_per_timestep = np.asarray(flow_per_timestep) flow_array[:, i] = flow_per_timestep flow_timeseries = flow_array.sum(axis=0) # return flow_timeseries except KeyError: print( "ERROR: There is no VELOCITY_X1 time series in the given tec-file." ) if save_flow == True: np.savetxt(single_file[:-4] + "_flow_timeseries.txt", flow_timeseries) return flow_timeseries
def extract_timeseries(path, which="mean", process="GROUNDWATER_FLOW"): """ Function to extract time series for each observation point and store them as .txt. Parameters ---------- path : string path to ogs directory which : string, 'mean', 'max', 'min' Which value should be taken from the vertical polyline of obs point. process : string Which modelling process. Yields ------ .txt from every observation point with head values """ import numpy as np from ogs5py.reader import readtec_polyline import glob import vtk from tools import get_ogs_task_id # pipe vtk output errors to file errOut = vtk.vtkFileOutputWindow() errOut.SetFileName(path + "/VTK_Error_Out.txt") vtkStdErrOut = vtk.vtkOutputWindow() vtkStdErrOut.SetInstance(errOut) task_id = get_ogs_task_id(path) # read all tec files print("Reading tec-files from " + path) # This will throw an ERROR which is redirected to VTK_Error_Out.txt tecs = readtec_polyline(task_id=task_id, task_root=path) # extract the time series and save them as .txt for obs in tecs["GROUNDWATER_FLOW"].keys(): time_steps = len(tecs["GROUNDWATER_FLOW"][obs]["TIME"]) number_of_columns = tecs[process][obs]["HEAD"].shape[1] if which == "max": # select the maximum value (i.e. the uppermost) of polyline as long as polylines are defined from bottom to top head_ogs_timeseries_each_obs = tecs[process][obs]["HEAD"][ :, number_of_columns - 1 ] elif which == "min": # select the minimum value (i.e. the lowermost) of polyline as long as polylines are defined from bottom to top head_ogs_timeseries_each_obs = tecs[process][obs]["HEAD"][:, 0] elif which == "mean": head_ogs_timeseries_each_obs = [] for step in range(time_steps): # calculates the mean of each time step head_ogs_timeseries_each_obs.append( np.mean(tecs[process][obs]["HEAD"][step, :]) ) head_ogs_timeseries_each_obs = np.asarray(head_ogs_timeseries_each_obs) np.savetxt( str(path) + "/" + "head_ogs_" + str(obs) + "_" + str(which) + ".txt", head_ogs_timeseries_each_obs, )
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Thu Jun 14 11:15:41 2018 @author: houben This is a script to plot the groundwater head of a confined aquifer from a transient ogs simulation. """ import matplotlib.pyplot as plt from ogs5py.reader import readtec_polyline import numpy as np tecs = readtec_polyline( task_id="transect_01", task_root="/Users/houben/PhD/transect/transect/ogs/confined/transient/topography/1layer/template/", ) process = "GROUNDWATER_FLOW" observation_point = "obs_0990" head = tecs[process][observation_point]["HEAD"] time = tecs[process][observation_point]["TIME"] dist = tecs[process][observation_point]["DIST"] # plot head at observation_poit over time plt.plot(time, head[:, head.shape[1] - 1]) # calculate avaraged head mean_head_per_timestep = []