- 积分
- 676
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-8-20
- 最后登录
- 1970-1-1
|
发表于 2017-3-10 16:43:55
|
显示全部楼层
不喜欢用fortran,用C#写了文中第一个读取数据并写入二进制文件的程序,测试可以出图,请大家批评指正
namespace test
{
class Program
{
static void Main(string[] args)
{
string line;
string[] str;
char[] stid = new char[8];
int nlev = 1; int flag = 1;
float tim = 0; float lon = 0; float lat = 0; float rain = 0;
try
{
StreamReader sr = new StreamReader(@"D:\test\GrADS站点资料作图\raindata.txt");
FileStream fs = new FileStream(@"D:\test\GrADS站点资料作图\test.grd", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
sr.ReadLine(); sr.ReadLine(); //头两行为无用字符串
while ((line = sr.ReadLine()) != null)
{
str = Regex.Split(line, @"\s+"); //用正则表达式,通过空格分割字符串
for (int i = 0; i < 5;i++ )
stid[i] = str[1].ToCharArray()[i];
lat = float.Parse(str[3]);
lon = float.Parse(str[2]);
rain = float.Parse(str[5]);
Console.WriteLine(stid);
bw.Write(stid);
bw.Write(lat);
bw.Write(lon);
bw.Write(tim);
bw.Write(nlev);
bw.Write(flag);
bw.Write(rain);
}
nlev = 0;
bw.Write(stid);
bw.Write(lat);
bw.Write(lon);
bw.Write(tim);
bw.Write(nlev);
bw.Write(flag);
sr.Close();
fs.Close();
bw.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
//Console.ReadKey();
}
}
} |
|