- 积分
- 5142
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-10-20
- 最后登录
- 1970-1-1
|
发表于 2012-1-10 13:12:29
|
显示全部楼层
本帖最后由 qw5622390 于 2012-1-10 13:12 编辑
这是别人写的
Diamond13.java
001 /*******************************************************************************
002 * *
003 * FUNCTION: 处理Micaps的第13类数据文件(云图图像数据) *
004 * PACKAGE: cma.micaps.diamond *
005 * FILENAME: Diamond13.java *
006 * LANGUAGE: Java2 v1.4 *
007 * ORIGINAL: c++ (CDiamond13.cpp CDiamond13.h SDiamond13.hpp) *
008 * DESCRIPTION: Micaps diamond 13 data I/O *
009 * CREATE: 2001-06-22 2006-11-03 改写为 JAVA 版 *
010 * UPDATE: 2002-05-04 *
011 * AUTHOR: 刘泽军 (BJ0773@gmail.com) *
012 * *
013 *******************************************************************************/
014
015 package cma.micaps.diamond;
016
017 import java.io.*;
018 import java.util.*;
019 import java.lang.*;
020 import java.awt.Color;
021 import java.text.DecimalFormat;
022 import java.awt.*;
023 import java.awt.image.*;
024 import java.awt.image.DataBufferByte;
025
026 import cma.common.atmos.*;
027 import cma.micaps.diamond.datatype.*;
028
029 public class Diamond13 {
030
031 public static String SYMBOL = "diamond"; //文件标志
032 public static int TYPE = 13; //文件类型
033
034 public boolean enabled = false;
035 public String filename = "";
036
037 public String symbol = "";
038 public String type = "";
039 public String title = "";
040 public String year = "";
041 public String month = "";
042 public String day = "";
043 public String hour = "";
044 public String width = "";
045 public String height = "";
046 public String longitudeWest = "";
047 public String latitudeSouth = "";
048 public String projection = "";
049 public String zoom = "";
050 public String kind = "";
051 public String refer = "";
052 public String longitudeCenter = "";
053 public String latitudeCenter = "";
054
055 public byte data[];
056 public double temperature[];
057
058 /**
059 * 功能:类构造函数
060 * 参数:
061 * 无
062 * 返回值:
063 * 无
064 */
065 public Diamond13() {
066 enabled = false;
067 }
068
069 /**
070 * 功能:类构造函数
071 * 参数:
072 * fname - Micaps调色板文件名
073 * 返回值:
074 * 无
075 */
076 public Diamond13(String fname) {
077 enabled = loadFromFile(fname);
078 }
079
080 /**
081 * 功能:根据云图像素点的byte值获得对应的颜色索引(同时也是云顶温度索引)
082 * 参数:
083 * value - 像素点值
084 * 返回值:
085 * 颜色索引
086 * 说明:
087 *
088 */
089 public int getIndex(byte value) {
090 int index = (new Byte(value)).intValue();
091 // -128 <= index <= 127
092 // 0 - 127 => 0 - 127
093 //-128 - -1 => 128 - 255
094 if( index < 0 ) index = index + 256;
095 if( index < 0 ) index = 0;
096 if( index > 255 ) index = 255;
097 return(index);
098 }
099
100 /**
101 * 功能:读取Micaps云图数据
102 * 参数:
103 * fname - Micaps云图文件名
104 * 返回值:
105 * 是否读取成功
106 */
107 public boolean loadFromFile(String fname) {
108 if( !(new File(fname)).exists() || (new File(fname)).length() <= 128 ) {
109 System.out.println("Diamond13.java #109 : " + fname + " not found!");
110 return(false);
111 }
112 try {
113 char symbolDiamond[] = new char[8];
114 char symbolType[] = new char[3];
115 char symbolTitle[] = new char[40];
116 FileInputStream fis = new FileInputStream(fname);
117 byte cHead[] = new byte[128];
118 fis.read(cHead);//读文件头
119
120 symbol = new String(cHead, 0, 8);
121 type = new String(cHead, 8, 3);
122 title = new String(cHead, 11, 40, "gb2312");//汉字编码
123 title = title.replaceAll(" ", "");
124 year = new String(cHead, 51, 5);
125 month = new String(cHead, 56, 3);
126 day = new String(cHead, 59, 3);
127 hour = new String(cHead, 62, 3);
128 width = new String(cHead, 65, 5);
129 height = new String(cHead, 70, 5);
130 longitudeWest = new String(cHead, 75, 8);
131 latitudeSouth = new String(cHead, 83, 8);
132 projection = new String(cHead, 91, 2);
133 zoom = new String(cHead, 93, 5);
134 kind = new String(cHead, 98, 2);
135 refer = new String(cHead,100, 12);
136 longitudeCenter = new String(cHead,112, 8);
137 latitudeCenter = new String(cHead,120, 8);
138
139 int iWidth = Integer.parseInt(width.trim());
140 int iHeight = Integer.parseInt(height.trim());
141
142 data = new byte[iWidth*iHeight];
143 int iLength = fis.read(data);//读云图灰度值
144 fis.close();
145 filename = iLength == iWidth*iHeight ? fname : "";
146 return(iLength == iWidth*iHeight);
147 }
148 catch(Exception ex) {
149 System.out.println(ex.getMessage());
150 ex.printStackTrace();
151 return(false);
152 }
153 }
154
155 /**
156 * 功能:画Micaps云图
157 * 参数:
158 * g - 目标
159 * x,y - 左上角位置
160 * p - MICAPS调色板文件(diamond 15类)
161 * z - 缩放比例
162 * 返回值:
163 * 是否成功
164 */
165 public boolean draw(Graphics2D g, int x, int y, String p, double z) {
166 if( !enabled || null == p || "".equals(p) || !(new File(p)).exists() ) {
167 return(false);
168 }
169 try {
170 Diamond15 d15 = new Diamond15();//
171 boolean enabled15 = d15.loadFromFile(p);
172 if( !enabled15 ) {
173 return(false);
174 }
175 int iWidth = Integer.parseInt(width.trim());
176 int iHeight = Integer.parseInt(height.trim());
177
178 byte cData[] = new byte[iWidth*iHeight*3];
179 int index = 0, rValue = 0, gValue = 0, bValue = 0;
180 for(int h=0;h<iHeight;h++) {
181 for(int w=0;w<iWidth;w++) {
182 index = Diamond15.getIndex(data[(iHeight-1-h)*iWidth+w]);
183 if( enabled15 ) {//调色板加载成功
184 rValue = d15.getRed (index);
185 gValue = d15.getGreen(index);
186 bValue = d15.getBlue (index);
187 }
188 else {//使用灰度图
189 rValue = index;
190 gValue = index;
191 bValue = index;
192 }
193
194 index = h*iWidth*3+w*3;
195 cData[index+0] = (new Integer(rValue)).byteValue();
196 cData[index+1] = (new Integer(gValue)).byteValue();
197 cData[index+2] = (new Integer(bValue)).byteValue();
198
199 }
200 }
201
202 DataBufferByte bufferCloud = new DataBufferByte(cData, cData.length);
203 Raster rasterCloud = Raster.createInterleavedRaster(bufferCloud, iWidth, iHeight, iWidth*3, 3, new int[]{0, 1, 2}, null);
204 BufferedImage imageCloud = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_INT_RGB);
205 imageCloud.setData(rasterCloud);
206
207 g.drawImage(imageCloud, 0, 0, (int)(iWidth*z), (int)(iHeight*z), d15.getColor(0), null);
208
209 return(true);
210 }
211 catch(Exception ex) {
212 System.out.println(ex.getMessage());
213 ex.printStackTrace();
214 return(false);
215 }
216 }
217
218 /**
219 * 功能:加载云顶温度对照表
220 * 参数:
221 * fname - 对照表文件名
222 * 返回值:
223 * 是否成功
224 */
225 public boolean loadReferFile(String fname) {
226 if( !enabled || !(new File(fname)).exists() ) {
227 return(false);
228 }
229 try {
230 temperature = new double[256];
231 InputStreamReader isr = new InputStreamReader(new FileInputStream(fname), "gb2312");//支持汉字
232 BufferedReader br = new BufferedReader(isr);
233 Vector vectorData = new Vector();
234 String lineString;
235 StringTokenizer st;
236 while( null != ( lineString = br.readLine() ) ) {
237 st = new StringTokenizer(lineString," /r/n");
238 while( st.hasMoreTokens() ) {
239 vectorData.add(st.nextToken());
240 }
241 }
242 br.close();
243
244 if( vectorData.size() != 256 ) {
245 return(false);
246 }
247 for(int i=0;i<256;i++ ) {
248 temperature = Double.parseDouble((String)vectorData.get(i));
249 }
250 return(true);
251 }
252 catch(Exception ex) {
253 System.out.println(ex.getMessage());
254 ex.printStackTrace();
255 return(true);
256 }
257 }
258
259 /**
260 * 功能:获得指定象素点的云顶温度(注意,用户必须确保云图数据及对照表数据均已功能读取,且x、y值有效,x、y值从0开始)
261 * 参数:
262 * x,y - 云图象素点位置
263 * 返回值:
264 * 云顶温度
265 */
266 public double getTemperature(int x, int y) {
267 int iWidth = Integer.parseInt(width.trim());
268 int iHeight = Integer.parseInt(height.trim());
269
270 int index = getIndex(data[(iHeight-1-y)*iWidth+x]);
271 return(temperature[index]);
272 }
273 } |
评分
-
查看全部评分
|