def _read_history( self) -> Dict[Tuple[datetime, datetime], "ReceiverSinex"]: """Read receiver site history from SINEX file Returns: Dictionary with (date_from, date_to) tuple as key. The values are ReceiverSinex objects. """ if self.source_path is None: log.fatal("No SINEX file path is defined.") # Find site_id and read antenna history p = parsers.parse_file("gnss_sinex_igs", file_path=self.source_path) data = p.as_dict() if self.station in data: raw_info = data[self.station]["site_receiver"] elif self.station.upper() in data: raw_info = data[self.station.upper()]["site_receiver"] else: raise ValueError( f"Station {self.station!r} unknown in source '{self.source_path}'." ) # Create list of receiver history history = dict() for receiver_info in raw_info: receiver = ReceiverSinex(self.station, receiver_info) interval = (receiver.date_installed, receiver.date_removed) history[interval] = receiver return history
def _read_history(self) -> Dict[Tuple[datetime, datetime], "AntennaSinex"]: """Read antenna site history from SINEX file Returns: Dictionary with (date_from, date_to) tuple as key. The values are AntennaSinex objects. """ if self.source_path is None: log.fatal("No SINEX file path is defined.") # Find site_id and read antenna history p = parsers.parse_file("gnss_sinex_igs", file_path=self.source_path) data = p.as_dict() try: if self.station in data: raw_info = data[self.station]["site_antenna"] elif self.station.upper() in data: raw_info = data[self.station.upper()]["site_antenna"] except KeyError: raise ValueError( f"Station {self.station!r} unknown in source {self.source!r}.") # Create list of antenna history history = dict() for antenna_info in raw_info: antenna = AntennaSinex(self.station, antenna_info) interval = (antenna.date_installed, antenna.date_removed) history[interval] = antenna return history
def parse_key(file_key, file_vars=None, parser_name=None, use_cache=True, **parser_args): """Parse a file given in the Where file-list and return parsed data By specifying a `file_key`. The file_key is looked up in the file list to figure out which file that should be parsed. The name of the parser will also be looked up in the file configuration. The dictionary `file_vars` may be specified if variables are needed to figure out the correct file path from the configuration. The following file keys are available: {doc_file_keys} Data can be retrieved either as Dictionaries, Pandas DataFrames or Where Datasets by using one of the methods `as_dict`, `as_dataframe` or `as_dataset`. Example: > df = parsers.parse_key('center_of_mass', file_vars=dict(satellite='Lageos')).as_dataset() Args: file_key (String): Used to look up parser_name and file_path in the Where file configuration. file_vars (Dict): Additional file variables used when looking up file path in configuration. parser_name (String): Name of parser to use. Default is to use parser named in the file list. use_cache (Boolean): Whether to use a cache to avoid parsing the same file several times. parser_args: Input arguments to the parser. Returns: Parser: Parser with the parsed data """ # Read parser_name from config.files if it is not given parser_name = config.files.get(section=file_key, key="parser", value=parser_name).str if not parser_name: log.warn( f"No parser found for {file_key!r} in {', '.join(config.files.sources)}" ) # Figure out the file path file_vars = dict() if file_vars is None else file_vars download_missing = config.where.files.download_missing.bool file_path = config.files.path(file_key, file_vars=file_vars, download_missing=download_missing, use_aliases=True) dependencies.add(file_path, label=file_key) parser_args.setdefault("encoding", config.files.encoding(file_key)) # Use the Midgard parser function to create parser and parse data return parse_file(parser_name, file_path, use_cache=use_cache, timer_logger=log.time, **parser_args)
def get_rinex2_or_rinex3(file_path: pathlib.PosixPath) -> "TODO": """Use either Rinex2NavParser or Rinex3NavParser for reading orbit files in format 2.11 or 3.03. Firstly the RINEX file version is read. Based on the read version number it is decided, which Parser should be used. Args: file_path (pathlib.PosixPath): File path to broadcast orbit file. """ version = gnss.get_rinex_file_version(file_path=file_path) if version.startswith("2"): parser_name = "rinex212_nav" if version == "2.12" else "rinex2_nav" elif version.startswith("3"): parser_name = "rinex3_nav" else: log.fatal(f"Unknown RINEX format {version} is used in file {file_path}") return parsers.parse_file(parser_name=parser_name, file_path=file_path, use_cache=True)
def __init__(self, file_path: Union[str, PosixPath]) -> None: """Set up a new GNSS antenna calibration object by parsing ANTEX file The parsing is done by `midgard.parsers.antex.py` parser. Args: file_path: File path of ANTEX file """ file_path = Path(file_path) if not file_path.exists(): raise ValueError(f"File {file_path} does not exists.") p = parsers.parse_file(parser_name="antex", file_path=file_path) if not p.data_available: raise ValueError(f"No observations in file {file_path}.") self.data = p.as_dict() self.file_path = p.file_path
def _parse_block(self, fid, block, name="", directory=""): # print("Parsing {} {}".format(block, name)) for line in fid: if not line or line.startswith("!"): continue line = line.split() if line[0].lower().startswith("end") and line[1] == block: # print("Finished {} {}".format(block, name)) return elif line[0].lower().startswith("begin"): # recursive call self._parse_block(fid, line[1], name=" ".join(line[2:]), directory=directory) elif line[0].lower().startswith("default_dir"): directory = line[1] elif line[0].endswith(".nc"): file_path = self.file_path.parents[0] / directory / line[0] if directory: data = self.raw.setdefault(directory, {}) else: data = self.raw nc_name = file_path.stem.split("_") nc_stub = nc_name.pop(0) data = data.setdefault(nc_stub, {}) for part in nc_name: if part.startswith("b"): data = data.setdefault(part[1:], {}) # print("Parse {}".format(file_path)) netcdf_data = parsers.parse_file( "vlbi_netcdf", file_path=file_path).as_dict() if "TimeUTC" in file_path.stem: self._parse_time(netcdf_data) data.update(netcdf_data) else: data = self.raw.setdefault(block, {}) if name: data = data.setdefault(name, {}) data[line[0]] = " ".join(line[1:])
def get_parser(parser_name): """Get a parser that has parsed an example file""" example_path = pathlib.Path( __file__).parent / "example_files" / parser_name return parsers.parse_file(parser_name, example_path)