Skip to content

Commit

Permalink
increase consistency path referencing BlueSky files
Browse files Browse the repository at this point in the history
  • Loading branch information
jooste committed May 30, 2022
1 parent 77a5184 commit 52d0fee
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 89 deletions.
25 changes: 11 additions & 14 deletions bluesky/navdatabase/loadnavdata_txt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
''' Load navigation data from text files.'''
import os
from pathlib import Path
import numpy as np
from zipfile import ZipFile
from bluesky import settings
Expand All @@ -23,7 +23,7 @@ def loadnavdata_txt():
wptdata['wpdesc'] = [] # description


with open(os.path.join(settings.navdata_path, 'nav.dat'), 'rb') as f:
with open(Path(settings.navdata_path) / 'nav.dat', 'rb') as f:
print("Reading nav.dat")

for line in f:
Expand Down Expand Up @@ -101,7 +101,7 @@ def loadnavdata_txt():
wptdata['wpdesc'].append(" ") # Description

#---------- Read fix.dat file ----------
with open(os.path.join(settings.navdata_path, 'fix.dat'), 'rb') as f:
with open(Path(settings.navdata_path) / 'fix.dat', 'rb') as f:
print("Reading fix.dat")
for line in f:
line = line.decode(encoding="ascii", errors="ignore").strip()
Expand Down Expand Up @@ -152,7 +152,7 @@ def loadnavdata_txt():
awydata['awupfl'] = [] # highest flight level (int)


with open(os.path.join(settings.navdata_path, 'awy.dat'), 'rb') as f:
with open(Path(settings.navdata_path) / 'awy.dat', 'rb') as f:
print("Reading awy.dat")

for line in f:
Expand Down Expand Up @@ -220,7 +220,7 @@ def loadnavdata_txt():
aptdata['aptype'] = [] # type (int, 1=large, 2=medium, 3=small)
aptdata['apco'] = [] # two char country code (string)
aptdata['apelev'] = [] # field elevation ft-> m
with open(os.path.join(settings.navdata_path, 'airports.dat'), 'rb') as f:
with open(Path(settings.navdata_path) / 'airports.dat', 'rb') as f:
types = {'L': 1, 'M': 2, 'S': 3}
for line in f:
line = line.decode(encoding="ascii", errors="ignore").strip()
Expand Down Expand Up @@ -276,15 +276,12 @@ def loadnavdata_txt():
firdata['firlat1'] = []
firdata['firlon1'] = []

files = os.listdir(os.path.join(settings.navdata_path, 'fir'))

# Get fir names
for filname in files:
if ".txt" in filname:
firname = filname[:filname.index(".txt")]
for filname in (Path(settings.navdata_path) / 'fir').iterdir():
if filname.suffix == '.txt':
firname = filname.stem
firdata['fir'].append([firname, [], []])

with open(os.path.join(settings.navdata_path, 'fir/' + filname), 'rb') as f:
with open(filname, 'rb') as f:
for line in f:
rec = line.decode(encoding="ascii", errors="ignore").upper().strip()

Expand Down Expand Up @@ -326,7 +323,7 @@ def loadnavdata_txt():
codata['cocode2'] = [] # 2 char code
codata['cocode3'] = [] # 3 char code
codata['conr'] = [] # country nr
with open(os.path.join(settings.navdata_path, 'icao-countries.dat'), 'rb') as f:
with open(Path(settings.navdata_path) / 'icao-countries.dat', 'rb') as f:
for line in f:
line = line.decode(encoding="ascii", errors="ignore").strip()
# Skip empty lines or comments
Expand Down Expand Up @@ -359,7 +356,7 @@ def loadthresholds_txt():
''' Runway threshold loader for navdatabase. '''
rwythresholds = dict()
curthresholds = None
zfile = ZipFile(os.path.join(settings.navdata_path, 'apt.zip'))
zfile = ZipFile(Path(settings.navdata_path) / 'apt.zip')
print("Reading apt.dat from apt.zip")
with zfile.open('apt.dat', 'r') as f:
for line in f:
Expand Down
9 changes: 5 additions & 4 deletions bluesky/stack/basecmds.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
''' BlueSky base stack commands. '''
import webbrowser
import os
from pathlib import Path

import bluesky as bs
from bluesky import settings
Expand Down Expand Up @@ -482,11 +482,12 @@ def setscenpath(newpath):
if not newpath:
return False, "Needs an absolute or relative path"

newpath = Path(newpath)
# If this is a relative path we need to prefix scenario folder
if not os.path.isabs(newpath):
newpath = os.path.join(settings.scenario_path, newpath)
if not newpath.is_absolute():
newpath = Path(settings.scenario_path) / newpath

if not os.path.exists(newpath):
if not newpath.is_dir():
return False, "Error: cannot find path: " + newpath

# Change path
Expand Down
15 changes: 7 additions & 8 deletions bluesky/stack/clientstack.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from pathlib import Path
import subprocess

