dkirkby / bossdata

Tools for accessing SDSS BOSS data
MIT License
1 stars 3 forks source link

Better error handling in bossplot #53

Closed dkirkby closed 9 years ago

dkirkby commented 9 years ago

The following should fail more gracefully:

bossplot --plate 5767 --mjd 56245 --fiber 146 --exposure 7
Traceback (most recent call last):
  File "bin/bossplot", line 275, in <module>
    main()
  File "bin/bossplot", line 190, in main
    pixel_quality_mask=pixel_quality_mask, **data_args))
  File "/Users/david/Cosmo/LyAlpha/code/bossdata/bossdata/spec.py", line 174, in get_valid_data
    hdu = self.get_exposure_hdu(exposure_index, camera)
  File "/Users/david/Cosmo/LyAlpha/code/bossdata/bossdata/spec.py", line 100, in get_exposure_hdu
    info = self.exposure_table[exposure_index]
  File "/Users/david/anaconda/lib/python2.7/site-packages/astropy/table/table.py", line 842, in __getitem__
    return self.Row(self, item)
  File "/Users/david/anaconda/lib/python2.7/site-packages/astropy/table/row.py", line 42, in __init__
    .format(index, len(table)))
IndexError: index 7 out of range for table with length 7

This issue is to do a systematic review of possible errors due to invalid command-line parameters and handle each with a short message printed to stdout and returning with a non-zero exit code, e.g.

print('The value --exposure {} is outside the allowed range {}-{}.'.format(...))
return -1
dkirkby commented 9 years ago

Using the latest commit to #70, the example above now exits with:

The value --exposure 7 is outside the allowed range 0-6.
Use the --verbose option for details on the available exposures.
dkirkby commented 9 years ago

The handling of missing spCFrame files is still not great, but really a separate issue #34:

% bossplot --cframe --exposure 0 --plate 6641 --mjd 56383 --fiber 30
HTTP request returned error code 404 for http://dr12.sdss3.org/sas/dr12/boss/spectro/redux/v5_7_0/6641/spCFrame-r1-00158840.fits.

At least now you know that the problem is not an invalid exposure index.

dkirkby commented 9 years ago

The same problem occurs for an invalid plate / mjd, so we really need a better solution:

% bossplot --plate 10000
HTTP request returned error code 404 for http://dr12.sdss3.org/sas/dr12/boss/spectro/redux/v5_7_0/spectra/lite/10000/spec-10000-56383-0030.fits.

Add special handling for HTTP 404 errors. Now the message is:

There is no remote file /sas/dr12/boss/spectro/redux/v5_7_0/spectra/lite/100000/spec-100000-56383-0030.fits.
dkirkby commented 9 years ago

Invalid fibers throw a ValueError which we are not catching:

% bossplot --fiber 0
Traceback (most recent call last):
  File "/Users/david/anaconda/bin/bossplot", line 6, in <module>
    exec(compile(open(__file__).read(), __file__, 'exec'))
  File "/Users/david/Cosmo/LyAlpha/code/bossdata/bin/bossplot", line 481, in <module>
    main()
  File "/Users/david/Cosmo/LyAlpha/code/bossdata/bin/bossplot", line 231, in main
    fiber=args.fiber, lite=lite)]
  File "/Users/david/Cosmo/LyAlpha/code/bossdata/bossdata/path.py", line 241, in get_spec_path
    fiber, get_num_fibers(plate), plate))
ValueError: Invalid fiber (0) must be 1-1000 for plate 6641.

Catch this to give:

% bossplot --fiber 0
Invalid fiber (0) must be 1-1000 for plate 6641.
dkirkby commented 9 years ago

Invalid mask bits also throw an un-caught ValueError. Fix this:

% bossplot --allow-mask INVALID
Invalid bit name: INVALID.
dkirkby commented 9 years ago

Processing of the `--figsize X,Y`` option is quite fragile. Some combinations silently open plots in invisible windows that you cannot close, e.g.

bossplot --figsize 10,-1
bossplot --figsize 0,0

Other values throw un-caught exceptions, e.g.

bossplot --figsize 10,10,10

These are now handled reasonably:

% bossplot --figsize 0,0
Minimum --figsize is 1,1.
% bossplot --figsize 10,10,10
Invalid option --figsize 10,10,10 (expected WIDTH,HEIGHT in inches).
dkirkby commented 9 years ago

Providing a bad file name to --save-data or --save-plot raises an un-caught IOError, e.g.

% bossplot --save-data /protected
...
IOError: [Errno 13] Permission denied: '/protected'

Now this exits with:

Unable to save data: [Errno 13] Permission denied: '/protected'.

Add similar logic for --save-plot errors.

dkirkby commented 9 years ago

I am declaring this done for now (once #70 gets merged), but re-open if you find any more un-caught exceptions.

dmargala commented 9 years ago

The platelist catalog could be used to check if the requested plate/mjd are valid.