-
Notifications
You must be signed in to change notification settings - Fork 23
/
SpectrumIO.java
810 lines (722 loc) · 26 KB
/
SpectrumIO.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/*
* Copyright (C) 2004 Central Laboratory of the Research Councils
* Copyright (C) 2008-2009 Science and Technology Facilities Council
*
* History:
* 13-SEP-2004 (Peter W. Draper):
* Original version.
*/
package uk.ac.starlink.splat.iface;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Vector;
import uk.ac.starlink.ast.FrameSet;
import uk.ac.starlink.splat.data.SpecData;
import uk.ac.starlink.splat.data.SpecDataFactory;
import uk.ac.starlink.splat.util.SplatException;
import uk.ac.starlink.splat.util.UnitUtilities;
import uk.ac.starlink.util.gui.ErrorDialog;
/**
* Load a list of spectra into the {@link SplatBrowser}, or save a spectrum
* using a thread to avoid blocking of the UI. A single instance of this class
* exists so it is only possible to load or save one set of files at a time.
* <p>
* Since the NDF library also caches AST references (to the WCS FrameSet) a
* further constraint is that loading and saving must happen in the same
* thread (other operations that touch the cached WCS should also happen in
* this thread), so all spectra are dealt with using a single thread.
* <p>
* To avoid this problem it would be necessary to free the NDF cache
* AST reference directly. Exported AST references are unlocked so using
* those in other threads should be OK.
* <p>
* When loading spectra you can add further spectra which will join the queue
* of spectra to load. When a list of spectra takes a long time to load a
* progress dialog window will be shown displaying the number of spectra
* remaining to load and the currently loading spectrum. Cancelling this
* window clears the queue of spectra and further loading, after the current
* spectrum completes, will not be done.
*
* @author Peter W. Draper
* @version $Id$
*/
public class SpectrumIO
{
// XXX deficiencies: cannot stop during load of a spectrum. This would
// require a lot of work catching the problems with interrupting
// incomplete objects.
/**
* Private constructor so that only one instance can exist.
*/
private SpectrumIO()
{
loadThread = new Loader();
loadThread.start();
}
/**
* Instance of this class.
*/
private static SpectrumIO instance = null;
/**
* Return the reference to the single instance of SpectrumIO.
*/
public static synchronized SpectrumIO getInstance()
{
if ( instance == null ) {
instance = new SpectrumIO();
}
return instance;
}
/**
* The global list of spectra and plots.
*/
protected GlobalSpecPlotList globalList = GlobalSpecPlotList.getInstance();
/**
* The list of spectra to load. These are characterised as Props
* instances.
*/
private Vector queue = new Vector();
/**
* The Thread that the loading or saving is actually performed in.
*/
private Loader loadThread = null;
/**
* The ProgressFrame. This appears to denote that something is happening.
*/
private ProgressFrame progressFrame =
new ProgressFrame( "Loading spectra..." );
/**
* The SplatBrowser to notify about any spectra that are loaded.
*/
private SplatBrowser browser = null;
/**
* Whether the spectra should also be displayed.
*/
private boolean display = true;
/**
* Last spectrum.
*/
private Props lastSpectrum = null;
/**
* Index of saved file.
*/
private final int globalIndex = 0;
/**
* Object to receive callbacks whenever the queue of files to load
* is cleared.
*/
private Watch watcher = null;
/**
* Load an array of spectra whose specifications are contained in the
* elements of an array of Strings. This version uses a single
* defined type for all spectra (set to {@link SpecDataFactory.DEFAULT}
* for the usual file extensions rules).
*/
public void load( SplatBrowser browser, String[] spectra,
boolean display, int usertype )
{
setSpectra( spectra, usertype );
this.browser = browser;
this.display = display;
loadSpectra();
}
/**
* Load an array of spectra whose specifications are contained in the
* elements of an array of Strings. This method uses a different
* type for each input spectrum, so the usertypes array is required to be
* the same size as the spectra array.
*/
public void load( SplatBrowser browser, String[] spectra,
boolean display, int[] usertypes, String[] shortNames )
{
setSpectra( spectra, usertypes, shortNames );
this.browser = browser;
this.display = display;
loadSpectra();
}
/**
* Load an array of spectra whose specifications are contained in the
* elements of the props array.
*/
public void load( SplatBrowser browser, boolean display, Props[] props )
{
for ( int i = 0; i < props.length; i++ ) {
queue.add( props[i] );
}
this.browser = browser;
this.display = display;
loadSpectra();
}
/**
* Set the spectra to load, all have the same data type.
*/
protected synchronized void setSpectra( String[] spectra, int type )
{
if ( spectra != null ) {
for ( int i = 0; i < spectra.length; i++ ) {
queue.add( new Props( spectra[i], type, null ) );
}
}
}
/**
* Set the spectra to load and, if given (can be null) the individual
* types.
*/
protected synchronized void setSpectra( String[] spectra, int[] types )
{
if ( spectra != null ) {
for ( int i = 0; i < spectra.length; i++ ) {
queue.add( new Props( spectra[i], types[i], null ) );
}
}
}
/**
* Set the spectra to load and, if given (can be null) the individual
* types and some short names.
*/
protected synchronized void setSpectra( String[] spectra, int[] types,
String[] shortNames )
{
if ( spectra != null ) {
for ( int i = 0; i < spectra.length; i++ ) {
queue.add( new Props( spectra[i], types[i], shortNames[i] ) );
}
}
}
/**
* Get properties of the next spectrum to load. Returns null when none
* left.
*/
protected synchronized Props getSpectrumProps()
{
try {
return (Props) queue.remove( 0 );
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Load all the spectra that are waiting to be processed.
*/
protected void loadSpectra()
{
if ( ! queue.isEmpty() ) {
progressFrame.setTitle( "Loading "+ queue.size() +" spectra..." );
progressFrame.start();
// Block the browser window during the load.
browser.setWaitCursor();
// Set up thread to start reading the spectra.
loadThread.setAdd( true );
}
}
/**
* Number of files which we have loaded.
*/
private int filesDone = 0;
/**
* Load all the currently queued spectra into the global list of spectra
* and inform the associated SplatBrowser to display. Note synchronized
* as the thread is permanently running.
*/
protected synchronized void addSpectra()
{
if ( queue.isEmpty() ) return;
int validFiles = 0;
int failedFiles = 0;
int initialsize = globalList.specCount();
filesDone = 0;
StringBuffer failures = null;
Exception lastException = null;
// Add all spectra to the browser until the queue is empty or
// the load is interrupted.
while( ! queue.isEmpty() && ! progressFrame.isInterrupted() ) {
progressFrame.setTitle( "Loading "+ queue.size() +" spectra..." );
Props props = getSpectrumProps();
progressFrame.setMessage( props.getSpectrum() );
boolean success = false;
try {
browser.tryAddSpectrum( props );
validFiles++;
success = true;
}
catch (Exception e) {
// Catch all exceptions, we need to make sure that the
// watcher notification proceeds regardless.
e.printStackTrace();
lastException = e;
if ( failures == null ) {
failures = new StringBuffer();
}
failures.append( e.getMessage() + "\n" );
failedFiles++;
}
finally {
if ( watcher != null ) {
if ( success ) {
watcher.loadSucceeded( props );
}
else {
watcher.loadFailed( props, lastException );
}
}
}
filesDone++;
}
// Report any failures. If there is just one make usual report.
// Report to the registered instance of Watch if set.
if ( failures != null ) {
String message = null;
if ( failedFiles == 1 ) {
message = lastException.getMessage();
}
else {
message = "Failed to open " + failedFiles + " spectra";
lastException = new SplatException( failures.toString() );
}
ErrorDialog.showError( browser, message, lastException );
}
// And now display them if we can.
if ( display && validFiles > 0 ) {
int count = globalList.specCount();
browser.displayRange( initialsize, count - 1 );
}
}
/**
* Save a given spectrum as a file.
*
* @param globalIndex global list index of the spectrum to save.
* @param target the file to write the spectrum into.
*/
public void save( SplatBrowser browser, int globalIndex, String target )
{
// Monitor progress by checking the filesDone variable.
progressFrame.setTitle( "Saving spectrum..." );
progressFrame.setMessage( "as " + target );
progressFrame.start();
this.browser = browser;
browser.setWaitCursor();
// Set up thread to save the spectrum.
loadThread.setAdd( false );
loadThread.setSave( globalIndex, target );
}
/**
* Container class for describing the properties of a spectrum to be
* loaded. The only required value is the spectrum specification (name,
* URL etc.). The others cover a pre-defined type, a shortname, the data
* and spectral coordinate units (ideally as strings understood by AST)
* and the columns to be used for data and coordinates, if the spectrum
* is a table.
*/
public static class Props
{
protected String spectrum;
protected int type;
protected String shortName;
protected String dataUnits;
protected String coordUnits;
protected String dataColumn;
protected String coordColumn;
protected String errorColumn;
protected String idValue;
protected String idSource;
protected String getDataRequest;
protected String getDataFormat;
protected String dataLinkRequest;
protected String dataLinkFormat;
protected String serverURL;
public Props( String spectrum )
{
this( spectrum, SpecDataFactory.DEFAULT, null, null, null,
null, null, null );
}
public Props( String spectrum, int type, String shortName )
{
this( spectrum, type, shortName, null, null, null, null, null );
}
public Props( String spectrum, int type, String shortName,
String dataUnits, String coordUnits,
String dataColumn, String coordColumn )
{
this( spectrum, type, shortName, dataUnits, coordUnits,
dataColumn, coordColumn, null );
}
public Props( String spectrum, int type, String shortName,
String dataUnits, String coordUnits,
String dataColumn, String coordColumn,
String errorColumn )
{
this( spectrum, type, shortName, dataUnits, coordUnits,
dataColumn, coordColumn, null, null, null );
}
public Props( String spectrum, int type, String shortName,
String dataUnits, String coordUnits,
String dataColumn, String coordColumn,
String errorColumn, String idsrc, String idValue )
{
this.spectrum = spectrum;
this.type = type;
this.shortName = shortName;
this.dataUnits = dataUnits;
this.coordUnits = coordUnits;
this.dataColumn = dataColumn;
this.coordColumn = coordColumn;
this.errorColumn = errorColumn;
this.getDataRequest=null;
this.getDataFormat=null;
this.dataLinkRequest=null;
this.dataLinkFormat=null;
this.idValue=idValue;
this.idSource = idsrc;
this.serverURL=null;
}
public String getSpectrum()
{
if (serverURL == null )
return spectrum;
// if (pubdidValue != null && getDataRequest != null )
if (idValue != null && dataLinkRequest != null && ! dataLinkRequest.isEmpty())
if ( serverURL.endsWith("?"))
try {
//return (serverURL+"REQUEST=getData"+URLEncoder.encode(getDataRequest+"&PUBDID="+pubdidValue, "UTF-8"));
return (serverURL+"ID="+URLEncoder.encode(idValue, "UTF-8")+dataLinkRequest);
// return (serverURL+"ID="+idValue+);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
else
try {
// return (serverURL+"?REQUEST=getData"+getDataRequest+"&PUBDID="+pubdidValue);
return (serverURL+ "?"+"ID="+URLEncoder.encode(idValue, "UTF-8")+dataLinkRequest);
// return (serverURL+"?REQUEST=getData"+URLEncoder.encode(getDataRequest+"&PUBDID="+pubdidValue, "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return spectrum;
}
public void setSpectrum( String spectrum )
{
this.spectrum = spectrum;
}
public int getType()
{
return type;
}
public void setType( int type )
{
this.type = type;
}
public void setGetDataFormat( String format )
{
if (format.contains("fits"))
this.getDataFormat = "FITS";
else if (format.contains("votable"))
this.getDataFormat = "XML";
else if (format.equalsIgnoreCase("text/plain"))
this.getDataFormat = "TEXT";
else if (format.equalsIgnoreCase("text/cvs"))
this.getDataFormat = "TEXT";
}
public String getGetDataFormat()
{
return getDataFormat;
}
public String getShortName()
{
return shortName;
}
public void setShortName( String shortName )
{
this.shortName = shortName;
}
public String getDataUnits()
{
return dataUnits;
}
public void setDataUnits( String dataUnits )
{
this.dataUnits = dataUnits;
}
public String getCoordUnits()
{
return coordUnits;
}
public void setCoordUnits( String coordUnits )
{
this.coordUnits = coordUnits ;
}
public String getDataColumn()
{
return dataColumn;
}
public void setDataColumn( String dataColumn )
{
this.dataColumn = dataColumn;
}
public String getCoordColumn()
{
return coordColumn;
}
public void setCoordColumn( String coordColumn )
{
this.coordColumn = coordColumn;
}
public String getErrorColumn()
{
return errorColumn;
}
public void setErrorColumn( String errorColumn )
{
this.errorColumn = errorColumn;
}
public String getidValue()
{
return idValue;
}
public void setIdValue( String idValue )
{
this.idValue = idValue; //!!!
}
public String getGetDataRequest()
{
return getDataRequest;
}
public void setDataLinkRequest( String dataLinkRequest )
{
this.dataLinkRequest = dataLinkRequest;
}
public String getDataLinkRequest()
{
return dataLinkRequest;
}
public void setGetDataRequest( String getDataRequest )
{
this.getDataRequest = getDataRequest;
}
public String getServerURL()
{
return serverURL;
}
public void setServerURL( String serverURL )
{
this.serverURL = serverURL;
}
// Create a copy of this object.
public Props copy()
{
return new Props( spectrum, type, shortName, dataUnits, coordUnits,
dataColumn, coordColumn, errorColumn );
}
/**
* Apply the tranmutable properties of this object to a SpecData
* instance. This does not include the spectrum specification, or data
* type, these must be used to open the spectrum.
*/
public void apply( SpecData spectrum )
{
boolean initialiseNeeded = false;
if ( shortName != null && shortName.length() != 0 ) {
spectrum.setShortName( shortName );
}
if ( dataUnits != null && dataUnits.length() != 0 ) {
spectrum.setDataUnits( dataUnits );
initialiseNeeded = true;
}
if ( coordUnits != null && coordUnits.length() != 0 ) {
// Bit tricky, set the units of the underlying frame produced
// from the data and reinitialise AST. Still might be lost if
// the spectrum is reloaded from disk file.
String units = UnitUtilities.fixUpUnits( coordUnits );
FrameSet frameSet = spectrum.getFrameSet();
frameSet.setUnit( 1, units );
initialiseNeeded = true;
}
if ( coordColumn != null && coordColumn.length() != 0 ) {
try {
spectrum.setXDataColumnName( coordColumn, false );
initialiseNeeded = true;
}
catch (SplatException e) {
e.printStackTrace();
}
}
if ( dataColumn != null && dataColumn.length() != 0 ) {
try {
if ( spectrum.setYDataColumnName( dataColumn ) ) {
// Coordinate systems update already done.
initialiseNeeded = false;
}
}
catch (SplatException e) {
e.printStackTrace();
}
}
if ( errorColumn != null && errorColumn.length() != 0 ) {
try {
spectrum.setYDataErrorColumnName( errorColumn );
}
catch (SplatException e) {
e.printStackTrace();
}
}
/*
if ( pubdidColumn != null && pubdidColumn.length() != 0 ) {
try {
spectrum.setsetGetDataColumnName( pubdidColumn );
}
catch (SplatException e) {
e.printStackTrace();
}
}
*/
if ( initialiseNeeded ) {
// Probably changed something important to the coordinate
// systems, so make sure AST descriptions are up to date.
try {
spectrum.initialiseAst();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public void setDataLinkFormat(String format) {
dataLinkFormat = format;
}
public void setIdSource(String idsrc) {
idSource = idsrc;
}
}
// Inner class for loading and saving Spectra.
// Extend with other operations if needed.
private class Loader
extends Thread
{
private boolean addAction = true;
private int globalIndex = 0;
private String target = null;
public Loader()
{
super( "Spectra loader" );
}
// Set run action, either add to browser or save to disk.
public void setAdd( boolean action )
{
addAction = action;
}
// If saving set the necessary parameters.
public void setSave( int globalIndex, String target )
{
this.globalIndex = globalIndex;
this.target = target;
}
// Thread run() method. This never exits as that would cause the
// thread to die and we'd lose the context. When there are no spectra
// to load or save this waits for a while and then checks again.
// In principle this could be hurried by using notify(), but that
// isn't necessary.
public void run()
{
while ( !interrupted() ) {
if ( browser != null ) {
if ( addAction ) {
try {
addSpectra();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Always tidy up and rewaken interface when
// complete (including if an error is thrown).
browser.resetWaitCursor();
progressFrame.stop();
if ( progressFrame.isInterrupted() ) {
queue.clear();
}
}
}
else {
try {
if ( target != null ) {
synchronized( target ) {
// Next loop will re-save if target is
// set, so clear that now, also do this
// before any exceptions could be thrown.
String tcopy = target;
target = null;
browser.saveSpectrum( globalIndex,
tcopy );
}
}
}
catch (Exception e) {
ErrorDialog.showError( browser,
"Failed to save spectrum: "
+ e.getMessage(), e );
}
finally {
// Always tidy up and rewaken interface when
// complete (including if an error is thrown).
browser.resetWaitCursor();
progressFrame.stop();
if ( progressFrame.isInterrupted() ) {
queue.clear();
}
}
}
}
waitDelay();
if ( progressFrame.isInterrupted() ) {
queue.clear();
}
}
}
// Wait for a while, then service the run method again for new
// actions.
synchronized void waitDelay() {
try {
wait( 500 );
}
catch (InterruptedException e) {
}
}
}
/**
* Set a watcher to be informed when the load queue has been emptied.
*
* @param watcher an instance of the Watch interface. Set to null
* to clear. Remember to do that otherwise you'll
* get reports about all spectra being loaded.
*/
public void setWatcher( Watch watcher )
{
this.watcher = watcher;
}
/**
* Interface for objects that wish to be informed about the result of
* spectrum load attempts performed by addSpectra(). One or other
* of the methods will be called for each load attempt.
*/
public interface Watch
{
/**
* Reports that a spectrum with given props was successfully loaded.
*
* @param props props object giving spectrum characteristics
*/
public void loadSucceeded( Props props );
/**
* Reports that a load attempt for the given props failed.
*
* @param props props object giving spectrum characteristics
* @param error exception resulting from load attempt
*/
public void loadFailed( Props props, Throwable error );
}
}