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-19
Revision:
3:5add3759e08a
Parent:
2:20ecfe6edd71
Child:
5:b61f3f5b0fc8

File content as of revision 3:5add3759e08a:

#include "main_util.hpp"

void check_file_open(FILE* file_p, const char* file_name)
{
    printf("[cnt:%d] %s : %p \r\n", open_count++, file_name, file_p);
    if ( file_p == NULL ) {
        fprintf( stderr, "Error : file %s open faild. \r\n", file_name );
        fflush( stderr );
        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, diff_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;
        diff_line = (line - LEN_DATA_SEQUENCE);
        dat_file_fp = fopen(SEQUENCE_DATA_NAME, "r");
        // rewind( dat_file_fp );
        check_file_open( dat_file_fp, SEQUENCE_DATA_NAME );
        tmp_fp = fopen( tmp_file_name, "w");
        check_file_open( tmp_fp, tmp_file_name );
        line = 0;
        while(fgets(trunc_buf_str, BUF_SIZE, dat_file_fp) != NULL) {
            line++;
            if (line >= diff_line) break;
        }
        // diff_line以降をテンポラリにコピー

        while( fgets( trunc_buf_str, BUF_SIZE, dat_file_fp) != NULL) {
            fputs( trunc_buf_str, tmp_fp );
        }

        fclose( dat_file_fp );
        fclose( tmp_fp );

        // 更新
        tmp_fp = fopen( tmp_file_name, "r");
        check_file_open( tmp_fp, tmp_file_name );
        dat_file_fp = fopen(SEQUENCE_DATA_NAME, "w");
        check_file_open( dat_file_fp, SEQUENCE_DATA_NAME );
        // 一時ファイルからコピー
        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 );
        // free( tmp_fp );
    }
    // free( dat_file_fp );
}