Exemplo n.º 1
0
int main (int argc, char * argv[])
{
   bool trace_parsing;
   bool trace_scanning;
   po::options_description visible_options(std::string("Usage: ") + argv[0] + " [options] file ...\nOptions");
   visible_options.add_options()
   ("help", "display this help")
   ("output,o", po::value<std::string>(), "output file (stdout is used by default)")
   ("trace_parsing,p", po::value(&trace_parsing)->zero_tokens(), "trace parsing")
   ("trace_scanning,s", po::value(&trace_scanning)->zero_tokens(), "trace scanning")
   ("max_var_len,M", po::value<size_t>()->default_value(10), "max length of obfuscated variable name")
   ("min_var_len,m", po::value<size_t>()->default_value(1), "min length of obfuscated variable name")
   ;

   po::options_description hidden_options("Hidden options");
   hidden_options.add_options()
   ("input", po::value< std::vector<std::string> >(), "input file")
   ;

   po::options_description cmdline_options;
   cmdline_options.add(visible_options).add(hidden_options);

   po::positional_options_description p;
   p.add("input", -1);

   po::variables_map vm;
   po::store(po::command_line_parser(argc, argv).options(cmdline_options).positional(p).run(), vm);
   po::notify(vm);

   if (vm.count("help") || argc == 1)
   {
      std::cout << visible_options << std::endl;
      return EXIT_SUCCESS;
   }

   if (!vm.count("input"))
   {
      std::cerr << "At least 1 input file must be specified" << std::endl;
      return EXIT_FAILURE;
   }

   std::ostream * output = vm.count("output") ? new std::ofstream(vm["output"].as<std::string>()) : &std::cout;

   obfuscation::obfuscator o(*output, vm["min_var_len"].as<size_t>(), vm["max_var_len"].as<size_t>(), trace_parsing, trace_scanning);

   int res = 0;

   for (const auto & filename : vm["input"].as< std::vector<std::string> >())
   {
      res |= o.obfuscate(filename);
   }

   if (vm.count("output"))
   {
      delete output;
   }

   return res;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
    try
    {
        LogPolicy::GetInstance().Unmute();
        TIMER_START(preparing);
        TIMER_START(expansion);

        boost::filesystem::path config_file_path, input_path, restrictions_path, profile_path;
        unsigned int requested_num_threads;
        bool use_elevation;

        // declare a group of options that will be allowed only on command line
        boost::program_options::options_description generic_options("Options");
        generic_options.add_options()("version,v", "Show version")("help,h",
                                                                   "Show this help message")(
            "config,c",
            boost::program_options::value<boost::filesystem::path>(&config_file_path)
                ->default_value("contractor.ini"),
            "Path to a configuration file.");

        // declare a group of options that will be allowed both on command line and in config file
        boost::program_options::options_description config_options("Configuration");
        config_options.add_options()(
            "restrictions,r",
            boost::program_options::value<boost::filesystem::path>(&restrictions_path),
            "Restrictions file in .osrm.restrictions format")(
            "profile,p",
            boost::program_options::value<boost::filesystem::path>(&profile_path)
                ->default_value("profile.lua"),"Path to LUA routing profile")(
            "elevation,e", boost::program_options::value<bool>(&use_elevation)->default_value(true),
                "Process node elevations")(
            "threads,t",
            boost::program_options::value<unsigned int>(&requested_num_threads)->default_value(tbb::task_scheduler_init::default_num_threads()),
            "Number of threads to use");

        // hidden options, will be allowed both on command line and in config file, but will not be
        // shown to the user
        boost::program_options::options_description hidden_options("Hidden options");
        hidden_options.add_options()(
            "input,i",
            boost::program_options::value<boost::filesystem::path>(&input_path),
            "Input file in .osm, .osm.bz2 or .osm.pbf format");

        // positional option
        boost::program_options::positional_options_description positional_options;
        positional_options.add("input", 1);

        // combine above options for parsing
        boost::program_options::options_description cmdline_options;
        cmdline_options.add(generic_options).add(config_options).add(hidden_options);

        boost::program_options::options_description config_file_options;
        config_file_options.add(config_options).add(hidden_options);

        boost::program_options::options_description visible_options(
            "Usage: " + boost::filesystem::basename(argv[0]) + " <input.osrm> [options]");
        visible_options.add(generic_options).add(config_options);

        // parse command line options
        boost::program_options::variables_map option_variables;
        boost::program_options::store(boost::program_options::command_line_parser(argc, argv)
                                          .options(cmdline_options)
                                          .positional(positional_options)
                                          .run(),
                                      option_variables);

        if (option_variables.count("version"))
        {
            SimpleLogger().Write() << g_GIT_DESCRIPTION;
            return 0;
        }

        if (option_variables.count("help"))
        {
            SimpleLogger().Write() << "\n" << visible_options;
            return 0;
        }

        boost::program_options::notify(option_variables);

        if (!option_variables.count("restrictions"))
        {
            restrictions_path = std::string(input_path.string() + ".restrictions");
        }

        if (!option_variables.count("input"))
        {
            SimpleLogger().Write() << "\n" << visible_options;
            return 0;
        }

        if (!boost::filesystem::is_regular_file(input_path))
        {
            SimpleLogger().Write(logWARNING) << "Input file " << input_path.string()
                                             << " not found!";
            return 1;
        }

        if (!boost::filesystem::is_regular_file(profile_path))
        {
            SimpleLogger().Write(logWARNING) << "Profile " << profile_path.string()
                                             << " not found!";
            return 1;
        }

        if (1 > requested_num_threads)
        {
            SimpleLogger().Write(logWARNING) << "Number of threads must be 1 or larger";
            return 1;
        }

        const unsigned recommended_num_threads = tbb::task_scheduler_init::default_num_threads();

        SimpleLogger().Write() << "Input file: " << input_path.filename().string();
        SimpleLogger().Write() << "Restrictions file: " << restrictions_path.filename().string();
        SimpleLogger().Write() << "Profile: " << profile_path.filename().string();
        SimpleLogger().Write() << "Using elevation: " << use_elevation;
        SimpleLogger().Write() << "Threads: " << requested_num_threads;
        if (recommended_num_threads != requested_num_threads)
        {
            SimpleLogger().Write(logWARNING) << "The recommended number of threads is "
                                             << recommended_num_threads
                                             << "! This setting may have performance side-effects.";
        }

        tbb::task_scheduler_init init(requested_num_threads);

        LogPolicy::GetInstance().Unmute();
        boost::filesystem::ifstream restriction_stream(restrictions_path, std::ios::binary);
        TurnRestriction restriction;
        FingerPrint fingerprint_loaded, fingerprint_orig;
        unsigned number_of_usable_restrictions = 0;
        restriction_stream.read((char *)&fingerprint_loaded, sizeof(FingerPrint));
        if (!fingerprint_loaded.TestPrepare(fingerprint_orig))
        {
            SimpleLogger().Write(logWARNING) << ".restrictions was prepared with different build.\n"
                                                "Reprocess to get rid of this warning.";
        }

        restriction_stream.read((char *)&number_of_usable_restrictions, sizeof(unsigned));
        restriction_list.resize(number_of_usable_restrictions);
        if (number_of_usable_restrictions > 0)
        {
            restriction_stream.read((char *)&(restriction_list[0]),
                                number_of_usable_restrictions * sizeof(TurnRestriction));
        }
        restriction_stream.close();

        boost::filesystem::ifstream in;
        in.open(input_path, std::ios::in | std::ios::binary);

        const std::string node_filename = input_path.string() + ".nodes";
        const std::string edge_out = input_path.string() + ".edges";
        const std::string geometry_filename = input_path.string() + ".geometry";
        const std::string graphOut = input_path.string() + ".hsgr";
        const std::string rtree_nodes_path = input_path.string() + ".ramIndex";
        const std::string rtree_leafs_path = input_path.string() + ".fileIndex";

        /*** Setup Scripting Environment ***/

        // Create a new lua state
        lua_State *lua_state = luaL_newstate();

        // Connect LuaBind to this lua state
        luabind::open(lua_state);

        // open utility libraries string library;
        luaL_openlibs(lua_state);

        // adjust lua load path
        luaAddScriptFolderToLoadPath(lua_state, profile_path.string().c_str());

        // Now call our function in a lua script
        if (0 != luaL_dofile(lua_state, profile_path.string().c_str()))
        {
            std::cerr << lua_tostring(lua_state, -1) << " occured in scripting block" << std::endl;
            return 1;
        }

        EdgeBasedGraphFactory::SpeedProfileProperties speed_profile;

        if (0 != luaL_dostring(lua_state, "return traffic_signal_penalty\n"))
        {
            std::cerr << lua_tostring(lua_state, -1) << " occured in scripting block" << std::endl;
            return 1;
        }
        speed_profile.trafficSignalPenalty = 10 * lua_tointeger(lua_state, -1);
        SimpleLogger().Write(logDEBUG)
            << "traffic_signal_penalty: " << speed_profile.trafficSignalPenalty;

        if (0 != luaL_dostring(lua_state, "return u_turn_penalty\n"))
        {
            std::cerr << lua_tostring(lua_state, -1) << " occured in scripting block" << std::endl;
            return 1;
        }
        speed_profile.uTurnPenalty = 10 * lua_tointeger(lua_state, -1);

        speed_profile.has_turn_penalty_function = lua_function_exists(lua_state, "turn_function");

        #ifdef WIN32
        #pragma message ("Memory consumption on Windows can be higher due to memory alignment")
        #else
        static_assert(sizeof(ImportEdge) == 20,
                      "changing ImportEdge type has influence on memory consumption!");
        #endif
        std::vector<ImportEdge> edge_list;
        NodeID number_of_node_based_nodes =
            readBinaryOSRMGraphFromStream(in,
                                          edge_list,
                                          barrier_node_list,
                                          traffic_light_list,
                                          &internal_to_external_node_map,
                                          restriction_list,
                                          use_elevation);
        in.close();

        if (edge_list.empty())
        {
            SimpleLogger().Write(logWARNING) << "The input data is empty, exiting.";
            return 1;
        }

        SimpleLogger().Write() << restriction_list.size() << " restrictions, "
                               << barrier_node_list.size() << " bollard nodes, "
                               << traffic_light_list.size() << " traffic lights";

        /***
         * Building an edge-expanded graph from node-based input and turn restrictions
         */

        SimpleLogger().Write() << "Generating edge-expanded graph representation";
        std::shared_ptr<NodeBasedDynamicGraph> node_based_graph =
            NodeBasedDynamicGraphFromImportEdges(number_of_node_based_nodes, edge_list);
        std::unique_ptr<RestrictionMap> restriction_map =
            std::unique_ptr<RestrictionMap>(new RestrictionMap(node_based_graph, restriction_list));
        EdgeBasedGraphFactory *edge_based_graph_factor =
            new EdgeBasedGraphFactory(node_based_graph,
                                      std::move(restriction_map),
                                      barrier_node_list,
                                      traffic_light_list,
                                      internal_to_external_node_map,
                                      speed_profile);
        edge_list.clear();
        edge_list.shrink_to_fit();

        edge_based_graph_factor->Run(edge_out, geometry_filename, lua_state);

        restriction_list.clear();
        restriction_list.shrink_to_fit();
        barrier_node_list.clear();
        barrier_node_list.shrink_to_fit();
        traffic_light_list.clear();
        traffic_light_list.shrink_to_fit();

        unsigned number_of_edge_based_nodes = edge_based_graph_factor->GetNumberOfEdgeBasedNodes();
        BOOST_ASSERT(number_of_edge_based_nodes != std::numeric_limits<unsigned>::max());
        DeallocatingVector<EdgeBasedEdge> edgeBasedEdgeList;
        #ifndef WIN32
        static_assert(sizeof(EdgeBasedEdge) == 16,
                      "changing ImportEdge type has influence on memory consumption!");
        #endif

        edge_based_graph_factor->GetEdgeBasedEdges(edgeBasedEdgeList);
        std::vector<EdgeBasedNode> node_based_edge_list;
        edge_based_graph_factor->GetEdgeBasedNodes(node_based_edge_list);
        delete edge_based_graph_factor;

        // TODO actually use scoping: Split this up in subfunctions
        node_based_graph.reset();

        TIMER_STOP(expansion);

        // Building grid-like nearest-neighbor data structure
        SimpleLogger().Write() << "building r-tree ...";
        StaticRTree<EdgeBasedNode> *rtree =
            new StaticRTree<EdgeBasedNode>(node_based_edge_list,
                                           rtree_nodes_path.c_str(),
                                           rtree_leafs_path.c_str(),
                                           internal_to_external_node_map);
        delete rtree;
        IteratorbasedCRC32<std::vector<EdgeBasedNode>> crc32;
        unsigned node_based_edge_list_CRC32 =
            crc32(node_based_edge_list.begin(), node_based_edge_list.end());
        node_based_edge_list.clear();
        node_based_edge_list.shrink_to_fit();
        SimpleLogger().Write() << "CRC32: " << node_based_edge_list_CRC32;

        /***
         * Writing info on original (node-based) nodes
         */

        SimpleLogger().Write() << "writing node map ...";
        boost::filesystem::ofstream node_stream(node_filename, std::ios::binary);
        const unsigned size_of_mapping = internal_to_external_node_map.size();
        node_stream.write((char *)&size_of_mapping, sizeof(unsigned));
        if (size_of_mapping > 0)
        {
            node_stream.write((char *)&(internal_to_external_node_map[0]),
                          size_of_mapping * sizeof(NodeInfo));
        }
        node_stream.close();
        internal_to_external_node_map.clear();
        internal_to_external_node_map.shrink_to_fit();

        /***
         * Contracting the edge-expanded graph
         */

        SimpleLogger().Write() << "initializing contractor";
        Contractor *contractor = new Contractor(number_of_edge_based_nodes, edgeBasedEdgeList);

        TIMER_START(contraction);
        contractor->Run();
        TIMER_STOP(contraction);

        SimpleLogger().Write() << "Contraction took " << TIMER_SEC(contraction) << " sec";

        DeallocatingVector<QueryEdge> contracted_edge_list;
        contractor->GetEdges(contracted_edge_list);
        delete contractor;

        /***
         * Sorting contracted edges in a way that the static query graph can read some in in-place.
         */

        std::sort(contracted_edge_list.begin(), contracted_edge_list.end());
        unsigned max_used_node_id = 0;
        unsigned contracted_edge_count = contracted_edge_list.size();
        SimpleLogger().Write() << "Serializing compacted graph of " << contracted_edge_count
                               << " edges";

        boost::filesystem::ofstream hsgr_output_stream(graphOut, std::ios::binary);
        hsgr_output_stream.write((char *)&fingerprint_orig, sizeof(FingerPrint));
        for (const QueryEdge &edge : contracted_edge_list)
        {
            BOOST_ASSERT(UINT_MAX != edge.source);
            BOOST_ASSERT(UINT_MAX != edge.target);

            max_used_node_id = std::max(max_used_node_id, edge.source);
            max_used_node_id = std::max(max_used_node_id, edge.target);
        }
        SimpleLogger().Write(logDEBUG) << "input graph has " << number_of_edge_based_nodes
                                       << " nodes";
        SimpleLogger().Write(logDEBUG) << "contracted graph has " << max_used_node_id << " nodes";
        max_used_node_id += 1;

        std::vector<StaticGraph<EdgeData>::NodeArrayEntry> node_array;
        node_array.resize(number_of_edge_based_nodes + 1);

        SimpleLogger().Write() << "Building node array";
        StaticGraph<EdgeData>::EdgeIterator edge = 0;
        StaticGraph<EdgeData>::EdgeIterator position = 0;
        StaticGraph<EdgeData>::EdgeIterator last_edge = edge;

        for (StaticGraph<EdgeData>::NodeIterator node = 0; node < max_used_node_id; ++node)
        {
            last_edge = edge;
            while ((edge < contracted_edge_count) && (contracted_edge_list[edge].source == node))
            {
                ++edge;
            }
            node_array[node].first_edge = position; //=edge
            position += edge - last_edge;           // remove
        }

        for (unsigned sentinel_counter = max_used_node_id; sentinel_counter != node_array.size();
             ++sentinel_counter)
        {
            // sentinel element, guarded against underflow
            node_array[sentinel_counter].first_edge = contracted_edge_count;
        }

        unsigned node_array_size = node_array.size();
        // serialize crc32, aka checksum
        hsgr_output_stream.write((char *)&node_based_edge_list_CRC32, sizeof(unsigned));
        // serialize number of nodes
        hsgr_output_stream.write((char *)&node_array_size, sizeof(unsigned));
        // serialize number of edges
        hsgr_output_stream.write((char *)&contracted_edge_count, sizeof(unsigned));
        // serialize all nodes
        if (node_array_size > 0)
        {
            hsgr_output_stream.write((char *)&node_array[0],
                                 sizeof(StaticGraph<EdgeData>::NodeArrayEntry) * node_array_size);
        }
        // serialize all edges

        SimpleLogger().Write() << "Building edge array";
        edge = 0;
        int number_of_used_edges = 0;

        StaticGraph<EdgeData>::EdgeArrayEntry current_edge;
        for (unsigned edge = 0; edge < contracted_edge_list.size(); ++edge)
        {
            // no eigen loops
            BOOST_ASSERT(contracted_edge_list[edge].source != contracted_edge_list[edge].target);
            current_edge.target = contracted_edge_list[edge].target;
            current_edge.data = contracted_edge_list[edge].data;

            // every target needs to be valid
            BOOST_ASSERT(current_edge.target < max_used_node_id);
#ifndef NDEBUG
            if (current_edge.data.distance <= 0)
            {
                SimpleLogger().Write(logWARNING)
                    << "Edge: " << edge << ",source: " << contracted_edge_list[edge].source
                    << ", target: " << contracted_edge_list[edge].target
                    << ", dist: " << current_edge.data.distance;

                SimpleLogger().Write(logWARNING) << "Failed at adjacency list of node "
                                                 << contracted_edge_list[edge].source << "/"
                                                 << node_array.size() - 1;
                return 1;
            }
#endif
            hsgr_output_stream.write((char *)&current_edge,
                                     sizeof(StaticGraph<EdgeData>::EdgeArrayEntry));
            ++number_of_used_edges;
        }
        hsgr_output_stream.close();

        TIMER_STOP(preparing);

        SimpleLogger().Write() << "Preprocessing : " << TIMER_SEC(preparing) << " seconds";
        SimpleLogger().Write() << "Expansion  : "
                               << (number_of_node_based_nodes / TIMER_SEC(expansion))
                               << " nodes/sec and "
                               << (number_of_edge_based_nodes / TIMER_SEC(expansion))
                               << " edges/sec";

        SimpleLogger().Write() << "Contraction: "
                               << (number_of_edge_based_nodes / TIMER_SEC(contraction))
                               << " nodes/sec and "
                               << number_of_used_edges / TIMER_SEC(contraction)
                               << " edges/sec";

        node_array.clear();
        SimpleLogger().Write() << "finished preprocessing";
    }
    catch (boost::program_options::too_many_positional_options_error &)
    {
        SimpleLogger().Write(logWARNING) << "Only one file can be specified";
        return 1;
    }
    catch (boost::program_options::error &e)
    {
        SimpleLogger().Write(logWARNING) << e.what();
        return 1;
    }
    catch (const std::exception &e)
    {
        SimpleLogger().Write(logWARNING) << "Exception occured: " << e.what() << std::endl;
        return 1;
    }
    return 0;
}
Exemplo n.º 3
0
// generate boost::program_options object for the routing part
inline unsigned GenerateServerProgramOptions(const int argc,
                                             const char *argv[],
                                             ServerPaths &paths,
                                             std::string &ip_address,
                                             int &ip_port,
                                             int &requested_num_threads,
                                             bool &use_shared_memory,
                                             bool &trial,
                                             int &max_locations_distance_table,
                                             int &max_locations_map_matching)
{
    // declare a group of options that will be allowed only on command line
    boost::program_options::options_description generic_options("Options");
    generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message")(
        "config,c", boost::program_options::value<boost::filesystem::path>(&paths["config"])
                        ->default_value("server.ini"),
        "Path to a configuration file")(
        "trial", boost::program_options::value<bool>(&trial)->implicit_value(true),
        "Quit after initialization");

    // declare a group of options that will be allowed both on command line
    // as well as in a config file
    boost::program_options::options_description config_options("Configuration");
    config_options.add_options()(
        "hsgrdata", boost::program_options::value<boost::filesystem::path>(&paths["hsgrdata"]),
        ".hsgr file")("nodesdata",
                      boost::program_options::value<boost::filesystem::path>(&paths["nodesdata"]),
                      ".nodes file")(
        "edgesdata", boost::program_options::value<boost::filesystem::path>(&paths["edgesdata"]),
        ".edges file")("geometry",
                       boost::program_options::value<boost::filesystem::path>(&paths["geometries"]),
                       ".geometry file")(
        "ramindex", boost::program_options::value<boost::filesystem::path>(&paths["ramindex"]),
        ".ramIndex file")(
        "fileindex", boost::program_options::value<boost::filesystem::path>(&paths["fileindex"]),
        "File index file")(
        "namesdata", boost::program_options::value<boost::filesystem::path>(&paths["namesdata"]),
        ".names file")("timestamp",
                       boost::program_options::value<boost::filesystem::path>(&paths["timestamp"]),
                       ".timestamp file")(
        "ip,i", boost::program_options::value<std::string>(&ip_address)->default_value("0.0.0.0"),
        "IP address")("port,p", boost::program_options::value<int>(&ip_port)->default_value(5000),
                      "TCP/IP port")(
        "threads,t", boost::program_options::value<int>(&requested_num_threads)->default_value(8),
        "Number of threads to use")(
        "shared-memory,s",
        boost::program_options::value<bool>(&use_shared_memory)->implicit_value(true),
        "Load data from shared memory")(
        "max-table-size,m",
        boost::program_options::value<int>(&max_locations_distance_table)->default_value(1000000),
        "Max. locations supported in distance table query")(
        "max-matching-size,m",
        boost::program_options::value<int>(&max_locations_map_matching)->default_value(2),
        "Max. locations supported in map matching query");

    // hidden options, will be allowed both on command line and in config
    // file, but will not be shown to the user
    boost::program_options::options_description hidden_options("Hidden options");
    hidden_options.add_options()(
        "base,b", boost::program_options::value<boost::filesystem::path>(&paths["base"]),
        "base path to .osrm file");

    // positional option
    boost::program_options::positional_options_description positional_options;
    positional_options.add("base", 1);

    // combine above options for parsing
    boost::program_options::options_description cmdline_options;
    cmdline_options.add(generic_options).add(config_options).add(hidden_options);

    boost::program_options::options_description config_file_options;
    config_file_options.add(config_options).add(hidden_options);

    boost::program_options::options_description visible_options(
        boost::filesystem::basename(argv[0]) + " <base.osrm> [<options>]");
    visible_options.add(generic_options).add(config_options);

    // parse command line options
    boost::program_options::variables_map option_variables;
    boost::program_options::store(boost::program_options::command_line_parser(argc, argv)
                                      .options(cmdline_options)
                                      .positional(positional_options)
                                      .run(),
                                  option_variables);

    if (option_variables.count("version"))
    {
        SimpleLogger().Write() << g_GIT_DESCRIPTION;
        return INIT_OK_DO_NOT_START_ENGINE;
    }

    if (option_variables.count("help"))
    {
        SimpleLogger().Write() << visible_options;
        return INIT_OK_DO_NOT_START_ENGINE;
    }

    boost::program_options::notify(option_variables);

    // parse config file
    auto path_iterator = paths.find("config");
    if (path_iterator != paths.end() && boost::filesystem::is_regular_file(path_iterator->second) &&
        !option_variables.count("base"))
    {
        SimpleLogger().Write() << "Reading options from: " << path_iterator->second.string();
        std::string ini_file_contents = read_file_lower_content(path_iterator->second);
        std::stringstream config_stream(ini_file_contents);
        boost::program_options::store(parse_config_file(config_stream, config_file_options),
                                      option_variables);
        boost::program_options::notify(option_variables);
        return INIT_OK_START_ENGINE;
    }

    if (1 > requested_num_threads)
    {
        throw osrm::exception("Number of threads must be a positive number");
    }

    if (!use_shared_memory && option_variables.count("base"))
    {
        return INIT_OK_START_ENGINE;
    }
    if (use_shared_memory && !option_variables.count("base"))
    {
        return INIT_OK_START_ENGINE;
    }
    if (1 > max_locations_distance_table)
    {
        throw osrm::exception("Max location for distance table must be a positive number");
    }
    if (2 > max_locations_map_matching)
    {
        throw osrm::exception("Max location for map matching must be at least two");
    }

    SimpleLogger().Write() << visible_options;
    return INIT_OK_DO_NOT_START_ENGINE;
}
return_code
ExtractorOptions::ParseArguments(int argc, char *argv[], ExtractorConfig &extractor_config)
{
    // declare a group of options that will be allowed only on command line
    boost::program_options::options_description generic_options("Options");
    generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message")(
        "config,c", boost::program_options::value<boost::filesystem::path>(
                        &extractor_config.config_file_path)->default_value("extractor.ini"),
        "Path to a configuration file.");

    // declare a group of options that will be allowed both on command line and in config file
    boost::program_options::options_description config_options("Configuration");
    config_options.add_options()("profile,p",
                                 boost::program_options::value<boost::filesystem::path>(
                                     &extractor_config.profile_path)->default_value("profile.lua"),
                                 "Path to LUA routing profile")(
        "threads,t",
        boost::program_options::value<unsigned int>(&extractor_config.requested_num_threads)
            ->default_value(tbb::task_scheduler_init::default_num_threads()),
        "Number of threads to use");

    // hidden options, will be allowed both on command line and in config file, but will not be
    // shown to the user
    boost::program_options::options_description hidden_options("Hidden options");
    hidden_options.add_options()("input,i", boost::program_options::value<boost::filesystem::path>(
                                                &extractor_config.input_path),
                                 "Input file in .osm, .osm.bz2 or .osm.pbf format");

    // positional option
    boost::program_options::positional_options_description positional_options;
    positional_options.add("input", 1);

    // combine above options for parsing
    boost::program_options::options_description cmdline_options;
    cmdline_options.add(generic_options).add(config_options).add(hidden_options);

    boost::program_options::options_description config_file_options;
    config_file_options.add(config_options).add(hidden_options);

    boost::program_options::options_description visible_options(
        boost::filesystem::basename(argv[0]) + " <input.osm/.osm.bz2/.osm.pbf> [options]");
    visible_options.add(generic_options).add(config_options);

    // parse command line options
    try
    {
        boost::program_options::variables_map option_variables;
        boost::program_options::store(boost::program_options::command_line_parser(argc, argv)
                                          .options(cmdline_options)
                                          .positional(positional_options)
                                          .run(),
                                      option_variables);
        if (option_variables.count("version"))
        {
            SimpleLogger().Write() << g_GIT_DESCRIPTION;
            return return_code::exit;
        }

        if (option_variables.count("help"))
        {
            SimpleLogger().Write() << visible_options;
            return return_code::exit;
        }

        boost::program_options::notify(option_variables);

        // parse config file
        if (boost::filesystem::is_regular_file(extractor_config.config_file_path))
        {
            SimpleLogger().Write()
                << "Reading options from: " << extractor_config.config_file_path.string();
            std::string ini_file_contents =
                read_file_lower_content(extractor_config.config_file_path);
            std::stringstream config_stream(ini_file_contents);
            boost::program_options::store(parse_config_file(config_stream, config_file_options),
                                          option_variables);
            boost::program_options::notify(option_variables);
        }

        if (!option_variables.count("input"))
        {
            SimpleLogger().Write() << visible_options;
            return return_code::exit;
        }
    }
    catch (std::exception &e)
    {
        SimpleLogger().Write(logWARNING) << e.what();
        return return_code::fail;
    }

    return return_code::ok;
}
Exemplo n.º 5
0
Config::Config(int argc, const char **argv)
{
	Command_line_parser parser;
	parser.add_command("makedb", "Build DIAMOND database from a FASTA file")
		.add_command("blastp", "Align amino acid query sequences against a protein reference database")
		.add_command("blastx", "Align DNA query sequences against a protein reference database")
		.add_command("view", "View DIAMOND alignment archive (DAA) formatted file")
		.add_command("help", "Produce help message")
		.add_command("version", "Display version information")
		.add_command("getseq", "")
		.add_command("benchmark", "")
		.add_command("random-seqs", "")
		.add_command("compare", "");

	Options_group general ("General options");
	general.add()
		("threads", 'p', "number of CPU threads", threads_)
		("db", 'd', "database file", database)
		("out", 'o', "output file", output_file)
		("outfmt", 'f', "output format (tab/sam/xml/daa)", output_format)
		("verbose", 'v', "verbose console output", verbose)
		("log", 0, "enable debug log", debug_log)
		("quiet", 0, "disable console output", quiet);

	Options_group makedb("Makedb options");
	makedb.add()
		("in", 0, "input reference file in FASTA format", input_ref_file)
#ifdef EXTRA
		("dbtype", po::value<string>(&program_options::db_type), "database type (nucl/prot)")
#endif
		;

	Options_group aligner("Aligner options");
	aligner.add()
		("query",'q', "input query file", query_file)
		("max-target-seqs",'k', "maximum number of target sequences to report alignments for", max_alignments, uint64_t(25))
		("top", 0, "report alignments within this percentage range of top alignment score (overrides --max-target-seqs)", toppercent, 100.0)
		("compress", 0, "compression for output files (0=none, 1=gzip)", compression)
		("evalue",'e', "maximum e-value to report alignments", max_evalue, 0.001)
		("min-score", 0, "minimum bit score to report alignments (overrides e-value setting)", min_bit_score)
		("id", 0, "minimum identity% to report an alignment", min_id)
		("query-cover", 0, "minimum query cover% to report an alignment", query_cover)
		("sensitive", 0, "enable sensitive mode (default: fast)", mode_sensitive)
		("more-sensitive", 0, "enable more sensitive mode (default: fast)", mode_more_sensitive)
		("block-size", 'b', "sequence block size in billions of letters (default=2.0)", chunk_size, 2.0)
		("index-chunks",'c', "number of chunks for index processing", lowmem, 4u)
		("tmpdir",'t', "directory for temporary files", tmpdir)
		("gapopen", 0, "gap open penalty (default=11 for protein)", gap_open, -1)
		("gapextend", 0, "gap extension penalty (default=1 for protein)", gap_extend, -1)
#ifdef EXTRA
		("reward", po::value<int>(&program_options::reward)->default_value(2), "match reward score (blastn only)")
		("penalty", po::value<int>(&program_options::penalty)->default_value(-3), "mismatch penalty score (blastn only)")
#endif
		("matrix", 0, "score matrix for protein alignment", matrix, string("blosum62"))
		("seg", 0, "enable SEG masking of queries (yes/no)", seg)
		("salltitles", 0, "print full subject titles in output files", salltitles);

	Options_group advanced("Advanced options");
	advanced.add()		
		("run-len", 'l', "mask runs between stop codons shorter than this length", run_len)
		("freq-sd", 0, "number of standard deviations for ignoring frequent seeds", freq_sd, 0.0)
		("id2", 0, "minimum number of identities for stage 1 hit", min_identities)
		("window", 'w', "window size for local hit search", window)
		("xdrop", 'x', "xdrop for ungapped alignment", xdrop, 20)
		("gapped-xdrop", 'X', "xdrop for gapped alignment in bits", gapped_xdrop, 20)
		("ungapped-score", 0, "minimum raw alignment score to continue local extension", min_ungapped_raw_score)
		("hit-band", 0, "band for hit verification", hit_band)
		("hit-score", 0, "minimum score to keep a tentative alignment", min_hit_score)
		("band", 0, "band for dynamic programming computation", padding)
		("shapes", 's', "number of seed shapes (0 = all available)", shapes)
		("index-mode", 0, "index mode (0=4x12, 1=16x9)", index_mode)
		("fetch-size", 0, "trace point fetch size", fetch_size, 4096u)
		("rank-factor", 0, "include subjects within this range of max-target-seqs", rank_factor, 2.0)
		("rank-ratio", 0, "include subjects within this ratio of last hit", rank_ratio, 0.35)
		("single-domain", 0, "Discard secondary domains within one target sequence", single_domain)
		("dbsize", 0, "effective database size (in letters)", db_size)
		("no-auto-append", 0, "disable auto appending of DAA and DMND file extensions", no_auto_append)
		("target-fetch-size", 0, "", target_fetch_size, 4u);
	
	Options_group view_options("View options");
	view_options.add()
		("daa", 'a', "DIAMOND alignment archive (DAA) file", daa_file)		
		("forwardonly", 0, "only show alignments of forward strand", forwardonly);

	Options_group hidden_options("");
	hidden_options.add()
		("extend-all", 0, "extend all seed hits", extend_all)
		("local-align", 0, "Local alignment algorithm", local_align_mode, 0u)
		("slow-search", 0, "", slow_search)
		("seq", 0, "", seq_no)
		("ht", 0, "", ht_mode)
		("old-freq", 0, "", old_freq)
		("qp", 0, "", query_parallel)
		("match1", 0, "", match_file1)
		("match2", 0, "", match_file2)
		("max-hits", 'C', "maximum number of hits to consider for one seed", hit_cap)
		("seed-freq", 0, "maximum seed frequency", max_seed_freq, -15.0);

	parser.add(general).add(makedb).add(aligner).add(advanced).add(view_options).add(hidden_options);
	parser.store(argc, argv, command);

	switch (command) {
	case Config::makedb:
		if (input_ref_file == "")
			throw std::runtime_error("Missing parameter: input file (--in)");
		if (database == "")
			throw std::runtime_error("Missing parameter: database file (--db/-d)");
		if (chunk_size != 2.0)
			throw std::runtime_error("Invalid option: --block-size/-b. Block size is set for the alignment commands.");
		break;
	case Config::blastp:
	case Config::blastx:
		if (query_file == "")
			throw std::runtime_error("Missing parameter: query file (--query/-q)");
		if (database == "")
			throw std::runtime_error("Missing parameter: database file (--db/-d)");
		if (daa_file.length() > 0) {
			if (output_file.length() > 0)
				throw std::runtime_error("Options --daa and --out cannot be used together.");
			if (output_format.length() > 0 && output_format != "daa")
				throw std::runtime_error("Invalid parameter: --daa/-a. Output file is specified with the --out/-o parameter.");
			output_file = daa_file;
		}
		if (daa_file.length() > 0 || output_format == "daa") {
			if (compression != 0)
				throw std::runtime_error("Compression is not supported for DAA format.");
			if (!no_auto_append)
				auto_append_extension(output_file, ".daa");
		}
		break;
	case Config::view:
		if (daa_file == "")
			throw std::runtime_error("Missing parameter: DAA file (--daa/-a)");
	default:
		;
	}

	if (hit_cap != 0)
		throw std::runtime_error("Deprecated parameter: --max-hits/-C.");

	if (debug_log)
		verbosity = 3;
	else if (quiet)
		verbosity = 0;
	else if (verbose)
		verbosity = 2;
	else if (((command == Config::view || command == blastx || command == blastp) && output_file == "")
		|| command == Config::version)
		verbosity = 0;
	else
		verbosity = 1;

	switch (verbosity) {
	case 0:
		message_stream = Message_stream(false);
		break;
	case 3:
		log_stream = Message_stream();
	case 2:
		verbose_stream = Message_stream();
	default:
		;
	}

	if (!no_auto_append) {
		auto_append_extension(database, ".dmnd");
		if (command == Config::view)
			auto_append_extension(daa_file, ".daa");
		if (compression == 1)
			auto_append_extension(output_file, ".gz");
	}

	message_stream << Const::program_name << " v" << Const::version_string << "." << (unsigned)Const::build_version << " | by Benjamin Buchfink <*****@*****.**>" << endl; 
	message_stream << "Check http://github.com/bbuchfink/diamond for updates." << endl << endl;
#ifndef NDEBUG
	verbose_stream << "Assertions enabled." << endl;
#endif
	set_option(threads_, tthread::thread::hardware_concurrency());

	switch (command) {
	case Config::makedb:
	case Config::blastp:
	case Config::blastx:
	case Config::view:
		message_stream << "#CPU threads: " << threads_ << endl;
	default:
		;
	}	

	if (command == Config::blastp || command == Config::blastx || command == Config::benchmark) {
		if (tmpdir == "")
			tmpdir = extract_dir(output_file);
		if (gap_open == -1)
			gap_open = 11;
		if (gap_extend == -1)
			gap_extend = 1;
		score_matrix = Score_matrix(to_upper_case(matrix), gap_open, gap_extend, reward, penalty);
		message_stream << "Scoring parameters: " << score_matrix << endl;
		if (seg == "" && command == blastx)
			seg = "yes";
		verbose_stream << "SEG masking = " << (seg == "yes") << endl;
		have_ssse3 = check_SSSE3();
		if (have_ssse3)
			verbose_stream << "SSSE3 enabled." << endl;
		verbose_stream << "Reduction: " << Reduction::reduction << endl;

		if (mode_more_sensitive) {
			set_option(index_mode, 1u);
			set_option(freq_sd, 200.0);
		}
		else if (mode_sensitive) {
			set_option(index_mode, 1u);
			set_option(freq_sd, 10.0);
		}
		else {
			set_option(index_mode, 0u);
			set_option(freq_sd, 50.0);
		}

		verbose_stream << "Seed frequency SD: " << freq_sd << endl;
		::shapes = shape_config(index_mode, shapes);
		verbose_stream << "Shape configuration: " << ::shapes << endl;

		message_stream << "#Target sequences to report alignments for: ";
		if (max_alignments == 0) {
			max_alignments = std::numeric_limits<uint64_t>::max();
			message_stream << "unlimited" << endl;
		} else
			message_stream << max_alignments << endl;
	}

	if (command == blastx)
		input_value_traits = nucleotide_traits;
	if (command == help)
		parser.print_help();

	/*log_stream << "sizeof(hit)=" << sizeof(hit) << " sizeof(packed_uint40_t)=" << sizeof(packed_uint40_t)
		<< " sizeof(sorted_list::entry)=" << sizeof(sorted_list::entry) << endl;*/
}
Exemplo n.º 6
0
        int runDbTests( int argc , char** argv , string default_dbpath ) {
            unsigned long long seed = time( 0 );
            int runsPerTest = 1;
            string dbpathSpec;

            po::options_description shell_options("options");
            po::options_description hidden_options("Hidden options");
            po::options_description cmdline_options("Command line options");
            po::positional_options_description positional_options;

            shell_options.add_options()
            ("help,h", "show this usage information")
            ("dbpath", po::value<string>(&dbpathSpec)->default_value(default_dbpath),
             "db data path for this test run. NOTE: the contents of this "
             "directory will be overwritten if it already exists")
            ("debug", "run tests with verbose output")
            ("list,l", "list available test suites")
            ("bigfiles", "use big datafiles instead of smallfiles which is the default")
            ("filter,f" , po::value<string>() , "string substring filter on test name" )
            ("verbose,v", "verbose")
            ("dur", "enable journaling (currently the default)")
            ("nodur", "disable journaling")
            ("seed", po::value<unsigned long long>(&seed), "random number seed")
            ("runs", po::value<int>(&runsPerTest), "number of times to run each test")
            ("perfHist", po::value<unsigned>(&perfHist), "number of back runs of perf stats to display")
            ;

            hidden_options.add_options()
            ("suites", po::value< vector<string> >(), "test suites to run")
            ("nopreallocj", "disable journal prealloc")
            ;

            positional_options.add("suites", -1);

            cmdline_options.add(shell_options).add(hidden_options);

            po::variables_map params;
            int command_line_style = (((po::command_line_style::unix_style ^
                                        po::command_line_style::allow_guessing) |
                                       po::command_line_style::allow_long_disguise) ^
                                      po::command_line_style::allow_sticky);

            try {
                po::store(po::command_line_parser(argc, argv).options(cmdline_options).
                          positional(positional_options).
                          style(command_line_style).run(), params);
                po::notify(params);
            }
            catch (po::error &e) {
                cout << "ERROR: " << e.what() << endl << endl;
                show_help_text(argv[0], shell_options);
                return EXIT_BADOPTIONS;
            }

            if (params.count("help")) {
                show_help_text(argv[0], shell_options);
                return EXIT_CLEAN;
            }

            bool nodur = false;
            if( params.count("nodur") ) {
                nodur = true;
                cmdLine.dur = false;
            }
            if( params.count("dur") || cmdLine.dur ) {
                cmdLine.dur = true;
            }

            if( params.count("nopreallocj") ) {
                cmdLine.preallocj = false;
            }

            if (params.count("debug") || params.count("verbose") ) {
                logger::globalLogDomain()->setMinimumLoggedSeverity(logger::LogSeverity::Debug(1));
            }

            if (params.count("list")) {
                std::vector<std::string> suiteNames = mongo::unittest::getAllSuiteNames();
                for ( std::vector<std::string>::const_iterator i = suiteNames.begin();
                      i != suiteNames.end(); ++i ) {

                    std::cout << *i << std::endl;
                }
                return 0;
            }

            boost::filesystem::path p(dbpathSpec);

            /* remove the contents of the test directory if it exists. */
            if (boost::filesystem::exists(p)) {
                if (!boost::filesystem::is_directory(p)) {
                    cout << "ERROR: path \"" << p.string() << "\" is not a directory" << endl << endl;
                    show_help_text(argv[0], shell_options);
                    return EXIT_BADOPTIONS;
                }
                boost::filesystem::directory_iterator end_iter;
                for (boost::filesystem::directory_iterator dir_iter(p);
                        dir_iter != end_iter; ++dir_iter) {
                    boost::filesystem::remove_all(*dir_iter);
                }
            }
            else {
                boost::filesystem::create_directory(p);
            }

            string dbpathString = p.string();
            dbpath = dbpathString.c_str();

            cmdLine.prealloc = false;

            // dbtest defaults to smallfiles
            cmdLine.smallfiles = true;
            if( params.count("bigfiles") ) {
                cmdLine.dur = true;
            }

            cmdLine.oplogSize = 10 * 1024 * 1024;
            Client::initThread("testsuite");
            acquirePathLock();

            srand( (unsigned) seed );
            printGitVersion();
            printSysInfo();
            DEV log() << "_DEBUG build" << endl;
            if( sizeof(void*)==4 )
                log() << "32bit" << endl;
            log() << "random seed: " << seed << endl;

            if( time(0) % 3 == 0 && !nodur ) {
                if( !cmdLine.dur ) {
                    cmdLine.dur = true;
                    log() << "****************" << endl;
                    log() << "running with journaling enabled to test that. dbtests will do this occasionally even if --dur is not specified." << endl;
                    log() << "****************" << endl;
                }
            }

            FileAllocator::get()->start();

            vector<string> suites;
            if (params.count("suites")) {
                suites = params["suites"].as< vector<string> >();
            }

            string filter = "";
            if ( params.count( "filter" ) ) {
                filter = params["filter"].as<string>();
            }

            dur::startup();

            if( debug && cmdLine.dur ) {
                log() << "_DEBUG: automatically enabling cmdLine.durOptions=8 (DurParanoid)" << endl;
                // this was commented out.  why too slow or something? : 
                cmdLine.durOptions |= 8;
            }

            TestWatchDog twd;
            twd.go();

            // set tlogLevel to -1 to suppress MONGO_TLOG(0) output in a test program
            tlogLevel = -1;

            int ret = ::mongo::unittest::Suite::run(suites,filter,runsPerTest);

#if !defined(_WIN32) && !defined(__sunos__)
            flock( lockFile, LOCK_UN );
#endif

            cc().shutdown();
            dbexit( (ExitCode)ret ); // so everything shuts down cleanly
            return ret;
        }
