def _load_csv(self, csv_file): """ Load simulation data from CSV file and return a numpy array. """ if csv_file is None: return None df = pd.read_csv(csv_file) if df.isnull().values.any(): raise ValueError("CSV file contains missing values. Please check data consistency.") data = df.to_numpy() if data.ndim != 2: raise ValueError("Data from CSV is not 2-dimensional (time versus variable)") if data.shape[0] < 2: logger.warning("CSV data does not contain more than one time step.") if data.shape[1] < (self.system.dae.m + self.system.dae.n): logger.warning("CSV data contains fewer variables than required.") logger.warning("Check if the CSV data file is generated from the test case.") # set start and end times from data self.config.t0 = data[0, 0] self.config.tf = data[-1, 0] return data
def list2array(self): """ Set internal storage for timeseries data. Open file and read data into internal storage. """ # TODO: timeseries file must exist for setup to pass. Consider moving # the file reading to a later stage so that adding sheets to xlsx file can work # without the file existing. Model.list2array(self) # read and store data for ii in range(self.n): idx = self.idx.v[ii] path = self.path.v[ii] sheet = self.sheet.v[ii] if not os.path.isabs(path): path = os.path.join(self.system.files.case_path, path) if not os.path.exists(path): raise FileNotFoundError('<%s idx=%s>: File not found: "%s"', self.class_name, idx, path) # --- read supported formats --- if path.endswith("xlsx") or path.endswith("xls"): df = self._read_excel(path, sheet, idx) elif path.endswith("csv"): df = pd.read_csv(path) for field in self.fields.v[ii]: if field not in df.columns: raise ValueError( 'Field {} not found in timeseries data'.format(field)) self._data[idx] = df logger.info('Read timeseries data from "%s"', path)