Skip to content

Commit

Permalink
Revert "Reapply "convert exchange checker to use web workers""
Browse files Browse the repository at this point in the history
This causes an infinite loop on github pages as well...

This reverts commit b2d0599.
  • Loading branch information
DocOtak committed Dec 14, 2024
1 parent b2d0599 commit 52e09d5
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 206 deletions.
202 changes: 128 additions & 74 deletions docs/_exchange_checker_include.html
Original file line number Diff line number Diff line change
@@ -1,102 +1,151 @@
<link rel="stylesheet" href="https://pyscript.net/releases/2024.11.1/core.css">
<script type="module" src="https://pyscript.net/releases/2024.11.1/core.js"></script>
<style>
#log-container{
max-height: 50vh;
overflow: scroll;
display: flex;
flex-direction: column-reverse;
}
</style>

<p>
cchdo.hydro version: <span id="hydro_version"></span><br />
cchdo.params version: <span id="params_version"></span>
</p>
<p>
<label>Add an exchange file (csv or zip) <input type="file" id="ex_file" name="ex_file" disabled></label>
<label>Add an exchange file (csv or zip) <input type="file" id="ex_file" name="ex_file"></label>
<h4>Options</h4>
<label><input id="checks_flags" type="checkbox" checked> Check Flags</label>
<p>
<button class="sd-sphinx-override sd-btn sd-text-wrap sd-btn-primary reference internal" id="process_exchange" py-click="_process_exchange" enabled="False">Process Exchange</button>
<button class="sd-sphinx-override sd-btn sd-text-wrap sd-btn-primary reference internal" id="process_exchange" py-click="_process_exchange">Process Exchange</button>
</p>
<p>
<div id='output'>
<span id="status">Loading Python Runtime</span>
<span id="status"></span>
<br />
</div>
<h4>Log Console</h4>
<div>
<div id="log-container">
<code id='log'>
</code>
</div>
</div>
<script type="py" src="./_static/convert_exchange.py" service-worker="./_static/sw.js" worker name="my-worker" config='{"packages": ["xarray", "cchdo.hydro<=1.0.2.8", "netcdf4"]}'></script>
<script type="py" src="./_static/derrived_makers.py" service-worker="./_static/sw.js" worker name="derived1" config='{"packages": ["xarray", "cchdo.hydro<=1.0.2.8", "netcdf4" ]}'></script>
<script type="py" src="./_static/derrived_makers.py" service-worker="./_static/sw.js" worker name="derived2" config='{"packages": ["xarray", "cchdo.hydro<=1.0.2.8", "netcdf4" ]}'></script>
<script type="py" src="./_static/derrived_makers.py" service-worker="./_static/sw.js" worker name="derived3" config='{"packages": ["xarray", "cchdo.hydro<=1.0.2.8", "netcdf4" ]}'></script>
<script>
addEventListener('py:ready', () => console.log("done"));
</script>
<script type="py">
from pyscript import workers, display
from js import document, console, window, Uint8Array, Blob
import json

def logger(msg):
display(msg, target="log", append=True)

logger("Waiting for Python to be ready")
my_worker = await workers["my-worker"]
my_worker.sync.logger = logger
logger("Python ready")
status = document.querySelector("#status")

versions = await my_worker.versions()
document.querySelector("#hydro_version").innerText = versions.hydro_version
document.querySelector("#params_version").innerText = versions.params_version

document.querySelector("#process_exchange").disabled = False
document.querySelector("#ex_file").disabled = False
status.innerText = "Ready..."

def _handle_success(result):
nc_bytes, nc_fname = result
nc_blob = Blob.new([Uint8Array.new(nc_bytes)], {type : 'application/octet-stream'})
nc_url = window.URL.createObjectURL(nc_blob)
nc_download_link = document.createElement("a")
nc_download_link.href = nc_url
nc_download_link.download = nc_fname
nc_download_link.innerText = f"Downlaod {nc_fname}"
output = document.querySelector("#output")
output.appendChild(nc_download_link)
output.appendChild(document.createElement("br"))
document.querySelector("#process_exchange").disabled = False
import asyncio
import io
import traceback

status = document.querySelector("#status")
status.innerText = f""
return nc_bytes
from cchdo.hydro import read_exchange
from cchdo.hydro import accessors

from cchdo.hydro import __version__ as hydro_version
from cchdo.params import __version__ as params_version

document.querySelector("#hydro_version").innerText = hydro_version
document.querySelector("#params_version").innerText = params_version

async def _make_derived(nc_bytes):
worker1 = await workers["derived1"]
worker2 = await workers["derived2"]
worker3 = await workers["derived3"]
import logging
import sys

worker1.make_derived(nc_bytes, "to_sum").then(_handle_success)
worker2.make_derived(nc_bytes, "to_woce").then(_handle_success)
# TODO make this not need scipy
#worker3.make_derived(nc_bytes, "to_coards").then(_handle_success)
root = logging.getLogger()
root.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)

def _handle_submit(arr_buffer):
async def load_ex_bytes(ex_bytes, checks):
return read_exchange(ex_bytes, checks=checks)

async def to_netcdf(ex):
ex.to_netcdf("out.nc")
with open("out.nc", "rb") as f:
return f.read()