return_code
ContractorOptions::ParseArguments(int argc, char *argv[], ContractorConfig &contractor_config)
{
    // declare a group of options that will be allowed only on command line
    boost::program_options::options_description generic_options("Options");
    generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message")(
        "config,c", boost::program_options::value<boost::filesystem::path>(&contractor_config.config_file_path)
                        ->default_value("contractor.ini"),
        "Path to a configuration file.");

    // declare a group of options that will be allowed both on command line and in config file
    boost::program_options::options_description config_options("Configuration");
    config_options.add_options()(
        "restrictions,r",
        boost::program_options::value<boost::filesystem::path>(&contractor_config.restrictions_path),
        "Restrictions file in .osrm.restrictions format")(
        "profile,p", boost::program_options::value<boost::filesystem::path>(&contractor_config.profile_path)
                         ->default_value("profile.lua"),
        "Path to LUA routing profile")(
        "threads,t", boost::program_options::value<unsigned int>(&contractor_config.requested_num_threads)
                         ->default_value(tbb::task_scheduler_init::default_num_threads()),
        "Number of threads to use");

    // hidden options, will be allowed both on command line and in config file, but will not be
    // shown to the user
    boost::program_options::options_description hidden_options("Hidden options");
    hidden_options.add_options()(
        "input,i", boost::program_options::value<boost::filesystem::path>(&contractor_config.osrm_input_path),
        "Input file in .osm, .osm.bz2 or .osm.pbf format");

    // positional option
    boost::program_options::positional_options_description positional_options;
    positional_options.add("input", 1);

    // combine above options for parsing
    boost::program_options::options_description cmdline_options;
    cmdline_options.add(generic_options).add(config_options).add(hidden_options);

    boost::program_options::options_description config_file_options;
    config_file_options.add(config_options).add(hidden_options);

    boost::program_options::options_description visible_options(
        "Usage: " + boost::filesystem::basename(argv[0]) + " <input.osrm> [options]");
    visible_options.add(generic_options).add(config_options);

    // parse command line options
    boost::program_options::variables_map option_variables;
    boost::program_options::store(boost::program_options::command_line_parser(argc, argv)
                                      .options(cmdline_options)
                                      .positional(positional_options)
                                      .run(),
                                  option_variables);

    const auto &temp_config_path = option_variables["config"].as<boost::filesystem::path>();
    if (boost::filesystem::is_regular_file(temp_config_path))
    {
        boost::program_options::store(boost::program_options::parse_config_file<char>(
                                          temp_config_path.string().c_str(), cmdline_options, true),
                                      option_variables);
    }

    if (option_variables.count("version"))
    {
        SimpleLogger().Write() << g_GIT_DESCRIPTION;
        return return_code::exit;
    }

    if (option_variables.count("help"))
    {
        SimpleLogger().Write() << "\n" << visible_options;
        return return_code::exit;
    }

    boost::program_options::notify(option_variables);

    if (!option_variables.count("restrictions"))
    {
        contractor_config.restrictions_path = contractor_config.osrm_input_path.string() + ".restrictions";
    }

    if (!option_variables.count("input"))
    {
        SimpleLogger().Write() << "\n" << visible_options;
        return return_code::fail;
    }

    return return_code::ok;
}
Exemplo n.º 8
0
        int Suite::run( int argc , char** argv , string default_dbpath ) {
            unsigned long long seed = time( 0 );
            string dbpathSpec;

            po::options_description shell_options("options");
            po::options_description hidden_options("Hidden options");
            po::options_description cmdline_options("Command line options");
            po::positional_options_description positional_options;

            shell_options.add_options()
                ("help,h", "show this usage information")
                ("dbpath", po::value<string>(&dbpathSpec)->default_value(default_dbpath),
                 "db data path for this test run. NOTE: the contents of this "
                 "directory will be overwritten if it already exists")
                ("debug", "run tests with verbose output")
                ("list,l", "list available test suites")
                ("verbose,v", "verbose")
                ("seed", po::value<unsigned long long>(&seed), "random number seed")
                ;

            hidden_options.add_options()
                ("suites", po::value< vector<string> >(), "test suites to run")
                ;

            positional_options.add("suites", -1);

            cmdline_options.add(shell_options).add(hidden_options);

            po::variables_map params;
            int command_line_style = (((po::command_line_style::unix_style ^
                                        po::command_line_style::allow_guessing) |
                                       po::command_line_style::allow_long_disguise) ^
                                      po::command_line_style::allow_sticky);

            try {
                po::store(po::command_line_parser(argc, argv).options(cmdline_options).
                          positional(positional_options).
                          style(command_line_style).run(), params);
                po::notify(params);
            } catch (po::error &e) {
                cout << "ERROR: " << e.what() << endl << endl;
                show_help_text(argv[0], shell_options);
                return EXIT_BADOPTIONS;
            }

            if (params.count("help")) {
                show_help_text(argv[0], shell_options);
                return EXIT_CLEAN;
            }

            if (params.count("debug") || params.count("verbose") ) {
                logLevel = 1;
            }

            if (params.count("list")) {
                for ( map<string,Suite*>::iterator i = _suites->begin() ; i != _suites->end(); i++ )
                    cout << i->first << endl;
                return 0;
            }

            boost::filesystem::path p(dbpathSpec);

            /* remove the contents of the test directory if it exists. */
            if (boost::filesystem::exists(p)) {
                if (!boost::filesystem::is_directory(p)) {
                    cout << "ERROR: path \"" << p.string() << "\" is not a directory" << endl << endl;
                    show_help_text(argv[0], shell_options);
                    return EXIT_BADOPTIONS;
                }
                boost::filesystem::directory_iterator end_iter;
                for (boost::filesystem::directory_iterator dir_iter(p);
                     dir_iter != end_iter; ++dir_iter) {
                    boost::filesystem::remove_all(*dir_iter);
                }
            } else {
                boost::filesystem::create_directory(p);
            }

            string dbpathString = p.native_directory_string();
            dbpath = dbpathString.c_str();
            
            cmdLine.prealloc = false;
            cmdLine.smallfiles = true;
            cmdLine.oplogSize = 10 * 1024 * 1024;
            Client::initThread("testsuite");
            acquirePathLock();

            srand( (unsigned) seed );
            printGitVersion();
            printSysInfo();
            out() << "random seed: " << seed << endl;

            theFileAllocator().start();

            vector<string> suites;
            if (params.count("suites")) {
                suites = params["suites"].as< vector<string> >();
            }
            int ret = run(suites);

#if !defined(_WIN32) && !defined(__sunos__)
            flock( lockFile, LOCK_UN );
#endif
            
            cc().shutdown();
            dbexit( (ExitCode)ret ); // so everything shuts down cleanly
            return ret;
        }
