-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathleica2rnx.py
111 lines (88 loc) · 3.8 KB
/
leica2rnx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#coding=UTF-8
#create date: 2017/1/2
#creater: Zhou Maosheng
#Python version: 3.4
"""Convert leica m00 file to RINEX"""
import os
import sys
import glob
import argparse
# dir_path: directory path
def createdir(dir_path):
"""Create new directory if not exist"""
if not os.path.exists(dir_path):
print('create directory: %s' %dir_path)
os.makedirs(dir_path)
# src_dir: source directory, out_dir: output directory,
# glob_str: glob string, year: year of data observation,
# recursive: search file recursively
def leica2rnx(src_dir, out_dir, year, glob_str, recursive):
"""Convert leica m00 file to RINEX using TEQC"""
file_list = []
for file in glob.glob(os.path.join(src_dir, glob_str)):
# get site and doy in filename
filename = os.path.basename(file)[0:7]
# get global mode of source files
sourcefile = os.path.join(src_dir, filename + '?.' + 'm00')
if sourcefile not in file_list:
file_list.append(sourcefile)
for sourcefile in file_list:
# name of output file
rinexfile = os.path.basename(sourcefile)[0:7] + '0.' + year + 'o'
gfile = os.path.basename(sourcefile)[0:7] + '0.' + year + 'g'
nfile = os.path.basename(sourcefile)[0:7] + '0.' + year + 'n'
# path of output file
rnxfilepath = os.path.join(out_dir, rinexfile.lower())
gfilepath = os.path.join(out_dir, gfile.lower())
nfilepath = os.path.join(out_dir, nfile.lower())
# run teqc
print('generate file: %s %s %s ......' %(rinexfile, nfile, gfile))
command = ('teqc +nav ' + nfilepath + ',' + gfilepath +
' ' + sourcefile + ' > ' + rnxfilepath)
os.system(command)
# process subfolders if --recursive is setted
if recursive:
for child in os.listdir(src_dir):
child_path = os.path.join(src_dir, child)
if os.path.isdir(child_path):
leica2rnx(child_path, out_dir, year, glob_str, recursive)
# args: user input arguments
def main(args):
"""Main function"""
src_dir, out_dir, year, glob_str = args.dir, args.out, args.yr, args.glob
# valid the input year
if not (year.isdigit() and (len(year) != 2 or len(year) != 4)):
print('Parameter %s is not valid!' %year, file=sys.stderr)
return 1
year = year[-2:]
createdir(out_dir)
print('-------------------- input params ----------------------')
print('source dirs: %s' %src_dir)
print('output dir: %s' %out_dir)
print('file mode: %s' %glob_str)
print('year of data: %s' %year)
print('--------------------------------------------------------\n')
for directory in glob.glob(src_dir):
leica2rnx(directory, out_dir, year, glob_str, args.recursive)
return 0
def init_args():
"""Initilize function, parse user input"""
# initilize a argument parser
parser = argparse.ArgumentParser(
description='translate leica m00 file to rinex.')
# add arguments
parser.add_argument('-v', '--version', action='version',
version='leica2rnx.py 0.1.6')
parser.add_argument('-r', '--recursive', action='store_true',
help='search file in subfolders')
parser.add_argument('-yr', metavar='<year>', required=True,
help='year of data [required]')
parser.add_argument('-dir', metavar='<input_dir>', default='.',
help='input dir mode [default: current]')
parser.add_argument('-glob', metavar='<mode>', default='*.[Mm]00',
help='filename search mode [default: *.[Mm]00]')
parser.add_argument('-out', metavar='<output>', default='rinex',
help='output dir [default: rinex in current]')
return main(parser.parse_args())
if __name__ == '__main__':
init_args()