1 | # -*- coding: utf-8 -*- |
---|
2 | """ |
---|
3 | ------------------------------------------------------------------------------ |
---|
4 | Mango 802.11 Reference Design Experiments Framework - Sample Log Data Utilities |
---|
5 | ------------------------------------------------------------------------------ |
---|
6 | License: Copyright 2019 Mango Communications, Inc. All rights reserved. |
---|
7 | Use and distribution subject to terms in LICENSE.txt |
---|
8 | ------------------------------------------------------------------------------ |
---|
9 | |
---|
10 | Provides sample data to use with the log examples. |
---|
11 | |
---|
12 | """ |
---|
13 | |
---|
14 | _SAMPLE_DATA_DIR = 'sample_data' |
---|
15 | |
---|
16 | _FILES_TO_DL = [ |
---|
17 | 'ap_two_node_two_flow_capture.hdf5', |
---|
18 | 'sta_two_node_two_flow_capture.hdf5', |
---|
19 | 'ap_one_node_capture.hdf5', |
---|
20 | 'np_rx_ofdm_entries.hdf5', |
---|
21 | ] |
---|
22 | |
---|
23 | def get_sample_data_dir(): |
---|
24 | import os |
---|
25 | |
---|
26 | # Find wlan_exp.log directory using this module's location as a reference |
---|
27 | sd_parent = os.path.split(os.path.abspath(__file__))[0] |
---|
28 | |
---|
29 | # Construct the full path to the sample_data directory |
---|
30 | sd_path = os.path.join(sd_parent, _SAMPLE_DATA_DIR) |
---|
31 | |
---|
32 | # Only return successfully if the directory already exists |
---|
33 | if(os.path.isdir(sd_path)): |
---|
34 | return sd_path |
---|
35 | else: |
---|
36 | raise Exception("ERROR: Sample data directory not found in wlan_exp.log package!\n") |
---|
37 | |
---|
38 | # End def |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | def download_sample_data(): |
---|
43 | import os, sys |
---|
44 | import wlan_exp.version as ver |
---|
45 | |
---|
46 | sample_data_dir = get_sample_data_dir() |
---|
47 | |
---|
48 | url_root = 'http://warpproject.org/dl/refdes/802.11/sample_data/{}.{}.{}/'.format(ver.WLAN_EXP_MAJOR, ver.WLAN_EXP_MINOR, ver.WLAN_EXP_REVISION) |
---|
49 | |
---|
50 | try: |
---|
51 | import requests |
---|
52 | except ImportError: |
---|
53 | print("\nERROR: auto download requires the Python requests package!\n") |
---|
54 | print(" Please download sample data files manually from:") |
---|
55 | print(" http://warpproject.org/w/802.11/wlan_exp/sample_data\n") |
---|
56 | print(" Sample data files should be saved in local folder:") |
---|
57 | print(" {0}".format(os.path.normpath(sample_data_dir))) |
---|
58 | return |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | print("Downloading 802.11 Reference Design sample data to local directory:") |
---|
63 | print(" From: {0}".format(url_root)) |
---|
64 | print(" To: {0}\n".format(os.path.normpath(sample_data_dir))) |
---|
65 | |
---|
66 | # Progress indicator based on great StackOverflow posts: |
---|
67 | # http://stackoverflow.com/questions/15644964/python-progress-bar-and-downloads |
---|
68 | |
---|
69 | for fn in _FILES_TO_DL: |
---|
70 | fn_full = os.path.join(sample_data_dir, fn) |
---|
71 | |
---|
72 | if(os.path.isfile(fn_full)): |
---|
73 | print("\rERROR: Local file {0} already exists".format(fn_full)) |
---|
74 | else: |
---|
75 | r = requests.get(url_root + fn, stream=True) |
---|
76 | len_total = r.headers.get('content-length') |
---|
77 | |
---|
78 | if(r.status_code != 200): |
---|
79 | print("\rERROR: Failed to download {0}; HTTP status {1}".format(url_root + fn, r.status_code)) |
---|
80 | else: |
---|
81 | with open(fn_full, "wb") as f: |
---|
82 | if len_total is None: # no content length header |
---|
83 | f.write(r.content) |
---|
84 | else: |
---|
85 | print("Downloading {0} ({1:5.2f} MB)".format(fn, float(len_total)/2**20)) |
---|
86 | len_done = 0 |
---|
87 | len_total = int(len_total) |
---|
88 | for data_chunk in r.iter_content(chunk_size=2**17): |
---|
89 | len_done += len(data_chunk) |
---|
90 | f.write(data_chunk) |
---|
91 | done = int(70 * len_done / len_total) |
---|
92 | sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (70-done)) ) |
---|
93 | sys.stdout.flush() |
---|
94 | print('\r') |
---|
95 | |
---|
96 | # End def |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | def get_sample_data_file(filename): |
---|
101 | import os |
---|
102 | |
---|
103 | sample_data_dir = get_sample_data_dir() |
---|
104 | |
---|
105 | file_path = os.path.join(sample_data_dir, filename) |
---|
106 | |
---|
107 | if(os.path.isfile(file_path)): |
---|
108 | return file_path |
---|
109 | else: |
---|
110 | msg = "ERROR: sample data file {0} not found in local sample data directory!\n".format(filename) |
---|
111 | msg += " Please ensure that sample data has been downloaded. Instructions at:\n" |
---|
112 | msg += " https://warpproject.org/trac/wiki/802.11/wlan_exp/sample_data" |
---|
113 | raise IOError(msg) |
---|
114 | |
---|
115 | # End def |
---|