Exemplo n.º 9
0
int main (int argc, char *argv[]) {
    try {
        LogPolicy::GetInstance().Unmute();
        double startup_time = get_timestamp();

        boost::filesystem::path config_file_path, input_path, profile_path;
        int requested_num_threads;

        // declare a group of options that will be allowed only on command line
        boost::program_options::options_description generic_options("Options");
        generic_options.add_options()
            ("version,v", "Show version")
            ("help,h", "Show this help message")
            ("config,c", boost::program_options::value<boost::filesystem::path>(&config_file_path)->default_value("extractor.ini"),
                  "Path to a configuration file.");

        // declare a group of options that will be allowed both on command line and in config file
        boost::program_options::options_description config_options("Configuration");
        config_options.add_options()
            ("profile,p", boost::program_options::value<boost::filesystem::path>(&profile_path)->default_value("profile.lua"),
                "Path to LUA routing profile")
            ("threads,t", boost::program_options::value<int>(&requested_num_threads)->default_value(8),
                "Number of threads to use");

        // hidden options, will be allowed both on command line and in config file, but will not be shown to the user
        boost::program_options::options_description hidden_options("Hidden options");
        hidden_options.add_options()
            ("input,i", boost::program_options::value<boost::filesystem::path>(&input_path),
                "Input file in .osm, .osm.bz2 or .osm.pbf format");

        // positional option
        boost::program_options::positional_options_description positional_options;
        positional_options.add("input", 1);

        // combine above options for parsing
        boost::program_options::options_description cmdline_options;
        cmdline_options.add(generic_options).add(config_options).add(hidden_options);

        boost::program_options::options_description config_file_options;
        config_file_options.add(config_options).add(hidden_options);

        boost::program_options::options_description visible_options(boost::filesystem::basename(argv[0]) + " <input.osm/.osm.bz2/.osm.pbf> [options]");
        visible_options.add(generic_options).add(config_options);

        // parse command line options
        boost::program_options::variables_map option_variables;
        boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
            options(cmdline_options).positional(positional_options).run(), option_variables);

        if(option_variables.count("version")) {
            SimpleLogger().Write() << g_GIT_DESCRIPTION;
            return 0;
        }

        if(option_variables.count("help")) {
            SimpleLogger().Write() << visible_options;
            return 0;
        }

        boost::program_options::notify(option_variables);

        // parse config file
        if(boost::filesystem::is_regular_file(config_file_path)) {
            SimpleLogger().Write() << "Reading options from: " << config_file_path.c_str();
            std::string config_str;
            PrepareConfigFile( config_file_path.c_str(), config_str );
            std::stringstream config_stream( config_str );
            boost::program_options::store(parse_config_file(config_stream, config_file_options), option_variables);
            boost::program_options::notify(option_variables);
        }

        if(!option_variables.count("input")) {
            SimpleLogger().Write(logWARNING) << "No input file specified";
            SimpleLogger().Write() << visible_options;
            return -1;
        }

        if(1 > requested_num_threads) {
            SimpleLogger().Write(logWARNING) << "Number of threads must be 1 or larger";
            return -1;
        }

        SimpleLogger().Write() << "Input file: " << input_path.filename().string();
        SimpleLogger().Write() << "Profile: " << profile_path.filename().string();
        SimpleLogger().Write() << "Threads: " << requested_num_threads;

        /*** Setup Scripting Environment ***/
        ScriptingEnvironment scriptingEnvironment(profile_path.c_str());

        omp_set_num_threads( std::min( omp_get_num_procs(), requested_num_threads) );

        bool file_has_pbf_format(false);
        std::string output_file_name(input_path.c_str());
        std::string restrictionsFileName(input_path.c_str());
        std::string::size_type pos = output_file_name.find(".osm.bz2");
        if(pos==std::string::npos) {
            pos = output_file_name.find(".osm.pbf");
            if(pos!=std::string::npos) {
                file_has_pbf_format = true;
            }
        }
        if(pos==std::string::npos) {
            pos = output_file_name.find(".pbf");
            if(pos!=std::string::npos) {
                file_has_pbf_format = true;
            }
        }
        if(pos!=std::string::npos) {
            output_file_name.replace(pos, 8, ".osrm");
            restrictionsFileName.replace(pos, 8, ".osrm.restrictions");
        } else {
            pos=output_file_name.find(".osm");
            if(pos!=std::string::npos) {
                output_file_name.replace(pos, 5, ".osrm");
                restrictionsFileName.replace(pos, 5, ".osrm.restrictions");
            } else {
                output_file_name.append(".osrm");
                restrictionsFileName.append(".osrm.restrictions");
            }
        }

        StringMap stringMap;
        ExtractionContainers externalMemory;

        stringMap[""] = 0;
        extractCallBacks = new ExtractorCallbacks(&externalMemory, &stringMap);
        BaseParser* parser;
        if(file_has_pbf_format) {
            parser = new PBFParser(input_path.c_str(), extractCallBacks, scriptingEnvironment);
        } else {
            parser = new XMLParser(input_path.c_str(), extractCallBacks, scriptingEnvironment);
        }

        if(!parser->ReadHeader()) {
            throw OSRMException("Parser not initialized!");
        }
        SimpleLogger().Write() << "Parsing in progress..";
        double parsing_start_time = get_timestamp();
        parser->Parse();
        SimpleLogger().Write() << "Parsing finished after " <<
            (get_timestamp() - parsing_start_time) <<
            " seconds";

        if( externalMemory.all_edges_list.empty() ) {
            SimpleLogger().Write(logWARNING) << "The input data is empty, exiting.";
            return -1;
        }

        externalMemory.PrepareData(output_file_name, restrictionsFileName);

        delete parser;
        delete extractCallBacks;

        SimpleLogger().Write() <<
            "extraction finished after " << get_timestamp() - startup_time <<
            "s";

         SimpleLogger().Write() << "To prepare the data for routing, run: "
            << "./osrm-prepare " << output_file_name << std::endl;

    } catch(boost::program_options::too_many_positional_options_error& e) {
        SimpleLogger().Write(logWARNING) << "Only one input file can be specified";
        return -1;
    } catch(boost::program_options::error& e) {
        SimpleLogger().Write(logWARNING) << e.what();
        return -1;
    } catch(std::exception & e) {
        SimpleLogger().Write(logWARNING) << "Unhandled exception: " << e.what();
        return -1;
    }
    return 0;
}
Exemplo n.º 10
0
        int runDbTests( int argc , char** argv , string default_dbpath ) {
            unsigned long long seed = time( 0 );
            int runsPerTest = 1;
            string dbpathSpec;

            po::options_description shell_options("options");
            po::options_description hidden_options("Hidden options");
            po::options_description cmdline_options("Command line options");
            po::positional_options_description positional_options;

            shell_options.add_options()
            ("help,h", "show this usage information")
            ("dbpath", po::value<string>(&dbpathSpec)->default_value(default_dbpath),
             "db data path for this test run. NOTE: the contents of this "
             "directory will be overwritten if it already exists")
            ("debug", "run tests with verbose output")
            ("list,l", "list available test suites")
            ("filter,f" , po::value<string>() , "string substring filter on test name" )
            ("verbose,v", "be more verbose (include multiple times for more verbosity e.g. -vvvvv)")
            ("dur", "enable journaling (currently the default)")
            ("nodur", "disable journaling")
            ("seed", po::value<unsigned long long>(&seed), "random number seed")
            ("runs", po::value<int>(&runsPerTest), "number of times to run each test")
            ("perfHist", po::value<unsigned>(&perfHist), "number of back runs of perf stats to display")
            ;

            hidden_options.add_options()
            ("suites", po::value< vector<string> >(), "test suites to run")
            ;

            positional_options.add("suites", -1);

            /* support for -vv -vvvv etc. */
            for (string s = "vv"; s.length() <= 10; s.append("v")) {
                hidden_options.add_options()(s.c_str(), "verbose");
            }

            cmdline_options.add(shell_options).add(hidden_options);

            po::variables_map params;
            int command_line_style = (((po::command_line_style::unix_style ^
                                        po::command_line_style::allow_guessing) |
                                       po::command_line_style::allow_long_disguise) ^
                                      po::command_line_style::allow_sticky);

            try {
                po::store(po::command_line_parser(argc, argv).options(cmdline_options).
                          positional(positional_options).
                          style(command_line_style).run(), params);
                po::notify(params);
            }
            catch (po::error &e) {
                cout << "ERROR: " << e.what() << endl << endl;
                show_help_text(argv[0], shell_options);
                return EXIT_BADOPTIONS;
            }

            if (params.count("help")) {
                show_help_text(argv[0], shell_options);
                return EXIT_CLEAN;
            }

            if (params.count("debug") || params.count("verbose") ) {
                logLevel = 1;
            }

            for (string s = "vv"; s.length() <= 10; s.append("v")) {
                if (params.count(s)) {
                    logLevel = s.length();
                }
            }

            if (params.count("list")) {
                std::vector<std::string> suiteNames = mongo::unittest::getAllSuiteNames();
                for ( std::vector<std::string>::const_iterator i = suiteNames.begin();
                      i != suiteNames.end(); ++i ) {

                    std::cout << *i << std::endl;
                }
                return 0;
            }

            boost::filesystem::path p(dbpathSpec);

            /* remove the contents of the test directory if it exists. */
            if (boost::filesystem::exists(p)) {
                if (!boost::filesystem::is_directory(p)) {
                    cout << "ERROR: path \"" << p.string() << "\" is not a directory" << endl << endl;
                    show_help_text(argv[0], shell_options);
                    return EXIT_BADOPTIONS;
                }
                boost::filesystem::directory_iterator end_iter;
                for (boost::filesystem::directory_iterator dir_iter(p);
                        dir_iter != end_iter; ++dir_iter) {
                    boost::filesystem::remove_all(*dir_iter);
                }
            }
            else {
                boost::filesystem::create_directory(p);
            }

            string dbpathString = p.native_directory_string();
            dbpath = dbpathString.c_str();

            Client::initThread("testsuite");
            acquirePathLock();

            srand( (unsigned) seed );
            printGitVersion();
            printSysInfo();
            DEV log() << "_DEBUG build" << endl;
            if( sizeof(void*)==4 )
                log() << "32bit" << endl;
            log() << "random seed: " << seed << endl;

            vector<string> suites;
            if (params.count("suites")) {
                suites = params["suites"].as< vector<string> >();
            }

            string filter = "";
            if ( params.count( "filter" ) ) {
                filter = params["filter"].as<string>();
            }

            mongo::setTxnCompleteHooks(&mongo::_txnCompleteHooks);
            storage::startup();

            TestWatchDog twd;
            twd.go();

            // set tlogLevel to -1 to suppress tlog() output in a test program
            tlogLevel = -1;

            int ret = ::mongo::unittest::Suite::run(suites,filter,runsPerTest);

#if !defined(_WIN32) && !defined(__sunos__)
            flock( lockFile, LOCK_UN );
#endif

            cc().shutdown();
            dbexit( (ExitCode)ret ); // so everything shuts down cleanly
            return ret;
        }
