Update:2024/11/14

1. New added:Version choose
2. Correct:The format fault of convert
This commit is contained in:
KenanZhu111 committed 2024-11-14 11:45:49 +08:00
1 parent 733fc3a61a
commit 9f2fada315
3 files changed
+966 -610

No files matched your search

+711 -375
View File
File diff suppressed because it is too large. Load diff
-235
View File
@@ -1,235 +0,0 @@
import time
import math
def ConvertObsVersion(ofile_path, out_directory, version, system):
blank = " "
global outf
if version == 2.10 or version == 2.11:
with open(ofile_path, 'r') as f:
with open(out_directory, 'w') as outf:
outf.close()
# Get the locate time and transform to RINEX format
y = time.localtime().tm_year
m = time.localtime().tm_mon
d = time.localtime().tm_mday
h = time.localtime().tm_hour
min = time.localtime().tm_min
sec = time.localtime().tm_sec
localtime = str(y)+str(m)+str(d)+blank+"%02d"%h+"%02d"%min+"%02d"%sec+blank+"LOC"+blank
# Output the convert message
outf = open(out_directory, 'a')
print(" %4.2f->3.04 COV-OBSERVATION DATA %s CONVERT VERSION / TYPE\n"
"KNZ_Convert ver 1.0.6 %sCONVERT BY / TIME\n"
%(version, system, localtime)
, end="", file=outf)
#Refactoring header file format to 3.04
while 1:
line = f.readline()
if line.find("COMMENT") >= 0:
print(line,end="",file=outf)
elif line.find("RINEX VERSION / TYPE") >= 0:
line = "%9s"%("3.04") + line[ 9:80] + "\r"
print(line,end="",file=outf)
elif line.find("PGM / RUN BY / DATE") >= 0:
hour = line[49:51]
min = line[52:54]
sec = line[55:57]
line = line[ 0:49] + hour + min + sec + blank + "UTC" + blank + "PGM / RUN BY / DATE\n"
print(line,end="",file=outf)
elif line.find("WAVELENGTH FACT L1/2") >= 0:
pass
elif line.find("# / TYPES OF OBSERV") >= 0:
global obsnum
obsnum = int(line[ 0: 6])
count0 = 0 # The line counters
count1 = 0 # The total counters
obstypes= []
while 1:
obstypes.append( str.strip(line[ 0 + 6 * (count0 + 1): 6 + 6 * (count0 + 1)]) )
count0 += 1
count1 += 1
if count1 == obsnum:
break
if count0 % 9 == 0:
line = f.readline()
count0 = 0
# #
# #
# #
#Covert the observation types from 2.xx to 3.04
count = 0
for obstype in obstypes:
if obstype.find("5") >= 0 or obstype.find("7") >= 0 or obstype.find("8") >= 0:
obstype = obstype + "I"
elif obstype.find("6") >= 0:
obstype = obstype + "C"
elif obstype.find("1") >= 0 or obstype.find("2") >= 0:
if obstype.find("P") >= 0:
obstype = "C" + obstype[1:2] + "P"
else:
obstype = obstype + "C"
obstypes[count] = obstype
count += 1
#Refactoring observation type format to 3.04
syss = ['G', 'R', 'E', 'S']
for sys in syss:
line = sys + 3*blank + "%2d"%obsnum + blank
count0 = 0
count1 = 0
for obstype in obstypes:
line += obstype + blank
count0 += 1
count1 += 1
# Output the final line
if count1 == obsnum:
line += (13 - count0) * 4 * blank + blank + "SYS / # / OBS TYPES\n"
print(line, end="", file=outf)
break
# Output the first line
if count0 % 13 == 0:
line += blank + "SYS / # / OBS TYPES\n"
print(line, end="", file=outf)
line = 7*blank
count0 = 0
elif line.find("END OF HEADER") >= 0:
print(line,end="",file=outf)
break
else:
print(line,end="",file=outf)
# Refactoring data file format to 3.04
while 1:
line = f.readline()
if line == "":
break
elif line[0:5] == 5*blank:
print(line, end="", file=outf)
elif line.find("COMMENT") >= 0:
print(line, end="", file=outf)
else:
# Refactoring epoch format to 3.04
year = int(line[ 0 : 3])
month = int(line[ 0+3 : 3+3])
day = int(line[ 0+3+3 : 3+3+3])
hour = int(line[ 0+3+3+3 : 3+3+3+3])
minute = int(line[ 0+3+3+3+3 : 3+3+3+3+3])
second = float(line[ 0+3+3+3+3+3 : 3+3+3+3+3+11])
e_flag = int(line[ 0+3+3+3+3+3+11 : 3+3+3+3+3+11+3])
satnum = int(line[ 0+3+3+3+3+3+11+3: 3+3+3+3+3+11+3+3])
sPRN = []
lineo = ">" + blank + "%4d %02d %02d %02d %02d %10.7f %2d %2d\n"\
%(year+2000, month, day, hour, minute, second, e_flag, satnum)
print(lineo, end="", file=outf)
count0 = 0 # The line counters
count1 = 0 # The total counters
while 1:
sPRN.append(line[32+3*count0:32+3*(count0+1)].replace(blank,"0"))
count0 += 1
count1 += 1
if count1 == satnum:
break
if count0 % 12 == 0:
count0 = 0
line = f.readline()
# #
# #
# #
#Refactoring data format to 3.04
count = 0
while 1:
if obsnum <= 5:
line = f.readline()
line = sPRN[count] + line
elif obsnum > 5:
count1 = 0
lineo =""
while 1:
line = f.readline()
lineo += line.replace("\n", "").replace("\r", "")
if len(lineo) % 16 == 15:
lineo += blank
if len(lineo) % 16 == 14:
lineo += 2*blank
if len(line) < 80:
lineo += (5 - int(len(lineo)/16))*16*blank
count1 += 1
if count1 == math.ceil(obsnum/5):
line = sPRN[count] + lineo +"\n"
break
print(line, end="", file=outf)
count += 1
if count == satnum:
break
outf.close()
f.close()
return 1
def ReadObsVersion(ofile_path, out_directory):
obsfile = None
# Get the obs file name---------------------------#
obspath = len(ofile_path)
while obspath > 0:
flag = ofile_path[obspath - 1:obspath]
if flag == "/":
obsfile = ofile_path[obspath:]
break
obspath -= 1
if out_directory == '':
# Get the file's father folder-------------------#
pathlon = len(ofile_path)
while pathlon - 3 > 0:
flag = ofile_path[pathlon - 3 - 1:pathlon - 3]
if flag == "/":
out_directory = ofile_path[:pathlon - 3] + "COV-" + obsfile
break
pathlon -= 1
# -----------------------------------------------#
else:
out_directory += "COV-" + obsfile
with open(ofile_path, 'r') as f:
while 1:
line = f.readline()
if line.find("RINEX VERSION / TYPE") >= 0:
version = float(line[ 5: 9])
system = line[40:41]
f.close()
if version > 2.11:
return 0
return ConvertObsVersion(ofile_path, out_directory, version, system)
else:
return 0
+255
View File
@@ -0,0 +1,255 @@
# @ File : rnx_convert.py
#
# Copyright (c) 2024 - 2024 KenanZhu. All Right Reserved.
#
# @ Author : KenanZhu
# @ Time : 2024/11/13
# @ Brief :
# :
# @ IDE : PyCharm
#
# ----------------------------------------------------------------------------------------
import re
import time
import math
import threading
bnk=' '
def conv_time():
# Get the locate time and transform to RINEX format
y=time.localtime().tm_year
m=time.localtime().tm_mon
d=time.localtime().tm_mday
h=time.localtime().tm_hour
min=time.localtime().tm_min
sec=time.localtime().tm_sec
localtime=str(y)+str(m)+str(d)+bnk+"%02d"%h+"%02d"%min+"%02d"%sec+bnk+"LLC"+bnk
outline=('KNZ_Convert ver1.2.1'+20*bnk+
'%s'%localtime+
'COV-PGM / DATE\n')
return outline
# =======================================================================================
# Convert observ type of RINEX 2.xx to others
# =======================================================================================
def obsfmt_conv(obstype):
match obstype[1:2]:
case '1':
if obstype[0:1]=='P':
obstype='C1P'
else:
obstype+='C'
case '2':
if obstype[0:1]=='P':
obstype='C2P'
else:
obstype+='C'
case '3': pass
case '4': pass
case '5': obstype+='I'
case '6': obstype+='C'
case '7': obstype+='I'
case '8': obstype+='I'
return obstype
# =======================================================================================
# Convert Format
# =======================================================================================
def datefmt_conv(inline):
hou = inline[49:51]
min = inline[52:54]
sec = inline[55:57]
return (inline[0:49] +
hou +
min +
sec +
bnk + "UTC" +
bnk + "PGM / RUN BY / DATE\n")
# =======================================================================================
# Convert observ msg of RINEX 2.xx to others
# =======================================================================================
def obstype_conv(inline, in_f):
outlines=[]
obstypes=[]
system=['G', 'R', 'E', 'S']
obsnum=int(inline[0:6])
for i in range(math.ceil(obsnum/9)):
obstypes.extend(inline[6:60].split())
if i+1==math.ceil(obsnum/9): break
inline = in_f.readline()
for i in range(len(obstypes)):
obstypes[i]=obsfmt_conv(obstypes[i])
for sys in system:
outline=sys+3*bnk+'%2d'%len(obstypes) + bnk
for i in range(len(obstypes)):
outline+=obstypes[i] + bnk
if (i+1)%13==0:
outlines.append(
outline+bnk+'SYS / # / OBS TYPES\n')
outline = 7*bnk
elif (i+1)==obsnum:
outlines.append(
outline + ( (13-len(obstypes))%13*4 + 1)*bnk + 'SYS / # / OBS TYPES\n')
return outlines
# =======================================================================================
# Convert obs of RINEX 2.xx to others
# =======================================================================================
def rnx2uni_head_bxx(in_path, out_path, oriver, desver):
try:
with open(in_path, 'r'): pass
except FileNotFoundError:
return -12
outlines=[]
in_f=open(in_path, 'r')
while True:
inline=in_f.readline()
if not inline: return 0
if inline.find('COMMENT')>=0:
outlines.append(inline)
elif inline.find('RINEX VERSION / TYPE')>=0:
if not inline.find('OBSERVATION DATA')>=0:
return -10
if not float(inline[5: 9])<3: return -10
outlines.append(
5*bnk+'%s->%s'%(str(oriver),str(desver))+
1*bnk+'COV-OBSERVATION DATA'+
24*bnk+'COV-RINEX VER / TYPE\n')
outlines.append(conv_time())
outlines.append(inline.replace(
'%s'%re.search(r'[1-9]\d*.\d*|0\.\d*[1-9]\d*', inline).group(),
desver))
elif inline.find('PGM / RUN BY / DATE')>=0:
outlines.append(datefmt_conv(inline))
elif inline.find('# / TYPES OF OBSERV')>=0:
outlines.extend(obstype_conv(inline, in_f))
elif inline.find("WAVELENGTH FACT L1/2")>=0: pass
elif inline.find("END OF HEADER")>=0:
outlines.append(inline)
break
else: outlines.append(inline)
try:
with open(out_path, 'w'): pass
except PermissionError:
return -11
out_f=open(out_path, 'a')
out_f.writelines(outlines)
out_f.close()
return 1
# =======================================================================================
# Convert head_data of RINEX 3.xx to Uni-format
# =======================================================================================
def data2uni_bxx(inline, obsnum, in_f):
sPRN=[]
outlines=[]
count_er=0
year = int(inline[0 :3])
month = int(inline[0+3 :3+3])
day = int(inline[0+3+3 :3+3+3])
hour = int(inline[0+3+3+3 :3+3+3+3])
minute= int(inline[0+3+3+3+3 :3+3+3+3+3])
second=float(inline[0+3+3+3+3+3 :3+3+3+3+3+11])
e_flag= int(inline[0+3+3+3+3+3+11 :3+3+3+3+3+11+3])
satnum= int(inline[0+3+3+3+3+3+11+3:3+3+3+3+3+11+3+3])
outlines.append(
"> %4d %02d %02d %02d %02d %10.7f %2d %2d\n" \
% (year + 2000, month, day, hour, minute, second, e_flag, satnum))
for i in range(math.ceil(satnum/12)):
if not i==0: inline = in_f.readline()
if i+1<math.ceil(satnum/12): count_er=12
if i+1==math.ceil(satnum/12): count_er=satnum-i*12
for j in range(count_er):
sPRN.append(inline[32+3*j : 32+3*(j+1)].replace(bnk, "0"))
for i in range(satnum):
outline=''
if obsnum <= 5:
inline=in_f.readline()
outline+=inline.replace("\n","").replace("\r","")
elif obsnum > 5:
for j in range(math.ceil(obsnum/5)):
inline=in_f.readline()
outline+=inline.replace("\n","").replace("\r","")
if len(outline)%16==15: outline+=bnk
if len(outline)%16==14: outline+=2*bnk
outline+=(5 - int(len(outline)/16))*16*bnk
outlines.append(sPRN[i] + outline + "\n")
return outlines
# =======================================================================================
# Convert Uni-format to RINEX 3.xx
# =======================================================================================
def uni2data_cxx(outlines, out_path):
out_f = open(out_path, 'a')
out_f.writelines(outlines)
out_f.close()
# =======================================================================================
# Convert data of RINEX 3.xx to Uni-format
# =======================================================================================
def rnx2uni_data_bxx(in_path, out_path, desver):
obsnum=0
obsget=False
in_f=open(in_path, 'r')
while True:
inline = in_f.readline()
if not inline: break
elif inline.find('# / TYPES OF OBSERV')>=0:
if not obsget:
obsget=True
obsnum=int(inline[0:6])
else: continue
elif inline.find("END OF HEADER")>=0: break
while True:
inline = in_f.readline()
if not inline: break
elif inline[0:5]==5*bnk: continue
elif inline.find("COMMENT")>=0: continue
else:
outlines=data2uni_bxx(inline, obsnum, in_f)
uni2data_cxx(outlines, out_path)
return 1
# =======================================================================================
#
#
#
#
# =======================================================================================
def Convert_Un(in_path, out_path, oriver, desver, resqueue):
origin=0
match oriver:
case '2.10': origin=2
case '2.11': origin=2
case '3.03': origin=3
case '3.04': origin=3
case '3.05': origin=3
if origin==2:
match rnx2uni_head_bxx(in_path, out_path, oriver, desver):
case 1: rnx2uni_data_bxx(in_path, out_path, desver)
case 0: return resqueue.put(0)
case-10: return resqueue.put(0)
case-11: return resqueue.put(0)
case-12: return resqueue.put(0)
elif origin==3: resqueue.put(0)
resqueue.put(1)
# =======================================================================================
def _Convert_Un(in_path, out_path, oriver, desver, resqueue):
T=threading.Thread(
target=lambda :Convert_Un(in_path, out_path, oriver, desver, resqueue)
)
T.start()
T.join()
return resqueue.get()