def num_pres_vel(model_name, dat, let): """ Number of velocity components for model_name. """ output_path = rm.make_output_dirs(model_name, dat, let)[0] enkf_input_file = rm.make_file_dir_names(model_name)[3] num_pres_vel = rm.read_records_input(output_path + '/' + enkf_input_file, '# prescribed velocity') return num_pres_vel
def num_mons(model_name, dat, let): """ Number of monitoring points for model_name. """ output_path = rm.make_output_dirs(model_name, dat, let)[0] true_input_file = rm.make_file_dir_names(model_name)[4] num_mons = rm.read_records_input(output_path + '/' + true_input_file, '# monitor,') return num_mons
def nrobs_int(model_name, dat, let): """ Number of observation intervals for model_name. """ output_path = rm.make_output_dirs(model_name, dat, let)[0] enkf_input_file = rm.make_file_dir_names(model_name)[3] line = rm.read_hashtag_input(output_path + '/' + enkf_input_file, '# nrobs_int', 1) first_entry = str.split(line)[0] nrobs_int = int(first_entry) return nrobs_int
def nrens(model_name, dat, let): """ Number of ensemble members for model_name """ output_path = rm.make_output_dirs(model_name, dat, let)[0] input_file = rm.make_file_dir_names(model_name)[2] line = rm.read_hashtag_input(output_path + '/' + input_file, '# simulate', 1) first_entry = str.split(line)[0] nrens = int(first_entry) return nrens
def num_single_cell(model_name, dat, let): """ Number of single cell observation points for model_name. Remark: We use the comma in the hashtag-search in order to exclude '# single cell output times' from the read_records_input search. """ output_path = rm.make_output_dirs(model_name, dat, let)[0] enkf_input_file = rm.make_file_dir_names(model_name)[3] num_single_cell = rm.read_records_input( output_path + '/' + enkf_input_file, '# single cell output,') return num_single_cell
def monitor_locs(model_name, dat, let): """ Monitor point locations of model_name. """ output_path = rm.make_output_dirs(model_name, dat, let)[0] true_input_file = rm.make_file_dir_names(model_name)[4] n_mons = num_mons(model_name, dat, let) lines = rm.read_hashtag_input(output_path + '/' + true_input_file, '# monitor', n_mons) locs = [[int(str.split(lines)[j]) for j in range(i, i + 3)] for i in range(0, 4 * n_mons, 4)] return locs
def ns(model_name, dat, let): """ First observation time for model_name. """ output_path = rm.make_output_dirs(model_name, dat, let)[0] obs_file = rm.make_file_dir_names(model_name)[13] file_input = open(output_path + '/' + obs_file, 'r') file_input.readline() ns = int(str.split(file_input.readline())[0]) # Python starts at 0 ns = ns - 1 return ns
def single_cell_locs(model_name, dat, let): """ Single cell locations of model_name, dat and lets """ output_path = rm.make_output_dirs(model_name, dat, let)[0] enkf_input_file = rm.make_file_dir_names(model_name)[3] n_sc = num_single_cell(model_name, dat, let) lines = rm.read_hashtag_input(output_path + '/' + enkf_input_file, '# single cell output, ', n_sc) locs = [[int(str.split(lines)[j]) for j in range(i, i + 3)] for i in range(0, 5 * n_sc, 5)] return locs
def single_cell_variables(model_name, dat, let): """ Single cell variable of model_name. 1: head, 2:temp, 3:conc, 4:kz, 5:lz, 6:por (inside mem) 11: head, 12:temp, 13:conc, 14:kz, 15:lz, 16:por (not inside mem) """ output_path = rm.make_output_dirs(model_name, dat, let)[0] enkf_input_file = rm.make_file_dir_names(model_name)[3] n_sc = num_single_cell(model_name, dat, let) lines = rm.read_hashtag_input(output_path + '/' + enkf_input_file, '# single cell output, ', n_sc) variables = [int(str.split(lines)[i]) for i in range(3, 5 * n_sc, 5)] return variables
def delz(model_name, dat, let): """ Read model name, output date and letter and return the corresponding array of cell lengths in z-direction. """ output_dir = rm.make_output_dirs(model_name, dat, let)[0] input_file = rm.make_file_dir_names(model_name)[2] arr_filename = pm.py_output_filename('specs', 'delz', specl(model_name, dat, let), 'npy') if rm.check_hashtag(output_dir, input_file, "# delz"): line = rm.read_hashtag_input(output_dir + '/' + input_file, '# delz', 1) else: input_file = rm.make_file_dir_names(model_name)[4] if rm.check_hashtag(output_dir, input_file, "# delz"): line = rm.read_hashtag_input(output_dir + '/' + input_file, '# delz', 1) else: if os.path.isfile(arr_filename): return np.load(arr_filename) else: raise IOError('Input files, hashtags or' + ' npy file not found:\n\n' + output_dir + '\n\n' + input_file + '\n\n' + arr_filename) num_entries = len(str.split(line)) nums = [ int(str.split(entry, "*")[0]) if len(str.split(entry, "*")) > 1 else 1 for entry in str.split(line) ] lens = [ float(str.split(entry, "*")[1]) if len(str.split(entry, "*")) > 1 else float(str.split(entry, "*")[0]) for entry in str.split(line) ] vec = [[lens[i] for j in range(nums[i])] for i in range(num_entries)] arr = np.array([num for elem in vec for num in elem]) np.save(arr_filename, arr) return arr
def nm(model_name, dat, let): """ Difference of observation times for model_name. """ output_path = rm.make_output_dirs(model_name, dat, let)[0] obs_file = rm.make_file_dir_names(model_name)[13] n_mons = num_mons(model_name, dat, let) file_input = open(output_path + '/' + obs_file, 'r') file_input.readline() ns1 = int(str.split(file_input.readline())[0]) for i in range(n_mons): file_input.readline() ns2 = int(str.split(file_input.readline())[0]) nm = ns2 - ns1 return nm
def read( model_name, dat, let, fdir=None, fname=None, varname='uindex', nt=0, ): """ Reading variable array from SHEMAT-Suite vtk-file. Parameters ---------- model_name : string String of model name. dat : string String with date of model run. let : string String of letter of model run. varname : string Variable name for array to be read. Possibilities: 'uindex' 'head','temp','kz', 'v' nt : string Number of time step output. Returns ------- numpy_array : array Array containing the variable array numpy_array_name : string Containing proposed saving location for Array. """ # Dirs if fdir is None: # samples_output_dir fdir = rm.make_output_dirs(model_name, dat, let)[1] if fname is None: # time_out_file fname = rm.make_file_dir_names(model_name, nt)[17] # Get filetype ############################################################ if fname[-3:] == 'vtk': ftype = 'vtk' elif fname[-2:] == 'h5' or fname[-3:] == 'hdf': ftype = 'hdf' else: print(fname) raise RuntimeError('Wrong filetype.') # Get reader ############################################################## if ftype == 'vtk': reader = pf.my_vtk(fdir, fname, varname) elif ftype == 'hdf': reader = pf.my_hdf(fdir+'/'+fname, varname) # Numpy Array ############################################################ if ftype == 'vtk': numpy_array = pf.my_vtk_to_numpy(reader) elif ftype == 'hdf': numpy_array = reader # Numpy Array Name ######################################################## numpy_array_name = pm.py_output_filename( fa.tag, varname, sc.specl(model_name, dat, let)+'_'+str(nt), "npy", ) return numpy_array, numpy_array_name
def read( model_name, dat, let, varname='kz_mean', befaft='aft', fdir=None, fname=None, nt=10, ): """ Reading variable arrays from SHEMAT-Suite. Parameters ---------- model_name : string String of model name. dat : string String with date of model run. let : string String of letter of model run. varname : string Variable name for array to be read. Possibilities: 'kz_mean' 'kz_std','head_mean','lz_mean', 'temp_mean' befaft : string Specifies whether the output is read in from before ('bef') or after ('aft') the EnKF update. nt : integer Number inside file name. fdir : string Full directory of vtk file. fname : string Full name of vtk file. Returns ------- numpy_array : array Array containing the variable array numpy_array_name : string Containing proposed saving location for Array. """ # Automatic file name generation if (not fdir and not fname): # enkf_output_dir fdir = rm.make_output_dirs(model_name, dat, let)[2] if befaft == 'aft': # assim_out_file_aft fname = rm.make_file_dir_names(model_name, nt)[19] elif befaft == 'bef': # assim_out_file_bef fname = rm.make_file_dir_names(model_name, nt)[18] # Get vtk_reader ########################################################## vtk_reader = pf.my_vtk(fdir, fname, varname) # Debug ################################################################### print(varname, vtk_reader.GetOutput().GetCellData().GetArray(0).GetValueRange()) # Numpy Array ############################################################ numpy_array = pf.my_vtk_to_numpy(vtk_reader) # Numpy Array Name ######################################################## numpy_array_name = pm.py_output_filename( aa.tag, varname + '_' + str(nt).zfill(4) + '_' + befaft, sc.specl(model_name, dat, let), "npy") return numpy_array, numpy_array_name
def read( model_name, dat, let, fdir=None, ): """ Reading variable array from SHEMAT-Suite vtk-file. Parameters ---------- model_name : string String of model name. dat : string String with date of model run. let : string String of letter of model run. Returns ------- numpy_array : array Array containing the full single_cell array numpy_array[irobs,iens,iloc]: - irobs: Observation time index - iens: Ensemble member index - iloc: Single cell location index numpy_array_name : string Containing proposed saving location for Array. """ # Single cell output directory if fdir is None: # single_cell_output_dir fdir = (rm.make_output_dirs(model_name, dat, let)[0] + '/single_cell_output') # Read single cell location and variable locs = sc.single_cell_locs(model_name, dat, let) variables = sc.single_cell_variables(model_name, dat, let) # Generate empty numpy_array nrobs_int = sc.nrobs_int(model_name, dat, let) num_locs = sc.num_single_cell(model_name, dat, let) nrens = sc.nrens(model_name, dat, let) numpy_array = np.zeros([nrobs_int, nrens, num_locs]) # single_cell_output_file for iloc, loc in enumerate(locs): fname = ('single_cell_E1_' + str(loc[0]).zfill(4) + '_' + str(loc[1]).zfill(4) + '_' + str(loc[2]).zfill(4) + '_' + str(variables[iloc]).zfill(4) + '_aft' + '.txt') numpy_array[:, :, iloc] = np.loadtxt(fdir + '/' + fname, skiprows=5) # Numpy Array Name ######################################################## numpy_array_name = pm.py_output_filename( sca.tag, 'single_cell', sc.specl(model_name, dat, let) + '_' + str(num_locs), "npy", ) return numpy_array, numpy_array_name
def read( model_name, dat, let, fdir=None, fname=None, varname='uindex', num_mon=1, ): """ Reading monitor arrays from SHEMAT-Suite. Parameters ---------- model_name : string String of model name. dat : string String with date of model run. let : string String of letter of model run. varname : string Variable name for array to be read. Possibilities: 'uindex' 'head','temp','kz', 'v' num_mon : integer Number for monitoring point. IMPORTANT: num_mon = 0 corresponds to the first monitoring point in SHEMAT monitor file. General: num_mon = i corresponds to monitoring point i+1 Returns ------- numpy_array : array Array containing the monitor variable array numpy_array_name : string Containing proposed saving location for Array. """ # Dirs if fdir is None: # samples_output_dir fdir = rm.make_output_dirs(model_name, dat, let)[1] if fname is None: # monitor_file fname = rm.make_file_dir_names(model_name)[16] # Read from monitor file ################################################## numpy_array = np.genfromtxt( fdir + '/' + fname, dtype='f8', comments='%', usecols=(ma.varpos[varname]), ) # Reshape ################################################################# num_mons = sc.num_mons(model_name, dat, let) if np.remainder(len(numpy_array), num_mons): raise RuntimeError('Problem with num_mons') numpy_array = numpy_array.reshape(len(numpy_array) / num_mons, num_mons) numpy_array = numpy_array[:, num_mon] # Numpy Array Name ######################################################## numpy_array_name = pm.py_output_filename( ma.tag, varname, sc.specl(model_name, dat, let) + '_' + str(num_mon), "npy") return numpy_array, numpy_array_name