fetch.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:pyrsss 作者: butala 项目源码 文件源码
def fetch(source,
          dates,
          stns,
          rinex_type='obs',
          template_map=TEMPLATE_MAP,
          local_path='./',
          local_template='{stn}{date:%j}0.{date:%y}{suffix}'):
    """
    ???
    """
    server, template, suffix = TEMPLATE_MAP[source][rinex_type]
    fname_map = defaultdict(dict)
    logger.info('opening connection to {}'.format(server))
    with closing(FTP(server)) as ftp:
        ftp.login()
        for date in dates:
            for stn in stns:
                remote_fname = template.format(date=date, stn=stn) + suffix
                local_fname = os.path.join(local_path.format(date=date, stn=stn, suffix=suffix),
                                           local_template.format(date=date, stn=stn, suffix=suffix))
                logger.info('fetching {} and storing to {}'.format(remote_fname,
                                                                   local_fname))
                touch_path(os.path.dirname(local_fname))
                with open(local_fname, 'w') as fid:
                    try:
                        ftp.retrbinary('RETR {}'.format(remote_fname),
                                       fid.write)
                        fname_map[date][stn] = local_fname
                    except Exception as e:
                        logger.warning('could not fetch {} ({}) --- skipping'.format(remote_fname,
                                                                                     e))
                        os.remove(local_fname)
                        continue
    for date in sorted(fname_map):
        for stn in sorted(fname_map[date]):
            fname_map[date][stn] = decompress(fname_map[date][stn])
    return fname_map
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号