数据处理,读取每一天逐三小时asc文件、逐日asc文件,将数据上下(南北)颠倒过来

最近处理CMIP6数据发生了许多问题,由于python读取nc文件写完了之后存储成了南北颠倒的asc文件,这里需要再将文件的南北倒过来。
由于以前没怎么用C++自带的处理时间的函数,习惯了用自己写的get_nexttime函数,用string 类型的年月日时,获取增加3小时的年月日时,如果时获取一天以后的,则直接循环8次。当然也可以用C++自带的,本人太懒了。

以下代码为读取每一个逐日存储的asc文件,并将函数南北颠倒,输出成新的asc文件,以开始时间和结束时间循环批量处理:

#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <stdlib.h>
#include <math.h>
using namespace std;

template <class Type>
Type StringToNum(const string& str) {
	istringstream iss(str);
	Type num;
	iss >> num;
	return num;
}
template<typename T> string toString(const T& t)
{
	ostringstream oss;  //创建一个格式化输出流
	oss << t;             //把值传递如流中
	return oss.str();
}

void get_nexttime(string& current_year, string& current_mon, string& current_day, string& current_hour, string& next_y, string& next_m, string& next_d, string& next_h);

int readLayer_asc(char* filename, float* buffer, int row, int col)
{
	int i, j;
	ifstream infile(filename);
	if (!infile)
	{
		cout << "Cannot open infile " << filename << endl;
		return -1;
	}
	string str;
	for (i = 0; i < 6; i++)
		getline(infile, str);
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			infile >> buffer[i * col + j];
		}
	}
	infile.close();
	return 1;
}


const int data_row = 181;
const int data_col = 361;
float lon_ll = -180;
float lat_ll = -90;
float res = 1;


void reverse_arr(float* buffer, float* buffer_re, int row, int col);
int main()
{
	float* buffer = new float[data_row * data_col];
	float* buffer_re = new float[data_row * data_col];

	string year, month, day, hour;
	string year_next, month_next, day_next, hour_next;
	string year_n3h, month_n3h, day_n3h, hour_n3h;

	string year_start = "2050";
	string month_start = "01";
	string day_start = "01";
	string hour_start = "00";
	year = year_start;
	month = month_start;
	day = day_start;
	hour = hour_start;
	string time_start = year + month + day + hour;

	string year_end = "2100";
	string month_end = "01";
	string day_end = "01";
	string hour_end = "00";
	string time_now = time_start;
	string time_end = year_end + month_end + day_end + hour_end;

	string infile_path = "./INM-CM4-8_ssp585/prec_";
	string outfile_path = "./INM-CM4-8_ssp585_Process/prec_";


	int i, j;
	while (true)
	{
		string infile = infile_path + time_now + ".txt";
		string outfile = outfile_path + time_now + ".txt";
		int flag = readLayer_asc(const_cast<char*>(infile.c_str()), buffer, data_row, data_col);

		cout << "reverse" << endl;
		if (flag == 1)
		{
			reverse_arr(buffer, buffer_re, data_row, data_col);

			ofstream asc_data(const_cast<char*>(outfile.c_str()));
			if (!asc_data)
			{
				cout << " Cannot open asc file: " << outfile_path << endl;
				exit(-1);
			}
			asc_data << "ncols " << data_col << endl;
			asc_data << "nrows " << data_row << endl;
			asc_data << "xllcorner " << lon_ll << endl;
			asc_data << "yllcorner " << lat_ll << endl;
			asc_data << "cellsize " << res << endl;
			asc_data << "NODATA_value -9999" << endl;

			for (i = 0; i < data_row; i++)
			{
				for (j = 0; j < data_col; j++)
				{

					asc_data << setiosflags(ios::fixed) << setprecision(2) << buffer_re[i * data_col + j] << "\t";

				}
				asc_data << endl;
			}
			asc_data.close();
		}

		for (int i = 0; i < 8; i++)
		{
			get_nexttime(year, month, day, hour, year_next, month_next, day_next, hour_next);
			year = year_next;
			month = month_next;
			day = day_next;
			hour = hour_next;
		}
		time_now = year + month + day + hour;
		if (time_now == time_end)
			break;
	}
	
	delete []buffer;
	delete []buffer_re;

}

void reverse_arr(float* buffer, float* buffer_re, int row, int col)
{

	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			buffer_re[((row - 1) - i) * col + j] = buffer[i * col + j];
		}
	}

}

