*/
public GridIndex open(String location, InputStream ios) throws IOException {
long start = System.currentTimeMillis();
GridIndex gridIndex = new GridIndex(location);
BufferedReader dataIS = null;
boolean old_index_version = false;
try {
dataIS = new BufferedReader(new InputStreamReader(ios));
// section 1 - global attributes
String centerS = null, sub_centerS = null, table_versionS = null;
while (true) {
String line = dataIS.readLine();
if (line == null || line.length() == 0) { // 0 length/corrupted index
return gridIndex;
}
if (line.startsWith("--")) {
break;
}
int pos = line.indexOf(" = ");
if (pos > 0) {
String key = line.substring(0, pos);
String value = line.substring(pos + 3).replaceAll( " ", "%20" );
gridIndex.addGlobalAttribute(key, value);
if (key.equals("center")) {
centerS = value;
} else if (key.equals("sub_center")) {
sub_centerS = value;
} else if (key.equals("table_version")) {
table_versionS = value;
} else if (key.equals("index_version")) {
old_index_version = ! value.startsWith( currentTextIndexVersion );
}
}
}
// section 2 -- grib records
while (true) {
String line = dataIS.readLine();
if (line == null || line.length() == 0) { // 0 length/corrupted index
return gridIndex;
}
if (line.startsWith("--")) {
break;
}
StringTokenizer stoke = new StringTokenizer(line);
String productType = stoke.nextToken();
String discipline = stoke.nextToken();
String category = stoke.nextToken();
String param = stoke.nextToken();
String typeGenProcess = stoke.nextToken();
String levelType1 = stoke.nextToken();
String levelValue1 = stoke.nextToken();
String levelType2 = stoke.nextToken();
String levelValue2 = stoke.nextToken();
String refTime = stoke.nextToken();
String foreTime = stoke.nextToken();
String gdsKey = stoke.nextToken();
if( old_index_version )
gdsKey = Integer.toString( gdsKey.hashCode() );
String offset1 = stoke.nextToken();
String offset2 = stoke.nextToken();
String decimalScale = stoke.hasMoreTokens()
? stoke.nextToken()
: null;
String bmsExists = stoke.hasMoreTokens()
? stoke.nextToken()
: null;
String center = stoke.hasMoreTokens()
? stoke.nextToken()
: centerS;
String subCenter = stoke.hasMoreTokens()
? stoke.nextToken()
: sub_centerS;
String table = stoke.hasMoreTokens()
? stoke.nextToken()
: table_versionS;
/* GribGridRecord ggr = new GribGridRecord(calendar, dateFormat,
productType,
discipline, category,
param, typeGenProcess, levelType1,
levelValue1, levelType2,
levelValue2, refTime, foreTime,
gdsKey, offset1, offset2,
decimalScale, bmsExists,
center, subCenter, table);
gridIndex.addGridRecord(ggr);
if (debugParse) {
System.out.println(ggr.toString());
} */
}
// section 3+ - GDS
GribGridDefRecord gds;
StringBuilder sb = new StringBuilder();
while (true) {
String line = dataIS.readLine();
if (line == null || line.length() == 0) {
break;
}
if (line.startsWith("--")) {
gds = new GribGridDefRecord(sb.toString());
sb.setLength(0); //reset it
gridIndex.addHorizCoordSys(gds);
continue;
}
int pos = line.indexOf(" = ");
// need to convert long GDSkey to int
if ( line.startsWith(GridDefRecord.GDS_KEY)) {
if( old_index_version ) {
int hc = line.substring(pos + 3).hashCode();
sb.append(line.substring(0, pos)).append("\t").append(Integer.toString(hc));
} else {
sb.append(line.substring(0, pos)).append("\t").append(line.substring(pos + 3));
}
continue;
}
if (pos > 0) {
sb.append("\t").append(line.substring(0, pos)).append("\t").append(line.substring(pos + 3));
}
}
// remove the Grib1 and Grib2 text indexes differences
String gdsStr = sb.toString();
if( ! gdsStr.contains( GridDefRecord.RADIUS_SPHERICAL_EARTH ))
gdsStr = gdsStr.replace( "radius_spherical_earth", GridDefRecord.RADIUS_SPHERICAL_EARTH);
gds = new GribGridDefRecord( gdsStr );
gridIndex.addHorizCoordSys( gds );
//dataIS.close();
if (debugTiming) {
long took = System.currentTimeMillis() - start;
System.out.println(" Index read " + location + " count="
+ gridIndex.getGridCount() + " took=" + took + " msec ");
}
log.debug("Text index read: " + location);
log.debug("Number Records =" + gridIndex.getGridCount() + " at " +
dateFormat.format(Calendar.getInstance().getTime()));
return gridIndex;
} catch (IOException e) {
log.error("open(): reading text index " + "[" + location + "]");