Exemplo n.º 11
0
int main(int argc, char **argv) {
	po::options_description options("Command line options");
	po::options_description hidden_options("Hiden options");
	po::variables_map vm, vm_temp;

	options.add_options()
		("help", "Print information on how to use this program\n")
		("config", po::value<std::string>(),
		    "Load the configuration file 'arg' as an additional source of command line parameters. "
			"Should contain one parameter per line in key=value format. The command line takes precedence "
			"when an argument is specified multiple times.\n")
		("saturation", po::value<float>(),
		    "Saturation threshold of the sensor: the ratio of the sensor's theoretical dynamic "
			"range, at which saturation occurs in practice (in [0,1]). Estimated automatically if not specified.\n")
		("fitexptimes", "On some cameras, the exposure times in the EXIF tags can't be trusted. Use "
		    "this parameter to estimate them automatically for the current image sequence\n")
		("exptimes", po::value<std::string>(),
		    "Override the EXIF exposure times with a manually specified sequence of the "
			"format 'time1,time2,time3,..'\n")
		("nodemosaic", "If specified, the raw Bayer grid is exported as a grayscale EXR file\n")
		("colormode", po::value<EColorMode>()->default_value(ESRGB, "sRGB"),
			"Output color space (one of 'native'/'sRGB'/'XYZ')\n")
		("sensor2xyz", po::value<std::string>(),
			"Matrix that transforms from the sensor color space to XYZ tristimulus values\n")
		("scale", po::value<float>(),
			"Optional scale factor that is applied to the image\n")
		("crop", po::value<std::string>(),
			"Crop to a rectangular area. 'arg' should be specified in the form x,y,width,height\n")
		("resample", po::value<std::string>(),
			"Resample the image to a different resolution. 'arg' can be "
			"a pair of integers like 1188x790 or the max. resolution ("
			"maintaining the aspect ratio)\n")
		("rfilter", po::value<std::string>()->default_value("lanczos"),
			"Resampling filter used by the --resample option (available choices: "
			"'tent' or 'lanczos')\n")
		("wbalpatch", po::value<std::string>(),
		    "White balance the image using a grey patch occupying the region "
			"'arg' (specified as x,y,width,height). Prints output suitable for --wbal\n")
		("wbal", po::value<std::string>(),
		    "White balance the image using floating point multipliers 'arg' "
			"specified as r,g,b\n")
		("vcal", "Calibrate vignetting correction given a uniformly illuminated image\n")
		("vcorr", po::value<std::string>(),
		    "Apply the vignetting correction computed using --vcal\n")
		("flip", po::value<std::string>()->default_value(""), "Flip the output image along the "
		  "specified axes (one of 'x', 'y', or 'xy')\n")
		("rotate", po::value<int>()->default_value(0), "Rotate the output image by 90, 180 or 270 degrees\n")
		("format", po::value<std::string>()->default_value("half"),
		  "Choose the desired output file format -- one of 'half' (OpenEXR, 16 bit HDR / half precision), "
		  "'single' (OpenEXR, 32 bit / single precision), 'jpeg' (libjpeg, 8 bit LDR for convenience)\n")
		("output", po::value<std::string>()->default_value("output.exr"),
			"Name of the output file in OpenEXR format. When only a single RAW file is processed, its "
			"name is used by default (with the ending replaced by .exr/.jpeg");

	hidden_options.add_options()
		("input-files", po::value<std::vector<std::string>>(), "Input files");

	po::options_description all_options;
	all_options.add(options).add(hidden_options);
	po::positional_options_description positional;
	positional.add("input-files", -1);

	try {
		/* Temporary command line parsing pass */
		po::store(po::command_line_parser(argc, argv)
			.options(all_options).positional(positional).run(), vm_temp);

		/* Is there a configuration file */
		std::string config = "hdrmerge.cfg";

		if (vm_temp.count("config"))
			config = vm_temp["config"].as<std::string>();

		if (fexists(config)) {
			std::ifstream settings(config, std::ifstream::in);
			po::store(po::parse_config_file(settings, all_options), vm);
			settings.close();
		}

		po::store(po::command_line_parser(argc, argv)
			.options(all_options).positional(positional).run(), vm);
		if (vm.count("help") || !vm.count("input-files")) {
			help(argv, options);
			return 0;
		}
		po::notify(vm);
	} catch (po::error &e) {
		cerr << "Error while parsing command line arguments: " << e.what() << endl << endl;
		help(argv, options);
		return -1;
	}

	try {
		EColorMode colormode = vm["colormode"].as<EColorMode>();
		std::vector<int> wbalpatch      = parse_list<int>(vm, "wbalpatch", { 4 });
		std::vector<float> wbal         = parse_list<float>(vm, "wbal", { 3 });
		std::vector<int> resample       = parse_list<int>(vm, "resample", { 1, 2 }, ", x");
		std::vector<int> crop           = parse_list<int>(vm, "crop", { 4 });
		std::vector<float> sensor2xyz_v = parse_list<float>(vm, "sensor2xyz", { 9 });
		std::vector<float> vcorr        = parse_list<float>(vm, "vcorr", { 3 });

		if (!wbal.empty() && !wbalpatch.empty()) {
			cerr << "Cannot specify --wbal and --wbalpatch at the same time!" << endl;
			return -1;
		}

		float sensor2xyz[9] = {
			0.412453f, 0.357580f, 0.180423f,
			0.212671f, 0.715160f, 0.072169f,
			0.019334f, 0.119193f, 0.950227f
		};

		if (!sensor2xyz_v.empty()) {
			for (int i=0; i<9; ++i)
				sensor2xyz[i] = sensor2xyz_v[i];
		} else if (colormode != ENative) {
			cerr << "*******************************************************************************" << endl
				 << "Warning: no sensor2xyz matrix was specified -- this is necessary to get proper" << endl
				 << "sRGB / XYZ output. To acquire this matrix, convert any one of your RAW images" << endl
				 << "into a DNG file using Adobe's DNG converter on Windows / Mac (or on Linux," << endl
				 << "using the 'wine' emulator). The run" << endl
				 << endl
				 << "  $ exiv2 -pt the_image.dng 2> /dev/null | grep ColorMatrix2" << endl
				 << "  Exif.Image.ColorMatrix2 SRational 9  <sequence of ratios>" << endl
				 << endl
				 << "The sequence of a rational numbers is a matrix in row-major order. Compute its" << endl
				 << "inverse using a tool like MATLAB or Octave and add a matching entry to the" << endl
				 << "file hdrmerge.cfg (creating it if necessary), like so:" << endl
				 << endl
				 << "# Sensor to XYZ color space transform (Canon EOS 50D)" << endl
				 << "sensor2xyz=1.933062 -0.1347 0.217175 0.880916 0.725958 -0.213945 0.089893 " << endl
				 << "-0.363462 1.579612" << endl
				 << endl
				 << "-> Providing output in the native sensor color space, as no matrix was given." << endl
				 << "*******************************************************************************" << endl
				 << endl;

			colormode = ENative;
		}

		std::vector<std::string> exposures = vm["input-files"].as<std::vector<std::string>>();
		float scale = 1.0f;
		if (vm.count("scale"))
			scale = vm["scale"].as<float>();

		/// Step 1: Load RAW
		ExposureSeries es;
		for (size_t i=0; i<exposures.size(); ++i)
			es.add(exposures[i]);
		es.check();
		if (es.size() == 0)
			throw std::runtime_error("No input found / list of exposures to merge is empty!");

		std::vector<float> exptimes;
		std::map<float, float> exptimes_map;
		if (vm.count("exptimes")) {
			std::string value = vm["exptimes"].as<std::string>();

			if (value.find("->") == std::string::npos) {
				/* Normal list of exposure times, load directly */
				exptimes = parse_list<float>(vm, "exptimes", { es.size() });
			} else {
				/* Map of exposure time replacement values */
				std::vector<std::string> map_str = parse_list<std::string>(vm, "exptimes", { }, ",");
				for (size_t i=0; i<map_str.size(); ++i) {
					std::vector<std::string> v;
					boost::algorithm::iter_split(v, map_str[i], boost::algorithm::first_finder("->"));
					if (v.size() != 2)
						throw std::runtime_error("Unable to parse the 'exptimes' parameter");
					try {
						exptimes_map[boost::lexical_cast<float>(boost::trim_copy(v[0]))] = boost::lexical_cast<float>(boost::trim_copy(v[1]));
					} catch (const boost::bad_lexical_cast &) {
						throw std::runtime_error("Unable to parse the 'exptimes' argument!");
					}
				}
			}
		}
		es.load();

		/// Precompute relative exposure + weight tables
		float saturation = 0;
		if (vm.count("saturation"))
			saturation = vm["saturation"].as<float>();
		es.initTables(saturation);

		if (!exptimes.empty()) {
			cout << "Overriding exposure times: [";

			for (size_t i=0; i<exptimes.size(); ++i) {
				cout << es.exposures[i].toString() << "->" << exptimes[i];
				es.exposures[i].exposure = exptimes[i];
				if (i+1 < exptimes.size())
					cout << ", ";
			}
			cout << "]" << endl;
		}

		if (!exptimes_map.empty()) {
			cout << "Overriding exposure times: [";
			for (size_t i=0; i<es.exposures.size(); ++i) {
				float from = es.exposures[i].exposure, to = 0;
				for (std::map<float, float>::const_iterator it = exptimes_map.begin(); it != exptimes_map.end(); ++it) {
					if (std::abs((it->first - from) / from) < 1e-5f) {
						if (to != 0)
							throw std::runtime_error("Internal error!");
						to = it->second;
					}
				}
				if (to == 0)
					throw std::runtime_error((boost::format("Specified an exposure time replacement map, but couldn't find an entry for %1%") % from).str());

				cout << es.exposures[i].toString() << "->" << to;
				if (i+1 < es.exposures.size())
					cout << ", ";
				es.exposures[i].exposure = to;
			}
			cout << "]" << endl;
		}


		if (vm.count("fitexptimes")) {
			es.fitExposureTimes();
			if (vm.count("exptimes"))
				cerr << "Note: you specified --exptimes and --fitexptimes at the same time. The" << endl
				     << "The test file exptime_showfit.m now compares these two sets of exposure" << endl
					 << "times, rather than the fit vs EXIF." << endl << endl;
		}

		/// Step 1: HDR merge
		es.merge();

		/// Step 3: Demosaicing
		bool demosaic = vm.count("nodemosaic") == 0;
		if (demosaic)
			es.demosaic(sensor2xyz);

		/// Step 4: Transform colors
		if (colormode != ENative) {
			if (!demosaic) {
				cerr << "Warning: you requested XYZ/sRGB output, but demosaicing was explicitly disabled! " << endl
					 << "Color processing is not supported in this case -- writing raw sensor colors instead." << endl;
			} else {
				es.transform_color(sensor2xyz, colormode == EXYZ);
			}
		}

		/// Step 5: White balancing
		if (!wbal.empty()) {
			float scale[3] = { wbal[0], wbal[1], wbal[2] };
			es.whitebalance(scale);
		} else if (wbalpatch.size()) {
			es.whitebalance(wbalpatch[0], wbalpatch[1], wbalpatch[2], wbalpatch[3]);
		}

		/// Step 6: Scale
		if (scale != 1.0f)
			es.scale(scale);

		/// Step 7: Remove vignetting
		if (vm.count("vcal")) {
			if (vm.count("vcorr")) {
				cerr << "Warning: only one of --vcal and --vcorr can be specified at a time. Ignoring --vcorr" << endl;
			}

			if (demosaic)
				es.vcal();
			else
				cerr << "Warning: Vignetting correction requires demosaicing. Ignoring.." << endl;
		} else if (!vcorr.empty()) {
			if (demosaic)
				es.vcorr(vcorr[0], vcorr[1], vcorr[2]);
			else
				cerr << "Warning: Vignetting correction requires demosaicing. Ignoring.." << endl;
		}

		/// Step 8: Crop
		if (!crop.empty())
			es.crop(crop[0], crop[1], crop[2], crop[3]);

		/// Step 9: Resample
		if (!resample.empty()) {
			int w, h;

			if (resample.size() == 1) {
				float factor = resample[0] / (float) std::max(es.width, es.height);
				w = (int) std::round(factor * es.width);
				h = (int) std::round(factor * es.height);
			} else {
				w = resample[0];
				h = resample[1];
			}

			if (demosaic) {
				std::string rfilter = boost::to_lower_copy(vm["rfilter"].as<std::string>());
				if (rfilter == "lanczos") {
					es.resample(LanczosSincFilter(), w, h);
				} else if (rfilter == "tent") {
					es.resample(TentFilter(), w, h);
				} else {
					cout << "Invalid resampling filter chosen (must be 'lanczos' / 'tent')" << endl;
					return -1;
				}
			} else {
				cout << "Warning: resampling a non-demosaiced image does not make much sense -- ignoring." << endl;
			}
		}

		/// Step 10: Flip / rotate
		ERotateFlipType flipType = flipTypeFromString(
			vm["rotate"].as<int>(), vm["flip"].as<std::string>());

		if (flipType != ERotateNoneFlipNone) {
			uint8_t *t_buf;
			size_t t_width, t_height;

			if (demosaic) {
				rotateFlip((uint8_t *) es.image_demosaiced, es.width, es.height,
					t_buf, t_width, t_height, 3*sizeof(float), flipType);
				delete[] es.image_demosaiced;
				es.image_demosaiced = (float3 *) t_buf;
				es.width = t_width;
				es.height = t_height;
			}
		}

		/// Step 11: Write output
		std::string output = vm["output"].as<std::string>();
		std::string format = boost::to_lower_copy(vm["format"].as<std::string>());

		if (vm["output"].defaulted() && exposures.size() == 1 && exposures[0].find("%") == std::string::npos) {
			std::string fname = exposures[0];
			size_t spos = fname.find_last_of(".");
			if (spos != std::string::npos)
				output = fname.substr(0, spos) + ".exr";
		}

		if (format == "jpg")
			format = "jpeg";

		if (format == "jpeg" && boost::ends_with(output,  ".exr"))
			output = output.substr(0, output.length()-4) + ".jpg";

		if (demosaic) {
			if (format == "half" || format == "single")
				writeOpenEXR(output, es.width, es.height, 3,
					(float *) es.image_demosaiced, es.metadata, format == "half");
			else if (format == "jpeg")
				writeJPEG(output, es.width, es.height, (float *) es.image_demosaiced);
			else
				throw std::runtime_error("Unsupported --format argument");
		} else {
			if (format == "half" || format == "single")
				writeOpenEXR(output, es.width, es.height, 1,
					(float *) es.image_merged, es.metadata, format == "half");
			else if (format == "jpeg")
				throw std::runtime_error("Tried to export the raw Bayer grid "
					"as a JPEG image -- this is not allowed.");
			else
				throw std::runtime_error("Unsupported --format argument");
		}
	} catch (const std::exception &ex) {
		cerr << "Encountered a fatal error: " << ex.what() << endl;
		return -1;
	}

	return 0;
}
Exemplo n.º 12
0
        static int main(int argc, char *argv[]) {

            const string DEFAULT_TITLE      = "Sequence Coverage Plot";
            const string DEFAULT_X_LABEL    = "X";
            const string DEFAULT_Y_LABEL    = "Y";
            const int32_t DEFAULT_X_MAX     = 1000;
            const int32_t DEFAULT_Y_MAX     = 1000;
            const uint32_t DEFAULT_FASTA_INDEX = 0;
        
            path        sect_file;           
            string      output_type;
            path        output;
            string      title;
            string      x_label;
            string      y_label;
            uint16_t    width;
            uint16_t    height;
            uint32_t    x_max;
            uint32_t    y_max;
            bool        y_logscale;
            uint32_t    fasta_index;
            string      fasta_header;
            bool        verbose;
            bool        help;

            // Declare the supported options.
            po::options_description generic_options(PlotProfile::helpMessage(), 100);
            generic_options.add_options()
                    ("output_type,p", po::value<string>(&output_type)->default_value("png"), 
                        "The plot file type to create: png, ps, pdf.  Warning... if pdf is selected please ensure your gnuplot installation can export pdf files.")
                    ("output,o", po::value<path>(&output),
                        "The path to the output file")
                    ("title,t", po::value<string>(&title)->default_value(DEFAULT_TITLE),
                        "Title for plot")
                    ("x_label,a", po::value<string>(&x_label)->default_value(DEFAULT_X_LABEL),
                        "Label for the x-axis (value taken from matrix metadata if present)")
                    ("y_label,b", po::value<string>(&y_label)->default_value(DEFAULT_Y_LABEL),
                        "Label for the y-axis (value taken from matrix metadata if present)")
                    ("x_max,x", po::value<uint32_t>(&x_max)->default_value(DEFAULT_X_MAX),
                        "Maximum value for the x-axis (value taken from matrix metadata if present)")
                    ("y_max,y", po::value<uint32_t>(&y_max)->default_value(DEFAULT_Y_MAX),
                        "Maximum value for the y-axis (value taken from matrix metadata if present)")
                    ("width,w", po::value<uint16_t>(&width)->default_value(1024),
                        "Width of canvas")
                    ("height,h", po::value<uint16_t>(&height)->default_value(1024),
                        "Height of canvas")
                    ("index,n", po::value<uint32_t>(&fasta_index)->default_value(DEFAULT_FASTA_INDEX),
                        "Index of fasta entry to plot.  First entry is 1.")
                    ("header,d", po::value<string>(&fasta_header),
                        "Fasta header of fasta entry to plot.  NOTE: \'--header\' has priority over index")
                    ("y_logscale,m", po::bool_switch(&y_logscale)->default_value(false),
                        "Y-axis is logscale.  This overrides the y_min and y_max limits.")
                    ("verbose,v", po::bool_switch(&verbose)->default_value(false), 
                        "Print extra information.")
                    ("help", po::bool_switch(&help)->default_value(false), "Produce help message.")
                    ;

            // Hidden options, will be allowed both on command line and
            // in config file, but will not be shown to the user.
            po::options_description hidden_options("Hidden options");
            hidden_options.add_options()
                    ("sect_file,s", po::value<path>(&sect_file), "Path to the sect profile file to plot.")                    
                    ;

            // Positional option for the input bam file
            po::positional_options_description p;
            p.add("sect_file", 1);


            // Combine non-positional options
            po::options_description cmdline_options;
            cmdline_options.add(generic_options).add(hidden_options);

            // Parse command line
            po::variables_map vm;
            po::store(po::command_line_parser(argc, argv).options(cmdline_options).positional(p).run(), vm);
            po::notify(vm);

            // Output help information the exit if requested
            if (help || argc <= 1) {
                cout << generic_options << endl;
                return 1;
            }

            // Check input file exists
            if (!bfs::exists(sect_file) && !bfs::symbolic_link_exists(sect_file)) {
                cerr << endl << "Could not find matrix file at: " << sect_file << "; please check the path and try again." << endl << endl;
                return 1;
            }

            string header;
            string coverages;

            if (!fasta_header.empty()) {
                header.assign(fasta_header);
                getEntryFromFasta(sect_file, header, coverages);
            }
            else if (fasta_index > 0) {
                getEntryFromFasta(sect_file, fasta_index, header, coverages);
            }

            if (coverages.length() == 0) {
                cerr << "Could not find requested fasta header in sect coverages fasta file" << endl;
            }
            else {
                if (verbose)
                    cerr << "Found requested sequence : " << header << endl << coverages << endl << endl;

                // Split coverages
                vector<uint32_t> cvs = kat::splitUInt32(coverages, ' ');

                uint32_t maxCvgVal = y_max != DEFAULT_Y_MAX ? y_max : (*(std::max_element(cvs.begin(), cvs.end())) + 1);

                string t = autoTitle(title, header);

                if (verbose)
                    cerr << "Acquired K-mer counts" << endl;

                // Initialise gnuplot
                Gnuplot profile_plot = Gnuplot("lines");

                profile_plot.configurePlot(output_type, output.string(), width, height);

                profile_plot.set_title(t);
                profile_plot.set_xlabel(x_label);
                profile_plot.set_ylabel(y_label);
                profile_plot.set_xrange(0, cvs.size());
                profile_plot.set_yrange(0, maxCvgVal);
                
                profile_plot.cmd("set style data linespoints");

                std::ostringstream data_str;

                for(uint32_t i = 0; i < cvs.size(); i++)
                {
                    uint32_t index = i+1;
                    double val = y_logscale ? (double)std::log(cvs[i]) : (double)cvs[i];
                    data_str << index << " " << val << "\n";
                }

                std::ostringstream plot_str;

                plot_str << "plot '-'\n" << data_str.str() << "e\n";

                profile_plot.cmd(plot_str.str());

                if (verbose)
                    cerr << "Plotted data: " << plot_str.str() << endl;
            }

            return 0;
        }
