input_output.organize
Descriptive description.
1"""Descriptive description.""" 2 3from copy import deepcopy 4import csv 5import datetime as dt 6 7 8class Organize_input(): 9 def __init__(self, keys_pr, keys_fwd, keys_en=None): 10 self.keys_pr = keys_pr 11 self.keys_fwd = keys_fwd 12 self.keys_en = keys_en 13 14 def organize(self): 15 # Organize the data types given by DATATYPE keyword 16 self._org_datatype() 17 # Organize the observed data given by TRUEDATA keyword and initialize predicted data variable 18 self._org_report() 19 20 def get_keys_pr(self): 21 return deepcopy(self.keys_pr) 22 23 def get_keys_fwd(self): 24 return deepcopy(self.keys_fwd) 25 26 def get_keys_en(self): 27 return deepcopy(self.keys_en) 28 29 def _org_datatype(self): 30 """ Check if datatype is given as a csv file. If so, we read and make a list.""" 31 if isinstance(self.keys_fwd['datatype'], str) and self.keys_fwd['datatype'].endswith('.csv'): 32 with open(self.keys_fwd['datatype']) as csvfile: 33 reader = csv.reader(csvfile) # get a reader object 34 datatype = [] # Initialize the list of csv data 35 for rows in reader: # Rows is a list of values in the csv file 36 csv_data = [None] * len(rows) 37 for col in range(len(rows)): 38 csv_data[col] = str(rows[col]) 39 datatype.extend(csv_data) 40 self.keys_fwd['datatype'] = datatype 41 42 if not isinstance(self.keys_fwd['datatype'], list): 43 self.keys_fwd['datatype'] = [self.keys_fwd['datatype']] 44 # make copy for problem keywords 45 self.keys_pr['datatype'] = self.keys_fwd['datatype'] 46 47 def _org_report(self): 48 """ 49 Organize the input true observed data. The obs_data will be a list of length equal length of "TRUEDATAINDEX", 50 and each entery in the list will be a dictionary with keys equal to the "DATATYPE". 51 Also, the pred_data variable (predicted data or forward simulation) will be initialized here with the same 52 structure as the obs_data variable. 53 54 .. warning:: An "N/A" entry in "TRUEDATA" is treated as a None-entry; that is, there is NOT an observed data at this 55 assimilation step. 56 57 .. warning:: The array associated with the first string inputted in "TRUEDATAINDEX" is assumed to be the "main" 58 index, that is, the length of this array will determine the length of the obs_data list! There arrays 59 associated with the subsequent strings in "TRUEDATAINDEX" are then assumed to be a subset of the first 60 string. 61 An example: the first string is SOURCE (e.g., sources in CSEM), where the array will be a list of numbering 62 for the sources; and the second string is FREQ, where the array associated will be a list of frequencies. 63 64 .. info:: It is assumed that the number of data associated with a subset is the same for each index in the subset. 65 For example: If two frequencies are inputted in FREQ, then the number of data for one SOURCE index and one 66 frequency is 1/2 of the total no. of data for that SOURCE index. If three frequencies are inputted, the number 67 of data for one SOURCE index and one frequencies is 1/3 of the total no of data for that SOURCE index, 68 and so on. 69 """ 70 71 # Extract primary indices from "TRUEDATAINDEX" 72 if 'truedataindex' in self.keys_pr: 73 74 if isinstance(self.keys_pr['truedataindex'], list): # List of prim. ind 75 true_prim = self.keys_pr['truedataindex'] 76 else: # Float 77 true_prim = [self.keys_pr['truedataindex']] 78 79 # Check if a csv file has been included as "TRUEDATAINDEX". If so, we read it and make a list, 80 if isinstance(self.keys_pr['truedataindex'], str) and self.keys_pr['truedataindex'].endswith('.csv'): 81 with open(self.keys_pr['truedataindex']) as csvfile: 82 reader = csv.reader(csvfile) # get a reader object 83 true_prim = [] # Initialize the list of csv data 84 for rows in reader: # Rows is a list of values in the csv file 85 csv_data = [None] * len(rows) 86 for ind, col in enumerate(rows): 87 csv_data[ind] = int(col) 88 true_prim.extend(csv_data) 89 self.keys_pr['truedataindex'] = true_prim 90 91 # Check if a csv file has been included as "REPORTPOINT". If so, we read it and make a list, 92 if 'reportpoint' in self.keys_fwd: 93 if isinstance(self.keys_fwd['reportpoint'], str) and self.keys_fwd['reportpoint'].endswith('.csv'): 94 with open(self.keys_fwd['reportpoint']) as csvfile: 95 reader = csv.reader(csvfile) # get a reader object 96 pred_prim = [] # Initialize the list of csv data 97 for rows in reader: # Rows is a list of values in the csv file 98 csv_data = [None] * len(rows) 99 for ind, col in enumerate(rows): 100 try: 101 csv_data[ind] = int(col) 102 except ValueError: 103 csv_data[ind] = dt.datetime.strptime( 104 col, '%Y-%m-%d %H:%M:%S') 105 106 pred_prim.extend(csv_data) 107 self.keys_fwd['reportpoint'] = pred_prim 108 109 # Check if assimindex is given as a csv file. If so, we read and make a potential 2D list (if sequential). 110 if 'assimindex' in self.keys_pr: 111 if isinstance(self.keys_pr['assimindex'], str) and self.keys_pr['assimindex'].endswith('.csv'): 112 with open(self.keys_pr['assimindex']) as csvfile: 113 reader = csv.reader(csvfile) # get a reader object 114 assimindx = [] # Initialize the 2D list of csv data 115 for rows in reader: # Rows is a list of values in the csv file 116 csv_data = [None] * len(rows) 117 for col in range(len(rows)): 118 csv_data[col] = int(rows[col]) 119 assimindx.append(csv_data) 120 self.keys_pr['assimindex'] = assimindx 121 122 # check that they are lists 123 if not isinstance(self.keys_pr['truedataindex'], list): 124 self.keys_pr['truedataindex'] = [self.keys_pr['truedataindex']] 125 if not isinstance(self.keys_fwd['reportpoint'], list): 126 self.keys_fwd['reportpoint'] = [self.keys_fwd['reportpoint']] 127 if not isinstance(self.keys_pr['assimindex'], list): 128 self.keys_pr['assimindex'] = [self.keys_pr['assimindex']]
class
Organize_input:
9class Organize_input(): 10 def __init__(self, keys_pr, keys_fwd, keys_en=None): 11 self.keys_pr = keys_pr 12 self.keys_fwd = keys_fwd 13 self.keys_en = keys_en 14 15 def organize(self): 16 # Organize the data types given by DATATYPE keyword 17 self._org_datatype() 18 # Organize the observed data given by TRUEDATA keyword and initialize predicted data variable 19 self._org_report() 20 21 def get_keys_pr(self): 22 return deepcopy(self.keys_pr) 23 24 def get_keys_fwd(self): 25 return deepcopy(self.keys_fwd) 26 27 def get_keys_en(self): 28 return deepcopy(self.keys_en) 29 30 def _org_datatype(self): 31 """ Check if datatype is given as a csv file. If so, we read and make a list.""" 32 if isinstance(self.keys_fwd['datatype'], str) and self.keys_fwd['datatype'].endswith('.csv'): 33 with open(self.keys_fwd['datatype']) as csvfile: 34 reader = csv.reader(csvfile) # get a reader object 35 datatype = [] # Initialize the list of csv data 36 for rows in reader: # Rows is a list of values in the csv file 37 csv_data = [None] * len(rows) 38 for col in range(len(rows)): 39 csv_data[col] = str(rows[col]) 40 datatype.extend(csv_data) 41 self.keys_fwd['datatype'] = datatype 42 43 if not isinstance(self.keys_fwd['datatype'], list): 44 self.keys_fwd['datatype'] = [self.keys_fwd['datatype']] 45 # make copy for problem keywords 46 self.keys_pr['datatype'] = self.keys_fwd['datatype'] 47 48 def _org_report(self): 49 """ 50 Organize the input true observed data. The obs_data will be a list of length equal length of "TRUEDATAINDEX", 51 and each entery in the list will be a dictionary with keys equal to the "DATATYPE". 52 Also, the pred_data variable (predicted data or forward simulation) will be initialized here with the same 53 structure as the obs_data variable. 54 55 .. warning:: An "N/A" entry in "TRUEDATA" is treated as a None-entry; that is, there is NOT an observed data at this 56 assimilation step. 57 58 .. warning:: The array associated with the first string inputted in "TRUEDATAINDEX" is assumed to be the "main" 59 index, that is, the length of this array will determine the length of the obs_data list! There arrays 60 associated with the subsequent strings in "TRUEDATAINDEX" are then assumed to be a subset of the first 61 string. 62 An example: the first string is SOURCE (e.g., sources in CSEM), where the array will be a list of numbering 63 for the sources; and the second string is FREQ, where the array associated will be a list of frequencies. 64 65 .. info:: It is assumed that the number of data associated with a subset is the same for each index in the subset. 66 For example: If two frequencies are inputted in FREQ, then the number of data for one SOURCE index and one 67 frequency is 1/2 of the total no. of data for that SOURCE index. If three frequencies are inputted, the number 68 of data for one SOURCE index and one frequencies is 1/3 of the total no of data for that SOURCE index, 69 and so on. 70 """ 71 72 # Extract primary indices from "TRUEDATAINDEX" 73 if 'truedataindex' in self.keys_pr: 74 75 if isinstance(self.keys_pr['truedataindex'], list): # List of prim. ind 76 true_prim = self.keys_pr['truedataindex'] 77 else: # Float 78 true_prim = [self.keys_pr['truedataindex']] 79 80 # Check if a csv file has been included as "TRUEDATAINDEX". If so, we read it and make a list, 81 if isinstance(self.keys_pr['truedataindex'], str) and self.keys_pr['truedataindex'].endswith('.csv'): 82 with open(self.keys_pr['truedataindex']) as csvfile: 83 reader = csv.reader(csvfile) # get a reader object 84 true_prim = [] # Initialize the list of csv data 85 for rows in reader: # Rows is a list of values in the csv file 86 csv_data = [None] * len(rows) 87 for ind, col in enumerate(rows): 88 csv_data[ind] = int(col) 89 true_prim.extend(csv_data) 90 self.keys_pr['truedataindex'] = true_prim 91 92 # Check if a csv file has been included as "REPORTPOINT". If so, we read it and make a list, 93 if 'reportpoint' in self.keys_fwd: 94 if isinstance(self.keys_fwd['reportpoint'], str) and self.keys_fwd['reportpoint'].endswith('.csv'): 95 with open(self.keys_fwd['reportpoint']) as csvfile: 96 reader = csv.reader(csvfile) # get a reader object 97 pred_prim = [] # Initialize the list of csv data 98 for rows in reader: # Rows is a list of values in the csv file 99 csv_data = [None] * len(rows) 100 for ind, col in enumerate(rows): 101 try: 102 csv_data[ind] = int(col) 103 except ValueError: 104 csv_data[ind] = dt.datetime.strptime( 105 col, '%Y-%m-%d %H:%M:%S') 106 107 pred_prim.extend(csv_data) 108 self.keys_fwd['reportpoint'] = pred_prim 109 110 # Check if assimindex is given as a csv file. If so, we read and make a potential 2D list (if sequential). 111 if 'assimindex' in self.keys_pr: 112 if isinstance(self.keys_pr['assimindex'], str) and self.keys_pr['assimindex'].endswith('.csv'): 113 with open(self.keys_pr['assimindex']) as csvfile: 114 reader = csv.reader(csvfile) # get a reader object 115 assimindx = [] # Initialize the 2D list of csv data 116 for rows in reader: # Rows is a list of values in the csv file 117 csv_data = [None] * len(rows) 118 for col in range(len(rows)): 119 csv_data[col] = int(rows[col]) 120 assimindx.append(csv_data) 121 self.keys_pr['assimindex'] = assimindx 122 123 # check that they are lists 124 if not isinstance(self.keys_pr['truedataindex'], list): 125 self.keys_pr['truedataindex'] = [self.keys_pr['truedataindex']] 126 if not isinstance(self.keys_fwd['reportpoint'], list): 127 self.keys_fwd['reportpoint'] = [self.keys_fwd['reportpoint']] 128 if not isinstance(self.keys_pr['assimindex'], list): 129 self.keys_pr['assimindex'] = [self.keys_pr['assimindex']]