★ Reading configuration files
posted Fri 12 Jul 2013 by Michael Galloy under IDLA configuration file is a text file which specifies values for some pre-defined keys. For example, the following configuration specifies a few values (some of which depend on other values) and has a comment that is ignored:
# example of a simple configuration file
data_dir: ~/data
terra_dir: %(data_dir)s/MODIS/Terra/C5
aqua_dir: %(data_dir)s/MODIS/Aqua/C5
This config file can be read with:
IDL> config = mg_read_config('simple.ini')
IDL> print, config->get('terra_dir')
~/data/MODIS/Terra/C5
My configuration file reader supports sections, interpolation (substituting a variable into another one, as above), and default values.
The returned value for my reader is an object with three methods: has_option
, get
, and put
, but also supports overloaded operators for brackets, PRINT
, and HELP
.
Let’s consider a more complicated configuration file, sections.ini
:
# example of using sections to group items (notice the same name can be used in
# different sections without conflict)
# these are options in the default section that can be accessed from any other
# section
data_root: ~/data
output_root: %(data_dir)s/processed
# Terra specific options
[ Terra ]
subdir: MODIS/Terra/C5
data: %(data_root)s/%(subdir)s
output: %(output_root)s/%(subdir)s
variables: [ Longitude, Latitude, Optical_Depth_Land_And_Ocean ]
grid: %(data_dir)s/grids/NCEP-T62.nc
# Aqua specific options
[ Aqua ]
subdir: MODIS/Aqua/C5
data: %(data_root)s/%(subdir)s
output: %(output_root)s/%(subdir)s
variables: [ Longitude, Latitude, Deep_Blue_Angstrom_Exponent_Land ]
grid: %(data_dir)s/grids/MERRA-125x1.25.nc
Read this file in the same manner as the simpler one:
IDL> config = mg_read_config('sections.ini')
Use the has_option
and get
methods to determine if particular options are present and to retrieve their values:
IDL> print, config->has_option('data_root')
1
IDL> print, config->get('data_root')
~/data
To retrieve an option that is not in the default section, the SECTION
keyword must be used:
IDL> print, config->get('data', section='Terra')
~/data/MODIS/Terra/C5
IDL> print, config->get('variables', section='Terra')
[ Longitude, Latitude, Optical_Depth_Land_And_Ocean ]
Arrays can be extracted during the get
method, if that is more useful:
IDL> terra_vars = config->get('variables', section='Terra', /extract)
IDL> help, terra_vars
TERRA_VARS STRING = Array[3]
IDL> print, terra_vars
Longitude Latitude Optical_Depth_Land_And_Ocean
The HELP
and PRINT
routines are overloaded for the config
object returned:
IDL> help, config
CONFIG MGFFOPTIONS <NSECTIONS=3 NOPTIONS=12>
IDL> print, config
[ ]
data_root: ~/data
output_root: %(data_dir)s/processed
[ Aqua ]
variables: [ Longitude, Latitude, Deep_Blue_Angstrom_Exponent_Land ]
grid: %(data_dir)s/grids/MERRA-125x1.25.nc
data: %(data_root)s/%(subdir)s
output: %(output_root)s/%(subdir)s
subdir: MODIS/Aqua/C5
[ Terra ]
variables: [ Longitude, Latitude, Optical_Depth_Land_And_Ocean ]
grid: %(data_dir)s/grids/NCEP-T62.nc
data: %(data_root)s/%(subdir)s
output: %(output_root)s/%(subdir)s
subdir: MODIS/Terra/C5
The code is GitHub in my library.
October 25th, 2013 at 5:35 am
Hi,
Can I get a packed version of your mglib? On your Github site, I only can download files one by one. It’s a hard work.
October 25th, 2013 at 8:17 am
Yes, there is a link on the right of the mglib page right below the clone in git command. Here is the direct link.
October 28th, 2013 at 7:02 am
Hi, I try your code in windows OS. It doesn’t work well. When I call to
it just give a
!NULL
value. I do not know where is wrong?October 28th, 2013 at 7:19 am
There is no variable
terra_dir
, try:or
Did that work?
October 28th, 2013 at 7:32 am
I use the simple.ini file for test. It does have a variable named “terra_dir”.
When I change to sections.ini file, it report:
Regards,
Jie
October 28th, 2013 at 7:42 am
Where does this simple.ini come from? It is not part of the repo.
What did you try with sections.ini?
October 28th, 2013 at 7:46 am
both the two files are accompany with your above articles.
http://michaelgalloy.com/2013/07/12/reading-configuration-files.html
October 28th, 2013 at 7:49 am
But that file is no longer part of the repo at GitHub. Do you have an up-to-date version of the files?
October 28th, 2013 at 7:59 am
I download the library today. And I create the two ini files by myself. In the fileio folder only exist one file config.ini. I try it, also the same problem. I check the code of
mgffoptions__define
roughly, the code from line 298 to line 301 as follows:as I know,
self.sections
here is a hash, do you think is it necessary to use parentheses to cover them?October 28th, 2013 at 8:05 am
When I try the following from the src/fileio directory of the repo, everything seems to work for me:
Are you saying this exact code does not work for you?
October 28th, 2013 at 8:11 am
Yes. It does not work. After I run:
It reports that:
October 28th, 2013 at 8:13 am
After I change all
self.sections[_section]
to(self.sections)[_section]
, it works, at least on my system.October 28th, 2013 at 8:15 am
What version of IDL are you using?
October 28th, 2013 at 8:17 am
IDL 8.0.1
October 28th, 2013 at 8:18 am
OK, will have to find an old version to check this out.
October 28th, 2013 at 9:47 pm
Something changed from IDL 8.0 to IDL 8.1. I have added parentheses for compatibility with IDL 8.0.
August 5th, 2014 at 9:01 am
[…] producing user specified plots and reports from the results. Control of the system is done via a configuration file, like this one, which has been a great, flexible way to handle users extending and controlling the […]