2016年11月22日 星期二

自動抓取RMT的圖檔



RMT的公開資料是從自由場加速度感測器取得,主要是用來計算地震當下的參數。從好幾次的觀察經驗得知,出震前1天到幾個小時,會有一些特殊的特徵波型,尤其是越大的地震會越明顯。目前中研院只用瞬間或很短的時間資料來計算,沒有用到震前幾小時的資料,因此可以研究一下如何利用這些資料來計算震前的現象。
這個簡單的java程式RMT網站抓取圖檔以每日/每小時分目錄置放,估計一天約有4.7GB的資料量。這些圖檔未來可進行OCR辨識,將數值還原成文字資料,或是以其他圖訊辨識技術,計算震前的特徵。使用本程式前,須先安裝 Java SDK,使用javac編譯後,再用java來執行。程式會呼叫外部wget程式,因此亦須安裝 wgetLinux系統直接安裝即可,Windows系統可用GnuWin32http://gnuwin32.sourceforge.net/packages/wget.htm,安裝後一樣要設定系統PATH才能被呼叫。為了準確地2秒抓一次圖,每2秒產生一個執行緒,每個抓圖執行緒應會在0.1秒內完成,如果失敗的話,圖檔的內容會不正常,便於瞭解錯誤狀況,若不需要可另加程式自動刪除。

import java.text.*;
import java.util.*;
import java.io.*;

public class RMT
{
        public static void main(String[] args)
        {
                while (true) {
                        try {
                                downloadThread thread = new downloadThread();
                                thread.start();                        
                                Thread.sleep(2000);
                        } catch(InterruptedException ex) {
                                Thread.currentThread().interrupt();
                        }
                }
        }
}

class downloadThread extends Thread{
        public void run(){
                downloadRMT();
        }  
       
        void downloadRMT()
        {
                Date now = new java.util.Date();
                String dirTodayName = new SimpleDateFormat("yyyyMMdd").format(now);
                String dirHourName  = new SimpleDateFormat("HH").format(now);
                String filename = new SimpleDateFormat("yyyyMMdd-HHmmss").format(now) + ".png";             
                String filepath =  dirTodayName + "/" + dirHourName + "/" + filename;
               
                File dirToday = new File(dirTodayName);
        if (!dirToday.exists()) {
            boolean success = dirToday.mkdir();
            if (success) {
                System.out.printf("Successfully created new directory : %s%n", dirTodayName);
            } else {
                System.out.printf("Failed to create new directory: %s%n", dirTodayName);
            }
        }
               
                File dirHour = new File(dirTodayName + "/" + dirHourName);
        if (!dirHour.exists()) {
            boolean success = dirHour.mkdir();
            if (success) {
                System.out.printf("Successfully created new directory : %s%n", dirHourName);
            } else {
                System.out.printf("Failed to create new directory: %s%n", dirHourName);
            }
        }
               
                try {           
            Runtime rt = Runtime.getRuntime ();
                        String command = "wget http://rmt.earth.sinica.edu.tw/rmt.png -O " + filepath;
                        System.out.println (command);
            Process proc = rt.exec (command);
                        int exitCode = proc.waitFor ();
                        if (exitCode==0) {
                                //System.out.println ("OK");
                        } else {
                                System.out.println ("Fail : Code = " + exitCode);
                        }
                       
        } catch (Exception e) {
            e.printStackTrace();
        }              
        }
}

沒有留言:

張貼留言