Skip to content

Commit

Permalink
Merge branch 'capi'
Browse files Browse the repository at this point in the history
  • Loading branch information
nwagenbrenner committed Oct 1, 2024
2 parents 6db1e93 + 3a90cf3 commit 012eab7
Show file tree
Hide file tree
Showing 16 changed files with 1,382 additions and 85 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ endif(WITH_LCP_CLIENT)
#disable for exception handling in the GUI and CLI
option(C_API "Enable WindNinja C API" OFF)
if(C_API)

add_definitions(-DC_API)

endif(C_API)

# Enable phone home and message server startup
Expand Down Expand Up @@ -354,6 +356,16 @@ option(RUN_CFG_TESTS "Run the cfg file tests in autotest/cfg OFF")
if(BUILD_TESTING)
add_subdirectory(autotest)
endif(BUILD_TESTING)
option(SWIG_PYTHON_MODULE "Install Python modules created by SWIG" OFF)
if(SWIG_PYTHON_MODULE)
add_definitions(-DSWIG_PYTHON_MODULE)
add_subdirectory(swig/python)
endif(SWIG_PYTHON_MODULE)
option (SWIG_JAVA_MODULE "Install Java modules created by SWIG" OFF)
if(SWIG_JAVA_MODULE)
add_definitions(-DSWIG_JAVA_MODULE)
add_subdirectory(swig/java)
endif(SWIG_JAVA_MODULE)

# Package related options and settings.
option(PACKAGE_DEBUG "Show some information about the package" OFF)
Expand Down
178 changes: 178 additions & 0 deletions src/examples/api_example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#include <stdio.h>
#include "../ninja/windninja.h"
#include <string>
int main()
{
NinjaH* ninjaArmy = NULL;
const char * comType = "cli"; //communication type is always set to "cli"
const int nCPUs = 1;
char ** papszOptions = NULL;
NinjaErr err = 0;
const char * demFile = "output.tif";
const char * initializationMethod = "domain_average";
const char * meshChoice = "coarse";
const char * vegetation = "grass";
const int nLayers = 20; //layers in the mesh
const int diurnalFlag = 0; //diurnal slope wind parameterization not used
const char * speedUnits = "mps";
const double height = 5.0;
const char * heightUnits = "m";
int momentumFlag = 0; //we're using the conservation of mass solver
int numNinjas = 2; //two ninjas in the ninjaArmy
const std::string demFileName = "/home/nicholas/Firelab/WindNew/windninja/src/examples/output.tif";
const std::string timeZone = "America/Denver";
const std::string wxmodel_type = "NOMADS-HRRR-CONUS-3-KM";
const std::string forecastFilename = NinjaFetchForecast(wxmodel_type, timeZone, 1, demFileName);
printf("Forecast filename: %s\n", forecastFilename.c_str());
/* inputs that can vary among ninjas in an army */
const double speed[2] = {5.5, 5.5};
const double direction[2] = {220, 300};
ninjaArmy = NinjaCreateArmy(numNinjas, papszOptions);
if( NULL == ninjaArmy )
{
printf("NinjaCreateArmy: ninjaArmy = NULL\n");
}

err = NinjaInit(); //must be called for any simulation
if(err != NINJA_SUCCESS)
{
printf("NinjaInit: err = %d\n", err);
}
for(unsigned int i=0; i<numNinjas; i++)
{
err = NinjaSetCommunication(ninjaArmy, i, comType);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetCommunication: err = %d\n", err);
}

err = NinjaSetNumberCPUs(ninjaArmy, i, nCPUs);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetNumberCPUs: err = %d\n", err);
}

err = NinjaSetInitializationMethod(ninjaArmy, i, initializationMethod);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetInitializationMethod: err = %d\n", err);
}

err = NinjaSetDem(ninjaArmy, i, demFile);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetDem: err = %d\n", err);
}

err = NinjaSetPosition(ninjaArmy, i);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetPosition: err = %d\n", err);
}

err = NinjaSetInputSpeed(ninjaArmy, i, speed[i], speedUnits);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetInputSpeed: err = %d\n", err);
}

err = NinjaSetInputDirection(ninjaArmy, i, direction[i]);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetInputDirection: err = %d\n", err);
}

err = NinjaSetInputWindHeight(ninjaArmy, i, height, heightUnits);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetInputWindHeight: err = %d\n", err);
}

err = NinjaSetOutputWindHeight(ninjaArmy, i, height, heightUnits);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetOutputWindHeight: err = %d\n", err);
}

