36 "List of values you want printed as columns")(
39 "Print in JSON format (CSV otherwise, unless output extension is "
40 ".json/.JSON)")(
"verbatim,v",
41 "Print exact properties specified, without prepending "
42 "'name' and 'selected' entries")(
45 "Print results all objects in input selection, whether or not they are "
46 "selected.")(
"no-header,n",
"Print without header (CSV only)")(
50 "Create an alias for a query that will persist within this project. "
51 "Ex: 'casm query --alias is_Ni_dilute = lt(atom_frac(Ni),0.10001)'")(
54 "Write POS file (VASP structure format) for each configuration")(
57 "Write config.json file (CASM DoF values format) for each configuration")(
60 "Write structure.json file (CASM structure format) for each "
63 "include-equivalents",
64 "Include an entry for all distinct configurations equivalent by "
65 "supercell symmetry");
82 namespace query_impl {
92 throw std::runtime_error(
"--write-config is not supported for supercells");
98 throw std::runtime_error(
"--write-structure is not supported for supercells");
112 virtual int help()
const;
114 virtual int desc()
const;
116 virtual int run()
const;
128 log() <<
"Prints the properties for the objects currently selected in the "
130 "selection or the specifed selection file via --selection (-c).\n\n"
132 "Property values are output in column-separated (default) or JSON "
133 "format. By default, \n"
134 "entries for 'name' and 'selected' values are included in the "
137 "The type of objects acted on is specified via --type (-t).\n\n";
158 template <
typename DataObject>
163 int help()
const override;
165 int desc()
const override;
167 int run()
const override;
223 template <
typename DataObject>
239 template <
typename DataObject>
243 for (
auto const &formatter :
m_data.dict()) {
251 template <
typename DataObject>
253 if (!m_cmd.opt().help_opt_vec().size()) {
257 log() <<
"Prints the properties for the objects currently selected in the "
259 "selection or the specifed selection file.\n\n"
261 "Property values are output in column-separated (default) or JSON "
262 "format. By default, \n"
263 "entries for 'name' and 'selected' values are included in the "
266 for (
const std::string &str : m_cmd.opt().help_opt_vec()) {
272 log() <<
"Available operators for use within queries:" << std::endl;
274 }
else if (str[0] ==
'p') {
275 log() <<
"Available property tags are currently:" << std::endl;
284 template <
typename DataObject>
289 template <
typename DataObject>
291 if (_count(
"columns")) {
294 }
else if (_count(
"alias")) {
297 }
else if (_count(
"write-pos") || _count(
"write-config") ||
298 _count(
"write-structure")) {
299 if (_count(
"write-pos")) _write_pos();
300 if (_count(
"write-config")) _write_config_json();
301 if (_count(
"write-structure")) _write_structure_json();
309 template <
typename DataObject>
314 std::string new_alias_str;
315 for (
auto const &substr : m_cmd.opt().new_alias_vec()) {
316 new_alias_str += substr;
320 auto it =
std::find(new_alias_str.cbegin(), new_alias_str.cend(),
'=');
321 std::string alias_name =
322 boost::trim_copy(std::string(new_alias_str.cbegin(), it));
323 std::string alias_command =
324 boost::trim_copy(std::string(++it, new_alias_str.cend()));
330 }
catch (std::runtime_error &e) {
331 err_log() <<
"Unable to learn alias\n"
332 <<
" \"" << alias_name <<
" = " << alias_command <<
"\"\n"
333 << e.what() << std::endl;
338 template <
typename DataObject>
340 using namespace query_impl;
341 for (
const auto &obj : _sel().
selected()) {
346 template <
typename DataObject>
348 using namespace query_impl;
349 for (
const auto &obj : _sel().
selected()) {
354 template <
typename DataObject>
356 using namespace query_impl;
357 for (
const auto &obj : _sel().
selected()) {
363 bool _check_gz(fs::path p) {
364 if (p.extension() ==
".gz" || p.extension() ==
".GZ") {
370 bool _check_json(fs::path p) {
371 if (p.extension() ==
".json" || p.extension() ==
".JSON") {
378 template <
typename DataObject>
381 DataObject
const &
object) {
382 std::stringstream msg;
383 msg <<
"Error in `casm query`: --include-equivalents not valid for type '"
385 throw std::runtime_error(msg.str());
392 bool include_equivalents =
true;
395 for (
auto const &configuration : enumerator) {
403 template <
typename DataObject>
406 DataObject
const &
object) {
407 std::stringstream msg;
408 msg <<
"Error in `casm query`: --include-equivalents not valid for type '"
410 throw std::runtime_error(msg.str());
417 bool include_equivalents =
true;
420 for (
auto const &configuration : enumerator) {
423 stream << formatter(data);
428 template <
typename DataObject>
433 bool include_equivalents = _count(
"include-equivalents");
436 std::unique_ptr<std::ostream> uniq_fout;
438 _count(
"output"),
log(), uniq_fout, _output_path(), _write_gz());
443 Log *status_log_ptr =
444 (_output_path().string() ==
"STDOUT") ? &
err_log() : &
log();
446 throw std::runtime_error(
"Unable to resolve default status log.");
447 Log &status_log(*status_log_ptr);
448 if (_output_path().
string() ==
"STDOUT") {
453 status_log <<
"Print:"
456 for (
int p = 0; p < _columns_vec().size(); p++) {
457 status_log <<
" - " << _columns_vec()[p]
460 if (_count(
"output")) {
461 if (_output_path().
string() ==
"STDOUT") {
462 status_log <<
"to " << _output_path() << std::endl;
464 status_log <<
"to " << fs::absolute(_output_path()) << std::endl;
467 status_log << std::endl;
470 _dict().parse(_all_columns());
472 auto begin = _count(
"all") ? _sel().all().begin() : _sel().selected().begin();
473 auto end = _count(
"all") ? _sel().all().end() : _sel().selected().end();
477 for (
auto it = begin; it != end; ++it) {
478 if (include_equivalents) {
485 output_stream << json;
487 for (
auto it = begin; it != end; ++it) {
488 if (include_equivalents) {
492 output_stream << formatter(data);
498 status_log <<
"\n -Output printed to terminal, since no output file "
501 status_log <<
" DONE." << std::endl << std::endl;
505 template <
typename DataObject>
508 std::vector<std::string> all_columns;
509 if (!m_cmd.opt().verbatim_flag()) {
510 all_columns.push_back(
"name");
512 all_columns.push_back(
"selected");
514 if (_count(
"include-equivalents")) {
515 all_columns.push_back(
"equivalent_index");
517 all_columns.push_back(
"permute_factor_group_op");
518 all_columns.push_back(
"permute_factor_group_op_desc");
519 all_columns.push_back(
"permute_translation");
521 all_columns.insert(all_columns.end(), _columns_vec().begin(),
522 _columns_vec().end());
526 template <
typename DataObject>
528 if (_count(
"json")) {
532 else if (_check_gz(_output_path())) {
533 return _check_json(_output_path().stem());
535 return _check_json(_output_path());
539 template <
typename DataObject>
541 if (m_cmd.opt().gzip_flag()) {
546 return _check_gz(_output_path());
568 std::vector<std::vector<std::string>> allowed_cmd = {
569 {
"alias"}, {
"columns"}, {
"write-pos",
"write-config",
"write-structure"}};
572 std::vector<bool> requested(3,
false);
573 for (
int i = 0; i < allowed_cmd.size(); ++i) {
574 for (
auto const &option : allowed_cmd[i]) {
585 err_log() <<
"Error in 'casm query'. Only the following combinations of "
586 "options are allowed: \n "
587 <<
"1) -k/--columns \n"
588 <<
"2) --alias" << allowed_cmd << std::endl;
603 if (!
opt().db_type_opts().
count(
opt().db_type())) {
604 std::stringstream msg;
605 msg <<
"--type " <<
opt().
db_type() <<
" is not allowed for 'casm "
614 m_impl = notstd::make_unique<QueryCommandImplBase>(*
this);
621 sout <<
"The allowed types are:\n";
623 for (
const auto &db_type :
opt().db_type_opts()) {
624 sout <<
" " << db_type << std::endl;
std::set< std::string > & s
const OptionType & opt() const
const po::variables_map & vm() const
int count(std::string s) const
static std::string query()
Get value_type string for query completion.
const po::options_description & desc()
Get the program options, filled with the initialized values.
po::variables_map & vm()
Get the variables map.
po::options_description m_desc
void add_db_type_suboption(std::string _default, std::set< std::string > _configtype_opts)
void add_general_help_suboption()
Add a smart –help suboption that takes "properties" or "operators".
void add_gzip_suboption()
void add_selection_suboption(const fs::path &_default="MASTER")
Add –selection suboption (defaults to MASTER)
void add_output_suboption()
Add a –output suboption. Expects to allow "STDOUT" to print to screen.
std::vector< std::string > m_columns_vec
const fs::path & selection_path() const
Returns the string corresponding to add_config_suboption()
void initialize() override
Fill in the options descriptions accordingly.
bool verbatim_flag() const
std::string db_type() const
const std::vector< std::string > & new_alias_vec() const
const std::vector< std::string > & columns_vec() const
std::vector< std::string > m_new_alias_vec
const fs::path output_path() const
Returns the path corresponding to add_output_suboption()
Enumerate all equivalent Configurations obtained by permuations in a Supercell.
ConfigInsertResult insert(bool primitive_only=false) const
Insert this configuration (in primitive & canonical form) in the database.
Specification of CASM project directory structure.
void set_verbosity(int _verbosity)
void error(const std::string &what)
PrimClex is the top-level data structure for a CASM project.
void set_query_alias(query_alias_map_type const &_query_alias)
QueryCommandImplBase & impl() const
static const std::string name
std::unique_ptr< QueryCommandImplBase > m_impl
int help() const override
QueryCommand(const CommandArgs &_args, Completer::QueryOption &_opt)
int vm_count_check() const override
int desc() const override
void print_names(std::ostream &sout) const
Defaults used if DataObject type doesn't matter or not given.
const QueryCommand & m_cmd
virtual ~QueryCommandImplBase()
QueryCommandImplBase(const QueryCommand &cmd)
std::string _sel_str() const
DataFormatterDictionary< QueryData< DataObject > > m_query_dict
void _write_config_json() const
DB::InterfaceData< DataObject > m_data
const std::vector< std::string > & _columns_vec() const
fs::path _output_path() const
DB::Selection< DataObject > & _sel(Index i=0)
int help() const override
QueryCommandImpl(const QueryCommand &cmd)
const DB::Selection< DataObject > & _sel(Index i=0) const
void _write_structure_json() const
const fs::path & _selection_path() const
const DataFormatterDictionary< QueryData< DataObject > > & _dict() const
std::vector< std::string > _all_columns() const
int _count(std::string s) const
int desc() const override
DataFormatterDictionary< QueryData< DataObject > > & _dict()
Represents a supercell of the primitive parent crystal structure.
static jsonParser array()
Returns an empty json array.
std::ostream & make_ostream_if(bool output, std::ostream &sout, std::unique_ptr< std::ostream > &fout, fs::path out_path, bool gzip)
Return a reference to proper std::ostream.
void commit(ProjectSettings const &set)
jsonParser & push_back(const T &value, Args &&... args)
GenericDatumFormatter< bool, ConfigEnumDataType > selected()
void for_type_short(std::string short_name, F f)
const std::set< std::string > & types_short()
std::set of all QueryTraits<DataObject>::short_name
Generic1DDatumFormatter< Eigen::Vector3l, QueryDataType > permute_translation()
GenericDatumFormatter< Index, QueryDataType > equivalent_index()
GenericDatumFormatter< std::string, QueryDataType > permute_factor_group_op_desc()
Description of permute factor group operation in the prim factor group.
GenericDatumFormatter< Index, QueryDataType > permute_factor_group_op()
void write_config_json(Supercell const &supercell, DirectoryStructure const &dir)
For 'write_config_json' with Supercell use 'write_lat'.
void write_pos(Supercell const &supercell, DirectoryStructure const &dir)
For 'write_pos' with Supercell use 'write_lat'.
void write_structure_json(Supercell const &supercell, DirectoryStructure const &dir)
For 'write_config_json' with Supercell use 'write_lat'.
void _query_equivalents(DataFormatter< QueryData< DataObject >> &formatter, jsonParser &json, PrimClex const &primclex, DataObject const &object)
void _update_query_dict(DataFormatterDictionary< QueryData< DataObject >> &query_dict)
void write_lat(Supercell const &supercell, DirectoryStructure const &dir)
DatumFormatterAdapter< ExternalType, InternalType > make_datum_formatter_adapter(BaseDatumFormatter< InternalType > const &_base_formatter)
Iterator find(Iterator begin, Iterator end, const T &value, BinaryCompare q)
Equivalent to std::find(begin, end, value), but with custom comparison.
std::string help()
Uses 'multiline_help<T>()' by default.
void _update_query_dict< Configuration >(DataFormatterDictionary< QueryData< Configuration >> &query_dict)
INDEX_TYPE Index
For long integer indexing:
DirectoryStructure const & dir
Data structure holding basic CASM command info.
PrimClex const & primclex
Provides access to project data.