Exemplo n.º 13
0
int main(int argc, char const * argv[])
{
  try {

    _note_c("dbg/main", "Begin of main()");
    // TODO parse the debug options like set log level right here at start

    tools::sanitize_locale();

    epee::string_tools::set_module_name_and_folder(argv[0]);

    // Build argument description
    po::options_description all_options("All");
    po::options_description hidden_options("Hidden");
    po::options_description visible_options("Options");
    po::options_description core_settings("Settings");
    po::positional_options_description positional_options;
    {
      bf::path default_data_dir = daemonizer::get_default_data_dir();
      bf::path default_testnet_data_dir = {default_data_dir / "testnet"};

      // Misc Options

      command_line::add_arg(visible_options, command_line::arg_help);
      command_line::add_arg(visible_options, command_line::arg_version);
      command_line::add_arg(visible_options, daemon_args::arg_os_version);
      bf::path default_conf = default_data_dir / std::string(CRYPTONOTE_NAME ".conf");
      command_line::add_arg(visible_options, daemon_args::arg_config_file, default_conf.string());
      command_line::add_arg(visible_options, command_line::arg_test_dbg_lock_sleep);
      cryptonote::core::init_options(core_settings);

      // Settings
      bf::path default_log = default_data_dir / std::string(CRYPTONOTE_NAME ".log");
      command_line::add_arg(core_settings, daemon_args::arg_log_file, default_log.string());
      command_line::add_arg(core_settings, daemon_args::arg_log_level);

      daemonizer::init_options(hidden_options, visible_options);
      daemonize::t_executor::init_options(core_settings);

      // Hidden options
      command_line::add_arg(hidden_options, daemon_args::arg_command);

      visible_options.add(core_settings);
      all_options.add(visible_options);
      all_options.add(hidden_options);

      // Positional
      positional_options.add(daemon_args::arg_command.name, -1); // -1 for unlimited arguments
    }

    // Do command line parsing
    po::variables_map vm;
    bool ok = command_line::handle_error_helper(visible_options, [&]()
    {
      boost::program_options::store(
        boost::program_options::command_line_parser(argc, argv)
          .options(all_options).positional(positional_options).run()
      , vm
      );

      return true;
    });
    if (!ok) return 1;

    if (command_line::get_arg(vm, command_line::arg_help))
    {
      std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL;
      std::cout << "Usage: " + std::string{argv[0]} + " [options|settings] [daemon_command...]" << std::endl << std::endl;
      std::cout << visible_options << std::endl;
      return 0;
    }

    // Monero Version
    if (command_line::get_arg(vm, command_line::arg_version))
    {
      std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL;
      return 0;
    }

    // OS
    if (command_line::get_arg(vm, daemon_args::arg_os_version))
    {
      std::cout << "OS: " << tools::get_os_version_string() << ENDL;
      return 0;
    }

    epee::g_test_dbg_lock_sleep = command_line::get_arg(vm, command_line::arg_test_dbg_lock_sleep);

    std::string db_type = command_line::get_arg(vm, command_line::arg_db_type);

    // verify that blockchaindb type is valid
    if(cryptonote::blockchain_db_types.count(db_type) == 0)
    {
      std::cout << "Invalid database type (" << db_type << "), available types are:" << std::endl;
      for (const auto& type : cryptonote::blockchain_db_types)
      {
        std::cout << "\t" << type << std::endl;
      }
      return 0;
    }

    bool testnet_mode = command_line::get_arg(vm, command_line::arg_testnet_on);

    auto data_dir_arg = testnet_mode ? command_line::arg_testnet_data_dir : command_line::arg_data_dir;

    // data_dir
    //   default: e.g. ~/.bitmonero/ or ~/.bitmonero/testnet
    //   if data-dir argument given:
    //     absolute path
    //     relative path: relative to cwd

    // Create data dir if it doesn't exist
    boost::filesystem::path data_dir = boost::filesystem::absolute(
        command_line::get_arg(vm, data_dir_arg));
    tools::create_directories_if_necessary(data_dir.string());

    // FIXME: not sure on windows implementation default, needs further review
    //bf::path relative_path_base = daemonizer::get_relative_path_base(vm);
    bf::path relative_path_base = data_dir;

    std::string config = command_line::get_arg(vm, daemon_args::arg_config_file);

    boost::filesystem::path data_dir_path(data_dir);
    boost::filesystem::path config_path(config);
    if (!config_path.has_parent_path())
    {
      config_path = data_dir / config_path;
    }

    boost::system::error_code ec;
    if (bf::exists(config_path, ec))
    {
      po::store(po::parse_config_file<char>(config_path.string<std::string>().c_str(), core_settings), vm);
    }
    po::notify(vm);

    // If there are positional options, we're running a daemon command
    {
      auto command = command_line::get_arg(vm, daemon_args::arg_command);

      if (command.size())
      {
        auto rpc_ip_str = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_rpc_bind_ip);
        auto rpc_port_str = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_rpc_bind_port);
        if (testnet_mode)
        {
          rpc_port_str = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_testnet_rpc_bind_port);
        }

        uint32_t rpc_ip;
        uint16_t rpc_port;
        if (!epee::string_tools::get_ip_int32_from_string(rpc_ip, rpc_ip_str))
        {
          std::cerr << "Invalid IP: " << rpc_ip_str << std::endl;
          return 1;
        }
        if (!epee::string_tools::get_xtype_from_string(rpc_port, rpc_port_str))
        {
          std::cerr << "Invalid port: " << rpc_port_str << std::endl;
          return 1;
        }

        daemonize::t_command_server rpc_commands{rpc_ip, rpc_port};
        if (rpc_commands.process_command_vec(command))
        {
          return 0;
        }
        else
        {
          std::cerr << "Unknown command" << std::endl;
          return 1;
        }
      }
    }

    // Start with log level 0
    epee::log_space::get_set_log_detalisation_level(true, LOG_LEVEL_0);

    // Set log level
    {
      int new_log_level = command_line::get_arg(vm, daemon_args::arg_log_level);
      if(new_log_level < LOG_LEVEL_MIN || new_log_level > LOG_LEVEL_MAX)
      {
        LOG_PRINT_L0("Wrong log level value: " << new_log_level);
      }
      else if (epee::log_space::get_set_log_detalisation_level(false) != new_log_level)
      {
        epee::log_space::get_set_log_detalisation_level(true, new_log_level);
        int otshell_utils_log_level = 100 - (new_log_level * 20);
        gCurrentLogger.setDebugLevel(otshell_utils_log_level);
        LOG_PRINT_L0("LOG_LEVEL set to " << new_log_level);
      }
    }

    // log_file_path
    //   default: <data_dir>/<CRYPTONOTE_NAME>.log
    //   if log-file argument given:
    //     absolute path
    //     relative path: relative to data_dir

    // Set log file
    {
      bf::path log_file_path {data_dir / std::string(CRYPTONOTE_NAME ".log")};
      if (! vm["log-file"].defaulted())
        log_file_path = command_line::get_arg(vm, daemon_args::arg_log_file);
      log_file_path = bf::absolute(log_file_path, relative_path_base);

      epee::log_space::log_singletone::add_logger(
          LOGGER_FILE
        , log_file_path.filename().string().c_str()
        , log_file_path.parent_path().string().c_str()
        );
    }

    _note_c("dbg/main", "Moving from main() into the daemonize now.");

    return daemonizer::daemonize(argc, argv, daemonize::t_executor{}, vm);
  }
  catch (std::exception const & ex)
  {
    LOG_ERROR("Exception in main! " << ex.what());
  }
  catch (...)
  {
    LOG_ERROR("Exception in main!");
  }
  return 1;
}