Q:/Codes/PPF/FullPPF/ppfsystem.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <cstdlib>
00004 #include <string>
00005 #include "ppftypes.hpp"
00006 #include "ppfsystem.hpp"
00007 #include "ppfdatatype.hpp"
00008 extern "C"{
00009 #include "ppf.h"
00010 };
00011 
00012 using namespace std;
00013 
00014 namespace ppf
00015 {
00016         // global variables --------------------------------------------------------
00017         system ppfdb;
00018 
00019         // class methods -----------------------------------------------------------
00020         void system::connect()
00021         {
00022                 connect("JETPPF", ppf::read);
00023         }
00024 
00025         void system::connect(string user)
00026         {
00027                 connect(user, ppf::read);
00028         }
00029         
00030         void system::connect(string user, accessflag flag)
00031         {
00032                 char caUserName[9];
00033                 char cR[2] = "R";
00034                 char cW[2] = "W";
00035 
00036                 // check access flags are valid
00037                 if (!(flag & ppf::read) && !(flag & ppf::write))
00038                 {
00039                         // throw error
00040                         throw ppf_error("Invalid access flags specified in call to connect().");
00041                 }
00042 
00043                 // make sure user name is no more then 8 characters
00044                 if (user.size() > 8) user.erase(8);
00045                 strcpy(caUserName, user.c_str());
00046 
00047                 // call PPFUID for reading and/or writing as requested via access flags
00048                 if (flag & ppf::read) PPFUID(caUserName, cR, 8,1);
00049                 if (flag & ppf::write) PPFUID(caUserName, cW, 8,1);
00050         }
00051 
00052         void system::read(datatype &d, const long shot, const string DDA, const string dtype)
00053         {
00054                 read(d, shot, 0, DDA, dtype, ppf::xvector | ppf::tvector);
00055         }
00056         
00057         void system::read(datatype &d, const long shot, const string DDA, const string dtype, const ioflag flag)
00058         {
00059                 read(d, shot, 0, DDA, dtype, flag);
00060         }
00061         
00062         void system::read(datatype &d, const long shot, const long sequence, const string DDA, const string dtype)
00063         {
00064                 read(d, shot, sequence, DDA, dtype,  ppf::xvector | ppf::tvector);
00065         }
00066 
00067         void system::read(datatype &d, long shot, long sequence, string DDA, string dtype, ioflag flag)
00068         {
00069                 int iError = 0;
00070                 int iShot = shot;
00071                 int iSequence = sequence;
00072                 int iLun = 0;
00073                 int nX = 0;
00074                 int nT = 0;
00075                 int nData = 0;
00076                 int iReadData[13];
00077                 int iWriteData[13];
00078                 char caDDA[5];
00079                 char caDataType[5];
00080                 char caHeaderData[60];
00081                 float *fData = 0;
00082                 float *fX = 0;
00083                 float *fT = 0;
00084                 bool usexvector;
00085                 bool usetvector;
00086 
00087                 // initialise the ppf system for reading
00088                 PPFGO(&iShot, &iSequence , &iLun , &iError);
00089 
00090                 // process any errors
00091                 if (iError) throw ppf_error("PPFGO", iError);
00092 
00093                 // check size and copy DDA and dtype strings to char arrays (can't use .c_str() as this returns a const char*, PPFGET need a plain char*)
00094                 DDA.resize(4, ' ');
00095                 strcpy(caDDA, DDA.c_str());
00096                 dtype.resize(4, ' ');
00097                 strcpy(caDataType, dtype.c_str());
00098 
00099                 // set variables as required to determine data size using PPFGET
00100                 iReadData[0] = 0;       // NX1
00101                 iReadData[1] = 0;       // NX2
00102                 iReadData[2] = 0;       // NT1
00103                 iReadData[3] = 0;       // NT2
00104                 iReadData[4] = 0;       // NDMAX, set to zero so no data is returned
00105                 iReadData[5] = 0;       // NXMAX, set to zero so no data is returned
00106                 iReadData[6] = 0;       // NTMAX, set to zero so no data is returned
00107                 iReadData[7] = -1;  // don't want x-vector at this stage
00108                 iReadData[8] = -1;  // don't want x-vector at this stage
00109                 iReadData[9] = iReadData[10] = iReadData[11] = iReadData[12] = 0;   // Not used so set to zero
00110 
00111                 // open datatype without loading data to determine array size required
00112                 PPFGET(&iShot, caDDA, caDataType, iReadData, caHeaderData, iWriteData, fData, fX, fT, &iError, 5, 5, 60);
00113 
00114                 // check for errors
00115                 if (iError) throw ppf_error("PPFGET", iError);
00116 
00117                 // get data array sizes
00118                 nX = iWriteData[5];
00119                 nT = iWriteData[6];
00120                 nData = nX * nT;
00121 
00122                 // if nData, nX or nT is < 1 throw an error
00123                 if ((nX < 1) || (nT < 1) || (nData < 1)) throw ppf_error("Invalid data ranges returned for datatype.");
00124 
00125                 // check if we are using the t or x vectors and set class flags accordingly
00126                 usexvector = !(flag & ppf::noxvector);
00127                 usetvector = !(flag & ppf::notvector);
00128 
00129                 // create arrays to hold data as necessary
00130                 fData = new float[nData];
00131                 if (usexvector) fX = new float[nX];    // only create array if noxvector flag is not set
00132                 if (usetvector) fT = new float[nT];    // only create array if notvector flag is not set
00133 
00134                 // update iReadData variables as required to read data with PPFGET
00135                 iReadData[4] = nData;   // NDMAX
00136                 iReadData[5] = nX;              // NXMAX
00137                 iReadData[6] = nT;              // NTMAX
00138                 if (usexvector) iReadData[7] = 0;       // was set to -1 in previous definition so re-enable if required
00139                 if (usetvector) iReadData[8] = 0;       // was set to -1 in previous definition so re-enable if required
00140 
00141                 // open datatype without loading data to determine array size required
00142                 PPFGET(&iShot, caDDA, caDataType, iReadData, caHeaderData, iWriteData, fData, fX, fT, &iError, 5, 5, 60);
00143 
00144                 // check for errors
00145                 if (iError)
00146                 {
00147                         if (fX) delete [] fX;
00148                         if (fT) delete [] fT;
00149                         delete [] fData;
00150                         throw ppf_error("PPFGET", iError);
00151                 }
00152 
00153                 // resise x, t and data vectors
00154                 d.data.resize(nT);
00155                 for (int i=0;i<nT;i++) d.data[i].resize(nX);
00156                 d.x.resize(nX);
00157                 d.t.resize(nT);
00158 
00159                 // copy data to class vectors (using an array then copying into a vector is inefficient - must rewrite class in the future)
00160                 for(int i=0;i<nT;i++) for(int j=0;j<nX;j++) d.data[i][j] = fData[i*nX + j];
00161                 if (usexvector) for(int i=0;i<nX;i++) d.x[i] = fX[i];
00162                 else for(int i=0;i<nX;i++) d.x[i] = 0;
00163                 if (usetvector) for(int i=0;i<nT;i++) d.t[i] = fT[i];
00164                 else for(int i=0;i<nT;i++) d.t[i] = 0;
00165 
00166                 // place header information in appropriate class strings
00167                 d.dataunit.assign(&caHeaderData[0], 8);
00168                 d.xunit.assign(&caHeaderData[8], 8);
00169                 d.tunit.assign(&caHeaderData[16], 8);
00170                 d.comment.assign(&caHeaderData[36], 24);
00171 
00172                 // set status values
00173                 d.userdtstatus = iWriteData[9];
00174                 d.sysddastatus = iWriteData[10];
00175                 d.sysdtstatus = iWriteData[11];
00176 
00177                 // delete arrays
00178                 if (fX) delete [] fX;
00179                 if (fT) delete [] fT;
00180                 delete [] fData;
00181         }
00182 
00183         
00184         void system::create(long shot)
00185         {
00186                 create(shot, 0, "");
00187         }
00188 
00189         void system::create(long shot, long status)
00190         {
00191                 create(shot, status, "");
00192         }
00193         
00194         void system::create(long shot, string comment)
00195         {
00196                 create(shot, 0, comment);
00197         }
00198         
00199         void system::create(long shot, long status, string comment)
00200         {
00201                 int iShot = shot;
00202                 int iSequence = 0;
00203                 int iLun = 0;
00204                 int iNRMax = 0;
00205                 int iInputData[10];
00206                 char *caComment;
00207                 int iError = 0;
00208                 int temp;
00209                 
00210                 // pad out comment to make sure length is a mutiple of 4
00211                 temp = comment.size() / 4;
00212                 if ((temp*4) < comment.size()) ++temp;
00213                 comment.resize(temp*4,' ');
00214 
00215                 // copy comment and set iInputData[9]
00216                 iInputData[9] = temp;
00217                 caComment = new char[(temp*4)+1];
00218                 strcpy(caComment, comment.c_str());
00219 
00220                 // set remaining iInputData values
00221                 iInputData[0] = shot;
00222                 iInputData[1] = 0;  // date set blank
00223                 iInputData[2] = 0;  // time set blank
00224                 iInputData[3] = status;
00225                 iInputData[4] = 0;
00226                 iInputData[5] = 0;
00227                 iInputData[6] = 0;
00228                 iInputData[7] = 0;
00229                 iInputData[8] = 0;
00230 
00231                 // create new ppf
00232                 PPFOPN(&iShot, &iSequence, &iLun, &iNRMax, iInputData, caComment, &iError, (temp*4)+1);
00233                 
00234                 // report any error
00235                 if (iError)
00236                 {
00237                         delete [] caComment;
00238                         throw ppf_error("PPFOPN", iError);
00239                 }
00240 
00241                 // set openshot to the correct shot number for thw new ppf
00242                 openshot = shot;
00243 
00244                 // reset X/T vector reference variables to -1 (so that if the user tried to write a the first datatype requesting reuse of vectors the writing of vectors will be forced)
00245                 lastXRef = -1;          
00246                 lastTRef = -1;
00247 
00248                 // clean up
00249                 delete [] caComment;
00250         }
00251 
00252         void system::write(datatype &d, string DDA, string dtype)
00253         {
00254                 write(d, DDA, dtype, ppf::xvector | ppf::tvector);
00255         }
00256 
00257         void system::write(datatype &d, string DDA, string dtype, ioflag flag)
00258         {
00259                 int iError = 0;
00260                 int iShot = openshot;
00261                 int iReadData[13];
00262                 int iWriteData[13];
00263                 char caDDA[5];
00264                 char caDataType[5];
00265                 char caHeaderData[60];
00266                 float *fData = 0;
00267                 float *fX = 0;
00268                 float *fT = 0;
00269                 const string storagetype = "F   ";
00270 
00271                 // check size and copy DDA and dtype strings to char arrays (can't use .c_str() as this returns a const char*, PPFGET need a plain char*)
00272                 DDA.resize(4, ' ');
00273                 strcpy(caDDA, DDA.c_str());
00274                 dtype.resize(4, ' ');
00275                 strcpy(caDataType, dtype.c_str());
00276                                         
00277                 // set iReadData, vector references determined by flags         
00278                 iReadData[0] = 0;
00279                 iReadData[1] = 0;
00280                 iReadData[2] = 0;
00281                 iReadData[3] = 0;
00282                 iReadData[4] = 0;
00283                 iReadData[5] = d.getXDim();
00284                 iReadData[6] = d.getTDim();
00285                 if (flag & ppf::noxvector) iReadData[7] = 0;
00286                 else if (flag & ppf::lastxvector) iReadData[7] = lastXRef;
00287                 else iReadData[7] = -1;
00288                 if (flag & ppf::notvector) iReadData[8] = 0;
00289                 else if (flag & ppf::lasttvector) iReadData[8] = lastTRef;
00290                 else iReadData[8] = -1;
00291                 iReadData[9] = d.getUserDTStatus();
00292                 iReadData[10] = d.getSysDTStatus();
00293                 iReadData[11] = 0;              
00294                 iReadData[12] = 0;
00295 
00296                 // set caHeaderData (check size of strings in datatype before copying them) MOVE THESE CHECKS TO DATATYPE METHODS
00297                 d.dataunit.resize(8,' ');
00298                 d.dataunit.copy(&caHeaderData[0],8,0);
00299                 d.xunit.resize(8,' ');
00300                 d.xunit.copy(&caHeaderData[8],8,0);
00301                 d.tunit.resize(8,' ');
00302                 d.tunit.copy(&caHeaderData[16],8,0);
00303                 storagetype.copy(&caHeaderData[24],4,0);
00304                 storagetype.copy(&caHeaderData[28],4,0);
00305                 storagetype.copy(&caHeaderData[32],4,0);                
00306                 d.comment.resize(24,' ');
00307                 d.comment.copy(&caHeaderData[36],24,0);
00308 
00309                 // create arrays to hold data (must allocate space even if it isn't used - PPFWRI is fickle and stupid)
00310                 fData = new float[d.getXDim() * d.getTDim()];
00311                 fX = new float[d.getXDim()];
00312                 fT = new float[d.getTDim()];
00313 
00314                 // copy data from datatype to temporary arrays (this will be removed once datatype is rewritten to remove the use of STL vectors, or direct connections to the PPF system can be used)
00315                 for(int i=0;i<d.getTDim();i++) for(int j=0;j<d.getXDim();j++) fData[i*d.getXDim() + j] = d.data[i][j];
00316                 if (iReadData[7] == -1) for(int i=0;i<d.getXDim();i++) fX[i] = d.x[i];
00317                 if (iReadData[8] == -1) for(int i=0;i<d.getTDim();i++) fT[i] = d.t[i];
00318 
00319                 // write
00320                 PPFWRI(&iShot, caDDA, caDataType, iReadData, caHeaderData, iWriteData, fData, fX, fT, &iError, 5, 5, 60);
00321 
00322                 // check for errors
00323                 if (iError)
00324                 {
00325                         delete [] fData;
00326                         delete [] fX;
00327                         delete [] fT;
00328                         throw ppf_error("PPFWRI", iError);
00329                 }
00330 
00331                 // set last X/T vector references if new X/T vectors written
00332                 if (iReadData[7] == -1) lastXRef = iWriteData[7];
00333                 if (iReadData[8] == -1) lastTRef = iWriteData[8];
00334 
00335                 // clean up
00336                 delete [] fData;
00337                 delete [] fX;
00338                 delete [] fT;
00339         }
00340 
00341         void system::closeDDA()
00342         {
00343                 closeDDA(0, "");
00344         }
00345         
00346         void system::closeDDA(int status)
00347         {
00348                 closeDDA(status, "");
00349         }
00350         
00351         void system::closeDDA(string comment)
00352         {
00353                 closeDDA(0, comment);
00354         }
00355         
00356         void system::closeDDA(int status, string comment)
00357         {
00358                 int iStatus = status;
00359                 int iCommentWords = 0;
00360                 char *caComment;
00361                 int iError = 0;
00362 
00363                 // pad out comment to make sure length is a mutiple of 4
00364                 iCommentWords = comment.size() / 4;
00365                 if ((iCommentWords*4) < comment.size()) ++iCommentWords;
00366                 comment.resize(iCommentWords*4,' ');
00367 
00368                 // copy comment
00369                 caComment = new char[(iCommentWords*4)+1];
00370                 strcpy(caComment, comment.c_str());
00371                 
00372                 // close DDA
00373                 DDACLO(&iStatus, &iCommentWords, caComment, &iError, (iCommentWords*4)+1);
00374                 
00375                 // check for errors
00376                 if (iError)
00377                 {
00378                         delete [] caComment;
00379                         throw ppf_error("DDACLO", iError);
00380                 }
00381                 // clean up
00382                 delete [] caComment;
00383 
00384         }
00385 
00386         void system::abort()
00387         {
00388                 int iError;
00389                 
00390                 // abort ppf
00391                 PPFABO(&iError);
00392 
00393                 // check for errors
00394                 if (iError) throw ppf_error("PPFABO", iError);
00395         }
00396         
00397         int system::commit()
00398         {
00399                 return commit("",0);
00400         }
00401 
00402         int system::commit(string program, int version)
00403         {
00404                 int iShot = openshot;
00405                 int iSequence = 0;
00406                 int iLun = 0;
00407                 char caProgramName[9];
00408                 int iVersion = version;
00409                 int iError = 0;
00410                 
00411                 // check program name is within 8 chars, pad with spaces if not, copy to buffer
00412                 program.resize(8,' ');
00413                 strcpy(caProgramName, program.c_str());
00414                 
00415                 // close ppf
00416                 PPFCLO(&iShot, &iSequence, &iLun, caProgramName, &iVersion, &iError, 9);
00417                 
00418                 // check for errors
00419                 if (iError) throw ppf_error("PPFCLO", iError);
00420 
00421                 // return sequence number
00422                 return iSequence;
00423         }
00424 };

Generated on Tue May 23 15:11:51 2006 for PPF C++ by  doxygen 1.4.6-NO