| 1 | ###################################################################### |
|---|
| 2 | # Import a calibration information file and construct a dictionary |
|---|
| 3 | ###################################################################### |
|---|
| 4 | |
|---|
| 5 | def importCalibrationInformation(calfilename): |
|---|
| 6 | """ Import calibration information file |
|---|
| 7 | |
|---|
| 8 | Return: |
|---|
| 9 | 1. ID of the bank to refine |
|---|
| 10 | 2. Calibration Information Dictionary |
|---|
| 11 | """ |
|---|
| 12 | try: |
|---|
| 13 | calfile = open(calfilename, "r") |
|---|
| 14 | filelines = calfile.readlines() |
|---|
| 15 | calfile.close() |
|---|
| 16 | except IOError: |
|---|
| 17 | print "Calibration information file %s is not readable or accessible. " % (calfilename) |
|---|
| 18 | raise NotImplementedError("Calibration information file %s is not readable or accessible. " % (calfilename)) |
|---|
| 19 | |
|---|
| 20 | caldict = {} |
|---|
| 21 | |
|---|
| 22 | bankid = -1 |
|---|
| 23 | for rawline in filelines: |
|---|
| 24 | line = rawline.strip() |
|---|
| 25 | |
|---|
| 26 | if len(line) == 0: |
|---|
| 27 | # Empty line |
|---|
| 28 | continue |
|---|
| 29 | elif line[0] == "#": |
|---|
| 30 | # Comment line |
|---|
| 31 | continue |
|---|
| 32 | else: |
|---|
| 33 | # Information line |
|---|
| 34 | terms = line.split("=") |
|---|
| 35 | |
|---|
| 36 | if len(terms) != 2: |
|---|
| 37 | # Type of line not defined |
|---|
| 38 | print "Bad line: %s" % (line) |
|---|
| 39 | else: |
|---|
| 40 | # Well defined |
|---|
| 41 | parname = terms[0].strip() |
|---|
| 42 | valuestr = terms[1].strip() |
|---|
| 43 | |
|---|
| 44 | if parname.lower() == "bank" and valuestr.lower() != "general": |
|---|
| 45 | # Starting of a new bank |
|---|
| 46 | bankid = int(valuestr) |
|---|
| 47 | caldict[bankid] = {} |
|---|
| 48 | elif parname.lower() == "bank": |
|---|
| 49 | # Skip information |
|---|
| 50 | continue |
|---|
| 51 | else: |
|---|
| 52 | # Regular Parameter = Value |
|---|
| 53 | if bankid < 0: |
|---|
| 54 | caldict[parname] = valuestr |
|---|
| 55 | else: |
|---|
| 56 | caldict[bankid][parname] = valuestr |
|---|
| 57 | # ENDIFELSE |
|---|
| 58 | # ENDIFELSE |
|---|
| 59 | # ENDIFELSE |
|---|
| 60 | # ENNDIFELSE |
|---|
| 61 | # ENDFOR |
|---|
| 62 | |
|---|
| 63 | return (int(caldict["WORKING_BANKID"]), caldict) |
|---|
| 64 | |
|---|
| 65 | if __name__=="__main__": |
|---|
| 66 | filename = "/home/wzz/Projects/MantidTests/LeBailFit/FinalExam/Calibration_Information.txt" |
|---|
| 67 | wkbankid, caldict = importCalibrationInformation(filename) |
|---|
| 68 | print wkbankid |
|---|
| 69 | print caldict.keys() |
|---|
| 70 | print caldict[1].keys() |
|---|
| 71 | print caldict[1].values() |
|---|
| 72 | |
|---|