Skip to content

Commit

Permalink
Opencharts : adding cache files for opencharts Projections.
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvielamythepaut committed Oct 6, 2023
1 parent 5d4efa8 commit be6bc5b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 38 deletions.
7 changes: 7 additions & 0 deletions src/attributes/TileDecoderAttributes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ using namespace magics;
TileDecoderAttributes::TileDecoderAttributes():
file_name_(ParameterManager::getString("grib_input_file_name")),
projection_(ParameterManager::getString("grib_tile_projection")),
mode_(ParameterManager::getString("grib_tile_mode")),
loop_(ParameterManager::getBool("grib_loop")),
z_(ParameterManager::getInt("grib_tile_z")),
x_(ParameterManager::getInt("grib_tile_x")),
Expand Down Expand Up @@ -54,6 +55,7 @@ void TileDecoderAttributes::set(const std::map<string, string>& params)

setAttribute(prefix, "grib_input_file_name", file_name_, params);
setAttribute(prefix, "grib_tile_projection", projection_, params);
setAttribute(prefix, "grib_tile_mode", mode_, params);
setAttribute(prefix, "grib_loop", loop_, params);
setAttribute(prefix, "grib_tile_z", z_, params);
setAttribute(prefix, "grib_tile_x", x_, params);
Expand All @@ -69,6 +71,7 @@ void TileDecoderAttributes::copy(const TileDecoderAttributes& other)
{
file_name_ = other.file_name_;
projection_ = other.projection_;
mode_ = other.mode_;
loop_ = other.loop_;
z_ = other.z_;
x_ = other.x_;
Expand Down Expand Up @@ -116,6 +119,7 @@ void TileDecoderAttributes::print(ostream& out) const
out << "Attributes[";
out << " file_name = " << file_name_;
out << " projection = " << projection_;
out << " mode = " << mode_;
out << " loop = " << loop_;
out << " z = " << z_;
out << " x = " << x_;
Expand All @@ -134,6 +138,8 @@ void TileDecoderAttributes::toxml(ostream& out) const
niceprint(out,file_name_);
out << ", \"grib_tile_projection\":";
niceprint(out,projection_);
out << ", \"grib_tile_mode\":";
niceprint(out,mode_);
out << ", \"grib_loop\":";
niceprint(out,loop_);
out << ", \"grib_tile_z\":";
Expand All @@ -153,6 +159,7 @@ void TileDecoderAttributes::toxml(ostream& out) const

static MagicsParameter<string> grib_input_file_name("grib_input_file_name", "");
static MagicsParameter<string> grib_tile_projection("grib_tile_projection", "cylindrical");
static MagicsParameter<string> grib_tile_mode("grib_tile_mode", "eccharts");
static MagicsParameter<string> grib_loop("grib_loop", "off");
static MagicsParameter<int> grib_tile_z("grib_tile_z", 1);
static MagicsParameter<int> grib_tile_x("grib_tile_x", 0);
Expand Down
1 change: 1 addition & 0 deletions src/attributes/TileDecoderAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class TileDecoderAttributes
string tag_;
string file_name_;
string projection_;
string mode_;
bool loop_;
int z_;
int x_;
Expand Down
4 changes: 4 additions & 0 deletions src/attributes/TileDecoderWrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ void TileDecoderWrapper::set(const MagRequest& request)
string projection_value = request("GRIB_TILE_PROJECTION");
tiledecoder_->projection_ = projection_value;
}
if (request.countValues("GRIB_TILE_MODE") ) {
string mode_value = request("GRIB_TILE_MODE");
tiledecoder_->mode_ = mode_value;
}
if (request.countValues("GRIB_LOOP") ) {
string loop_value = request("GRIB_LOOP");

Expand Down
93 changes: 55 additions & 38 deletions src/decoders/TileDecoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ string TileDecoder::projection() {
string TileDecoder::weights() {
ostringstream out;
string parent = getEnvVariable("MAGPLUS_TILE");

if (parent.empty()) {
parent = buildSharePath("tiles");
}
out << parent << "/weight-" << grid_ << "-" << projection() << "-z" + tostring(z_) << ".nc";
if (mode_ == "opencharts" )
out << parent << "/opencharts-cache-" << grid_ << ".nc";

else
out << parent << "/weight-" << grid_ << "-" << projection() << "-z" + tostring(z_) << ".nc";
return out.str();
}

Expand Down Expand Up @@ -435,56 +440,68 @@ void TileDecoder::decode() {

Timer timer("Tile", path);
#ifdef HAVE_NETCDF

Netcdf netcdf(path, "index");

map<string, string> first, last;
first["x"] = tostring(x_);
first["y"] = tostring(y_);
last["x"] = tostring(x_);
last["y"] = tostring(y_);

static vector<double> bbox;
static vector<double> latitudes;
static vector<double> longitudes;

static vector<double> dindex;
static vector<double> distances;

int nblat = netcdf.getDimension("lat") - 1;
int nblon = netcdf.getDimension("lon") - 1;

if (bbox.empty()) {
netcdf.get("bounding-box", bbox, first, last);
if ( mode_ == "opencharts") {

int error;
FILE* in = fopen(file_name_.c_str(), "rb");
if (!in) {
if (MagicsGlobal::strict()) {
throw CannotOpenFile(file_name_);
netcdf.get(projection_ + "_lat", latitudes, first, last);
netcdf.get(projection_ + "_lon", longitudes, first, last);
netcdf.get(projection_ + "_index", dindex, first, last);
netcdf.get(projection_ + "_distances", distances, first, last);
}
else {
first["x"] = tostring(x_);
first["y"] = tostring(y_);
last["x"] = tostring(x_);
last["y"] = tostring(y_);



int nblat = netcdf.getDimension("lat") - 1;
int nblon = netcdf.getDimension("lon") - 1;

if (bbox.empty()) {
netcdf.get("bounding-box", bbox, first, last);

int error;
FILE* in = fopen(file_name_.c_str(), "rb");
if (!in) {
if (MagicsGlobal::strict()) {
throw CannotOpenFile(file_name_);
}
MagLog::error() << "ERROR: unable to create handle from file" << file_name_ << endl;
return;
}
MagLog::error() << "ERROR: unable to create handle from file" << file_name_ << endl;
return;
}

// Adding a gutter of 5 points to avoid borders in tiles
int miny = std::min(bbox[1], bbox[3]);
miny = std::max(0, miny - 5);
int maxy = std::max(bbox[1], bbox[3]);
maxy = std::min(nblat, maxy + 5);
int minx = std::min(bbox[0], bbox[2]);
minx = std::max(0, minx - 5);
int maxx = std::max(bbox[0], bbox[2]);
maxx = std::min(nblon, maxx + 5);

first["lat"] = tostring(miny);
first["lon"] = tostring(minx);
last["lat"] = tostring(maxy);
last["lon"] = tostring(maxx);

netcdf.get("lat", latitudes, first, last);
netcdf.get("lon", longitudes, first, last);
netcdf.get("index", dindex, first, last);
netcdf.get("distances", distances, first, last);
// Adding a gutter of 5 points to avoid borders in tiles
int miny = std::min(bbox[1], bbox[3]);
miny = std::max(0, miny - 5);
int maxy = std::max(bbox[1], bbox[3]);
maxy = std::min(nblat, maxy + 5);
int minx = std::min(bbox[0], bbox[2]);
minx = std::max(0, minx - 5);
int maxx = std::max(bbox[0], bbox[2]);
maxx = std::min(nblon, maxx + 5);

first["lat"] = tostring(miny);
first["lon"] = tostring(minx);
last["lat"] = tostring(maxy);
last["lon"] = tostring(maxx);

netcdf.get("lat", latitudes, first, last);
netcdf.get("lon", longitudes, first, last);
netcdf.get("index", dindex, first, last);
netcdf.get("distances", distances, first, last);
}
}

int index[4];
Expand Down
5 changes: 5 additions & 0 deletions src/params/TileDecoder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ does it submit to any jurisdiction.
default="cylindrical"
from="string"
name="grib_tile_projection"></parameter>
<parameter member="mode"
to="string"
default="eccharts"
from="string"
name="grib_tile_mode"></parameter>
<parameter member="loop"
to="bool"
default="off"
Expand Down

0 comments on commit be6bc5b

Please sign in to comment.