39 #include <dods-datatypes.h>
41 #include <InternalErr.h>
57 GridGeoConstraint::GridGeoConstraint(Grid *grid)
58 :
GeoConstraint(), d_grid(grid), d_latitude(0), d_longitude(0)
60 if (d_grid->get_array()->dimensions() < 2
61 || d_grid->get_array()->dimensions() > 3)
62 throw Error(
"The geogrid() function works only with Grids of two or three dimensions.");
65 if (!build_lat_lon_maps())
66 throw Error(
string(
"The grid '") + d_grid->name()
67 +
"' does not have identifiable latitude/longitude map vectors.");
69 if (!lat_lon_dimensions_ok())
70 throw Error(
"The geogrid() function will only work when the Grid's Longitude and Latitude maps are the rightmost dimensions (grid: " + grid->name() +
", 1).");
74 :
GeoConstraint(), d_grid(grid), d_latitude(0), d_longitude(0)
76 if (d_grid->get_array()->dimensions() < 2
77 || d_grid->get_array()->dimensions() > 3)
78 throw Error(
"The geogrid() function works only with Grids of two or three dimensions.");
81 if (!build_lat_lon_maps(lat, lon))
82 throw Error(
string(
"The grid '") + d_grid->name()
83 +
"' does not have valid latitude/longitude map vectors.");
86 if (!lat_lon_dimensions_ok())
87 throw Error(
"The geogrid() function will only work when the Grid's Longitude and Latitude maps are the rightmost dimensions (grid: " + grid->name() +
", 2).");
105 bool GridGeoConstraint::build_lat_lon_maps()
107 Grid::Map_iter m = d_grid->map_begin();
112 Array::Dim_iter d = d_grid->get_array()->dim_begin();
119 while (m != d_grid->map_end() && (!d_latitude || !d_longitude)) {
120 string units_value = (*m)->get_attr_table().get_attr(
"units");
121 units_value = remove_quotes(units_value);
122 string map_name = (*m)->name();
128 units_value, map_name)) {
136 d_latitude = dynamic_cast < Array * >(*m);
138 throw InternalErr(__FILE__, __LINE__,
"Expected an array.");
139 if (!d_latitude->read_p())
142 set_lat(extract_double_array(d_latitude));
150 units_value, map_name)) {
152 d_longitude = dynamic_cast < Array * >(*m);
154 throw InternalErr(__FILE__, __LINE__,
"Expected an array.");
155 if (!d_longitude->read_p())
158 set_lon(extract_double_array(d_longitude));
163 if (m + 1 == d_grid->map_end())
181 bool GridGeoConstraint::build_lat_lon_maps(Array *lat, Array *lon)
183 Grid::Map_iter m = d_grid->map_begin();
185 Array::Dim_iter d = d_grid->get_array()->dim_begin();
187 while (m != d_grid->map_end() && (!d_latitude || !d_longitude)) {
189 if (!d_latitude && *m == lat) {
193 if (!d_latitude->read_p())
196 set_lat(extract_double_array(d_latitude));
202 if (!d_longitude && *m == lon) {
206 if (!d_longitude->read_p())
209 set_lon(extract_double_array(d_longitude));
214 if (m + 1 == d_grid->map_end())
236 GridGeoConstraint::lat_lon_dimensions_ok()
239 Grid::Map_riter rightmost = d_grid->map_rbegin();
240 Grid::Map_riter next_rightmost = rightmost + 1;
242 if (*rightmost == d_longitude && *next_rightmost == d_latitude)
244 else if (*rightmost == d_latitude && *next_rightmost == d_longitude)
276 throw InternalErr(
"The Latitude and Longitude constraints must be set before calling apply_constraint_to_data().");
278 Array::Dim_iter fd = d_latitude->dim_begin();
289 throw Error(
"The upper and lower latitude indices appear to be reversed. Please provide the latitude bounding box numbers giving the northern-most latitude first.");
338 fd = d_longitude->dim_begin();
350 DBG(cerr <<
"Inverted latitude sense" << endl);
366 Grid::Map_iter i = d_grid->map_begin();
367 Grid::Map_iter end = d_grid->map_end();
369 if (*i != d_latitude && *i != d_longitude) {
370 if ((*i)->send_p()) {
371 DBG(cerr <<
"reading grid map: " << (*i)->name() << endl);
384 throw InternalErr(__FILE__, __LINE__,
"Expected data size not copied to the Grid's buffer.");
386 d_grid->set_read_p(
true);
389 d_grid->get_array()->read();
void set_lat_dim(Array::Dim_iter lat)
char * get_array_data() const
void set_latitude_index_bottom(int bottom)
void set_lon(double *lon)
virtual void reorder_data_longitude_axis(Array &a, Array::Dim_iter lon_dim)
Reorder the data values relative to the longitude axis so that the reordered longitude map (see GeoCo...
void set_latitude_index_top(int top)
set< string > get_coards_lat_units() const
set< string > get_lon_names() const
void set_longitude_rightmost(bool state)
int get_longitude_index_left() const
Array::Dim_iter get_lon_dim() const
void set_lat(double *lat)
bool unit_or_name_match(set< string > units, set< string > names, const string &var_units, const string &var_name)
Look in the containers which hold the units attributes and variable name prefixes which are considere...
void set_lat_length(int len)
virtual void transpose_vector(double *src, const int length)
Given a vector of doubles, transpose the elements.
GridGeoConstraint(Grid *grid)
Initialize GeoConstraint with a Grid.
int get_longitude_index_right() const
int get_array_data_size() const
int get_latitude_index_bottom() const
set< string > get_coards_lon_units() const
virtual void flip_latitude_within_array(Array &a, int lat_length, int lon_length)
virtual void transform_longitude_to_neg_pos_notation()
Given that the Grid has a longitude map that uses the 'pos' notation, transform it to the 'neg_pos' n...
LatitudeSense get_latitude_sense() const
set< string > get_lat_names() const
void set_longitude_index_right(int right)
Notation get_longitude_notation() const
virtual void reorder_longitude_map(int longitude_index_left)
Reorder the elements in the longitude map so that the longitude constraint no longer crosses the edge...
int get_lon_length() const
void set_longitude_index_left(int left)
Encapsulate the logic needed to handle geographical constraints when they are applied to DAP Grid (an...
Array::Dim_iter get_lat_dim() const
bool is_bounding_box_set() const
void set_lon_dim(Array::Dim_iter lon)
int get_latitude_index_top() const
virtual void apply_constraint_to_data()
Once the bounding box is set use this method to apply the constraint.
void set_lon_length(int len)