需求分析:
1、多個(gè)子網(wǎng)的單天H文件合吗铐。也就是同一天的H文件都排列到一起息楔,并在文件地址 后面 加上一個(gè)符號(hào) “+”,而在單天內(nèi)最后一個(gè)文件不加該符號(hào)重斑。
文件特征:
h1704051200_igs1.glx h1706291200_igs7.glx h1709211200_igs6.glx
h1704051200_igs2.glx h1706291200_igs8.glx h1709211200_igs7.glx
h1704051200_igs3.glx h1706301200_igs1.glx h1709211200_igs8.glx
h1704051200_igs4.glx h1706301200_igs2.glx h1709221200_igs1.glx
h1704051200_igs5.glx h1706301200_igs3.glx h1709221200_igs2.glx
h1704051200_igs6.glx h1706301200_igs4.glx h1709221200_igs3.glx
h1704051200_igs7.glx h1706301200_igs5.glx h1709221200_igs4.glx
h1704051200_igs8.glx h1706301200_igs6.glx h1709221200_igs5.glx
h1704061200_igs1.glx h1706301200_igs7.glx h1709221200_igs6.glx
h1704061200_igs2.glx h1706301200_igs8.glx h1709221200_igs7.glx
h1704061200_igs3.glx h1707011200_igs1.glx h1709221200_igs8.glx
如上所示兵睛,每個(gè)文件都是以.glx后綴結(jié)尾;第一個(gè)字符是h窥浪,然后是10位的日期祖很,例如:1704051200,表示2017年4月5日12點(diǎn)0分漾脂,然后是一個(gè)下劃線假颇,然后是4個(gè)字符的子網(wǎng)代碼。
最終效果:
h1704051200_igs1.glx +
h1704051200_igs4.glx +
h1704051200_igs5.glx
h1706291200_igs7.glx +
h1706291200_igs8.glx
python代碼如下:
# -*- coding:utf-8 -*-
"""
coding by python 3.5
功能:把GAMIT的二進(jìn)制H文件骨稿,進(jìn)行列表笨鸡,用于glred平差處理
輸入:二進(jìn)制H文件所在目錄
輸出:以gdl結(jié)尾的文本文件
二進(jìn)制H文件樣例:h1704051200_igs1.glx
by: zzh_my@163.com
create at 2017-10-5
"""
import glob
import shutil
import os
import sys
print('coding by zzh_my@163.com')
print('2017-10-5\n')
hFileFromCatalog = r"E:\ZZH\coding\gamit\example" # H文件的目錄
outHfileAdress = hFileFromCatalog + os.sep + 'gamitHfiles.gdl'
findFiles = hFileFromCatalog+os.sep+'h*.glx' # 0級(jí)目錄搜索
getFileList = [] # 列表 h文件信息存儲(chǔ):文件詳細(xì)地址 短文件名 10字符日期
for hfile in glob.glob(findFiles):
shortFileName = os.path.basename(hfile).lower() # h1704051200_igs1.glx
time10 = shortFileName[1:11].lower() # 10字符日期 1704051200
oneFile = (hfile, shortFileName, time10) # 元組
getFileList.append(oneFile)
# 把H文件列表進(jìn)行排序
afterSortHfiles = sorted(getFileList,key=lambda timeHfile:timeHfile[2])
allNumber = len(afterSortHfiles)
# 單天內(nèi)H文件合并,單天內(nèi)除最后一個(gè)文件不加‘+’符號(hào)外啊终,其余文件后均加符號(hào)‘+’
outHfiles=[]
for icout in range(0 , allNumber-1): #最后一個(gè)永遠(yuǎn)都不加符號(hào)‘+’
sFileOne = afterSortHfiles[icout][2] # afterSortHfiles[icout][0] + ' ' + '+'
sFileTwo=afterSortHfiles[icout+1][2]
if sFileOne == sFileTwo:
goFile = afterSortHfiles[icout][0] + ' ' + '+'
else:
goFile = afterSortHfiles[icout][0]
outHfiles.append(goFile)
#添加最后一個(gè)不加符號(hào)的文件
goFile = afterSortHfiles[allNumber-1][0]
outHfiles.append(goFile)
#把outHfiles列表寫到文件中去镜豹。outHfileAdress
fopen=open(outHfileAdress,'w')
for i in outHfiles:
fopen.write(i)
fopen.write("\n")
fopen.close()
print("everything is ok!!!")