Skip to content
Snippets Groups Projects

Draft: Removed almost all operator dependend sections in FC.cc, added init functions...

Closed Oliver Heidmann requested to merge m300433/FC_streamlining into develop
+ 199
169
@@ -59,16 +59,27 @@ class ModuleFC
size_t nlon = 0, nlat = 0;
int ntr = 0;
int nsp = 0, nfc = 0;
int gridIDsp = -1, gridIDgp = -1, gridIDfc = -1;
int operatorID;
CdoStreamID streamID1;
CdoStreamID streamID2;
int vlistID1;
int vlistID2;
int taxisID1;
int taxisID2;
int nvars;
/* Only used in grid2fourier where it is set to 2
* Introduced to remove if statement, further rework required
*/
int grid_size_multiplier = 1;
size_t gridsizemax;
bool dataIsUnchanged;
Varray<double> array1;
@@ -81,221 +92,249 @@ class ModuleFC
int FC2SP, SP2FC, FC2GP, GP2FC, GRID2FOURIER, FOURIER2GRID;
public:
std::pair<std::function<void()>, std::function<void()>> init_run_pair;
void
init(void *process)
init_sp2fc()
{
cdo_initialize(process);
if (gridIDsp == -1) cdo_warning("No spectral data found!");
operator_check_argc(0);
gridID1 = gridIDsp;
dataIsUnchanged = data_is_unchanged();
if (gridID1 != -1)
{
ntr = gridInqTrunc(gridID1);
nlat = ntr_to_nlat(ntr);
FC2SP = cdo_operator_add("fc2sp", 0, 0, nullptr);
SP2FC = cdo_operator_add("sp2fc", 0, 0, nullptr);
FC2GP = cdo_operator_add("fc2gp", 0, 0, nullptr);
GP2FC = cdo_operator_add("gp2fc", 0, 0, nullptr);
GRID2FOURIER = cdo_operator_add("grid2fourier", 1, 0, nullptr);
FOURIER2GRID = cdo_operator_add("fourier2grid", 1, 0, nullptr);
if (gridIDfc != -1)
{
if (ntr != gridInqTrunc(gridIDfc)) gridIDfc = -1;
}
operatorID = cdo_operator_id();
auto operfunc = cdo_operator_f1(operatorID);
if (gridIDfc == -1)
{
nfc = 2 * nlat * (ntr + 1);
gridIDfc = gridCreate(GRID_FOURIER, nfc);
gridDefTrunc(gridIDfc, ntr);
}
streamID1 = cdo_open_read(0);
gridID2 = gridIDfc;
nlon = 2 * nlat;
}
vlistID1 = cdo_stream_inq_vlist(streamID1);
auto vlistID2 = vlistDuplicate(vlistID1);
spTrans.init(nlon, nlat, ntr, PolFlag::SP2FC);
if (gridID1 != -1) vlistChangeGrid(vlistID2, gridID1, gridID2);
}
taxisID1 = vlistInqTaxis(vlistID1);
taxisID2 = taxisDuplicate(taxisID1);
vlistDefTaxis(vlistID2, taxisID2);
void
init_fc2sp()
{
if (gridIDfc == -1) cdo_warning("No fourier data found!");
int gridIDsp = -1, gridIDgp = -1, gridIDfc = -1;
if (operfunc == 0)
{
gridIDsp = vlist_get_first_spectral_grid(vlistID1);
gridIDgp = vlist_get_first_gaussian_grid(vlistID1);
gridIDfc = vlist_get_first_fourier_grid(vlistID1);
}
gridID1 = gridIDfc;
// define output grid
if (operatorID == FC2SP)
if (gridID1 != -1)
{
if (gridIDfc == -1) cdo_warning("No fourier data found!");
nfc = gridInqSize(gridID1);
ntr = gridInqTrunc(gridID1);
nlat = nfc_to_nlat(nfc, ntr);
gridID1 = gridIDfc;
if (gridIDsp != -1)
if (ntr != gridInqTrunc(gridIDsp)) gridIDsp = -1;
if (gridID1 != -1)
if (gridIDsp == -1)
{
nfc = gridInqSize(gridID1);
ntr = gridInqTrunc(gridID1);
nlat = nfc_to_nlat(nfc, ntr);
nsp = (ntr + 1) * (ntr + 2);
gridIDsp = gridCreate(GRID_SPECTRAL, nsp);
gridDefTrunc(gridIDsp, ntr);
gridDefComplexPacking(gridIDsp, 1);
}
if (gridIDsp != -1)
if (ntr != gridInqTrunc(gridIDsp)) gridIDsp = -1;
gridID2 = gridIDsp;
nlon = 2 * nlat;
ntr = gridInqTrunc(gridID2);
}
if (gridIDsp == -1)
{
nsp = (ntr + 1) * (ntr + 2);
gridIDsp = gridCreate(GRID_SPECTRAL, nsp);
gridDefTrunc(gridIDsp, ntr);
gridDefComplexPacking(gridIDsp, 1);
}
spTrans.init(nlon, nlat, ntr, PolFlag::FC2SP);
if (gridID1 != -1) vlistChangeGrid(vlistID2, gridID1, gridID2);
}
void
init_gp2fc()
{
if (gridIDgp == -1) cdo_warning("No Gaussian grid data found!");
gridID2 = gridIDsp;
nlon = 2 * nlat;
ntr = gridInqTrunc(gridID2);
}
}
else if (operatorID == SP2FC)
gridID1 = gridIDgp;
if (gridID1 != -1)
{
if (gridIDsp == -1) cdo_warning("No spectral data found!");
nlon = gridInqXsize(gridID1);
nlat = gridInqYsize(gridID1);
ntr = nlat_to_ntr(nlat);
gridID1 = gridIDsp;
if (gridIDfc != -1)
if (ntr != gridInqTrunc(gridIDfc)) gridIDfc = -1;
if (gridID1 != -1)
if (gridIDfc == -1)
{
ntr = gridInqTrunc(gridID1);
nlat = ntr_to_nlat(ntr);
nfc = 2 * nlat * (ntr + 1);
gridIDfc = gridCreate(GRID_FOURIER, nfc);
gridDefTrunc(gridIDfc, ntr);
}
if (gridIDfc != -1)
{
if (ntr != gridInqTrunc(gridIDfc)) gridIDfc = -1;
}
gridID2 = gridIDfc;
}
if (gridIDfc == -1)
{
nfc = 2 * nlat * (ntr + 1);
gridIDfc = gridCreate(GRID_FOURIER, nfc);
gridDefTrunc(gridIDfc, ntr);
}
fcTrans.init(nlon, nlat, ntr);
if (gridID1 != -1) vlistChangeGrid(vlistID2, gridID1, gridID2);
}
gridID2 = gridIDfc;
nlon = 2 * nlat;
}
}
else if (operatorID == GP2FC)
void
init_fc2gp()
{
if (gridIDfc == -1) cdo_warning("No fourier data found!");
gridID1 = gridIDfc;
if (gridID1 != -1)
{
if (gridIDgp == -1) cdo_warning("No Gaussian grid data found!");
nfc = gridInqSize(gridID1);
ntr = gridInqTrunc(gridID1);
nlat = nfc_to_nlat(nfc, ntr);
gridID1 = gridIDgp;
if (gridIDgp != -1)
{
if (nlat != gridInqYsize(gridIDgp)) gridIDgp = -1;
}
if (gridID1 != -1)
if (gridIDgp == -1)
{
nlon = gridInqXsize(gridID1);
nlat = gridInqYsize(gridID1);
ntr = nlat_to_ntr(nlat);
char gridname[20];
std::snprintf(gridname, sizeof(gridname), "t%dgrid", ntr);
if (gridIDfc != -1)
if (ntr != gridInqTrunc(gridIDfc)) gridIDfc = -1;
gridIDgp = grid_from_name(gridname);
}
if (gridIDfc == -1)
{
nfc = 2 * nlat * (ntr + 1);
gridIDfc = gridCreate(GRID_FOURIER, nfc);
gridDefTrunc(gridIDfc, ntr);
}
gridID2 = gridIDgp;
nlon = gridInqXsize(gridID2);
nlat = gridInqYsize(gridID2);
}
gridID2 = gridIDfc;
}
fcTrans.init(nlon, nlat, ntr);
if (gridID1 != -1) vlistChangeGrid(vlistID2, gridID1, gridID2);
}
void
init_fourier2grid()
{
gridID1 = vlistGetFirstReg2DGrid(vlistID1);
if (gridID1 == -1) cdo_warning("No regular 2D data found!");
if (gridID1 != -1)
{
nlon = gridInqXsize(gridID1);
nlat = gridInqYsize(gridID1);
gridID2 = gridID1;
}
else if (operatorID == FC2GP)
for (int varID = 0; varID < nvars; ++varID)
if (vars[varID]) vlistDefVarDatatype(vlistID2, varID, CDI_DATATYPE_FLT32);
gridsizemax *= 2;
}
void
init_grid2fourier()
{
gridID1 = vlistGetFirstReg2DGrid(vlistID1);
if (gridID1 == -1) cdo_warning("No regular 2D data found!");
if (gridID1 != -1)
{
if (gridIDfc == -1) cdo_warning("No fourier data found!");
nlon = gridInqXsize(gridID1);
nlat = gridInqYsize(gridID1);
gridID2 = gridID1;
}
if (gridID1 != -1) vlistChangeGrid(vlistID2, gridID1, gridID2);
gridID1 = gridIDfc;
for (int varID = 0; varID < nvars; ++varID)
if (vars[varID]) vlistDefVarDatatype(vlistID2, varID, CDI_DATATYPE_CPX64);
if (gridID1 != -1)
{
nfc = gridInqSize(gridID1);
ntr = gridInqTrunc(gridID1);
nlat = nfc_to_nlat(nfc, ntr);
grid_size_multiplier = 2;
}
std::map<int, std::pair<std::function<void()>, std::function<void()>>> init_run_map;
if (gridIDgp != -1)
{
if (nlat != gridInqYsize(gridIDgp)) gridIDgp = -1;
}
void
init_operator()
{
init_run_map[operatorID].first();
}
void
run_operator()
{
init_run_map[operatorID].second();
}
if (gridIDgp == -1)
{
char gridname[20];
std::snprintf(gridname, sizeof(gridname), "t%dgrid", ntr);
public:
void
init(void *process)
{
cdo_initialize(process);
gridIDgp = grid_from_name(gridname);
}
operator_check_argc(0);
gridID2 = gridIDgp;
nlon = gridInqXsize(gridID2);
nlat = gridInqYsize(gridID2);
}
}
else if (operatorID == FOURIER2GRID)
{
gridID1 = vlistGetFirstReg2DGrid(vlistID1);
if (gridID1 == -1) cdo_warning("No regular 2D data found!");
if (gridID1 != -1)
{
nlon = gridInqXsize(gridID1);
nlat = gridInqYsize(gridID1);
gridID2 = gridID1;
}
}
else if (operatorID == GRID2FOURIER)
{
gridID1 = vlistGetFirstReg2DGrid(vlistID1);
if (gridID1 == -1) cdo_warning("No regular 2D data found!");
dataIsUnchanged = data_is_unchanged();
if (gridID1 != -1)
{
nlon = gridInqXsize(gridID1);
nlat = gridInqYsize(gridID1);
gridID2 = gridID1;
}
}
FC2SP = cdo_operator_add("fc2sp", 0, 0, nullptr);
SP2FC = cdo_operator_add("sp2fc", 0, 0, nullptr);
FC2GP = cdo_operator_add("fc2gp", 0, 0, nullptr);
GP2FC = cdo_operator_add("gp2fc", 0, 0, nullptr);
GRID2FOURIER = cdo_operator_add("grid2fourier", 1, 0, nullptr);
FOURIER2GRID = cdo_operator_add("fourier2grid", 1, 0, nullptr);
operatorID = cdo_operator_id();
auto operfunc = cdo_operator_f1(operatorID);
init_run_map
= { { FC2SP, { [&]() { init_fc2sp(); }, [&]() { four2spec(spTrans, gridID1, array1, gridID2, array2); } } },
{ SP2FC, { [&]() { init_sp2fc(); }, [&]() { spec2four(spTrans, gridID1, array1, gridID2, array2); } } },
{ GP2FC, { [&]() { init_gp2fc(); }, [&]() { grid2four(fcTrans, gridID1, array1, gridID2, array2); } } },
{ FC2GP, { [&]() { init_fc2gp(); }, [&]() { spec2four(spTrans, gridID1, array1, gridID2, array2); } } },
{ FOURIER2GRID, { [&]() { init_fourier2grid(); }, [&]() { fourier2grid(gridID1, array1, array2); } } },
{ GRID2FOURIER, { [&]() { init_grid2fourier(); }, [&]() { grid2fourier(gridID1, array1, gridID2, array2); } } } };
streamID1 = cdo_open_read(0);
vlistID1 = cdo_stream_inq_vlist(streamID1);
vlistID2 = vlistDuplicate(vlistID1);
taxisID1 = vlistInqTaxis(vlistID1);
taxisID2 = taxisDuplicate(taxisID1);
vlistDefTaxis(vlistID2, taxisID2);
if (operfunc == 0)
{
if (nlon > 0)
{
if (operatorID == GP2FC || operatorID == FC2GP)
fcTrans.init(nlon, nlat, ntr);
else if (operatorID == SP2FC)
spTrans.init(nlon, nlat, ntr, PolFlag::SP2FC);
else if (operatorID == FC2SP)
spTrans.init(nlon, nlat, ntr, PolFlag::FC2SP);
}
gridIDsp = vlist_get_first_spectral_grid(vlistID1);
gridIDgp = vlist_get_first_gaussian_grid(vlistID1);
gridIDfc = vlist_get_first_fourier_grid(vlistID1);
}
// printf("nfc %d, ntr %d, nlat %zu, nlon %zu\n", nfc, ntr, nlat, nlon);
auto nvars = vlistNvars(vlistID2);
nvars = vlistNvars(vlistID2);
vars = std::vector<bool>(nvars);
for (int varID = 0; varID < nvars; ++varID) vars[varID] = gridID1 == vlistInqVarGrid(vlistID1, varID);
gridsizemax = vlistGridsizeMax(vlistID1);
if (gridID1 != -1) vlistChangeGrid(vlistID2, gridID1, gridID2);
if (operatorID == GRID2FOURIER)
{
for (int varID = 0; varID < nvars; ++varID)
if (vars[varID]) vlistDefVarDatatype(vlistID2, varID, CDI_DATATYPE_CPX64);
}
else if (operatorID == FOURIER2GRID)
{
for (int varID = 0; varID < nvars; ++varID)
if (vars[varID]) vlistDefVarDatatype(vlistID2, varID, CDI_DATATYPE_FLT32);
}
init_operator();
streamID2 = cdo_open_write(1);
// printf("nfc %d, ntr %d, nlat %zu, nlon %zu\n", nfc, ntr, nlat, nlon);
streamID2 = cdo_open_write(1);
cdo_def_vlist(streamID2, vlistID2);
auto gridsizemax = vlistGridsizeMax(vlistID1);
if (operatorID == FOURIER2GRID) gridsizemax *= 2;
array1 = Varray<double>(gridsizemax);
//@Uwe when does this happen?
if (gridID2 != -1)
{
auto gridsize = gridInqSize(gridID2);
if (operatorID == GRID2FOURIER) gridsize *= 2;
gridsize *= grid_size_multiplier;
array2.resize(gridsize);
}
}
@@ -323,18 +362,9 @@ public:
if (nmiss) cdo_abort("Missing values unsupported for spectral/fourier data!");
gridID1 = vlistInqVarGrid(vlistID1, varID);
if (operatorID == FC2SP)
four2spec(spTrans, gridID1, array1, gridID2, array2);
else if (operatorID == SP2FC)
spec2four(spTrans, gridID1, array1, gridID2, array2);
else if (operatorID == FC2GP)
four2grid(fcTrans, gridID1, array1, gridID2, array2);
else if (operatorID == GP2FC)
grid2four(fcTrans, gridID1, array1, gridID2, array2);
else if (operatorID == FOURIER2GRID)
fourier2grid(gridID1, array1, array2);
else if (operatorID == GRID2FOURIER)
grid2fourier(gridID1, array1, gridID2, array2);
run_operator();
cdo_def_record(streamID2, varID, levelID);
cdo_write_record(streamID2, array2.data(), nmiss);
}
Loading