2022-03-11 10:00:29 +01:00
import json
2022-04-04 14:48:59 +02:00
import numpy as np
2022-06-16 16:26:41 +02:00
import os
2022-03-11 10:00:29 +01:00
def update_options ( options , required_options , default_options ) :
''' Takes a dictionary of options along with a list of required options and dictionary of default options, and sets all keyval-pairs of options that is not already defined to the default values '''
for option in required_options :
if option not in options . keys ( ) :
options [ option ] = default_options [ option ]
return options
2022-06-29 15:26:43 +02:00
def save_options ( options , path , ignore = None ) :
2022-03-11 10:00:29 +01:00
''' Saves any options dictionary to a JSON-file in the specified path '''
2022-06-29 15:26:43 +02:00
options_copy = options . copy ( )
if ignore :
if not isinstance ( ignore , list ) :
ignore = [ ignore ]
for i in ignore :
options_copy [ i ] = ' Removed '
2022-07-05 16:37:31 +02:00
if not os . path . isdir ( os . path . dirname ( path ) ) :
os . makedirs ( os . path . dirname ( path ) )
2022-03-11 10:00:29 +01:00
with open ( path , ' w ' ) as f :
2022-06-29 15:26:43 +02:00
json . dump ( options_copy , f , skipkeys = True , indent = 4 )
2022-03-11 10:00:29 +01:00
def load_options ( path ) :
''' Loads JSON-file into a dictionary '''
with open ( path , ' r ' ) as f :
options = json . load ( f )
return ( options )
def swap_values ( dict , key1 , key2 ) :
key1_val = dict [ key1 ]
dict [ key1 ] = dict [ key2 ]
dict [ key2 ] = key1_val
return dict
2022-04-04 14:48:59 +02:00
def ceil ( a , roundto = 1 ) :
2022-03-11 10:00:29 +01:00
2022-04-04 14:48:59 +02:00
fac = 1 / roundto
a = np . ceil ( a * fac ) / fac
return a
def floor ( a , roundto = 1 ) :
fac = 1 / roundto
a = np . floor ( a * fac ) / fac
2022-06-15 14:28:50 +02:00
return a
def write_log ( message , options = { } ) :
from datetime import datetime
required_options = [ ' logfile ' ]
default_options = {
' logfile ' : f ' { datetime . now ( ) . strftime ( " % Y- % m- %d - % H- % M- % S.log " ) } '
}
options = update_options ( options = options , required_options = required_options , default_options = default_options )
2022-06-30 17:07:31 +02:00
if not os . path . isdir ( os . path . dirname ( options [ ' logfile ' ] ) ) :
os . makedirs ( os . path . dirname ( options [ ' logfile ' ] ) )
2022-06-15 14:28:50 +02:00
now = datetime . now ( ) . strftime ( ' % Y/ % m/ %d % H: % M: % S ' )
2022-06-16 17:54:51 +02:00
message = f ' [ { now } ] { message } \n '
2022-06-15 14:28:50 +02:00
with open ( options [ ' logfile ' ] , ' a ' ) as f :
2022-06-16 16:26:41 +02:00
f . write ( message )
#Function that "collects" all the files in a folder, only accepting .dat-files from xanes-measurements
2022-06-16 17:54:51 +02:00
def get_filenames ( path , ext , filter = ' ' ) :
2022-06-16 16:26:41 +02:00
''' Collects all filenames from specified path with a specificed extension
Input :
path : path to find all filenames ( relative or absolute )
ext : extension ( including " . " ) '''
2022-06-16 17:54:51 +02:00
filenames = [ os . path . join ( path , filename ) for filename in os . listdir ( path ) if os . path . isfile ( os . path . join ( path , filename ) ) and filename . endswith ( ext ) and filter in filename ]
2022-06-16 16:26:41 +02:00
return filenames