From d6c78fe61ae2d53090d07a2a222c0a5d83c37602 Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Wed, 3 Aug 2022 21:05:48 +0530 Subject: [PATCH 01/27] create the .spec file -->>"/dowload" --- server/main.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/server/main.py b/server/main.py index dd5ebb31..0afcca10 100644 --- a/server/main.py +++ b/server/main.py @@ -4,6 +4,7 @@ import radis from fastapi.middleware.cors import CORSMiddleware from pydantic.typing import Literal +import datetime # for high resolution radis.config["GRIDPOINTS_PER_LINEWIDTH_WARN_THRESHOLD"] = 7 app = FastAPI() @@ -94,3 +95,40 @@ async def calculate_spectrum(payload: Payload): "units": spectrum.units[payload.mode], }, } + + +@app.get("/download") +async def download(payload: 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, + ) + if payload.use_simulate_slit is True: + spectrum.apply_slit(payload.simulate_slit, "nm") + + 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: + file_name = datetime.datetime.now() + s.store(f'radis{file_name}.spec', compress=True, if_exists_then='replace') + file_path = f'radis{file_name}.spec' + return FileResponse(file_path, media_type='application/octet-stream', filename=f'{file_name}.txt') From 8cf0a5b66611407717e5878c7d1bde009e78ef3a Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Wed, 3 Aug 2022 21:11:12 +0530 Subject: [PATCH 02/27] correct variable name --- server/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/main.py b/server/main.py index 0afcca10..c0bc2081 100644 --- a/server/main.py +++ b/server/main.py @@ -129,6 +129,6 @@ async def download(payload: Payload): return {"error": str(exc)} else: file_name = datetime.datetime.now() - s.store(f'radis{file_name}.spec', compress=True, if_exists_then='replace') + spectrum.store(f'radis{file_name}.spec', compress=True, if_exists_then='replace') file_path = f'radis{file_name}.spec' return FileResponse(file_path, media_type='application/octet-stream', filename=f'{file_name}.txt') From 1f965afcc4adc25219d1fc12a170177116377d97 Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Mon, 8 Aug 2022 00:50:54 +0530 Subject: [PATCH 03/27] adding download .spec files as as background task --- server/main.py | 81 ++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 45 deletions(-) diff --git a/server/main.py b/server/main.py index 4dfc9ba6..a73c67fd 100644 --- a/server/main.py +++ b/server/main.py @@ -1,10 +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 -import datetime + # for high resolution radis.config["GRIDPOINTS_PER_LINEWIDTH_WARN_THRESHOLD"] = 7 app = FastAPI() @@ -38,33 +39,41 @@ class Payload(BaseModel): use_simulate_slit: bool = False +def calculate_spectrum(): + global spectrum + 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(): + + + @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() if payload.use_simulate_slit is True: spectrum.apply_slit(payload.simulate_slit, "nm") - + background_taks.add_task() except radis.misc.warning.EmptyDatabaseError: return {"error": "No line in the specified wavenumber range"} except Exception as exc: @@ -95,33 +104,15 @@ async def calculate_spectrum(payload: Payload): "units": spectrum.units[payload.mode], }, } +# =====================================================================# @app.get("/download") async def download(payload: 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() if payload.use_simulate_slit is True: spectrum.apply_slit(payload.simulate_slit, "nm") - except radis.misc.warning.EmptyDatabaseError: return {"error": "No line in the specified wavenumber range"} except Exception as exc: From 07282213f76a233516fb6fd075925c0d77aa6952 Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Mon, 8 Aug 2022 00:54:20 +0530 Subject: [PATCH 04/27] adding the .spec to logic to `download_spec func` --- server/main.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/server/main.py b/server/main.py index a73c67fd..12b58b43 100644 --- a/server/main.py +++ b/server/main.py @@ -60,7 +60,11 @@ def calculate_spectrum(): databank=payload.database, use_cached=True, ) -# def download_spec(): +def download_spec(): + file_name = datetime.datetime.now() + spectrum.store(f'radis{file_name}.spec', compress=True, if_exists_then='replace') + file_path = f'radis{file_name}.spec' + return FileResponse(file_path, media_type='application/octet-stream', filename=f'{file_name}.txt') @@ -119,7 +123,4 @@ async def download(payload: Payload): print("Error", exc) return {"error": str(exc)} else: - file_name = datetime.datetime.now() - spectrum.store(f'radis{file_name}.spec', compress=True, if_exists_then='replace') - file_path = f'radis{file_name}.spec' - return FileResponse(file_path, media_type='application/octet-stream', filename=f'{file_name}.txt') + download_spec() From 9b74cab57948a81e6e3a649cb4e9b5742411b49d Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Mon, 8 Aug 2022 01:02:30 +0530 Subject: [PATCH 05/27] removing unnecessary code --- server/main.py | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/server/main.py b/server/main.py index 12b58b43..6621a04a 100644 --- a/server/main.py +++ b/server/main.py @@ -60,35 +60,34 @@ def calculate_spectrum(): databank=payload.database, use_cached=True, ) -def download_spec(): - file_name = datetime.datetime.now() - spectrum.store(f'radis{file_name}.spec', compress=True, if_exists_then='replace') - file_path = f'radis{file_name}.spec' - return FileResponse(file_path, media_type='application/octet-stream', filename=f'{file_name}.txt') +def download_spec(): + file_name = datetime.datetime.now() + spectrum.store(f'radis{file_name}.spec', compress=True, if_exists_then='replace') + file_path = f'radis{file_name}.spec' + return FileResponse(file_path, media_type='application/octet-stream', filename=f'{file_name}.txt') + @app.post("/calculate-spectrum") async def calculate_spectrum(payload: Payload, background_taks: BackgroundTasks): print(payload) - try: - calculate_spectrum() if payload.use_simulate_slit is True: spectrum.apply_slit(payload.simulate_slit, "nm") - background_taks.add_task() + # 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: @@ -100,27 +99,21 @@ async def calculate_spectrum(payload: Payload, background_taks: BackgroundTasks) # 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(payload: Payload): try: - calculate_spectrum() - if payload.use_simulate_slit is True: - spectrum.apply_slit(payload.simulate_slit, "nm") + 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: - download_spec() From 80eefd12e73c5385d210dde45ce97c96cb9a3682 Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Mon, 8 Aug 2022 21:58:07 +0530 Subject: [PATCH 06/27] fixing up the function --- server/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/main.py b/server/main.py index 6621a04a..b3b0f374 100644 --- a/server/main.py +++ b/server/main.py @@ -39,7 +39,7 @@ class Payload(BaseModel): use_simulate_slit: bool = False -def calculate_spectrum(): +def calculate_spectrum(min_wavenumber_range:str,max_wavenumber_range:str,): global spectrum spectrum = radis.calc_spectrum( payload.min_wavenumber_range, From cdac922de879633f2eefe9242eb233b8b5f0c54d Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Tue, 9 Aug 2022 02:05:36 +0530 Subject: [PATCH 07/27] sperate the `generate and return file function ` --- server/main.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/server/main.py b/server/main.py index b3b0f374..35f96986 100644 --- a/server/main.py +++ b/server/main.py @@ -5,7 +5,7 @@ from pydantic import BaseModel 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() @@ -33,13 +33,13 @@ 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(min_wavenumber_range:str,max_wavenumber_range:str,): +def calculate_spectrum(payload): global spectrum spectrum = radis.calc_spectrum( payload.min_wavenumber_range, @@ -60,20 +60,21 @@ def calculate_spectrum(min_wavenumber_range:str,max_wavenumber_range:str,): databank=payload.database, use_cached=True, ) - - -def download_spec(): - file_name = datetime.datetime.now() - spectrum.store(f'radis{file_name}.spec', compress=True, if_exists_then='replace') - file_path = f'radis{file_name}.spec' - return FileResponse(file_path, media_type='application/octet-stream', filename=f'{file_name}.txt') +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, background_taks: BackgroundTasks): print(payload) try: - calculate_spectrum() + 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 @@ -109,11 +110,13 @@ async def calculate_spectrum(payload: Payload, background_taks: BackgroundTasks) @app.get("/download") -async def download(payload: Payload): +async def download(): try: - download_spec() + 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" \ No newline at end of file From 237986a58459438a9fc2f287ac9b288935e989f9 Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Sat, 13 Aug 2022 18:29:50 +0530 Subject: [PATCH 08/27] modified --- frontend/src/components/CalcSpectrum.tsx | 15 +++++++++++++++ frontend/src/components/fields/DownloadButton.tsx | 13 +++++++++++++ server/main.py | 5 ++--- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 frontend/src/components/fields/DownloadButton.tsx diff --git a/frontend/src/components/CalcSpectrum.tsx b/frontend/src/components/CalcSpectrum.tsx index 8b799f66..7c5ae076 100644 --- a/frontend/src/components/CalcSpectrum.tsx +++ b/frontend/src/components/CalcSpectrum.tsx @@ -20,6 +20,7 @@ import { Species } from "./fields/Species/Species"; import { SimulateSlit } from "./fields/SimulateSlit"; import { WavenumberRangeSlider } from "./fields/WavenumberRangeSlider"; import { CalcSpectrumButton } from "./fields/CalSpectrumButtom"; +import { DownloadSpectrum } from "./fields/DownloadButton"; import { CalcSpectrumPlot } from "./CalcSpectrumPlot"; import { ErrorAlert } from "./ErrorAlert"; @@ -166,6 +167,19 @@ export const CalcSpectrum: React.FC = () => { setLoading(false); //setLoading(false) is called after the response is received }); }; + //testing endpoint + const apiEndpoint = "http://localhost:5000/"; + const downloadSpectrum = async (): Promise<> => { + const fileResponse = axios + .get(`${apiEndpoint}download-spectrum`) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.log(error); + }); + }; + const databaseWatch = watch("database"); const modeWatch = watch("mode"); @@ -315,6 +329,7 @@ export const CalcSpectrum: React.FC = () => { + diff --git a/frontend/src/components/fields/DownloadButton.tsx b/frontend/src/components/fields/DownloadButton.tsx new file mode 100644 index 00000000..8401edf3 --- /dev/null +++ b/frontend/src/components/fields/DownloadButton.tsx @@ -0,0 +1,13 @@ +import React from "react"; +import Button from "@mui/material/Button"; + +export const DownloadSpectrum: React.FC = () => ( + +); diff --git a/server/main.py b/server/main.py index 35f96986..f748684e 100644 --- a/server/main.py +++ b/server/main.py @@ -40,7 +40,7 @@ class Payload(BaseModel): def calculate_spectrum(payload): - global spectrum + # global spectrum spectrum = radis.calc_spectrum( payload.min_wavenumber_range, payload.max_wavenumber_range, @@ -60,7 +60,6 @@ def calculate_spectrum(payload): 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' @@ -79,7 +78,7 @@ async def calculate_spectrum(payload: Payload, background_taks: BackgroundTasks) 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()) + background_taks.add_task(download_spec_file()) except radis.misc.warning.EmptyDatabaseError: return {"error": "No line in the specified wavenumber range"} except Exception as exc: From 8a4ca30b6bf28032ffacb36c290d25dfad601213 Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Sun, 14 Aug 2022 21:23:21 +0530 Subject: [PATCH 09/27] =?UTF-8?q?server=20setup=20complete=20=F0=9F=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/main.py | 132 ++++++++++++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 52 deletions(-) diff --git a/server/main.py b/server/main.py index f748684e..753018eb 100644 --- a/server/main.py +++ b/server/main.py @@ -1,7 +1,7 @@ -import radis import datetime +import radis from typing import List, Optional -from fastapi import BackgroundTasks, FastAPI +from fastapi import FastAPI from pydantic import BaseModel from fastapi.middleware.cors import CORSMiddleware from pydantic.typing import Literal @@ -10,6 +10,7 @@ radis.config["GRIDPOINTS_PER_LINEWIDTH_WARN_THRESHOLD"] = 7 app = FastAPI() + app.add_middleware( CORSMiddleware, allow_origins=["*"], @@ -17,13 +18,12 @@ allow_methods=["*"], allow_headers=["*"], ) - - +@app.get("/") +def read_root(): + return {"message": " Hello World"} class Species(BaseModel): molecule: str mole_fraction: float - - class Payload(BaseModel): min_wavenumber_range: float max_wavenumber_range: float @@ -33,61 +33,76 @@ class Payload(BaseModel): tvib: Optional[float] = None trot: Optional[float] = None path_length: float - simulate_slit: Optional[int] - mode: Literal["absorbance", "transmittance_noslit", "radiance_noslit", "transmittance", "radiance"] + simulate_slit: int + use_simulate_slit:bool + mode: Literal["absorbance", "transmittance_noslit", "radiance_noslit"] database: Literal["hitran", "geisa"] - use_simulate_slit: bool = False - def calculate_spectrum(payload): - # global spectrum + print(payload) 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, - ) - 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') + 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, + ) + return spectrum + + @app.post("/calculate-spectrum") -async def calculate_spectrum(payload: Payload, background_taks: BackgroundTasks): +async def cal_spectrum(payload: Payload): print(payload) + try: - calculate_spectrum(payload) + 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, + ) if payload.use_simulate_slit is True: + print("Applying simulate slit") 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_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: + 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: @@ -99,23 +114,36 @@ async def calculate_spectrum(payload: Payload, background_taks: BackgroundTasks) # 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(): +#[Date] _[database] _[molecule]_[temperature]K_[pressure]atm +@app.get("/download_spectrum") +async def download_spec(payload: Payload): + print(payload) + date = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S"); + file_name = f"create_spectrum/{date}_{payload.database}_{payload.tgas}k_{payload.pressure}atm.spec" + # file_path=f"create_spectrum/{file_name}" + print(f".spec file is created here{file_name}") + try: - return_spec_file() + spectrum = calculate_spectrum(payload) + + if payload.use_simulate_slit is True: + print("Applying simulate slit") + spectrum.apply_slit(payload.simulate_slit, "nm") 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" \ No newline at end of file + spectrum.store(file_name, compress=True, if_exists_then='replace') + return FileResponse(file_name, media_type='application/octet-stream', filename=file_name) + From 873446b648dfa9a4e0db74fd4dce5ea2b9d8e517 Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Sun, 14 Aug 2022 21:23:56 +0530 Subject: [PATCH 10/27] frontend setup complete --- frontend/src/components/CalcSpectrum.tsx | 69 +++++++++++++++---- .../components/fields/CalSpectrumButtom.tsx | 1 + .../src/components/fields/DownloadButton.tsx | 13 ---- 3 files changed, 55 insertions(+), 28 deletions(-) delete mode 100644 frontend/src/components/fields/DownloadButton.tsx diff --git a/frontend/src/components/CalcSpectrum.tsx b/frontend/src/components/CalcSpectrum.tsx index 7c5ae076..107505fd 100644 --- a/frontend/src/components/CalcSpectrum.tsx +++ b/frontend/src/components/CalcSpectrum.tsx @@ -7,6 +7,7 @@ import { Controller, useForm } from "react-hook-form"; import Switch from "@mui/material/Switch"; import FormControlLabel from "@mui/material/FormControlLabel"; import CircularProgress from "@mui/material/CircularProgress"; +import Button from "@mui/material/Button"; import { CalcSpectrumPlotData, CalcSpectrumResponseData } from "../constants"; import { FormValues } from "./types"; import { Database } from "./fields/Database"; @@ -20,7 +21,6 @@ import { Species } from "./fields/Species/Species"; import { SimulateSlit } from "./fields/SimulateSlit"; import { WavenumberRangeSlider } from "./fields/WavenumberRangeSlider"; import { CalcSpectrumButton } from "./fields/CalSpectrumButtom"; -import { DownloadSpectrum } from "./fields/DownloadButton"; import { CalcSpectrumPlot } from "./CalcSpectrumPlot"; import { ErrorAlert } from "./ErrorAlert"; @@ -42,6 +42,7 @@ export const CalcSpectrum: React.FC = () => { const [useGesia, setUseGesia] = useState(false); const [useSlit, setUseSlit] = useState(false); // checking that user wants to apply the slit function or not in available modes const [useSimulateSlitFunction, setUseSimulateSlitFunction] = useState(false); // checking the mode and enable or disable slit feature + const [downloadbutton, setdownloadbutton] = useState(false); const Schema = yup.object().shape({ useNonEqi: yup.boolean(), use_simulate_slit: yup.boolean(), @@ -137,6 +138,7 @@ export const CalcSpectrum: React.FC = () => { } setLoading(true); + setdownloadbutton(true); console.log(data); setError(undefined); @@ -164,22 +166,42 @@ export const CalcSpectrum: React.FC = () => { setCalcSpectrumResponse(response); } } - setLoading(false); //setLoading(false) is called after the response is received + setLoading(false); + setdownloadbutton(false); }); }; - //testing endpoint - const apiEndpoint = "http://localhost:5000/"; - const downloadSpectrum = async (): Promise<> => { - const fileResponse = axios - .get(`${apiEndpoint}download-spectrum`) - .then((response) => { - console.log(response); - }) - .catch((error) => { - console.log(error); - }); + //download the .spec file + const downloadSpec = async (data: FormValues): Promise => { + if (useSlit == true) { + if (data.mode === "radiance_noslit") { + data.mode = "radiance"; + } + if (data.mode === "transmittance_noslit") { + data.mode = "transmittance"; + } + } + console.log(data); + setError(undefined); + import(/* webpackIgnore: true */ "./config.js").then(async (module) => { + const rawResponse = await axios.post( + module.apiEndpoint + `download_spectrum`, + data + ); + if ( + rawResponse.data.data === undefined && + rawResponse.data.error === undefined + ) { + handleBadResponse("Bad response from backend!"); + } else { + const response = await rawResponse.data; + if (response.error) { + handleBadResponse(response.error); + } else { + setCalcSpectrumResponse(response); + } + } + }); }; - const databaseWatch = watch("database"); const modeWatch = watch("mode"); @@ -201,6 +223,21 @@ export const CalcSpectrum: React.FC = () => { } }, [databaseWatch, modeWatch]); + //download button + const DownloadSpectrum: React.FC = () => ( + + ); const UseNonEquilibriumCalculations = () => ( { - + + + diff --git a/frontend/src/components/fields/CalSpectrumButtom.tsx b/frontend/src/components/fields/CalSpectrumButtom.tsx index 4e28bc50..d1edd348 100644 --- a/frontend/src/components/fields/CalSpectrumButtom.tsx +++ b/frontend/src/components/fields/CalSpectrumButtom.tsx @@ -3,6 +3,7 @@ import Button from "@mui/material/Button"; export const CalcSpectrumButton: React.FC = () => ( -); From 0352695ec6ce811735e493b23434917f1fc03569 Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Sun, 14 Aug 2022 21:30:44 +0530 Subject: [PATCH 11/27] remove the size from `CALCULATE SPECTRUM button` --- frontend/src/components/fields/CalSpectrumButtom.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/frontend/src/components/fields/CalSpectrumButtom.tsx b/frontend/src/components/fields/CalSpectrumButtom.tsx index d1edd348..4e28bc50 100644 --- a/frontend/src/components/fields/CalSpectrumButtom.tsx +++ b/frontend/src/components/fields/CalSpectrumButtom.tsx @@ -3,7 +3,6 @@ import Button from "@mui/material/Button"; export const CalcSpectrumButton: React.FC = () => ( ); + //equilibrium-switch const UseNonEquilibriumCalculations = () => ( { )} /> ); + //slit-switch const UseSimulateSlit = () => ( { /> ); return ( - onSubmit(data, `calculate-spectrum`))} - > - {error ? : null} - - - - - - - - - - - - - - - - + <> + setProgress(0)} + /> + onSubmit(data, `calculate-spectrum`))} + > + {error ? : null} + + + + + + + + + + + + - {isNonEquilibrium ? ( - <> - - - - - - - - ) : null} + + + - - - + {isNonEquilibrium ? ( + <> + + + + + + + + ) : null} - - - + + + - - - + + + - {useSimulateSlitFunction ? ( - + - ) : null} - {useSimulateSlitFunction ? ( - useSlit ? ( + {useSimulateSlitFunction ? ( + + + + ) : null} + + {useSimulateSlitFunction ? ( + useSlit ? ( + + + + ) : null + ) : null} + {useGesia ? null : ( - + - ) : null - ) : null} - {useGesia ? null : ( + )} + - + + + + - )} - - - - - - - - - {loading ? ( -
- -
- ) : ( - calcSpectrumResponse?.data && - plotData?.species && ( - - ) - )} + + {loading ? ( +
+ +
+ ) : ( + calcSpectrumResponse?.data && + plotData?.species && ( + + ) + )} +
-
- + + ); }; diff --git a/frontend/yarn.lock b/frontend/yarn.lock index d178445c..5c73a881 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -9055,6 +9055,11 @@ react-scripts@5.0.1: optionalDependencies: fsevents "^2.3.2" +react-top-loading-bar@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/react-top-loading-bar/-/react-top-loading-bar-2.1.0.tgz#61198174e734606056f64262da312c02414b6f36" + integrity sha512-07IPCC4fThfkH5PHm7d49s9UAq2rpy4RAyD+5gtwtBbBrwxkvcff7ZlbCgzrBXq8AvcGafDkpjcGdntJ4F0O9A== + react-transition-group@^4.4.2: version "4.4.2" resolved "https://registry.yarnpkg.com/react-transition-group/-/react-transition-group-4.4.2.tgz#8b59a56f09ced7b55cbd53c36768b922890d5470" diff --git a/server/main.py b/server/main.py index 59ed06ae..f809145b 100644 --- a/server/main.py +++ b/server/main.py @@ -1,5 +1,6 @@ -import radis import os +import radis +import numpy as np from typing import List, Optional from fastapi import BackgroundTasks, FastAPI from pydantic import BaseModel @@ -73,12 +74,16 @@ def calculate_spectrum(payload): # create the folder in server for better organization +DOWNLOADED_SPECFILES_DIRECTORY = "DOWNLOADED_SPECFILES" + + def create_download_directory(): - if os.path.exists("DOWNLOADED_SPECFILES"): + + if os.path.exists(DOWNLOADED_SPECFILES_DIRECTORY): print(" >> Folder already exists ") else: print(">> creating DOWNLOADED_SPECFILES folder") - os.mkdir("DOWNLOADED_SPECFILES") + os.mkdir(DOWNLOADED_SPECFILES_DIRECTORY) # delete the file after giving the file response back to the user @@ -112,8 +117,10 @@ async def calc_spectrum(payload: Payload): wunit = spectrum.get_waveunit() iunit = "default" - x, y = spectrum.get(payload.mode, wunit=wunit, Iunit=iunit) - + xNan, yNan = spectrum.get(payload.mode, wunit=wunit, Iunit=iunit) + # to remove the nan values from x and y + x = xNan[~np.isnan(xNan)] + y = yNan[~np.isnan(yNan)] # Reduce payload size threshold = 5e7 if len(spectrum) * 8 * 2 > threshold: @@ -142,9 +149,9 @@ async def download_spec(payload: Payload, background_tasks: BackgroundTasks): try: create_download_directory() spectrum = calculate_spectrum(payload) - file_name_spec= spectrum.get_name() + file_name_spec = spectrum.get_name() file_name = f"{file_name_spec}.spec" - file_path = f"DOWNLOADED_SPECFILES/{file_name}" + file_path = f"{DOWNLOADED_SPECFILES_DIRECTORY/file_name}" if payload.use_simulate_slit is True: print(" >> Applying simulate slit") spectrum.apply_slit(payload.simulate_slit, "nm") @@ -161,5 +168,3 @@ async def download_spec(payload: Payload, background_tasks: BackgroundTasks): return FileResponse( file_path, media_type="application/octet-stream", filename=file_name ) - - From d0116a4fdc22e69f47f9359a94d876ee36e4281e Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Fri, 19 Aug 2022 21:05:04 +0530 Subject: [PATCH 26/27] =?UTF-8?q?remove=20`submitcount`=F0=9F=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/CalcSpectrum.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frontend/src/components/CalcSpectrum.tsx b/frontend/src/components/CalcSpectrum.tsx index 8f44cdb8..5b220d61 100644 --- a/frontend/src/components/CalcSpectrum.tsx +++ b/frontend/src/components/CalcSpectrum.tsx @@ -124,7 +124,7 @@ export const CalcSpectrum: React.FC = () => { handleSubmit, setValue, watch, - formState: { isDirty, submitCount }, + formState: { isDirty }, } = useForm({ defaultValues: { species: [{ molecule: "CO", mole_fraction: 0.1 }] }, resolver: yupResolver(Schema), @@ -278,7 +278,6 @@ export const CalcSpectrum: React.FC = () => { }, [ databaseWatch, modeWatch, - submitCount, isDirty, tgasWatch, pressureWatch, From de597dd7606a2d82867f2232f7fe5de88ce8e925 Mon Sep 17 00:00:00 2001 From: Arunava Basu Date: Mon, 22 Aug 2022 18:05:08 +0530 Subject: [PATCH 27/27] fixing download button --- frontend/src/components/CalcSpectrum.tsx | 76 +++--------------------- 1 file changed, 7 insertions(+), 69 deletions(-) diff --git a/frontend/src/components/CalcSpectrum.tsx b/frontend/src/components/CalcSpectrum.tsx index 5b220d61..8ac7a58b 100644 --- a/frontend/src/components/CalcSpectrum.tsx +++ b/frontend/src/components/CalcSpectrum.tsx @@ -119,13 +119,7 @@ export const CalcSpectrum: React.FC = () => { .max(30, "Simulate slit must be less than 30"), }), }); - const { - control, - handleSubmit, - setValue, - watch, - formState: { isDirty }, - } = useForm({ + const { control, handleSubmit, setValue, watch } = useForm({ defaultValues: { species: [{ molecule: "CO", mole_fraction: 0.1 }] }, resolver: yupResolver(Schema), }); @@ -147,6 +141,7 @@ export const CalcSpectrum: React.FC = () => { data.mode = "transmittance"; } } + setDownloadButton(true); setLoading(true); setError(undefined); setPlotData({ @@ -155,6 +150,7 @@ export const CalcSpectrum: React.FC = () => { mode: data.mode, species: data.species, }); + import(/* webpackIgnore: true */ "./config.js").then(async (module) => { if (endpoint === "calculate-spectrum") { setProgress(30); @@ -172,6 +168,7 @@ export const CalcSpectrum: React.FC = () => { rawResponse.data.error === undefined ) { handleBadResponse("Bad response from backend!"); + setDownloadButton(true); } else { const response = await rawResponse.data; if (response.error) { @@ -179,12 +176,12 @@ export const CalcSpectrum: React.FC = () => { setDownloadButton(true); } else { setCalcSpectrumResponse(response); + setDownloadButton(false); } } setProgress(100); setLoading(false); - setDownloadButton(false); } if (endpoint === "download-spectrum") { @@ -223,15 +220,6 @@ export const CalcSpectrum: React.FC = () => { const databaseWatch = watch("database"); const modeWatch = watch("mode"); - const tgasWatch = watch("tgas"); - const pressureWatch = watch("pressure"); - const pathLengthWatch = watch("path_length"); - const tvibWathch = watch("tvib"); - const trotWathch = watch("trot"); - const slitWatch = watch("simulate_slit"); - const speciesWatch = watch("species"); - const minWaveRangeWatch = watch("min_wavenumber_range"); - const maxWaveRangeWatch = watch("max_wavenumber_range"); React.useEffect(() => { if (databaseWatch === "geisa") { setUseGesia(true); @@ -248,58 +236,8 @@ export const CalcSpectrum: React.FC = () => { } else { setValue("simulate_slit", 5); } - if (isDirty === true) { - setDownloadButton(true); - } - if (tgasWatch !== 300) { - setDownloadButton(true); - } - if (pressureWatch !== 1.01325) { - setDownloadButton(true); - } - if (pathLengthWatch !== 1) { - setDownloadButton(true); - } - if (tvibWathch === 300 || tvibWathch === undefined) { - setDownloadButton(true); - } - if (trotWathch === 300 || trotWathch === undefined) { - setDownloadButton(true); - } - if (slitWatch === 5 && slitWatch === undefined) { - setDownloadButton(true); - } - if (minWaveRangeWatch !== 1900) { - setDownloadButton(true); - } - if (maxWaveRangeWatch !== 2300) { - setDownloadButton(true); - } - }, [ - databaseWatch, - modeWatch, - isDirty, - tgasWatch, - pressureWatch, - pathLengthWatch, - tvibWathch, - trotWathch, - slitWatch, - minWaveRangeWatch, - maxWaveRangeWatch, - ]); - speciesWatch.map((_, index) => { - const moleFractionWatch = watch(`species.${index}.mole_fraction`); - const moleculeWatch = watch(`species.${index}.molecule`); - React.useEffect(() => { - if (moleFractionWatch !== 0.1) { - setDownloadButton(true); - } - if (moleculeWatch !== "CO" || moleculeWatch !== undefined) { - setDownloadButton(true); - } - }, [moleFractionWatch]); - }); + }, [databaseWatch, modeWatch]); + //downloadButton const DownloadSpectrum: React.FC = () => (