Diamond01.java
001 /*******************************************************************************
002 * *
003 * 定义Micaps的第1类数据的读写 *
004 * *
005 * PACKAGE: cma.micaps.diamond *
006 * FILENAME: Diamond01.java *
007 * LANGUAGE: Java2 v1.4 *
008 * ORIGINAL: c++ (CDiamond01.cpp CDiamond01.h SDiamond01.hpp) *
009 * DESCRIPTION: Micaps diamond 01 data I/O *
010 * CREATE: 2000-03 *
011 * UPDATE: 2006-05-04 22:52:45 改写为 Java 版 *
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 Diamond01 {
027
028 public static double VALUE = Algorithm.DefaultValue; //缺省值
029
030 public DiamondHeader01 Header = null ; //文件头
031 private Vector Data = new Vector () ; //数据
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 DiamondHeader01 getHeader () {
044 return ( Header ) ;
045 }
046
047 public DiamondData01 getData ( int index ) {
048 if ( index >= 0 && index < Data.size () ) {
049 return (( DiamondData01 ) Data.get ( index )) ;
050 }
051 else {
052 return ( new DiamondData01 ()) ;
053 }
054 }
055
056 public DiamondData01 getData ( String name ) {
057 DiamondData01 dataTemp;
058 for ( int i= 0 ;i<Data.size () ;i++ ) {
059 dataTemp = ( DiamondData01 ) Data.get ( i ) ;
060 if ( name.compareTo ( dataTemp.Station ) == 0 ) {
061 return (( DiamondData01 ) Data.get ( i )) ;
062 }
063 }
064 return ( new DiamondData01 ()) ;
065 }
066
067 public int getCount () {
068 return ( Math.min ( Header.Count, Data.size ())) ;
069 } ;
070
071 public boolean setData ( int index, DiamondData01 temp ) {
072 int iExists = - 1 ;
073 DiamondData01 dataTemp;
074 for ( int i= 0 ;i<Data.size () ;i++ ) {
075 dataTemp = ( DiamondData01 ) Data.get ( i ) ;
076 if ( temp.Station.compareTo ( dataTemp.Station ) == 0 ) {
077 iExists = i;
078 i = Data.size () ;
079 }
080 }
081 if ( iExists != - 1 ) {
082 Data.set ( index, temp ) ;
083 return ( true ) ;
084 }
085 return ( false ) ;
086 }
087
088 public boolean setData ( String name, DiamondData01 temp ) {
089 int iExists = - 1 ;
090 DiamondData01 dataTemp;
091 for ( int i= 0 ;i<Data.size () ;i++ ) {
092 dataTemp = ( DiamondData01 ) Data.get ( i ) ;
093 if ( name.compareTo ( dataTemp.Station ) == 0 ) {
094 iExists = i;
095 i = Data.size () ;
096 }
097 }
098 if ( iExists != - 1 ) {
099 Data.add ( iExists, temp ) ;
100 Data.remove ( iExists+ 1 ) ;
101 Header.Count = Data.size () ;
102 return ( true ) ;
103 }
104 return ( false ) ;
105 }
106
107 public void addData ( DiamondData01 temp ) {
108 int iExists = - 1 ;
109 DiamondData01 dataTemp;
110 for ( int i= 0 ;i<Data.size () ;i++ ) {
111 dataTemp = ( DiamondData01 ) Data.get ( i ) ;
112 if ( temp.Station.compareTo ( dataTemp.Station ) == 0 ) {
113 iExists = i;
114 i = Data.size () ;
115 }
116 }
117 if ( iExists != - 1 ) {
118 Data.add ( iExists, temp ) ;
119 Data.remove ( iExists+ 1 ) ;
120 Header.Count = Data.size () ;
121 }
122 else {
123 Data.add ( temp ) ;
124 Header.Count = Data.size () ;
125 }
126 }
127
128 public Diamond01 () {
129 Enabled = false ;
130 }
131
132 public Diamond01 ( String fname ) {
133 Enabled = loadFromFile ( fname ) ;
134 }
135
136 public boolean loadFromFile ( String fname ) {
137
138 Enabled = false ;
139 filename = "" ;
140 Vector vectorData = new Vector () ;
141 File f = new File ( fname ) ;
142 DiamondType dt = new DiamondType () ;
143 if ( !f.exists () || !f.canRead () || !dt.parseFile ( fname, DiamondHeader01.TYPE ) ) {
144 return ( Enabled ) ;
145 }
146 try { //read file header
147
148 InputStreamReader inputStreamReader = new InputStreamReader ( new FileInputStream ( fname ) , "gb2312" ) ; //支持汉字
149 BufferedReader bufferedReader = new BufferedReader ( inputStreamReader ) ;
150
151 String lineString;
152 StringTokenizer st;
153 while ( null != ( lineString = bufferedReader.readLine () ) ) { //System.out.println(lineString);
154 st = new StringTokenizer ( lineString, " /r/n" ) ;
155 while ( st.hasMoreTokens () ) {
156 vectorData.add ( st.nextToken ()) ; //System.out.print((String)vectorData.get(vectorData.size()-1) + " ");
157 } //System.out.println();
158 } //System.out.println(vectorData.size());
159 inputStreamReader.close () ;
160 if ( vectorData.size () < DiamondHeader01.SIZE + DiamondData01.SIZE ||
161 !DiamondHeader01.SYMBOL.equalsIgnoreCase (( String ) vectorData.get ( 0 )) ||
162 DiamondHeader01.TYPE != Integer.parseInt (( String ) vectorData.get ( 1 )) ) {
163 return ( false ) ;
164 }
165 //读文件头
166 int index = 0 ;
167 Header = new DiamondHeader01 () ;
168 Header.Symbol = ( String ) vectorData.get ( 0 ) ; //文件标志 diamond
169 Header.Type = Integer.parseInt (( String ) vectorData.get ( 1 )) ; //文件类型 1
170 Header.Title = ( String ) vectorData.get ( 2 ) ; //屏幕上需显示的内容
171 Header.Year = Integer.parseInt (( String ) vectorData.get ( 3 )) ; //年
172 Header.Month = Integer.parseInt (( String ) vectorData.get ( 4 )) ; //月
173 Header.Day = Integer.parseInt (( String ) vectorData.get ( 5 )) ; //日
174 Header.Hour = Integer.parseInt (( String ) vectorData.get ( 6 )) ; //时次
175 Header.Count = Integer.parseInt (( String ) vectorData.get ( 7 )) ; //总站点数
176
177 //读数据
178 if ( Header.Count > 0 && vectorData.size () >= DiamondHeader01.SIZE + Header.Count*DiamondData01.SIZE ) {
179 for ( int i=DiamondHeader01.SIZE;i<vectorData.size () ;i=i+DiamondData01.SIZE ) {
180 DiamondData01 data = new DiamondData01 () ;
181 data.Station = ( String ) vectorData.get ( i+ 0 ) ; //区站号
182 data.Longitude = Double.parseDouble (( String ) vectorData.get ( i+ 1 )) ; //经度
183 data.Latitude = Double.parseDouble (( String ) vectorData.get ( i+ 2 )) ; //纬度
184 data.Altitude = Double.parseDouble (( String ) vectorData.get ( i+ 3 )) ; //拔海高度
185 data.Level = Integer.parseInt (( String ) vectorData.get ( i+ 4 )) ; //站点级别
186 data.N = Integer.parseInt (( String ) vectorData.get ( i+ 5 )) ; //总云量
187 data.dd = Integer.parseInt (( String ) vectorData.get ( i+ 6 )) ; //风向
188 data.ff = Integer.parseInt (( String ) vectorData.get ( i+ 7 )) ; //风速
189 data.P = Integer.parseInt (( String ) vectorData.get ( i+ 8 )) ; //海平面气压(本站气压)
190 data.P3 = Integer.parseInt (( String ) vectorData.get ( i+ 9 )) ; //3小时变压
191 data.W1 = Integer.parseInt (( String ) vectorData.get ( i+ 10 )) ; //过去天气1
192 data.W2 = Integer.parseInt (( String ) vectorData.get ( i+ 11 )) ; //过去天气2
193 data.R6 = ( String ) vectorData.get ( i+ 12 ) ; //6小时降水
194 data.Cl = Integer.parseInt (( String ) vectorData.get ( i+ 13 )) ; //低云状
195 data.Nh = Integer.parseInt (( String ) vectorData.get ( i+ 14 )) ; //低云量
196 data.h = Integer.parseInt (( String ) vectorData.get ( i+ 15 )) ; //低云高
197 data.Td = Double.parseDouble (( String ) vectorData.get ( i+ 16 )) ; //露点
198 data.vv = Double.parseDouble (( String ) vectorData.get ( i+ 17 )) ; //能见度
199 data.WW = Integer.parseInt (( String ) vectorData.get ( i+ 18 )) ; //现在天气
200 data.T = Double.parseDouble (( String ) vectorData.get ( i+ 19 )) ; //温度
201 data.Cm = Integer.parseInt (( String ) vectorData.get ( i+ 20 )) ; //中云状
202 data.Ch = Integer.parseInt (( String ) vectorData.get ( i+ 21 )) ; //高云状
203 data.Sign1 = Integer.parseInt (( String ) vectorData.get ( i+ 22 )) ; //标志1
204 data.Sign2 = Integer.parseInt (( String ) vectorData.get ( i+ 23 )) ; //标志2
205 data.T24 = Integer.parseInt (( String ) vectorData.get ( i+ 24 )) ; //24小时变温或船向
206 data.P24 = Integer.parseInt (( String ) vectorData.get ( i+ 25 )) ; //24小时变压或船速
207 Data.add ( data ) ;
208 //System.out.println(String.valueOf(i) + " " + String.valueOf(Data.size()));
209 }
210 Enabled = true ;
211 filename = ( new File ( fname )) .getAbsolutePath () ; //System.out.println(filename + " " + String.valueOf(Data.size()));
212 }
213 else {
214 Enabled = false ;
215 filename = "" ;
216 }
217 }
218 catch ( IOException ex ) {
219 System.out.println ( ex.getMessage ()) ;
220 ex.printStackTrace () ;
221 Enabled = false ;
222 filename = "" ;
223 }
224 return ( Enabled ) ;
225 }
226 /*
227 * 保存 Micaps 的第 1 类格式文件
228 * fname - 输出文件名
229 */
230 public boolean saveToFile ( String fname ) {
231 try {
232 File file1 = new File ( fname ) ;
233 if ( !Enabled ||
234 ( file1.exists () && !file1.canWrite ()) ||
235 ( !file1.exists () && !file1.createNewFile ()) ) { //System.out.println("### 232");
236 return false ;
237 }
238 //输出格式
239 int iLen = 10 ;
240 int iDigits = 2 ;
241 String fmt = "0." ;
242 for ( int i= 0 ;i<iDigits;i++ ) {
243 fmt = fmt + "#" ;
244 }
245 DecimalFormat df = new DecimalFormat ( fmt ) ;
246 fmt = "" ;
247 for ( int i= 0 ;i<iLen;i++ ) {
248 fmt = fmt + " " ;
249 }
250
251 OutputStreamWriter osw = new OutputStreamWriter ( new FileOutputStream ( fname ) , "gb2312" ) ;
252 BufferedWriter bw = new BufferedWriter ( osw ) ;
253
254 bw.write ( Header.Symbol + " " + String.valueOf ( Header.Type ) + " " + Header.Title ) ; //diamond 1 title
255 bw.newLine () ;
256 bw.write ( String.valueOf ( Header.Year ) + " " ) ;
257 bw.write ( String.valueOf ( Header.Month ) + " " ) ;
258 bw.write ( String.valueOf ( Header.Day ) + " " ) ;
259 bw.write ( String.valueOf ( Header.Hour ) + " " ) ;
260 bw.write ( String.valueOf ( Header.Count )) ;
261 bw.newLine () ;
262 DiamondData01 data;
263 String line = "" , str = "" , result = "" ;
264 for ( int i= 0 ;i<Header.Count;i++ ) {
265 data = ( DiamondData01 ) Data.get ( i ) ;
266
267 str = fmt + data.Station; //区站号
268 str = str.substring ( str.length () - 5 ) ;
269 line = str;
270 str = fmt + df.format ( data.Longitude ) ; //经度
271 line = line + str.substring ( str.length () - iLen ) ;
272 str = fmt + df.format ( data.Latitude ) ; //纬度
273 line = line + str.substring ( str.length () - iLen ) ;
274 str = fmt + df.format ( data.Altitude ) ; //海拔
275 line = line + str.substring ( str.length () - iLen ) ;
276 str = fmt + df.format ( data.Level ) ; //站点级别
277 line = line + str.substring ( str.length () - iLen ) ;
278 str = fmt + df.format ( data.N ) ; //总云量
279 line = line + str.substring ( str.length () - iLen ) ;
280 str = fmt + df.format ( data.dd ) ; //风向
281 line = line + str.substring ( str.length () - iLen ) ;
282 str = fmt + df.format ( data.ff ) ; //风速
283 line = line + str.substring ( str.length () - iLen ) ;
284 str = fmt + df.format ( data.P ) ; //海平面气压(本站气压)
285 line = line + str.substring ( str.length () - iLen ) ;
286 str = fmt + df.format ( data.P3 ) ; //3小时变压
287 line = line + str.substring ( str.length () - iLen ) ;
288 str = fmt + df.format ( data.W1 ) ; //过去天气1
289 line = line + str.substring ( str.length () - iLen ) ;
290 str = fmt + df.format ( data.W2 ) ; //过去天气2
291 line = line + str.substring ( str.length () - iLen ) ;
292 bw.write ( line ) ;
293 bw.newLine () ;
294
295 str = fmt + data.R6; //6小时降水
296 str = str.substring ( str.length () - 5 ) ;
297 line = str;
298 str = fmt + df.format ( data.Cl ) ; //低云状
299 line = line + str.substring ( str.length () - iLen ) ;
300 str = fmt + df.format ( data.Nh ) ; //低云量
301 line = line + str.substring ( str.length () - iLen ) ;
302 str = fmt + df.format ( data.h ) ; //低云高
303 line = line + str.substring ( str.length () - iLen ) ;
304 str = fmt + df.format ( data.Td ) ; //露点
305 line = line + str.substring ( str.length () - iLen ) ;
306 str = fmt + df.format ( data.vv ) ; //能见度
307 line = line + str.substring ( str.length () - iLen ) ;
308 str = fmt + df.format ( data.WW ) ; //现在天气
309 line = line + str.substring ( str.length () - iLen ) ;
310 str = fmt + df.format ( data.T ) ; //温度
311 line = line + str.substring ( str.length () - iLen ) ;
312 str = fmt + df.format ( data.Cm ) ; //中云状
313 line = line + str.substring ( str.length () - iLen ) ;
314 str = fmt + df.format ( data.Ch ) ; //高云状
315 line = line + str.substring ( str.length () - iLen ) ;
316 str = fmt + df.format ( data.Sign1 ) ; //标志1
317 line = line + str.substring ( str.length () - iLen ) ;
318 str = fmt + df.format ( data.Sign2 ) ; //标志2
319 line = line + str.substring ( str.length () - iLen ) ;
320 str = fmt + df.format ( data.T24 ) ; //24小时变温或船向
321 line = line + str.substring ( str.length () - iLen ) ;
322 str = fmt + df.format ( data.P24 ) ; //24小时变压或船速
323 line = line + str.substring ( str.length () - iLen ) ;
324 bw.write ( line ) ;
325 bw.newLine () ;
326 } ;
327 bw.newLine () ;
328 bw.flush () ;
329 bw.close () ;
330 return ( true ) ;
331 }
332 catch ( Exception ex ) {
333 System.out.println ( ex.getMessage ()) ;
334 ex.printStackTrace () ;
335 return ( false ) ;
336 }
337 }
338 }
DiamondData01.java
001 package cma.micaps.diamond.datatype;
002
003 import java.text.DecimalFormat;
004
005 import cma.common.atmos.*;
006
007 public class DiamondData01 {
008 public static int SIZE = 26 ; //数据长度(个数)
009
010 public String Station; //区站号
011 public double Longitude, Latitude, Altitude; //经度 纬度 海拔高度
012 public int Level, N, dd, ff; //站点级别 总云量 风向 风速
013 public int P, P3; //海平面气压(本站气压) 3小时变压
014 public int W1, W2; //过去天气1 过去天气2
015 public String R6; //6小时降水
016 public int Cl, Nh, h; //低云状 低云量 低云高
017 public double Td, vv; //露点温度 能见度
018 public int WW; //现在天气
019 public double T; //气温
020 public int Cm, Ch; //中云状 高云状
021 //注;从2001-02-28 11时起Diamond01格式增加两个数据
022 public int Sign1, Sign2; //标志1 标志2,若Sign1=1,Sign2=2时T24,P24为24小时变温变压,否则为船向船速
023 public int T24, P24; //24小时变温、变压或船向、船速
024
025 public DiamondData01 () {
026 Station = "59046" ;
027 Longitude = 0 ;
028 Latitude = 0 ;
029 Altitude = 0 ;
030 Level = 0 ;
031 N = 0 ;
032 dd = 0 ;
033 ff = 0 ;
034 P = 0 ;
035 P3 = 0 ;
036 W1 = 0 ;
037 W2 = 0 ;
038 R6 = "0" ;
039 Cl = 0 ;
040 Nh = 0 ;
041 h = 0 ;
042 Td = 0 ;
043 vv = 0.0 ;
044 WW = 0 ;
045 T = 0.0 ;
046 Cm = 0 ;
047 Ch = 0 ;
048 Sign1 = 1 ;
049 Sign2 = 2 ;
050 T24 = 0 ;
051 P24 = 0 ;
052 }
053
054 public void reset ( double value ) { //重设除站点信息之后的其他数据
055 DecimalFormat fmt = new DecimalFormat ( "0" ) ;
056 int intValue = Integer.parseInt ( String.valueOf ( fmt.format ( value ))) ;
057 N = intValue;
058 dd = intValue;
059 ff = intValue;
060 P = intValue;
061 P3 = intValue;
062 W1 = intValue;
063 W2 = intValue;
064 R6 = "0" ;
065 Cl = intValue;
066 Nh = intValue;
067 h = intValue;
068 Td = value;
069 vv = value;
070 WW = intValue;
071 T = value;
072 Cm = intValue;
073 Ch = intValue;
074 Sign1 = 1 ;
075 Sign2 = 2 ;
076 T24 = intValue;
077 P24 = intValue;
078 }
079
080 public String get ( int index, DecimalFormat df, String fmt, String result ) {
081 String value = "" ;
082 switch ( index ) {
083 case 0 : value = this .Station;
084 case 1 : value = df.format ( this .Longitude ) ;
085 case 2 : value = df.format ( this .Latitude ) ;
086 case 3 : value = df.format ( this .Altitude ) ;
087 case 4 : value = df.format ( this .Level ) ;
088 case 5 : value = df.format ( this .N ) ;
089 case 6 : value = df.format ( this .dd ) ;
090 case 7 : value = df.format ( this .ff ) ;
091 case 8 : value = df.format ( this .P ) ;
092 case 9 : value = df.format ( this .P3 ) ;
093 case 10 : value = df.format ( this .W1 ) ;
094 case 11 : value = df.format ( this .W2 ) ;
095 case 12 : value = R6;
096 case 13 : value = df.format ( Cl ) ;
097 case 14 : value = df.format ( Nh ) ;
098 case 15 : value = df.format ( h ) ;
099 case 16 : value = df.format ( Td ) ;
100 case 17 : value = df.format ( vv ) ;
101 case 18 : value = df.format ( WW ) ;
102 case 19 : value = df.format ( T ) ;
103 case 20 : value = df.format ( Cm ) ;
104 case 21 : value = df.format ( Sign1 ) ;
105 case 22 : value = df.format ( Sign2 ) ;
106 case 23 : value = df.format ( T24 ) ;
107 case 24 : value = df.format ( P24 ) ;
108 default : value = "" ;
109 }
110 int iLen = fmt.length () ;
111 result = fmt + value;
112 System.out.println ( result ) ;
113 result = result.substring ( result.length () -iLen ) ;
114 System.out.println ( result ) ;
115 return ( result ) ;
116 }
117
118 }