00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "config.h"
00030
00031 static char id[] not_used =
00032 { "$Id: GridGeoConstraint.cc 16966 2007-08-20 21:54:12Z jimg $"
00033 };
00034
00035 #include <math.h>
00036 #include <string.h>
00037
00038 #include <iostream>
00039 #include <sstream>
00040
00041
00042
00043 #include "debug.h"
00044 #include "dods-datatypes.h"
00045 #include "GridGeoConstraint.h"
00046 #include "Float64.h"
00047
00048 #include "Error.h"
00049 #include "InternalErr.h"
00050 #include "ce_functions.h"
00051
00052 using namespace std;
00053 using namespace libdap;
00054
00055
00062 GridGeoConstraint::GridGeoConstraint(Grid *grid, const string &ds_name)
00063 : GeoConstraint(ds_name), d_grid(grid), d_latitude(0), d_longitude(0)
00064 {
00065 if (d_grid->get_array()->dimensions() < 2
00066 || d_grid->get_array()->dimensions() > 3)
00067 throw Error("The geogrid() function works only with Grids of two or three dimensions.");
00068
00069
00070 if (!build_lat_lon_maps())
00071 throw Error(string("The grid '") + d_grid->name()
00072 +
00073 "' does not have identifiable latitude/longitude map vectors.");
00074
00075 if (!lat_lon_dimensions_ok())
00076 throw Error("The geogrid() function will only work when the Grid's Longitude and Latitude\nmaps are the rightmost dimensions.");
00077 }
00078
00094 bool GridGeoConstraint::build_lat_lon_maps()
00095 {
00096 Grid::Map_iter m = d_grid->map_begin();
00097
00098
00099
00100 Array::Dim_iter d = d_grid->get_array()->dim_begin();
00101
00102 while (m != d_grid->map_end() && (!d_latitude || !d_longitude)) {
00103 string units_value = (*m)->get_attr_table().get_attr("units");
00104 remove_quotes(units_value);
00105 string map_name = (*m)->name();
00106
00107
00108
00109 if (!d_latitude
00110 && unit_or_name_match(get_coards_lat_units(), get_lat_names(),
00111 units_value, map_name)) {
00112
00113
00114
00115
00116
00117
00118
00119 d_latitude = dynamic_cast < Array * >(*m);
00120 if (!d_latitude->read_p())
00121 d_latitude->read(get_dataset());
00122
00123 set_lat(extract_double_array(d_latitude));
00124 set_lat_length(d_latitude->length());
00125
00126 set_lat_dim(d);
00127 }
00128
00129 if (!d_longitude
00130 && unit_or_name_match(get_coards_lon_units(), get_lon_names(),
00131 units_value, map_name)) {
00132
00133 d_longitude = dynamic_cast < Array * >(*m);
00134 if (!d_longitude->read_p())
00135 d_longitude->read(get_dataset());
00136
00137 set_lon(extract_double_array(d_longitude));
00138 set_lon_length(d_longitude->length());
00139
00140 set_lon_dim(d);
00141 }
00142
00143 ++m;
00144 ++d;
00145 }
00146
00147 return get_lat() && get_lon();
00148 }
00149
00160 bool
00161 GridGeoConstraint::lat_lon_dimensions_ok()
00162 {
00163
00164 Grid::Map_riter rightmost = d_grid->map_rbegin();
00165 Grid::Map_riter next_rightmost = rightmost + 1;
00166
00167 if (*rightmost == d_longitude && *next_rightmost == d_latitude)
00168 set_longitude_rightmost(true);
00169 else if (*rightmost == d_latitude && *next_rightmost == d_longitude)
00170 set_longitude_rightmost(false);
00171 else
00172 return false;
00173
00174 return true;
00175 }
00176
00198 void GridGeoConstraint::apply_constraint_to_data()
00199 {
00200 if (!get_bounding_box_set())
00201 throw
00202 InternalErr
00203 ("The Latitude and Longitude constraints must be set before calling apply_constraint_to_data().");
00204
00205 Array::Dim_iter fd = d_latitude->dim_begin();
00206 if (get_latitude_sense() == inverted) {
00207 int tmp = get_latitude_index_top();
00208 set_latitude_index_top(get_latitude_index_bottom());
00209 set_latitude_index_bottom(tmp);
00210 }
00211
00212
00213
00214 if (get_latitude_index_top() > get_latitude_index_bottom())
00215 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.");
00216
00217
00218 d_latitude->add_constraint(fd, get_latitude_index_top(), 1,
00219 get_latitude_index_bottom());
00220 d_grid->get_array()->add_constraint(get_lat_dim(),
00221 get_latitude_index_top(), 1,
00222 get_latitude_index_bottom());
00223
00224
00225
00226
00227 if (get_longitude_index_left() > get_longitude_index_right()) {
00228 reorder_longitude_map(get_longitude_index_left());
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 reorder_data_longitude_axis(*d_grid->get_array());
00239
00240
00241
00242
00243
00244 set_longitude_index_right(get_lon_length() - get_longitude_index_left()
00245 + get_longitude_index_right());
00246 set_longitude_index_left(0);
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 if (get_longitude_notation() == neg_pos) {
00260 transform_longitude_to_neg_pos_notation();
00261 }
00262
00263 fd = d_longitude->dim_begin();
00264 d_longitude->add_constraint(fd, get_longitude_index_left(), 1,
00265 get_longitude_index_right());
00266
00267 d_grid->get_array()->add_constraint(get_lon_dim(),
00268 get_longitude_index_left(),
00269 1, get_longitude_index_right());
00270
00271
00272 set_array_using_double(d_latitude, get_lat() + get_latitude_index_top(),
00273 get_latitude_index_bottom() - get_latitude_index_top() + 1);
00274
00275 set_array_using_double(d_longitude, get_lon() + get_longitude_index_left(),
00276 get_longitude_index_right() - get_longitude_index_left() + 1);
00277
00278
00279 if (get_array_data()) {
00280 int size = d_grid->get_array()->val2buf(get_array_data());
00281 if (size != get_array_data_size())
00282 throw
00283 InternalErr
00284 ("Expected data size not copied to the Grid's buffer.");
00285 d_grid->set_read_p(true);
00286 }
00287 else {
00288 d_grid->get_array()->read(get_dataset());
00289 }
00290 }