Repository: sunpy/sunpy

Issue:
Specifying path in Fido.fetch behaves strangely
For example:


    In [36]: aia = Fido.search(a.Time('2011-06-07 06:33','2011-06-07 06:33:08'),a.Instrument('aia'))

    In [37]: aia
    Out[37]: 
    <sunpy.net.fido_factory.UnifiedResponse object at 0x11836d890>
    Results from the VSOClient:
    <Table length=8>
       Start Time [1]       End Time [1]    Source ...   Type    Wavelength [2] 
                                                   ...              Angstrom    
           str19               str19         str3  ...   str8       float64     
    ------------------- ------------------- ------ ... -------- ----------------
    2011-06-07 06:33:01 2011-06-07 06:33:02    SDO ... FULLDISK   131.0 .. 131.0
    2011-06-07 06:33:02 2011-06-07 06:33:03    SDO ... FULLDISK   211.0 .. 211.0
    2011-06-07 06:33:02 2011-06-07 06:33:03    SDO ... FULLDISK   171.0 .. 171.0
    2011-06-07 06:33:03 2011-06-07 06:33:04    SDO ... FULLDISK   335.0 .. 335.0
    2011-06-07 06:33:05 2011-06-07 06:33:06    SDO ... FULLDISK 1600.0 .. 1600.0
    2011-06-07 06:33:05 2011-06-07 06:33:06    SDO ... FULLDISK     94.0 .. 94.0
    2011-06-07 06:33:07 2011-06-07 06:33:08    SDO ... FULLDISK   193.0 .. 193.0
    2011-06-07 06:33:08 2011-06-07 06:33:09    SDO ... FULLDISK     94.0 .. 94.0

    In [38]: a = Fido.fetch(aia,path='/Users/ainglis/python/new_sample_data/')
    [========================================]

    In [39]: a
    Out[39]: 
    ['/Users/ainglis/python/new_sample_data/.2',
     '/Users/ainglis/python/new_sample_data/.0',
     '/Users/ainglis/python/new_sample_data/.1',
     '/Users/ainglis/python/new_sample_data/.0',
     '/Users/ainglis/python/new_sample_data/.3',
     '/Users/ainglis/python/new_sample_data/.4']

The filenames are incorrect. Fido should retain the filenames from the Fido response object, but it does not appear to. This might depend on the client that was queried by Fido, I'm not sure.






Discussion:

Resolving patch:
diff --git a/sunpy/net/vso/vso.py b/sunpy/net/vso/vso.py
--- a/sunpy/net/vso/vso.py
+++ b/sunpy/net/vso/vso.py
@@ -597,6 +597,8 @@ def get(self, query_response, path=None, methods=('URL-FILE_Rice', 'URL-FILE'),
         if path is None:
             path = os.path.join(config.get('downloads', 'download_dir'),
                                 '{file}')
+        elif isinstance(path, six.string_types) and '{file}' not in path:
+            path = os.path.join(path, '{file}')
         path = os.path.expanduser(path)
 
         fileids = VSOClient.by_fileid(query_response)


Test patch:
diff --git a/sunpy/net/tests/test_vso.py b/sunpy/net/tests/test_vso.py
--- a/sunpy/net/tests/test_vso.py
+++ b/sunpy/net/tests/test_vso.py
@@ -5,7 +5,9 @@
 
 from __future__ import absolute_import
 
+import tempfile
 import datetime
+
 import pytest
 from astropy import units as u
 
@@ -207,3 +209,23 @@ def test_str():
 def test_repr():
     qr = QueryResponse([])
     assert "Start Time End Time  Source Instrument   Type" in repr(qr)
+
+@pytest.mark.online
+def test_path(client):
+    """
+    Test that '{file}' is automatically appended to the end of a custom path if
+    it is not specified.
+    """
+    qr = client.query(va.Time('2011-06-07 06:33',
+                              '2011-06-07 06:33:08'),
+                      va.Instrument('aia'),
+                      va.Wavelength(171*u.AA))
+    tmp_dir = tempfile.mkdtemp()
+    files = client.get(qr, path=tmp_dir).wait(progress=False)
+
+    assert len(files) == 1
+
+    # The construction of a VSO filename is bonkers complex, so there is no
+    # practical way to determine what it should be in this test, so we just
+    # put it here.
+    assert "aia_lev1_171a_2011_06_07t06_33_02_77z_image_lev1.fits" in files[0]