libdap++ Updated for version 3.8.2
|
00001 00002 // -*- mode: c++; c-basic-offset:4 -*- 00003 00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data 00005 // Access Protocol. 00006 00007 // Copyright (c) 2002,2003 OPeNDAP, Inc. 00008 // Author: James Gallagher <jgallagher@opendap.org> 00009 // 00010 // This library is free software; you can redistribute it and/or 00011 // modify it under the terms of the GNU Lesser General Public 00012 // License as published by the Free Software Foundation; either 00013 // version 2.1 of the License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 // 00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112. 00025 00026 // The Grid Selection Expression Clause class. 00027 00028 00029 #include "config.h" 00030 00031 static char id[] not_used = 00032 { "$Id: GridGeoConstraint.cc 20518 2009-03-05 23:39:46Z jimg $" 00033 }; 00034 00035 #include <cmath> 00036 00037 #include <iostream> 00038 #include <sstream> 00039 00040 //#define DODS_DEBUG 00041 00042 #include "debug.h" 00043 #include "dods-datatypes.h" 00044 #include "GridGeoConstraint.h" 00045 #include "Float64.h" 00046 00047 #include "Error.h" 00048 #include "InternalErr.h" 00049 #include "ce_functions.h" 00050 #include "util.h" 00051 00052 using namespace std; 00053 00054 namespace libdap { 00055 00056 00065 GridGeoConstraint::GridGeoConstraint(Grid *grid) 00066 : GeoConstraint(), d_grid(grid), d_latitude(0), d_longitude(0) 00067 { 00068 if (d_grid->get_array()->dimensions() < 2 00069 || d_grid->get_array()->dimensions() > 3) 00070 throw Error("The geogrid() function works only with Grids of two or three dimensions."); 00071 00072 // Is this Grid a geo-referenced grid? Throw Error if not. 00073 if (!build_lat_lon_maps()) 00074 throw Error(string("The grid '") + d_grid->name() 00075 + 00076 "' does not have identifiable latitude/longitude map vectors."); 00077 00078 if (!lat_lon_dimensions_ok()) 00079 throw Error("The geogrid() function will only work when the Grid's Longitude and Latitude\nmaps are the rightmost dimensions."); 00080 } 00081 00097 bool GridGeoConstraint::build_lat_lon_maps() 00098 { 00099 Grid::Map_iter m = d_grid->map_begin(); 00100 // Assume that a Grid is correct and thus has exactly as many maps at its 00101 // array part has dimensions. Thus don't bother to test the Grid's array 00102 // dimension iterator for '!= dim_end()'. 00103 Array::Dim_iter d = d_grid->get_array()->dim_begin(); 00104 // The fields d_latitude and d_longitude are initialized to null 00105 while (m != d_grid->map_end() && (!d_latitude || !d_longitude)) { 00106 string units_value = (*m)->get_attr_table().get_attr("units"); 00107 units_value = remove_quotes(units_value); 00108 string map_name = (*m)->name(); 00109 00110 // The 'units' attribute must match exactly; the name only needs to 00111 // match a prefix. 00112 if (!d_latitude 00113 && unit_or_name_match(get_coards_lat_units(), get_lat_names(), 00114 units_value, map_name)) { 00115 00116 // Set both d_latitude (a pointer to the real map vector) and 00117 // d_lat, a vector of the values represented as doubles. It's easier 00118 // to work with d_lat, but it's d_latitude that needs to be set 00119 // when constraining the grid. Also, record the grid variable's 00120 // dimension iterator so that it's easier to set the Grid's Array 00121 // (which also has to be constrained). 00122 d_latitude = dynamic_cast < Array * >(*m); 00123 if (!d_latitude) 00124 throw InternalErr(__FILE__, __LINE__, "Expected an array."); 00125 if (!d_latitude->read_p()) 00126 d_latitude->read(); 00127 00128 set_lat(extract_double_array(d_latitude)); // throws Error 00129 set_lat_length(d_latitude->length()); 00130 00131 set_lat_dim(d); 00132 } 00133 00134 if (!d_longitude // && !units_value.empty() 00135 && unit_or_name_match(get_coards_lon_units(), get_lon_names(), 00136 units_value, map_name)) { 00137 00138 d_longitude = dynamic_cast < Array * >(*m); 00139 if (!d_longitude) 00140 throw InternalErr(__FILE__, __LINE__, "Expected an array."); 00141 if (!d_longitude->read_p()) 00142 d_longitude->read(); 00143 00144 set_lon(extract_double_array(d_longitude)); 00145 set_lon_length(d_longitude->length()); 00146 00147 set_lon_dim(d); 00148 } 00149 00150 ++m; 00151 ++d; 00152 } 00153 00154 return get_lat() && get_lon(); 00155 } 00156 00167 bool 00168 GridGeoConstraint::lat_lon_dimensions_ok() 00169 { 00170 // get the last two map iterators 00171 Grid::Map_riter rightmost = d_grid->map_rbegin(); 00172 Grid::Map_riter next_rightmost = rightmost + 1; 00173 00174 if (*rightmost == d_longitude && *next_rightmost == d_latitude) 00175 set_longitude_rightmost(true); 00176 else if (*rightmost == d_latitude && *next_rightmost == d_longitude) 00177 set_longitude_rightmost(false); 00178 else 00179 return false; 00180 00181 return true; 00182 } 00183 00205 void GridGeoConstraint::apply_constraint_to_data() 00206 { 00207 if (!get_bounding_box_set()) 00208 throw 00209 InternalErr 00210 ("The Latitude and Longitude constraints must be set before calling apply_constraint_to_data()."); 00211 00212 Array::Dim_iter fd = d_latitude->dim_begin(); 00213 if (get_latitude_sense() == inverted) { 00214 int tmp = get_latitude_index_top(); 00215 set_latitude_index_top(get_latitude_index_bottom()); 00216 set_latitude_index_bottom(tmp); 00217 } 00218 00219 // It's esy to flip the Latitude values; if the bottom index value 00220 // is before/above the top index, return an error explaining that. 00221 if (get_latitude_index_top() > get_latitude_index_bottom()) 00222 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."); 00223 00224 // Constrain the lat vector and lat dim of the array 00225 d_latitude->add_constraint(fd, get_latitude_index_top(), 1, 00226 get_latitude_index_bottom()); 00227 d_grid->get_array()->add_constraint(get_lat_dim(), 00228 get_latitude_index_top(), 1, 00229 get_latitude_index_bottom()); 00230 00231 // Does the longitude constraint cross the edge of the longitude vector? 00232 // If so, reorder the grid's data (array), longitude map vector and the 00233 // local vector of longitude data used for computation. 00234 if (get_longitude_index_left() > get_longitude_index_right()) { 00235 reorder_longitude_map(get_longitude_index_left()); 00236 00237 // If the longitude constraint is 'split', join the two parts, reload 00238 // the data into the Grid's Array and make sure the Array is marked as 00239 // already read. This should be true for the whole Grid, but if some 00240 // future modification changes that, the array will be covered here. 00241 // Note that the following method only reads the data out and stores 00242 // it in this object after joining the two parts. The method 00243 // apply_constraint_to_data() transfers the data back from the this 00244 // objet to the DAP Grid variable. 00245 reorder_data_longitude_axis(*d_grid->get_array()); 00246 00247 // Now the data are all in local storage 00248 00249 // alter the indices; the left index has now been moved to 0, and the right 00250 // index is now at lon_vector_length-left+right. 00251 set_longitude_index_right(get_lon_length() - get_longitude_index_left() 00252 + get_longitude_index_right()); 00253 set_longitude_index_left(0); 00254 } 00255 // If the constraint used the -180/179 (neg_pos) notation, transform 00256 // the longitude map s it uses the -180/179 notation. Note that at this 00257 // point, d_longitude always uses the pos notation because of the earlier 00258 // conditional transformation. 00259 00260 // Do this _before_ applying the constraint since set_array_using_double() 00261 // tests the array length using Vector::length() and that method returns 00262 // the length _as constrained_. We want to move all of the longitude 00263 // values from d_lon back into the map, not just the number that will be 00264 // sent (although an optimization might do this, it's hard to imagine 00265 // it would gain much). 00266 if (get_longitude_notation() == neg_pos) { 00267 transform_longitude_to_neg_pos_notation(); 00268 } 00269 // Apply constraint; stride is always one and maps only have one dimension 00270 fd = d_longitude->dim_begin(); 00271 d_longitude->add_constraint(fd, get_longitude_index_left(), 1, 00272 get_longitude_index_right()); 00273 00274 d_grid->get_array()->add_constraint(get_lon_dim(), 00275 get_longitude_index_left(), 00276 1, get_longitude_index_right()); 00277 00278 // Transfer values from the local lat vector to the Grid's 00279 set_array_using_double(d_latitude, get_lat() + get_latitude_index_top(), 00280 get_latitude_index_bottom() - get_latitude_index_top() + 1); 00281 00282 set_array_using_double(d_longitude, get_lon() + get_longitude_index_left(), 00283 get_longitude_index_right() - get_longitude_index_left() + 1); 00284 00285 // ... and then the Grid's array if it has been read. 00286 if (get_array_data()) { 00287 /* ***/ 00288 int size = d_grid->get_array()->val2buf(get_array_data()); 00289 00290 if (size != get_array_data_size()) 00291 throw 00292 InternalErr 00293 ("Expected data size not copied to the Grid's buffer."); 00294 d_grid->set_read_p(true); 00295 } 00296 else { 00297 d_grid->get_array()->read(); 00298 } 00299 } 00300 00301 } // namespace libdap