#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "set.h"
#include "glva.h"

void shus_fullout(int);  
void shus_nenbeout(int);



static double d4x[5][4][14][8][ENDY-STTY+1][116];  
static double d4a[5][4][14][8][ENDY-STTY+1];       
static double d65[5][4][14][8][ENDY-STTY+1];       
static double dkof[5][3][ENDY-STTY+1];            
static double ddaik[5][ENDY-STTY+1];


void shus_fullout(int mode){ 
	FILE *ofp;
	int s, i, j, k, x;
	int dx;
	double riv, bpre, bend, dtemp, dtemp2;
	int is, ie, ii, se;
	
	if(Seidn!=0){
		is=ie=0;
	} else {
		if(Cntlset==1) is=0;
		else           is=1;
		ie=4;
	}
	
	
	for(ii=is; ii<=ie; ii++){
		
		for(s=0; s<4; s++){
			for(i=0; i<14; i++){
				for(j=0; j<8; j++){
					for(k=0; k<ENDY-STTY+1; k++){
						for(x=0; x<116; x++){
							d4x[ii][s][i][j][k][x] = 0.;
						}
						d4a[ii][s][i][j][k] = 0.;
						d65[ii][s][i][j][k] = 0.;
					}
				}
			}
		}
		for(i=0; i<3; i++){
			for(k=0; k<ENDY-STTY+1; k++){
				dkof[ii][i][k] = 0.;
			}
		}
		
		if(Cntlset==1 && ii==0) continue;
		
		if(Seidn==1 || ii==1 ) se=3;
		else                   se=2;
		
		for(s=0; s<=se; s++){
			for(i=0; i<=13; i++){
				for(j=1; j<=7; j++){
					if(j==2) continue;
					for(k=Ks+1; k<=Ke; k++){
					for(x=XA-1; x<=XB; x++){ 
						dx = MAX(x, 67);
						if(j==1){       
							riv = Krb[k-ECSTY][dx-ECXA] * (Escutrrh[k-ECSTY][dx-ECXA]/Escutrrh[k-1-ECSTY][dx-1-ECXA]);
							bpre = D3bxtp[ii][s][i][j][k-1-STTY][x-1] * Escutrrh[k-1-ECSTY][dx-1-ECXA];
							bend = D3bxtp[ii][s][i][j][k-STTY][x] * Escutrrh[k-ECSTY][dx-ECXA];
						} else if (j!=5){ 
							riv = Kra[k-ECSTY][dx-ECXA] * (Escutrrt[k-ECSTY][dx-ECXA]/Escutrrt[k-1-ECSTY][dx-1-ECXA]);
							bpre = D3bxtp[ii][s][i][j][k-1-STTY][x-1] * Escutrrt[k-1-ECSTY][dx-1-ECXA];
							bend = D3bxtp[ii][s][i][j][k-STTY][x] * Escutrrt[k-ECSTY][dx-ECXA];
						} else {         
							riv = Kra[k-ECSTY][67-ECXA] * (Escutrrt[k-ECSTY][67-ECXA]/Escutrrt[k-1-ECSTY][67-ECXA]);
							bpre = D3bxtp[ii][s][i][j][k-1-STTY][x-1] * Escutrrt[k-1-ECSTY][67-ECXA];
							bend = D3bxtp[ii][s][i][j][k-STTY][x] * Escutrrt[k-ECSTY][67-ECXA];
						}
						if(Flg_Kaisho==1 && k==12) riv += 0.009;  
						if(Flg_Kaisho==2 && k==13) riv += 0.010;  
						
						dtemp = (bpre * 2.0 + bpre * riv * 6.0 + bend * 4.0) / 12.0 * 1.001 / 1.0E+8;
						
						d4x[ii][s][i][j][k-STTY][x]          = dtemp;
						d4x[ii][s][i][0][k-STTY][x]         += dtemp;
						if(j>2) d4x[ii][s][i][2][k-STTY][x] += dtemp;
						
						d4a[ii][s][i][j][k-STTY]            += dtemp;
						d4a[ii][s][i][0][k-STTY]            += dtemp;
						if(j>2) d4a[ii][s][i][2][k-STTY]    += dtemp;
						
						if(x>=65){
							d65[ii][s][i][j][k-STTY]          += dtemp;
							d65[ii][s][i][0][k-STTY]          += dtemp;
							if(j>2) d65[ii][s][i][2][k-STTY]  += dtemp;
						}
						
						if(Cntlset==1){
							if(ii>1 && k<Kyoskijun){
								dtemp = 0.;
							} else if(ii>1 && k==Kyoskijun){
								dtemp = (bpre * riv * 2. + bend * 4.) /12.0 * 1.001 / 1.0E+8;
							}
							d4x[0][s][i][j][k-STTY][x]         += dtemp;
							d4x[0][s][i][0][k-STTY][x]         += dtemp;
							if(j>2) d4x[0][s][i][2][k-STTY][x] += dtemp;
							
							d4a[0][s][i][j][k-STTY]            += dtemp;
							d4a[0][s][i][0][k-STTY]            += dtemp;
							if(j>2) d4a[0][s][i][2][k-STTY]    += dtemp;
							
							if(x>=65){
								d65[0][s][i][j][k-STTY]          += dtemp;
								d65[0][s][i][0][k-STTY]          += dtemp;
								if(j>2) d65[0][s][i][2][k-STTY]  += dtemp;
							}
						}
					}
					}
				}
			}
		}
		
		
		for(i=1; i<=2; i++){
			for(k=Ks+1; k<=Ke; k++){
				for(x=XA-1; x<=XB; x++){  
					dx = MAX(x, 67);
					if(i==1){
						riv = Kra[k-ECSTY][dx-ECXA] * (Escutrrt[k-ECSTY][dx-ECXA]/Escutrrt[k-1-ECSTY][dx-1-ECXA]);
						bpre = Kofte[ii][0][0][k-1-STTY][x-1] * Escutrrt[k-1-ECSTY][dx-1-ECXA];
						bend = Kofte[ii][0][0][k-STTY][x] * Escutrrt[k-ECSTY][dx-ECXA];
					} else {
						riv = Kra[k-ECSTY][dx-ECXA] * (Escutrrt[k-ECSTY][dx-ECXA]/Escutrrt[k-1-ECSTY][dx-1-ECXA]);
						bpre = Kofkk[ii][0][0][k-1-STTY][x-1] * Escutrrt[k-1-ECSTY][dx-1-ECXA];
						bend = Kofkk[ii][0][0][k-STTY][x] * Escutrrt[k-ECSTY][dx-ECXA];
					}
					if(Flg_Kaisho==1 && k==12) riv += 0.009; 
					if(Flg_Kaisho==2 && k==13) riv += 0.010;  
					dtemp = (bpre * 2.0 + bpre * riv * 6.0 + bend * 4.0) / 12.0 / 1.0E+8;
					
					dkof[ii][i][k-STTY] += dtemp;
					dkof[ii][0][k-STTY] += dtemp;
					
					if(Cntlset==1){
						if(ii>1 &&  k<Kyoskijun){
							dtemp =0.;
						} else if (ii>1 && k==Kyoskijun){
							dtemp = (bpre * riv * 2. + bend * 4.) / 12. / 1.0E+8;
						}
						dkof[0][i][k-STTY] += dtemp;
						dkof[0][0][k-STTY] += dtemp;
						
					}
				}
			}
		}
		
		if(mode==0){
			for(k=0; k<ENDY-STTY+1; k++){
				ddaik[ii][k] = 0.;
			}
			for(s=0; s<=se; s++){
				for(i=0; i<=8; i++){
					for(k=Ke; k>=Ks+1; k--){
						for(x=XA-1; x<=XB; x++){
							dtemp =(   8.0 * Daikbxtp[ii][s][i][k-1-STTY][x-1]
					    		     + 4.0 * Daikbxtp[ii][s][i][k-STTY][x]    ) / 12.0 * 1.001 / 1.0E+8;
							dtemp2=(   2.0 * Daikbxtp[ii][s][i][k-1-STTY][x-1]
					    		     + 4.0 * Daikbxtp[ii][s][i][k-STTY][x]    ) / 12.0 * 1.001 / 1.0E+8;
							Daikbxtp[ii][s][i][k-STTY][x]       = dtemp;
							if(s==0 && i==0) ddaik[ii][k-STTY] += dtemp;
							
							if(Cntlset==1){
								if(ii==1 || k>Kyoskijun){
									Daikbxtp[0][s][i][k-STTY][x]      += dtemp;
									if(s==0 && i==0) ddaik[0][k-STTY] += dtemp;
								} else if(k==Kyoskijun){
									Daikbxtp[0][s][i][k-STTY][x]      += dtemp2;
									if(s==0 && i==0) ddaik[0][k-STTY] += dtemp2;
								}
							}
						}
					}
				}
			}
		}
	}
	
	for(ii=is; ii<=ie; ii++){
		ofp = ofp01_shushi[ii];
		
		fprintf(ofp, "\n徻嵶廂巟");
		if(mode==0) fprintf(ofp, "亂僗儔僀僪挷惍慜亃\n");
		else        fprintf(ofp, "亂僗儔僀僪挷惍屻亃\n");
		fprintf(ofp, "擭搙,");
		fprintf(ofp, "婎慴擭嬥岎晅嬥,戙峴憡摉媼晅,撈帺崌寁,媽朄榁楊,媽朄忈奞,媽朄堚懓,,");
		
		fprintf(ofp, "榁戅,榁嵼,捠戅,捠嵼,忈奞,堚懓,");
		fprintf(ofp, "榁戅嘆,榁嵼嘆,捠戅嘆,捠嵼嘆,忈奞嘆,堚懓嘆,");
		fprintf(ofp, "榁戅嘇,榁嵼嘇,捠戅嘇,捠嵼嘇,忈奞嘇,堚懓嘇,");
		fprintf(ofp, "榁戅嘊,榁嵼嘊,捠戅嘊,捠嵼嘊,忈奞嘊,堚懓嘊,");
		fprintf(ofp, "65-榁戅,65-榁嵼,65-捠戅,65-捠嵼,65-忈奞,65-堚懓,,");
		
		fprintf(ofp, "斾榁戅,斾榁嵼,斾捠戅,斾捠嵼,斾忈奞,斾堚懓,");
		fprintf(ofp, "65-斾榁戅,65-斾榁嵼,65-斾捠戅,65-斾捠嵼,65-斾忈奞,65-斾堚懓,,");
		
		fprintf(ofp, "偦偺懠寁,掕妟,宱夁揑壛嶼,壛媼,堚懓壛嶼,嵟掅曐忈,");
		fprintf(ofp, "65-偦偺懠寁,65-掕妟,65-宱夁揑壛嶼,65-壛媼,65-堚懓壛嶼,65-嵟掅曐忈,,");
		
		fprintf(ofp, "怴榁戅寁,怴榁嵼寁,怴捠戅寁,怴捠嵼寁,");
		fprintf(ofp, "媽榁戅寁,媽榁嵼寁,媽捠戅寁,媽捠嵼寁,");
		fprintf(ofp, "怴忈奞寁,媽忈奞寁,怴堚懓寁,媽堚懓寁,媽捠堚寁,");
		fprintf(ofp, "怴榁戅斾,怴榁嵼斾,怴捠戅斾,怴捠嵼斾,");
		fprintf(ofp, "媽榁戅斾,媽榁嵼斾,媽捠戅斾,媽捠嵼斾,");
		fprintf(ofp, "怴忈奞斾,媽忈奞斾,怴堚懓斾,媽堚懓斾,媽捠堚斾,");
		fprintf(ofp, "怴榁戅懠,怴榁嵼懠,怴捠戅懠,怴捠嵼懠,");
		fprintf(ofp, "媽榁戅懠,媽榁嵼懠,媽捠戅懠,媽捠嵼懠,");
		fprintf(ofp, "怴忈奞懠,媽忈奞懠,怴堚懓懠,媽堚懓懠,媽捠堚懠,");
		fprintf(ofp, "怴榁戅掕,怴榁嵼掕,怴捠戅掕,怴捠嵼掕,");
		fprintf(ofp, "媽榁戅掕,媽榁嵼掕,媽捠戅掕,媽捠嵼掕,");
		fprintf(ofp, "怴忈奞掕,媽忈奞掕,怴堚懓掕,媽堚懓掕,媽捠堚掕,");
		fprintf(ofp, "怴榁戅宱,怴榁嵼宱,怴捠戅宱,怴捠嵼宱,");
		fprintf(ofp, "媽榁戅宱,媽榁嵼宱,媽捠戅宱,媽捠嵼宱,");
		fprintf(ofp, "怴忈奞宱,媽忈奞宱,怴堚懓宱,媽堚懓宱,媽捠堚宱,");
		fprintf(ofp, "怴榁戅壛,怴榁嵼壛,怴捠戅壛,怴捠嵼壛,");
		fprintf(ofp, "媽榁戅壛,媽榁嵼壛,媽捠戅壛,媽捠嵼壛,");
		fprintf(ofp, "怴忈奞壛,媽忈奞壛,怴堚懓壛,媽堚懓壛,媽捠堚壛,");
		fprintf(ofp, "怴榁戅堚壛,怴榁嵼堚壛,怴捠戅堚壛,怴捠嵼堚壛,");
		fprintf(ofp, "媽榁戅堚壛,媽榁嵼堚壛,媽捠戅堚壛,媽捠嵼堚壛,");
		fprintf(ofp, "怴忈奞堚壛,媽忈奞堚壛,怴堚懓堚壛,媽堚懓堚壛,媽捠堚堚壛,");
		fprintf(ofp, "怴榁戅嵟,怴榁嵼嵟,怴捠戅嵟,怴捠嵼嵟,");
		fprintf(ofp, "媽榁戅嵟,媽榁嵼嵟,媽捠戅嵟,媽捠嵼嵟,");
		fprintf(ofp, "怴忈奞嵟,媽忈奞嵟,怴堚懓嵟,媽堚懓嵟,媽捠堚嵟,");
		fprintf(ofp, "\n");
		
		for(k=FLKS_SHU; k<=FLKE_SHU; k++){

			fprintf(ofp, "%3d,", k);
			fprintf(ofp, "%20.13e,%20.13e,", dkof[ii][0][k-STTY], ddaik[ii][k-STTY]);
			fprintf(ofp, "%20.13e,", d4a[ii][0][0][0][k-STTY]);		
			dtemp = 0.;
			for(i=5; i<=8; i++) dtemp += d4a[ii][0][i][0][k-STTY];
			fprintf(ofp, "%20.13e,",dtemp);
			fprintf(ofp, "%20.13e,",d4a[ii][0][10][0][k-STTY]);
			fprintf(ofp, "%20.13e,",d4a[ii][0][12][0][k-STTY]+d4a[ii][0][13][0][k-STTY]);
			fprintf(ofp,  ",");
			
			for(s=0; s<=3; s++){
				for(i=1; i<=4; i++){
					fprintf(ofp,"%20.13e,",d4a[ii][s][i][0][k-STTY]+d4a[ii][s][i+4][0][k-STTY]);
				}
				fprintf(ofp,"%20.13e,",d4a[ii][s][ 9][0][k-STTY]+d4a[ii][s][10][0][k-STTY]);
				dtemp = 0.;
				for(i=11; i<=13; i++) dtemp += d4a[ii][s][i][0][k-STTY];
				fprintf(ofp,"%20.13e,",dtemp);
			}
			for(i=1; i<=4; i++){
				fprintf(ofp,"%20.13e,",d65[ii][0][i][0][k-STTY]+d65[ii][0][i+4][0][k-STTY]);
			}
			fprintf(ofp,"%20.13e,",d65[ii][0][ 9][0][k-STTY]+d65[ii][0][10][0][k-STTY]);
			dtemp = 0.;
			for(i=11; i<=13; i++) dtemp += d65[ii][0][i][0][k-STTY];
			fprintf(ofp,"%20.13e,",dtemp);
			fprintf(ofp, ",");
			
			for(i=1; i<=4; i++){
				fprintf(ofp,"%20.13e,",d4a[ii][0][i][1][k-STTY]+d4a[ii][0][i+4][1][k-STTY]);
			}
			fprintf(ofp,"%20.13e,",d4a[ii][0][ 9][1][k-STTY]+d4a[ii][0][10][1][k-STTY]);
			dtemp = 0.;
			for(i=11; i<=13; i++) dtemp += d4a[ii][0][i][1][k-STTY];
			fprintf(ofp,"%20.13e,",dtemp);
			for(i=1; i<=4; i++){
				fprintf(ofp,"%20.13e,",d65[ii][0][i][1][k-STTY]+d65[ii][0][i+4][1][k-STTY]);
			}
			fprintf(ofp,"%20.13e,",d65[ii][0][ 9][1][k-STTY]+d65[ii][0][10][1][k-STTY]);
			dtemp = 0.;
			for(i=11; i<=13; i++) dtemp += d65[ii][0][i][1][k-STTY];
			fprintf(ofp,"%20.13e,",dtemp);
			fprintf(ofp, ",");
			
			for(j=2; j<=7; j++) fprintf(ofp, "%20.13e,", d4a[ii][0][0][j][k-STTY]);
			for(j=2; j<=7; j++) fprintf(ofp, "%20.13e,", d65[ii][0][0][j][k-STTY]);
			fprintf(ofp, ",");
			
			for(j=0; j<=7; j++){
				for(i=1; i<=13; i++){
					fprintf(ofp, "%20.13e,", d4a[ii][0][i][j][k-STTY]);
				}
			}
			
			fprintf(ofp, "\n");
		}
		fprintf(ofp, "\n");
		
	}
	
	if(Nenbeex2==1 ) shus_nenbeout(mode);
	
	if(Cntlset==1 && (Saimu==0 || Saimu==1 || Saimu==2) && mode==1){
		for(ii=1; ii<=4; ii++){
			for(i=1; i<=13; i++){
				if(i==1 || i==2 || i==5 || i==6){
					j=0;
				} else if(i==3 || i==4 || i==7 || i==8){
					j=1;
				} else if(i<=10){
					j=2;
				} else {
					j=3;
				}
				for(k=Ks; k<=Ke; k++){
					Ukyu[1][ii][j][k-STTY] += d4a[ii][0][i][0][k-STTY];
					Ukyu[1][0][j][k-STTY]  += d4a[ii][0][i][0][k-STTY];
				}
			}
		}
	}
}


