格式为:四川省降水强度数据集
目的:主要练习提取降水强度,而创建随机的分钟降水文件。
处理:
雨量筒降水不需要,统一处理为666666。
无降水与缺测(标志2,3)增加出现概率,以符合正常情况,同时两种不同标志不能相邻同时出现。
private static void Main(string[] args)
{
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "R01.txt");
StringBuilder sb = new StringBuilder();
//创建开始与结束日期
DateTime startDay = new DateTime(2001, 1, 1, 20, 1, 0);//气象日,日界20时,20:01-20:00为一天
DateTime endDay = new DateTime(2001, 12, 31, 20, 0, 0);
DateTime curDay = startDay.Date;
Random r = new Random();
int pre = r.Next(100);//先产生一个,后面产生的比较与之不同
if (pre > 80) pre = 3;
else if (pre > 6) pre = 2;
int z;
while (curDay <= endDay.Date)
{
//设置一天的时间段 20:01-20:00
DateTime curTime = new DateTime(curDay.Year, curDay.Month, curDay.Day, 20, 1, 0);
DateTime endTime = curTime.AddDays(1).AddMinutes(-1);
sb.Append($"{curDay:yyyy MM dd} 1 666666 ");
while (curTime <= endTime)
{
do
{
z = r.Next(100);//种类
if (z > 80) z = 3;
else if (z > 6) z = 2;
} while (pre == z);
pre = z;//生成z 数据段的开始,=0,1正常,=2无降水,=3缺测,=5正常,=6翻斗式。
sb.Append(z + " ");
sb.Append(curTime.ToString("HHmm "));// 生成 t1t1t1t1
TimeSpan ts = endTime - curTime;//19:55-20:00 55-60共6分钟,但差值是5
int ts1 = (int)ts.TotalMinutes;//5
int len; //产生原剩余分钟数2倍的随机数 ,为的是尽快结束
if (z == 2 || z == 3) len = r.Next(2 * ts1) + 10;
else len = r.Next(ts1 / 2) + 10;
Console.WriteLine(curTime.ToString("yyyyMMdd hh:mm ") + z.ToString());
if (z == 2 || z == 3)//缺测无降水情况
{
if (len < ts1)//4<5
{
curTime = curTime.AddMinutes(len);
sb.Append(curTime.ToString("HHmm "));
}
else
{
sb.AppendLine(endTime.ToString("HHmm"));
break;
}
}
else
{
//19:55-20:00 ts1=5实际为6分钟
if (len < ts1)//3<5
{
curTime = curTime.AddMinutes(len);
sb.Append(curTime.ToString("HHmm "));
for (int i = 0; i < len; i++)
{
sb.Append(r.Next(1, 200).ToString("000 "));//0.01mm
}
}
else
{
sb.Append(endTime.ToString("HHmm "));
for (int i = 0; i < ts1 + 1; i++)//5:0-4 6:0-5=>55-60
{
sb.Append(r.Next(1, 200).ToString("000 "));
}
sb.Remove(sb.Length - 1, 1);//删除多余空格,添加回车
sb.AppendLine();
break;
}
}
}
curDay = curDay.AddDays(1);
}
File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8);
}
结果: