getWeatherWindData.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const fs = require('fs');
  2. const path = require('path');
  3. const dayjs = require('dayjs');
  4. const { execSync } = require('child_process');
  5. const { v4: uuidv4 } = require('uuid');
  6. const { getSubdirectories, getFilesInDirectory } = require('./folder_reader.js');
  7. const { createWindData } = require("../../schema/createWindData.js");
  8. async function getLastNewModel(degree) {
  9. let gfsList = null;
  10. let hourList = null;
  11. gfsList = await getSubdirectories(path.join(__dirname, `../../model/grib2/${degree}`));
  12. if (gfsList?.length) {
  13. let dates = [];
  14. for (let i = 0; i < gfsList.length; i++) {
  15. const filePath = gfsList[i].split("\\");
  16. dates.push({
  17. fileName: filePath[filePath.length - 1],
  18. path: gfsList[i],
  19. date: Number(filePath[filePath.length - 1].split(".")[1])
  20. });
  21. }
  22. dates.sort((a, b) => {
  23. return b.date - a.date;
  24. });
  25. hourList = await getSubdirectories(path.join(__dirname, `../../model/grib2/${degree}/${dates[0].fileName}`));
  26. if (hourList?.length) {
  27. let hours = [];
  28. for (let i = 0; i < hourList.length; i++) {
  29. const filePath = hourList[i].split("\\");
  30. hours.push({
  31. fileName: filePath[filePath.length - 1],
  32. path: hourList[i],
  33. date: Number(filePath[filePath.length - 1])
  34. });
  35. }
  36. hours.sort((a, b) => {
  37. return b.date - a.date;
  38. });
  39. const fileList = await getFilesInDirectory(hours[0].path);
  40. // return global.downloadManager.manager.getStatus();
  41. return {
  42. utcData: convertUtcDateForChinaDate(hours[0].path.split("gfs.")[1].split("\\")[0], hours[0].date - 8),
  43. chinaData: convertUtcDateForChinaDate(hours[0].path.split("gfs.")[1].split("\\")[0], hours[0].date),
  44. ...hours[0],
  45. list: fileList?.length ? fileList : []
  46. };
  47. }
  48. return [];
  49. }
  50. return [];
  51. }
  52. // utc 时间转中国时间
  53. function convertUtcDateForChinaDate(day, hour) {
  54. return dayjs(`${day} ${hour}:00:00`).add(8, 'hour').format("YYYY-MM-DD HH:mm:ss");
  55. }
  56. exports.getWeatherWindData = async (req, res) => {
  57. const fileList = await getLastNewModel("gfs_1p00");
  58. const uniqueId = uuidv4();
  59. const uTempTxtFilePath = path.join(__dirname, `../../model/model_u_${uniqueId}.csv`);
  60. const vTempTxtFilePath = path.join(__dirname, `../../model/model_v_${uniqueId}.csv`);
  61. // const command = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[0]}" -match "TCDC:entire atmosphere" -text ${tempTxtFilePath}`;
  62. const uCommand = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[1]}" -match "UGRD:10 m above ground" -csv ${uTempTxtFilePath}`;
  63. const vCommand = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[1]}" -match "VGRD:10 m above ground" -csv ${vTempTxtFilePath}`;
  64. const u = execSync(uCommand);
  65. const v = execSync(vCommand);
  66. createWindData(uTempTxtFilePath, vTempTxtFilePath);
  67. // 将数据返回给前端
  68. res.rescc("成功", 200, { u, v });
  69. }