Weather casting with Machine Learning (SVM and SRNN).

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main_util.cpp Source File

main_util.cpp

00001 #include "main_util.hpp"
00002 
00003 void update_srnn_sample(float* sample_queue, float* new_data)
00004 {
00005     // 古いデータの消去, データのシフト
00006     for (int i = 0; i < DIM_SIGNAL * LEN_DATA_SEQUENCE; i++) {
00007         if ( i < DIM_SIGNAL * (LEN_DATA_SEQUENCE-1) ) 
00008             sample_queue[i] = sample_queue[i+DIM_SIGNAL];
00009     }
00010         
00011     // 末尾に新データを追加
00012     memcpy(&(sample_queue[DIM_SIGNAL * (LEN_DATA_SEQUENCE-1)]), new_data, sizeof(float) * DIM_SIGNAL);
00013 }
00014 
00015 // ファイルのオープンチェック(いや、fopenをラップしろよサーセン)
00016 void check_file_open(FILE* file_p, const char* file_name)
00017 {
00018     // static int open_count;
00019     // printf("[cnt:%d] %s : %p \r\n", open_count++, file_name, file_p);
00020     if ( file_p == NULL ) {
00021         fprintf( stderr, "Error : file %s open faild. \r\n", file_name );
00022         fflush( stderr );
00023         exit(1);
00024     }
00025 }
00026 
00027 // データ系列ファイルの行数をmaxlineまで切り詰める
00028 void truncate_data_file(int maxline)
00029 {
00030     FILE* dat_file_fp;
00031     const char tmp_file_name[] = "/local/TMP_DAT.CSV";
00032     int line, diff_line;
00033     char trunc_buf_str[BUF_SIZE];
00034     // 最初に現在の行数を数える
00035     dat_file_fp = fopen( SEQUENCE_DATA_NAME, "r");
00036     check_file_open( dat_file_fp, SEQUENCE_DATA_NAME);
00037     line = 0;
00038     while(fgets(trunc_buf_str, BUF_SIZE, dat_file_fp) != NULL) {
00039         line++;
00040     }
00041     fclose( dat_file_fp );
00042 
00043     // 切り詰め開始
00044     if ( line > maxline ) {
00045         FILE* tmp_fp;
00046         diff_line = (line - maxline);
00047         dat_file_fp = fopen(SEQUENCE_DATA_NAME, "r");
00048         // rewind( dat_file_fp );
00049         check_file_open( dat_file_fp, SEQUENCE_DATA_NAME );
00050         tmp_fp = fopen( tmp_file_name, "w");
00051         check_file_open( tmp_fp, tmp_file_name );
00052         line = 0;
00053         while(fgets(trunc_buf_str, BUF_SIZE, dat_file_fp) != NULL) {
00054             line++;
00055             if (line >= diff_line) break;
00056         }
00057         // diff_line以降をテンポラリにコピー
00058 
00059         while( fgets( trunc_buf_str, BUF_SIZE, dat_file_fp) != NULL) {
00060             fputs( trunc_buf_str, tmp_fp );
00061         }
00062 
00063         fclose( dat_file_fp );
00064         fclose( tmp_fp );
00065 
00066         // 更新
00067         tmp_fp = fopen( tmp_file_name, "r");
00068         check_file_open( tmp_fp, tmp_file_name );
00069         dat_file_fp = fopen(SEQUENCE_DATA_NAME, "w");
00070         check_file_open( dat_file_fp, SEQUENCE_DATA_NAME );
00071         // 一時ファイルからコピー
00072         while( fgets( trunc_buf_str, BUF_SIZE, tmp_fp) != NULL) {
00073             fputs( trunc_buf_str, dat_file_fp);
00074         }
00075 
00076         fclose( dat_file_fp );
00077         fclose( tmp_fp );
00078 
00079         // テンポラリの削除
00080         remove( tmp_file_name );
00081     }
00082 }