[Python] ENVI|SARscape常用的Python脚本

25 篇文章 3 订阅

许多操作需要选择很多文件,但经典版的ENVI中可以以文件路径的形式导入
使用Python脚本生成文件路径

列出指定文件夹下指定类型的文件

# -*- coding:utf-8 -*-
# 列出指定文件夹下指定类型的文件
import os

path = r'J:\sentinel404075-process\1_import'


for parent,dirnames,filenames in os.walk(path):
    for file in filenames:
        if file.endswith("SIW1_A_VV_slc_list"):
            print os.path.join(parent,file)

文件夹重命名

# -*- coding:utf-8 -*-
# 重命名 将文件夹中的空格修改为_
import os

path = r'J:\sentinel-process-40_40_75\1_import'



for parent,dirnames,filenames in os.walk(path):
    for dirname in dirnames:
        if dirname=="1 Import":
            whole_path = os.path.join(parent,dirname)
            new_name = os.path.join(parent,"1_import")
            os.rename(whole_path,new_name)

自动下载哨兵的精轨数据

# -*- coding:utf-8 -*-
# Author:PasserQi
# Time:2019-4-5
# 下载文件夹下哨兵数据的精轨数据
# 须知:文件夹下的哨兵数据需解压。不想解压可以修改程序的第43行,.SAFE该为.zip
import urllib
from bs4 import BeautifulSoup
import re
import os
import datetime
import time

# 需要修改的参数
dir_path = r'G:\Sentinel-original data\Orbit40-path40\Frame75-11\added_20180105\SourceData' # 哨兵数据存在的目录
out_path = r'C:\Users\PasserQi\Desktop' #精轨数据保存的目录
FILE_TYPE = ".SAFE" #文件格式:.SAFE .zip
IsDownload = True #是否下载:True False

download_urls = []
error_url = []
url_prefix = 'https://qc.sentinel1.eo.esa.int/aux_poeorb/' #下载地址
def download(dest_dir, url):
    print "正在下载:{}\n\t至{}\n".format(url, dest_dir)
    try:
        urllib.urlretrieve(url, dest_dir, callbackfunc)
    except:
        error_url.append(url)
        print '\tError retrieving the URL:', dest_dir
    else: # 没有异常
        print "\t[done]"
        if url in error_url: #在错误列表里
            error_url.remove(url)
def callbackfunc(blocknum, blocksize, totalsize):
    '''回调函数
    @blocknum: 已经下载的数据块
    @blocksize: 数据块的大小
    @totalsize: 远程文件的大小
    '''
    percent = 100.0 * blocknum * blocksize / totalsize
    if percent > 100:
        percent = 100
    print "%.2f%%"% percent

def get_yestoday(mytime):
    myday = datetime.datetime( int(mytime[0:4]),int(mytime[4:6]),int(mytime[6:8]) )
    delta = datetime.timedelta(days=-1)
    my_yestoday = myday + delta
    my_yes_time = my_yestoday.strftime('%Y%m%d')
    return my_yes_time

if __name__ == '__main__':
    # 获得files
    files = os.listdir(dir_path)

    #files = [
    #   "S1A_IW_SLC__1SDV_20180201T101712_20180201T101742_020412_022E1C_43FD.SAFE",
    #   "S1A_IW_SLC__1SDV_20180213T101712_20180213T101742_020587_0233BB_CA75.SAFE",
    #   "S1A_IW_SLC__1SDV_20180309T101712_20180309T101742_020937_023ED6_693E.SAFE",
    #   ]

    for file in files:
        if not file.endswith(FILE_TYPE):
            continue

        # ###########################
        # 按文件名上的信息查找EOF

        # 拼接URL
        url_param_json = {}
        url_param_json['sentinel1__mission'] = file[0:3]
        date = re.findall(r"\d{8}",file)[0]

        # 若参数为20170316,则搜索的是20170317的数据
        # 所以参数应该提前一天
        # 求date的前一天
        date = get_yestoday(date)

        # 在字符串指定位置插入指定字符
        # 例:20170101 --> 2017-01-01
        tmp = list(date)
        tmp.insert(4,'-');tmp.insert(7,'-')
        date = "".join(tmp)
        url_param_json['validity_start'] = date

        # 获得EOF下载网址
        url_param = urllib.urlencode(url_param_json) #url参数
        url = 'https://qc.sentinel1.eo.esa.int/aux_poeorb/?%s' % url_param #拼接
        print "url:{}".format(url)
        html = urllib.urlopen(url)  # 获取html
        dom = BeautifulSoup(html) # 解析html文档
        a_list = dom.findAll("a")  # 找出<a>
        eof_lists = [a['href'] for a in a_list if a['href'].endswith('.EOF')]  # 找出EOF
        for eof in eof_lists:
            if IsDownload:
                eof_name = eof.split('/')[-1] #名字
                savefile = os.path.join(out_path, eof_name) #保存路径
                download(savefile, eof)
            else:
                download_urls.append(eof)


    if IsDownload: #下载
        print "------------------------------------"
        print "开始下载出错的数据"
        # 下载出错的数据重新下载
        while len(error_url)!=0:
            print "出错的数据有"
            print error_url
            for eof in error_url:
                savefile = os.path.join(out_path, eof)
                download(savefile, url_prefix + eof)
        print "全部下载成功,无出错文件"
    else: #不下载
        with open(os.path.join(out_path, u"下载链接.txt"), "w+") as f:
            for eof in download_urls:
                f.write(eof)
                f.write("\n")
            f.close()

