forked from nasa/georef_imageregistration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IrgStringFunctions.py
100 lines (76 loc) · 3.17 KB
/
IrgStringFunctions.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# __BEGIN_LICENSE__
# Copyright (c) 2009-2013, United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration. All
# rights reserved.
#
# The NGT platform is licensed under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# __END_LICENSE__
"""IrgStringFunctions.py - String parsing functions"""
import sys, os, re, string, time
def isNumber(text):
"""Returns True if the text is a number"""
try:
float(text)
except ValueError: # Not a number
return False
return True # No error, the number must have converted
def convertToFloatIfNumber(text):
"""If the text is a number returns float(text), otherwise just text"""
try:
a = float(text)
except ValueError: # Not a number
return text
return a # No error, the number must have converted
def getLineAfterText(text, prefix, startPos=0, includeText=False):
"""Extracts the remainder of a line after a prefix"""
# StartPos = index in the text to start looking at
# Find the prefix
prefixLoc = text.find(prefix, startPos)
if prefixLoc < 0:
raise Exception('Desired text not found: ' + prefix)
# Find the bounds after the prefix
prefixEnd = prefixLoc + len(prefix)
nextEnd = text.find('\n', prefixEnd)
if nextEnd == -1: # If there was no trailing \n, use one past last character.
nextEnd = len(text)
# Check that we found the bounds
if (prefixEnd < startPos) or (nextEnd <= prefixEnd):
raise Exception('Desired text not found: ' + prefix)
# Extract the text
if includeText:
return text[prefixLoc:nextEnd]
else:
return text[prefixEnd:nextEnd]
def getNumbersInParentheses(text):
"""Returns all sets of numbers in parentheses"""
numberSets = []
# Find all number sets in the text
parenGroups = re.findall( r'\([ \d.,-]*\)', text)
for p in parenGroups:
numberText = p[1:-1] # Remove ()
numberList = numberText.split(',')
numbers = []
for n in numberList: # Convert strings to floats
numbers.append(float(n))
numberSets.append(numbers) # Add this set of numbers to output list
# If only one set of numbers return a list
if len(numberSets) == 1:
return numberSets[0]
else: # Otherwise return a list of lists
return numberSets
def getNumberAfterEqualSign(text, lineStart=0):
"""Extracts a number after an '=' sign on a line"""
# LineStart = index in the text to start looking at
numberText = getLineAfterText(text, '=', lineStart, False)
return convertToFloatIfNumber(numberText)