-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert exchange checker to use web workers
- Loading branch information
Showing
5 changed files
with
206 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import io | ||
import json | ||
import logging | ||
import traceback | ||
from html import escape | ||
|
||
from js import Uint8Array, console # noqa: F401 | ||
from pyscript import display # type: ignore | ||
|
||
from cchdo.hydro import __version__ as hydro_version | ||
from cchdo.hydro import accessors, read_exchange # noqa: F401 | ||
from cchdo.params import __version__ as params_version | ||
|
||
|
||
def logger(msg): | ||
display(msg, target="log", append=True) | ||
|
||
|
||
class DisplaylHandler(logging.Handler): | ||
def emit(self, record) -> None: | ||
logger(self.formatter.format(record)) # type: ignore | ||
|
||
|
||
root = logging.getLogger() | ||
root.setLevel(logging.DEBUG) | ||
|
||
handler = DisplaylHandler() | ||
handler.setLevel(logging.DEBUG) | ||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | ||
handler.setFormatter(formatter) | ||
root.addHandler(handler) | ||
|
||
|
||
def versions(): | ||
return { | ||
"hydro_version": hydro_version, | ||
"params_version": params_version, | ||
} | ||
|
||
|
||
def to_netcdf(ex): | ||
ex.to_netcdf("out.nc") | ||
with open("out.nc", "rb") as f: | ||
return f.read() | ||
|
||
|
||
class Pre: | ||
def __init__(self, text): | ||
self.value = text | ||
|
||
def _repr_html_(self): | ||
return f"<pre>{escape(self.value)}</pre>" | ||
|
||
|
||
def to_xarray(array_buffer, checks): | ||
checks = json.loads(checks) | ||
logger(checks) | ||
logger("to_xarray called") | ||
bytes = bytearray(Uint8Array.new(array_buffer)) | ||
logger("got bytes") | ||
ex_bytes = io.BytesIO(bytes) | ||
try: | ||
ex = read_exchange(ex_bytes, checks=checks) | ||
logger("success! makeing a netCDF file") | ||
except ValueError as er: | ||
display(Pre("".join(traceback.format_exception(er))), target="log", append=True) | ||
display(er.error_data, target="log", append=True) | ||
raise # this is so the promise rejects and the main thread knows what's up | ||
return to_netcdf(ex), ex.cchdo.gen_fname() | ||
|
||
|
||
__export__ = ["to_xarray", "versions"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import xarray as xr | ||
from js import Uint8Array | ||
from pyscript import display | ||
|
||
from cchdo.hydro import accessors # noqa: F401 | ||
|
||
|
||
def logger(msg): | ||
display(msg, target="log", append=True) | ||
|
||
|
||
def load_netcdf(array_buffer): | ||
with open("out.nc", "wb") as f: | ||
f.write(bytearray(Uint8Array.new(array_buffer))) | ||
return xr.load_dataset("out.nc") | ||
|
||
|
||
def make_derived(array_buffer, type): | ||
logger(type) | ||
xr = load_netcdf(array_buffer) | ||
if type == "to_sum": | ||
return xr.cchdo.to_sum(), "summary.txt" | ||
if type == "to_woce": | ||
return xr.cchdo.to_woce(), xr.cchdo.gen_fname("woce") | ||
if type == "to_coards": | ||
return xr.cchdo.to_coards(), xr.cchdo.gen_fname("coards") | ||
|
||
|
||
__export__ = [ | ||
"make_derived", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */ | ||
/*! mini-coi - Andrea Giammarchi and contributors, licensed under MIT */ | ||
(({ document: d, navigator: { serviceWorker: s } }) => { | ||
if (d) { | ||
const { currentScript: c } = d; | ||
s.register(c.src, { scope: c.getAttribute('scope') || '.' }).then(r => { | ||
r.addEventListener('updatefound', () => location.reload()); | ||
if (r.active && !s.controller) location.reload(); | ||
}); | ||
} | ||
else { | ||
addEventListener('install', () => skipWaiting()); | ||
addEventListener('activate', e => e.waitUntil(clients.claim())); | ||
addEventListener('fetch', e => { | ||
const { request: r } = e; | ||
if (r.cache === 'only-if-cached' && r.mode !== 'same-origin') return; | ||
e.respondWith(fetch(r).then(r => { | ||
const { body, status, statusText } = r; | ||
if (!status || status > 399) return r; | ||
const h = new Headers(r.headers); | ||
h.set('Cross-Origin-Opener-Policy', 'same-origin'); | ||
h.set('Cross-Origin-Embedder-Policy', 'require-corp'); | ||
h.set('Cross-Origin-Resource-Policy', 'cross-origin'); | ||
return new Response(body, { status, statusText, headers: h }); | ||
})); | ||
}); | ||
} | ||
})(self); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters