⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mrt.java

📁 MRT批处理的JAVA实现,需要先安装MRT程序
💻 JAVA
字号:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class MRT {
	
	private Runtime run = Runtime.getRuntime();
	private BufferedWriter log;
	private File hdfPath;
	private File tifPath;
	private File classPath;
	
	private void resample() {
		try { 
			Process p = run.exec("cmd /c start resample -p " + classPath + "/in_use.prm");
//			Process p = run.exec("ping 192.168.9.254");

			log.append("Completed successfully!\r\n");
			log.append("------------------------------------------------------------------------------------\r\n");
			
			BufferedInputStream in = new BufferedInputStream(p.getInputStream());
			BufferedInputStream err = new BufferedInputStream(p.getErrorStream());
			BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
			BufferedReader errBr = new BufferedReader(new InputStreamReader(err));
			
			String lineStr;
			while ((lineStr = errBr.readLine()) != null)
				System.out.println(lineStr);
			while ((lineStr = inBr.readLine()) != null)
				System.out.println(lineStr);
			
			//检查命令是否执行失败。
			try {
				if (p.waitFor()!=0) {
					if(p.exitValue()==1)  //p.exitValue()==0表示正常结束,1:非正常结束
						System.err.println("命令执行失败!");
				}
			} catch(InterruptedException e){
				e.printStackTrace();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private void changeFile(String newFileName) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(classPath + "/model.prm"));
		BufferedWriter out = new BufferedWriter(new FileWriter(classPath + "/in_use.prm"));
		String line;
		int num = 1;
		
		while((line = in.readLine()) != null) {
			if(num == 1) {
				out.append(line.substring(0, line.indexOf("hdfPath")) + hdfPath + "/" + newFileName + "\r\n");
				log.append(line.substring(0, line.indexOf("hdfPath")) + hdfPath + "/" + newFileName + "\r\n");
				num++;
			}
			else if(num == 6) {
				out.append(line.substring(0, line.indexOf("tifPath")) + tifPath + "/" + newFileName.subSequence(0, newFileName.indexOf("h24v03")) + "tif" + "\r\n");
				log.append(line.substring(0, line.indexOf("tifPath")) + tifPath + "/" + newFileName.subSequence(0, newFileName.indexOf("h24v03")) + "tif" + "\r\n");
				num++;
			}
			else {
				out.append(line + "\r\n");
				log.append(line + "\r\n");
				num++;
			}
		}
		
		in.close();
		out.close();
	}
	
	private void makeFileList() throws IOException {
		BufferedWriter out = new BufferedWriter(new FileWriter(hdfPath + "/filelist.txt"));
		
		String[] hdfFiles = hdfPath.list();
		for(int i = 0; i < hdfFiles.length; i++) {
			if(".hdf".equals(hdfFiles[i].substring(hdfFiles[i].length() - 4))) {
				out.append(hdfFiles[i]);
				out.append("\r\n");
//				System.out.println(hdfFiles[i]);
			}
		}
		
		out.close();
	}
	
	private void setClassPath() {
		String strClassName = getClass().getName();

        URL url = null;
        url = getClass().getResource(strClassName + ".class");
        String strURL = url.toString();
        
        try {
            strURL = java.net.URLDecoder.decode(strURL,"UTF-8");
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        
        classPath = new File(strURL.substring(strURL.indexOf('/') + 1,strURL.lastIndexOf('/')));
	}
	
	private void setHdfPath(String hdfPath) {
		this.hdfPath = new File(hdfPath);
	}
	
	private void setTifPath(String tifPath) {
		this.tifPath = new File(tifPath);
	}
	
	public static void main(String[] args) throws IOException {
		if(args.length != 2) {
			System.out.println("MRT BAT v2.1 10:30 July 7th 2008");
			System.out.println("Usage: java MRT hdfPath(Dir) tifPath(Dir)");
			return;
		}
		
		MRT mrt = new MRT();
		
		mrt.setHdfPath(args[0]);  System.out.println(mrt.hdfPath);
		mrt.setTifPath(args[1]);  System.out.println(mrt.tifPath);
		mrt.setClassPath();       System.out.println(mrt.classPath);
		
		mrt.makeFileList();
//		mrt.changeFile("MOD13Q1.A2001001.h24v03.005.2006352053028.hdf");
		
		BufferedReader in = new BufferedReader(new FileReader(mrt.hdfPath + "/filelist.txt"));
		mrt.log = new BufferedWriter(new FileWriter(mrt.tifPath + "/log.txt"));
		
		String line;
		while((line = in.readLine()) != null) {
			mrt.changeFile(line);
			mrt.resample();
		}
		
		mrt.log.close();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -