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

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

void shus_fullout(int);  /** 徻嵶廂巟偺弌椡 **/


/* 媼晅旓(擭搙娫抣) : d4x[psd:0-4][S:0-3][I:0-13][J:0-9][K][X] */
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 ii, se;
	
	
	for(ii=0; ii<=4; 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(ii==0) continue;
		
		/*** 媼晅旓偺寁嶼 ***/
		if(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(k==13) riv += 0.010;  /* 俁擭嫮惂夝徚偺弶擭搙2013偺敿婜暘懳墳 */
						
						dtemp = (bpre * 2.0 + bpre * riv * 6.0 + bend * 4.0) / 12.0 * 1.002 / 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;
						}
						
						/* 堦尦壔帪偵tou傊偺懌偟偙傒 */
						if(ii>1 && k<Kyoskijun){
							dtemp = 0.;
						} else if(ii>1 && k==Kyoskijun){
							dtemp = (bpre * riv * 2. + bend * 4.) /12.0 * 1.002 / 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++){  /* XA-2偼0偱弶婜壔偝傟偰偄傞 */
					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(k==13) riv += 0.010;  /* 俁擭嫮惂夝徚偺弶擭搙2013偺敿婜暘懳墳 */
					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(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.002 / 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.002 / 1.0E+8;
							Daikbxtp[ii][s][i][k-STTY][x]       = dtemp;
							if(s==0 && i==0) ddaik[ii][k-STTY] += dtemp;
							
							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=0; ii<=4; 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,  ",");
			
			/* 乮惈暿0乣3乯"榁戅,榁嵼,捠戅,捠嵼,忈奞,堚懓," */
			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);
			}
			/* "65-榁戅,65-榁嵼,65-捠戅,65-捠嵼,65-忈奞,65-堚懓,," */
			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);
			/* "65-斾榁戅,65-斾榁嵼,65-斾捠戅,65-斾捠嵼,65-斾忈奞,65-斾堚懓," */
			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, ",");
			
			/* "偦偺懠寁,掕妟,宱夁揑壛嶼,壛媼,堚懓壛嶼,嵟掅曐忈," */
			/* "65-偦偺懠寁,65-掕妟,65-宱夁揑壛嶼,65-壛媼,65-堚懓壛嶼,65-嵟掅曐忈,," */
			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, ",");
			
			/* 徻嵶弌椡丗j,i暿 */
			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(mode==1){
		/* 媼晅旓(擭搙娫抣) : d4a[psd:0-4][S:0-3][I:0-13][J:0-9][K] */
		/* Ukyu[2][5][4][ENDY-STTY+1]; /* Ukyu[0恖1妟][Psd][I:0榁摉,1捠憡,2忈奞,3堚懓][K] */
		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];
				}
			}
		}
	}
}



