using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Threading;
namespace ServerV1._1
{
public partial class Form1 : Form
{
public static string exeDir = "C:\\calpuff\\exe"; // exe文件的路径
public static string inpDir = "C:\\calpuff\\inp";
public static string wrfoutDir = "C:\\calpuff\\wrfout";
// Server Database
public static string dbIp = "127.0.0.1";
public static string dbName = "mysql";
public static string dbUser = "root";
public static string dbPasswd = "1234";
public static string outputDir = "C:\\calpuff\\output";
// ftp 相关操作
public static string ftpIp="120.25.206.223";
public static string ftpUser="finalformat";
public static string ftpPasswd="finalformat";
// 与头文件相关
public static double[] maxLstValue = new double [1000];// #MaxLimit# 就当最多有1000种类污染物
public static int calpuffDaySpan = 0;
public static string finalFormatDir = "C:\\calpuff\\finalFormat";
// 头文件
public static string[] headFile = new string[1000];
// 与calpuff 相关
public static string srcDir = "C:\\calpuff\\src";
// public static string logs = "";
public static string taskRecords = "";
// 取出来的数据用来更新
public static int id;
public static string modelname = "";
public static string startdate = "";
public static string enddate = "";
public static string xrefkm = "";
public static string yrefkm = "";
public static string nx = "";
public static string ny = "";
public static string dgridkm = "";
public static string identifier = "";
public static string gtopo30 = "";
public static string glazas = "";
public static byte[] srcfile;
// 重新定义每一个exePath... 又刷新一遍逻辑
public static string terrelExePath = "C:\\calpuff\\exe\\terrel.exe";
public static string ctgprocExePath = "C:\\calpuff\\exe\\ctgproc.exe";
public static string makegeoExePath = "C:\\calpuff\\exe\\makegeo.exe";
public static string calwrfExePath = "C:\\calpuff\\exe\\calwrf.exe";
public static string calmetExePath = "C:\\calpuff\\exe\\calmet.exe";
public static string calpuffExePath = "C:\\calpuff\\exe\\calpuff.exe";
public static string terrelInpPath = "C:\\calpuff\\output\\terrel.inp";
public static string ctgprocInpPath = "C:\\calpuff\\output\\ctgproc.inp";
public static string makegeoInpPath = "C:\\calpuff\\output\\makegeo.inp";
public static string calwrfInpPath = "C:\\calpuff\\output\\calwrf.inp";
public static string calmetInpPath = "C:\\calpuff\\output\\calmet.inp";
public static string calpuffInpPath = "C:\\calpuff\\output\\calpuff.inp";
// 两个函数保证static的参数和本地的ini文件始终保持一致
public static void loadIni()
{
string iniPath="C:\\calpuff\\ini\\calpuff.ini";
if(!File.Exists(iniPath))
{
MessageBox.Show("calpuf.ini is missing ,please place it as 'C:\\calpuff\\ini\\calpuff.ini'");
return;
}
// 先获取最大行~
int lineCnt = 0;
FileStream fs = new FileStream(iniPath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string tempStr="";
while (tempStr != null)
{
tempStr = sr.ReadLine();
lineCnt++;
}
lineCnt--; // 有一位位移
string [] ini = new string[lineCnt + 100];
fs = new FileStream(iniPath, FileMode.Open, FileAccess.Read);
sr = new StreamReader(fs);
int index = 0;
tempStr = "";
while (tempStr != null)
{
tempStr = sr.ReadLine();
ini[index++] = tempStr;
}
index--; // 此时index就是行数
// 这样做其实很麻烦的 完全可以一次性就申请多一点 但是既然现成的代码也有 就这样写吧
string [] key =new string [index];
string [] value = new string [index];
string [] keyAndValue=new string [100];// 2就够了
for(int i=0;i<index;i++)
{
string curStr=ini[i];
int wordsCnt = 0;
infoExtract(keyAndValue, ref wordsCnt, curStr);
key[i] = keyAndValue[0];
value[i] = keyAndValue[1];
}
// 有点枚举 但是数据规模比较小
for(int i=0;i<index;i++)
{
string curKey=key[i];
string curValue=value[i];
if (curKey == "dbIp") dbIp = curValue;
else if (curKey == "dbName") dbName = curValue;
else if (curKey == "dbUser") dbUser = curValue;
else if (curKey == "dbPasswd") dbPasswd = curValue;
else if (curKey == "ftpIp") ftpIp = curValue;
else if (curKey == "ftpUser") ftpUser = curValue;
else if (curKey == "ftpPasswd") ftpPasswd = curValue;
else if (curKey == "inpDir") inpDir = curValue;
else if (curKey == "exeDir") exeDir = curValue;
else if (curKey == "wrfoutDir") wrfoutDir = curValue;
else if (curKey == "outputDir") outputDir = curValue;
else if (curKey == "srcDir") srcDir = curValue;
}
sr.Close();
fs.Close();
// V1.2.1 labelTxt也要更新啊
}
public static void saveIni()
{
// 写文件
string iniPath="C:\\calpuff\\ini\\calpuff.ini";
if (!File.Exists(iniPath))
{
MessageBox.Show("calpuf.ini is missing ,please place it as 'C:\\calpuff\\ini\\calpuff.ini'");
return;
}
FileStream fs = new FileStream(iniPath, FileMode.Open, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("dbIp" + " " + dbIp);
sw.WriteLine("dbName" + " " + dbName);
sw.WriteLine("dbUser" + " " + dbUser);
sw.WriteLine("dbPasswd" + " " + dbPasswd);
sw.WriteLine("ftpIp" + " " + ftpIp);
sw.WriteLine("ftpUser" + " " + ftpUser);
sw.WriteLine("ftpPasswd" + " " + ftpPasswd);
sw.WriteLine("inpDir" + " " + inpDir);
sw.WriteLine("exeDir" + " " + exeDir);
sw.WriteLine("wrfoutDir" + " " + wrfoutDir);
sw.WriteLine("outputDir" + " " + outputDir);
sw.WriteLine("srcDir" + " " + srcDir);
sw.Close();
fs.Close();
}
// 构造函数
public Form1()
{
InitializeComponent();
Point point = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Size.Width / 2, Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Size.Height / 2);//窗体位置
this.StartPosition = FormStartPosition.Manual;
this.Location = point;
// 这个时候就要loadIni
// 本地配置文件
loadIni();
// load以后 注意更新labelTxt
label5.Text = exeDir;
label6.Text = inpDir;
label7.Text = wrfoutDir;
label8.Text = outputDir;
label9.Text = srcDir;
// 等待标示
pictureBox1.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
Form form = new GeoFile();
Point point = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - form.Size.Width / 2, Screen.PrimaryScreen.WorkingArea.Height / 2 - form.Size.Height / 2);//窗体位置
form.StartPosition = FormStartPosition.Manual;
form.Location = point;
form.ShowDialog();
}
// File Test 检查所有文件是否存在
private void button2_Click(object sender, EventArgs e)
{
// -------------------------------------------------- test only----------------
string terrelInpPath = inpDir + "\\terrel.inp";
string ctgprocInpPath = inpDir + "\\ctgproc.inp";
string makegeoInpPath = inpDir + "\\makegeo.inp";
string calwrfInpPath = inpDir + "\\calwrf.inp";
string calmetInpPath = inpDir + "\\calmet.inp";
string calpuffInpPath = inpDir + "\\calpuff.inp";
string terrelExePath = exeDir + "\\terrel.exe";
string ctgprocExePath = exeDir + "\\ctgproc.exe";
string makegeoExePath = exeDir + "\\makegeo.exe";
string calwrfExePath = exeDir + "\\calwrf.exe";
string calmetExePath = exeDir + "\\calmet.exe";
string calpuffExePath = exeDir + "\\calpuffl.exe"; // note 'l'
if (!File.Exists(terrelInpPath))
{
MessageBox.Show("Error:terrel.inp does not exist in current directory");
return;
}
if (!File.Exists(ctgprocInpPath))
{
MessageBox.Show("Error:ctgproc.inp does not exist in current directory");
return;
}
if (!File.Exists(makegeoInpPath))
{
MessageBox.Show("Error:makegeo.inp does not exist in current directory");
return;
}
if (!File.Exists(calwrfInpPath))
{
MessageBox.Show("Error:calwrf.inp does not exist in current directory");
return;
}
if (!File.Exists(calmetInpPath))
{
MessageBox.Show("Error:calmet.inp does not exist in current directory");
return;
}
if (!File.Exists(calpuffInpPath))
{
MessageBox.Show("Error:calpuff.inp does not exist in current directory");
return;
}
// exe
if (!File.Exists(terrelExePath))
{
MessageBox.Show("Error:terrel.exe does not exist in current directory");
return;
}
if (!File.Exists(ctgprocExePath))
{
MessageBox.Show("Error:ctgproc.exe does not exist in current directory");
return;
}
if (!File.Exists(makegeoExePath))
{
MessageBox.Show("Error:makegeo.exe does not exist in current directory");
return;
}
if (!File.Exists(calwrfExePath))
{
MessageBox.Show("Error:calwrf.exe does not exist in current directory");
return;
}
if (!File.Exists(calmetExePath))
{
MessageBox.Show("Error:calmet.exe does not exist in current directory");
return;
}
if (!File.Exists(calpuffExePath))
{
MessageBox.Show("Error:calpuffl.exe does not exist in current directory");
return;
}
// dll 文件检查
string dllPath1=exeDir+"\\libgcc_s_dw2-1.dll";
string dllPath2=exeDir+"\\libgfortran-3.dll";
string dllPath3=exeDir+"\\libnetcdf-0.dll";
string dllPath4=exeDir+"\\libquadmath-0.dll";
if (!File.Exists(dllPath1))
{
MessageBox.Show("Error:libgcc_s_dw2-1.dll should exist in exe directory");
return;
}
if (!File.Exists(dllPath2))
{
MessageBox.Show("Error:libgfortran-3.dll should exist in current directory");
return;
}
if (!File.Exists(dllPath3))
{
MessageBox.Show("Error:libnetcdf-0.dll does not exist in current directory");
return;
}
if (!File.Exists(dllPath4))
{
MessageBox.Show("Error:libquadmath-0.dll should exist in exe directory");
return;
}
MessageBox.Show("All The Necessary Files Are Available");
}
// Server Conf
private void button7_Click(object sender, EventArgs e)
{
Form form = new ServerConf();
Point point = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - form.Size.Width / 2, Screen.PrimaryScreen.WorkingArea.Height / 2 - form.Size.Height / 2);//窗体位置
form.StartPosition = FormStartPosition.Manual;
form.Location = point;
form.ShowDialog();
}
//设置exe目录
private void button3_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
folderBrowser.ShowDialog();
if (folderBrowser.SelectedPath != "")
{
exeDir = folderBrowser.SelectedPath;
label5.Text = exeDir;
// 存储到本地配置
saveIni();
}
}
//设置inpDir
private void button4_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
folderBrowser.ShowDialog();
if (folderBrowser.SelectedPath != "")
{
inpDir = folderBrowser.SelectedPath;
label6.Text = inpDir;
// 存储到本地配置
saveIni();
}
}
// 设置wrfout 目录
private void button5_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
folderBrowser.ShowDialog();
if (folderBrowser.SelectedPath != "")
{
wrfoutDir = folderBrowser.SelectedPath;
label7.Text = wrfoutDir;
// 存储到本地配置
saveIni();
}
}
// output 目录选择
private void button6_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
folderBrowser.ShowDialog();
if (folderBrowser.SelectedPath != "")
{
outputDir = folderBrowser.SelectedPath;
label8.Text = outputDir;
// 存储到本地配置
saveIni();
}
}
// Load Tasks
private void button8_Click(object sender, EventArgs e)
{
this.loadTasks();
}
// Start Running---------mannual
private void button9_Click(object sender, EventArgs e)
{
// 六个模型一起Run
MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
// 到时候换成一个公网IP
string connStr = "server=" + Form1.dbIp + ";user id=" + Form1.dbUser + ";password=" + Form1.dbPasswd + ";database=" + Form1.dbName + ";pooling=false;charset=utf8";
conn.ConnectionString = connStr;
try
{
conn.Open(); // conn Open();
}
catch
{
MessageBox.Show("Database Connection Error");
}
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select id,modelname,startdate,enddate,xrefkm,yrefkm,nx,ny,dgridkm,gtopo30,glazas,identifier,srcfile from t_calpuf_conf where done=0";
System.Data.Common.DbDataReader reader = cmd.ExecuteReader();
if(!reader.HasRows)
{
MessageBox.Show("No Tasks To Be Dealt With");
return;
}
// 一个个的取出应用
while (reader.Read()) // 想一次处理完 Read() 也是个修改调用者的函数啊
{
id = reader.GetInt32(reader.GetOrdinal("id"));
identifier = reader.GetString(reader.GetOrdinal("identifier"));
modelname = reader.GetString(reader.GetOrdinal("modelname"));
if (modelname == "terrel")
{
// 需要再从数据库取出来
nx = reader.GetString(reader.GetOrdinal("nx"));
ny = reader.GetString(reader.GetOrdinal("ny"));
xrefkm = reader.GetString(reader.GetOrdinal("xrefkm"));
yrefkm = reader.GetString(reader.GetOrdinal("yrefkm"));
dgridkm = reader.GetString(reader.GetOrdinal("dgridkm"));
gtopo30 = reader.GetString(reader.GetOrdinal("gtopo30"));
runTerrel();
}
else if (modelname == "ctgproc")
{
nx = reader.GetString(reader.GetOrdinal("nx"));
ny = reader.GetString(reader.GetOrdinal("ny"));
xrefkm = reader.GetString(reader.GetOrdinal("xrefkm"));
yrefkm = reader.GetString(reader.GetOrdinal("yrefkm"));
dgridkm = reader.GetString(reader.GetOrdinal("dgridkm"));
glazas = reader.GetString(reader.GetOrdinal("glazas"));
runCtgproc();
}
else if(modelname=="makegeo")
{
nx = reader.GetString(reader.GetOrdinal("nx"));
ny = reader.GetString(reader.GetOrdinal("ny"));
xrefkm = reader.GetString(reader.GetOrdinal("xrefkm"));
yrefkm = reader.GetString(reader.GetOrdinal("yrefkm"));
dgridkm = reader.GetString(reader.GetOrdinal("dgridkm"));
gtopo30 = reader.GetString(reader.GetOrdinal("gtopo30"));
glazas = reader.GetString(reader.GetOrdinal("glazas"));
runMakegeo();
}
else if (modelname == "calwrf") // 依赖于wrfout 只需要起始日期即可
{
startdate = reader.GetString(reader.GetOrdinal("startdate"));
enddate = reader.GetString(reader.GetOrdinal("enddate"));
runCalwrf();
}
else if (modelname == "calmet")
{
nx = reader.GetString(reader.GetOrdinal("nx"));
ny = reader.GetString(reader.GetOrdinal("ny"));
xrefkm = reader.GetString(reader.GetOrdinal("xrefkm"));
yrefkm = reader.GetString(reader.GetOrdinal("yrefkm"));
dgridkm = reader.GetString(reader.GetOrdinal("dgridkm"));
// for calwrf
startdate = reader.GetString(reader.GetOrdinal("startdate"));
enddate = reader.GetString(reader.GetOrdinal("enddate"));
// for makegeo
gtopo30 = reader.GetString(reader.GetOrdinal("gtopo30"));
glazas = reader.GetString(reader.GetOrdinal("glazas"));
runCalmet();
}
else if (modelname == "calpuff")
{
nx = reader.GetString(reader.GetOrdinal("nx"));
ny = reader.GetString(reader.GetOrdinal("ny"));
xrefkm = reader.GetString(reader.GetOrdinal("xrefkm"));
yrefkm = reader.GetString(reade
calpuff-server-Form1.cs
最新推荐文章于 2024-03-22 08:30:34 发布