00001 #include "FlowData.h"
00002 #include <math.h>
00003 #include "reverseBytes.h"
00004
00005 FlowData::FlowData()
00006 {
00007
00008 for(int i = 0; i < max_channels; i++)
00009 {
00010 channels[i] = NULL;
00011 freeChannel[i] = true;
00012 }
00013 }
00014
00015 FlowData::~FlowData()
00016 {
00017
00018 for(int i = 0; i < max_channels; i++)
00019 if (!freeChannel[i])
00020 deleteChannel(i);
00021 }
00022
00023 bool FlowData::loadDataset(string filename, bool bigEndian)
00024 {
00025 FILE* griFile = NULL;
00026 FILE* datFile = NULL;
00027 char header[40];
00028
00029
00030 int lastdot = filename.find_last_of(".",filename.length()-1);
00031 if (lastdot != string::npos)
00032
00033 filename = filename.substr(0,lastdot);
00034
00036
00038 string griName = filename+".gri";
00039
00040 std::cout << "- Loading grid file '" << griName << "' ... " << std::endl;
00041
00042 griFile = fopen(griName.c_str(),"rb");
00043
00044 if (!griFile)
00045 {
00046 std::cerr << "+ Error loading grid file:" << griName << std::endl << std::endl;
00047 return false;
00048 }
00049
00050 fread(header,40,1,griFile);
00051
00052 if (!geometry.readFromFile(header,griFile,bigEndian))
00053 return false;
00054
00055 fclose(griFile);
00056
00057 int dimX,dimY,dimZ;
00058 float DT;
00059
00060 sscanf(header,"SN4DB %d %d %d %d %d %f",&dimX,&dimY,&dimZ,&numAddChannels,×teps,&DT);
00061 printf("Channels: %d\nTimesteps: %d\n",numAddChannels,timesteps);
00062
00063 int numChannels = numAddChannels;
00064
00066
00068
00069 char suffix[16];
00070 sprintf(suffix,".%.5u.dat",0);
00071 string datName = filename.append(suffix);
00072 std::cout << "- Loading grid file '" << datName << "' ... " << std::endl;
00073
00074 datFile = fopen(datName.c_str(),"rb");
00075 if (!datFile)
00076 {
00077 std::cerr << "+ Error loading dat file:" << datName << std::endl << std::endl;
00078 return false;
00079 }
00080
00081 numChannels += 3;
00082 int* ch = new int[numChannels];
00083
00084
00085
00086 float* tmpArray = new float[numChannels*geometry.getDimX()*geometry.getDimY()];
00087 int result = fread(tmpArray,sizeof(float),numChannels*geometry.getDimX()*geometry.getDimY(),datFile);
00088
00089 if (result != numChannels*geometry.getDimX()*geometry.getDimY())
00090 {
00091 std::cerr << "+ Error reading dat file:" << datName << std::endl << std::endl;
00092 return false;
00093 }
00094
00095 fclose(datFile);
00096
00097
00098 if (bigEndian)
00099 for(int j = 0; j < numChannels*geometry.getDimX()*geometry.getDimY(); j++)
00100 tmpArray[j] = reverseBytes<float>(tmpArray[j]);
00101
00102 for (int j = 0; j < (numChannels); j++)
00103 {
00104
00105 ch[j] = createChannel();
00106
00107 channels[ch[j]]->copyValues(tmpArray,(numChannels),j);
00108 }
00109 delete[] ch;
00110 delete[] tmpArray;
00111 return true;
00112 }
00113
00114 int FlowData::createChannel()
00115 {
00116
00117 int i = 0;
00118 while ((!freeChannel[i])&&(i < max_channels)) i++;
00119
00120 if (i < max_channels)
00121 {
00122 std::cout << "Creating channel at " << i << " ... ";
00123
00124 channels[i] = new FlowChannel(&geometry);
00125
00126 freeChannel[i] = false;
00127
00128 return i;
00129 }
00130
00131 else
00132 {
00133 return -1;
00134 std::cerr << "There is no free channel slot!" << std::endl;
00135 }
00136 }
00137
00138 void FlowData::deleteChannel(int i)
00139 {
00140
00141 if (!freeChannel[i])
00142 {
00143 std::cout << "Deleting channel at " << i << " ... ";
00144
00145 delete channels[i];
00146 channels[i] = NULL;
00147
00148 freeChannel[i] = true;
00149 }
00150 else std::cout << "Tried to delete a non-existing channel at " << i << "." << std::endl;
00151 }
00152
00153 FlowChannel* FlowData::getChannel(int i)
00154 {
00155 return channels[i];
00156 }
00157
00158 int FlowData::createChannelGeometry(int dimension)
00159 {
00160 int result = createChannel();
00161
00162 channels[result]->copyValues((float*)geometry.geometryData, 3, dimension);
00163 return result;
00164 }
00165
00166 int FlowData::createChannelVectorLength(FlowChannel* chX, FlowChannel* chY, FlowChannel* chZ)
00167 {
00168 int result = createChannel();
00169
00170 if (chZ)
00171 for (int i = 0; i < geometry.getDimX()*geometry.getDimY(); i++)
00172
00173 channels[result]->setValue(i,sqrt(chX->getValue(i)*chX->getValue(i) + chY->getValue(i)*chY->getValue(i) + chZ->getValue(i)*chZ->getValue(i)));
00174 else
00175 for (int i = 0; i < geometry.getDimX()*geometry.getDimY(); i++)
00176 channels[result]->setValue(i,sqrt(chX->getValue(i)*chX->getValue(i) + chY->getValue(i)*chY->getValue(i)));
00177
00178 return result;
00179 }
00180
00181 int FlowData::createChannelVectorLength(int chX, int chY, int chZ)
00182 {
00183
00184 if (chZ >= 0)
00185 return createChannelVectorLength(getChannel(chX),getChannel(chY),getChannel(chZ));
00186 else return createChannelVectorLength(getChannel(chX),getChannel(chY));
00187 }
00188
00189 int FlowData::getNumTimesteps()
00190 {
00191 return timesteps;
00192 }
00193
00194 int FlowData::getNumAddChannels()
00195 {
00196 return numAddChannels;
00197 }