|
| 1 | +# The source code has been authored by federal and non-federal employees. To the extent that a federal employee is an author of a portion of this software or a derivative work thereof, no copyright is claimed by the United States Government, as represented by the Secretary of the Navy ("GOVERNMENT") under Title 17, U.S. Code. All Other Rights Reserved. To the extent that a non-federal employee is an author of a portion of this software or a derivative work thereof, the work was funded in whole or in part by the U.S. Government, and is, therefore, subject to the following license: The Government has unlimited rights to use, modify, reproduce, release, perform, display, or disclose the computer software and computer software documentation in whole or in part, in any manner and for any purpose whatsoever, and to have or authorize others to do so. Any other rights are reserved by the copyright owner. |
| 2 | + |
| 3 | +# Neither the name of NRL or its contributors, nor any entity of the United States Government may be used to endorse or promote products derived from this software, nor does the inclusion of the NRL written and developed software directly or indirectly suggest NRL's or the United States Government's endorsement of this product. |
| 4 | + |
| 5 | +# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 6 | + |
| 7 | +import os |
| 8 | +import csv |
| 9 | +import pickle |
| 10 | +import shutil |
| 11 | + |
| 12 | +class FileIO: |
| 13 | + dataFolderName = "data/" |
| 14 | + logFolderName = "logs/" |
| 15 | + dataDir = "" |
| 16 | + folder = "" |
| 17 | + |
| 18 | + def __init__(self): |
| 19 | + #folder = "" |
| 20 | + #folderFinal = folderTry |
| 21 | + #i = 1 |
| 22 | + #while os.path.isdir(folderFinal): |
| 23 | + # folderFinal = folderTry+"_"+str(i) |
| 24 | + # i+=1 |
| 25 | + #print("Saving simulation in folder:",folderFinal) |
| 26 | + #os.makedirs(folderFinal) |
| 27 | + #self.folder = folderFinal+"/" |
| 28 | + self.folder = ""# Just make the simulation folders in the local directory |
| 29 | + self.dataDir = self.folder + self.dataFolderName |
| 30 | + self.logDir = self.folder + self.logFolderName |
| 31 | + |
| 32 | + self.simFolders = [self.dataDir, self.logDir] |
| 33 | + self.simFolders = list(set(self.simFolders)) # Select unique (in case there are duplicates) |
| 34 | + if any([os.path.exists(f) for f in self.simFolders]): # If any folders already exist |
| 35 | + askDelete = input("Simulation folders already exist. Delete them? (y/n)") |
| 36 | + print("input is "+askDelete) |
| 37 | + if askDelete == "y": |
| 38 | + print("Deleting existing folders") |
| 39 | + for f in self.simFolders: |
| 40 | + if os.path.exists(f): |
| 41 | + shutil.rmtree(f) |
| 42 | + else: |
| 43 | + print("Cannot run simulation because folders already exist. Quitting.") |
| 44 | + quit() |
| 45 | + for f in self.simFolders: |
| 46 | + os.makedirs(f) |
| 47 | + |
| 48 | + def saveTextFile(self, textArr, fname): |
| 49 | + fname = self.logDir+fname+".txt" |
| 50 | + if os.path.exists(fname): |
| 51 | + print("Could not save file",fname) |
| 52 | + else: |
| 53 | + f = open(fname,"w") |
| 54 | + for row in textArr: |
| 55 | + f.write(row) |
| 56 | + f.write('\n') |
| 57 | + f.close() |
| 58 | + |
| 59 | + def saveDataTextFile(self, textArr, fname): |
| 60 | + fname = self.dataDir+fname+".txt" |
| 61 | + if os.path.exists(fname): |
| 62 | + print("Could not save file",fname) |
| 63 | + else: |
| 64 | + f = open(fname,"w") |
| 65 | + for row in textArr: |
| 66 | + f.write(row) |
| 67 | + f.write('\n') |
| 68 | + f.close() |
| 69 | + |
| 70 | + def makeCSVFile(self, fname, labels): |
| 71 | + newfname = self.dataDir+fname+'.csv' |
| 72 | + with open(newfname,'w') as f: |
| 73 | + f.write(','.join([str(e) for e in labels])) |
| 74 | + return newfname |
| 75 | + |
| 76 | + def appendRow(self, fname, numbers): |
| 77 | + with open(fname,'a') as f: |
| 78 | + f.write('\n'+','.join([str(e) for e in numbers])) |
| 79 | + |
| 80 | + def saveDataFile(self, frameData, fname, labels=None, info=None): |
| 81 | + fname = self.dataDir+fname+".csv" |
| 82 | + if os.path.exists(fname): |
| 83 | + print("Could not save file",fname) |
| 84 | + else: |
| 85 | + if labels is None: |
| 86 | + f = open(fname,"w", newline='') |
| 87 | + writer = csv.writer(f) |
| 88 | + for row in frameData: |
| 89 | + writer.writerow(row) |
| 90 | + f.close() |
| 91 | + else: |
| 92 | + f = open(fname,"w", newline='') |
| 93 | + writer = csv.writer(f) |
| 94 | + writer.writerow([info]) |
| 95 | + writer.writerow(labels) |
| 96 | + for row in frameData: |
| 97 | + writer.writerow(row) |
| 98 | + f.close() |
| 99 | + def savePickle(self, data, fname): |
| 100 | + with open(self.dataDir+fname+'.pckl', 'wb') as f: |
| 101 | + pickle.dump(data, f) |
0 commit comments