void shus_nenbeout(int mode){ 
	FILE *ofp;
	double dtemp;
	int s, i, j, k, x;
	int is, ie, ii;
	char sei[3][10]    = {"0崌寁", "1抝惈", "2彈惈"};
	char kyufus[4][10] = {"0崌寁", "1榁楊", "2忈奞", "3堚懓"};
	char newold[3][10] = {"0崌寁", "1怴朄", "2媽朄"};
	char zaitai[4][10] = {"0崌寁", "1戅怑", "2嵼怑", "3忈堚"};
	char shurui[3][10] = {"0崌寁", "1斾椺", "2懠寁"};
	int i2k[14] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3};
	int i2n[14] = { 0, 1, 1, 1, 1, 2, 2, 2, 2, 1, 2, 1, 2, 2};
	int i2z[14] = { 0, 1, 2, 1, 2, 1, 2, 1, 2, 3, 3, 3, 3, 3};
	
	
	if(Seidn!=0){
		is=ie=0;
	} else {
		if(Cntlset==1) is=0;  
		else           is=1;
		ie=4;
	}
	
	for(ii=is; ii<=ie; ii++){
		ofp = ofp90_nenbe[ii];
		if(mode==0) fprintf(ofp, "亂僗儔僀僪挷惍慜亃\n");
		else        fprintf(ofp, "亂僗儔僀僪挷惍屻亃\n");
		
		
		fprintf(ofp, "擭搙,惈暿,媼晅庬暿,怴媽暿,嵼戅暿,庬椶,擭楊,媼晅旓\n");
		for(s=1; s<=2; s++){
			for(i=1; i<=13; i++){
				for(j=1; j<=2; j++){
					for(k=Ks+1; k<=Ke; k++){
						for(x=XA-1; x<=XB; x++){
							fprintf(ofp, "%d,%s,%s,%s,%s,%s,",
							        k, sei[s], kyufus[i2k[i]], newold[i2n[i]], zaitai[i2z[i]], shurui[j]);
							
							if(s==1 && (Seidn==1 || ii==1 || (Cntlset==1 && ii==0))){
								dtemp = d4x[ii][1][i][j][k-STTY][x] + d4x[ii][3][i][j][k-STTY][x];
							}else{
								dtemp = d4x[ii][s][i][j][k-STTY][x];
							}
							fprintf(ofp, "%d,%20.13e\n", x, dtemp);
						}
					}
				}
			}
		}
	}
}

