def __init__(self, filename, target=None): warnings.warn( "Support for PFS files in mikeio is experimental. The API is likely to change!" ) print( "Support for PFS files in mikeio is experimental. The API is likely to change!" ) self._pfs = PFSFile(filename) if target is None: self._target = self._find_target(filename) else: self._target = target
class PfsCore: def __init__(self, filename, target=None): warnings.warn( "Support for PFS files in mikeio is experimental. The API is likely to change!" ) print( "Support for PFS files in mikeio is experimental. The API is likely to change!" ) self._pfs = PFSFile(filename) if target is None: self._target = self._find_target(filename) else: self._target = target def section(self, name: str, index=0): pfssection = self._pfs.GetTarget(self._target, 1).GetSection(name, index + 1) return Section(pfssection) def write(self, filename): """ Write PFS file Parameters ---------- filename, str """ self._pfs.Write(filename) @property def start_time(self) -> datetime: target = self._pfs.GetTarget(self._target, 1) section = target.GetSection("TIME", 1) start_time = section.GetKeyword("start_time", 1) vals = [start_time.GetParameter(i).ToInt() for i in range(1, 7)] return datetime(*vals) @start_time.setter def start_time(self, value: datetime): target = self._pfs.GetTarget(self._target, 1) section = target.GetSection("TIME", 1) start_time = section.GetKeyword("start_time", 1) start_time.GetParameter(1).ModifyIntParameter(value.year) start_time.GetParameter(2).ModifyIntParameter(value.month) start_time.GetParameter(3).ModifyIntParameter(value.day) start_time.GetParameter(4).ModifyIntParameter(value.hour) start_time.GetParameter(5).ModifyIntParameter(value.minute) start_time.GetParameter(6).ModifyIntParameter(value.second) @property def end_time(self) -> datetime: start_time = self.start_time nt = self.section("TIME")["number_of_time_steps"].value dt = self.section("TIME")["time_step_interval"].value return start_time + timedelta(seconds=nt * dt) @end_time.setter def end_time(self, value: datetime): print("FOO") start_time = self.start_time dt = self.section("TIME")["time_step_interval"].value nt = int((value - start_time).total_seconds() / dt) self.section("TIME")["number_of_time_steps"] = nt def _find_target(self, filename): with open(filename) as f: lines = f.readlines() for line in lines: if "//" in line: text, comment = line.split("//") else: text = line if "[" in text: startidx = text.index("[") + 1 endidx = text.index("]") target = text[startidx:endidx] return target return None