err = NinjaSetOutputSpeedUnits(ninjaArmy, i, speedUnits);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetOutputSpeedUnits: err = %d\n", err);
}

err = NinjaSetDiurnalWinds(ninjaArmy, i, diurnalFlag);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetDiurnalWinds: err = %d\n", err);
}

err = NinjaSetUniVegetation(ninjaArmy, i, vegetation);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetUniVegetation: err = %d\n", err);
}

err = NinjaSetMeshResolutionChoice(ninjaArmy, i, meshChoice);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetMeshResolutionChoice: err = %d\n", err);
}

err = NinjaSetNumVertLayers(ninjaArmy, i, nLayers);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetNumVertLayers: err = %d\n", err);
}
err = NinjaSetOutputPath(ninjaArmy, i, "./");
if(err != NINJA_SUCCESS)
{
printf("NinjaSetOutputPath: err = %d\n", err);
}
err = NinjaSetGoogOutFlag(ninjaArmy, i, 1);
if(err != NINJA_SUCCESS)
{
printf("NinjaSetGoogOutFlag: err = %d\n", err);
}
}
err = NinjaStartRuns(ninjaArmy, nCPUs);
if(err != 1) //NinjaStartRuns returns 1 on success
{
printf("NinjaStartRuns: err = %d\n", err);
}
const double* outputSpeedGrid = NULL;
const double* outputDirectionGrid = NULL;
const char* outputGridProjection = NULL;
const int nIndex = 0;

outputSpeedGrid = NinjaGetOutputSpeedGrid(ninjaArmy, nIndex);
if( NULL == outputSpeedGrid )
{
printf("Error in NinjaGetOutputSpeedGrid");
}

outputDirectionGrid = NinjaGetOutputDirectionGrid(ninjaArmy, nIndex);
if( NULL == outputDirectionGrid )
{
printf("Error in NinjaGetOutputDirectionGrid");
}

outputGridProjection = NinjaGetOutputGridProjection(ninjaArmy, nIndex);
if( NULL == outputGridProjection )
{
printf("Error in NinjaGetOutputGridProjection");
}

const char* prj = NinjaGetOutputGridProjection(ninjaArmy, nIndex);
const double cellSize = NinjaGetOutputGridCellSize(ninjaArmy, nIndex);
const double xllCorner = NinjaGetOutputGridxllCorner(ninjaArmy, nIndex);
const double yllCorner = NinjaGetOutputGridyllCorner(ninjaArmy, nIndex);
const int nCols = NinjaGetOutputGridnCols(ninjaArmy, nIndex);
const int nRows = NinjaGetOutputGridnRows(ninjaArmy, nIndex);
err = NinjaDestroyArmy(ninjaArmy);
if(err != NINJA_SUCCESS)
{
printf("NinjaDestroyRuns: err = %d\n", err);
}

return NINJA_SUCCESS;
}
Binary file added src/examples/output.tif
Binary file not shown.
2 changes: 2 additions & 0 deletions src/ninja/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,5 @@ target_link_libraries(ninja ${LINK_LIBS})
install(TARGETS ninja DESTINATION lib COMPONENT libs)
install(FILES WindNinjaInputs.h ninja.h windninja.h ninja_errors.h DESTINATION include COMPONENT includes)



80 changes: 65 additions & 15 deletions src/ninja/ninja.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,9 @@ if(input.frictionVelocityFlag == 1){
deleteDynamicMemory();
if(!input.keepOutGridsInMemory)
{
u.deallocate();
v.deallocate();
w.deallocate();
AngleGrid.deallocate();
VelocityGrid.deallocate();
CloudGrid.deallocate();
Expand Down Expand Up @@ -2848,9 +2851,7 @@ void ninja::writeOutputFiles()
}
}

u.deallocate();
v.deallocate();
w.deallocate();


#pragma omp parallel sections
{
Expand Down Expand Up @@ -4558,26 +4559,30 @@ void ninja::set_numberCPUs(int CPUs)
//ninjaCom(ninjaComClass::ninjaDebug, "In parallel = %d", omp_in_parallel());
}

