Skip to content

Commit

Permalink
clean VINE3D
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Jul 15, 2024
1 parent 2bdeacb commit 73b9125
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 59 deletions.
101 changes: 50 additions & 51 deletions bin/VINE3D/vine3DProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void Vine3DProject::initializeVine3DProject()

cultivar.clear();
trainingSystems.clear();
inputModelCases.clear();
modelCases.clear();
fieldBook.clear();

Expand Down Expand Up @@ -411,64 +412,66 @@ int Vine3DProject::queryFieldPoint(double x, double y)
}


int getCaseIndexFromId(int caseId, std::vector <Crit3DModelCase> modelCases)
int Vine3DProject::getCaseIndexFromId(int caseId)
{
if (modelCases.size() == 0)
return NODATA;

int i;
for (i=0; i < modelCases.size(); i++)
for (int i=0; i < modelCases.size(); i++)
if (caseId == modelCases[i].id)
return i;

//default value
if (i == modelCases.size() - 1)
return 0;

return NODATA;
// default value
return 0;
}

void modelCaseIndexMapIndexFromId(gis::Crit3DRasterGrid* myGrid, std::vector <Crit3DModelCase> modelCases)

void Vine3DProject::setModelCasesMap(gis::Crit3DRasterGrid &myGrid)
{
int fieldId, fieldIndex;

// transform from id to index
for (int myRow = 0; myRow < myGrid->header->nrRows; myRow++)
for (int myCol = 0; myCol < myGrid->header->nrCols; myCol++)
for (int row = 0; row < myGrid.header->nrRows; row++)
{
for (int col = 0; col < myGrid.header->nrCols; col++)
{
fieldId = int(myGrid->value[myRow][myCol]);
if (fieldId != int(myGrid->header->flag))
fieldId = int(myGrid.value[row][col]);
if (! isEqual(fieldId, myGrid.header->flag))
{
fieldIndex = getCaseIndexFromId(fieldId, modelCases);
fieldIndex = getCaseIndexFromId(fieldId);
if (fieldIndex != NODATA)
myGrid->value[myRow][myCol] = fieldIndex;
{
myGrid.value[row][col] = fieldIndex;
}
}
}
}
}

bool Vine3DProject::loadFieldMap(QString myFileName)

bool Vine3DProject::loadFieldMap(QString mapFileName)
{
this->logInfo ("Read fields map...");
logInfo ("Read fields map...");

std::string fn = myFileName.left(myFileName.length()-4).toStdString();
gis::Crit3DRasterGrid myGrid;
std::string fn = mapFileName.left(mapFileName.length()-4).toStdString();
gis::Crit3DRasterGrid inputGrid;

std::string errorStr;
if (! gis::readEsriGrid(fn, &(myGrid), errorStr))
if (! gis::readEsriGrid(fn, &(inputGrid), errorStr))
{
this->errorString = "Load fields map failed:\n" + myFileName + "\n" + QString::fromStdString(errorStr);
errorString = "Load fields map failed:\n" + mapFileName + "\n" + QString::fromStdString(errorStr);
logError();
return (false);
return false;
}

// compute prevailing map
landUseMap.initializeGrid(DEM);
gis::prevailingMap(myGrid, &(landUseMap));
gis::prevailingMap(inputGrid, &(landUseMap));
gis::updateMinMaxRasterGrid(&(landUseMap));

modelCaseIndexMapIndexFromId(&landUseMap, modelCases);
setModelCasesMap(landUseMap);

this->logInfo ("Field map = " + myFileName);
logInfo ("Field map = " + mapFileName);
return true;
}

Expand Down Expand Up @@ -527,7 +530,7 @@ bool Vine3DProject::loadFieldsProperties()
{
logInfo ("Read fields properties...");

int fieldIndex, idField, vineIndex, trainingIndex;
int idField, vineIndex, trainingIndex;
float maxLaiGrass, maxIrrigationRate;
Crit3DLanduse landuse;

Expand All @@ -542,38 +545,37 @@ bool Vine3DProject::loadFieldsProperties()
}
else if (! myQuery.next())
{
this->errorString = "Missing default field (index = 0) in fields table.";
errorString = "Missing default field (index = 0) in fields table.";
return false;
}

// READ PROPERTIES
myQuery.prepare("SELECT id_field, landuse, id_cultivar, id_training_system, max_lai_grass, irrigation_max_rate FROM fields ORDER BY id_field");
if (! myQuery.exec())
{
errorString = "Error reading fields table.\n" + myQuery.lastError().text();
errorString = "Error reading fields table:\n" + myQuery.lastError().text();
return false;
}

fieldIndex = 0;
Crit3DModelCase myCase;