async def to_coards(ex):
return ex.cchdo.to_coards()

async def to_woce(ex):
return ex.cchdo.to_woce()

async def to_xarray_callback(arg):
bytes = bytearray(Uint8Array.new(arg))
ex_bytes = io.BytesIO(bytes)
status = document.querySelector("#status")
check_flags = document.querySelector("#checks_flags").checked
checks = json.dumps({"flags": check_flags})
# cannot actually pass objects it seems, so stringiffy it
return my_worker.to_xarray(arr_buffer, checks)
checks = {
"flags": check_flags
}
try:
ex = await load_ex_bytes(ex_bytes, checks=checks)
except ValueError as er:
traceback.print_exception(er)
status.innerText = f"Failure see traceback..."
document.querySelector("#process_exchange").disabled = False
return
status.innerText = f"Success, generating files"
await asyncio.sleep(0.1)
try:
nc = await to_netcdf(ex)
nc_blob = Blob.new([Uint8Array.new(nc)], {type : 'application/netcdf'})
nc_url = window.URL.createObjectURL(nc_blob)
nc_download_link = document.createElement("a")
nc_download_link.href = nc_url
nc_fname = ex.cchdo.gen_fname()
nc_download_link.download = nc_fname
nc_download_link.innerText = f"Download CF/netCDF: {nc_fname}"
output = document.querySelector("#output")
output.appendChild(nc_download_link)
output.appendChild(document.createElement("br"))

except Exception as er:
status.innerText = f"Could not generate CF/netCDF"

await asyncio.sleep(0.1)
#status.element.innerText = f"Generating COARDS netCDF (very slow)"
#await asyncio.sleep(0.1)
#try:
# coards = await to_coards(ex)
# coards_blob = Blob.new([Uint8Array.new(coards)], {type : 'application/octet-stream'})
# coards_url = window.URL.createObjectURL(coards_blob)
# coards_download_link = document.createElement("a")
# coards_download_link.href = coards_url
# coards_download_link.download = "coards_nc.zip"
# coards_download_link.innerText = "Download COARDS netcdf zip"
# output = Element("output")
# output.element.appendChild(coards_download_link)
# output.element.appendChild(document.createElement("br"))
#except Exception as ex:
# print(ex)
# status.element.innerText = f"Could not generate COARDS netCDF"

await asyncio.sleep(0.1)
status.innerText = f"Generating WOCE Files"
await asyncio.sleep(0.1)
try:
woce = await to_woce(ex)
woce_blob = Blob.new([Uint8Array.new(woce)], {type : 'application/octet-stream'})
woce_url = window.URL.createObjectURL(woce_blob)
woce_download_link = document.createElement("a")
woce_download_link.href = woce_url
woce_download_link.download = "woce_output.txt"
woce_download_link.innerText = "Download Woce (might be txt or zip)"
output = document.querySelector("#output")
output.appendChild(woce_download_link)
output.appendChild(document.createElement("br"))
except:
status.innerText = f"Could not generate WOCE"

await asyncio.sleep(0.1)
try:
summary = ex.cchdo.to_sum()
summary_blob = Blob.new([Uint8Array.new(summary)], {type : 'application/octet-stream'})
summary_url = window.URL.createObjectURL(summary_blob)
summary_download_link = document.createElement("a")
summary_download_link.href = summary_url
summary_download_link.download = "woce_sum.txt"
summary_download_link.innerText = "Download Woce Sumfile"
output = document.querySelector("#output")
output.appendChild(summary_download_link)
output.appendChild(document.createElement("br"))
except:
status.innerText = f"Could not generate WOCE"

def _handle_fail(something):
status = document.querySelector("#status")
status.innerText = f"Failure see traceback..."
document.querySelector("#process_exchange").disabled = False
status.innerText = "File Generation Complete"

def _process_exchange(*args):
document.querySelector("#process_exchange").disabled = True
Expand All @@ -106,9 +155,14 @@ <h4>Log Console</h4>
file_list = document.querySelector("#ex_file").files
first_item = file_list.item(0)

first_item.arrayBuffer().then(_handle_submit, _handle_fail).then(_handle_success).then(_make_derived).catch(_handle_fail)
first_item.arrayBuffer().then(to_xarray_callback)
except:
status.innerText = "Error, was a file picked?"
document.querySelector("#process_exchange").disabled = False

</script>
</script>
<h4>Python Log Console</h4>
<py-terminal true></py-terminal>
<py-config type="toml">
packages = ["xarray", "cchdo.hydro<=1.0.2.8", "netcdf4"]
</py-config>
72 changes: 0 additions & 72 deletions docs/_static/convert_exchange.py

This file was deleted.

31 changes: 0 additions & 31 deletions docs/_static/derrived_makers.py

This file was deleted.

28 changes: 0 additions & 28 deletions docs/_static/sw.js

This file was deleted.

2 changes: 1 addition & 1 deletion docs/exchange_checker.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ It will also output the other legacy formats at CCHDO: WOCE, the COARDS netCDF f
This converter is only available in the html/browser versions of the documentation.

:::{note}
Processing a CTD file can take a long time.
Processing a CTD file can take a long time and I don't yet know how to show progress in the browser.
:::

:::{warning}
Expand Down

0 comments on commit 52e09d5

Please sign in to comment.