Skip to content
Snippets Groups Projects
Commit 1ff37b26 authored by Martin Schrön's avatar Martin Schrön
Browse files

Collected some general functions into lib/PASDY

parent 23474bb8
No related branches found
No related tags found
No related merge requests found
import CORN
\ No newline at end of file
import PASDY
# General functions
## Flushed print
##
_orig_print = print
def print_flush(*args, **kwargs):
_orig_print(*args, flush=True, **kwargs)
## System calls
##
### Launch a file using its corresponding default application
### Usage: execute('file.pdf')
###
def execute(file):
import subprocess, os, platform
file = os.path.abspath(file)
if platform.system() == 'Darwin': # macOS
subprocess.call(('open', file))
elif platform.system() == 'Windows': # Windows
os.startfile(file)
else: # linux variants
subprocess.call(('xdg-open', file))
## Definition shortcuts
##
### If a: convert(a), else b
###
def ifdef(a, b, convert=float):
if not a:
return(b)
else:
return(convert(a))
## Arrays
##
### Split str by comma
### Append some NaNs
###
def xsplit(s, range=0, type=str):
a = np.array(s.replace(',',' ').split(), dtype=type)
# fill up with NaNs
for i in np.arange(range-len(a)):
a = np.append(a, np.nan)
return(a)
## Strings
##
### Chomp
###
def chomp(x):
if x.endswith("\r\n"): return x[:-2]
if x.endswith("\n") or x.endswith("\r"): return x[:-1]
return(x)
## Input
##
### yes/no <-> Boolean
###
def yesno2bool(s):
s = s.lower()
if s == 'yes' or s == 'y':
return(True)
else:
return(False)
###
def bool2yesno(boo):
if boo:
return('yes')
else:
return('no')
## Output
##
### Print newly created file name and size
###
def file_save_msg(file, name='File', end=''):
size = os.stat(file).st_size
if size < 1024: sizestr = "%.0f Bytes" % (size)
elif size < 1024**2: sizestr = "%.0f KB" % (size/1024)
else: sizestr = "%.1f MB" % (size/1024/1024)
print("%s saved to %s (%s)" % (name, file, sizestr), end=end)
# Spatial functions
## Distance
##
### use x nearest neighbors for averaging
### find k closest lat/lon to every lat/lon coordinates
###
def distance(pointA, pointB):
return sqrt((pointB[0] - pointA[0]) ** 2 + (pointB[1] - pointA[1]) ** 2)
## Coordinate conversion
##
### recalculate strange coordinates to proper decimals
###
def deg100min2dec(coord):
coord = coord/100
deg = np.floor(coord)
minute = (coord - deg)/0.6
dec = deg+minute
return(dec)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment