Modernisation: Nearest: Move shared variables to the base class

This commit is contained in:
Eugen Betke 2024-11-03 21:29:50 +01:00
parent 2a330d3b2a
commit 9b5ddb9be7
20 changed files with 52 additions and 266 deletions

View File

@ -30,8 +30,14 @@ int Gen::init(grib_handle* h, grib_arguments* args)
int Gen::destroy() int Gen::destroy()
{ {
grib_context* c = grib_context_get_default(); grib_context* c = grib_context_get_default();
if (values_)
grib_context_free(c, values_); if (lats_) grib_context_free(c, lats_);
if (lons_) grib_context_free(c, lons_);
if (i_) grib_context_free(c, i_);
if (j_) grib_context_free(c, j_);
if (k_) grib_context_free(c, k_);
if (distances_) grib_context_free(c, distances_);
if (values_) grib_context_free(c, values_);
return Nearest::destroy(); return Nearest::destroy();
} }

View File

@ -25,6 +25,18 @@ protected:
int cargs_ = 0; int cargs_ = 0;
const char* values_key_ = nullptr; const char* values_key_ = nullptr;
double* lats_ = nullptr;
int lats_count_ = 0;
double* lons_ = nullptr;
int lons_count_ = 0;
double* distances_ = nullptr;
size_t* k_ = nullptr;
size_t* i_ = nullptr;
size_t* j_ = nullptr;
const char* Ni_ = nullptr;
const char* Nj_ = nullptr;
private: private:
const char* radius_ = nullptr; const char* radius_ = nullptr;
}; };

View File

