MessageHeaderBlock.java
001 /**
002 * PACKAGE : cma.gmb.doppler.datatype
003 * FILENAME : MessageHeaderBlock.java
004 * DESCRIPTION : 多普勒雷达产品数据结构
005 * AUTHOR : 刘泽军
006 * EMAIL : BJ0773@gmail.com
007 * Date : 2007-05-21 09:37:34
008 * Update :
009 * Reference : 《NEXRAD LEVEL II数据格式》中文版及英文版
010 */
011
012 package cma.gmb.doppler.datatype;
013
014 import java.io.*;
015 import java.lang.*;
016
017 import cma.common.dataio.*;//需要进行BigEndian转换
018
019 public class MessageHeaderBlock { //信息头数据结构
020
021 /**
022 * 功能:根据自1970-01-01以来的天数计算日期,来源:雷达格式说明
023 * 参数:
024 * days - 自1970-01-01以来的天数
025 * 返回值:
026 * 日期串,yyyymmdd格式
027 */
028 public static String getDate ( int days ) {
029 int JLDAYN = days+ 2440587 ;
030 int L = JLDAYN + 68569 ;
031 int N = 4 * L / 146097 ;
032 L = L - ( 146097 * N + 3 ) / 4 ;
033 int I = 4000 * ( L + 1 ) / 1461001 ;
034 L = L - 1461 * I / 4 + 31 ;
035 int J = 80 * L / 2447 ;
036 int day = L - 2447 * J / 80 ;
037 L = J / 11 ;
038 int month = J + 2 - 12 * L;
039 int year = 100 * ( N - 49 ) + I + L;
040 return (
041 String.valueOf ( year ) +
042 ( month>= 10 ? "" : "0" ) +String.valueOf ( month ) +
043 ( day>= 10 ? "" : "0" ) +String.valueOf ( day )
044 ) ;
045 }
046
047 /**
048 * 功能:根据自00:00:00以来的秒数获得时间
049 * 参数:
050 * seconds - 自1970-01-01以来的天数
051 * 返回值:
052 * 时间串,hhmmss格式
053 */
054 public static String getTime ( int seconds ) {
055 int scs = seconds;
056 int sc = scs % 60 ;
057 int mn = (( scs - scs% 60 ) / 60 ) % 60 ;
058 int hr = (( scs - scs% 3600 ) / 3600 ) % 24 ;
059 return ( ( hr>= 10 ? "" : "0" ) +String.valueOf ( hr ) +
060 ( mn>= 10 ? "" : "0" ) +String.valueOf ( mn ) +
061 ( sc>= 10 ? "" : "0" ) +String.valueOf ( sc )
062 ) ;
063 }
064
065 public static int SIZE = 18 ;
066
067 public short MessageCode; //0~1 16~109表示产品
068 public short MessageDate; //2~3 传输日期,1/1/1970以来的Julian日期
069 public int MessageTime; //4~7 传输时间,GMT时间,0~86399秒
070 public int MessageLength; //8~11 数据长度(字节数)
071 public short SourceID; //12~13 RDA的ID号 一般为区站号的后三位
072 public short DestinationID; //14~15 数据接受ID号 同上
073 public short BlocksNumber; //16~17 数据块个数
074
075 /**
076 * 功能:构造函数
077 * 参数:
078 * 无
079 * 返回:
080 * 无
081 */
082 public MessageHeaderBlock () {
083 }
084
085 /**
086 * 功能:从文件中读取数据,并进行BigEndian转换
087 * 参数:
088 * raf - 随机访问的文件对象
089 * 返回:
090 * 是否成功
091 */
092 public boolean read ( RandomAccessFile raf ) {
093 try {
094 byte [] buf = new byte [ MessageHeaderBlock.SIZE ] ;
095 int len = raf.read ( buf ) ;
096 return ( len == MessageHeaderBlock.SIZE ? parse ( buf, 0 ) : false ) ;
097 }
098 catch ( Exception ex ) {
099 return ( false ) ;
100 }
101 }
102
103 /**
104 * 功能:从输入流文件中读取数据,并进行BigEndian转换
105 * 参数:
106 * in - InputStream对象
107 * 返回:
108 * 是否成功
109 */
110 public boolean read ( InputStream in ) {
111 try {
112 byte [] buf = new byte [ MessageHeaderBlock.SIZE ] ;
113 int len = in.read ( buf ) ;
114 return ( len == MessageHeaderBlock.SIZE ? parse ( buf, 0 ) : false ) ;
115 }
116 catch ( Exception ex ) {
117 return ( false ) ;
118 }
119 }
120
121 /**
122 * 功能:从缓冲区中读数据
123 * (在外部方法中,一次性读入所有数据,然后逐类分析数据)
124 * 参数:
125 * buf - 缓冲数据
126 * index - 偏移
127 * 返回:
128 * 正确读出的数据字节数
129 */
130 public int read ( byte [] buf, int index ) {
131 return ( parse ( buf, index ) ?MessageHeaderBlock.SIZE: 0 ) ;
132 }
133
134 /**
135 * 功能:从缓冲区中分析出数据
136 * 参数:
137 * buf - 缓冲数据
138 * 返回:
139 * 是否成功
140 */
141 public boolean parse ( byte [] buf ) {
142 return ( parse ( buf, 0 )) ;
143 }
144
145 /**
146 * 功能:从缓冲区中分析出数据
147 * 参数:
148 * buf - 缓冲数据
149 * index - 偏移
150 * 返回:
151 * 是否成功
152 */
153 public boolean parse ( byte [] buf, int index ) {
154 if ( buf.length < index + MessageHeaderBlock.SIZE ) {
155 return ( false ) ;
156 }
157 MessageCode = DataConverterBE.getShort ( buf, index+ 0 ) ;
158 MessageDate = DataConverterBE.getShort ( buf, index+ 2 ) ;
159 MessageTime = DataConverterBE.getInt ( buf, index+ 4 ) ;
160 MessageLength = DataConverterBE.getInt ( buf, index+ 8 ) ;
161 SourceID = DataConverterBE.getShort ( buf, index+ 12 ) ;
162 DestinationID = DataConverterBE.getShort ( buf, index+ 14 ) ;
163 BlocksNumber = DataConverterBE.getShort ( buf, index+ 16 ) ;
164 return ( true ) ;
165 }
166
167 /**
168 * 功能:获得数据信息
169 * 参数:
170 * 无
171 * 返回:
172 * 数据信息
173 */
174 public String info () {
175 String msg =
176 "/nMessageHeaderBlock.SIZE = " + String.valueOf ( MessageHeaderBlock.SIZE ) +
177 "/n MessageCode = " + String.valueOf ( MessageCode ) +
178 "/n MessageDate = " + String.valueOf ( MessageDate ) + "(" + getDate (( int ) MessageDate ) + ")" +
179 "/n MessageTime = " + String.valueOf ( MessageTime ) + "(" + getTime (( int ) MessageTime ) + ")" +
180 "/n MessageLength = " + String.valueOf ( MessageLength ) +
181 "/n SourceID = " + String.valueOf ( SourceID ) +
182 "/n DestinationID = " + String.valueOf ( DestinationID ) +
183 "/n BlocksNumber = " + String.valueOf ( BlocksNumber ) +
184 "/n" ;
185 return ( msg ) ;
186 }
187
188 /**
189 * 功能:打印数据,主要用于测试
190 * 参数:
191 * 无
192 * 返回:
193 * 无
194 */
195 public void print () {
196 System.out.println ( info ()) ;
197 }
198
199 }