while (myQuery.next())
{
if (readFieldQuery(myQuery, idField, landuse, vineIndex, trainingIndex, maxLaiGrass, maxIrrigationRate))
{
myCase.id = idField;
myCase.landuse = landuse;
myCase.cultivar = &(this->cultivar[vineIndex]);
myCase.maxLAIGrass = maxLaiGrass;
myCase.maxIrrigationRate = maxIrrigationRate;
Crit3DModelCase currentCase;

currentCase.id = idField;
currentCase.landuse = landuse;
currentCase.cultivar = &(cultivar[vineIndex]);
currentCase.maxLAIGrass = maxLaiGrass;
currentCase.maxIrrigationRate = maxIrrigationRate;

float density = 1 / (trainingSystems[trainingIndex].rowDistance * trainingSystems[trainingIndex].plantDistance);

myCase.trainingSystem = trainingIndex;
myCase.plantDensity = density;
myCase.shootsPerPlant = this->trainingSystems[trainingIndex].shootsPerPlant;
currentCase.trainingSystem = trainingIndex;
currentCase.plantDensity = density;
currentCase.shootsPerPlant = trainingSystems[trainingIndex].shootsPerPlant;

modelCases.push_back(myCase);
inputModelCases.push_back(currentCase);
}
else
{
Expand Down Expand Up @@ -1209,9 +1211,6 @@ bool Vine3DProject::loadStates(QDate myDate)
{
QString statePath = getProjectPath() + "states/" + myDate.toString("yyyy/MM/dd/");

//if (!loadPlantState(this, tartaricAcidVar, myDate, myStatePath)) return(false);
//if (!loadPlantState(this, pHBerryVar, myDate, myStatePath)) return(false);
//if (!loadPlantState(this, fruitBiomassIndexVar, myDate, myStatePath)) return(false);
if (!loadPlantState(this, daysAfterBloomVar, myDate, statePath)) return(false);
if (!loadPlantState(this, cumulatedBiomassVar, myDate, statePath)) return(false);
if (!loadPlantState(this, fruitBiomassVar, myDate, statePath)) return(false);
Expand All @@ -1223,14 +1222,14 @@ bool Vine3DProject::loadStates(QDate myDate)
if (!loadPlantState(this, stageVar, myDate, statePath)) return(false);
if (!loadPlantState(this, leafAreaIndexVar, myDate, statePath)) return(false);

if (!loadPlantState(this, isHarvestedVar, myDate, statePath))
if (! loadPlantState(this, isHarvestedVar, myDate, statePath))
{
this->statePlantMaps->isHarvestedMap->setConstantValueWithBase(0, DEM);
statePlantMaps->isHarvestedMap->setConstantValueWithBase(0, DEM);
}
if (!loadPlantState(this, fruitBiomassIndexVar, myDate, statePath))
if (! loadPlantState(this, fruitBiomassIndexVar, myDate, statePath))
{
//defualt= chardonnay
this->statePlantMaps->fruitBiomassIndexMap->setConstantValueWithBase(this->modelCases[1].cultivar->parameterBindiMiglietta.fruitBiomassSlope, DEM);
statePlantMaps->fruitBiomassIndexMap->setConstantValueWithBase(inputModelCases[1].cultivar->parameterBindiMiglietta.fruitBiomassSlope, DEM);
}

//problema: mancano nei precedenti stati
Expand All @@ -1242,10 +1241,11 @@ bool Vine3DProject::loadStates(QDate myDate)
loadPlantState(this, powderyCurrentColoniesVar, myDate, statePath);
loadPlantState(this, powderySporulatingColoniesVar, myDate, statePath);

if (!loadWaterBalanceState(this, myDate, statePath, waterMatricPotential)) return false;
if (! loadWaterBalanceState(this, myDate, statePath, waterMatricPotential)) return false;

this->logInfo("Load state: " + myDate.toString("yyyy-MM-dd"));
return(true);
logInfo("Load state: " + myDate.toString("yyyy-MM-dd"));

return true;
}


Expand Down Expand Up @@ -1498,7 +1498,6 @@ bool Vine3DProject::initializeGrapevine()
double depthModeRootDensity; // [m] depth of mode of root density
double depthMeanRootDensity; // [m] depth of mean of root density


for (int i = 0; i < modelCases.size(); i++)
{
// TODO
Expand Down
16 changes: 8 additions & 8 deletions bin/VINE3D/vine3DProject.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
std::vector <TVineCultivar> cultivar;
std::vector <TtrainingSystem> trainingSystems;

std::vector <Crit3DModelCase> inputModelCases;
std::vector <Crit3DModelCase> modelCases;
std::vector <TVine3DOperation> fieldBook;

Expand All @@ -87,6 +88,9 @@

Vine3DProject();

void initializeVine3DProject();
void clearVine3DProject();

void loadVine3DSettings();

bool loadFieldsProperties();
Expand All @@ -98,16 +102,16 @@
float findSoilMaxDepth();
soil::Crit3DSoil *loadHorizons(int idSoil, QString soil_code);

void initializeVine3DProject();
void clearVine3DProject();

bool loadVine3DProject(QString projectFileName);
bool openVine3DDatabase(QString fileName);

bool initializeGrapevine();

int getCaseIndexFromId(int caseId);
void setModelCasesMap(gis::Crit3DRasterGrid &myGrid);

int queryFieldPoint(double x, double y);
bool loadFieldMap(QString myFileName);
bool loadFieldMap(QString mapFileName);

bool readFieldQuery(QSqlQuery &myQuery, int &idField, Crit3DLanduse &landuse, int &vineIndex, int &trainingIndex, float &maxLaiGrass, float &maxIrrigationRate);
bool setField(int fieldIndex, int fieldId, Crit3DLanduse landuse, int soilIndex, int vineIndex, int trainingIndex,
Expand All @@ -123,10 +127,6 @@
bool isMeteoDataLoaded(const Crit3DTime& myTimeIni, const Crit3DTime& myTimeFin);
float meteoDataConsistency(meteoVariable myVar, const Crit3DTime& myTimeIni, const Crit3DTime& myTimeFin);

//bool loadObsDataSubHourly(int indexPoint, meteoVariable myVar, QDateTime d1, QDateTime d2, QString tableName);
//bool loadObsDataHourly(int indexPoint, QDate d1, QDate d2, QString tableName, bool useAggrCodes);
//bool loadObsDataFilled(QDateTime firstTime, QDateTime lastTime);
//bool loadObsDataAllPoints(QDate d1, QDate d2, bool showInfo);
void findVine3DLastMeteoDate();

bool loadStates(QDate myDate);
Expand Down

0 comments on commit 73b9125

Please sign in to comment.