Added the compute function.

This commit is contained in:
Sylvain Tricot 2022-03-07 11:53:05 +01:00
parent 88436fabdf
commit a28696d2a7
3 changed files with 74 additions and 4 deletions

View File

@ -21,3 +21,4 @@
# Committed by : Sylvain Tricot <sylvain.tricot@univ-rennes1.fr> 1645806435 +0100
from msspec_dfm.version import __version__
from msspec_dfm.compute import compute

View File

@ -0,0 +1,63 @@
#!/usr/bin/env python
# coding: utf-8
#
# Copyright © 2022 - Rennes Physics Institute
#
# This file is part of MsSpec-DFM.
#
# MsSpec-DFM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# MsSpec-DFM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with MsSpec-DFM. If not, see <http://www.gnu.org/licenses/>.
#
# Source file : src/python/msspec_dfm/version.py
# Last modified: Fri, 25 Feb 2022 17:27:32 +0100
# Committed by : Sylvain Tricot <sylvain.tricot@univ-rennes1.fr> 1645806435 +0100
import os
import sys
from tempfile import NamedTemporaryFile, TemporaryDirectory
from matplotlib import pyplot as plt
import numpy as np
from msspec_dfm.input_file import InputFile
from msspec_dfm.eps import epsilon
from contextlib import redirect_stdout
def compute(filename=None, folder=None, logfile='-', **parameters):
# If filename is None, a temporary name is provided
if filename is None:
filename = NamedTemporaryFile().name
# If folder is None, a temporary name is provided
if folder is None:
folder = TemporaryDirectory().name
if logfile != '-':
sys.stdout.flush()
old_fd = os.dup(1)
os.close(1)
os.open(logfile, os.O_WRONLY|os.O_CREAT)
data = {}
input_file = InputFile(filename, **parameters)
input_file.write()
epsilon(filename, folder)
for root, dirs, files in os.walk(folder):
for f in files:
data[os.path.splitext(f)[0]] = np.loadtxt(os.path.join(root, f))
if logfile != '-':
os.close(1)
os.dup(old_fd)
os.close(old_fd)
return data

View File

@ -260,14 +260,18 @@ class InputFile:
'I_TI' : (0, '10d'),
}
def __init__(self, filename):
def __init__(self, filename, **parameters):
self.filename = filename
self.parameters = self.defaults
for key, value in parameters.items():
fmt = self.parameters[key][1]
self.parameters.update({key:(value, fmt)})
def _stack(self, *keys, comments=""):
params = []
for key in keys:
value, fmt = self.defaults[key]
value, fmt = self.parameters[key]
params.append((key, value, fmt))
line = [' ',] * 49
labels = []
@ -299,7 +303,7 @@ class InputFile:
return ''.join(line)
def write(self, **kwargs):
def write(self):
content = ""
content += " ******************************************************************************\n"
content += " * MsSpec DIELECTRIC FUNCTION MODULE *\n"
@ -487,6 +491,8 @@ class InputFile:
content += " *============================================================================*\n"
content += " ******************************************************************************\n"
os.makedirs(os.path.dirname(self.filename), exist_ok=True)
abs_path = os.path.abspath(self.filename)
dir_path = os.path.dirname(abs_path)
os.makedirs(dir_path, exist_ok=True)
with open(self.filename, 'w') as fd:
fd.write(content)