Skip to content
Snippets Groups Projects

RINEX_loader_client

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Artem Vesnin
    Edited
    rinex_loader_client.py 2.41 KiB
    import requests
    import logging
    import time
    import os 
    
    logger = logging.getLogger("RINEXLoaderClient")
    logger.setLevel(logging.INFO)
    ch = logging.StreamHandler()
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    
    IP = os.environ["RINEX_LOADER_IP"]
    if IP == "":
        raise ValueError(
            "Define enviromnetal variable RINEX_LOADER_IP" \
            "Note that by default Orchester port is 8001 and Collector 8002" 
        )
    
    PORT = 8001
    PORT_COLLECTOR = 8002
    
    date = "1993-01-01"
    
    url_sites = "/api/data/fbd/stations"
    url_file_query = "/api/data/download/date"
    url_status = "/api/task/info/status"
    url_load = "/api/task/download/{}"
    
    def download_file(url):
        local_filename = url.split('/')[-1]
        # NOTE the stream=True parameter below
        with requests.get(url, stream=True) as r:
            r.raise_for_status()
            with open(local_filename, 'wb') as f:
                logger.info(f"Start downloading file from {url}")
                for chunk in r.iter_content(chunk_size=8192): 
                    # If you have chunk encoded response uncomment if
                    # and set chunk_size parameter to None.
                    #if chunk: 
                    f.write(chunk)
        return local_filename
    
    if __name__ == "__main__":
        sites_resp = requests.get(
            f"http://{IP}:{PORT}{url_sites}", 
            json={"date": date}
        )
    
    
        sites = sites_resp.json()["stations"]
        logger.info(f"Got sites: {sites}")
        for site in sites:
            logger.info(f"Start query for site {site}")
            file_resp = requests.get(
                f"http://{IP}:{PORT}{url_file_query}",
                json={"date": "1993-01-01", "prefix": site}
            )
            task_id = file_resp.json()["task"]
            logger.info(f"Create task {task_id}")
            while True:
                status_resp = requests.get(
                    f"http://{IP}:{PORT}{url_status}",
                    json={"task": task_id}
                )
                statuses = list(set(status_resp.json().values()))
                if len(statuses) == 1 and statuses[0] == "FINISHED":
                    logger.info(f"Task {task_id} is ready. Details {status_resp.json()}")
                    break
                else:
                    logger.info(f"Task {task_id} is not ready yet. Details {status_resp.json()}")
                time.sleep(1)
            local_file = download_file(f"http://{IP}:{PORT_COLLECTOR}{url_load}".format(task_id))
            logger.info(local_file)
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment