/* POSSUM Ivana Drobnjak & Mark Jenkinson Copyright (C) 2005-2007 University of Oxford */ /* Part of FSL - FMRIB's Software Library http://www.fmrib.ox.ac.uk/fsl fsl@fmrib.ox.ac.uk Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance Imaging of the Brain), Department of Clinical Neurology, Oxford University, Oxford, UK LICENCE FMRIB Software Library, Release 5.0 (c) 2012, The University of Oxford (the "Software") The Software remains the property of the University of Oxford ("the University"). The Software is distributed "AS IS" under this Licence solely for non-commercial use in the hope that it will be useful, but in order that the University as a charitable foundation protects its assets for the benefit of its educational and research purposes, the University makes clear that no condition is made or to be implied, nor is any warranty given or to be implied, as to the accuracy of the Software, or that it will be suitable for any particular purpose or for use under any specific conditions. Furthermore, the University disclaims all responsibility for the use which is made of the Software. It further disclaims any liability for the outcomes arising from using the Software. The Licensee agrees to indemnify the University and hold the University harmless from and against any and all claims, damages and liabilities asserted by third parties (including claims for negligence) which arise directly or indirectly from the use of the Software or the sale of any products based on the Software. No part of the Software may be reproduced, modified, transmitted or transferred in any form or by any means, electronic or mechanical, without the express permission of the University. The permission of the University is not required if the said reproduction, modification, transmission or transference is done without financial return, the conditions of this Licence are imposed upon the receiver of the product, and all original and amended source code is included in any transmitted product. You may be held legally responsible for any copyright infringement that is caused or encouraged by your failure to abide by these terms and conditions. You are not permitted under this Licence to use this Software commercially. Use for which any financial return is received shall be defined as commercial use, and includes (1) integration of all or part of the source code or the Software into a product for sale or license by or on behalf of Licensee to third parties or (2) use of the Software or any derivative of it for research with the final aim of developing software products for sale or license to a third party or (3) use of the Software or any derivative of it for research with the final aim of developing non-software products for sale or license to a third party, or (4) use of the Software to provide any service to an external organisation for which payment is received. If you are interested in using the Software commercially, please contact Isis Innovation Limited ("Isis"), the technology transfer company of the University, to negotiate a licence. Contact details are: innovation@isis.ox.ac.uk quoting reference DE/9564. */ //POSSUM-FUNCTIONS #include #include //standard c++ library #include //include string class #include //to read and write to the file #include //what is this? #include //for precalcuation of some variables #include "newmatap.h"//including NEWMAT library #include "newmatio.h"// #include "newimage/newimageall.h"//including NEW IMAGE library #include "possumfns.h"//including possum functions I made #include "libprob.h" #include "miscmaths/miscprob.h" #include "newimage/costfns.h" #include #define _GNU_SOURCE 1 #define POSIX_SOURCE 1 using namespace std; using namespace MISCMATHS; using namespace NEWIMAGE; using namespace NEWMAT; //string title="possumfns (Version 2.0)\nCopyright(c) 2003, University of Oxford (Ivana Drobnjak)"; vector gstatic; double* g1static; double* g2static; double* g3static; vector g1motion; vector g2motion; vector g3motion; vector g4motion; vector rotmotion1; vector rotmotion2; vector rotmotion3; vector transmotion; vector b0tmp111(1,0.0); vector b0tmp112(1,0.0); vector b0tmp113(1,0.0); vector b0tmp221(1,0.0); vector b0tmp222(1,0.0); vector b0tmp223(1,0.0); vector b0tmp331(1,0.0); vector b0tmp332(1,0.0); vector b0tmp333(1,0.0); vector b0tmp111freq(1,0.0); vector b0tmp112freq(1,0.0); vector b0tmp113freq(1,0.0); vector b0tmp221freq(1,0.0); vector b0tmp222freq(1,0.0); vector b0tmp223freq(1,0.0); vector b0tmp331freq(1,0.0); vector b0tmp332freq(1,0.0); vector b0tmp333freq(1,0.0); double glo_zero=1e-20; //TABLE SINC double* table_sinc=0;//table for SINC in range [0,Dsinc] int glo_Nsinc=5000000;//no of elements double glo_Dsinc=500.00;//domain double glo_dsinc=glo_Dsinc/glo_Nsinc;//step size for SINC double glo_idsinc=1/glo_dsinc;//inverse of step size for fster calc //TABLE SIN AND COS double* table_sin=0;//table for SIN in range [0,2pi] double* table_cos=0;//table for COS in range [0,2pi] int glo_Nsin=6000000;//no of elements double glo_Dsin=2.0*M_PI;//domain double glo_dsin=glo_Dsin/glo_Nsin;//step size for SIN and COS double glo_idsin=1/glo_dsin;//inverse of the step size double glo_twopi=2*M_PI; double glo_itwopi=1/glo_twopi; double glo_cx; double glo_cy; double glo_cz; //SOME CONSTANTS const double gammabar=42.58*1e06;//(in Hz/T) const double gama=2*M_PI*gammabar; ///////// //GENERAL /////////////////////////////// double round_ivana(const double x, const int n){ //rounds a number up to n digits of precision double xx,xxx,xxxx,nn; nn=MISCMATHS::pow(10.0f,(double) n); xx=nn*x; if (x>0) xxx=floor((float)xx+0.5); else xxx=ceil(xx-0.5); xxxx=xxx/nn; return xxxx; } ///////////////////////////// double norm(const RowVector q){ int x=q.Ncols(); double sqsum=0.0; for (int i=1;i<=x;i++){ sqsum+=q(i)*q(i); } double norm_q=sqrt(sqsum); return norm_q; } //////////////////////////////////////// void coeff(const double xold, const double xnew, const double told, const double tnew, double& a, double& b){//needs improvement // return slope and intercept of a line //cout<<"xold="<1e-12) { cout<<"Warning, euler_to_quat is producing quaternions with the norm different from 1!"<<"Norm difference from one is "<1e-12) { cout<<"Warning, quaternion is not normalised !"<<"Norm_q-1 is "<0) q_n(1)=1; //the difference can be really small i.e. 1e-16, but acos gives nan for it (for 1+1e-16) if ((q_n(1)+1)<0) q_n(1)=-1; angle=acos(q_n(1))*2; //cout<1e-12) { axis(1)=axis(1)/norm_axis; axis(2)=axis(2)/norm_axis; axis(3)=axis(3)/norm_axis; } double x=axis(1)*sin(angle/2); double y=axis(2)*sin(angle/2); double z=axis(3)*sin(angle/2); double w=cos(angle/2); RowVector q(4); q<1e-12) { q_n(1)=q(1)/norm_q; q_n(2)=q(2)/norm_q; q_n(3)=q(3)/norm_q; q_n(4)=q(4)/norm_q; } double x=q_n(2); double y=q_n(3); double z=q_n(4); double w=q_n(1); Matrix R(3,3); R<<1-2*y*y-2*z*z<<2*x*y-2*z*w<<2*x*z+2*y*w <<2*x*y+2*z*w<<1-2*x*x-2*z*z<<2*y*z-2*x*w <<2*x*z-2*y*w<<2*y*z+2*x*w<<1-2*x*x-2*y*y; return R; } //////////////////////////////////////////////////////////////////////////////// RowVector matrix_to_quaternion(const Matrix M){//not using double trace = M(1,1) + M(2,2) + M(3,3) + 1; double s,x,y,z,w; if( trace > 0 ) { s = 0.5 / sqrt(trace); w = 0.25f/s; x = (M(3,2) - M(2,3))*s; y = (M(1,3) - M(3,1))*s; z = (M(2,1) - M(1,2))*s; } else { if ( M(1,1) > M(2,2) && M(1,1) > M(3,3) ) { double s = 2 * sqrt( 1 + M(1,1) - M(2,2) - M(3,3)); x = 0.25 * s; y = (M(1,2)+M(2,1))/s; z = (M(1,3)+M(3,1))/s; w = (M(2,3)-M(3,2))/s; } else if (M(2,2) > M(3,3)) { double s = 2 * sqrt( 1 + M(2,2) - M(1,1) - M(3,3)); x = (M(1,2) + M(2,1) ) / s; y = 0.25f * s; z = (M(2,3) + M(3,2) ) / s; w = (M(1,3) - M(3,1) ) / s; } else { double s = 2 * sqrt( 1 + M(3,3) - M(1,1) - M(2,2) ); x = (M(1,3) + M(3,1) ) / s; y = (M(2,3) + M(3,2) ) / s; z = 0.25 * s; w = (M(1,2) - M(2,1) ) / s; } } RowVector q(4); q<1e-12) { if (fabs(norm_axis)>1e-12){ axis(1)=axis(1)/norm_axis; axis(2)=axis(2)/norm_axis; axis(3)=axis(3)/norm_axis; } else { //cout<<"Warning in axismatm: Norm of the axis is ZERO!"< 0.01"< (tx2,ty2,tz2) int d=vectortime.Nrows(); double dtt=0; Matrix inttra(d,3); double dt=vectortime(d)-vectortime(1); for (int k=1;k<=d-1;k++){ inttra(k,1)=dtt*(tx2-tx1)/dt+tx1; inttra(k,2)=dtt*(ty2-ty1)/dt+ty1; inttra(k,3)=dtt*(tz2-tz1)/dt+tz1; dtt=dtt+vectortime(k+1)-vectortime(k); } inttra(d,1)=tx2; inttra(d,2)=ty2; inttra(d,3)=tz2; return inttra; } /////////////////////////// Matrix intertranslation_old(const double tx1,const double ty1,const double tz1, const double tx2, const double ty2, const double tz2, const ColumnVector vectortime){ // interpolate between two positions (translations) // vectortime is a list of times to interpolate at *except* first point // specifies when tx1,ty1,tz1 occurs and last point for tx2,ty2,tz2 // output is (tx,ty,tz) per row, with first row for *second* input time // but still including last input time => (tx2,ty2,tz2) int d=vectortime.Nrows(); double dtt=0; Matrix inttra(d,3); double dt=vectortime(d)-vectortime(1); for (int k=1;k<=d-1;k++){ dtt=dtt+vectortime(k+1)-vectortime(k); inttra(k,1)=dtt*(tx2-tx1)/dt+tx1; inttra(k,2)=dtt*(ty2-ty1)/dt+ty1; inttra(k,3)=dtt*(tz2-tz1)/dt+tz1; } inttra(d,1)=inttra(d-1,1); inttra(d,2)=inttra(d-1,2); inttra(d,3)=inttra(d-1,3); return inttra; } //////////////////// Matrix interrotation(const double a1,const double b1,const double c1, const double a2,const double b2,const double c2, const ColumnVector vectortime){ // as above but for rotations (input in euler angles, output in angle/axis) int d=vectortime.Nrows(); Matrix introt(d,8); RowVector aa(8); RowVector q1=euler_to_quaternion(a1,b1,c1); RowVector q2=euler_to_quaternion(a2,b2,c2); RowVector q1c(4); //conjugate of q1 q1c <2){ double dtt=0; double dt=vectortime(d)-vectortime(1); RowVector angle(d-2); for (int k=1;k<=d-2;k++){ dtt=dtt+vectortime(k+1)-vectortime(k); angle(k)=dtt*qa(1)/dt; aa<2){ double dtt=0; double dt=vectortime(d)-vectortime(1); RowVector angle(d-2); for (int k=1;k<=d-2;k++){ dtt=dtt+vectortime(k+1)-vectortime(k); angle(k)=dtt*qa(1)/dt; aa<1e-6){ ts++;tp++; } ts++; if (opt_test==1) cout<<"tp before equal"< tsort(dimnew); if (opt_test==1) cout<<"tsort was just assigned "<1e-6){ ts++;tsort[ts-1]=tp;tp++; } ts++;tsort[ts-1]=-tm; if (opt_test==1) cout<<"ts= "<=0;ii--){ if (opt_test==1) cout<<"And ii index is "<0){ if (opt_test==1) cout<<"No idea fuck "<=1){ if (opt_test==1) cout<<"ii is"<0){ cc++;ii++; } int ccdim=cc; cc=1; if (opt_test==1) cout<<"ccdim="<0){ if (opt_test==1) cout<<"Values of ii= "<1e-10){ l=l+1; timevector(l)=epi(t1,1); t1=t1+1; } l=l+1; timevector(l)=motion(t2+1,1); int tmp6=0; RowVector G(8); G=0; if (fabs(motion(t2+1,1)-epi(t1,1))<=1e-10){ t1=t1+1; tmp6=0; } else{ G=interpolation_gradients(epi.Row(t1-1),epi.Row(t1),motion(t2+1,1)); tmp6=1; } Matrix R=interrotation_old(motion(t2,5),motion(t2,6),motion(t2,7),motion(t2+1,5),motion(t2+1,6),motion(t2+1,7),timevector.Rows(1,l)); Matrix T=intertranslation_old(motion(t2,2),motion(t2,3),motion(t2,4),motion(t2+1,2),motion(t2+1,3),motion(t2+1,4),timevector.Rows(1,l)); for (int tt=1;tt<=l-2;tt++){ t=t+1; RowVector tmp1(19); tmp1.Columns(1,8)=epi.Row(t1-l+tt+tmp6); tmp1.Columns(9,11)=T.Row(tt); tmp1.Columns(12,19)=R.Row(tt); mainmatrix.Row(t)=tmp1; } t=t+1; if (tmp6==1){ RowVector Gpre(8); Gpre<& b, volume& b0gx, volume& b0gy, volume& b0gz){ // b is in mT, and dimensions of voxels are in mm b0gx = b*0; b0gy = b0gx; b0gz = b0gx; for (int z=1;z& b0, volume& b0x, volume& b0y, volume& b0z, const int myid, const int Nxx,const int numprocs){ calc_gradients(b0,b0x,b0y,b0z); int Nx=b0.xsize(); int Ny=b0.ysize(); int Nz=b0.zsize(); int Ntmp=(int)((Nxx-myid-1)/numprocs)+1; if (Ntmp<1){ cout<<"WARNING:Number of processors bigger than the number of voxels in x-direction."< tmpvol1(Ntmp,Ny,Nz); volume tmpvol2(Ntmp,Ny,Nz); volume tmpvol3(Ntmp,Ny,Nz); volume tmpvol4(Ntmp,Ny,Nz); int x0=0, x1=0, y0=0, y1=0, z0=0, z1=0; for (z0=0, z1=0; z0& b, volume4D& b0gx, volume4D& b0gy, volume4D& b0gz){ // b is in mT, and dimensions of voxels are in mm b0gx = b*0; b0gy = b0gx; b0gz = b0gx; for (int t=0;t& b0, volume4D& b0x, volume4D& b0y, volume4D& b0z, const int myid, const int Nxx, const int numprocs){ calc_gradients4D(b0,b0x,b0y,b0z); int Nx=b0.xsize(); int Ny=b0.ysize(); int Nz=b0.zsize(); int Nt=b0.tsize(); volume4D tmpvol1(Nx,Ny,Nz,Nt); volume4D tmpvol2(Nx,Ny,Nz,Nt); volume4D tmpvol3(Nx,Ny,Nz,Nt); volume4D tmpvol4(Nx,Ny,Nz,Nt); int x0=0, x1=0, y0=0, y1=0, z0=0, z1=0, t0=0, t1=0; for (t0=0, t1=0; t0= timecourse[actstep] && actstep <= (Nact-2) ) { coeff(activation[actstep],activation[actstep+1],timecourse[actstep],timecourse[actstep+1],dT2_1,dT2_2); dT2_1 = dT2_1*iT2*iT2; dT2_2 = dT2_2*iT2*iT2; actstep = actstep+1; } actint += (dT2_1+dT2_2*(tnew + told)/2)*(tnew - told); // if (dT2_2<1e-12) actint+=(tnew-told)/(dT2_1+T2); // else actint+=log(1+(tnew-told)/(told+dT2_1/dT2_2))/dT2_2 // double phase = 0; double phase = gama*gg1*x + gama*gg2*y + gama*gg3*z+gama*b0*tt + gama*chshift*tt; if (rfangle != 0) { excitation = 0; double df = H(step,3); double fc = H(step,4); double f = gammabar*(gxnew*x+gynew*y+gznew*z+b0+chshift); if (opt_test==1) cout<<"gxnew="<= 0 && nf <= (Nslc-2)) { off -= nf; double ts = table_slcprof[nf]; double sx = (table_slcprof[nf+1]-ts)*off + ts; double rfangle_f = sx*rfangle*RFtrans;//RFtrans are values 0 to 1 to derscribe the inhomogeneity f the receive RF field. 1 is for perfectly homog; if (opt_test==1 && readstep%4096==0 && v==1) { cout<<"table_slcprof[nf]= "<0) //new stuff mon dec 19 { excitation = 1; m = free(m,tt,tissue,phase,actint); //new stuff mon dec 19 //due to crushers or any gradient induced dephasing over the voxel a new initial magnetisation is introduced which is the average of the magnetisations over the voxel double xvalrf = fabs(glo_cx*(gg1 + b0x*tt)); double yvalrf = fabs(glo_cy*(gg2 + b0y*tt)); double zvalrf = fabs(glo_cz*(gg3 + b0z*tt)); double xyzrf = Sinc(xvalrf)*Sinc(yvalrf)*Sinc(zvalrf); m(1) = m(1)*xyzrf; m(2) = m(2)*xyzrf; m = rot(rfangle_f,"x")*m; //new stuff m00 = sqrt( m(1)*m(1)+m(2)*m(2) ); if (opt_test==1 && readstep%4096==0 && v==1 ) cout<<"Projection of the magnetisation vector into the xy plane after flipping is "<=0 && nf<=(Nslc-2)) { off-=nf; double ts=table_slcprof[nf]; double sx=(table_slcprof[nf+1]-ts)*off + ts; double rfangle_f=sx*rfangle*RFtrans;//RFtrans are values 0 to 1 to derscribe the inhomogeneity f the receive RF field. 1 is for perfectly homog; if (opt_test==1 && readstep%4096==0 && v==1 ){ cout<<"table_slcprof[nf]= "<1e-06){//new stuff mon dec 19 //Edit: 29.11.12 excitation=1; if (opt_test==1 && readstep%4096==0 && readstep>7*4096){ cout< > pstate(nonzero, vector(npst,0.0)); if (segA==1){ pstate[v-1][2]=tissue(3); } ColumnVector m(3);//magnetization vector: M m(1)=pstate[v-1][0]; m(2)=pstate[v-1][1]; m(3)=pstate[v-1][2]; int excitation=(int)pstate[v-1][3]; double trf=pstate[v-1][4];//last RF time double grf1=pstate[v-1][5]; double grf2=pstate[v-1][6]; double grf3=pstate[v-1][7]; double grf4=pstate[v-1][8]; double b0susrf=pstate[v-1][9]; double b0xsusrf=pstate[v-1][10]; double b0ysusrf=pstate[v-1][11]; double b0zsusrf=pstate[v-1][12]; double actint=pstate[v-1][13]; int actstep=(int)pstate[v-1][14]; double m00=pstate[v-1][15]; //for V=1 int npste=13; static vector pstatev(npste,0.0); static Matrix g(4,3); int readstep=(int) pstatev[0]; static int ars=0; static int arsf=0; static double b0tmp11=0.0; static double b0tmp12=0.0; static double b0tmp13=0.0; static double b0tmp21=0.0; static double b0tmp22=0.0; static double b0tmp23=0.0; static double b0tmp31=0.0; static double b0tmp32=0.0; static double b0tmp33=0.0; static double b0tmp11freq=0.0; static double b0tmp12freq=0.0; static double b0tmp13freq=0.0; static double b0tmp21freq=0.0; static double b0tmp22freq=0.0; static double b0tmp23freq=0.0; static double b0tmp31freq=0.0; static double b0tmp32freq=0.0; static double b0tmp33freq=0.0; ///////////////////////////////////////////////////////////////////////// //PHASE,GRADIENTS,COUNTERS,CONSTATNTS //////////////////////////////////////////////////////////////////////// double chshift=tissue(4);//chemical shift int numpoints=H.Nrows();//number of timepoints in the eventsequencer matrix double T2=tissue(2); double iT2=1/T2; double g1,g2,g3,g4; double rr1,rr2,rr3,trr; Matrix coord(3,nreadp); RowVector rnew(4),rmnew(4);//angle, axis values double trnew1=0.0;//translation double trnew2=0.0; double trnew3=0.0; //slice-profile weight def for the sections if (v==1) { g<0) phase_2pi=phase- ((int) (phase*glo_itwopi))*glo_twopi;//one solution //when phase exceedes Dsin , this is faster else phase_2pi=phase- ((int) (phase*glo_itwopi))*glo_twopi+glo_twopi; double off=phase_2pi*glo_idsin; int nphase=(int) off; off-=nphase; if (opt_test==1 && readstep%4096==2081 && v==1) { cout<<"nphase= "<=timecourse[actstep] && actstep<=(Nact-2)){ coeff(activation[actstep],activation[actstep+1],timecourse[actstep],timecourse[actstep+1],dT2_1,dT2_2); dT2_1=dT2_1*iT2*iT2; dT2_2=dT2_2*iT2*iT2; actstep=actstep+1; } actint+=(dT2_1+dT2_2*(tnew+told)/2)*(tnew-told); if (told>=b0timecourse[b0step] && b0step<=(Nb0-2)){ coeff(b0time[b0step],b0time[b0step+1],b0timecourse[b0step],b0timecourse[b0step+1],db0_1,db0_2); coeff(b0xtime[b0step],b0xtime[b0step+1],b0timecourse[b0step],b0timecourse[b0step+1],db0x_1,db0x_2); coeff(b0ytime[b0step],b0ytime[b0step+1],b0timecourse[b0step],b0timecourse[b0step+1],db0y_1,db0y_2); coeff(b0ztime[b0step],b0ztime[b0step+1],b0timecourse[b0step],b0timecourse[b0step+1],db0z_1,db0z_2); if (opt_test==1){ cout<<"Voxel number="<0){//new stuff mon dec 19 excitation=1; m=free(m,tt,tissue,phase,actint); //new stuff mon dec 19 //due to crushers or any gradient induced dephasing over the voxel a new initial magnetisation is introduced which is the average of the magnetisations over the voxel double xvalrf=fabs(glo_cx*(gg1+b0xint+b0x*tt)); double yvalrf=fabs(glo_cy*(gg2+b0yint+b0y*tt)); double zvalrf=fabs(glo_cz*(gg3+b0zint+b0z*tt)); double xyzrf=Sinc(xvalrf)*Sinc(yvalrf)*Sinc(zvalrf); m(1)=m(1)*xyzrf; m(2)=m(2)*xyzrf; m=rot(rfangle_f,"x")*m; //new stuff m00=sqrt(m(1)*m(1)+m(2)*m(2)); if (opt_test==1 && fabs(z)<0.0005 ){ cout<<"Projection of the magnetisation vector into the xy plane after flipping is "<1) && (rows>0) ) { dmat=new double[rows]; fmat=new float[(cols-1)*rows]; for (int n=0; ninitialize(nrows,ncols); } void PMatrix::destroy() { if ( (rows>0) && (cols>1) ) { delete [] dmat; delete [] fmat; } rows=0; cols=0; } PMatrix::~PMatrix() { this->destroy(); } void PMatrix::ReSize(int nrows, int ncols) { this->destroy(); this->initialize(nrows, ncols); } PMatrix& PMatrix::operator=(const PMatrix& src) { if ( (rows!=src.rows) || (cols!=src.cols) ) { this->initialize(src.rows,src.cols); } if (rows>0) { if (cols>1) { for (int n=0; n0) { if (cols>1) { for (int n=0; nbounds_check(r,c)) { cerr << "INAVLID ACCESS TO ELEMENT " << r << " , " << c << " OF PMatrix MATRIX" << endl; return fmat[0]; } return this->operator()(r,c); } float PMatrix::at(int r, int c) const { if (!this->bounds_check(r,c)) { cerr << "INAVLID ACCESS TO ELEMENT " << r << " , " << c << " OF PMatrix MATRIX" << endl; return fmat[0]; } return this->operator()(r,c); } bool PMatrix::bounds_check(int r, int c) const { if ( (r<=0) || (c<=1) || (r>rows) || (c>cols) ) return false; else return true; } //////////////////////////////////////////////////////////////////////////// #define BINFLAGP 43 int read_binary_matrix(PMatrix& mres, const string& filename) { if ( filename.size()<1 ) return 1; ifstream fs(filename.c_str(), ios::in | ios::binary); if (!fs) { cerr << "Could not open matrix file " << filename << endl; return 2; } bool swapbytes = false; unsigned int testval; // test for byte swapping fs.read((char*)&testval,sizeof(testval)); if (testval!=BINFLAGP) { swapbytes = true; Swap_Nbytes(1,sizeof(testval),&testval); if (testval!=BINFLAGP) { cerr << "Unrecognised binary matrix file format" << endl; return 2; } } // read matrix dimensions (num rows x num cols) unsigned int ival,nx,ny; fs.read((char*)&ival,sizeof(ival)); // ignore the padding (reserved for future use) fs.read((char*)&ival,sizeof(ival)); if (swapbytes) Swap_Nbytes(1,sizeof(ival),&ival); nx = ival; fs.read((char*)&ival,sizeof(ival)); if (swapbytes) Swap_Nbytes(1,sizeof(ival),&ival); ny = ival; // set up and read matrix (rows fast, cols slow) double dval; float fval; if ( (((unsigned int) mres.Ncols())nx)) r1=1; if ((r2<1) || (r2>nx)) r2=nx; if ((c1<1) || (c1>ny)) c1=1; if ((c2<1) || (c2>ny)) c2=ny; // set up and read matrix (rows fast, cols slow) double dval; float fval; if ( (((int) mres.Ncols())<(c2-c1+1)) || (((int) mres.Nrows())<(r2-r1+1)) ) { mres.ReSize(c2-c1+1,r2-r1+1); } for (int y=1; y<=ny; y++) { // cerr<<" y= "<=r1) && (x<=r2) && (y>=c1) && (y<=c2)) { if (swapbytes) Swap_Nbytes(1,sizeof(dval),&dval); mres.time(x-r1+1)=dval; } } else { fs.read((char*)&fval,sizeof(fval)); if ((x>=r1) && (x<=r2) && (y>=c1) && (y<=c2)) { if (swapbytes) Swap_Nbytes(1,sizeof(fval),&fval); mres(x-r1+1,y-c1+1)=fval; } } } } fs.close(); return 0; }