Weather casting with Machine Learning (SVM and SRNN).

Dependencies:   EthernetInterface GraphicHandler NTPClient SRNN SVM SensorModule mbed-rtos mbed

main_util.cpp

Committer:
yukari_hinata
Date:
2015-02-18
Revision:
2:20ecfe6edd71
Child:
3:5add3759e08a

File content as of revision 2:20ecfe6edd71:

#include "main_util.hpp"

void check_file_open(FILE* file_p, const char* file_name)
{
    if ( file_p == NULL ) {
        fprintf( stderr, "Error : file %s open faild. \r\n", file_name );
        exit(1);
    }
}

// データ系列ファイルの行数をLEN_DATA_SEQUENCEまで切り詰める
void truncate_data_file(void)
{
    FILE* dat_file_fp;
    const char tmp_file_name[] = "/local/TMP_DAT.CSV";
    int line;
    char trunc_buf_str[BUF_SIZE];
    // 最初に現在の行数を数える
    dat_file_fp = fopen( SEQUENCE_DATA_NAME, "r");
    check_file_open( dat_file_fp, SEQUENCE_DATA_NAME);
    line = 0;
    while(fgets(trunc_buf_str, BUF_SIZE, dat_file_fp) != NULL) {
        line++;
        // printf("line %d : %s\r\n", line, trunc_buf_str);
    }
    // printf("current num of line : %d \r\n", line);
    fclose( dat_file_fp );

    // 切り詰め開始
    if ( line > LEN_DATA_SEQUENCE ) {
        FILE* tmp_fp;
        int diff_line = line - LEN_DATA_SEQUENCE;
        dat_file_fp = fopen(SEQUENCE_DATA_NAME, "r");
        check_file_open( dat_file_fp, SEQUENCE_DATA_NAME );
        line = 0;
        while(fgets(trunc_buf_str, BUF_SIZE, dat_file_fp) != NULL) {
            line++;
            if (line == diff_line) break;
        }
        // diff_line以降をテンポラリにコピー
        tmp_fp = fopen( tmp_file_name, "w");
        check_file_open( tmp_fp, tmp_file_name );
        fflush( tmp_fp );
        fflush( dat_file_fp );
        while( fgets( trunc_buf_str, BUF_SIZE, dat_file_fp) != NULL) {
            fputs( trunc_buf_str, tmp_fp);
        }
        //fclose( tmp_fp );
        //fclose( dat_file_fp );

        // 更新
        tmp_fp = freopen( tmp_file_name, "r", tmp_fp);
        check_file_open( tmp_fp, tmp_file_name );
        dat_file_fp = freopen(SEQUENCE_DATA_NAME, "w", dat_file_fp);
        check_file_open( dat_file_fp, SEQUENCE_DATA_NAME );
        // 一時ファイルからコピー
        fflush( tmp_fp );
        fflush( dat_file_fp );
        while( fgets( trunc_buf_str, BUF_SIZE, tmp_fp) != NULL) {
            fputs( trunc_buf_str, dat_file_fp);
        }

        fclose( dat_file_fp );
        fclose( tmp_fp );

        // テンポラリの削除
        remove( tmp_file_name );
    }
}