@ -23,8 +23,8 @@ int Healpix::init(grib_handle* h, grib_arguments* args)
Ni_ = grib_arguments_get_name(h, args, cargs_++); Ni_ = grib_arguments_get_name(h, args, cargs_++);
Nj_ = grib_arguments_get_name(h, args, cargs_++); Nj_ = grib_arguments_get_name(h, args, cargs_++);
i_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
return ret; return ret;
} }
@ -48,18 +48,4 @@ int Healpix::find(grib_handle* h,
values, distances, indexes, len); values, distances, indexes, len);
} }
int Healpix::destroy()
{
grib_context* c = grib_context_get_default();
if (lats_) grib_context_free(c, lats_);
if (lons_) grib_context_free(c, lons_);
if (i_) grib_context_free(c, i_);
if (j_) grib_context_free(c, j_);
if (k_) grib_context_free(c, k_);
if (distances_) grib_context_free(c, distances_);
return GRIB_SUCCESS;
}
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new Healpix(); }; Nearest* create() override { return new Healpix(); };
int init(grib_handle*, grib_arguments*) override; int init(grib_handle*, grib_arguments*) override;
int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override; int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override;
int destroy() override;
private:
double* lats_ = nullptr;
int lats_count_ = 0;
double* lons_ = nullptr;
int lons_count_ = 0;
double* distances_ = nullptr;
int* k_ = nullptr;
int* i_ = nullptr;
int* j_ = nullptr;
const char* Ni_ = nullptr;
const char* Nj_ = nullptr;
}; };
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -23,8 +23,9 @@ int LambertAzimuthalEqualArea::init(grib_handle* h, grib_arguments* args)
Ni_ = grib_arguments_get_name(h, args, cargs_++); Ni_ = grib_arguments_get_name(h, args, cargs_++);
Nj_ = grib_arguments_get_name(h, args, cargs_++); Nj_ = grib_arguments_get_name(h, args, cargs_++);
i_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
return ret; return ret;
} }
@ -47,18 +48,4 @@ int LambertAzimuthalEqualArea::find(grib_handle* h,
values, distances, indexes, len); values, distances, indexes, len);
} }
int LambertAzimuthalEqualArea::destroy()
{
grib_context* c = grib_context_get_default();
if (lats_) grib_context_free(c, lats_);
if (lons_) grib_context_free(c, lons_);
if (i_) grib_context_free(c, i_);
if (j_) grib_context_free(c, j_);
if (k_) grib_context_free(c, k_);
if (distances_) grib_context_free(c, distances_);
return Gen::destroy();
}
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new LambertAzimuthalEqualArea(); } Nearest* create() override { return new LambertAzimuthalEqualArea(); }
int init(grib_handle*, grib_arguments*) override; int init(grib_handle*, grib_arguments*) override;
int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override; int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override;
int destroy() override;
private:
double* lats_ = nullptr;
int lats_count_ = 0;
double* lons_ = nullptr;
int lons_count_ = 0;
double* distances_ = nullptr;
int* k_ = nullptr;
int* i_ = nullptr;
int* j_ = nullptr;
const char* Ni_ = nullptr;
const char* Nj_ = nullptr;
}; };
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -23,8 +23,8 @@ int LambertConformal::init(grib_handle* h, grib_arguments* args)
Ni_ = grib_arguments_get_name(h, args, cargs_++); Ni_ = grib_arguments_get_name(h, args, cargs_++);
Nj_ = grib_arguments_get_name(h, args, cargs_++); Nj_ = grib_arguments_get_name(h, args, cargs_++);
i_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
return ret; return ret;
} }
@ -48,18 +48,4 @@ int LambertConformal::find(grib_handle* h,
values, distances, indexes, len); values, distances, indexes, len);
} }
int LambertConformal::destroy()
{
grib_context* c = grib_context_get_default();
if (lats_) grib_context_free(c, lats_);
if (lons_) grib_context_free(c, lons_);
if (i_) grib_context_free(c, i_);
if (j_) grib_context_free(c, j_);
if (k_) grib_context_free(c, k_);
if (distances_) grib_context_free(c, distances_);
return Gen::destroy();
}
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new LambertConformal(); }; Nearest* create() override { return new LambertConformal(); };
int init(grib_handle*, grib_arguments*) override; int init(grib_handle*, grib_arguments*) override;
int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override; int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override;
int destroy() override;
private:
double* lats_ = nullptr;
int lats_count_ = 0;
double* lons_ = nullptr;
int lons_count_ = 0;
double* distances_ = nullptr;
int* k_ = nullptr;
int* i_ = nullptr;
int* j_ = nullptr;
const char* Ni_ = nullptr;
const char* Nj_ = nullptr;
}; };
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -227,6 +227,7 @@ int LatlonReduced::find_global(grib_handle* h,
if (!distances_) if (!distances_)
return GRIB_OUT_OF_MEMORY; return GRIB_OUT_OF_MEMORY;
//void grib_binary_search(const double xx[], const size_t n, double x, size_t* ju, size_t* jl)
grib_binary_search(lats_, ilat - 1, inlat, grib_binary_search(lats_, ilat - 1, inlat,
&(j_[0]), &(j_[1])); &(j_[0]), &(j_[1]));
@ -340,21 +341,4 @@ int LatlonReduced::find_global(grib_handle* h,
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }
int LatlonReduced::destroy()
{
grib_context* c = grib_context_get_default();
if (lats_)
grib_context_free(c, lats_);
if (lons_)
grib_context_free(c, lons_);
if (j_)
grib_context_free(c, j_);
if (k_)
grib_context_free(c, k_);
if (distances_)
grib_context_free(c, distances_);
return Gen::destroy();
}
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -22,21 +22,12 @@ public:
Nearest* create() override { return new LatlonReduced(); } Nearest* create() override { return new LatlonReduced(); }
int init(grib_handle*, grib_arguments*) override; int init(grib_handle*, grib_arguments*) override;
int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override; int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override;
int destroy() override;
private: private:
double* lats_ = nullptr;
int lats_count_ = 0;
double* lons_ = nullptr;
double* distances_ = nullptr;
size_t* k_ = nullptr;
size_t* j_ = nullptr;
const char* Nj_ = nullptr;
const char* pl_ = nullptr; const char* pl_ = nullptr;
const char* lonFirst_ = nullptr; const char* lonFirst_ = nullptr;
const char* lonLast_ = nullptr; const char* lonLast_ = nullptr;
int find_global(grib_handle*, int find_global(grib_handle*,
double, double, unsigned long, double, double, unsigned long,
double*, double*, double*, double*, double*, double*,

View File

@ -23,10 +23,9 @@ int Mercator::init(grib_handle* h, grib_arguments* args)
Ni_ = grib_arguments_get_name(h, args, cargs_++); Ni_ = grib_arguments_get_name(h, args, cargs_++);
Nj_ = grib_arguments_get_name(h, args, cargs_++); Nj_ = grib_arguments_get_name(h, args, cargs_++);
lats_ = lons_ = distances_ = NULL; i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
lats_count_ = lons_count_ = 0; j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
i_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int));
return ret; return ret;
} }
@ -49,17 +48,4 @@ int Mercator::find(grib_handle* h,
values, distances, indexes, len); values, distances, indexes, len);
} }
int Mercator::destroy()
{
grib_context* c = grib_context_get_default();
if (lats_) grib_context_free(c, lats_);
if (lons_) grib_context_free(c, lons_);
if (i_) grib_context_free(c, i_);
if (j_) grib_context_free(c, j_);
if (k_) grib_context_free(c, k_);
if (distances_) grib_context_free(c, distances_);
return GRIB_SUCCESS;
}
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new Mercator(); }; Nearest* create() override { return new Mercator(); };
int init(grib_handle*, grib_arguments*) override; int init(grib_handle*, grib_arguments*) override;
int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override; int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override;
int destroy() override;
private:
double* lats_ = nullptr;
int lats_count_ = 0;
double* lons_ = nullptr;
int lons_count_ = 0;
double* distances_ = nullptr;
int* k_ = nullptr;
int* i_ = nullptr;
int* j_ = nullptr;
const char* Ni_ = nullptr;
const char* Nj_ = nullptr;
}; };
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -23,8 +23,9 @@ int PolarStereographic::init(grib_handle* h, grib_arguments* args)
Ni_ = grib_arguments_get_name(h, args, cargs_++); Ni_ = grib_arguments_get_name(h, args, cargs_++);
Nj_ = grib_arguments_get_name(h, args, cargs_++); Nj_ = grib_arguments_get_name(h, args, cargs_++);
i_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
return ret; return ret;
} }
@ -47,17 +48,4 @@ int PolarStereographic::find(grib_handle* h,
values, distances, indexes, len); values, distances, indexes, len);
} }
int PolarStereographic::destroy()
{
grib_context* c = grib_context_get_default();
if (lats_) grib_context_free(c, lats_);
if (lons_) grib_context_free(c, lons_);
if (i_) grib_context_free(c, i_);
if (j_) grib_context_free(c, j_);
if (k_) grib_context_free(c, k_);
if (distances_) grib_context_free(c, distances_);
return GRIB_SUCCESS;
}
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new PolarStereographic(); } Nearest* create() override { return new PolarStereographic(); }
int init(grib_handle*, grib_arguments*) override; int init(grib_handle*, grib_arguments*) override;
int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override; int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override;
int destroy() override;
private:
double* lats_ = nullptr;
int lats_count_ = 0;
double* lons_ = nullptr;
int lons_count_ = 0;
double* distances_ = nullptr;
int* k_ = nullptr;
int* i_ = nullptr;
int* j_ = nullptr;
const char* Ni_ = nullptr;
const char* Nj_ = nullptr;
}; };
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -433,17 +433,4 @@ int Reduced::find_global(grib_handle* h,
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }
int Reduced::destroy()
{
grib_context* c = grib_context_get_default();
if (lats_) grib_context_free(c, lats_);
if (lons_) grib_context_free(c, lons_);
if (j_) grib_context_free(c, j_);
if (k_) grib_context_free(c, k_);
if (distances_) grib_context_free(c, distances_);
return GRIB_SUCCESS;
}
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -22,16 +22,8 @@ public:
Nearest* create() override { return new Reduced(); } Nearest* create() override { return new Reduced(); }
int init(grib_handle*, grib_arguments*) override; int init(grib_handle*, grib_arguments*) override;
int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override; int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override;
int destroy() override;
private: private:
double* lats_ = nullptr;
int lats_count_ = 0;
double* lons_ = nullptr;
double* distances_ = nullptr;
size_t* k_ = nullptr;
size_t* j_ = nullptr;
const char* Nj_ = nullptr;
const char* pl_ = nullptr; const char* pl_ = nullptr;
long global_ = 0.0; long global_ = 0.0;
double lon_first_ = 0.0; double lon_first_ = 0.0;

