# -*-Python-*-
# Created by bgrierson at 24 Dec 2016 17:03
# Basic namelist creation and maniuplation
# Set up the OMFIT tree to store the output from this script
root['OUTPUTS'].setdefault('NAMELISTS', OMFITtree())
# Easy to make a namelist from a block of text
nml_txt = '''
! A namelist comment
! A float
FLOAT = 1.0
! An array
ARRAY(1) = 1.0
ARRAY(2) = 2.0
'''
# Basic OMFITnamelist makes a namelist that looks like this:
# ! A namelist comment
# ! A float
# FLOAT 1.0
#
# ! An array
# ARRAY = 1.0 2.0
basic1_nml = OMFITnamelist('basic1.nml', fromString=nml_txt)
root['OUTPUTS']['NAMELISTS']['basic1'] = basic1_nml
# Array elements are acessed like this:
array = root['OUTPUTS']['NAMELISTS']['basic1']['ARRAY']
array1 = array[0]
array2 = array[1]
# A new array is entered like this
root['OUTPUTS']['NAMELISTS']['basic1']['ARRAY2'] = np.array([3, 4])
# Basic OMFITnamelist with collect_arrays=False makes a namelist that looks like this:
# ! A namelist comment
# ! A float
# FLOAT 1.0
#
# ! An array
# ARRAY(1) = 1.0
# ARRAY(2) = 2.0
basic2_nml = OMFITnamelist('basic2.nml', fromString=nml_txt, collect_arrays=False)
root['OUTPUTS']['NAMELISTS']['basic2'] = basic2_nml
# Array elements are acessed like this:
array1 = root['OUTPUTS']['NAMELISTS']['basic2']['ARRAY(1)']
array2 = root['OUTPUTS']['NAMELISTS']['basic2']['ARRAY(2)']
# A new array is entered like this
root['OUTPUTS']['NAMELISTS']['basic2']['ARRAY2(1)'] = 3.0