/*****************************************
*                                        *
*    外枠システム  readkyos.c            *
*  （共済関係ファイル読み込み）          *
*                                        *
*                                        *
*****************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "set.h"
#include "cntl.h"
#include "kyos.h"
#include "file_open.h"



void readkyos_1( int seido, int KYOREQB ) {

    int   ii ;
    int   jj ;

    switch ( seido ) {
        case 1:
            sprintf(filename, "/rev26/wakuc/data/kyos/kok26-%02d.csv", KYOREQB ) ;
            break ;
        case 2:
            sprintf(filename, "/rev26/wakuc/data/kyos/ren26-%02d.csv", KYOREQB ) ;
            break ;
        case 3:
            sprintf(filename, "/rev26/wakuc/data/kyos/sig26-%02d.csv", KYOREQB ) ;
            break ;
    }
    if ((fp_setakyo = fopen(filename, "r")) == NULL) {
        fprintf(stderr, "共済からのデータファイルを開けません\n");
        exit (2);
    }

        karayomi( fp_setakyo );

    for ( sei=1; sei<=2; sei++ ) {
      for ( nendo=2005; nendo<=2110; nendo++ ) {  
        read_csv( buffer, fp_setakyo, &data_number );

        if ( buffer[0] != seido ) {
            fprintf(stderr, "共済データの整合性エラー\n" );
            exit (2);
        }
        if ( buffer[1] != 2 ) {
            fprintf(stderr, "共済データの整合性エラー\n" );
            exit (2);
        }
        ii = (int)buffer[2] ; 
        jj = (int)buffer[3] ; 
        if ( jj>KIJUN ) { 
            for ( nenrei=15; nenrei<=100; nenrei++ ) {
              kyosai[seido][jj-STARTY][ii][nenrei] = buffer[ nenrei-10 ] ;
            }
        }
      }
    }

    fclose( fp_setakyo ) ;

    printf("共済%dのreadkyos_1終了\n", seido);

}



void readkyos_2( int seido, int nendo ) {

    int   ii ;
    int   jj ;

    switch ( seido ) {
        case 1:
            sprintf(filename, "/rev26/wakuc/data/kyos/kok26-kisor.csv" ) ;
            break ;
        case 2:
            sprintf(filename, "/rev26/wakuc/data/kyos/ren26-kisor.csv" ) ;
            break ;
        case 3:
            sprintf(filename, "/rev26/wakuc/data/kyos/sig26-kisor.csv" ) ;
            break ;
        case 4:
        case 5:
        case 6:
        case 7:
            sprintf(filename, "/rev26/wakuc/data/kyos/kou26-kisor.csv" ) ; 
            break ;
    }
    if ((fp_setakyo = fopen(filename, "r")) == NULL) {
        fprintf(stderr, "共済の基礎率データファイルを開けません!\n");
        exit (2);
    }

        karayomi( fp_setakyo );
        karayomi( fp_setakyo );

    for ( nenrei=15; nenrei<=85; nenrei++ ) {
        read_csv( buffer, fp_setakyo, &data_number );
        ii = (int)buffer[0] ; 
        dattai[seido][nendo-STARTY][1][ii] = buffer[1] ; 
        dattai[seido][nendo-STARTY][2][ii] = buffer[2] ; 
        jj = (int)buffer[3] ; 
        bup[seido][nendo-STARTY][1][jj] = buffer[4] ; 
        bup[seido][nendo-STARTY][2][jj] = buffer[5] ; 
    }

    fclose( fp_setakyo ) ;

    printf("共済%dのreadkyos_2終了\n", seido);

}



void readkyos_3( int seido, int KYOREQB ) {

    int   ii ;

    switch ( seido ) {
        case 1:
            sprintf(filename, "/rev26/wakuc/data/kyos/kok26-akyos-%02d.csv" , KYOREQB ) ;
            break ;
        case 2:
            sprintf(filename, "/rev26/wakuc/data/kyos/ren26-akyos-%02d.csv" , KYOREQB ) ;
            break ;
        case 3:
            sprintf(filename, "/rev26/wakuc/data/kyos/sig26-akyos-%02d.csv" , KYOREQB ) ;
            break ;
    }
    if ((fp_setakyo = fopen(filename, "r")) == NULL) {
        fprintf(stderr, "共済のデータファイルを開けません\n");
        exit (2);
    }

        karayomi( fp_setakyo );

    for ( nendo=2010; nendo<=2110; nendo++ ) {  
        read_csv( buffer, fp_setakyo, &data_number );
        ii = (int)buffer[0] ; 
        akyos[seido][ii-STARTY][0] = buffer[1]    ; 
        st_akyos[seido][ii-STARTY][0] = buffer[1] ; 
        akyos[seido][ii-STARTY][1] = buffer[2]    ; 
        st_akyos[seido][ii-STARTY][1] = buffer[2] ; 
        akyos[seido][ii-STARTY][2] = buffer[3]    ; 
        st_akyos[seido][ii-STARTY][2] = buffer[3] ; 
    }

    fclose( fp_setakyo ) ;

    printf("共済%dのreadkyos_3終了\n", seido);

}

