import base64 from urllib import response from quart import Blueprint from CONSTANT import DATA_BASEPATH import modules.balloon as balloon from quart import jsonify, request, send_file from modules.balloon.plot_once_backend import render_by_mode_single from modules.balloon.plot_year_backend import get_all_modes, render_based_on_mode from modules.balloon.utils import * BASE_PATH_BALLOON = DATA_BASEPATH.balloon all_year_data = pd.read_parquet(f"{BASE_PATH_BALLOON}/ballon_data_lin.parquet") def get_dataframe_between_year(start_year, end_year): res = all_year_data filtered_res = res[(res['file_name'].str.extract(r'LIN-(\d{4})')[0].astype(int) >= start_year) & (res['file_name'].str.extract(r'LIN-(\d{4})')[0].astype(int) <= end_year)] return filtered_res balloon_module = Blueprint("Balloon", __name__) @balloon_module.route('/') def home(): return jsonify(message="Welcome to the Flask balloon_module!") @balloon_module.route("/metadata/modes") def supermeta(): return jsonify({ "combos": comboType, "comboMode": comboMode, "date": comboDate }) @balloon_module.route("/metadata") def list_ballon(): all_ballon_files = glob.glob( f"{BASE_PATH_BALLOON}/**/**.nc", recursive=True) return all_ballon_files @balloon_module.route("/metadata/year") def list_ballon_year_modes(): return get_all_modes() @balloon_module.route("/metadata/stations") def list_stations(): return [] @balloon_module.route("/render/year") async def render_full_year(): # get start_year and end_year from query start_year = request.args.get('start_year') end_year = request.args.get('end_year') mode = request.args.get('mode') season = request.args.get('season') print(start_year, end_year) df = get_dataframe_between_year(int(start_year), int(end_year)) buff = render_based_on_mode(df, mode, season) return await send_file(buff, mimetype='image/png') @balloon_module.route("/render/single") async def render_single_path(): path = request.args.get('path') mode = request.args.get('mode') data = balloon.read_data(path) result = render_by_mode_single(data, mode) if result.has_data is False: # give a 204 response if no data is found return '', 204 img_str = base64.b64encode(result.image.getvalue()).decode("utf-8") response_data = { "image": img_str, "metadata": { "是否是地形波": "是" if result.is_terrain_wave else "否", } } return response_data