39#include <libdap/Float64.h>
40#include <libdap/Array.h>
41#include <libdap/Error.h>
42#include <libdap/InternalErr.h>
43#include <libdap/dods-datatypes.h>
44#include <libdap/util.h>
45#include <libdap/debug.h>
47#include "GeoConstraint.h"
63 is_prefix(
const string & in): s(in)
66 bool operator()(
const string & prefix)
68 return s.find(prefix) == 0;
83unit_or_name_match(set < string > units, set < string > names,
84 const string & var_units,
const string & var_name)
86 return (units.find(var_units) != units.end()
87 || find_if(names.begin(), names.end(),
88 is_prefix(var_name)) != names.end());
107 const double right)
const
109 return (left < 0 || right < 0) ? neg_pos : pos;
127GeoConstraint::transform_constraint_to_pos_notation(
double &left,
150 for (
int i = 0; i < d_lon_length; ++i)
166 for (
int i = 0; i < d_lon_length; ++i)
171bool GeoConstraint::is_bounding_box_valid(
const double left,
const double top,
172 const double right,
const double bottom)
const
174 if ((left < d_lon[0] && right < d_lon[0])
175 || (left > d_lon[d_lon_length-1] && right > d_lon[d_lon_length-1]))
178 if (d_latitude_sense == normal) {
180 if ((top > d_lat[0] && bottom > d_lat[0])
181 || (top < d_lat[d_lat_length-1] && bottom < d_lat[d_lat_length-1]))
185 if ((top < d_lat[0] && bottom < d_lat[0])
186 || (top > d_lat[d_lat_length-1] && bottom > d_lat[d_lat_length-1]))
204 int &longitude_index_left,
int &longitude_index_right)
const
209 double t_left = fmod(left, 360.0);
210 double t_right = fmod(right, 360.0);
217 int lon_origin_index = 0;
218 double smallest_lon = fmod(d_lon[0], 360.0);
219 while (i < d_lon_length) {
220 double curent_lon_value = fmod(d_lon[i], 360.0);
221 if (smallest_lon > curent_lon_value) {
222 smallest_lon = curent_lon_value;
223 lon_origin_index = i;
228 DBG2(cerr <<
"lon_origin_index: " << lon_origin_index << endl);
233 i = lon_origin_index;
234 while (fmod(d_lon[i], 360.0) < t_left) {
236 i = i % d_lon_length;
239 if (i == lon_origin_index)
240 throw Error(
"geogrid: Could not find an index for the longitude value '" + double_to_string(left) +
"'");
243 if (fmod(d_lon[i], 360.0) == t_left)
244 longitude_index_left = i;
246 longitude_index_left = (i - 1) > 0 ? i - 1 : 0;
248 DBG2(cerr <<
"longitude_index_left: " << longitude_index_left << endl);
252 int largest_lon_index = (lon_origin_index - 1 + d_lon_length) % d_lon_length;
253 i = largest_lon_index;
254 while (fmod(d_lon[i], 360.0) > t_right) {
256 i = (i == 0) ? d_lon_length - 1 : i - 1;
257 if (i == largest_lon_index)
258 throw Error(
"geogrid: Could not find an index for the longitude value '" + double_to_string(right) +
"'");
261 if (fmod(d_lon[i], 360.0) == t_right)
262 longitude_index_right = i;
264 longitude_index_right = (i + 1) < d_lon_length - 1 ? i + 1 : d_lon_length - 1;
266 DBG2(cerr <<
"longitude_index_right: " << longitude_index_right << endl);
283 int &latitude_index_top,
284 int &latitude_index_bottom)
const
288 if (sense == normal) {
290 while (i < d_lat_length - 1 && top < d_lat[i])
293 j = d_lat_length - 1;
294 while (j > 0 && bottom > d_lat[j])
298 latitude_index_top = i;
300 latitude_index_top = (i - 1) > 0 ? i - 1 : 0;
302 if (d_lat[j] == bottom)
303 latitude_index_bottom = j;
305 latitude_index_bottom =
306 (j + 1) < d_lat_length - 1 ? j + 1 : d_lat_length - 1;
309 i = d_lat_length - 1;
310 while (i > 0 && d_lat[i] > top)
314 while (j < d_lat_length - 1 && d_lat[j] < bottom)
318 latitude_index_top = i;
320 latitude_index_top = (i + 1) < d_lat_length - 1 ? i + 1 : d_lat_length - 1;
322 if (d_lat[j] == bottom)
323 latitude_index_bottom = j;
325 latitude_index_bottom = (j - 1) > 0 ? j - 1 : 0;
334 return d_lat[0] >= d_lat[d_lat_length - 1] ? normal : inverted;
340swap_vector_ends(
char *dest,
char *src,
int len,
int index,
int elem_sz)
342 memcpy(dest, src + index * elem_sz, (len - index) * elem_sz);
344 memcpy(dest + (len - index) * elem_sz, src, index * elem_sz);
348static void transpose(std::vector<std::vector<T> > a,
349 std::vector<std::vector<T> > b,
int width,
int height)
351 for (
int i = 0; i < width; i++) {
352 for (
int j = 0; j < height; j++) {
367 double *tmp =
new double[length];
369 int i = 0, j = length-1;
373 memcpy(src, tmp,length *
sizeof(
double));
379count_size_except_latitude_and_longitude(Array &a)
381 if (a.dim_end() - a.dim_begin() <= 2)
385 for (Array::Dim_iter i = a.dim_begin(); (i + 2) != a.dim_end(); ++i)
386 size *= a.dimension_size(i,
true);
391void GeoConstraint::flip_latitude_within_array(Array &a,
int lat_length,
396 d_array_data =
static_cast<char*
>(a.value());
397 d_array_data_size = a.width(
true);
400 int size = count_size_except_latitude_and_longitude(a);
402 vector<char> tmp_data(d_array_data_size);
403 int array_elem_size = a.var()->width(
true);
404 int lat_lon_size = (d_array_data_size / size);
406 DBG(cerr <<
"lat, lon_length: " << lat_length <<
", " << lon_length << endl);
407 DBG(cerr <<
"size: " << size << endl);
408 DBG(cerr <<
"d_array_data_size: " << d_array_data_size << endl);
409 DBG(cerr <<
"array_elem_size: " << array_elem_size<< endl);
410 DBG(cerr <<
"lat_lon_size: " << lat_lon_size<< endl);
412 for (
int i = 0; i < size; ++i) {
414 int s_lat = lat_length - 1;
416 int lon_size = array_elem_size * lon_length;
417 int offset = i * lat_lon_size;
419 memcpy(tmp_data.data() + offset + (lat++ * lon_size),
420 d_array_data + offset + (s_lat-- * lon_size),
424 memcpy(d_array_data, tmp_data.data(), d_array_data_size);
437 double *tmp_lon =
new double[d_lon_length];
439 swap_vector_ends((
char *) tmp_lon, (
char *) d_lon, d_lon_length,
440 longitude_index_left,
sizeof(
double));
442 memcpy(d_lon, tmp_lon, d_lon_length *
sizeof(
double));
448count_dimensions_except_longitude(Array &a)
451 for (Array::Dim_iter i = a.dim_begin(); (i + 1) != a.dim_end(); ++i)
452 size *= a.dimension_size(i,
true);
477 if (!is_longitude_rightmost())
478 throw Error(
"This grid does not have Longitude as its rightmost dimension, the geogrid()\ndoes not support constraints that wrap around the edges of this type of grid.");
480 DBG(cerr <<
"Constraint for the left half: " << get_longitude_index_left()
481 <<
", " << get_lon_length() - 1 << endl);
484 a.add_constraint(lon_dim, get_longitude_index_left(), 1,
485 get_lon_length() - 1);
488 DBG2(a.print_val(stderr));
491 int left_size = a.width(
true);
492 char *left_data = (
char*)a.value();
500 DBG(cerr <<
"Constraint for the right half: " << 0
501 <<
", " << get_longitude_index_right() << endl);
503 a.add_constraint(lon_dim, 0, 1, get_longitude_index_right());
506 DBG2(a.print_val(stderr));
508 char *right_data = (
char*)a.value();
509 int right_size = a.width(
true);
512 d_array_data_size = left_size + right_size;
513 d_array_data =
new char[d_array_data_size];
518 int elem_size = a.var()->width(
true);
519 int left_row_size = (get_lon_length() - get_longitude_index_left()) * elem_size;
520 int right_row_size = (get_longitude_index_right() + 1) * elem_size;
521 int total_bytes_per_row = left_row_size + right_row_size;
523 DBG2(cerr <<
"elem_size: " << elem_size <<
"; left & right size: "
524 << left_row_size <<
", " << right_row_size << endl);
528 int rows_to_copy = count_dimensions_except_longitude(a);
529 for (
int i = 0; i < rows_to_copy; ++i) {
530 DBG(cerr <<
"Copying " << i <<
"th row" << endl);
531 DBG(cerr <<
"left memcpy: " << *(
float *)(left_data + (left_row_size * i)) << endl);
533 memcpy(d_array_data + (total_bytes_per_row * i),
534 left_data + (left_row_size * i),
537 DBG(cerr <<
"right memcpy: " << *(
float *)(right_data + (right_row_size * i)) << endl);
539 memcpy(d_array_data + (total_bytes_per_row * i) + left_row_size,
540 right_data + (right_row_size * i),
551 : d_array_data(0), d_array_data_size(0),
552 d_lat(0), d_lon(0), d_lat_length(0), d_lon_length(0),
553 d_latitude_index_top(0),
554 d_latitude_index_bottom(0),
555 d_longitude_index_left(0),
556 d_longitude_index_right(0),
557 d_bounding_box_set(false),
558 d_longitude_rightmost(false),
559 d_longitude_notation(unknown_notation),
560 d_latitude_sense(unknown_sense)
563 d_coards_lat_units.insert(
"degrees_north");
564 d_coards_lat_units.insert(
"degree_north");
565 d_coards_lat_units.insert(
"degree_N");
566 d_coards_lat_units.insert(
"degrees_N");
568 d_coards_lon_units.insert(
"degrees_east");
569 d_coards_lon_units.insert(
"degree_east");
570 d_coards_lon_units.insert(
"degrees_E");
571 d_coards_lon_units.insert(
"degree_E");
573 d_lat_names.insert(
"COADSY");
574 d_lat_names.insert(
"lat");
575 d_lat_names.insert(
"Lat");
576 d_lat_names.insert(
"LAT");
578 d_lon_names.insert(
"COADSX");
579 d_lon_names.insert(
"lon");
580 d_lon_names.insert(
"Lon");
581 d_lon_names.insert(
"LON");
595 double bottom,
double right)
600 if (d_bounding_box_set)
601 throw Error(
"It is not possible to register more than one geographical constraint on a variable.");
606 if (d_latitude_sense == inverted)
607 throw Error(
"geogrid() does not currently work with inverted data (data where the north pole is at a negative latitude value).");
614 if (d_longitude_notation == neg_pos)
615 transform_constraint_to_pos_notation(left, right);
623 if (longitude_notation == neg_pos)
626 if (!is_bounding_box_valid(left, top, right, bottom))
627 throw Error(
"The bounding box does not intersect any data within this Grid or Array. The\ngeographical extent of these data are from latitude "
628 + double_to_string(d_lat[0]) +
" to "
629 + double_to_string(d_lat[d_lat_length-1])
630 +
"\nand longitude " + double_to_string(d_lon[0])
631 +
" to " + double_to_string(d_lon[d_lon_length-1])
632 +
" while the bounding box provided was latitude "
633 + double_to_string(top) +
" to "
634 + double_to_string(bottom) +
"\nand longitude "
635 + double_to_string(left) +
" to "
636 + double_to_string(right));
643 d_latitude_index_top, d_latitude_index_bottom);
648 d_longitude_index_right);
650 DBG(cerr <<
"Bounding box (tlbr): " << d_latitude_index_top <<
", "
651 << d_longitude_index_left <<
", "
652 << d_latitude_index_bottom <<
", "
653 << d_longitude_index_right << endl);
655 d_bounding_box_set =
true;
GeoConstraint()
Initialize GeoConstraint.
virtual void transform_longitude_to_pos_notation()
virtual void reorder_longitude_map(int longitude_index_left)
virtual void transform_longitude_to_neg_pos_notation()
virtual void reorder_data_longitude_axis(libdap::Array &a, libdap::Array::Dim_iter lon_dim)
void find_latitude_indeces(double top, double bottom, LatitudeSense sense, int &latitude_index_top, int &latitude_index_bottom) const
Notation categorize_notation(const double left, const double right) const
void find_longitude_indeces(double left, double right, int &longitude_index_left, int &longitude_index_right) const
virtual void transpose_vector(double *src, const int length)
virtual LatitudeSense categorize_latitude() const
void set_bounding_box(double top, double left, double bottom, double right)