double* ninja::get_outputSpeedGrid()
double* ninja::get_outputSpeedGrid(double resolution, lengthUnits::eLengthUnits units)
{
outputSpeedArray = new double[VelocityGrid.get_arraySize()];

for(int i=0; i<VelocityGrid.get_nRows(); i++){
for(int j=0; j<VelocityGrid.get_nCols(); j++){
outputSpeedArray[i * VelocityGrid.get_nCols() + j] = VelocityGrid(i,j);
lengthUnits::toBaseUnits(resolution, units);
AsciiGrid<double> *velTempGrid;
velTempGrid = new AsciiGrid<double> (VelocityGrid.resample_Grid(resolution, AsciiGrid<double>::order0));
outputSpeedArray = new double[velTempGrid->get_arraySize()];
for(int i=0; i<velTempGrid->get_nRows(); i++){
for(int j=0; j<velTempGrid->get_nCols(); j++){
outputSpeedArray[i * velTempGrid->get_nCols() + j] = velTempGrid->get_cellValue(i,j);
}
}

return outputSpeedArray;
}

double* ninja::get_outputDirectionGrid()
double* ninja::get_outputDirectionGrid(double resolution, lengthUnits::eLengthUnits units)
{
outputDirectionArray = new double[AngleGrid.get_arraySize()];

for(int i=0; i<AngleGrid.get_nRows(); i++){
for(int j=0; j<AngleGrid.get_nCols(); j++){
outputDirectionArray[i * AngleGrid.get_nCols() + j] = AngleGrid(i,j);
lengthUnits::toBaseUnits(resolution, units);
AsciiGrid<double> *dirTempGrid;
dirTempGrid = new AsciiGrid<double> (AngleGrid.resample_Grid(resolution, AsciiGrid<double>::order0));
outputDirectionArray = new double[dirTempGrid->get_arraySize()];
for(int i=0; i<dirTempGrid->get_nRows(); i++){
for(int j=0; j<dirTempGrid->get_nCols(); j++){
outputDirectionArray[i * dirTempGrid->get_nCols() + j] = dirTempGrid->get_cellValue(i,j);
}
}

Expand Down Expand Up @@ -4646,6 +4651,51 @@ int ninja::get_outputGridnRows()
{
return input.dem.get_nRows();
}
double * ninja::get_u()
{
double * outputArray = new double[mesh.nrows * mesh.ncols*mesh.nlayers];
for (int i = 0; i < mesh.nrows; i++)
{
for (int j = 0; j < mesh.ncols; j++)
{
for (int k = 0; k < mesh.nlayers; k++)
{
outputArray[i * mesh.ncols * mesh.nlayers + j * mesh.nlayers + k] = u(i,j,k);
}
}
}
return outputArray;
}
double * ninja::get_v()
{
double * outputArray = new double[mesh.nrows * mesh.ncols*mesh.nlayers];
for (int i = 0; i < mesh.nrows; i++)
{
for (int j = 0; j < mesh.ncols; j++)
{
for (int k = 0; k < mesh.nlayers; k++)
{
outputArray[i * mesh.ncols * mesh.nlayers + j * mesh.nlayers + k] = v(i,j,k);
}
}
}
return outputArray;
}
double * ninja::get_w()
{
double * outputArray = new double[mesh.nrows * mesh.ncols*mesh.nlayers];
for (int i = 0; i < mesh.nrows; i++)
{
for (int j = 0; j < mesh.ncols; j++)
{
for (int k = 0; k < mesh.nlayers; k++)
{
outputArray[i * mesh.ncols * mesh.nlayers + j * mesh.nlayers + k] = w(i,j,k);
}
}
}
return outputArray;
}

void ninja::set_outputBufferClipping(double percent)
{
Expand Down
7 changes: 5 additions & 2 deletions src/ninja/ninja.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,17 @@ class ninja
void set_position(double lat_degrees, double lat_minutes, double long_degrees, double long_minutes); //input as degrees, decimal minutes
void set_position(double lat_degrees, double lat_minutes, double lat_seconds, double long_degrees, double long_minutes, double long_seconds); //input as degrees, minutes, seconds
void set_numberCPUs(int CPUs);
double *get_outputSpeedGrid();
double *get_outputDirectionGrid();
double *get_outputSpeedGrid(double resolution, lengthUnits::eLengthUnits units);
double *get_outputDirectionGrid(double resolution, lengthUnits::eLengthUnits units);
const char* get_outputGridProjection();
double get_outputGridCellSize();
double get_outputGridxllCorner();
double get_outputGridyllCorner();
int get_outputGridnCols();
int get_outputGridnRows();
double * ninja::get_u();
double * ninja::get_v();
double * ninja::get_w();
void set_outputBufferClipping(double percent);
void set_writeAtmFile(bool flag); //Flag that determines if an atm file should be written. Usually set by ninjaArmy, NOT directly by the user!
void set_googOutFlag(bool flag);
Expand Down
Loading

0 comments on commit 012eab7

Please sign in to comment.