import copy
from operator import add
from utils.PhysicsQuantities import Isotope, Activity
from base import IllegalArgumentError
from base.StoredData import StoredData
from collections import OrderedDict

class ResnucReader:
    def __init__(self):
        pass

    @staticmethod
    def load(files):
        if isinstance(files, list):
            merged_data = None
            for file_name in files:
                data = ResnucReader._load(file_name)
                if merged_data:
                    ResnucReader._merge(merged_data, data)
                else:
                    merged_data = data
            return merged_data
        elif isinstance(files, str):
            return ResnucReader._load(files)
        else:
            raise IllegalArgumentError("Received unsupported type for input files: " + str(type(files)))

    @staticmethod
    def _load(filename):
        resnucl_data_dict = {}
        first_line_of_detector = False
        data_section = False
        isomere_section = False
        current_detector_name = None
        data = OrderedDict()

        for (i, line) in enumerate(file(filename)):
            if line.find("Detector n:") > 0:
                first_line_of_detector = True
                data_section = False
                isomere_section = False
            elif line.find("A/Z Isotopes:") > 0:
                first_line_of_detector = False
                data_section = True
                isomere_section = False
            elif line.find("A/Z/m Isomers:") > 0:
                first_line_of_detector = False
                data_section = False
                isomere_section = True
            elif first_line_of_detector:
                if current_detector_name is not None:
                    resnucl_data_dict[current_detector_name] = copy.copy(data)
                    current_detector_name = None
                    data = {}
                current_detector_name = line.strip()
                first_line_of_detector = False
            elif data_section:
                split_line = line.split()
                A = split_line[0]
                Z = int(split_line[1])
                value = float(split_line[2])
                error_percent = float(split_line[3])
                if value > 0.:
                    try:
                        data[Isotope(A, Z, 0)].append(Activity(value, unc=value * error_percent / 100.))
                    except KeyError:
                        data[Isotope(A, Z, 0)] = StoredData(Activity(value, unc=value * error_percent / 100.))
            elif isomere_section:
                split_line = line.split()
                if not len(split_line):
                    continue
                A = split_line[0]
                Z = int(split_line[1])
                iso = int(split_line[2])
                value = float(split_line[3])
                error_percent = float(split_line[4])
                if value > 0.:
                    try:
                        data[Isotope(A, Z, iso)].append(Activity(value, unc=value * error_percent / 100.))
                    except KeyError:
                        data[Isotope(A, Z, iso)] = StoredData(Activity(value, unc=value * error_percent / 100.))

        if current_detector_name is not None:
            resnucl_data_dict[current_detector_name] = copy.copy(data)

        return resnucl_data_dict

    @staticmethod
    def _merge(res, addend):
        for det in res.keys():
            all_isotopes = list(set(res[det].keys()) | set(addend[det].keys()))
            for isotope in all_isotopes:
                res[det][isotope] += (addend[det][isotope])



@@ -9,37 +9,31 @@
 opy%0A
-from operator import add%0Afrom
+import importlib%0Aimport
  uti
@@ -57,32 +57,13 @@
 ies 
-import Isotope, Activity
+as PQ
 %0Afro
@@ -172,16 +172,40 @@
 redDict%0A
+from utils import ureg%0A%0A
 %0Aclass R
@@ -242,42 +242,434 @@
 self
-):%0A        pass%0A%0A    @staticmethod
+, quantity=%22Activity%22, dim=None):%0A        %22%22%22%0A        Constructor for reader of RESNUClei scored data%0A        :param quantity (str): physics quantity scored; defaults to Activity%0A        :param dim (str): dimension of scored quantity%0A        :return:%0A        %22%22%22%0A        m = importlib.import_module(%22utils.PhysicsQuantities%22)%0A        if dim is not None:%0A            self.dim = ureg(dim)%0A        self.pq = getattr(m, quantity)%0A
 %0A   
@@ -674,24 +674,30 @@
    def load(
+self, 
 files):%0A    
@@ -818,28 +818,20 @@
  data = 
-ResnucReader
+self
 ._load(f
@@ -1079,28 +1079,20 @@
  return 
-ResnucReader
+self
 ._load(f
@@ -1217,34 +1217,16 @@
 les)))%0A%0A
-    @staticmethod%0A
     def 
@@ -1227,24 +1227,30 @@
   def _load(
+self, 
 filename):%0A 
@@ -2685,32 +2685,35 @@
            data%5B
+PQ.
 Isotope(A, Z, 0)
@@ -2717,32 +2717,31 @@
  0)%5D.append(
-Activity
+self.pq
 (value, unc=
@@ -2829,32 +2829,35 @@
            data%5B
+PQ.
 Isotope(A, Z, 0)
@@ -2867,32 +2867,31 @@
  StoredData(
-Activity
+self.pq
 (value, unc=
@@ -3355,32 +3355,35 @@
            data%5B
+PQ.
 Isotope(A, Z, is
@@ -3393,24 +3393,23 @@
 .append(
-Activity
+self.pq
 (value, 
@@ -3509,16 +3509,19 @@
    data%5B
+PQ.
 Isotope(
@@ -3545,24 +3545,23 @@
 redData(
-Activity
+self.pq
 (value, 

add some docstrings