gradsCtl.java
01 /**
02 * PACKAGE : cma.common.grads
03 * DESCRIPTION :
04 * AUTHOR : 刘泽军
05 * EMAIL : BJ0773@gmail.com
06 * Date : 2004-12-01
07 * Time : 22:39:59
08 * Relation : gradsDim.java, gradsCtl.java, gradsVar.java, grads.java
09 * Compile : javac -d . gradsDim.java gradsCtl.java gradsVar.java grads.java
10 */
11
12 package cma.common.grads;
13
14 import java.util.Vector;
15
16 public class gradsCtl {
17 public String dset;
18 public String index; //暂未支持
19 public String dtype; //暂未支持
20 public String options;
21 public String title;
22 public String undef;
23
24 public String pdef; //暂未支持
25
26 public gradsDim xdef;
27 public gradsDim ydef;
28 public gradsDim zdef;
29 public gradsDim tdef;
30
31 public Vector vars; //Object of class gradsVar
32 public gradsCtl () {
33 xdef = new gradsDim () ;
34 ydef = new gradsDim () ;
35 zdef = new gradsDim () ;
36 tdef = new gradsDim () ;
37 vars = new Vector () ;
38 }
39 }
gradsVar.java
01 /**
02 * PACKAGE : cma.common.grads
03 * DESCRIPTION :
04 * AUTHOR : 刘泽军
05 * EMAIL : BJ0773@gmail.com
06 * Date : 2004-12-01
07 * Time : 22:40:26
08 * Relation : gradsDim.java, gradsCtl.java, gradsVar.java, grads.java
09 * Compile : javac -d . gradsDim.java gradsCtl.java gradsVar.java grads.java
10 */
11
12 package cma.common.grads;
13
14 public class gradsVar {
15 public String name = "" ;
16 public int levels = 0 ;
17 public String reserve = "" ;
18 public String contents = "" ;
19 public String unicode = "" ;
20 public gradsVar () {
21 }
22 public String convertToString () {
23 return ( name + " " + String.valueOf ( levels ) + " " + reserve + " " + contents ) ;
24 }
25 }
gradsDim.java
001 /**
002 * PACKAGE : cma.common.grads
003 * DESCRIPTION :
004 * AUTHOR : 刘泽军
005 * EMAIL : BJ0773@gmail.com
006 * Date : 2004-12-01
007 * Time : 22:40:19
008 * Relation : gradsDim.java, gradsCtl.java, gradsVar.java, grads.java
009 * Compile : javac -d . gradsDim.java gradsCtl.java gradsVar.java grads.java
010 */
011
012 package cma.common.grads;
013
014 public class gradsDim {
015 public String symbol = "" ;
016 public int count = 0 ;
017 public String type = "" ;
018 public String start = "" ;
019 public String increment = "" ;
020 public String value [] = { "" } ;
021 public gradsDim () {
022 }
023 public boolean extractValue ( String str []) {
024 if ( str.length < 5 ) {
025 return ( false ) ;
026 }
027 symbol = str [ 0 ] ;
028 count = Integer.parseInt ( str [ 1 ]) ;
029 value = new String [ count ] ;
030 for ( int i= 0 ;i<count;i++ ) {
031 value [ i ] = "" ;
032 }
033 type = str [ 2 ] ;
034 try {
035 if ( 0 == symbol.compareToIgnoreCase ( "tdef" ) ) {
036 start = str [ 3 ] ;
037 increment = str [ 4 ] ;
038 int year=- 1 , month=- 1 , day= 1 , hour= 0 , min= 0 ;
039 String monStr [] = { "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" } ;
040 String mon = "" ;
041 String tmp = str [ 3 ] .substring ( 0 ,str [ 3 ] .length () - 7 ) .toUpperCase () ;
042 year = Integer.parseInt ( str [ 3 ] .substring ( str [ 3 ] .length () - 4 )) ;
043 mon = str [ 3 ] .substring ( str [ 3 ] .length () - 7 ,str [ 3 ] .length () - 4 ) ;
044 for ( int m= 1 ;m<= 12 ;m++ ) {
045 if ( 0 == monStr [ m- 1 ] .compareToIgnoreCase ( mon ) ) {
046 month = m;
047 start = str [ 3 ] .replaceAll ( mon, monStr [ m- 1 ]) ;
048 }
049 }
050 if ( 0 > year || 9999 <= year || 1 > month || 12 < month ) {
051 return ( false ) ;
052 }
053 try {
054 if ( - 1 != tmp.indexOf ( "Z" ) ) {
055 day = Integer.parseInt ( tmp.substring ( tmp.indexOf ( "Z" ) + 1 )) ;
056 tmp = tmp.substring ( 0 , tmp.indexOf ( "Z" )) ;
057 }
058 if ( - 1 != tmp.indexOf ( ":" ) ) {
059 hour = Integer.parseInt ( tmp.substring ( 0 ,tmp.indexOf ( ":" ))) ;
060 min = Integer.parseInt ( tmp.substring ( tmp.indexOf ( ":" ) + 1 )) ;
061 }
062 else {
063 hour = Integer.parseInt ( tmp ) ;
064 }
065 }
066 catch ( Exception e ) {
067 }
068 value [ 0 ] =
069 String.valueOf ( year + 10000 ) .substring ( 1 ) +
070 String.valueOf ( month+ 100 ) .substring ( 1 ) +
071 String.valueOf ( day + 100 ) .substring ( 1 ) +
072 String.valueOf ( hour + 100 ) .substring ( 1 ) ;
073 int intIncrement = 0 ;
074 String suffix = "" ;
075 if ( str [ 4 ] .toLowerCase () .endsWith ( "mn" ) ) {
076 intIncrement = Integer.parseInt ( str [ 4 ] .substring ( 0 ,str [ 4 ] .toLowerCase () .indexOf ( "mn" ))) ;
077 if ( intIncrement >= 60 ) {
078 intIncrement = intIncrement / 60 ;
079 suffix = "hr" ;
080 }
081 else {
082 suffix = "mn" ;
083 }
084 }
085 else if ( str [ 4 ] .toLowerCase () .endsWith ( "hr" ) ) {
086 intIncrement = Integer.parseInt ( str [ 4 ] .substring ( 0 ,str [ 4 ] .toLowerCase () .indexOf ( "hr" ))) ;
087 if ( intIncrement >= 24 ) {
088 intIncrement = intIncrement / 24 ;
089 suffix = "dy" ;
090 }
091 else {
092 suffix = "hr" ;
093 }
094 }
095 else if ( str [ 4 ] .toLowerCase () .endsWith ( "dy" ) ) {
096 intIncrement = Integer.parseInt ( str [ 4 ] .substring ( 0 ,str [ 4 ] .toLowerCase () .indexOf ( "dy" ))) ;
097 suffix = "dy" ;
098 }
099 else if ( str [ 4 ] .toLowerCase () .endsWith ( "mo" ) ) {
100 intIncrement = Integer.parseInt ( str [ 4 ] .substring ( 0 ,str [ 4 ] .toLowerCase () .indexOf ( "mo" ))) ;
101 suffix = "mo" ;
102 }
103 else if ( str [ 4 ] .toLowerCase () .endsWith ( "yr" ) ) {
104 intIncrement = Integer.parseInt ( str [ 4 ] .substring ( 0 ,str [ 4 ] .toLowerCase () .indexOf ( "yr" ))) ;
105 suffix = "yr" ;
106 }
107 else {
108 try {
109 intIncrement = Integer.parseInt ( str [ 4 ]) ;
110 suffix = "" ;
111 }
112 catch ( Exception e ) {
113 }
114 }
115 increment = String.valueOf ( intIncrement ) + suffix;
116 for ( int j= 0 ;j<count;j++ ) {
117 value [ j ] = String.valueOf ( 1000000 +j*intIncrement ) .substring ( 1 ) ;
118 while ( value [ j ] .length () > 3 && value [ j ] .startsWith ( "0" ) ) {
119 value [ j ] = value [ j ] .substring ( 1 ) ;
120 }
121 value [ j ] = value [ j ] + suffix;
122 }
123 if ( 1 == count ) {
124 value [ 0 ] = "anl" ;
125 }
126 }
127 else { //xdef ydef zdef
128 start = str [ 3 ] ;
129 if ( 0 == type.compareToIgnoreCase ( "LINEAR" ) ) {
130 increment = String.valueOf ( Float.parseFloat ( str [ 4 ])) ;
131 for ( int j= 0 ;j<count;j++ ) {
132 value [ j ] = start + Float.parseFloat ( increment ) * j;
133 }
134 }
135 else if ( 0 == type.compareToIgnoreCase ( "LEVELS" ) ) {
136 if ( str.length - 3 != count ) {
137 return ( false ) ;
138 }
139 for ( int j= 0 ;j<count;j++ ) {
140 value [ j ] = String.valueOf ( Float.parseFloat ( str [ 3 +j ])) ;
141 }
142 }
143 else {
144 return ( false ) ;
145 }
146 if ( symbol.equalsIgnoreCase ( "zdef" ) ) {
147 //System.out.println(symbol + " ====================== " + String.valueOf(count));
148 for ( int i= 0 ;i<count;i++ ) {
149 //System.out.println("LEVEL = " + value[i]);
150 if ( - 1 != value [ i ] .indexOf ( "." ) && !value [ i ] .endsWith ( "." ) &&
151 0 == Integer.parseInt ( value [ i ] .substring ( value [ i ] .indexOf ( "." ) + 1 )) ) {
152 value [ i ] = value [ i ] .substring ( 0 ,value [ i ] .indexOf ( "." )) ;
153 //System.out.println(" ====>>>> " + value[i]);
154 }
155 }
156 }
157 }
158 return ( true ) ;
159 }
160 catch ( Exception e ) {
161 return ( false ) ;
162 }
163 }
164 public String convertToString () {
165 String s = symbol + " " + String.valueOf ( count ) + " " + type;
166 if ( 0 == type.compareToIgnoreCase ( "levels" ) ) {
167 for ( int i= 0 ;i<count;i++ ) {
168 s = s + "/n" + value [ i ] ;
169 }
170 }
171 else {
172 s = s + " " + start + " " + increment;
173 }
174 /*
175 if( 0 == symbol.compareToIgnoreCase("tdef") ) {
176 String s1 = "";
177 for(int i=0;i<count;i++) {
178 s1 = s1 + "/n" + value[i];
179 }
180 s = s + s1;
181 }
182 */
183 return ( s ) ;
184 }
185 public static String newDate ( String yyyymmddhh, int incHours ) {
186 /*
187 功能:计算yyyymmddhh加上incHours个小时后对应的日期,仍以yyyymmddhh的形式返回,出错时返回YYYYMMDDHH。
188 作者:刘泽军
189 更新:2004-11-24
190 */
191 if ( 10 != yyyymmddhh.length () ) {
192 return ( "YYYYMMDDHH" ) ;
193 }
194 int yyyy, mm, dd, hh; //开始日期
195 int yyyy1, mm1, dd1, hh1; //结束日期
196 try {
197 yyyy = Integer.parseInt ( yyyymmddhh.substring ( 0 , 4 )) ;
198 mm = Integer.parseInt ( yyyymmddhh.substring ( 4 , 6 )) ;
199 dd = Integer.parseInt ( yyyymmddhh.substring ( 6 , 8 )) ;
200 hh = Integer.parseInt ( yyyymmddhh.substring ( 8 )) ;
201 }
202 catch ( Exception ex ) {
203 return ( "YYYYMMDDHH" ) ;
204 }
205 yyyy1 = yyyy;
206 mm1 = mm;
207 dd1 = dd;
208 hh1 = ( hh+incHours ) % 24 ;
209 int days = ( hh + incHours - hh1 ) / 24 ;
210 if ( hh1 < 0 ) {
211 days = days - 1 ;
212 hh1 = 24 + hh1;
213 }
214 // 12 1 2 3 4 5 6 7 8 9 10 11 12
215 int daysOfMonth [] = { 31 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ;
216
217 if ( ( yyyy % 4 ) == 0 && ( ( yyyy % 100 ) != 0 || ( yyyy % 400 ) == 0 ) ) { //闰年
218 daysOfMonth [ 2 ] = 29 ;
219 }
220 if ( yyyy < 0 || yyyy + days < 0 ||
221 yyyy >= 9999 || yyyy + days >= 9999 ||
222 mm < 1 || mm > 12 ||
223 dd < 1 || dd > daysOfMonth [ mm ] ) {
224 return ( "YYYYMMDDHH" ) ;
225 }
226
227 if ( days >= 0 ) {
228 for ( int i= 0 ;i<days;i++ ) {
229 if ( dd1 < daysOfMonth [ mm1 ] ) {
230 dd1 = dd1 + 1 ;
231 }
232 else if ( mm1 < 12 ) {
233 mm1 = mm1 + 1 ;
234 dd1 = 1 ;
235 }
236 else {
237 yyyy1 = yyyy1 + 1 ;
238 mm1 = 1 ;
239 dd1 = 1 ;
240 if ( ( yyyy1 % 4 ) == 0 && ( ( yyyy1 % 100 ) != 0 || ( yyyy1 % 400 ) == 0 ) ) { //闰年
241 daysOfMonth [ 2 ] = 29 ;
242 }
243 else {
244 daysOfMonth [ 2 ] = 28 ;
245 }
246 }
247 }
248 }
249 else {
250 for ( int i=days;i< 0 ;i++ ) {
251 if ( dd1 > 1 ) {
252 dd1 = dd1 - 1 ;
253 }
254 else if ( mm1 > 1 ) {
255 mm1 = mm1 - 1 ;
256 dd1 = daysOfMonth [ mm1 ] ;
257 }
258 else {
259 yyyy1 = yyyy1 - 1 ;
260 mm1 = 12 ;
261 dd1 = 31 ;
262 if ( ( yyyy1 % 4 ) == 0 && ( ( yyyy1 % 100 ) != 0 || ( yyyy1 % 400 ) == 0 ) ) { //闰年
263 daysOfMonth [ 2 ] = 29 ;
264 }
265 else {
266 daysOfMonth [ 2 ] = 28 ;
267 }
268 }
269 }
270 }
271 return (
272 String.valueOf ( 10000 +yyyy1 ) .substring ( 1 ) +
273 String.valueOf ( 100 +mm1 ) .substring ( 1 ) +
274 String.valueOf ( 100 +dd1 ) .substring ( 1 ) +
275 String.valueOf ( 100 +hh1 ) .substring ( 1 )
276 ) ;
277 }
278 }