哨兵数据导入-生成导入参数

# -*- coding:utf-8 -*-
# Author:PasserQi
# Python:2.7
# Time:
#       2018-01-13 v1 创建 生成哨兵数据导入参数
#       2019-04-04 v1.1 修改
import re
import os
import datetime
import json

# 需要修改的参数
safe_path = r'G:\xiamen_20190403\1safe'     #哨兵数据存在的目录
eof_path = r'G:\xiamen_20190403\1eof'       #精轨数据存在的目录
out_path = r'G:\xiamen_20190403\2import'    #输出文件夹
again_generate = False
    # False:第一次导入
    # True: 二次导入--从ERRORS中导入

def get_yestoday(mytime):
    myday = datetime.datetime( int(mytime[0:4]),int(mytime[4:6]),int(mytime[6:8]) )
    delta = datetime.timedelta(days=-1)
    my_yestoday = myday + delta
    my_yes_time = my_yestoday.strftime('%Y%m%d')
    return my_yes_time

def save_json(dir, fn, obj):
    """ 将Python对象保存成JSON文件
    :param dir:
    :param fn:
    :param obj:
    :return:
    """
    import os
    fp = os.path.join(dir, fn + '.json')
    if os.path.exists(fp): #原来有,删掉
        os.remove(fp)
    with open(fp, 'w+') as f:
        json.dump(obj, f, indent=4)
        f.close()
    return fp

eof_datas = []
datas = []
def gen_params(again_generate):
    safe_datas = []

    if again_generate: #从ERRORS导入
        with open(os.path.join(out_path, "errors.json"), "r") as f:
            safe_datas = json.load(f)
    else: #第一次导入
        # 得到safe文件
        files = os.listdir(safe_path)
        for file in files:
            if file.endswith(".SAFE"):
                safe_datas.append(file)

    out_str = ""

    # 得到eof文件
    files = os.listdir(eof_path)
    for file in files:
        if file.endswith(".eof") or file.endswith(".EOF"):
            eof_datas.append(file)

    cnt = 0
    errors = []
    for safe_data in safe_datas:
        # 获得数据日期
        date = re.findall(r"\d{8}", safe_data)[0]
        # 获得前一天的日期
        yestoday_date = get_yestoday(date)

        # 找到对应的eof文件
        isFound = False
        for eof_data in eof_datas:
            if yestoday_date in eof_data:
                datas.append({
                    "date": date,
                    "safe_data": os.path.join(safe_path, safe_data),
                    "eof_data": os.path.join(eof_path, eof_data),
                    "out_path": os.path.join(out_path, date)
                })
                eof_datas.remove(eof_data)
                isFound = True
                break
        if isFound == False:  # 没有EOF文件
            errors.append(safe_data)

    out_str += "\n--------Input file list(哨兵数据列表)--------\n"
    for data in datas:
        out_str += "%s\\manifest.safe\n" % data["safe_data"]

    out_str += "\n--------Orbit list(EOF数据列表)--------\n"
    for data in datas:
        out_str += data["eof_data"] + "\n"

    out_str += "\n--------Output file list(输出列表)--------\n"
    for data in datas:
        out_str += "%s\\sentinel1\n" % data["out_path"]

    out_str += "\n--------没有找到以下数据的EOF--------\n"
    out_str += "[\n"
    for error in errors:
        out_str += '"{}",\n'.format(error)
    out_str += "]\n"

    if again_generate:
        fp = os.path.join(out_path, 'import_params2.txt')
    else:
        fp = os.path.join(out_path, "import_params1.txt")
    with open(fp, "w+") as f:
        f.write(out_str)
        f.close()
    print "[done] 请查看文件:{}".format(fp)

    # 保存ERRORS数据
    save_json(out_path, "errors", errors)

if __name__ == '__main__':
    gen_params(again_generate)

生成裁剪参数

# -*- coding:utf-8 -*-
# 生成裁剪参数
import os

path = r'J:\ningbo\1_import'
out_path = r""
FILE_NAME_END = "VV_slc_list"

files = []
out_files = []
i = 0
for parent,dirnames,filenames in os.walk(path):
    for file in filenames:
        if file.endswith(FILE_NAME_END):
            i += 1
            # intput
            fp = os.path.join(parent, file)
            files.append(fp)
            # output
            date_str = fp.split("\\")[-2]
            out_fp = os.path.join(out_path, date_str)
            out_fp = os.path.join(out_fp, file + "_cut")
            out_files.append(out_fp)


with open(os.path.join(out_path, "sub_params_list.txt"), "w+") as fp:
    fp.write("\n---------input list---------\n")
    for file in files:
        fp.write("{}\n".format(file) )
    fp.write("\n---------output list---------\n")
    for file in out_files:
        fp.write("{}\n".format(file))
    fp.close()


print "[done] {}个文件".format(i)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

geodoer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值