### Setup: Import libraries and create temp directory Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Imports matplotlib, tempfile, pytz, datetime, and pyart, and creates a temporary directory for storing downloaded data. This setup is required before instantiating the NexradAwsInterface. ```python %matplotlib inline import matplotlib.pyplot as plt import tempfile import pytz from datetime import datetime import pyart templocation = tempfile.mkdtemp() ``` -------------------------------- ### get_avail_radars example Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/nexradawsinterface.html Example usage of get_avail_radars with year, month, and day arguments. Returns a list of radar site strings available for the given date. ```python >>> print conn.get_avail_radars('2013','05','31') >>> [u'DAN1', u'KABR', u'KABX', u'KAKQ', u'KAMA', u'KAMX', u'KAPX', u'KARX', u'KATX', u'KBBX', u'KBGM', u'KBHX', u'KBIS', u'KBLX', u'KBMX', u'KBOX', u'KBRO', u'KBUF', u'KBYX', u'KCAE', u'KCBW', u'KCBX', u'KCCX', u'KCLE', u'KCLX', u'KCRP', u'KCXX', u'KCYS', u'KDAX', u'KDDC', u'KDFX', u'KDGX', u'KDLH', u'KDMX', u'KDOX', u'KDTX', u'KDVN', u'KEAX', u'KEMX', u'KENX', u'KEOX', u'KEPZ', u'KESX', u'KEVX', u'KEWX', u'KEYX', u'KFCX', u'KFDR', u'KFFC', u'KFSD', u'KFSX', u'KFTG', u'KFWS', u'KGGW', u'KGJX', u'KGLD', u'KGRB', u'KGRK', u'KGRR', u'KGSP', u'KGWX', u'KGYX', u'KHDX', u'HGX', u'KHNX', u'KHPX', u'KHTX', u'KICT', u'KICX', u'KILN', u'KILX', u'KIND', u'KINX', u'KIWA', u'KIWX', u'KJAX', u'KJGX', u'KJKL', u'KLBB', u'KLCH', u'KLGX', u'KLIX', u'KLNX', u'KLOT', u'KLRX', u'KLSX', u'KLTX', u'KLVX', u'KLWX', u'KLZK', u'KMAF', u'KMAX', u'KMBX', u'KMHX', u'KMKX', u'KMLB', u'KMOB', u'KMPX', u'KMQT', u'KMRX', u'KMSX', u'KMTX', u'KMUX', u'KMVX', u'KMXX', u'KNKX', u'KNQA', u'KOAX', u'KOHX', u'KOKX', u'KOTX', u'KPAH', u'KPBZ', u'KPDT', u'KPOE', u'KPUX', u'KRAX', u'KRGX', u'KRIW', u'KRLX', u'KRTX', u'KSFX', u'KSGF', u'KSHV', u'KSJT', u'KSOX', u'KSRX', u'KTBW', u'KTFX', u'KTLH', u'KTLX', u'KTWX', u'KTYX', u'KUDX', u'KUEX', u'KVNX', u'KVTX', u'KVWX', u'KYUX', u'PHKI', u'PHKM', u'PHMO', u'PHWA', u'TJUA'] ``` -------------------------------- ### Instantiate NexradAwsInterface Source: https://nexradaws.readthedocs.io/en/latest/apidocs.html Creates a connection to the Nexrad AWS bucket. Use this to start querying metadata and downloading files. ```python >>> import nexradaws >>> conn = nexradaws.NexradAwsInterface() ``` -------------------------------- ### Install nexradaws with pip Source: https://nexradaws.readthedocs.io/en/latest/index.html Install the nexradaws module using pip. This is the standard installation command for the package. ```bash pip install nexradaws ``` -------------------------------- ### get_avail_scans example Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/nexradawsinterface.html Example usage of get_avail_scans with year, month, day, and radar arguments. Returns a list of AwsNexradFile objects representing available scans. ```python >>> print conn.get_avail_scans('2013','05','31','KTLX') >>> [AwsNexradFile object - 2013/05/31/KTLX/KTLX20130531_000358_V06.gz, AwsNexradFile object - 2013/05/31/KTLX/KTLX20130531_000834_V06.gz, AwsNexradFile object - 2013/05/31/KTLX/KTLX20130531_001311_V06.gz,... ``` -------------------------------- ### get_avail_years method Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/nexradawsinterface.html Returns a list of available years by listing the bucket with a delimiter and extracting year prefixes. The docstring shows an example output. ```python def get_avail_years(self): """ This method allows you to get the years that are currently available. >>> print conn.get_avail_years() >>> [u'1991', u'1992', u'1993', u'1994', u'1995', u'1996', u'1997', u'1998', u'1999', u'2000', u'2001', u'2002', u'2003', u'2004', u'2005', u'2006', u'2007', u'2008', u'2009', u'2010', u'2011', u'2012', u'2013', u'2014', u'2015', u'2016', u'2017'] :return: A list of strings representing the years available :rtype list: """ years = [] resp = self._bucket.meta.client.list_objects(Bucket='noaa-nexrad-level2',Delimiter='/') for each in resp.get('CommonPrefixes'): match = self._year_re.match(each.get('Prefix')) if match is not None: years.append(match.group(1)) return years ``` -------------------------------- ### get_avail_scans_in_range example Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/nexradawsinterface.html Example usage of get_avail_scans_in_range with start and end datetime objects and a radar id. If datetimes lack a timezone, they are set to UTC. ```python >>> from datetime import datetime >>> radarid = 'KTLX' >>> start = datetime(2013, 5, 31, 20, 0) >>> end = datetime(2013, 5, 31, 23, 0) >>> print conn.get_avail_scans_in_range(start,end,radarid) >>> [AwsNexradFile object - 2013/05/31/KTLX/KTLX20130531_200046_V06.gz, AwsNexradFile object - 2013/05/31/KTLX/KTLX20130531_200415_V06.gz, AwsNexradFile object - 2013/05/31/KTLX/KTLX20130531_200745_V06.gz,... ``` -------------------------------- ### get_avail_months method Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/nexradawsinterface.html Returns available months for a given year by listing objects with a prefix and delimiter. The docstring shows an example output for '2013'. ```python def get_avail_months(self, year): """ This method allows you to get the available months in a given year. >>> print conn.get_avail_months('2013') >>> [u'01', u'02', u'03', u'04', u'05', u'06', u'07', u'08', u'09', u'10', u'11', u'12'] :param year: the year we are requesting available months for (i.e. 2010) :type year: str or int :return: A list of strings representing the months available for that year :rtype list: """ months = [] prefix = self._build_prefix(year=year, month=None, day=None, station_id=None) resp = self._bucket.meta.client.list_objects(Bucket='noaa-nexrad-level2', Prefix=prefix, Delimiter='/') for each in resp.get('CommonPrefixes'): match = self._month_re.match(each.get('Prefix')) if match is not None: months.append(match.group(1)) return months ``` -------------------------------- ### get_avail_days method Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/nexradawsinterface.html Returns available days for a given year and month by listing objects with a prefix and delimiter. The docstring shows an example output for '2013','05'. ```python def get_avail_days(self, year, month): """ This method allows you to get the available days in a given year and month. >>> print conn.get_avail_days('2013','05') >>> [u'01', u'02', u'03', u'04', u'05', u'06', u'07', u'08', u'09', u'10', u'11', u'12', u'13', u'14', u'15', u'16', u'17', u'18', u'19', u'20', u'21', u'22', u'23', u'24', u'25', u'26', u'27', u'28', u'29', u'30', u'31'] :param year: the year we are requesting available days for (i.e 2010) :type year: str or int :param month: the month we are requesting available days for (i.e. 05) :type month: str or int :return: A list of strings representing the days available in the given month and year :rtype list: """ days = [] prefix = self._build_prefix(year=year, month=month, day=None, station_id=None) resp = self._bucket.meta.client.list_objects(Bucket='noaa-nexrad-level2', Prefix=prefix, Delimiter='/') for each in resp.get('CommonPrefixes'): match = self._day_re.match(each.get('Prefix')) if match is not None: days.append(match.group(1)) return days ``` -------------------------------- ### NexradAwsInterface Source: https://nexradaws.readthedocs.io/en/latest/apidocs.html Instantiate an instance of this class to get a connection to the Nexrad AWS bucket. This class provides methods to query for various metadata of the AWS bucket as well as download files. ```APIDOC ## NexradAwsInterface ### Description Instantiate an instance of this class to get a connection to the Nexrad AWS bucket. This class provides methods to query for various metadata of the AWS bucket as well as download files. ### Usage Example ```python >>> import nexradaws >>> conn = nexradaws.NexradAwsInterface() ``` ``` -------------------------------- ### Get available years with get_avail_years Source: https://nexradaws.readthedocs.io/en/latest/apidocs.html Prints the list of years available in the NEXRAD AWS bucket. The output shows years from 1991 to 2017. ```python >>> print conn.get_avail_years() >>> [u'1991', u'1992', u'1993', u'1994', u'1995', u'1996', u'1997', u'1998', u'1999', u'2000', u'2001', u'2002', u'2003', u'2004', u'2005', u'2006', u'2007', u'2008', u'2009', u'2010', u'2011', u'2012', u'2013', u'2014', u'2015', u'2016', u'2017'] ``` -------------------------------- ### get_avail_years Source: https://nexradaws.readthedocs.io/en/latest/apidocs.html This method allows you to get the years that are currently available. Returns a list of strings representing the years available. ```APIDOC ## get_avail_years ### Description This method allows you to get the years that are currently available. ### Method GET ### Endpoint NexradAwsInterface.get_avail_years() ### Parameters None ### Request Example ```python >>> print conn.get_avail_years() ``` ### Response #### Success Response - **Returns** (list) - A list of strings representing the years available #### Response Example ```python >>> [u'1991', u'1992', u'1993', u'1994', u'1995', u'1996', u'1997', u'1998', u'1999', u'2000', u'2001', u'2002', u'2003', u'2004', u'2005', u'2006', u'2007', u'2008', u'2009', u'2010', u'2011', u'2012', u'2013', u'2014', u'2015', u'2016', u'2017'] ``` ``` -------------------------------- ### LocalNexradFile class source code Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/resources/localnexradfile.html Full source of the LocalNexradFile class. It stores NEXRAD file metadata and provides open() and open_pyart() methods. open_pyart() raises ImportError if pyart is not installed. ```python import os try: import pyart pyart_avail = True except ImportError: pyart_avail = False [docs]class LocalNexradFile(object): """ This class contains metadata about the local NEXRAD file as well as methods to open the file. :var key: AWS key for this NEXRAD file :vartype key: str :var last_modified: when the file was last modified on AWS :vartype last_modified: datetime :var filename: the NEXRAD filename :vartype filename: str :var scan_time: volume scan time for the NEXRAD file :vartype scan_time: datetime :var radar_id: the four letter radar id (i.e. KTLX) :vartype radar_id: str :var filepath: absolute path to the downloaded file on the local system :vartype str: """ def __init__(self, awsnexradfile, localfilepath): super(LocalNexradFile, self).__init__() self.key = awsnexradfile.key self.last_modified = awsnexradfile.last_modified self.filename = awsnexradfile.filename self.scan_time = awsnexradfile.scan_time self.radar_id = awsnexradfile.radar_id self.filepath = localfilepath [docs] def open(self): """ Provides a file object to the local nexrad radar file. \ Be sure to close the file object when processing is complete. :return: file object ready for reading :rtype file: """ return open(self.filepath, 'rb') [docs] def open_pyart(self): """ If pyart is available this method will read in the nexrad archive file and return a \ pyart Radar object. :return: a pyart radar object :rtype pyart.core.Radar: """ if pyart_avail: return pyart.io.read_nexrad_archive(self.filepath) else: raise ImportError("pyart module must be installed to use this function.") def __repr__(self): return ''.format(self.filepath) ``` -------------------------------- ### Get available years Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Calls get_avail_years() on the connection object and prints the list of available years. The output shows years from 1991 to 2017. ```python years = conn.get_avail_years() print(years) ``` ```output ['1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017'] ``` -------------------------------- ### Get available days in a given year and month Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Calls get_avail_days('2013','05') to retrieve the days that have data for May 2013, printing the list of day strings. ```python days = conn.get_avail_days('2013','05') print(days) ``` ```output ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'] ``` -------------------------------- ### Get available months in a year Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Calls get_avail_months('2013') to retrieve the months that have data for the given year, printing the list of two-digit month strings. ```python months = conn.get_avail_months('2013') print(months) ``` ```output ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'] ``` -------------------------------- ### Get available radars in a given year, month, and day Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Calls get_avail_radars('2013','05','31') to retrieve the list of radar station IDs available for that date, printing the list of radar codes. ```python radars = conn.get_avail_radars('2013','05','31') print(radars) ``` ```output ['DAN1', 'KABR', 'KABX', 'KAKQ', 'KAMA', 'KAMX', 'KAPX', 'KARX', 'KATX', 'KBBX', 'KBGM', 'KBHX', 'KBIS', 'KBLX', 'KBMX', 'KBOX', 'KBRO', 'KBUF', 'KBYX', 'KCAE', 'KCBW', 'KCBX', 'KCCX', 'KCLE', 'KCLX', 'KCRP', 'KCXX', 'KCYS', 'KDAX', 'KDDC', 'KDFX', 'KDGX', 'KDLH', 'KDMX', 'KDOX', 'KDTX', 'KDVN', 'KEAX', 'KEMX', 'KENX', 'KEOX', 'KEPZ', 'KESX', 'KEVX', 'KEWX', 'KEYX', 'KFCX', 'KFDR', 'KFFC', 'KFSD', 'KFSX', 'KFTG', 'KFWS', 'KGGW', 'KGJX', 'KGLD', 'KGRB', 'KGRK', 'KGRR', 'KGSP', 'KGWX', 'KGYX', 'KHDX', 'KHGX', 'KHNX', 'KHPX', 'KHTX', 'KICT', 'KICX', 'KILN', 'KILX', 'KIND', 'KINX', 'KIWA', 'KIWX', 'KJAX', 'KJGX', 'KJKL', 'KLBB', 'KLCH', 'KLGX', 'KLIX', 'KLNX', 'KLOT', 'KLRX', 'KLSX', 'KLTX', 'KLVX', 'KLWX', 'KLZK', 'KMAF', 'KMAX', 'KMBX', 'KMHX', 'KMKX', 'KMLB', 'KMOB', 'KMPX', 'KMQT', 'KMRX', 'KMSX', 'KMTX', 'KMUX', 'KMVX', 'KMXX', 'KNKX', 'KNQA', 'KOAX', 'KOHX', 'KOKX', 'KOTX', 'KPAH', 'KPBZ', 'KPDT', 'KPOE', 'KPUX', 'KRAX', 'KRGX', 'KRIW', 'KRLX', 'KRTX', 'KSFX', 'KSGF', 'KSHV', 'KSJT', 'KSOX', 'KSRX', 'KTBW', 'KTFX', 'KTLH', 'KTLX', 'KTWX', 'KTYX', 'KUDX', 'KUEX', 'KVNX', 'KVTX', 'KVWX', 'KYUX', 'PHKI', 'PHKM', 'PHMO', 'PHWA', 'TJUA'] ``` -------------------------------- ### get_avail_scans_in_range Source: https://nexradaws.readthedocs.io/en/latest/apidocs.html Get all available scans for a radar between start and end date. If datetimes do not include a timezone they will be set to UTC. ```APIDOC ## [FUNCTION] get_avail_scans_in_range ### Description Get all available scans for a radar between start and end date. If datetimes do not include a timezone they will be set to UTC. ### Method FUNCTION ### Endpoint get_avail_scans_in_range(start, end, radar) ### Parameters #### Path Parameters - **start** (datetime) - Required - start time for range - **end** (datetime) - Required - end time for range - **radar** (str) - Required - radar id ### Request Example ```python >>> from datetime import datetime >>> radarid = 'KTLX' >>> start = datetime(2013, 5, 31, 20, 0) >>> end = datetime(2013, 5, 31, 23, 0) >>> print conn.get_avail_scans_in_range(start,end,radarid) ``` ### Response #### Success Response - **list** - A list of `AwsNexradFile` objects representing the radar scans available in the passed time range. #### Response Example ``` [AwsNexradFile object - 2013/05/31/KTLX/KTLX20130531_200046_V06.gz, AwsNexradFile object - 2013/05/31/KTLX/KTLX20130531_200415_V06.gz, ...] ``` ``` -------------------------------- ### Instantiate NexradAwsInterface Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Imports the nexradaws module and creates an instance of the NexradAwsInterface class, which provides methods to query and download from the NEXRAD Amazon S3 bucket. ```python import nexradaws conn = nexradaws.NexradAwsInterface() ``` -------------------------------- ### NexradAwsInterface class and __init__ Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/nexradawsinterface.html Defines the NexradAwsInterface class with its constructor. It sets up regular expressions for parsing S3 keys and connects to the 'noaa-nexrad-level2' bucket, disabling signing for public access. ```python class NexradAwsInterface(object): """ Instantiate an instance of this class to get a connection to the Nexrad AWS bucket. \ This class provides methods to query for various metadata of the AWS bucket as well \ as download files. >>> import nexradaws >>> conn = nexradaws.NexradAwsInterface() """ def __init__(self): super(NexradAwsInterface, self).__init__() self._year_re = re.compile(r'^(\d{4})/') self._month_re = re.compile(r'^\d{4}/(\d{2})') self._day_re = re.compile(r'^\d{4}/\d{2}/(\d{2})') self._radar_re = re.compile(r'^\d{4}/\d{2}/\d{2}/(....)/') self._scan_re = re.compile(r'^\d{4}/\d{2}/\d{2}/..../(?:(?=(.*.gz))|(?=(.*V0*.gz))|(?=(.*V0*)))') self._s3conn = boto3.resource('s3') self._s3conn.meta.client.meta.events.register('choose-signer.s3.*', disable_signing) self._bucket = self._s3conn.Bucket('noaa-nexrad-level2') ``` -------------------------------- ### open() Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/resources/localnexradfile.html Method to open the local NEXRAD file and return a file object for reading. ```APIDOC ## open() ### Description Provides a file object to the local NEXRAD radar file. Be sure to close the file object when processing is complete. ### Method open ### Parameters None ### Returns - **file** (file) - file object ready for reading ### Example ```python local_file = LocalNexradFile(awsnexradfile, localfilepath) file_obj = local_file.open() # process file file_obj.close() ``` ``` -------------------------------- ### Plot reflectivity data with pyart Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Opens each downloaded NEXRAD file using open_pyart() and plots the reflectivity field with a 150 km zoom. Uses results.iter_success() to iterate over successful downloads. ```python fig = plt.figure(figsize=(16,12)) for i,scan in enumerate(results.iter_success(),start=1): ax = fig.add_subplot(2,2,i) radar = scan.open_pyart() display = pyart.graph.RadarDisplay(radar) display.plot('reflectivity',0,ax=ax,title="{} {}".format(scan.radar_id,scan.scan_time)) display.set_limits((-150, 150), (-150, 150), ax=ax) ``` -------------------------------- ### Print successful downloads with results.success Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Prints the list of successfully downloaded LocalNexradFile objects. The success attribute returns a list of LocalNexradFile objects. ```python print(results.success) ``` ```output [, , , ] ``` -------------------------------- ### Iterate over successful downloads with iter_success Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Loops through the successfully downloaded files using the iter_success generator method, printing the radar ID and scan time for each. ```python for scan in results.iter_success(): print ("{} volume scan time {}".format(scan.radar_id,scan.scan_time)) ``` ```output KTLX volume scan time 2013-05-31 22:01:14+00:00 KTLX volume scan time 2013-05-31 22:05:37+00:00 KTLX volume scan time 2013-05-31 22:10:11+00:00 KTLX volume scan time 2013-05-31 22:14:45+00:00 ``` -------------------------------- ### NexradAwsInterface.get_avail_days(year, month) Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/nexradawsinterface.html Returns a list of strings representing the days available in a given year and month. ```APIDOC ## NexradAwsInterface.get_avail_days(year, month) ### Description Returns a list of strings representing the days available in a given year and month. ### Method GET ### Endpoint NexradAwsInterface.get_avail_days(year, month) ### Parameters #### Path Parameters - **year** (str or int) - Required - The year we are requesting available days for (i.e. 2010) - **month** (str or int) - Required - The month we are requesting available days for (i.e. 05) ### Request Example ```python conn.get_avail_days('2013','05') ``` ### Response #### Success Response - **return** (list) - A list of strings representing the days available in the given month and year #### Response Example ```python [u'01', u'02', u'03', u'04', u'05', u'06', u'07', u'08', u'09', u'10', u'11', u'12', u'13', u'14', u'15', u'16', u'17', u'18', u'19', u'20', u'21', u'22', u'23', u'24', u'25', u'26', u'27', u'28', u'29', u'30', u'31'] ``` ``` -------------------------------- ### open_pyart() Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/resources/localnexradfile.html Method to read the NEXRAD archive file using pyart and return a pyart Radar object. ```APIDOC ## open_pyart() ### Description If pyart is available, this method reads in the NEXRAD archive file and returns a pyart Radar object. Raises ImportError if pyart is not installed. ### Method open_pyart ### Parameters None ### Returns - **pyart.core.Radar** - a pyart radar object ### Raises - **ImportError** - if pyart module is not installed ### Example ```python local_file = LocalNexradFile(awsnexradfile, localfilepath) radar = local_file.open_pyart() ``` ``` -------------------------------- ### Plot velocity data with pyart Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Opens each downloaded NEXRAD file using open_pyart() and plots the velocity field with a 150 km zoom. Uses results.iter_success() to iterate over successful downloads. ```python fig = plt.figure(figsize=(16,12)) for i,scan in enumerate(results.iter_success(),start=1): ax = fig.add_subplot(2,2,i) radar = scan.open_pyart() display = pyart.graph.RadarDisplay(radar) display.plot('velocity',1,ax=ax,title="{} {}".format(scan.radar_id,scan.scan_time)) display.set_limits((-150, 150), (-150, 150), ax=ax) ``` -------------------------------- ### Download radar files with conn.download Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Downloads the first 4 scans from the query. The download method returns a DownloadResult object. The success attribute lists successfully downloaded LocalNexradFile objects. ```python results = conn.download(scans[0:4], templocation) ``` ```output Downloaded KTLX20130531_221445_V06.gz Downloaded KTLX20130531_220537_V06.gz Downloaded KTLX20130531_221011_V06.gz Downloaded KTLX20130531_220114_V06.gz 4 out of 4 files downloaded...0 errors ``` -------------------------------- ### Imports for nexradaws.nexradawsinterface Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/nexradawsinterface.html Imports required for the NexradAwsInterface class, including boto3, pytz, and internal resource modules. ```python import os import re from datetime import timedelta import boto3 import errno import pytz import six from botocore.handlers import disable_signing from .resources.downloadresults import DownloadResults from .resources.localnexradfile import LocalNexradFile from .resources.awsnexradfile import AwsNexradFile import concurrent.futures ``` -------------------------------- ### Iterate over successful downloads Source: https://nexradaws.readthedocs.io/en/latest/apidocs.html Shows how to use iter_success() to iterate over successful downloads from a DownloadResults object. The generator yields LocalNexradFile objects for each successful download. ```python >>> for localnexradfile in downloads.iter_success(): >>> ...do something... ``` -------------------------------- ### DownloadResults.iter_success() Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/resources/downloadresults.html A generator function that allows you to iterate over successful downloads. ```APIDOC ## DownloadResults.iter_success() ### Description A generator function that allows you to iterate over successful downloads. ### Method iter_success() ### Parameters None ### Returns A generator yielding LocalNexradFile objects representing successful downloads. ### Example ```python for localnexradfile in downloads.iter_success(): ...do something... ``` ``` -------------------------------- ### download Source: https://nexradaws.readthedocs.io/en/latest/apidocs.html Download the passed AwsNexradFile object(s) to the given basepath folder. If keep_aws_folders is True then subfolders will be created under the basepath with the same structure as on AWS. ```APIDOC ## [FUNCTION] download ### Description Download the passed AwsNexradFile object(s) to the given basepath folder. If keep_aws_folders is True then subfolders will be created under the basepath with the same structure as on AWS. ### Method FUNCTION ### Endpoint download(awsnexradfiles, basepath, keep_aws_folders=False, threads=6) ### Parameters #### Path Parameters - **awsnexradfiles** (list) - Required - A list of `AwsNexradFile` objects to download - **basepath** (str) - Required - location to save downloaded files - **keep_aws_folders** (bool) - Optional - weather or not to use the aws folder structure inside the basepath…(year/month/day/radar/) - **threads** (int) - Optional - number of download threads to utilize (default=6) ### Response #### Success Response - **DownloadResults** - A `DownloadResults` object that contains successful downloads as `LocalNexradFile` objects as well as any `AwsNexradFile` objects that failed ``` -------------------------------- ### Check failed download count with failed_count Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Prints the number of failed downloads using the failed_count attribute. ```python print("{} downloads failed.".format(results.failed_count)) ``` ```output 0 downloads failed. ``` -------------------------------- ### NexradAwsInterface.get_avail_months(year) Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/nexradawsinterface.html Returns a list of strings representing the months available in a given year. ```APIDOC ## NexradAwsInterface.get_avail_months(year) ### Description Returns a list of strings representing the months available in a given year. ### Method GET ### Endpoint NexradAwsInterface.get_avail_months(year) ### Parameters #### Path Parameters - **year** (str or int) - Required - The year we are requesting available months for (i.e. 2010) ### Request Example ```python conn.get_avail_months('2013') ``` ### Response #### Success Response - **return** (list) - A list of strings representing the months available for that year #### Response Example ```python [u'01', u'02', u'03', u'04', u'05', u'06', u'07', u'08', u'09', u'10', u'11', u'12'] ``` ``` -------------------------------- ### Print failed downloads list with results.failed Source: https://nexradaws.readthedocs.io/en/latest/Tutorial.html Prints the list of failed AwsNexradFile objects using the failed attribute. ```python print(results.failed) ``` ```output [] ``` -------------------------------- ### NexradAwsInterface.get_avail_years() Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/nexradawsinterface.html Returns a list of strings representing the years currently available in the Nexrad AWS bucket. ```APIDOC ## NexradAwsInterface.get_avail_years() ### Description Returns a list of strings representing the years currently available in the Nexrad AWS bucket. ### Method GET ### Endpoint NexradAwsInterface.get_avail_years() ### Parameters None ### Request Example ```python conn.get_avail_years() ``` ### Response #### Success Response - **return** (list) - A list of strings representing the years available #### Response Example ```python [u'1991', u'1992', u'1993', u'1994', u'1995', u'1996', u'1997', u'1998', u'1999', u'2000', u'2001', u'2002', u'2003', u'2004', u'2005', u'2006', u'2007', u'2008', u'2009', u'2010', u'2011', u'2012', u'2013', u'2014', u'2015', u'2016', u'2017'] ``` ``` -------------------------------- ### LocalNexradFile Source: https://nexradaws.readthedocs.io/en/latest/apidocs.html This class contains metadata about the local NEXRAD file as well as methods to open the file. ```APIDOC ## [CLASS] LocalNexradFile ### Description This class contains metadata about the local NEXRAD file as well as methods to open the file. ### Attributes - **key** (str) - AWS key for this NEXRAD file - **last_modified** (datetime) - when the file was last modified on AWS - **filename** (str) - the NEXRAD filename - **scan_time** (datetime) - volume scan time for the NEXRAD file - **radar_id** (str) - the four letter radar id (i.e. KTLX) - **filepath** - absolute path to the downloaded file on the local system ### Methods #### open ##### Description Provides a file object to the local nexrad radar file. Be sure to close the file object when processing is complete. ##### Returns - **file** - file object ready for reading #### open_pyart ##### Description If pyart is available this method will read in the nexrad archive file and return a pyart Radar object. ##### Returns - **pyart.core.Radar** - a pyart radar object ``` -------------------------------- ### DownloadResults class source code Source: https://nexradaws.readthedocs.io/en/latest/_modules/nexradaws/resources/downloadresults.html The full source code for the DownloadResults class, including initialization, properties for accessing success/failure lists and counts, and generator methods for iterating over results. ```python [docs]class DownloadResults(object): """ This class contains the results of the download call as well as methods for accessing \ the results. :var success: a list of :class:`LocalNexradFile ` \ objects representing the successful downloads :vartype success: list :var failed: a list of any :class:`AwsNexradFile ` \ objects that failed to download :vartype error: list :var success_count: The number of successful downloads :vartype success_count: int :var failed_count: The number of downloads that failed :vartype failed_count: int :var total: The total number of nexrad files that were attempted :vartype total: int """ def __init__(self,localfiles,failedfiles): super(DownloadResults, self).__init__() self._successfiles = localfiles self._failedfiles = failedfiles @property def failed_count(self): return len(self._failedfiles) @property def success_count(self): return len(self._successfiles) @property def total(self): return self.success_count + self.failed_count @property def success(self): return self._successfiles @property def failed(self): return self._failedfiles [docs] def iter_success(self): """ A generator function that allows you to iterate over successful downloads >>> for localnexradfile in downloads.iter_success(): >>> ...do something... """ for nexradfile in self.success: yield nexradfile [docs] def iter_failed(self): """ A generator function that allows you to iterate over failed downloads >>> for remotenexradfile in downloads.iter_failed(): >>> ...do something... """ for awsnexradfile in self.failed: yield awsnexradfile ```