# -*- coding: utf-8 -*-
"""
Created on Mon Sep 28 12:55:48 2020

@author: jbobowsk
"""

# In this Python script we attempt to develop a scheme to import and then
# process data from multiple files.
import numpy as np 

# Before importing multiple files, we first have to learn about some of the
# properties of the '%' operator.  The command '%f %n' outputs the floating
# point number n.
print("%f" %3.21)

# If we put a number m in front of the f, it specifies that the number
# output should contain m characters.  In the example below, there's lots
# of white space in front of the output.
print("%24f" %3.21)

# We can force the white space to be filled with zeros by putting a 0
# (zero) in front of the number m.
print("%024f" %3.21)

# We can also specify the number of characters that should follow the
# decimal point using .p before the f where p is a postive integer.
print("%024.6f" %3.21)
print("%024.6f" %321)

# Suppose our files a labelled something like file001data.txt,
# file002data.txt, ...  We can use %f inside a loop to make the 001,
# 002, ...  We want our numbers to be m = 3 characters long and to have p =
# 0 characters after the decimal place.
for i in range(3):
    print("%03.0f" %i)

# We combine our %f statements with additional text to create the full file
# name.
for i in range(3):
    print("file%03.0fdata.txt" %i)

# The following acheives exactly the same output but, in my opinion, is a little
# cleaner.
head = "file"
toe = "data.txt"
for i in range(3):
    print(head + "%03.0f" %i + toe)

dataArray = []
for i in range(1, 6, 1):
    dataArray = dataArray + [np.loadtxt(head + "%03.0f" %i + toe)]

for i in range(5):
    print('The contents of file ', i + 1, ': ', dataArray[i], sep = '')
    
# Maybe we only want every second file to be imported.  Only a slight
# change to the for loop control statement is required.    
oddData = []
for i in range(1, 6, 2):
    oddData = oddData + [np.loadtxt(head + "%03.0f" %i + toe)]

for i in range(3):
    print('The contents of every odd file ', 2*i + 1, ': ', oddData[i], sep = '')
    
evenData = []
for i in range(2, 6, 2):
    evenData = evenData + [np.loadtxt(head + "%03.0f" %i + toe)]

for i in range(2):
    print('The contents of every even file ', 2*(i + 1), ': ', evenData[i], sep = '')
    
# If you want to select only specific files to be imported using
# non-sequential numbers, you can do the following.
selectedData = []
importList = [1, 2, 3, 4, 5, 8, 12]
for i in range(len(importList)):
    selectedData = selectedData + [np.loadtxt(head + "%03.0f" %importList[i] + toe)]
    
for i in range(len(importList)):
    print('The contents of each selected file %02.0f' %importList[i], ': ', selectedData[i], sep = '')
    
# Now we can do calculations using the imported data.  For example, the
# element-by-element average of the 7 imported files is:
elementAvg = []
for j in range(len(selectedData[0])):
    tot = 0
    for i in range(len(importList)):
        tot += selectedData[i][j]
    elementAvg += [tot/len(importList)]
print(elementAvg)

# Here's the average of each individual file:
fileAvg = []
for i in range(len(importList)):
    tot = 0;
    for j in range(len(selectedData[0])):
        tot += selectedData[i][j]
    fileAvg += [tot/len(selectedData[0])]
print(fileAvg)