Skip to content

Commit

Permalink
Simplify setting arbitrary parameters from the command line
Browse files Browse the repository at this point in the history
Object::setParameter(name, content, unit) is made recursive: if name
contains "::" (e.g. Astrobj::Spectrum::Temperature), the component
before the first occurence of "::" (here, "Astrobj") is interpreted as
a path. A property by that name in the curent object is sought. If it
is found, setParameter is called on this child object with the
remainder of the string (here "Spectrum::Temperature") and the other
parameters unchanged (content and unit).

On the command-line, replace the bunch of --TYPE-parameter options
with a single --parameter option, which applies to the root (Scenery)
level and uses this facility to access members in the Astrobj, Screen
etc.
  • Loading branch information
paumard committed Jun 16, 2015
1 parent b8766a3 commit f0282ed
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 68 deletions.
67 changes: 38 additions & 29 deletions bin/gyoto.1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ gyoto [\fB\-\-silent\fR|\fB\-\-quiet\fR|\fB\-\-verbose\fR[=\fIN\fR]|\fB\-\-debug
[\fB\-\-plugins\fR=\fIpluglist\fR]
[\fB\-\-impact-coords\fR[=\fIfname.fits\fR]]
[\fB\-\-unit\fR[=\fIunit\fR]]
[\fB\-\-(astrobj|metric|scenery|screen|spectrometer)-parameter\fR=\fIName\fR[=\fIvalue\fR]]
[\fB\-\-parameter\fR=\fIPath::Name\fR[=\fIvalue\fR]]
[\fB\-\-xmlwrite\fR=\fIoutput.xml\fR]
[\fB\-\-\fR] \fIinput.xml \fIoutput.fits
.SH DESCRIPTION
Expand All @@ -44,18 +44,18 @@ option.

.SH OPTIONS
The \fBgyoto\fR program accepts many options. Most have a long name
(e.g. \fB\-\-astrobj\-parameter\fR) and a short name
(e.g. \fB\-A\fR). When an option takes an argument, this argument must
follow immediately the short option (e.g. \fB\-A\fIName\fR) and be
separated from the long option by exactly the character "="
(e.g. \fB\-\-astrobj\-parameter\fR=\fIName\fR). Long options can be
(e.g. \fB\-\-parameter\fR) and a short name (e.g. \fB\-E\fR). When an
option takes an argument, this argument must follow immediately the
short option (e.g. \fB\-E\fIPath::Name\fR) and be separated from the
long option by exactly the character "="
(e.g. \fB\-\-parameter\fR=\fIPath::Name\fR). Long options can be
abbreviated as long as the abbreviation is unambiguous
(e.g. \fB\-\-astrobj=\fIName\fR or even \fB\-\-a=\fIName\fR). Most
options can appear several times and are processed in the order they
appear in the command line. The two positional parameters
(\fIinput.xml\fR and \fIoutput.fits\fR) can appear anywhere in the
command line, except if they start with a minus character (\-) in
which case they must appear last, after the option \fB\-\-\fR.
(e.g. \fB\-\-par=\fIPath::Name\fR). Most options can appear several
times and are processed in the order they appear in the command
line. The two positional parameters (\fIinput.xml\fR and
\fIoutput.fits\fR) can appear anywhere in the command line, except if
they start with a minus character (\-) in which case they must appear
last, after the option \fB\-\-\fR.

.SS Getting help
.IP \fB\-\-help\fR
Expand Down Expand Up @@ -263,28 +263,37 @@ In this case, the impact coordinates are read from
.PD 0
.IP \fB\-u\fR[\fIunit\fR]
.PD
Specify unit to use for allowing instances of
\fB\-\-(astrobj|metric|scenery|screen|spectrometer)-parameter\fR,
Specify unit to use for allowing instances of \fB\-\-parameter\fR,
untill next instance of \fB\-\-unit\fR.
.IP \fB\-\-astrobj-parameter\fR=\fIName\fR[=\fIvalue\fR]
.IP \fB\-\-parameter\fR=\fIPath::Name\fR[=\fIvalue\fR]
.PD 0
.IP \fB\-\-metric-parameter\fR=\fIName\fR[=\fIvalue\fR]
.IP \fB\-\-scenery-parameter\fR=\fIName\fR[=\fIvalue\fR]
.IP \fB\-\-screen-parameter\fR=\fIName\fR[=\fIvalue\fR]
.IP \fB\-\-spectrometer-parameter\fR=\fIName\fR[=\fIvalue\fR]
.IP \fB\-(A|M|E|R|O)\fIName\fR[=\fIvalue\fR]
.IP \fB\-E\fIPath::Name\fR[=\fIvalue\fR]
.PD
Set arbitray parameter by name in the Astrobj, Metric, scEnery, scReen
or spectrOmeter. For instance, assuming the Astrobj in \fIstar.xml\fR
has a property named "Radius" that can be set in unit "km":
Set arbitray parameter by name. Parameters can be set in the Astrobj,
Metric etc. using the \fIPath\fR componenent. For instance,

For instance, assuming the Astrobj in \fIstar.xml\fR
has a property named "Radius" that can be set in unit "km", and a property named "Spectrum" which has a property named "Temperature", we can set the radius, temperature and the quantities to compute (a property in the Scenery itself) with:
.RS 10
.PD 0
.IP
\fBgyoto\fR \-ukm \-ARadius=3 star.xml star.fits
.IP
\fBgyoto\fR \-\-unit=km \-\-astrobj-parameter=Radius=3 star.xml star.fits
\fBgyoto\fR \-EQuantities=Spectrum \\
.br
\-ukm \-EAstrobj::Radius=3 \\
.br
\-u \-EAstrobj::Spectrum::Temperature=1000 \\
.br
star.xml star.fits

\fBgyoto\fR \-\-parameter=Quantities=Spectrum \\
.br
\-\-unit=km \-\-parameter=Astrobj::Radius=3 \\
.br
\-\-unit="" \-\-param=Astrobj::Spectrum::Temperature=1000 \\
.br
star.xml star.fits

.RE
.PD


.IP \fB\-\-xmlwrite\fR=\fIoutput.xml\fR
.PD 0
.IP \fB\-X\fIoutput.xml\fR
Expand Down
44 changes: 7 additions & 37 deletions bin/gyoto.C
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ struct Gyoto::Arg: public option::Arg
}
};
enum optionType { DEBUG, QUIET, VERBOSE, SILENT,
IMIN, IMAX, JMIN, JMAX, ISTEP, JSTEP, ISPEC, JSPEC,
ASTROBJ, METRIC, SPECTROMETER, SCREEN, SCENERY };
IMIN, IMAX, JMIN, JMAX, ISTEP, JSTEP, ISPEC, JSPEC};
enum optionIndex { UNKNOWN, HELP, PLUGINS, LIST, VERBOSITY, NOSIGFPE, RANGE,
BOUNDARIES, STEPS, IPCT, TIME, TMIN, FOV, RESOLUTION,
DISTANCE, PALN, INCLINATION, ARGUMENT, NTHREADS, NPROCESSES,
Expand All @@ -102,7 +101,7 @@ const option::Descriptor usage[] =
{NTHREADS, 0, "T", "nthreads", Gyoto::Arg::Required, " --nthreads=<n>, -T<n> \tNumber of parallel threads to use."},
{NPROCESSES, 0, "P", "nprocesses", Gyoto::Arg::Required, " --nprocesses=<n>, -P<n> \tNumber of MPI parallel processes to use."},
{IPCT, 0, "", "impact-coords", option::Arg::Optional, " --impact-coords[=<f>] \tRead impact coordinates from file <f> or store in output.fits."},
{XMLWRITE, 0, "X", "xmlwrite", Gyoto::Arg::Required, " --xmlwrite=<f>, -X<f> \tWrite back scenery to XML file <f>. Useful to see default values and check the effect of --<object>-parameter, see below."},
{XMLWRITE, 0, "X", "xmlwrite", Gyoto::Arg::Required, " --xmlwrite=<f>, -X<f> \tWrite back scenery to XML file <f>. Useful to see default values and check the effect of --parameter, see below."},
{UNKNOWN, 0, "", "",option::Arg::None, "\nVerbosity level:" },
{VERBOSITY, SILENT, "s", "silent", option::Arg::None, " --silent, -s \tBe silent." },
{VERBOSITY, QUIET, "q", "quiet", option::Arg::None, " --quiet, -q \tBe quiet." },
Expand All @@ -128,17 +127,9 @@ const option::Descriptor usage[] =
{ARGUMENT, 0, "", "argument", Gyoto::Arg::Required, " --argument=<arg> \tArgument of the x axis."},
{UNKNOWN, 0, "", "",option::Arg::None, "\nArbitrary parameters:" },
{UNIT, 0, "u", "unit", Gyoto::Arg::Optional, " --unit[=<u>], -u[<u>] \tUnit for following parameters (until next instance of this option)."},
{SETPARAMETER, METRIC, "M", "metric-parameter", Gyoto::Arg::Required,
" --metric-parameter=<Name>[=<value>], -M<Name>[=<value>]"},
{SETPARAMETER, SCREEN, "R", "screen-parameter", Gyoto::Arg::Required,
" --screen-parameter=<Name>[=<value>], -R<Name>[=<value>]"},
{SETPARAMETER, SCENERY, "E", "scenery-parameter", Gyoto::Arg::Required,
" --scenery-parameter=<Name>[=<value>], -E<Name>[=<value>]"},
{SETPARAMETER, ASTROBJ, "A", "astrobj-parameter", Gyoto::Arg::Required,
" --astrobj-parameter=<Name>[=<value>], -A<Name>[=<value>]"},
{SETPARAMETER, SPECTROMETER, "O", "spectrometer-parameter", Gyoto::Arg::Required,
" --spectrometer-parameter=<Name>[=<value>], -O<Name>[=<value>] \n"
"\tSet arbitrary parameter by name. Optional value is expressed in unit previously set with --unit/-u.)"},
{SETPARAMETER, 0, "E", "parameter", Gyoto::Arg::Required,
" --parameter=<Name>[=<value>], -E<Name>[=<value>]"
"\tSet arbitrary parameter by name. Optional value is expressed in unit previously set with --unit/-u. Examples: -ENThreads=5, -EAstrobj::Spectrum::Temperature=100."},
{0,0,0,0,0,0}
};

