|
Re: Library/Functions to write configuration file for application [message #74729 is a reply to message #74712] |
Tue, 01 February 2011 02:17   |
sirvival
Messages: 18 Registered: August 2010
|
Junior Member |
|
|
Hi,
I got a pro (not sure from where) that maybe does what you want:
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++
++++++++
FUNCTION DATA_INPUT, key, default, FILE=file
;_Purpose: ASCII data input from runtime file specification
;
;_Description: This is an extension of the FITS format such that a
single
; keyword may imply input of up to 35 data items,
provided the
; data are non-character, and they are read from a
single input
; line, the length of which is < 81 characters.
; Data delimiter is a comma. The number of data items
identified
; is equal to the number of commas found + 1.
; Character input is constrained to a single item.
; A '!' marks the start of a comment.
;
;_Language: IDL V3
;
;_Input Par: key Keyword (FITS type) to extract
; default Default value assigned if key or file not
found
;
;_Keywords: FILE Input from specified disk file
; Default is 'USER.PAR'
;
;_Result: Value(s) of data following the keyword in the input
file. The
; data type is determined at run time.
;
;_History: TG 28-May-93 Initial programming
; TG 10-Feb-96 '.' recognition produces FLOAT
;
;_Copyright: (c) FOCES project, University Observatory Munich
;
;----------------------------------------------------------- -------------------
line = STRING(' ',FORMAT='(A80)') ; Single
line format
ON_IOERROR, no_such_file
IF (NOT KEYWORD_SET(file)) THEN file = 'ORIENTATION.PAR' ;
Default input file
GET_LUN, lun
OPENR, lun, file
k = -1
WHILE (k EQ -1) AND NOT EOF(lun) DO BEGIN ; Read
until EOF
READF, lun, line
keyword = STRMID(line, 0, 8)
k = STRPOS(keyword, key)
IF (k NE -1) THEN line = STRMID(line, 10, 70) ; Extract keyword
contents
ENDWHILE
FREE_LUN, lun
IF (k EQ -1) THEN BEGIN ; Keyword
not found
value = default ;
Set default
RETURN, value ; ... and return to
calling program
ENDIF
IF (STRPOS(line, "'") EQ 0) THEN BEGIN ; String value
detected
tmp = STRPOS(line, "'", 1)
IF (tmp EQ -1) THEN BEGIN ; Second string
delimiter missing
PRINT, '###ERROR: non-FITS format'
PRINT, line
STOP
ENDIF
value = STRMID(line, 1, tmp-1) ;
Extract string
ENDIF ELSE BEGIN ; Number(s)
detected
i_elem = 0
k = -1
i = 0
WHILE (i NE -1) DO BEGIN
i = STRPOS(line, ",", k+1) ; Detect
next comma
j = STRPOS(line, "!", k+1) ; Detect
exclamation mark
ii = i
IF (ii EQ -1) THEN BEGIN
ii = STRLEN(line) ; End of
input line
IF (j NE -1) THEN ii = j-1 ; End of input
with comment
ENDIF
tmp = STRMID(line, k+1, ii-k-1) ; Extract substring
between commas
CASE (1) OF ; Determine
data type
(STRPOS(tmp, 'D') NE -1): tmp = DOUBLE(tmp)
(STRPOS(tmp, 'E') NE -1): tmp = FLOAT(tmp)
(STRPOS(tmp, '.') NE -1): tmp = FLOAT(tmp)
ELSE: tmp = LONG(tmp)
ENDCASE
i_elem = i_elem+1
IF (i_elem EQ 1) THEN value = tmp ELSE value = [value,tmp]
k = i
ENDWHILE
ENDELSE
RETURN, value
no_such_file: ; File
not found
ON_IOERROR, NULL ; Reset
error status
value = default ;
Set default
RETURN, value
END
e.g. name.par
!Input file for the orientation simulation giving star and
spectrograph parameters
!
Dtel = 1.2
Fnum = 13.0
Groove = 31.6e3
------------------------------------------------------------ --------
a short introduction to the parameters
#Dtel : Diameter of the telescope in m
#Fnum : Fnumber of the telescope
#groove : of the grating in m
|
|
|
|
|
|
|
Re: Library/Functions to write configuration file for application [message #74739 is a reply to message #74737] |
Mon, 31 January 2011 13:09   |
Gray
Messages: 253 Registered: February 2010
|
Senior Member |
|
|
On Jan 31, 2:52 pm, Paolo <pgri...@gmail.com> wrote:
> On Jan 31, 2:05 pm, Robin Wilson <ro...@rtwilson.com> wrote:
>
>
>
>
>
>
>
>
>
>> Hi,
>
>> I've written a fairly complex piece of modelling software in IDL, and
>> the model requires a number of parameters to run (input and output files
>> as well as various numerical parameters).
>
>> I want to be able to run this model from a batch file, and would like to
>> have a configuration file in which I specify all of these parameters and
>> then all I have to do is give the model the name of the configuration file.
>
>> What I was wondering is whether there are any libraries/sets of
>> functions in IDL that would help with reading and writing these
>> configuration files? I know that a number of programming languages
>> either have inbuilt functions for writing configuration files or some
>> add-ons which do that - do any of these exist for IDL?
>
>> I could knock up some simple file reading/writing code, but that would
>> make it rather difficult if I changed the order/number of variables in
>> the file, as I'd probably have to read some lines as strings and some as
>> floats, ints etc, and I could see it getting very complicated and
>> difficult to maintain.
>
> Well, if the values can only be int, floats or strings, nothing
> like a good old-fashioned text file in format
>
> parname = parvalue
> anotherparname = anothervalue
>
> etc.
>
> which is quite easy to parse.
>
> Ciao,
> Paolo
>
>
>
>
>
>
>
>
>
>> Any ideas?
>
>> Cheers,
>
>> Robin
>
>> ------------------
>> Robin Wilson
>> A PhD student studying complexity in remote sensingwww.rtwilson.com/academic
Or, something along the lines of a FITS header, which has a lot of
stuff written for parsing already. Check out the NASA astronomy IDL
library for examples.
|
|
|
|
Re: Library/Functions to write configuration file for application [message #74742 is a reply to message #74740] |
Mon, 31 January 2011 11:52   |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
On Jan 31, 2:05 pm, Robin Wilson <ro...@rtwilson.com> wrote:
> Hi,
>
> I've written a fairly complex piece of modelling software in IDL, and
> the model requires a number of parameters to run (input and output files
> as well as various numerical parameters).
>
> I want to be able to run this model from a batch file, and would like to
> have a configuration file in which I specify all of these parameters and
> then all I have to do is give the model the name of the configuration file.
>
> What I was wondering is whether there are any libraries/sets of
> functions in IDL that would help with reading and writing these
> configuration files? I know that a number of programming languages
> either have inbuilt functions for writing configuration files or some
> add-ons which do that - do any of these exist for IDL?
>
> I could knock up some simple file reading/writing code, but that would
> make it rather difficult if I changed the order/number of variables in
> the file, as I'd probably have to read some lines as strings and some as
> floats, ints etc, and I could see it getting very complicated and
> difficult to maintain.
Well, if the values can only be int, floats or strings, nothing
like a good old-fashioned text file in format
parname = parvalue
anotherparname = anothervalue
etc.
which is quite easy to parse.
Ciao,
Paolo
>
> Any ideas?
>
> Cheers,
>
> Robin
>
> ------------------
> Robin Wilson
> A PhD student studying complexity in remote sensingwww.rtwilson.com/academic
|
|
|
Re: Library/Functions to write configuration file for application [message #74743 is a reply to message #74742] |
Mon, 31 January 2011 11:47   |
penteado
Messages: 866 Registered: February 2018
|
Senior Member Administrator |
|
|
On Jan 31, 5:44 pm, Paulo Penteado <pp.pente...@gmail.com> wrote:
> I have some very old routines I wrote for exactly that purpose. They
> worked with text files, recognizing a few comment markers, and
> optionally converting the values to numbers (reals or integers) if
> they are compatible with being numbers. I can tidy them up and publish
> them, maybe later tonight. They fit well into some classes I was
> preparing.
>
> Another approach I used for the same purpose is NetCDF files, which
> nicely handle names dimensions and types, and can be easily put into
> hashes or structures. Writing takes slightly more work, due to the
> need to associate dimensions with the variables, but can also be
> nicely expressed by hashes.
By the way, those formats (text and NetCDF) were chosen for
portability with code in other languages. But if you are using only
IDL, maybe savefiles would be easier. Especially if handled through
Craig Markwardt's cmsvlib (http://www.physics.wisc.edu/~craigm/idl/
cmsave.html), or IDL_Savefile objects.
|
|
|
Re: Library/Functions to write configuration file for application [message #74744 is a reply to message #74743] |
Mon, 31 January 2011 11:44   |
penteado
Messages: 866 Registered: February 2018
|
Senior Member Administrator |
|
|
I have some very old routines I wrote for exactly that purpose. They
worked with text files, recognizing a few comment markers, and
optionally converting the values to numbers (reals or integers) if
they are compatible with being numbers. I can tidy them up and publish
them, maybe later tonight. They fit well into some classes I was
preparing.
Another approach I used for the same purpose is NetCDF files, which
nicely handle names dimensions and types, and can be easily put into
hashes or structures. Writing takes slightly more work, due to the
need to associate dimensions with the variables, but can also be
nicely expressed by hashes.
On Jan 31, 5:05 pm, Robin Wilson <ro...@rtwilson.com> wrote:
> What I was wondering is whether there are any libraries/sets of
> functions in IDL that would help with reading and writing these
> configuration files? I know that a number of programming languages
> either have inbuilt functions for writing configuration files or some
> add-ons which do that - do any of these exist for IDL?
>
> I could knock up some simple file reading/writing code, but that would
> make it rather difficult if I changed the order/number of variables in
> the file, as I'd probably have to read some lines as strings and some as
> floats, ints etc, and I could see it getting very complicated and
> difficult to maintain.
|
|
|
|