def import_transmission_data_raw(compound_name, verbose=0): """ Takes a name for a material and outputs the wavelength axis (in eV) and the absorption for 1 micron. INPUT: material: name of material. The data for the gas has to be in '/Data/TransmissionSpectra/' and in the python file there. OUTPUT: - wl_nm (ndarray): wavelength axis - tr_norm (ndarray): transmission, normalized to 1 mm CHANGELOG: """ # find path path = NPCTools.Data.TransmissionSpectra.TransmissionSpectraData.data_path( ) # convert to title case compound_name = compound_name.lower() files_list = NPCTools.Data.TransmissionSpectra.TransmissionSpectraData.files_list( ) # find the name in the list mat = [item for item in files_list if compound_name in item["compound"]] # if not name was found, throw an error if len(mat) == 0: DEBUG.printError("Compound %s not found" % compound_name, inspect.stack()) return [0], [0] else: mat = mat[0] if verbose > 0: s = "TransmissionSpectra.py:import_transmission_data: Found this item for element %s: %s" % ( compound_name, str(mat)) DEBUG.verbose(s, verbose, 1) print("Importing data for compound %s" % (compound_name)) # import data data = numpy.loadtxt(path + "/" + mat["filename"], comments="#", delimiter=",") wl = data[:, 0] tr = data[:, 1] if mat["wl_unit"] == "nm": wl_nm = wl wl_um = wl / 1000 elif mat["wl_unit"] == "um": wl_nm = 1000 * wl wl_um = wl if mat["tr_unit"] == "pct": tr /= 100 return wl_nm, tr
def import_transmission_data(compound_name, verbose=0): """ Takes a name for a material and outputs the wavelength axis (in eV) and the absorption for 1 micron. INPUT: material: name of material. The data for the gas has to be in '/Data/HenkeSolidTransmission/' and in the python file there. OUTPUT: - e_ev (ndarray): ev axis - tr_norm (ndarray): transmission, normalized to 1 micron CHANGELOG: """ # find path path = NPCTools.Data.HenkeSolidTransmission.HenkeSolidData.data_path() # convert to title case compound_name = compound_name.title() files_list = NPCTools.Data.HenkeSolidTransmission.HenkeSolidData.files_list( ) # find the name in the list mat = [item for item in files_list if compound_name in item["compound"]] # if not name was found, throw an error if len(mat) == 0: DEBUG.printError("Compound %s not found" % compound_name, inspect.stack()) return [0], [0] else: mat = mat[0] if verbose > 0: s = "HenkeSolids.py:import_transmission_data: Found this item for element %s: %s" % ( compound_name, str(mat)) DEBUG.verbose(s, verbose, 1) print("Importing data for compound %s" % (compound_name)) # import data data = numpy.loadtxt(path + "/" + mat["filename"], skiprows=2) e_ev = data[:, 0] tr = data[:, 1] # normalize ab = numpy.log10(tr) ab /= mat["um"] tr_norm = 10**ab return e_ev, tr_norm
def import_absorption_data(gas_name, verbose=0): """ Takes a name for a gas and outputs the wavelength axis (in eV) and the absorption for 1 mbar and 1 cm path length. INPUT: gas_name: name of gas. The data for the gas has to be in '/Data/HenkeGasTransmission/' and in the python file there. OUTPUT: - ev (ndarray): ev axis - absorption (ndarray): absorption, normalized to 1 mbar and 1 cm CHANGELOG: """ # find path path = NPCTools.Data.HenkeGasTransmission.HenkeGasData.data_path() # convert to title case gas_name = gas_name.title() files_list = NPCTools.Data.HenkeGasTransmission.HenkeGasData.files_list() gas = [item for item in files_list if item["gas"] == gas_name] if len(gas) == 0: DEBUG.printError("Gas %s not found" % gas_name, inspect.stack()) return [0], [0] else: gas = gas[0] if verbose > 0: s = "HenkeGas.py:import_absorption_data: Found this item for gas %s: %s" % ( gas_name, str(gas)) DEBUG.verbose(s, verbose, 1) print("Importing data for %s gas" % (gas_name)) data = numpy.loadtxt(path + "/" + gas["filename"], skiprows=2) ev = data[:, 0] transmission = data[:, 1] absorption = numpy.log10(transmission) # normalize to 1 mbar and 1 cm absorption /= gas["mbar"] absorption /= gas["cm"] return ev, absorption
def import_transmission_data(compound_name, wl_range=(), verbose=0): """ Takes a name for a material and outputs the wavelength axis (in eV) and the absorption for 1 micron. INPUT: material: name of material. The data for the gas has to be in '/Data/TransmissionSpectra/' and in the python file there. OUTPUT: - wl_nm (ndarray): wavelength axis - tr_norm (ndarray): transmission, normalized to 1 mm CHANGELOG: """ # find path path = NPCTools.Data.TransmissionSpectra.TransmissionSpectraData.data_path( ) # convert to title case compound_name = compound_name.lower() files_list = NPCTools.Data.TransmissionSpectra.TransmissionSpectraData.files_list( ) # find the name in the list mat = [item for item in files_list if compound_name in item["compound"]] # if not name was found, throw an error if len(mat) == 0: DEBUG.printError("Compound %s not found" % compound_name, inspect.stack()) return [0], [0] else: mat = mat[0] if verbose > 0: s = "TransmissionSpectra.py:import_transmission_data: Found this item for element %s: %s" % ( compound_name, str(mat)) DEBUG.verbose(s, verbose, 1) print("Importing data for compound %s" % (compound_name)) # import data data = numpy.loadtxt(path + "/" + mat["filename"], comments="#", delimiter=",") wl = data[:, 0] tr = data[:, 1] if wl[0] > wl[-1]: wl = wl[::-1] tr = tr[::-1] if mat["wl_unit"] == "nm": wl_nm = wl wl_um = wl / 1000 elif mat["wl_unit"] == "um": wl_nm = 1000 * wl wl_um = wl temp = numpy.where(wl_um <= wl_range[0])[0] if len(temp) > 0: wl_nm = wl_nm[temp[-1]:] wl_um = wl_um[temp[-1]:] tr = tr[temp[-1]:] temp = numpy.where(wl_um >= wl_range[1])[0] if len(temp) > 0: wl_nm = wl_nm[:temp[0]] wl_um = wl_um[:temp[0]] tr = tr[:temp[0]] a_deg = [0] path_to_ri_database = "/Users/rbloem/Developer/NPCTools/Data/RefractiveIndexDB/" paf = path_to_ri_database + mat["refractive_index_path"] # air -> material wl_um, a_deg, Rs, Rp = RI.get_reflectance(paf2=paf, wl_um=wl_um, a_deg=a_deg) Rs = Rs[0, :] Rp = Rp[0, :] if mat["tr_unit"] == "pct": tr /= 100 R = (1 - Rs)**2 absorption_component = tr / R #(1 - R) abs = -numpy.log10(absorption_component) abs_mm = abs / mat["mm"] numpy.putmask(abs_mm, abs_mm < 0, 0) return wl_nm, abs_mm, Rs, Rp
def printError(self, string, location=[]): DEBUG.printError(string, location)
def fit(x_array, y_array, function, A_start, return_all = False): """ Fit data 20101209/RB: started 20130131/RB: imported in Crocodile, added example to doc-string INPUT: x_array: the array with time or something y-array: the array with the values that have to be fitted function: one of the functions, in the format as in the file "Equations" A_start: a starting point for the fitting return_all: the function used to return only the final result. The leastsq method does however return more data, which may be useful for debugging. When the this flag is True, it will return these extras as well. For legacy purposes the default is False. See reference of leastsq method for the extra output: http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.leastsq.html OUTPUT: A_final: the final parameters of the fitting When return_all == True: - cov_x (ndarray): Uses the fjac and ipvt optional outputs to construct an estimate of the jacobian around the solution. None if a singular matrix encountered (indicates very flat curvature in some direction). This matrix must be multiplied by the residual variance to get the covariance of the parameter estimates - see curve_fit. - infodict (dict): a dictionary of optional outputs with the key s: - "nfev" : the number of function calls - "fvec" : the function evaluated at the output - "fjac" : A permutation of the R matrix of a QR factorization of the final approximate Jacobian matrix, stored column wise. Together with ipvt, the covariance of the estimate can be approximated. - "ipvt" : an integer array of length N which defines a permutation matrix, p, such that fjac*p = q*r, where r is upper triangular with diagonal elements of nonincreasing magnitude. Column j of p is column ipvt(j) of the identity matrix. - "qtf" : the vector (transpose(q) * fvec). - mesg (str): A string message giving information about the cause of failure. - ier (int): An integer flag. If it is equal to 1, 2, 3 or 4, the solution was found. Otherwise, the solution was not found. In either case, the optional output variable "mesg" gives more information. EXAMPLE: Fit some data to this function from Crocodile.Resources.Equations: def linear(A, t): return A[0] + A[1] * t ### x = x-axis y = some data A = [0,1] # initial guess A_final = fit(x, y, Crocodile.Resources.Equations.linear, A) ### WARNING: Always check the result, it might sometimes be sensitive to a good starting point. """ if scipy_import: param = (x_array, y_array, function) A_final, cov_x, infodict, mesg, ier = leastsq(minimize, A_start, args=param, full_output=True) if return_all: return A_final, cov_x, infodict, mesg, ier else: return A_final else: DEBUG.printError("Scipy.leastsq is not loaded. Fit is not done", inspect.stack()) return False