/***************************************
*                                      *
*            外枠の読み込み            *
*                                      *
*                                      *
***************************************/

#include <iostream>
#include <cstdlib>
#include "snaps.h"
#include "kisosu.h"
#include "file_open.h"
#include "cntl.h"


void waku()
{
	// 変数宣言
	int nendo;
	int sotai_nendo;
	int nenrei;
	int seibetu;
	int shubetu;
	int waku_seido;
	int waku_seido_p;
	
	int counter;
	double buffer[BUFFER_MAX];
	int data_number;
	
	using namespace std;
	
	// 初期化
	for( shubetu = 0 ; shubetu <= MAX_SHUBETU - 1 ; shubetu++ )
	{
		for( nendo = SOTOWAKU_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
		{
			sotai_nendo = nendo - SOTOWAKU_SHONENDO;
			for( nenrei = MIN_HIHO_NENREI ; nenrei <= MAX_HIHO_NENREI ; nenrei++ )
			{
				Sotowaku[shubetu][sotai_nendo][nenrei - MIN_HIHO_NENREI] = 0.;
				Sotowaku_Jurai[shubetu][sotai_nendo][nenrei - MIN_HIHO_NENREI] = 0.;
			}
		}
	}
	
	// 外枠ファイルより読み込み
	for( waku_seido = 0 ; waku_seido <= 2 ; waku_seido++ )
	{
		for( counter = 1 ; counter <= 2 ; counter++ )
		{
			read_data( buffer , fp_sotowaku[waku_seido] , &data_number );
		}
		for( seibetu = 0 ; seibetu <= 2 ; seibetu++ )
		{
			for( nendo = SOTOWAKU_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
			{
				sotai_nendo = nendo - SOTOWAKU_SHONENDO;
				if( read_data( buffer , fp_sotowaku[waku_seido] , &data_number ) != EOF && buffer[1] == seibetu && buffer[2] == nendo )
				{
					if( seibetu >= 1 )
					{
						for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
						{
							Sotowaku[( seibetu - 1 ) * 3 + waku_seido + 1][sotai_nendo][nenrei + 11 - MIN_HIHO_NENREI] = buffer[nenrei];
						}
					}
				}
				else
				{
					cout << "外枠ファイル読込中にＥＯＦを検出しました" << endl;
					exit( 1 );
				}
			}
		}
	}
	
	// パート適用拡大
	for( waku_seido_p = 0 ; waku_seido_p <= 1 ; waku_seido_p++ )
	{
		for( counter = 1 ; counter <= 2 ; counter++ )
		{
			read_data( buffer , fp_sotowaku_part[waku_seido_p] , &data_number );
		}
		for( seibetu = 0 ; seibetu <= 2 ; seibetu++ )
		{
			for( nendo = SOTOWAKU_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
			{
				if( read_data( buffer , fp_sotowaku_part[waku_seido_p] , &data_number ) != EOF && buffer[1] == seibetu && buffer[2] == nendo )
				{
					if( seibetu >= 1 )
					{
						if( nendo == 2016 - 1 )
						{
							for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
							{
								Part_Taishou[( seibetu - 1 ) * 3 + waku_seido_p + 2][1][nenrei + 11 - MIN_HIHO_NENREI][0] = buffer[nenrei];
							}
						}
						else if( nendo == 2016 )
						{
							for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
							{
								Part_Taishou[( seibetu - 1 ) * 3 + waku_seido_p + 2][1][nenrei + 11 - MIN_HIHO_NENREI][1] = buffer[nenrei];
							}
						}
						else if( nendo == 2024 - 1 )
						{
							for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
							{
								Part_Taishou[( seibetu - 1 ) * 3 + waku_seido_p + 2][0][nenrei + 11 - MIN_HIHO_NENREI][0] = buffer[nenrei];
							}
						}
						else if( nendo == 2024 )
						{
							for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
							{
								Part_Taishou[( seibetu - 1 ) * 3 + waku_seido_p + 2][0][nenrei + 11 - MIN_HIHO_NENREI][1] = buffer[nenrei];
							}
						}
					}
				}
				else
				{
					cout << "パート適用拡大用外枠ファイルの読込中にＥＯＦを検出しました" << endl;
					exit( 1 );
				}
			}
		}
	}
	if( Option2 == 1 || Option2 == 2 )
	{
		// パート適用の更なる拡大
		for( waku_seido_p = 2 ; waku_seido_p <= 3 ; waku_seido_p++ )
		{
			for( counter = 1 ; counter <= 2 ; counter++ )
			{
				read_data( buffer , fp_sotowaku_part[waku_seido_p] , &data_number );
			}
			for( seibetu = 0 ; seibetu <= 2 ; seibetu++ )
			{
				for( nendo = SOTOWAKU_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
				{
					if( read_data( buffer , fp_sotowaku_part[waku_seido_p] , &data_number ) != EOF && buffer[1] == seibetu && buffer[2] == nendo )
					{
						if( seibetu >= 1 )
						{
							if( nendo == 2024 - 1 )
							{
								for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
								{
									Part_Taishou[( seibetu - 1 ) * 3 + waku_seido_p][2][nenrei + 11 - MIN_HIHO_NENREI][0] = buffer[nenrei];
								}
							}
							if( nendo == 2024 )
							{
								for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
								{
									Part_Taishou[( seibetu - 1 ) * 3 + waku_seido_p][2][nenrei + 11 - MIN_HIHO_NENREI][1] = buffer[nenrei];
								}
							}
						}
					}
					else
					{
						cout << "パート適の更なる拡大用外枠ファイルの読込中にＥＯＦを検出しました" << endl;
						exit( 1 );
					}
				}
			}
		}
		
		for( nenrei = MIN_HIHO_NENREI ; nenrei <= MAX_HIHO_NENREI ; nenrei++ )
		{
			for( counter = 0 ; counter <= 1 ; counter++ )
			{
				Part_Taishou[2][2][nenrei - MIN_HIHO_NENREI][counter] -= Part_Taishou[2][0][nenrei - MIN_HIHO_NENREI][counter];
				Part_Taishou[3][2][nenrei - MIN_HIHO_NENREI][counter] -= Part_Taishou[3][0][nenrei - MIN_HIHO_NENREI][counter];
				Part_Taishou[5][2][nenrei - MIN_HIHO_NENREI][counter] -= Part_Taishou[5][0][nenrei - MIN_HIHO_NENREI][counter];
				Part_Taishou[6][2][nenrei - MIN_HIHO_NENREI][counter] -= Part_Taishou[6][0][nenrei - MIN_HIHO_NENREI][counter];
			}
		}
	}
	
	if( Option3 == 1 )
	{
		for( waku_seido = 0 ; waku_seido <= 2 ; waku_seido++ )
		{
			for( counter = 1 ; counter <= 2 ; counter++ )
			{
				read_data( buffer , fp_sotowaku_jurai[waku_seido] , &data_number );
			}
			for( seibetu = 0 ; seibetu <= 2 ; seibetu++ )
			{
				for( nendo = SOTOWAKU_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
				{
					sotai_nendo = nendo - SOTOWAKU_SHONENDO;
					if( read_data( buffer , fp_sotowaku_jurai[waku_seido] , &data_number ) != EOF && buffer[1] == seibetu && buffer[2] == nendo )
					{
						if( seibetu >= 1 )
						{
							for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
							{
								Sotowaku_Jurai[( seibetu - 1 ) * 3 + waku_seido + 1][sotai_nendo][nenrei + 11 - MIN_HIHO_NENREI] = buffer[nenrei];
							}
						}
					}
					else
					{
						cout << "外枠ファイル読込中にＥＯＦを検出しました" << endl;
						exit( 1 );
					}
				}
			}
		}
		
		// パート適用拡大
		for( waku_seido_p = 0 ; waku_seido_p <= 1 ; waku_seido_p++ )
		{
			for( counter = 1 ; counter <= 2 ; counter++ )
			{
				read_data( buffer , fp_sotowaku_part_jurai[waku_seido_p] , &data_number );
			}
			for( seibetu = 0 ; seibetu <= 2 ; seibetu++ )
			{
				for( nendo = SOTOWAKU_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
				{
					if( read_data( buffer , fp_sotowaku_part_jurai[waku_seido_p] , &data_number ) != EOF && buffer[1] == seibetu && buffer[2] == nendo )
					{
						if( seibetu >= 1 )
						{
							if( nendo == 2016 - 1 )
							{
								for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
								{
									Part_Taishou_Jurai[( seibetu - 1 ) * 3 + waku_seido_p + 2][1][nenrei + 11 - MIN_HIHO_NENREI][0] = buffer[nenrei];
								}
							}
							else if( nendo == 2016 )
							{
								for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
								{
									Part_Taishou_Jurai[( seibetu - 1 ) * 3 + waku_seido_p + 2][1][nenrei + 11 - MIN_HIHO_NENREI][1] = buffer[nenrei];
								}
							}
							else if( nendo == 2024 - 1 )
							{
								for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
								{
									Part_Taishou_Jurai[( seibetu - 1 ) * 3 + waku_seido_p + 2][0][nenrei + 11 - MIN_HIHO_NENREI][0] = buffer[nenrei];
								}
							}
							else if( nendo == 2024 )
							{
								for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
								{
									Part_Taishou_Jurai[( seibetu - 1 ) * 3 + waku_seido_p + 2][0][nenrei + 11 - MIN_HIHO_NENREI][1] = buffer[nenrei];
								}
							}
						}
					}
					else
					{
						cout << "パート適用拡大用外枠ファイルの読込中にＥＯＦを検出しました" << endl;
						exit( 1 );
					}
				}
			}
		}
		if( Option2 == 1 || Option2 == 2 )
		{
			// パート適用再拡大
			for( waku_seido_p = 2 ; waku_seido_p <= 3 ; waku_seido_p++ )
			{
				for( counter = 1 ; counter <= 2 ; counter++ )
				{
					read_data( buffer , fp_sotowaku_part_jurai[waku_seido_p] , &data_number );
				}
				for( seibetu = 0 ; seibetu <= 2 ; seibetu++ )
				{
					for( nendo = SOTOWAKU_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
					{
						if( read_data( buffer , fp_sotowaku_part_jurai[waku_seido_p] , &data_number ) != EOF && buffer[1] == seibetu && buffer[2] == nendo )
						{
							if( seibetu >= 1 )
							{
								if( nendo == 2024 - 1 )
								{
									for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
									{
										Part_Taishou_Jurai[( seibetu - 1 ) * 3 + waku_seido_p][2][nenrei + 11 - MIN_HIHO_NENREI][0] = buffer[nenrei];
									}
								}
								if( nendo == 2024 )
								{
									for( nenrei = MIN_HIHO_NENREI - 11 ; nenrei <= MAX_HIHO_NENREI - 11 ; nenrei++ )
									{
										Part_Taishou_Jurai[( seibetu - 1 ) * 3 + waku_seido_p][2][nenrei + 11 - MIN_HIHO_NENREI][1] = buffer[nenrei];
									}
								}
							}
						}
						else
						{
							cout << "パート適用再拡大用外枠ファイルの読込中にＥＯＦを検出しました" << endl;
							exit( 1 );
						}
					}
				}
			}
			
			for( nenrei = MIN_HIHO_NENREI ; nenrei <= MAX_HIHO_NENREI ; nenrei++ )
			{
				for( counter = 0 ; counter <= 1 ; counter++ )
				{
					Part_Taishou_Jurai[2][2][nenrei - MIN_HIHO_NENREI][counter] -= Part_Taishou_Jurai[2][0][nenrei - MIN_HIHO_NENREI][counter];
					Part_Taishou_Jurai[3][2][nenrei - MIN_HIHO_NENREI][counter] -= Part_Taishou_Jurai[3][0][nenrei - MIN_HIHO_NENREI][counter];
					Part_Taishou_Jurai[5][2][nenrei - MIN_HIHO_NENREI][counter] -= Part_Taishou_Jurai[5][0][nenrei - MIN_HIHO_NENREI][counter];
					Part_Taishou_Jurai[6][2][nenrei - MIN_HIHO_NENREI][counter] -= Part_Taishou_Jurai[6][0][nenrei - MIN_HIHO_NENREI][counter];
				}
			}
		}
	}
}
