Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@

This repository contains the back-end of the [Virtual FTIR Spectrometer](https://github.com/RastonLab/Virtual-FTIR-Spectrometer).

## [flask](https://github.com/RastonLab/Virtual-FTIR-Functions/tree/main/flask)
## [api](https://github.com/RastonLab/Virtual-FTIR-Functions/tree/main/api)

This directory holds the Flask web framework that the front-end reaches out to.
This directory contains

- `background.py`
- Contains a program that strictly generates a background sample. This program uses `data.json` for its input and is capable of graphing using `mathplotlib`.
- `fast_api.py`
- Contains a program that accepts API calls for both a background sample and spectrum. This program accepts JSON through a `fetch()` or `cURL` command and returns JSON of X and Y coordinates.
- `flask_api`
- Contains a program that accepts API calls for both a background sample and spectrum. This program accepts JSON through a `fetch()` or `cURL` command and returns JSON of X and Y coordinates.
- `functions.py`
- Contains functions used in all other files. Designed so edits only need to be made to one file for main functionality updates and changes.
- `spectrum.py`
- Contains a program that strictly generates a spectrum. This program uses `data.json` for its input and is capable of graphing using `mathplotlib`.

## [jupyter-notebook](https://github.com/RastonLab/Virtual-FTIR-Functions/tree/main/jupyter-notebook)

This directory holds a Jupyter Notebook that was used in the testing and development of the spectrum modification functions. These functions emulate the blackbody spectrum (sPlanck), the beamsplitter, the cell windows, and the detector response spectrum.

## [create-virtual-environments.sh](https://github.com/RastonLab/Virtual-FTIR-Functions/blob/main/create-virtual-environments.sh)

This script creates a virtual python environments (venv) needed to run the files in the `flask` and `jupyter-notebook` directories. The script uses `requirements.txt` to install the required dependencies to a venv that is used for both directories.
This script creates a virtual python environments (venv) needed to run the files in the `api` and `jupyter-notebook` directories. The script uses `requirements.txt` to install the required dependencies to a venv that is used for both directories.

To activate the virtual environment (venv), run `source venv/bin/activate` in the terminal. To deactivate the virtual enviornment (venv), run `deactivate` in the terminal.
To activate the virtual environment (venv), run `source venv/bin/activate` in the terminal. To deactivate the virtual environment (venv), run `deactivate` in the terminal.
2 changes: 1 addition & 1 deletion flask/README.md → api/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Virtual-FTIR-Functions

## flask
## api

### `background.py`

Expand Down
4 changes: 1 addition & 3 deletions flask/background.py → api/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

from functions import __param_check, __generate_background

bool


def main():
# read local data file into a dictionary
with open("data.json", "r") as data_json:
with open("../data.json", "r") as data_json:
data = json.load(data_json)

print(data)
Expand Down
File renamed without changes.
186 changes: 186 additions & 0 deletions api/fast_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# FastAPI server
# https://fastapi.tiangolo.com/
# uvicorn fast_api:app --reload

# https://realpython.com/fastapi-python-web-apis/
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Literal

from functions import __param_check, __generate_spectra, __generate_background

molecule_list = Literal[
"C2H2",
"C2H4",
"C2H6",
"C2N2",
"C4H2",
"CF4",
"CH3Br",
"CH3Cl",
"CH3CN",
"CH3OH",
"CH4",
"ClO",
"ClONO2",
"CO",
"CO2",
"COCl2",
"COF2",
"CS",
"H2",
"H2CO",
"H2O",
"H2O2",
"H2S",
"HBr",
"HC3N",
"HCl",
"HCN",
"HCOOH",
"HF",
"HI",
"HNO3",
"HO2",
"HOBr",
"HOCl",
"N2",
"N2O",
"NH3",
"NO",
"NO+",
"NO2",
"O",
"O2",
"O3",
"OCS",
"OH",
"PH3",
"SF6",
"SO2",
"SO3",
]


class Payload(BaseModel):
minWave: float
maxWave: float
molecule: molecule_list
pressure: float
resolution: float
numScan: int
zeroFill: Literal[0, 1, 2]
source: Literal[1700, 3100]
beamsplitter: Literal["AR_ZnSe", "AR_CaF2"]
cellWindow: Literal["ZnSe", "CaF2"]
detector: Literal["MCT", "InSb"]


app = FastAPI()
origins = [
"http://localhost:3000",
]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


@app.get("/")
async def root():
return "<h1>ftir</h1>"


@app.post("/fetch_background/")
async def create_item(payload: Payload):
data = {
"minWave": payload.minWave,
"maxWave": payload.maxWave,
"molecule": payload.molecule,
"pressure": payload.pressure,
"resolution": payload.resolution,
"numScan": payload.numScan,
"zeroFill": payload.zeroFill,
"source": payload.source,
"beamsplitter": payload.beamsplitter,
"cellWindow": payload.cellWindow,
"detector": payload.detector,
}

# verify the information in the dictionary
__param_check(data)

print("----- output verified params to console as self-check -----")
for key, value in data.items():
print(" %s: %s" % (key, value))

# perform: transmission spectrum of gas sample (calc_spectrum)
# --> blackbody spectrum of source (sPlanck)
# --> transmission spectrum of beamsplitter and cell windows
# --> detector response spectrum
print("----- start __generate_background() -----")
result = __generate_background(data)
print("----- end __generate_background() -----")

if result != False:
# convert dictionary values to strings and return as JSON
return {
"success": True,
"x": list(result.keys()),
"y": [str(flt) for flt in result.values()],
}
else:
return {
"success": False,
"text": "No line in the specified wavenumber range",
}


@app.post("/post_json/")
async def create_item(payload: Payload):
data = {
"minWave": payload.minWave,
"maxWave": payload.maxWave,
"molecule": payload.molecule,
"pressure": payload.pressure,
"resolution": payload.resolution,
"numScan": payload.numScan,
"zeroFill": payload.zeroFill,
"source": payload.source,
"beamsplitter": payload.beamsplitter,
"cellWindow": payload.cellWindow,
"detector": payload.detector,
}

# verify the information in the dictionary
__param_check(data)

print("----- output verified params to console as self-check -----")
for key, value in data.items():
print(" %s: %s" % (key, value))

# perform: transmission spectrum of gas sample (calc_spectrum)
# --> blackbody spectrum of source (sPlanck)
# --> transmission spectrum of beamsplitter and cell windows
# --> detector response spectrum
print("----- start __generate_spectra() -----")
result = __generate_spectra(data)
print("----- end __generate_spectra() -----")

if result != False:
# convert dictionary values to strings and return as JSON
return {
"success": True,
"x": list(result.keys()),
"y": [str(flt) for flt in result.values()],
}
else:
return {
"success": False,
"text": "No line in the specified wavenumber range",
}
4 changes: 4 additions & 0 deletions flask/flask.py → api/flask_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Flask server
# https://flask.palletsprojects.com/en/2.2.x/
# python3 flask_api.py

from flask import Flask, request
from flask_cors import CORS

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion flask/calculate.py → api/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def main():
# read local data file into a dictionary
with open("data.json", "r") as data_json:
with open("../data.json", "r") as data_json:
data = json.load(data_json)

print(data)
Expand Down
2 changes: 0 additions & 2 deletions create-virtual-environments.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ source venv/bin/activate

pip install -r requirements.txt

deactivate

echo "------ script finished ------"
56 changes: 56 additions & 0 deletions download_hitran.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# this script installs the HITRAN data for the following molecules locally
# the commented out molecules do not download properly but are included in the UI
from radis.io.hitran import fetch_hitran

for molecule in [
"C2H2",
"C2H4",
"C2H6",
"C2N2",
"C4H2",
# "CF4",
"CH3Br",
"CH3Cl",
"CH3CN",
"CH3OH",
"CH4",
"ClO",
# "ClONO2",
"CO",
"CO2",
"COCl2",
"COF2",
"CS",
"H2",
"H2CO",
"H2O",
"H2O2",
"H2S",
"HBr",
"HC3N",
"HCl",
"HCN",
"HCOOH",
"HF",
"HI",
"HNO3",
"HO2",
"HOBr",
"HOCl",
"N2",
"N2O",
"NH3",
"NO",
"NO+",
"NO2",
"O",
"O2",
"O3",
"OCS",
"OH",
"PH3",
# "SF6",
"SO2",
"SO3",
]:
fetch_hitran(molecule, parse_quanta=molecule in ["CO", "CO2"])
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ specutils
# used by flask
flask
flask-cors

# used by fast
fastapi

# formatting
black