1 Star 0 Fork 10

michael sun/GeoFly

forked from Yonghe/GeoFlying 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
NetCDF_Form.cs 7.82 KB
一键复制 编辑 原始数据 按行查看 历史
Yonghe 提交于 2020-10-02 09:33 . first
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2016/11/13
* Time: 6:46
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using NetCDFDotnet;
using System.Collections.Generic;
namespace GeoFly
{
/// <summary>
/// Description of NetCDF_Form.
/// </summary>
public partial class NetCDF_Form : Form
{
NetCDFDotnet.NCFile file = null;
public NetCDF_Form()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void Button1Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK) {
file = new NCFile(dialog.FileName, OpenMode.read);
listBoxVars.Items.Clear();
comboBox_lat.Items.Clear();
comboBox_long.Items.Clear();
for (int i = 0; i < file.variables.Length; i++) {
NCVariable var = file.variables[i];
listBoxVars.Items.Add(var.name);
comboBox_lat.Items.Add(var.name);
comboBox_long.Items.Add(var.name);
if (var.name.Length > 2 && var.name.ToLower().Substring(0, 3) == "lat") {
comboBox_lat.Text = var.name;
this.latVar = var;
}
if (var.name.Length > 2 && var.name.ToLower().Substring(0, 3) == "lon") {
comboBox_long.Text = var.name;
this.lonVar = var;
}
}
button1.Enabled = false;
}
}
void Button3Click(object sender, EventArgs e)
{
if (file != null) {
file.Close();
button1.Enabled = true;
button3.Enabled = false;
listBoxVars.Items.Clear();
comboBox_lat.Items.Clear();
comboBox_long.Items.Clear();
}
}
void ListBoxVarsSelectedIndexChanged(object sender, EventArgs e)
{
NCVariable var = file.variables[listBoxVars.SelectedIndex];
richTextBox1.Text = var.name + ":" + var.nc_type + "\nDimensions:";
for (int i = 0; i < var.Dimensions.Length; i++) {
richTextBox1.Text += var.Dimensions[i].dimname + " ";
}
richTextBox1.Text += "\n";
for (int i = 0; i < var.Atributes.Count; i++) {
richTextBox1.Text += var.Atributes.Keys[i] + "=" + var.Atributes.Values[i].value + "\n";
}
}
void NetCDF_FormLoad(object sender, EventArgs e)
{
}
NCVariable latVar = null;
NCVariable lonVar = null;
SortedList<string,NCVariable> SelectedVars = new SortedList<string, NCVariable>();
List<int> levelIndice = new List<int>();
void ComboBox_longSelectedIndexChanged(object sender, EventArgs e)
{
lonVar = file.variables[comboBox_long.SelectedIndex];
}
void ComboBox_latSelectedIndexChanged(object sender, EventArgs e)
{
latVar = file.variables[comboBox_lat.SelectedIndex];
}
void Button2Click(object sender, EventArgs e)
{
object varname = listBoxVars.SelectedItem.ToString();
if (!listBox2.Items.Contains(varname)) {
listBox2.Items.Add(varname);
NCVariable var = null;
for (int i = 0; i < file.variables.Length; i++) {
if (varname.ToString() == file.variables[i].name) {
SelectedVars.Add(varname.ToString(), file.variables[i]);
levelIndice.Add(0);
}
}
}
}
NCVariable currentVar = null;
void ListBox2SelectedIndexChanged(object sender, EventArgs e)
{
currentVar = SelectedVars[listBox2.SelectedItem.ToString()];
if (currentVar.Dimensions.Length == 3) {
comboBox_Level.Enabled = false;
}
if (currentVar.Dimensions.Length == 4) {
comboBox_Level.Enabled = true;
NC_Dimension dim = currentVar.Dimensions[1];
for (int i = 0; i < dim.length; i++) {
comboBox_Level.Items.Add(i);
}
}
}
void Button4Click(object sender, EventArgs e)
{
SelectedVars.Clear();
levelIndice.Clear();
listBox2.Items.Clear();
}
void ComboBox_LevelSelectedIndexChanged(object sender, EventArgs e)
{
int v = comboBox_Level.SelectedIndex;
levelIndice[listBox2.SelectedIndex] = v;
}
void ComboBox5SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox_process.SelectedIndex == 0 || comboBox_process.SelectedIndex == 1) {
label_Parameters.Text = "参数表:起始经度,起始纬度,结束经度,结束纬度,分辨率";
}
}
void Button6Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK) {
textBox2.Text = dialog.SelectedPath;
}
}
void Button5Click(object sender, EventArgs e)
{
int levelIndex = comboBox_Level.SelectedIndex;
this.button5.Enabled = false;
//Time
for (int ti = 0; ti < file.dimensions[2].length; ti++) {
//Var
string[] param = this.textBox_Parameters.Text.Split(new char[]{ ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
float lat0 = Convert.ToSingle(param[1]);
float lon0 = Convert.ToSingle(param[0]);
float lat1 = Convert.ToSingle(param[3]);
float lon1 = Convert.ToSingle(param[2]);
float resol = Convert.ToSingle(param[4]);
int rows = (int)((lat1 - lat0) / resol);
int cols = (int)((lon1 - lon0) / resol);
float[] lats = new float[rows];
for (int ind = 0; ind < rows; ind++) {
lats[ind] = lat0 + ind * resol;
}
float[] lons = new float[cols];
for (int ind = 0; ind < cols; ind++) {
lons[ind] = lon0 + ind * resol;
}
int id = 0;
NetCDFDotnet.NC_LowAPI.nc_create(file.ToString() + "_" + ti.ToString("0000") + ".nc", 1, ref id);
label7.Text = "Processing " + ti;
Application.DoEvents();
int LonDimId = -1;
NC_LowAPI.nc_def_dim(id, "lon", cols, ref LonDimId);
int LatDimId = -1;
NC_LowAPI.nc_def_dim(id, "lat", rows, ref LatDimId);
int varId = -1;
int lonid = -1;
int latid = -1;
NC_LowAPI.nc_def_var(id, "lon", NC_Type.NC_FLOAT, 1, new int[]{ LonDimId }, ref lonid);
NC_LowAPI.nc_def_var(id, "lat", NC_Type.NC_FLOAT, 1, new int[]{ LatDimId }, ref latid);
for (int i = 0; i < listBox2.Items.Count; i++) {
string varName = listBox2.Items[0].ToString();
NC_LowAPI.nc_def_var(id, varName, NC_Type.NC_FLOAT, 2, new int[] {
LatDimId,
LonDimId
}, ref varId);
}
NC_LowAPI.nc_enddef(id);
for (int i = 0; i < listBox2.Items.Count; i++) {
string varName = listBox2.Items[0].ToString();
NCVariable ncvar = null;
for (int vi = 0; vi < file.variables.Length; vi++) {
if (file.variables[vi].name == varName) {
ncvar = file.variables[vi];
break;
}
}
NC_Data2d ncData = file.GetData2D(varName, "lon", "lat", ti, comboBox_Level.SelectedIndex);
double[,] data = ncData.Data;
//重采样
float[,] result = null;
if (comboBox_process.SelectedIndex == 0)
result = MatrixFuncs.ReGridBilinear(data, ncData.YCoord, ncData.XCoord, lats, lons);
else if (comboBox_process.SelectedIndex == 1)
result = MatrixFuncs.ReGridNearest(data, ncData.YCoord, ncData.XCoord, lats, lons);
else
result = MatrixFuncs.ReGridLambert(data, ncData.YCoord, ncData.XCoord, 10, 70, 6000, 600, 600, 35, 105, 30, 60);
float[] values = new float[rows * cols];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
values[row * cols + col] = (float)result[row, col];
}
}
NC_LowAPI.nc_put_var_float(id, varId, values);
NC_LowAPI.nc_put_var_float(id, latid, lats);
NC_LowAPI.nc_put_var_float(id, lonid, lons);
//ncData.Data
//创建磁盘文件nc
//写入磁盘
}
NC_LowAPI.nc_close(id);
}
this.button5.Enabled = true;
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/michael-sun123/geo-fly.git
git@gitee.com:michael-sun123/geo-fly.git
michael-sun123
geo-fly
GeoFly
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891