View File

@ -285,18 +285,4 @@ int Regular::find(grib_handle* h,
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }
int Regular::destroy()
{
grib_context* c = grib_context_get_default();
if (lats_) grib_context_free(c, lats_);
if (lons_) grib_context_free(c, lons_);
if (i_) grib_context_free(c, i_);
if (j_) grib_context_free(c, j_);
if (k_) grib_context_free(c, k_);
if (distances_) grib_context_free(c, distances_);
return Gen::destroy();
}
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new Regular(); } Nearest* create() override { return new Regular(); }
int init(grib_handle*, grib_arguments*) override; int init(grib_handle*, grib_arguments*) override;
int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override; int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override;
int destroy() override;
private:
double* lats_ = nullptr;
int lats_count_ = 0;
double* lons_ = nullptr;
int lons_count_ = 0;
double* distances_ = nullptr;
size_t* k_ = nullptr;
size_t* i_ = nullptr;
size_t* j_ = nullptr;
const char* Ni_ = nullptr;
const char* Nj_ = nullptr;
}; };
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -23,8 +23,8 @@ int SpaceView::init(grib_handle* h, grib_arguments* args)
Ni_ = grib_arguments_get_name(h, args, cargs_++); Ni_ = grib_arguments_get_name(h, args, cargs_++);
Nj_ = grib_arguments_get_name(h, args, cargs_++); Nj_ = grib_arguments_get_name(h, args, cargs_++);
i_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
return ret; return ret;
} }
@ -48,18 +48,4 @@ int SpaceView::find(grib_handle* h,
values, distances, indexes, len); values, distances, indexes, len);
} }
int SpaceView::destroy()
{
grib_context* c = grib_context_get_default();
if (lats_) grib_context_free(c, lats_);
if (lons_) grib_context_free(c, lons_);
if (i_) grib_context_free(c, i_);
if (j_) grib_context_free(c, j_);
if (k_) grib_context_free(c, k_);
if (distances_) grib_context_free(c, distances_);
return Gen::destroy();
}
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new SpaceView(); } Nearest* create() override { return new SpaceView(); }
int init(grib_handle*, grib_arguments*) override; int init(grib_handle*, grib_arguments*) override;
int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override; int find(grib_handle*, double, double, unsigned long, double*, double*, double*, double*, int*, size_t*) override;
int destroy() override;
private:
double* lats_ = nullptr;
int lats_count_ = 0;
double* lons_ = nullptr;
int lons_count_ = 0;
double* distances_ = nullptr;
int* k_ = nullptr;
int* i_ = nullptr;
int* j_ = nullptr;
const char* Ni_ = nullptr;
const char* Nj_ = nullptr;
}; };
} // namespace eccodes::geo_nearest } // namespace eccodes::geo_nearest