void get_nexttime(string& current_year, string& current_mon, string& current_day, string& current_hour, string& next_y, string& next_m, string& next_d, string& next_h)
{
	int next_hour, next_day, next_mon, next_year;
	int h1 = StringToNum<int>(current_hour) + 3;
	int m1 = StringToNum<int>(current_mon);
	if (h1 > 23)
	{
		next_hour = h1 - 24;
		if (StringToNum<int>(current_mon) == 12)
		{
			if (StringToNum<int>(current_day) == 31)
			{
				next_year = StringToNum<int>(current_year) + 1;
				next_mon = 1;
				next_day = 1;
			}
			else
			{
				next_year = StringToNum<int>(current_year);
				next_mon = StringToNum<int>(current_mon);
				next_day = StringToNum<int>(current_day) + 1;
			}
		}
		//如果不是12月,年不会有变化
		else
		{
			switch (m1)
			{
			case 1: {
				if (StringToNum<int>(current_day) == 31)
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 2;
					next_day = 1;
				}
				else
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 1;
					next_day = StringToNum<int>(current_day) + 1;
				}
				break;
			}
			case 2: {
				if ((StringToNum<int>(current_year) % 100 == 0) || ((StringToNum<int>(current_year) % 4 == 0) && (StringToNum<int>(current_year) % 100 != 0)))
				{//闰年
					if (StringToNum<int>(current_day) == 29)
					{
						next_year = StringToNum<int>(current_year);
						next_mon = 3;
						next_day = 1;
					}
					else
					{
						next_year = StringToNum<int>(current_year);
						next_mon = 2;
						next_day = StringToNum<int>(current_day) + 1;
					}
				}
				else
				{
					if (StringToNum<int>(current_day) == 28)
					{
						next_year = StringToNum<int>(current_year);
						next_mon = 3;
						next_day = 1;
					}
					else
					{
						next_year = StringToNum<int>(current_year);
						next_mon = 2;
						next_day = StringToNum<int>(current_day) + 1;
					}
				}
				break;
			}
			case 3: {
				if (StringToNum<int>(current_day) == 31)
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 4;
					next_day = 1;
				}
				else
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 3;
					next_day = StringToNum<int>(current_day) + 1;
				}
				break;
			}
			case 4: {
				if (StringToNum<int>(current_day) == 30)
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 5;
					next_day = 1;
				}
				else
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 4;
					next_day = StringToNum<int>(current_day) + 1;
				}
				break;
			}
			case 5: {
				if (StringToNum<int>(current_day) == 31)
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 6;
					next_day = 1;
				}
				else
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 5;
					next_day = StringToNum<int>(current_day) + 1;
				}
				break;
			}
			case 6: {
				if (StringToNum<int>(current_day) == 30)
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 7;
					next_day = 1;
				}
				else
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 6;
					next_day = StringToNum<int>(current_day) + 1;
				}
				break;
			}
			case 7: {
				if (StringToNum<int>(current_day) == 31)
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 8;
					next_day = 1;
				}
				else
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 7;
					next_day = StringToNum<int>(current_day) + 1;
				}
				break;
			}
			case 8: {
				if (StringToNum<int>(current_day) == 31)
				{

					next_year = StringToNum<int>(current_year);
					next_mon = 9;
					next_day = 1;
				}
				else
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 8;
					next_day = StringToNum<int>(current_day) + 1;

				}
				break;
			}
			case 9: {
				if (StringToNum<int>(current_day) == 30)
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 10;
					next_day = 1;
				}
				else
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 9;
					next_day = StringToNum<int>(current_day) + 1;
				}
				break;
			}
			case 10: {
				if (StringToNum<int>(current_day) == 31)
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 11;
					next_day = 1;
				}
				else
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 10;
					next_day = StringToNum<int>(current_day) + 1;
				}
				break;
			}
			case 11:
			{
				if (StringToNum<int>(current_day) == 30)
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 12;
					next_day = 1;
				}
				else
				{
					next_year = StringToNum<int>(current_year);
					next_mon = 11;
					next_day = StringToNum<int>(current_day) + 1;
				}
				break;
			}
			}
		}
		next_h = toString(next_hour);
		next_d = toString(next_day);
		next_m = toString(next_mon);
		next_y = toString(next_year);
	}
	else
	{//不会超过24小时				
		next_h = toString(h1);
		next_d = toString(current_day);
		next_m = toString(current_mon);
		next_y = toString(current_year);
	}
	if (StringToNum<int>(next_h) < 10)
		next_h = "0" + toString(StringToNum<int>(next_h));
	if (StringToNum<int>(next_d) < 10)
		next_d = "0" + toString(StringToNum<int>(next_d));
	if (StringToNum<int>(next_m) < 10)
		next_m = "0" + toString(StringToNum<int>(next_m));

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宇天y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值