mirror of
https://github.com/Telecominfraproject/wlan-lanforge-scripts.git
synced 2025-11-01 11:18:03 +00:00
csv-convert: Open-code the csv manipulation.
This makes it easier to deal with slightly different input formats, and allows us to properly convert Mbps, Gbps, Kbps, bps to Mbps units. Signed-off-by: Ben Greear <greearb@candelatech.com>
This commit is contained in:
@@ -14,34 +14,53 @@ if 'py-json' not in sys.path:
|
|||||||
sys.path.append(os.path.join(os.path.abspath('..'), 'py-json'))
|
sys.path.append(os.path.join(os.path.abspath('..'), 'py-json'))
|
||||||
|
|
||||||
class CSVParcer():
|
class CSVParcer():
|
||||||
def __init__(self,csv_infile=None,csv_outfile=None):
|
def __init__(self,csv_infile=None,csv_outfile=None,ddb=False):
|
||||||
|
|
||||||
# Grab the appropriate columns from Candela csv and place in the Comcast csv
|
idx = 0
|
||||||
include_summary = ['Atten','Rotation','Rx-Bps']
|
i_atten = -1
|
||||||
self.csv_infile = csv_infile
|
i_rotation = -1
|
||||||
self.csv_outfile = csv_outfile
|
i_rxbps = -1
|
||||||
|
fpo = open(csv_outfile, "w")
|
||||||
|
with open(csv_infile) as fp:
|
||||||
|
line = fp.readline()
|
||||||
|
if not line:
|
||||||
|
exit(1)
|
||||||
|
x = line.split(",")
|
||||||
|
cni = 0
|
||||||
|
for cn in x:
|
||||||
|
if (cn == "Atten"):
|
||||||
|
i_atten = cni
|
||||||
|
if (cn == "Rotation"):
|
||||||
|
i_rotation = cni
|
||||||
|
if (cn == "Rx-Bps"):
|
||||||
|
i_rxbps = cni
|
||||||
|
cni += 1
|
||||||
|
|
||||||
try:
|
# Write out out header
|
||||||
#dataframe = pd.read_csv(self.csv_infile,header = 0, usecols = lambda column : any(substr in column for substr in include_summary ))
|
fpo.write("Step Index,Attenuation [dB],Position [Deg],Traffic Pair 1 Throughput [Mbps]\n")
|
||||||
dataframe = pd.read_csv(self.csv_infile,header = 0, usecols = lambda column : column in include_summary )
|
|
||||||
except:
|
|
||||||
print("Input file not accessible, please check for presence of input file")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
dataframe.index.name = 'Step Index'
|
line = fp.readline()
|
||||||
dataframe = dataframe.replace('Mbps','',regex=True)
|
|
||||||
dataframe = dataframe.rename(columns={'Atten':'Attenuation [dB]','Rotation':'Position [Deg]','Rx-Bps':'Traffic Pair 1 Throughput [Mbps]'})
|
|
||||||
|
|
||||||
#print('{}'.format(self.csv_infile))
|
step_i = 0
|
||||||
#print("dataframe {}".format(dataframe))
|
while line:
|
||||||
csv_summary = self.csv_outfile
|
x = line.split(",")
|
||||||
csv_summary = os.path.splitext(csv_summary)[0] + '.csv'
|
mbps_data = x[i_rxbps]
|
||||||
|
mbps_array = mbps_data.split(" ")
|
||||||
dataframe.to_csv(csv_summary, index = True, header=True)
|
mbps_val = float(mbps_array[0])
|
||||||
xlsx_summary = os.path.splitext(csv_summary)[0] + '.xlsx'
|
if (mbps_array[1] == "Gbps"):
|
||||||
dataframe.to_excel(xlsx_summary, index = True, header=True)
|
mbps_val *= 1000
|
||||||
print("INFILE: {} OUTFILE(xlsx): {} OUTFILE(csv): {}".format(csv_infile,xlsx_summary,csv_summary))
|
if (mbps_array[1] == "Kbps"):
|
||||||
|
mbps_val /= 1000
|
||||||
|
if (mbps_array[1] == "bps"):
|
||||||
|
mbps_val /= 1000000
|
||||||
|
|
||||||
|
attenv = int(x[i_atten])
|
||||||
|
if ddb:
|
||||||
|
attenv /= 10
|
||||||
|
|
||||||
|
fpo.write("%s,%s,%s,%s\n" % (step_i, attenv, x[i_rotation], mbps_val))
|
||||||
|
line = fp.readline()
|
||||||
|
step_i += 1
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
@@ -61,7 +80,9 @@ csv_convert.py:
|
|||||||
|
|
||||||
# for testing parser.add_argument('-i','--infile', help="input file of csv data", default='text-csv-0-candela.csv')
|
# for testing parser.add_argument('-i','--infile', help="input file of csv data", default='text-csv-0-candela.csv')
|
||||||
parser.add_argument('-i','--infile', help="input file of csv data", required=True)
|
parser.add_argument('-i','--infile', help="input file of csv data", required=True)
|
||||||
parser.add_argument('-o','--outfile', help="output file of csv, xlsx data , default: outfile.xlsx outfile.csv", default='outfile')
|
parser.add_argument('-d','--ddb', help="Specify attenuation units are in ddb in source file",
|
||||||
|
action='store_true', default=False)
|
||||||
|
parser.add_argument('-o','--outfile', help="output file in .csv format", default='outfile.csv')
|
||||||
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
@@ -72,7 +93,7 @@ csv_convert.py:
|
|||||||
if args.outfile:
|
if args.outfile:
|
||||||
csv_outfile_name = args.outfile
|
csv_outfile_name = args.outfile
|
||||||
|
|
||||||
CSVParcer(csv_infile_name,csv_outfile_name)
|
CSVParcer(csv_infile_name, csv_outfile_name, args.ddb)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user