Skip to content

Commit

Permalink
Resolved the x11 issue with gnuplot. Integrated netcdf data with gnup…
Browse files Browse the repository at this point in the history
…lot, generating images to validate data.
  • Loading branch information
Gregory Burgess authored and Gregory Burgess committed Feb 11, 2014
1 parent cfa604a commit d1ff4be
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 169 deletions.
6 changes: 0 additions & 6 deletions data/test.cmd

This file was deleted.

8 changes: 0 additions & 8 deletions data/test.cmd~

This file was deleted.

100 changes: 0 additions & 100 deletions data/test.dat

This file was deleted.

Binary file removed data/test.gif
Binary file not shown.
18 changes: 5 additions & 13 deletions src/Bathy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,9 @@ Grid simulatetopographyGrid(int XDist, int YDist) {
Grid getBathy(string inputFile, string inputFileType, int startX, int startY, int XDist, int YDist, string seriesName, long timestamp, bool debug) {
Grid topographyGrid = new Grid(XDist, YDist);
if(strcmp(inputFileType.c_str(),"netcdf") == 0){
// grab a slice (in grid form)

// This will be the netCDF ID for the file and data variable.
int ncid, varid;
int ncid, varid, retval;

// Loop indexes, and error handling.
int retval;
// Open the file. NC_NOWRITE tells netCDF we want read-only access to the file.
if ((retval = nc_open(inputFile.c_str(), NC_NOWRITE, &ncid))) {
printError("ERROR: Can't open NetCDF File; check your inputFile.", retval, timestamp);
Expand All @@ -67,19 +63,15 @@ Grid getBathy(string inputFile, string inputFileType, int startX, int startY, in
}
// Close the file, freeing all resources.
if ((retval = nc_close(ncid))) {
cout << "ERROR: Error closing the file." << retval;
cout << "ERROR: Error closing the file." << retval << "\n";
}
}

else if(inputFileType == "arcgis"){
// Read an ArcGIS file
}
else {
cout << "Bathymetry file type not supported. Simulating Bathymetry";
cout << "Bathymetry file type not supported. Simulating Bathymetry.\n";
topographyGrid = simulatetopographyGrid(XDist,YDist);
}

topographyGrid.printData();
topographyGrid.clearNA(0);
//topographyGrid.printData();
return(topographyGrid);
}

Expand Down
12 changes: 11 additions & 1 deletion src/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ Grid::Grid(Grid* mat) {
data = (double*) memcpy(malloc(size), matrix.data, size);
}

void Grid::clearNA(double val) {
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
if(isnan(data[(i * cols) + j])){
data[(i * cols) + j] = 0;
}
}
cout << "\n";
}
}
/**
* Prints the contents of the data array.
*/
Expand All @@ -55,7 +65,7 @@ void Grid::printData() {
void Grid::GNUwrite(string filename) {
ofstream out;
out.open(filename.c_str());
for (int i=rows; i>0; i--) {
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
out << setprecision(3)<<data[(i * cols) + j] << " ";
}
Expand Down
1 change: 1 addition & 0 deletions src/Grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Grid {
Grid();
Grid(int Rows, int Cols);
Grid(Grid* mat);
void clearNA(double val);
void printData();
void GNUwrite(std::string filename);
};
41 changes: 17 additions & 24 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,33 @@
using namespace std;

int main() {
string inputFile = "himbsyn.bathytopo.1km.v19.grd",
inputFileType="a",
string inputFile = "himbsyn.bathy.v19.grd",
inputFileType="netcdf",
seriesName="z";
int startX = 700,
startY = 800,
XDist = 100,
YDist = 100;
int startX = 7000,
startY = 7000,
XDist = 3000,
YDist = 3000;
long timestamp = -1;
bool debug = false;

Grid tGrid = getBathy(inputFile, inputFileType, startX, startY,XDist, YDist, seriesName, timestamp, debug);
//Grid tGrid = simulatetopographyGrid(XDist,YDist);
//tGrid.printData();

string title = "test",
dataFilePath = "data/test.dat",
deepHexColor = "#000066",
shallowHexColor = "#ffffff";
dataFilePath = "data/test.dat";
int width = 500,
height = 500;
//tGrid.printData();
tGrid.GNUwrite(dataFilePath);
printGraph(title, XDist,YDist, dataFilePath, width, height, deepHexColor, shallowHexColor);
cout << "Done";
cout << "Printing" << "\n";
try {
printGraph(title, XDist,YDist, dataFilePath, width, height);
}
catch(int e) {
return 0;
}
cout << "Done" << "\n";
return 0;
}
/*
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

int main() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL)
fprintf(stdout, "Current working dir: %s\n", cwd);
else
perror("getcwd() error");
return 0;
}*/
48 changes: 33 additions & 15 deletions src/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,36 @@ using namespace std;

void printError(string msg, int errcode, long timestamp) {
if (errcode)
cout << msg << " \nError Code: " << errcode ;
cout << msg << " \nError Code: " << errcode << "\n";
else
cout << msg << " ";
cout << msg << " \n";
exit(1);
}

void printGraph(string title, int XDist, int YDist, string dataFilePath, int width, int height, string deepHexColor, string shallowHexColor) {
//set to -.5 so that the first cell doesn't get cut off...
void printGraph(string title, int XDist, int YDist, string dataFilePath, int width, int height) {
double xstart = -.5,
ystart = -.5;
string filetype = "gif",
outfile = title + filetype,
string filetype = ".gif",
outfile = "img/" + title + filetype,
setOuput = "set output \'" + outfile + "\';",
xrange,
yrange,
size,
plotData,
pallete;

cout << "Output File: " << outfile << "\n";
cout << "Data File: " << dataFilePath << "\n";
if (!fexists(dataFilePath)) {
cout << "Input Data File not Found!\n";
throw 1;
}
std::stringstream ss;
ss << "set size " << width << "," << height << ";";
size = ss.str();
ss.str("");
ss.clear();

ss << "set yrange [" << ystart << ":" << YDist - 0.5 << "];";
ss << "set yrange [" << ystart << ":" << YDist - 0.5 << "];";
yrange = ss.str();
ss.str("");
ss.clear();
Expand All @@ -49,31 +54,39 @@ void printGraph(string title, int XDist, int YDist, string dataFilePath, int wi
ss.str("");
ss.clear();

ss << "plot \"" << dataFilePath << "\" matrix with image;";
ss << "plot \"" << dataFilePath << "\" matrix with image;";
plotData = ss.str();
ss.str("");
ss.clear();

ss << "set palette defined (0 '" << deepHexColor <<"', 1 '" << shallowHexColor << "');";
ss << "set palette defined (0 '#000022', 1 '#000066', 2 '#009999', 3 '#cc6600', 4 '#FF9933', 5'#FF0000', 6 '#ffffff');";
pallete= ss.str();

try {
Gnuplot plots;
string setOuput = "set output \"" + outfile + "\";";
plots.cmd(setOuput);
plots.cmd("set terminal gif;");
plots.cmd(xrange);
plots.cmd(yrange);
plots.cmd(size);
plots.cmd("set palette maxcolors 15");
//plots.cmd(size);
plots.cmd("set palette maxcolors 10000;");
plots.cmd(pallete);
cout << "writing...";

cout << setOuput << "\n";
cout << "set terminal gif\n";
cout << xrange << "\n";
cout << yrange << "\n";
cout << size << "\n";
cout << "set palette maxcolors 100\n";
cout << pallete << "\n";
cout << plotData << "\n";

plots.cmd(plotData);
}
catch (GnuplotException ge) {
cout << ge.what() << endl;
}
cout <<"Finished writing";
cout <<"Finished writing" << "\n";
}

double* seq(double start, double end, int size) {
Expand All @@ -85,3 +98,8 @@ double* seq(double start, double end, int size) {
}
return array;
}

bool fexists(string filename) {
ifstream ifile(filename.c_str());
return ifile;
}
4 changes: 2 additions & 2 deletions src/Utility.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
void printError(std::string msg, int errcode, long timestamp);
void printGraph(std::string title, int XDist, int YDist, std::string dataFilePath,
int width, int height, std::string deepHexColor, std::string shallowHexColor);
void printGraph(std::string title, int XDist, int YDist, std::string dataFilePath,int width, int height);
double* seq(double start, double end, int size);
bool fexists(std::string filename);

0 comments on commit d1ff4be

Please sign in to comment.