45#include "InternalErr.h"
46#include "XDRStreamMarshaller.h"
51#include "D4Attributes.h"
63void Grid::m_duplicate(
const Grid &s) { d_is_array_set = s.d_is_array_set; }
74Grid::Grid(
const string &n) : Constructor(n, dods_grid_c), d_is_array_set(false) {}
87Grid::Grid(
const string &n,
const string &d) : Constructor(n, d, dods_grid_c), d_is_array_set(false) {}
98Grid &Grid::operator=(
const Grid &rhs) {
101 Constructor::operator=(rhs);
110 DBG(cerr << __func__ <<
"() - BEGIN (name:" <<
name() <<
")(type:" <<
type_name() <<
")(root:'" << root->
name()
111 <<
"':" << (
void *)root <<
")(container:'" << container->
name() <<
"':" << (
void *)container <<
")"
114 vector<Array *> d4_map_arrays;
123 DBG(cerr << __func__ <<
"() - Processing Map Array: '" << (*i)->name() <<
"' (" << (
void *)(*i) <<
")"
130 Array *the_map_array;
131 Array *container_map_array =
static_cast<Array *
>(container->
var((*i)->name()));
132 if (!container_map_array) {
133 DBG(cerr << __func__ <<
"() - No Map Array '" << (*i)->name() <<
"' present in the current DAP4 container ("
134 << container->
name() <<
":" << (
void *)container <<
"). Let's fix that..." << endl;);
136 Array *root_map_array =
static_cast<Array *
>(root->
var((*i)->name()));
137 if (!root_map_array) {
139 DBG(cerr << __func__ <<
"() - No Map Array '" << (*i)->name() <<
"' present in the root Group ("
140 << root->
name() <<
":" << (
void *)root <<
"). Let's fix that..." << endl;);
142 (*i)->transform_to_dap4(root, container);
144 the_map_array =
static_cast<Array *
>(container->
var((*i)->name()));
145 DBG(cerr << __func__ <<
"() - Transformed array '" << the_map_array->
name() <<
"' to DAP4 Array ("
146 << (
void *)the_map_array <<
") added to container: '" << container->
name() <<
"'" << endl;);
148 the_map_array = root_map_array;
149 DBG(cerr << __func__ <<
"() - Located Map Array '" << the_map_array->
name() <<
"' ("
150 << (
void *)the_map_array <<
") present in the root group (" << root->
name() <<
":"
151 << (
void *)root <<
")" << endl;);
154 the_map_array = container_map_array;
155 DBG(cerr << __func__ <<
"() - Located Map Array '" << the_map_array->
name() <<
"' ("
156 << (
void *)the_map_array <<
") present in the current DAP4 container (" << container->
name() <<
":"
157 << (
void *)container <<
")" << endl;);
160 d4_map_arrays.push_back(the_map_array);
168 DBG(cerr << __func__ <<
"() - Transformed and added DAP4 coverage Array '" << coverage->
name()
169 <<
"' to parent container: '" << container->
name() <<
"'" << endl;);
173 DBG(cerr << __func__ <<
"() - " <<
"Coverage Array '" << coverage->
name() <<
"' attributes: " << endl;
179 for (; d4aItr != end; d4aItr++) {
180 Array *the_map_array = *d4aItr;
186 D4Map *d4_map =
new D4Map(the_map_array->
FQN(), the_map_array);
187 coverage->maps()->
add_map(d4_map);
191 DBG(cerr << __func__ <<
"() - Added DAP4 Map Array: '" << d4_map->name() <<
"' (" << (
void *)d4_map->
array()
192 <<
") to coverage: '" << coverage->
name() <<
"'" << endl;);
194 DBG(cerr << __func__ <<
"() - END (grid:" <<
name() <<
")" << endl;);
218 throw InternalErr(__FILE__, __LINE__,
"Passing NULL pointer as variable to be added.");
221 throw Error(
string(
"A method usable only with DAP2 variables was called on a DAP4 variable (")
226 if (part == array && d_is_array_set ) {
229 "Error: Grid::add_var called with part==Array, but the array was already set!");
233 if (!
dynamic_cast<Array *
>(bt)) {
234 throw InternalErr(__FILE__, __LINE__,
"Grid::add_var(): object is not an Array!");
251 d_vars.push_back(bt_clone);
255 if (!d_is_array_set ) {
262 d_vars.push_back(bt_clone);
285 throw InternalErr(__FILE__, __LINE__,
"Passing NULL pointer as variable to be added.");
288 throw Error(
string(
"A method usable only with DAP2 variables was called on a DAP4 variable (")
293 if (part == array && d_is_array_set ) {
296 "Error: Grid::add_var called with part==Array, but the array was already set!");
300 if (!
dynamic_cast<Array *
>(bt)) {
301 throw InternalErr(__FILE__, __LINE__,
"Grid::add_var(): object is not an Array!");
316 d_vars.push_back(bt);
320 if (!d_is_array_set ) {
325 d_vars.push_back(bt);
346 throw InternalErr(__FILE__, __LINE__,
"Grid::set_array(): Cannot set to null!");
360 if (d_vars.size() == 0) {
361 d_vars.push_back(p_new_arr);
362 }
else if (!d_is_array_set ) {
363 d_vars.insert(d_vars.begin(), p_new_arr);
367 d_vars[0] = p_new_arr;
370 d_is_array_set =
true;
373 d_array_var = p_new_arr;
408 throw InternalErr(__FILE__, __LINE__,
"Grid::add_map(): cannot have p_new_map null!");
415 d_vars.push_back(p_new_map);
460 return d_is_array_set ? d_vars.begin() + 1 : d_vars.begin();
470 return d_vars.rbegin();
475Grid::Map_riter
Grid::map_rend() {
return d_is_array_set ? d_vars.rend() - 1 : d_vars.rend(); }
480Grid::Map_iter
Grid::get_map_iter(
int i) {
return d_is_array_set ? d_vars.begin() + 1 + i : d_vars.begin() + i; }
504 if ((*i)->send_p()) {
509 comp = d_vars.size();
521 DBG(cerr << __func__ <<
"() - BEGIN " <<
type_name() <<
" " <<
name()
522 <<
" (at_container:" << at_container->
get_name() <<
":" << (
void *)at_container <<
")" << endl;);
527 DBG(cerr << __func__ <<
"() - Found AttrTable (" << at->
get_name() <<
":" << (
void *)at <<
")" << endl;);
528 at->set_is_global_attribute(
false);
546 dvat->set_is_global_attribute(
false);
551 (*map)->transfer_attributes(at);
560 if (at->is_global_attribute(at_p)) {
561 DBG(cerr << __func__ <<
"() - " <<
"Adding unclaimed Attribute (" << at->
get_type(at_p) <<
":"
562 << at->
get_name(at_p) <<
":" << (
void *)(*map) <<
") from AttrTable (" << at->
get_name() <<
":"
563 << (
void *)at <<
")" <<
" to the variable " <<
type_name() <<
" " <<
name() << endl;);
569 (*at_p)->is_utf8_str);
575 DBG(cerr << __func__ <<
"() - No AttrTable named '" <<
name() <<
"' was found in at_container ("
576 << at_container->
get_name() <<
":" << (
void *)at <<
")" << endl;);
578 DBG(cerr << __func__ <<
"() - END " <<
type_name() <<
" " <<
name() <<
" (at_container:" << at_container->
get_name()
579 <<
":" << (
void *)at_container <<
")" << endl;);
628 valid = map.dimension_start(fd,
true) == a->
dimension_start(d,
true) &&
648void Grid::print_decl(FILE *out,
string space,
bool print_semi,
bool constraint_info,
bool constrained) {
650 print_decl(oss, space, print_semi, constraint_info, constrained);
651 fwrite(oss.str().data(),
sizeof(
char), oss.str().length(), out);
654void Grid::print_decl(ostream &out,
string space,
bool print_semi,
bool constraint_info,
bool constrained) {
655 if (constrained && !
send_p())
660 out << space <<
"Structure {\n";
665 (*i)->print_decl(out, space +
" ",
true, constraint_info, constrained);
674 out << space <<
" Array:\n";
677 out << space <<
" Maps:\n";
679 (*i)->print_decl(out, space +
" ",
true, constraint_info, constrained);
685 if (constraint_info) {
687 out <<
": Send True";
689 out <<
": Send False";
704 fwrite(xml.get_doc(),
sizeof(
char), xml.get_doc_size(), out);
713 out << xml.get_doc();
716class PrintGridFieldXMLWriter :
public unary_function<BaseType *, void> {
722 PrintGridFieldXMLWriter(
XMLWriter &x,
bool c,
const string &t =
"Map") : d_xml(x), d_constrained(c), d_tag(t) {}
724 void operator()(BaseType *btp) {
725 Array *a =
dynamic_cast<Array *
>(btp);
727 throw InternalErr(__FILE__, __LINE__,
"Expected an Array.");
728 a->print_xml_writer_core(d_xml, d_constrained, d_tag);
733 if (constrained && !
send_p())
737 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar *)
"Structure") < 0)
738 throw InternalErr(__FILE__, __LINE__,
"Could not write Structure element");
741 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar *)
"name",
742 (
const xmlChar *)
name().c_str()) < 0)
743 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
749 for_each(
map_begin(),
map_end(), PrintGridFieldXMLWriter(xml, constrained,
"Array"));
751 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
752 throw InternalErr(__FILE__, __LINE__,
"Could not end Structure element");
756 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar *)
"Grid") < 0)
757 throw InternalErr(__FILE__, __LINE__,
"Could not write Grid element");
760 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar *)
"name",
761 (
const xmlChar *)
name().c_str()) < 0)
762 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
768 for_each(
map_begin(),
map_end(), PrintGridFieldXMLWriter(xml, constrained,
"Map"));
770 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
771 throw InternalErr(__FILE__, __LINE__,
"Could not end Grid element");
778 fwrite(oss.str().data(),
sizeof(
char), oss.str().length(), out);
803 (*i)->print_val(out,
"",
false);
825 msg +=
"Null grid base array in `" +
name() +
"'\n";
831 msg +=
"Grid `" +
name() +
"'s' member `" +
get_array()->
name() +
"' must be an array\n";
839 msg +=
"The field variable `" + this->
name() +
840 "' must be an array of simple type elements (e.g., int32, String)\n";
845 if ((
unsigned)d_vars.size() - 1 != av->
dimensions()) {
847 "The number of map variables for grid `" + this->
name() +
"' does not match the number of dimensions of `";
848 msg += av->
name() +
"'\n";
852 const string array_var_name = av->
name();
859 if (array_var_name == mv->
name()) {
861 "Grid map variable `" + mv->
name() +
"' conflicts with the grid array name in grid `" +
name() +
"'\n";
865 if (mv->
type() != dods_array_c) {
866 msg +=
"Grid map variable `" + mv->
name() +
"' is not an array\n";
874 msg +=
"The field variable `" + this->
name() +
875 "' must be an array of simple type elements (e.g., int32, String)\n";
881 msg +=
"Grid map variable `" + mv_a->
name() +
"' must be only one dimension\n";
888 if (mv_a_size != av_size) {
889 msg +=
"Grid map variable `" + mv_a->
name() +
"'s' size does not match the size of array variable '";
890 msg +=
get_array()->
name() +
"'s' cooresponding dimension\n";
899 if (!(*mvi)->check_semantics(msg,
true)) {
917 strm << DapIndent::LMarg <<
"Grid::dump - (" << (
void *)
this <<
")" << endl;
921 DapIndent::UnIndent();
A multidimensional array of identical data types.
virtual int dimension_start(Dim_iter i, bool constrained=false)
Return the start index of a dimension.
virtual void clear_constraint()
Clears the projection; add each projected dimension explicitly using add_constraint.
virtual int dimension_stop(Dim_iter i, bool constrained=false)
Return the stop index of the constraint.
void print_decl(ostream &out, string space=" ", bool print_semi=true, bool constraint_info=false, bool constrained=false) override
Prints a DDS entry for the Array.
void print_val(ostream &out, string space="", bool print_decl_p=true) override
Prints the value of the variable.
BaseType * ptr_duplicate() override
std::vector< dimension >::iterator Dim_iter
virtual int dimension_size(Dim_iter i, bool constrained=false)
Returns the size of the dimension.
void print_xml_writer(XMLWriter &xml, bool constrained=false) override
virtual unsigned int dimensions(bool constrained=false)
Return the total number of dimensions in the array.
virtual int dimension_stride(Dim_iter i, bool constrained=false)
Returns the stride value of the constraint.
Contains the attributes for a dataset.
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
virtual Attr_iter attr_end()
virtual string get_type(const string &name)
Get the type name of an attribute within this attribute table.
virtual vector< string > * get_attr_vector(const string &name)
Get a vector-valued attribute.
virtual unsigned int append_attr(const string &name, const string &type, const string &value)
Add an attribute to the table.
virtual Attr_iter attr_begin()
void print_dap4(XMLWriter &xml)
virtual string get_name() const
Get the name of this attribute table.
void print_xml_writer(XMLWriter &xml)
virtual AttrType get_attr_type(const string &name)
Get the type of an attribute.
The basic data type for the DODS DAP types.
virtual string type_name() const
Returns the type of the class instance as a string.
virtual AttrTable & get_attr_table()
virtual string name() const
Returns the name of the class instance.
virtual void set_parent(BaseType *parent)
virtual D4Attributes * attributes()
virtual std::string FQN() const
virtual bool send_p()
Should this variable be sent?
virtual bool is_simple_type() const
Returns true if the instance is a numeric, string or URL type variable.
virtual BaseType * ptr_duplicate()=0
virtual void transform_to_dap4(D4Group *root, Constructor *container)
DAP2 to DAP4 transform.
virtual void transfer_attributes(AttrTable *at)
virtual bool check_semantics(string &msg, bool all=false)
Compare an object's current state with the semantics of its type.
virtual Type type() const
Returns the type of the class instance.
BaseType * var(const string &name, bool exact_match=true, btp_stack *s=nullptr) override
btp_stack no longer needed; use back pointers (BaseType::get_parent())
void dump(ostream &strm) const override
dumps information about this object
void transform_to_dap4(AttrTable &at)
copy attributes from DAP2 to DAP4
Array * array(D4Group *root)
This will always return the correct pointer for a valid data set.
A class for error processing.
Holds the Grid data type.
virtual BaseType * ptr_duplicate()
BaseType * array_var()
Returns the Grid Array.
virtual void transform_to_dap4(D4Group *root, Constructor *container)
DAP2 to DAP4 transform.
virtual void print_xml(ostream &out, string space=" ", bool constrained=false)
Map_iter map_begin()
Returns an iterator referencing the first Map vector.
Map_iter get_map_iter(int i)
virtual void set_array(Array *p_new_arr)
virtual void clear_constraint()
virtual void print_val(ostream &out, string space="", bool print_decl_p=true)
Prints the value of the variable.
virtual void print_decl(ostream &out, string space=" ", bool print_semi=true, bool constraint_info=false, bool constrained=false)
Print an ASCII representation of the variable structure.
virtual Array * prepend_map(Array *p_new_map, bool add_copy)
Grid(const string &n)
The Grid constructor.
Array * get_array()
Returns the Grid Array. This method returns the array using an Array*, so no cast is required.
virtual void transfer_attributes(AttrTable *at_container)
virtual bool projection_yields_grid()
virtual void dump(ostream &strm) const
dumps information about this object
virtual Array * add_map(Array *p_new_map, bool add_copy)
virtual int components(bool constrained=false)
Returns the number of components in the Grid object.
virtual void print_xml_writer(XMLWriter &xml, bool constrained=false)
virtual bool check_semantics(string &msg, bool all=false)
Return true if this Grid is well formed.
virtual void add_var(BaseType *bt, Part part)
virtual void add_var_nocopy(BaseType *bt, Part part)
Map_riter map_rbegin()
Returns an iterator referencing the first Map vector.
virtual bool is_dap2_only_type()
A class for software fault reporting.
BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=nullptr) override
top level DAP object to house generic methods
Part
Names the parts of multi-section constructor data types.
string id2www(string in, const string &allowable)