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()
{
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();
}

View File

@ -25,6 +25,18 @@ protected:
int cargs_ = 0;
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:
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_++);
Nj_ = grib_arguments_get_name(h, args, cargs_++);
i_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int));
i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
return ret;
}
@ -48,18 +48,4 @@ int Healpix::find(grib_handle* h,
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

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new Healpix(); };
int init(grib_handle*, grib_arguments*) 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

View File

@ -23,8 +23,9 @@ int LambertAzimuthalEqualArea::init(grib_handle* h, grib_arguments* args)
Ni_ = 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));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int));
i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
return ret;
}
@ -47,18 +48,4 @@ int LambertAzimuthalEqualArea::find(grib_handle* h,
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

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new LambertAzimuthalEqualArea(); }
int init(grib_handle*, grib_arguments*) 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

View File

@ -23,8 +23,8 @@ int LambertConformal::init(grib_handle* h, grib_arguments* args)
Ni_ = 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));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int));
i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
return ret;
}
@ -48,18 +48,4 @@ int LambertConformal::find(grib_handle* h,
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

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new LambertConformal(); };
int init(grib_handle*, grib_arguments*) 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

View File

@ -21,11 +21,11 @@ int LatlonReduced::init(grib_handle* h, grib_arguments* args)
if ((ret = Gen::init(h, args) != GRIB_SUCCESS))
return ret;
Nj_ = grib_arguments_get_name(h, args, cargs_++);
pl_ = grib_arguments_get_name(h, args, cargs_++);
lonFirst_ = grib_arguments_get_name(h, args, cargs_++);
lonLast_ = grib_arguments_get_name(h, args, cargs_++);
j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
Nj_ = grib_arguments_get_name(h, args, cargs_++);
pl_ = grib_arguments_get_name(h, args, cargs_++);
lonFirst_ = grib_arguments_get_name(h, args, cargs_++);
lonLast_ = grib_arguments_get_name(h, args, cargs_++);
j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
if (!j_)
return GRIB_OUT_OF_MEMORY;
k_ = (size_t*)grib_context_malloc(h->context, 4 * sizeof(size_t));
@ -227,6 +227,7 @@ int LatlonReduced::find_global(grib_handle* h,
if (!distances_)
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,
&(j_[0]), &(j_[1]));
@ -340,21 +341,4 @@ int LatlonReduced::find_global(grib_handle* h,
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

View File

@ -22,21 +22,12 @@ public:
Nearest* create() override { return new LatlonReduced(); }
int init(grib_handle*, grib_arguments*) 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;
double* distances_ = nullptr;
size_t* k_ = nullptr;
size_t* j_ = nullptr;
const char* Nj_ = nullptr;
const char* pl_ = nullptr;
const char* lonFirst_ = nullptr;
const char* lonLast_ = nullptr;
int find_global(grib_handle*,
double, double, unsigned long,
double*, double*, double*,

View File

@ -21,12 +21,11 @@ int Mercator::init(grib_handle* h, grib_arguments* args)
if ((ret = Gen::init(h, args) != GRIB_SUCCESS))
return ret;
Ni_ = grib_arguments_get_name(h, args, cargs_++);
Nj_ = grib_arguments_get_name(h, args, cargs_++);
lats_ = lons_ = distances_ = NULL;
lats_count_ = lons_count_ = 0;
i_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int));
Ni_ = grib_arguments_get_name(h, args, cargs_++);
Nj_ = grib_arguments_get_name(h, args, cargs_++);
i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
return ret;
}
@ -49,17 +48,4 @@ int Mercator::find(grib_handle* h,
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

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new Mercator(); };
int init(grib_handle*, grib_arguments*) 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

View File

@ -21,10 +21,11 @@ int PolarStereographic::init(grib_handle* h, grib_arguments* args)
if ((ret = Gen::init(h, args) != GRIB_SUCCESS))
return ret;
Ni_ = 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));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int));
Ni_ = grib_arguments_get_name(h, args, cargs_++);
Nj_ = grib_arguments_get_name(h, args, cargs_++);
i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
return ret;
}
@ -47,17 +48,4 @@ int PolarStereographic::find(grib_handle* h,
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

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new PolarStereographic(); }
int init(grib_handle*, grib_arguments*) 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

View File

@ -23,11 +23,11 @@ int Reduced::init(grib_handle* h, grib_arguments* args)
if ((ret = Gen::init(h, args) != GRIB_SUCCESS))
return ret;
Nj_ = grib_arguments_get_name(h, args, cargs_++);
pl_ = grib_arguments_get_name(h, args, cargs_++);
j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
legacy_ = -1;
rotated_ = -1;
Nj_ = grib_arguments_get_name(h, args, cargs_++);
pl_ = grib_arguments_get_name(h, args, cargs_++);
j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
legacy_ = -1;
rotated_ = -1;
if (!j_)
return GRIB_OUT_OF_MEMORY;
k_ = (size_t*)grib_context_malloc(h->context, NUM_NEIGHBOURS * sizeof(size_t));
@ -433,17 +433,4 @@ int Reduced::find_global(grib_handle* h,
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

View File

@ -22,16 +22,8 @@ public:
Nearest* create() override { return new Reduced(); }
int init(grib_handle*, grib_arguments*) 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;
double* distances_ = nullptr;
size_t* k_ = nullptr;
size_t* j_ = nullptr;
const char* Nj_ = nullptr;
const char* pl_ = nullptr;
long global_ = 0.0;
double lon_first_ = 0.0;

View File

@ -285,18 +285,4 @@ int Regular::find(grib_handle* h,
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

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new Regular(); }
int init(grib_handle*, grib_arguments*) 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

View File

@ -21,10 +21,10 @@ int SpaceView::init(grib_handle* h, grib_arguments* args)
if ((ret = Gen::init(h, args) != GRIB_SUCCESS))
return ret;
Ni_ = 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));
j_ = (int*)grib_context_malloc(h->context, 2 * sizeof(int));
Ni_ = grib_arguments_get_name(h, args, cargs_++);
Nj_ = grib_arguments_get_name(h, args, cargs_++);
i_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
j_ = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t));
return ret;
}
@ -48,18 +48,4 @@ int SpaceView::find(grib_handle* h,
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

View File

@ -22,19 +22,6 @@ public:
Nearest* create() override { return new SpaceView(); }
int init(grib_handle*, grib_arguments*) 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