-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModisSensor.java
executable file
·443 lines (386 loc) · 16.2 KB
/
ModisSensor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// ModisSensor.java defines the MODIS sensor details.
//---------------------------------------------------
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Vector;
public class ModisSensor extends Sensor
{
private DecimalFormat twoDigitFormat; // two digit number formatter
private static final int[] resolutions = {10000,5000};
private static final int[] borderX = {-4,4,4,-4};
// 4 corner highlight X border offsets
private static final int[] borderY = {-4,-4,4,4};
// 4 corner highlight Y border offsets
private static final Color borderColor = Color.YELLOW;
// color to use for the highlight border
private LatLongToModisTile tileConverter;
// for converting tile numbers to upper-left coordinates
private static final int[] lineOffsets = {0, 0, 239, 239};
private static final int[] sampOffsets = {0, 239, 239, 0};
// constant line and sample offsets to the image data
// Constructor
ModisSensor
(
imgViewer applet, // I: applet reference
String sensorName, // I: name to use for the sensor
String inventoryDir, // I: name of directory for the data
String datasetName, // I: data set name for ordering
String cgiDatasetName, // I: data set name for the cgi scripts
boolean hasCloudCover // I: flag to indicate product has cloud cover
)
{
super(applet,sensorName,inventoryDir,datasetName,"showModisBrowse.cgi",
"showModisMetadata.cgi","NASA_small.gif",
"http://lpdaac.usgs.gov/products/modis_overview",
"https://lpdaac.usgs.gov/products/modis_products_table",
null,"Enter a MODIS scene ID",resolutions,
borderX,borderY,borderColor);
// set the dataset name for the show browse/metadata cgi scripts so
// the datasets with two browse can be displayed
this.cgiDatasetName = cgiDatasetName;
// Set up formatter for creating file names
twoDigitFormat = new DecimalFormat ("00");
// set the flags for the optional sensor features available
isOrderable = true;
hasColRowInSceneID = true;
hasConstantOffsets = true;
hasUpperLeftInToc = false;
hasSecondaryIDMetadata = true;
hasMultipleBrowse = true;
hasCustomSceneInfoLine = true;
hasDataVersions = true;
hasGeographicBumper = false;
this.hasCloudCover = hasCloudCover;
// do not factor in cloud cover when picking default scenes to display
useCloudCoverForDefaultScenes = false;
// set the default projection code for this sensor
defaultProjectionCode = CreateProjection.SINUSOIDAL;
// use the sinusoidal locator map
locatorMap = LocatorMap.SINUSOIDAL_MAP;
// setup the navigation model
navModel = new ModisTileModel();
// setup the object to convert tile numbers to upper-left coordinates
tileConverter = new LatLongToModisTile();
// read the data versions from the server
dataVersions = getDataVersions();
// place the logo in the lower right corner
logoLocation = Sensor.LOGO_LOWER_RIGHT;
}
// getDataVersions reads all of the available versions for this sensor
// from a file on the server.
//-----------------------------------------------------------------------
private String[] getDataVersions()
{
BufferedReader versionData = null; // stream for reading the file
ArrayList dataVersionList = new ArrayList();
try
{
// open the version data file
URL versionURL = new URL(applet.getCodeBase(),"modis/versions.txt");
versionData = new BufferedReader(
new InputStreamReader(versionURL.openStream()));
// read the version numbers from the file (they are sorted)
String dataLine;
while ((dataLine = versionData.readLine()) != null)
{
dataVersionList.add(dataLine.trim());
}
// close the data stream
versionData.close();
versionData = null;
}
catch (Exception e)
{
// error reading or parsing the file
dataVersionList.add("ERROR");
// make sure the stream is closed
if (versionData != null)
{
try {versionData.close();} catch (Exception e1){};
}
}
dataVersionList.trimToSize();
String[] dataVersions = new String[dataVersionList.size()];
for (int i = 0; i < dataVersionList.size(); i++)
{
dataVersions[i] = dataVersionList.get(i).toString();
}
return dataVersions;
}
// method to populate the scene metadata fields that are constants or
// can be calculated from other fields
//-------------------------------------------------------------------
public void completeMetadata(Metadata scene)
{
// set the line and sample offsets
scene.lineOffset = lineOffsets;
scene.sampOffset = sampOffsets;
// Finding the 3rd and 4th "." and pulling only the version number out
// to create the orderID that needs to be passed in the URL.
int index = scene.secondaryID.indexOf('.');
int index2 = scene.secondaryID.indexOf('.',index + 1);
int index3 = scene.secondaryID.indexOf('.',index2 + 1);
int index4 = scene.secondaryID.indexOf('.',index3 + 2);
scene.dataVersion = scene.secondaryID.substring(index3+1,index4);
scene.entityID = "." + scene.dataVersion + ":" + scene.entityID;
// fill in the upper left X/Y based on the tile coordinates
ModisTile tile = new ModisTile(scene.gridCol, scene.gridRow);
Point coordinate = tileConverter.tileToCoordinate(tile,false);
scene.ulX = coordinate.x;
scene.ulY = coordinate.y;
}
// method to return an image file name for a given metadata reference and
// resolution
// Returns: full file name, including directory
//-----------------------------------------------------------------------
public String makeImageName
(
Metadata scene, // I: metadata for scene
int resolution // I: resolution requested in meters
)
{
StringBuffer imgName;
// build the image name, starting with the TOC directory and adding
// the items that make up the file name
imgName = new StringBuffer(getCellDirectory(scene.gridCol,
scene.gridRow));
imgName.append("/y");
imgName.append(scene.year);
imgName.append("/");
if (resolution != 5000)
{
// the image is named using the scene ID up to the production date,
// so get the scene ID up to the production date
int index = scene.secondaryID.lastIndexOf('.');
int index2 = scene.secondaryID.lastIndexOf('.',index - 1);
imgName.append(scene.secondaryID.substring(0,index2));
// append the browse number
if (scene.browseNumber > 0)
{
imgName.append("_");
imgName.append(scene.browseNumber);
}
imgName.append(".");
// convert the resolution to kilometers and add it to the file name
imgName.append((resolution/1000));
imgName.append("km.jpg");
}
else
{
// for the 5km browse, just replace the "hdf" at the end with "jpg"
// and insert the browse number
int index = scene.secondaryID.lastIndexOf('.');
imgName.append(scene.secondaryID.substring(0,index));
if (scene.browseNumber > 0)
{
imgName.append("_");
imgName.append(scene.browseNumber);
}
imgName.append(".jpg");
}
return imgName.toString();
}
// method to return the directory name for a given cell. Note this
// implementation assumes a path/row naming scene, but individual
// sensors may override this if needed.
//-----------------------------------------------------------------
public String getCellDirectory
(
int gridCol, // I: grid column
int gridRow // I: grid row
)
{
StringBuffer cellDir = new StringBuffer(getSensorDirectory());
cellDir.append("/h");
cellDir.append(twoDigitFormat.format(gridCol));
cellDir.append("/v");
cellDir.append(twoDigitFormat.format(gridRow));
return cellDir.toString();
}
// method to return the resolution (in meters) used to define the offsets
// in the TOC for this sensor
//-----------------------------------------------------------------------
public double getOffsetResolution()
{
// use the 5km resolution for offsets
int numRes = resolutions.length;
return getActualResolution(resolutions[numRes - 1]);
}
// method to convert the current advertised resolution into the actual
// resolution. This was created since MODIS pixel size is advertised to
// be 1000 meters, but it is roughly 926 meters.
//----------------------------------------------------------------------
public double getActualResolution(int resolution)
{
return ((double)resolution) * 926.625433/1000.0;
}
// method to return the number of cells to display for a given resolution.
// Sensor.SINGLE_SCENE is returned if only one scene should be displayed.
//------------------------------------------------------------------------
public int getNumCellsAtResolution
(
int resolution // I: resolution in meters
)
{
if (resolution == resolutions[0])
return applet.md.getMosaicSize();
else
return SINGLE_SCENE;
}
// method to return a scene filter that is compatible with this sensor.
// Returns a SceneFilter object.
//---------------------------------------------------------------------
public SceneFilter getSceneFilter
(
MosaicData md, // I: the mosaic data object
TOC[] tocs // I: array of TOC's to build the scene filter for
)
{
return new ModisSceneFilter(md);
}
// method to build the URL for ordering a list of scenes. ASTER/MODIS
// orders go to the EE shopping cart.
//------------------------------------------------------------------
public String buildOrderURL
(
Metadata[] sceneList // I: array of scenes to order
)
{
Vector idList = new Vector();
String orderUrl = new String();
if (sceneList.length < 1)
{
return null;
}
for (int i = 0; i < sceneList.length; i++)
{
// add the entity id without the version number
// (entityID formatted as .VVV: so start at position 5)
idList.add(sceneList[i].entityID.substring(5));
}
orderUrl = EarthExplorer.buildShoppingCartUrl(this.datasetName, idList);
return orderUrl;
}
// method to return a nominal scene size in meters
//------------------------------------------------
public Dimension getNominalSceneSize()
{
return new Dimension(1111951,1111951);
}
// method to return the starting year for the sensor
//--------------------------------------------------
public int getStartingYear()
{
return 2000;
}
// method to return the ending year for the sensor (or -1 if collections
// continue)
//----------------------------------------------------------------------
public int getEndingYear()
{
return -1;
}
// method to return the estimated size (in bytes) of an image file at the
// indicated resolution
//-----------------------------------------------------------------------
public int getImageFileSize(int resolution)
{
if (resolution == 10000)
return 3500;
else if (resolution == 5000)
return 55000;
else
return 1500;
}
// method to return the custom scene info line
// ----------------------------------------------------------------------
public String getCustomSceneInfo(Metadata scene)
{
// if this sensor has cloud cover, SceneInfo adds CC: AND Date in line2
// otherwise, SceneInfo does not add a second line because this
// sensor hasCustomSceneInfoLine, so we need to include Date
// either way, add the Granule ID and Browse Number
// (Metadata has browseNumber from TOC, not BrowseType from .meta)
if (hasCloudCover)
{
// we just have 1 line worth of display area, Date already done
if (scene == null)
return "Granule ID: #";
else
return "Granule ID: " + scene.entityID.substring(5)
+ " # " + scene.browseNumber;
}
else
{
// line with Date hasn't been added, so add the Date
if (scene == null)
return "Date:\nGranule ID: #";
return "Date: " + scene.getDateString()
+ "\nGranule ID: " + scene.entityID.substring(5)
+ " # " + scene.browseNumber;
}
}
// method to convert an entity ID from a scene's metadata to the value
// medium length for display in the scene list in the left panel of the
// applet. The production timestamp and ".hdf" are dropped from the end.
//-----------------------------------------------------------------------
String buildMediumEntityID(Metadata scene)
{
int index = scene.secondaryID.lastIndexOf('.');
int index2 = scene.secondaryID.lastIndexOf('.',index - 1);
return (scene.secondaryID.substring(0,index2));
}
// method to convert an entity ID from a scene's metadata to the value
// short enough to display. The dataset name is dropped from the front
// of the secondary ID and the production timestamp and ".hdf" are
// dropped from the end.
//-------------------------------------------------------------------------
String buildShortEntityID(Metadata scene)
{
int index1 = scene.secondaryID.indexOf('.') + 1;
int index = scene.secondaryID.lastIndexOf('.');
int index2 = scene.secondaryID.lastIndexOf('.',index - 1);
return (scene.secondaryID.substring(index1,index2));
}
// method to convert an entity ID from a scene's metadata to the
// value to display.
//--------------------------------------------------------------
String buildEntityID(Metadata scene)
{
return (scene.secondaryID);
}
// method to return the files to download for a given scene when the
// user downloads metadata and browse. Returns an array of filenames where
// the source and destination file names are paired up. If the source
// browse name is not known, null is returned in that array entry.
//-------------------------------------------------------------------------
public String[] getFilesForScene(Metadata scene)
{
String[] files = new String[4];
int index = scene.secondaryID.lastIndexOf('.');
StringBuffer baseFilename = new
StringBuffer(scene.secondaryID.substring(0, index));
if (scene.browseNumber > 0)
{
baseFilename.append('_');
baseFilename.append(scene.browseNumber);
}
String basename = getCellDirectory(scene.gridCol, scene.gridRow)
+ "/y" + scene.year + "/" + baseFilename.toString();
// set the metadata source and destination names
files[0] = basename + ".meta";
files[1] = baseFilename.toString() + ".meta";
// set the browse image source and destination names
files[2] = basename + ".jpg";
files[3] = baseFilename.toString() + ".jpg";
return files;
}
}