Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for remove_stns_sinex() #141

Merged
merged 5 commits into from
Jul 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ env
/geodepy/XVSOLFIN_20220115.SNX.AUS
/geodepy/XVSOLFIN_20220115.SNX
/geodepy/REDX.SNX
/geodepy/splitApref.py
/geodepy/SNXEPO.SNX
/geodepy/exciseStationsNGCA.py
/geodepy/XVSOLFIN_20220326.SNX
/geodepy/exciseStationsAPREF.py
/geodepy/gda2020.dat
114 changes: 107 additions & 7 deletions geodepy/gnss.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,74 @@
In Development
"""

import sys
from datetime import datetime
from numpy import zeros
from geodepy.angles import DMSAngle
import sys


def list_sinex_blocks(file):
"""This script lists the blocks in a SINEX file

:param str file: the input SINEX file
"""
blocks = []
with open(file) as f:
for line in f.readlines():
if line.startswith('+'):
col = line.split(' ')
block = col[0].replace('+', '')
blocks.append(block.strip())
for block in blocks:
print(block)


def print_sinex_comments(file):
"""This script lists prints comments in a SINEX file

:param str file: the input SINEX file
"""
go = False
with open(file) as f:
for line in f.readlines():
if line.startswith('+FILE/COMMENT'):
go = True
if go:
print(line.strip())
if line.startswith('-FILE/COMMENT'):
go = False

def set_creation_time():
"""This function sets the creation time, in format YY:DDD:SSSSS, for use
in the SINEX header line

:return: creation_time
:rtype: str
"""
now = datetime.now()
time_tup = now.timetuple()
year = str(time_tup.tm_year)[2:]
doy = time_tup.tm_yday
doy = '{:03d}'.format(doy)
seconds = (now - now.replace(hour=0, minute=0, second=0, microsecond=0))\
.total_seconds()
seconds = '{:.0f}'.format(seconds)
creation_time = year + ':' + doy + ':' + seconds

return creation_time


def read_sinex_header_line(file):
"""This function reads the header line of a SINEX file into a string

:param str file: the input SINEX file
:return: header_line
:rtype: str
"""
with open(file) as f:
header_line = f.readline()

return header_line


def read_sinex_estimate(file):
Expand Down Expand Up @@ -386,6 +451,7 @@ def read_sinex_header_block(sinex):

block = []
with open(sinex, 'r') as f:
line = f.readline()
line = f.readline()
while line:
block.append(line)
Expand Down Expand Up @@ -495,13 +561,36 @@ def remove_stns_sinex(sinex, sites):
:return: SINEX file output.snx
"""

# The block separator
separator = '*' + '-' * 79 + '\n'

# Open the output file
with open('output.snx', 'w') as out:
header = read_sinex_header_block(sinex)
for line in header:
out.write(line)
del header

# Get header line and update the creation time and the number of
# parameter estimates. Write the updated header line to the new file
header = read_sinex_header_line(sinex)
old_creation_time = header[15:27]
creation_time = set_creation_time()
header = header.replace(old_creation_time, creation_time)
old_num_params = int(header[60:65])
if header[70:71] == 'V':
num_stn_params = 6
else:
num_stn_params = 3
solution_epochs = read_sinex_solution_epochs_block(sinex)
num_stns_to_remove = 0
for line in solution_epochs:
site = line[1:5]
if site in sites:
num_stns_to_remove += 1
del solution_epochs
num_params = old_num_params - num_stn_params * num_stns_to_remove
header = header.replace(str(old_num_params), str(num_params))
out.write(header)
out.write(separator)

# Read in the site ID block and write out the sites not being removed
site_id = read_sinex_site_id_block(sinex)
for line in site_id:
if line.startswith('*') or line.startswith('+') or \
Expand All @@ -513,6 +602,9 @@ def remove_stns_sinex(sinex, sites):
out.write(line)
del site_id
out.write(separator)

# Read in the solution epochs block and write out the epochs of the
# sites not being removed
solution_epochs = read_sinex_solution_epochs_block(sinex)
for line in solution_epochs:
if line.startswith('*') or line.startswith('+') or \
Expand All @@ -524,6 +616,9 @@ def remove_stns_sinex(sinex, sites):
out.write(line)
del solution_epochs
out.write(separator)

# Read in the solution estimate block and write out the estimates of
# the sites not being removed
skip = []
estimate_number = 0
solution_estimate = read_sinex_solution_estimate_block(sinex)
Expand All @@ -543,6 +638,9 @@ def remove_stns_sinex(sinex, sites):
out.write(line)
del solution_estimate
out.write(separator)

# Read in the matrix estimate block and write out minus the sites
# being removed
vcv = {}
solution_matrix_estimate = \
read_sinex_solution_matrix_estimate_block(sinex)
Expand All @@ -558,7 +656,7 @@ def remove_stns_sinex(sinex, sites):
except KeyError:
vcv[row] = []
vcv[row].append(cols[i])
block_close = solution_matrix_estimate[-1]
block_end = solution_matrix_estimate[-1]
del solution_matrix_estimate
sub_vcv = {}
sub_row = 0
Expand All @@ -584,7 +682,9 @@ def remove_stns_sinex(sinex, sites):
val = sub_vcv[str(i)].pop(0)
line += ' ' + val
out.write(line + '\n')
out.write(block_close)
out.write(block_end)

# Write out the trailer line
out.write('%ENDSNX\n')

return