def __call__(self, parser, ns, value, option_string=None): out = value[0] try: # We figure out if the user wants to write # to a geometry obj = get_sile(out, mode='w') if hasattr(obj, 'write_geometry'): with obj as fh: fh.write_geometry(ns._geometry) return raise NotImplementedError except: pass if len(ns._data) == 0: ns._data.append(ns._E) ns._data_header.append('Energy[eV]') ns._data.append(ns._PDOS_filter(ns._PDOS).sum(0)) if ns._PDOS_filter_name is not None: ns._data_header.append( f"DOS[spin={ns._PDOS_filter_name}][1/eV]") else: ns._data_header.append("DOS[1/eV]") from sisl.io import tableSile tableSile(out, mode='w').write(*ns._data, comment=comment, header=ns._data_header) # Clean all data ns._data = [] ns._data_header = [] ns._PDOS_filter_name = None ns._PDOS_filter = _sum_filter ns._Erng = None
def __call__(self, parser, ns, value, option_string=None): out = value[0] try: # We figure out if the user wants to write # to a geometry obj = get_sile(out, mode='w') if hasattr(obj, 'write_geometry'): with obj as fh: fh.write_geometry(ns._geometry) return raise NotImplementedError except: pass if len(ns._data) == 0: orbs = parse_atom_range(ns._geometry, f"1-{len(geometry)}") ns._data.append(ns._E) ns._data.append(ns._PDOS_filter(ns._PDOS)[orbs].sum(0)) ns._data_header.append("DOS[1/eV]") from sisl.io import tableSile tableSile(out, mode='w').write(*ns._data, comment=comment, header=ns._data_header) # Clean all data ns._data = [] ns._data_header = [] ns._PDOS_filter = _sum_filter ns._Erng = None
def write(self, sile, *args, **kwargs): """ Writes k-points to a `~sisl.io.tableSile`. This allows one to pass a `tableSile` or a file-name. """ from sisl.io import tableSile kw = np.concatenate((self.k, self.weight.reshape(-1, 1)), axis=1) if isinstance(sile, tableSile): sile.write_data(kw.T, *args, **kwargs) else: with tableSile(sile, 'w') as fh: fh.write_data(kw.T, *args, **kwargs)
def __enter__(self): """ Open the file and fill with stuff """ _log.debug(f"__enter__ {self.__class__.__name__}") # check if the file exists if self.out.exists(): # read in previous data # this will be "[variables, runs]" data, header = tableSile(self.out).read_data(ret_header=True) else: data = np.array([]) # check if the file exists if self.out.exists() and data.size > 0: nvars = data.shape[0] - 1 if nvars != len(self): raise ValueError( f"Found old file {self.out} which contains previous data for another number of parameters, please delete or move file" ) # now parse header *header, _ = header[1:].split() idx = [] for name in self.names: # find index in header for i, head in enumerate(header): if head == name: idx.append(i) break if nvars != len(idx): print(header) print(self.names) print(idx) raise ValueError( f"Found old file {self.out} which contains previous data with some variables being renamed, please correct header or move file" ) # add functional value, no pivot idx.append(len(self)) # re-arrange data (in case user swapped order of variables) data = np.ascontiguousarray(data[idx].T) x, y = data[:, :-1], data[:, -1] # We populate with hashes without the functional # That would mean we can't compare hashes between input arguments # only make the first index a list (x.tolist() makes everything a list) self.data.x = [xi for xi in x] self.data.y = [yi for yi in y] self.data.hash = list(map(self.get_hash, self.data.x)) # Re-open file (overwriting it) # First output a few things in this file comment = f"Created by sisl '{self.__class__.__name__}'." header = self.names + ["metric"] if len(self.data.x) == 0: self._fh = tableSile(self.out, 'w').__enter__() self._fh.write_data(comment=comment, header=header) else: comment += f" The first {len(self.data)} lines contains prior content." data = np.column_stack((self.data.x, self.data.y)) self._fh = tableSile(self.out, 'w').__enter__() self._fh.write_data(data.T, comment=comment, header=header, fmt='20.17e') self._fh.flush() return self