Expand Down Expand Up @@ -368,29 +359,8 @@ int main(int argc, char** argv) {
string name=arg.substr(0, pos);
string val=(pos==string::npos)?"":arg.substr(pos+1);
GYOTO_DEBUG << "Setting parameter \"" << name << "\" to value \"" << val << "\" using unit \"" << unit << "\".\n";
switch (opt.type()) {
case ASTROBJ:
if(scenery -> astrobj() -> setParameter(name, val, unit))
throwError("Unknown parameter");
break;
case METRIC:
if(scenery -> metric() -> setParameter(name, val, unit))
throwError("Unknown parameter");
break;
case SPECTROMETER:
if(screen -> spectrometer() -> setParameter(name, val, unit))
throwError("Unknown parameter");
break;
case SCREEN:
if(scenery -> screen() -> setParameter(name, val, unit))
throwError("Unknown parameter");
break;
case SCENERY:
if(scenery -> setParameter(name, val, unit))
throwError("Unknown parameter");
break;
default: throwError("BUG");
}
if(scenery -> setParameter(name, val, unit))
throwError("Unknown parameter");
}
break;
case XMLWRITE: Factory(scenery).write(opt.arg); break;
Expand Down
6 changes: 5 additions & 1 deletion include/GyotoObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ Gyoto::Metric::MyKind::Subcontractor(FactoryMessenger* fmp) {
* }
* \endcode
*
* \param name XML name of the parameter (XML entity)
* \param name XML name of the parameter (XML entity). This may have
* a path component, e.g. "Astrobj::Radius", in which case a
* property named "Astrobj" will be sought in the current
* object, and setParameter will be called recusrively on
* this Astrobj with Radius as name.
* \param content string representation of the value
* \param unit string representation of the unit
* \return 0 if this parameter is known, 1 if it is not.
Expand Down
34 changes: 33 additions & 1 deletion lib/Object.C
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,39 @@ void Object::setParameter(Property const &p, string const &name,

int Object::setParameter(string name, string content, string unit) {
Property const * prop = property(name);
if (!prop) return 1;
if (!prop) {
size_t pos=name.find("::");
if (pos != string::npos) {
string childname = name.substr(0,pos);
name=name.substr(pos+2);
prop=property(childname);
if (!prop) return 1;
Object * obj=NULL;
Value val=get(*prop);
switch (prop->type) {
case Property::screen_t:
obj = SmartPointer<Screen>(val);
break;
case Property::metric_t:
obj = SmartPointer<Metric::Generic>(val);
break;
case Property::astrobj_t:
obj = SmartPointer<Astrobj::Generic>(val);
break;
case Property::spectrum_t:
obj = SmartPointer<Spectrum::Generic>(val);
break;
case Property::spectrometer_t:
obj = SmartPointer<Spectrometer::Generic>(val);
break;
default:
throwError(childname+" is not an object");
}
if (obj) return obj -> setParameter(name, content, unit);
throwError(childname+" not set yet");
}
return 1;
}
setParameter(*prop, name, content, unit);
return 0;
}

0 comments on commit f0282ed

Please sign in to comment.