import bluesky as bs
Expand Down Expand Up @@ -90,14 +90,13 @@ def showhelp(cmd: 'txt' = '', subcmd: 'txt' = ''):
@showhelp.subcommand
def pdf():
''' Open a pdf file with BlueSky command help text. '''
os.chdir("docs")
pdfhelp = "BLUESKY-COMMAND-TABLE.pdf"
if os.path.isfile(pdfhelp):
pdfhelp = Path("BLUESKY-COMMAND-TABLE.pdf")
if (Path('docs') / pdfhelp).is_file():
try:
subprocess.Popen(pdfhelp, shell=True)
subprocess.Popen(pdfhelp, shell=True, cwd='docs')
except:
return "Opening " + pdfhelp + " failed."
return "Opening " + pdfhelp.as_posix() + " failed."
else:
return pdfhelp + "does not exist."
os.chdir("..")
return pdfhelp.as_posix() + "does not exist."

return "Pdf window opened"
36 changes: 17 additions & 19 deletions bluesky/stack/simstack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
''' Main simulation-side stack functions. '''
from fnmatch import fnmatch
import math
import os
from pathlib import Path
import traceback
import bluesky as bs
from bluesky.stack.stackbase import Stack, stack, checkscen, forward
Expand Down Expand Up @@ -137,16 +138,12 @@ def process(from_pcall=None):

def readscn(fname):
''' Read a scenario file. '''
# Split the incoming filename into a path + filename and an extension
base, ext = os.path.splitext(fname.replace("\\", "/"))
if not os.path.isabs(base):
base = os.path.join(settings.scenario_path, base)
ext = ext or ".scn"

# The entire filename, possibly with added path and extension
fname_full = os.path.normpath(base + ext)

with open(fname_full, "r") as fscen:
# Ensure .scn suffix and specify path if necessary
fname = Path(fname).with_suffix('.scn')
if not fname.is_absolute():
fname = Path(settings.scenario_path) / fname

with open(fname, "r") as fscen:
prevline = ''
for line in fscen:
line = line.strip()
Expand Down Expand Up @@ -264,25 +261,25 @@ def ic(filename : 'string' = ''):
filename = bs.scr.show_file_dialog()

# Clean up filename
filename = filename.strip()
filename = Path(filename)

# Reset sim and open new scenario file
if filename:
try:
for (cmdtime, cmd) in readscn(filename):
Stack.scentime.append(cmdtime)
Stack.scencmd.append(cmd)
Stack.scenname, _ = os.path.splitext(os.path.basename(filename))
Stack.scenname = filename.stem

# Remember this filename in IC.scn in scenario folder
with open(settings.scenario_path + "/" + "ic.scn", "w") as keepicfile:
with open(Path(settings.scenario_path) / "ic.scn", "w") as keepicfile:
keepicfile.write(
"# This file is used by BlueSky to save the last used scenario file\n"
)
keepicfile.write(
"# So in the console type 'IC IC' to restart the previously used scenario file\n"
)
keepicfile.write("00:00:00.00>IC " + filename + "\n")
keepicfile.write(f"00:00:00.00>IC {filename}\n")

