Repository: aarande/nexradaws

Issue:
Function get_avail_scans should check for existence of resp['Contents']
If no data at all is available for a given date, then resp['Contents'] does not exist in the function get_avail_scans. This raises an error, while more desired behaviour is to return an empty list I think. 

I found this issue because I wanted to retrieve data for a given time range for one day, for which there indeed is data. For the next date there however there is no data, and it appears that the function get_avail_scans_in_range includes by default the next date in its attempt to obtain available scans, if the time span is nonzero. The error that I mentioned above caused get_avail_scans_in_range to return an error, instead of a list with scans that were in fact available.

Here is a simple fix:
`if 'Contents' in resp:
            for scan in resp.get('Contents'):
                match = self._scan_re.match(scan.get('Key'))
                if match is not None:
                    scans.append(AwsNexradFile(scan))`

Discussion:

Patch:
diff --git a/nexradaws/nexradawsinterface.py b/nexradaws/nexradawsinterface.py
index 63ceb75..8b4ba0f 100644
--- a/nexradaws/nexradawsinterface.py
+++ b/nexradaws/nexradawsinterface.py
@@ -47,7 +47,8 @@ class NexradAwsInterface(object):
         """
         years = []
         resp = self._bucket.meta.client.list_objects(Bucket='noaa-nexrad-level2',Delimiter='/')
-        for each in resp.get('CommonPrefixes'):
+
+        for each in resp.get('CommonPrefixes', []):
             match = self._year_re.match(each.get('Prefix'))
             if match is not None:
                 years.append(match.group(1))
@@ -72,7 +73,7 @@ class NexradAwsInterface(object):
         resp = self._bucket.meta.client.list_objects(Bucket='noaa-nexrad-level2',
                                                      Prefix=prefix,
                                                      Delimiter='/')
-        for each in resp.get('CommonPrefixes'):
+        for each in resp.get('CommonPrefixes', []):
             match = self._month_re.match(each.get('Prefix'))
             if match is not None:
                 months.append(match.group(1))
@@ -98,7 +99,7 @@ class NexradAwsInterface(object):
         resp = self._bucket.meta.client.list_objects(Bucket='noaa-nexrad-level2',
                                                      Prefix=prefix,
                                                      Delimiter='/')
-        for each in resp.get('CommonPrefixes'):
+        for each in resp.get('CommonPrefixes', []):
             match = self._day_re.match(each.get('Prefix'))
             if match is not None:
                 days.append(match.group(1))
@@ -126,7 +127,7 @@ class NexradAwsInterface(object):
         resp = self._bucket.meta.client.list_objects(Bucket='noaa-nexrad-level2',
                                                      Prefix=prefix,
                                                      Delimiter='/')
-        for each in resp.get('CommonPrefixes'):
+        for each in resp.get('CommonPrefixes', []):
             match = self._radar_re.match(each.get('Prefix'))
             if match is not None:
                 radars.append(match.group(1))
@@ -157,7 +158,7 @@ class NexradAwsInterface(object):
         resp = self._bucket.meta.client.list_objects(Bucket='noaa-nexrad-level2',
                                                      Prefix=prefix,
                                                      Delimiter='/')
-        for scan in resp.get('Contents'):
+        for scan in resp.get('Contents', []):
             match = self._scan_re.match(scan.get('Key'))
             if match is not None:
                 scans.append(AwsNexradFile(scan))


Test patch:
diff --git a/tests/test_nexradAwsInterface.py b/tests/test_nexradAwsInterface.py
index 01b5947..32cf370 100644
--- a/tests/test_nexradAwsInterface.py
+++ b/tests/test_nexradAwsInterface.py
@@ -40,21 +40,37 @@ class TestNexradAwsInterface(TestCase):
         six.assertCountEqual(self, months, examplemonths)
         self.assertEqual(12, len(months))
 
+    def test_get_available_months_missing(self):
+        months = self.query.get_avail_months('1900')
+        self.assertEqual(months, [])
+
     def test_get_available_days(self):
         days = self.query.get_avail_days('2006', '05')
         self.assertIsInstance(days, list)
         self.assertEqual(31, len(days))
         six.assertCountEqual(self, days, exampledays)
 
+    def test_get_available_days_missing(self):
+        days = self.query.get_avail_days('2006', '13')
+        self.assertEqual(days, [])
+
     def test_get_available_radars(self):
         radars = self.query.get_avail_radars('2006', '05', '31')
         self.assertIsInstance(radars, list)
         self.assertTrue('KTLX' in radars)
 
+    def test_get_available_radars_missing(self):
+        radars = self.query.get_avail_radars('1900', '05', '31')
+        self.assertEqual(radars, [])
+
     def test_get_available_scans(self):
         scans = self.query.get_avail_scans('2006', '05', '31', 'KTLX')
         self.assertIsInstance(scans, list)
 
+    def test_get_available_scan_missing(self):
+        scans = self.query.get_avail_scans('1991', '05', '31', 'KTLX')
+        self.assertEqual(scans, [])
+
     def test_get_available_scans_in_range(self):
         start = datetime(2013, 5, 20, 18, 00)
         end = datetime(2013, 5, 20, 22, 00)