Diamond02.java
001 /*******************************************************************************
002 * *
003 * 定义Micaps的第2类数据的读写 *
004 * *
005 * PACKAGE: cma.micaps.diamond *
006 * FILENAME: Diamond02.java *
007 * LANGUAGE: Java2 v1.4 *
008 * ORIGINAL: c++ (CDiamond02.cpp CDiamond02.h SDiamond02.hpp) *
009 * DESCRIPTION: Micaps diamond 02 data I/O *
010 * CREATE: 2000-03-12 *
011 * UPDATE: 2006-03-12 12:09:03 *
012 * AUTHOR: 刘泽军 (BJ0773@gmail.com) *
013 * *
014 *******************************************************************************/
015
016 package cma.micaps.diamond;
017
018 import java.io.*;
019 import java.util.*;
020 import java.lang.*;
021 import java.text.DecimalFormat;
022
023 import cma.common.atmos.*;
024 import cma.micaps.diamond.datatype.*;
025
026 public class Diamond02 {
027
028 public static double VALUE = Algorithm.DefaultValue;
029 private DiamondHeader02 Header;
030 private Vector Data = new Vector();;
031
032 private boolean Enabled = false;
033 private String filename = "";
034
035 public boolean isEnabled() {
036 return(Enabled);
037 }
038
039 public String getFilename() {
040 return(filename);
041 }
042
043 public DiamondHeader02 getHeader() {
044 return(Header);
045 }
046
047 public DiamondData02 getData(int index) {
048 if( index >= 0 && index < Data.size() ) {
049 return((DiamondData02)Data.get(index));
050 }
051 else {
052 return(new DiamondData02());
053 }
054 }
055
056 public int getCount() {
057 return(Math.min(Header.Count, Data.size()));
058 };
059
060 public Diamond02() {
061 Enabled = false;
062 }
063
064 public Diamond02(String fname) {
065 Enabled = loadFromFile(fname);
066 }
067
068 public boolean loadFromFile(String fname) {
069
070 Enabled = false;
071 filename = "";
072 Vector vectorData = new Vector();
073 File f = new File(fname);
074 DiamondType dt = new DiamondType();
075 if( !f.exists() || !f.canRead() || !dt.parseFile(fname, DiamondHeader02.TYPE) ) {
076 return(Enabled);
077 }
078 try {//read file header
079
080 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(fname), "gb2312");//支持汉字
081 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
082
083 String lineString;
084 StringTokenizer st;
085 while( null != ( lineString = bufferedReader.readLine() ) ) {//System.out.println(lineString);
086 st = new StringTokenizer(lineString," /r/n");
087 while( st.hasMoreTokens() ) {
088 vectorData.add(st.nextToken());//System.out.print((String)vectorData.get(vectorData.size()-1) + " ");
089 }//System.out.println();
090 }//System.out.println(vectorData.size());
091 inputStreamReader.close();
092 if( vectorData.size() < DiamondHeader02.SIZE + DiamondData02.SIZE ||
093 !DiamondHeader02.SYMBOL.equalsIgnoreCase((String)vectorData.get(0)) ||
094 DiamondHeader02.TYPE != Integer.parseInt((String)vectorData.get(1)) ) {
095 return(false);
096 }
097
098 //读文件头
099 int index = 0;
100 Header = new DiamondHeader02();
101 Header.Symbol = (String)vectorData.get(0); //数据标志 diamond
102 Header.Type = Integer.parseInt((String)vectorData.get(1)); //数据类型 2
103 Header.Title = (String)vectorData.get(2); //标题
104 Header.Year = Integer.parseInt((String)vectorData.get(3)); //年
105 Header.Month = Integer.parseInt((String)vectorData.get(4)); //月
106 Header.Day = Integer.parseInt((String)vectorData.get(5)); //日
107 Header.Hour = Integer.parseInt((String)vectorData.get(6)); //时次
108 Header.Layer = Integer.parseInt((String)vectorData.get(7)); //层次
109 Header.Count = Integer.parseInt((String)vectorData.get(8)); //站点总数
110
111 //读数据
112 if( Header.Count > 0 && vectorData.size() >= DiamondHeader02.SIZE + Header.Count*DiamondData02.SIZE ) {
113 for( int i=DiamondHeader02.SIZE;i<vectorData.size();i=i+DiamondData02.SIZE ) {
114 DiamondData02 data = new DiamondData02();
115 data.Station = (String)vectorData.get(i+0); //区站号
116 data.Longitude = Double.parseDouble((String)vectorData.get(i+1)); //经度
117 data.Latitude = Double.parseDouble((String)vectorData.get(i+2)); //纬度
118 data.Altitude = Double.parseDouble((String)vectorData.get(i+3)); //拔海高度
119 data.Level = Integer.parseInt ((String)vectorData.get(i+4)); //站点级别
120 data.H = Double.parseDouble((String)vectorData.get(i+5)); //高度
121 data.T = Double.parseDouble((String)vectorData.get(i+6)); //温度
122 data.T_Td = Double.parseDouble((String)vectorData.get(i+7)); //温度露点差
123 data.dd = Double.parseDouble((String)vectorData.get(i+8)); //风向
124 data.ff = Double.parseDouble((String)vectorData.get(i+9)); //风速
125 Data.add(data);//System.out.println(String.valueOf(i) + " " + String.valueOf(Data.size()));
126 }
127 Enabled = true;
128 filename = (new File(fname)).getAbsolutePath();//System.out.println(filename + " " + String.valueOf(Data.size()));
129 }
130 else {
131 Enabled = false;
132 filename = "";
133 }
134 }
135 catch( IOException ex ) {
136 System.out.println(ex.getMessage());
137 ex.printStackTrace();
138 Enabled = false;
139 filename = "";
140 }
141 return(Enabled);
142 }
143 /**
144 * 保存 Micaps 的第 2 类格式文件
145 * fname - 输出文件名
146 */
147 public boolean saveToFile(String fname) {
148 try {
149 return(saveToFile(fname, 10, 2));
150 }
151 catch(Exception ex) {
152 System.out.println(ex.getMessage());
153 ex.printStackTrace();
154 return(false);
155 }
156 }
157 /*
158 * 保存 Micaps 的第 2 类格式文件
159 * fname - 输出文件名
160 * len - 数据长度(占多少个字符)
161 * digits - 小数部份长度(占多少个字符)
162 */
163 public boolean saveToFile(String fname, int len, int digits) {
164 try {
165 File file1 = new File(fname);
166 if( !Enabled ||
167 (file1.exists() && !file1.canWrite()) ||
168 (!file1.exists() && !file1.createNewFile()) ) {
169 return false;
170 }
171 //输出格式
172 int iLen = len > 1 ? len : 1;
173 int iDigits = digits > 0 && digits < iLen ? digits : iLen - 1;
174 String fmt = "0.";
175 for(int i=0;i<iDigits;i++) {
176 fmt = fmt + "#";
177 }
178 DecimalFormat df = new DecimalFormat(fmt);
179 fmt = "";
180 for(int i=0;i<iLen;i++) {
181 fmt = fmt + " ";
182 }
183
184 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(fname), "gb2312");
185 BufferedWriter bw = new BufferedWriter(osw);
186
187 bw.write(Header.Symbol + " " + String.valueOf(Header.Type) + " " + Header.Title); //diamond 2 title
188 bw.newLine();
189 bw.write(String.valueOf(Header.Year) + " ");
190 bw.write(String.valueOf(Header.Month) + " ");
191 bw.write(String.valueOf(Header.Day) + " ");
192 bw.write(String.valueOf(Header.Hour) + " ");
193 bw.write(String.valueOf(Header.Layer) + " ");
194 bw.write(String.valueOf(Header.Count));
195 bw.newLine();
196 DiamondData02 data;
197 String line = "", str = "";
198 for( int i=0;i<Header.Count;i++ ) {
199 data = (DiamondData02)Data.get(i);
200 line = data.Station; //区站号
201 str = fmt + df.format(data.Longitude); //经度
202 line = line + str.substring(str.length() - iLen);
203 str = fmt + df.format(data.Latitude); //纬度
204 line = line + str.substring(str.length() - iLen);
205 str = fmt + df.format(data.Altitude); //拔海高度
206 line = line + str.substring(str.length() - iLen);
207 str = fmt + String.valueOf(data.Level); //站点级别
208 line = line + str.substring(str.length() - iLen);
209 str = fmt + (data.H == Math.abs(VALUE) ? "9999" : df.format(data.H)); //高度
210 line = line + str.substring(str.length() - iLen);
211 str = fmt + (data.T == Math.abs(VALUE) ? "9999" : df.format(data.H)); //温度
212 line = line + str.substring(str.length() - iLen);
213 str = fmt + (data.T_Td == Math.abs(VALUE) ? "9999" : df.format(data.H)); //温度露点差
214 line = line + str.substring(str.length() - iLen);
215 str = fmt + String.valueOf(data.dd); //风向
216 line = line + str.substring(str.length() - iLen);
217 str = fmt + String.valueOf(data.ff); //风速
218 line = line + str.substring(str.length() - iLen);
219 bw.write(line);
220 bw.newLine();
221 };
222 bw.newLine();
223 bw.flush();
224 bw.close();
225 return(true);
226 }
227 catch(Exception ex) {
228 System.out.println(ex.getMessage());
229 ex.printStackTrace();
230 return(false);
231 }
232 }
233 }
DiamondData02.java
01 /*******************************************************************************
02 * *
03 * 定义Micaps的第2类数据 *
04 * *
05 * PACKAGE: cma.micaps.diamond.datatype *
06 * FILENAME: DiamondData04.java *
07 * LANGUAGE: Java2 v1.4 *
08 * ORIGINAL: c++ *
09 * DESCRIPTION: Micaps diamond 02 data *
10 * CREATE: 2006-03-12 11:59:54 *
11 * UPDATE: 2006-03-12 *
12 * AUTHOR: 刘泽军 (BJ0773@gmail.com) *
13 * *
14 *******************************************************************************/
15
16 package cma.micaps.diamond.datatype;
17
18 import cma.common.atmos.*;
19
20 public class DiamondData02 {
21
22 public static int SIZE = 10; //数据长度(个数)
23
24 public String Station; //区站号
25 public double Longitude, Latitude, Altitude; //经度 纬度 拔海高度
26 public int Level; //站点级别
27 public double H, T, T_Td; //高度 温度 温度露点差
28 public double dd, ff; //风向 风速
29
30 public DiamondData02() {//构造函数
31 setDefault();
32 }
33
34 public void setDefault() {//设置数据为缺省值
35 Longitude = Algorithm.DefaultValue;
36 Latitude = Algorithm.DefaultValue;
37 Altitude = Algorithm.DefaultValue;
38 Level = (new Double(Algorithm.DefaultValue)).intValue();
39 H = Algorithm.DefaultValue;
40 T = Algorithm.DefaultValue;
41 T_Td = Algorithm.DefaultValue;
42 dd = (int)Algorithm.DefaultValue;
43 ff = (int)Algorithm.DefaultValue;
44 }
45
46 public String toString() {
47 return(
48 Station + " " +
49 String.valueOf(Longitude) + " " +
50 String.valueOf(Latitude) + " " +
51 String.valueOf(Altitude) + " " +
52 String.valueOf(Level) + " " +
53 String.valueOf(H) + " " +
54 String.valueOf(T) + " " +
55 String.valueOf(T_Td) + " " +
56 String.valueOf(dd) + " " +
57 String.valueOf(ff)
58 );
59 }
60 }