return True, f"IC: Opened {filename}"
except FileNotFoundError:
Expand Down Expand Up @@ -380,13 +377,14 @@ def makedoc():
''' MAKEDOC: Make markdown templates for all stack functions
that don't have a doc page yet.
'''
if not os.path.isdir("tmp"):
os.mkdir("tmp")
tmp = Path('tmp')
if not tmp.is_dir():
tmp.mkdir()
# Get unique set of commands
cmdobjs = set(Command.cmddict.values())
for o in cmdobjs:
if not os.path.isfile(f"data/html/{o.name}.html"):
with open(f"tmp/{o.name.lower()}.md", "w") as f:
if not Path(f"data/html/{o.name}.html").is_file():
with open(tmp / f"{o.name.lower()}.md", "w") as f:
f.write(
f"# {o.name}: {o.name.capitalize()}\n"
+ o.help
Expand Down
14 changes: 7 additions & 7 deletions bluesky/tools/cachefile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from os import path
from pathlib import Path
import pickle

from bluesky import settings
Expand All @@ -19,14 +19,14 @@ class CacheError(Exception):
class CacheFile():
''' Convenience class for loading and saving pickle cache files. '''
def __init__(self, fname, version_ref='1'):
self.fname = path.join(settings.cache_path, fname)
self.fname = Path(settings.cache_path) / fname
self.version_ref = version_ref
self.file = None

def check_cache(self):
''' Check whether the cachefile exists, and is of the correct version. '''
if not path.isfile(self.fname):
raise CacheError('Cachefile not found: ' + self.fname)
if not self.fname.is_file():
raise CacheError('Cachefile not found: ' + str(self.fname))

self.file = open(self.fname, 'rb')
version = pickle.load(self.file)
Expand All @@ -35,8 +35,8 @@ def check_cache(self):
if not version == self.version_ref:
self.file.close()
self.file = None
raise CacheError('Cache file out of date: ' + self.fname)
print('Reading cache: ' + self.fname)
raise CacheError('Cache file out of date: ' + str(self.fname))
print('Reading cache:', self.fname)

def load(self):
''' Load a variable from the cache file. '''
Expand All @@ -50,7 +50,7 @@ def dump(self, var):
if self.file is None:
self.file = open(self.fname, 'wb')
pickle.dump(self.version_ref, self.file, pickle.HIGHEST_PROTOCOL)
print("Writing cache: " + self.fname)
print("Writing cache:", self.fname)
pickle.dump(var, self.file, pickle.HIGHEST_PROTOCOL)

def __enter__(self):
Expand Down
4 changes: 2 additions & 2 deletions bluesky/tools/geo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" This module defines a set of standard geographic functions and constants for
easy use in BlueSky. """
from os import path
from pathlib import Path
import numpy as np
from math import *

Expand Down Expand Up @@ -493,7 +493,7 @@ def initdecl_data():
# lat : 89 ... -90
# Lon: -180 ... 179
global decl_read, decl_lat_lon
dec_table = np.genfromtxt(path.join(settings.navdata_path, 'geo_declination_data.csv'),
dec_table = np.genfromtxt(Path(settings.navdata_path) / 'geo_declination_data.csv',
comments='#',delimiter=",")

decl = dec_table[:,4]
Expand Down
4 changes: 2 additions & 2 deletions bluesky/traffic/route.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" Route implementation for the BlueSky FMS."""
from os import path
from pathlib import Path
from weakref import WeakValueDictionary
from numpy import *
import bluesky as bs
Expand Down Expand Up @@ -1487,7 +1487,7 @@ def dumprte(acidx: 'acid'):
acid = bs.traf.id[acidx]
acrte = Route._routes.get(acid)
# Open file in append mode, write header
with open(path.join(bs.settings.log_path, 'routelog.txt'), "a") as f:
with open(Path(bs.settings.log_path) / 'routelog.txt', "a") as f:
f.write("\nRoute "+acid+":\n")
f.write("(name,type,lat,lon,alt,spd,toalt,xtoalt) ")
f.write("type: 0=latlon 1=navdb 2=orig 3=dest 4=calwp\n")
Expand Down
6 changes: 3 additions & 3 deletions bluesky/ui/loadvisuals_txt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
''' Load visual data from text files.'''
from bluesky.navdatabase.loadnavdata_txt import thresholds, thrpoints
import os
from pathlib import Path
from zipfile import ZipFile
import numpy as np
import bluesky as bs
Expand Down Expand Up @@ -57,7 +57,7 @@ def load_coastline_txt():
# coastlines to numpy arrays with lat/lon
coast = []
clat = clon = 0.0
with open(os.path.join(settings.navdata_path, 'coastlines.dat'), 'r') as f:
with open(Path(settings.navdata_path) / 'coastlines.dat', 'r') as f:
print("Reading coastlines.dat")
for line in f:
line = line.strip()
Expand Down Expand Up @@ -131,7 +131,7 @@ def load_aptsurface_txt():
apt_bb = BoundingBox()
count = 0
bytecount = 0
zfile = ZipFile(os.path.join(settings.navdata_path, 'apt.zip'))
zfile = ZipFile(Path(settings.navdata_path) / 'apt.zip')
fsize = float(zfile.getinfo('apt.dat').file_size)
print("Reading apt.dat from apt.zip")
with zfile.open('apt.dat', 'r') as f:
Expand Down
6 changes: 3 additions & 3 deletions extra/CRmethods/Difgame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
@author: Jerom Maas
"""
import os
from pathlib import Path
import numpy as np
import Difgamelearnerfunctions as dg
from bluesky.tools.aero import nm, kts, vtas2eas


def start(dbconf):
filepath=os.path.abspath("Difgame.py")
fpath=filepath[:-10]+"\CDRmethods\DifgameActions.npy"
filepath=Path(__file__).parent
fpath = filepath / 'CDRmethods' / 'DifgameActions.npy'
dbconf.Controls = np.load(fpath)

# Discretization numbers: in how many elements is discretized
Expand Down
17 changes: 11 additions & 6 deletions plugins/windecmwf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
import os
import cdsapi
import datetime
Expand All @@ -8,13 +9,17 @@
from bluesky.core import timed_function
from bluesky.traffic.windsim import WindSim

datadir = settings.data_path + '/NetCDF/'

if not os.path.exists(datadir):
os.makedirs(datadir)
datadir = Path('')


def init_plugin():
global datadir
datadir = Path(settings.data_path) / 'NetCDF'

if not datadir.is_dir():
datadir.mkdir()

global windecmwf
windecmwf = WindECMWF()

Expand Down Expand Up @@ -47,10 +52,10 @@ def fetch_nc(self, year, month, day):

ymd = "%04d%02d%02d" % (year, month, day)

fname = 'p_levels_%s.nc' % (ymd)
fpath = datadir + fname
fname = f'p_levels_{ymd}.nc'
fpath = datadir / fname

if not os.path.isfile(fpath):
if not fpath.is_file():
bs.scr.echo("Downloading file, please wait...")

# Set client
Expand Down
Loading

0 comments on commit 52d0fee

Please sign in to comment.