Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature to download the spectrum file (.spec) #619

Merged
merged 30 commits into from
Aug 22, 2022
Merged
Changes from 9 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d6c78fe
create the .spec file -->>"/dowload"
arunavabasucom Aug 3, 2022
8cf0a5b
correct variable name
arunavabasucom Aug 3, 2022
da0a8e2
Merge branch 'main' into download_molecule
arunavabasucom Aug 4, 2022
1f965af
adding download .spec files as as background task
arunavabasucom Aug 7, 2022
0728221
adding the .spec to logic to `download_spec func`
arunavabasucom Aug 7, 2022
9b74cab
removing unnecessary code
arunavabasucom Aug 7, 2022
80eefd1
fixing up the function
arunavabasucom Aug 8, 2022
2d7e3ac
Merge branch 'main' into download_molecule
arunavabasucom Aug 8, 2022
cdac922
sperate the `generate and return file function `
arunavabasucom Aug 8, 2022
237986a
modified
arunavabasucom Aug 13, 2022
8a4ca30
server setup complete 😎
arunavabasucom Aug 14, 2022
873446b
frontend setup complete
arunavabasucom Aug 14, 2022
0352695
remove the size from `CALCULATE SPECTRUM button`
arunavabasucom Aug 14, 2022
3d56338
change the folder name
arunavabasucom Aug 14, 2022
f09d854
change the `http` method
arunavabasucom Aug 14, 2022
06ff1cc
correct the `endpoint` name
arunavabasucom Aug 14, 2022
fb2523f
fixed the `main.py`
arunavabasucom Aug 14, 2022
33e1419
make a common function for `submit and download`
arunavabasucom Aug 14, 2022
5955c2b
add delete script && fix naming of the hooks
arunavabasucom Aug 15, 2022
ef6e1de
use the specrate function in calculate-spectrum endpoint
arunavabasucom Aug 15, 2022
80aec4f
Merge branch 'main' into download_molecule
arunavabasucom Aug 15, 2022
e2f4437
remove emoji
arunavabasucom Aug 15, 2022
bd4dbb1
fix label and eqilibrium bug
arunavabasucom Aug 15, 2022
b51a09b
fix the naming conventions
arunavabasucom Aug 15, 2022
7eca8ec
fix the type of simulate_slit
arunavabasucom Aug 15, 2022
3bb2779
working on download
arunavabasucom Aug 16, 2022
486908c
fix the auto downloading
arunavabasucom Aug 17, 2022
6e437f1
done with download feature 😀 || remove nan values form json
arunavabasucom Aug 19, 2022
d0116a4
remove `submitcount`😎
arunavabasucom Aug 19, 2022
de597dd
fixing download button
arunavabasucom Aug 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 60 additions & 34 deletions server/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import radis
import datetime
from typing import List, Optional
from fastapi import FastAPI
from fastapi import BackgroundTasks, FastAPI
from pydantic import BaseModel
import radis
from fastapi.middleware.cors import CORSMiddleware
from pydantic.typing import Literal
from fastapi.responses import FileResponse
# for high resolution
radis.config["GRIDPOINTS_PER_LINEWIDTH_WARN_THRESHOLD"] = 7
app = FastAPI()
Expand Down Expand Up @@ -31,50 +33,62 @@ class Payload(BaseModel):
tvib: Optional[float] = None
trot: Optional[float] = None
path_length: float
simulate_slit: Optional[int] = None
simulate_slit: Optional[int]
mode: Literal["absorbance", "transmittance_noslit", "radiance_noslit", "transmittance", "radiance"]
database: Literal["hitran", "geisa"]
use_simulate_slit: bool = False


def calculate_spectrum(payload):
global spectrum
arunavabasucom marked this conversation as resolved.
Show resolved Hide resolved
spectrum = radis.calc_spectrum(
payload.min_wavenumber_range,
payload.max_wavenumber_range,
molecule=[species.molecule for species in payload.species],
mole_fraction={
species.molecule: species.mole_fraction for species in payload.species
},
# TODO: Hard-coding "1,2,3" as the isotopologue for the time-being
isotope={species.molecule: "1,2,3" for species in payload.species},
pressure=payload.pressure,
Tgas=payload.tgas,
Tvib=payload.tvib,
Trot=payload.trot,
path_length=payload.path_length,
export_lines=False,
wstep="auto",
databank=payload.database,
use_cached=True,
)
def download_spec_file():
file_name_notation = datetime.datetime.now()
global file_name
file_name = f'radis{file_name_notation}.spec'
spectrum.store(file_name, compress=True, if_exists_then='replace')
return "spectrum file is created successfully"
def return_spec_file():
return FileResponse(file_name, media_type='application/octet-stream', filename=f'{file_name}.txt')


@app.post("/calculate-spectrum")
async def calculate_spectrum(payload: Payload):
async def calculate_spectrum(payload: Payload, background_taks: BackgroundTasks):
print(payload)

try:
spectrum = radis.calc_spectrum(
payload.min_wavenumber_range,
payload.max_wavenumber_range,
molecule=[species.molecule for species in payload.species],
mole_fraction={
species.molecule: species.mole_fraction for species in payload.species
},
# TODO: Hard-coding "1,2,3" as the isotopologue for the time-being
isotope={species.molecule: "1,2,3" for species in payload.species},
pressure=payload.pressure,
Tgas=payload.tgas,
Tvib=payload.tvib,
Trot=payload.trot,
path_length=payload.path_length,
export_lines=False,
wstep="auto",
databank=payload.database,
use_cached=True,
)
calculate_spectrum(payload)
if payload.use_simulate_slit is True:
spectrum.apply_slit(payload.simulate_slit, "nm")

# downloading the molecule as a background task so that the retrieving
# x and y values are are not effected
background_taks.add_task(download_spec())
except radis.misc.warning.EmptyDatabaseError:
return {"error": "No line in the specified wavenumber range"}
except Exception as exc:
print("Error", exc)
return {"error": str(exc)}
else:

wunit = spectrum.get_waveunit()
iunit = "default"
x, y = spectrum.get(payload.mode, wunit=wunit, Iunit=iunit)

# Reduce payload size
threshold = 5e7
if len(spectrum) * 8 * 2 > threshold:
Expand All @@ -86,11 +100,23 @@ async def calculate_spectrum(payload: Payload):
# from the x min, max and step --> less data transfer. TODO )
resample = int(len(spectrum) * 8 * 2 // threshold)
x, y = x[::resample], y[::resample]
return {
"data": {
"x": list(x),
"y": list(y),
"units": spectrum.units[payload.mode],
},
}

return {
"data": {
"x": list(x),
"y": list(y),
"units": spectrum.units[payload.mode],
},
}

@app.get("/download")
async def download():
try:
return_spec_file()
except radis.misc.warning.EmptyDatabaseError:
return {"error": "No line in the specified wavenumber range"}
except Exception as exc:
print("Error", exc)
return {"error": str(exc)}
else:
return "file returned successfully"