Department of Scientific Computing   
Institute for Numerical Simulation   
University of Bonn   
Documentation
Download
Programming References
Bug Reports / Suggestions
FAQ
Authors
Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

UDFInformation.hpp

00001 //
00002 // collects some function concerning basic
00003 // informations about UDF files
00004 //
00005 
00006 #ifndef _UDF_INFORMATION_
00007 # define _UDF_INFORMATION_
00008 
00009 # include <stdio.h>
00010 # include <stdlib.h>
00011 # include <string.h>
00012 
00013 #include "UDF.h"
00014 #include "Math.hpp"
00015 #include "Extensions.hpp"
00016 
00017 
00018 template<int DIM>
00019 struct UDFInformation {
00020   double *X  [20] ; // X[i][0], ... ,X[size[i]-1]: Gridlines for ith coord.dir. 
00021   int    size[20],  // 
00022          nel     ;  // size[0]*..*size[DIM-1], actual dimension
00023 
00024   double mi,ma ; // min/max of scalar data
00025 
00026   bool ascii,do_swap,scan,read ;
00027   FILE *lastfile ;
00028 
00029     UDFInformation() ;
00030    ~UDFInformation() ;
00031 
00032     void ReadHeader(FILE *fid, Extensions<DIM> *E) ; // fid must be pointer to an open file
00033     void Scan      (FILE *fid) ;
00034     void Print     () ;
00035 } ;
00036 
00037 
00038 //  
00039 //  Implementations
00040 //
00041 
00042 template<int DIM>
00043 UDFInformation<DIM>::UDFInformation() {
00044  for (size_t i=0; i<sizeof(size)/sizeof(size[0]); i++) {
00045    X[i]   =NULL ;
00046    size[i]=0 ;
00047  }
00048  ma=mi=0 ;
00049  
00050  do_swap= (UDF_CPUisBigEndian()==0) ;
00051  read=false;
00052  lastfile=NULL ;
00053  scan=false ; 
00054 }
00055 
00056 template<int DIM>
00057 UDFInformation<DIM>::~UDFInformation() {
00058  for (size_t i=0; i<sizeof(size)/sizeof(size[0]); i++)
00059    if (X[i]!=NULL) delete []X[i] ;
00060 }
00061 
00062 
00063 //
00064 // Print UDFinformation
00065 //
00066 template<int DIM>
00067 void UDFInformation<DIM>::Print() {
00068  int i,j ;
00069  if (!read) {
00070    std::cout<< "no header read\n" ;
00071    exit(-1) ;
00072  }
00073  std::cout<< "UDF file statistics\n" ;
00074  std::cout<< "number of elements: "<<nel<<" = "<<size[0] ;
00075  for (i=1; i<DIM; i++) std::cout<<'x'<<size[i] ;
00076  std::cout<<"\n" ;
00077  
00078  if (ascii) std::cout<<"ascii\n"; else std::cout<<"binary\n" ;
00079  if (scan) {
00080    std::cout<<"Data scaned\n" ;
00081    std::cout<<"min="<<mi<<" , max="<<ma<<"\n" ;
00082  }
00083 }
00084 
00085 //
00086 // read UDF header
00087 //
00088 template<int DIM>
00089 void UDFInformation<DIM>::ReadHeader(FILE *fid, Extensions<DIM> *Ext) {
00090  int i,si,j,N,TypeID, st1,st2 ;
00091  char s[1000],fd[100],c ;
00092  float  fx ;
00093  double x  ;
00094 
00095  read=true  ;
00096  lastfile=fid   ;
00097  scan=false ;
00098 
00099  fscanf(fid,"%s %s %s %s %s",s,s,s,s,s) ; // # vtk ....
00100  fscanf(fid,"%s",s) ;                     // UDF-Data 
00101  // read extensions:
00102  if (strcmp(s,"UDF-DataSetEXT00")==0) {
00103    fscanf(fid,"%d %d %d %d",&N,&TypeID,&st1,&st2) ;
00104    bool ms=false ;
00105    for (i=0; i<DIM; i++) {
00106      si=1<<i ;
00107      Ext->islifting   [i] =  ((st1&si) != 0);
00108      Ext->ismultiscale[i] =  ((st2&si) != 0);
00109      if (Ext->ismultiscale[i]) ms=true ;    
00110      fscanf(fid,"%d %d", &Ext->BC[i][0], &Ext->BC[i][1]) ;
00111      if ( (Ext->BC[i][1]!=-1) && (Ext->BC[i][1]!=0) && (Ext->BC[i][1]!=1) ) Ext->BC[i][1]=PERIODIC ;
00112    }
00113 
00114    // if multiscale representation, then check consistency of wavelet basis 
00115    if (ms && ( (N!=Ext->WC->N)||(TypeID!=Ext->WC->TypeID(Ext->WC->type)) ) ) {
00116      std::cout<<"File to read contains wavelet coefficients w.r.t wavelets with ID("<<N<<','<<TypeID<<") but current wavelets have ID (";
00117      std::cout<<Ext->WC->N<<','<<Ext->WC->TypeID(Ext->WC->type)<<"). This may cause wrong results\n" ;
00118      assert(0) ;
00119    }
00120 
00121  } else {
00122    for (i=0; i<DIM; i++) {
00123      Ext->BC[i][0]=Ext->BC[i][1] = -MAXINT ;
00124      Ext->islifting   [i]=false ;
00125      Ext->ismultiscale[i]=true  ;
00126    }
00127  }
00128  
00129  fscanf(fid,"%s",s) ;                     // ASCII/BINARY
00130 
00131  ascii=strcmp(s,"ASCII")==0 ;
00132 
00133  fscanf(fid,"%s %s",s,s) ; // DATASET RECTI....
00134  fscanf(fid,"%s",s) ;      // DIMENSION
00135 
00136  // Dimensions
00137  nel=1 ;
00138  for (i=0; i<max(DIM , 3); i++) {
00139    fscanf(fid,"%d",&si) ;
00140 
00141    if (size[i]>0) {
00142      if (X[i]==NULL) { std::cout<<"size="<<size[i]<<"\n"; exit(-1) ;}
00143      if (si>size[i]) {
00144        delete []X[i] ;
00145        X[i]=new double[si];
00146      }
00147    } else {
00148      X[i]=new double[si];
00149    }
00150      
00151    size[i]=si ;
00152    nel *=si   ;
00153 
00154  }
00155 
00156  // coordinates
00157  for (i=0; i<max(DIM,3); i++) {
00158    fscanf(fid,"%s %s %s",s,s,fd) ; // X_COORDINATES xx float / double
00159    fscanf(fid,"%c",&c)     ; // EOL
00160 
00161    if (i<DIM) { Ext->XA[i]=HUGE_VAL, Ext->XE[i]=-HUGE_VAL ;}
00162    for (j=0; j<size[i]; j++) { // read all values
00163      if (ascii) {
00164        fscanf(fid,"%le",&x) ;
00165      } else {
00166        if (strcmp(fd,"float")==0) {
00167          fread(&fx,sizeof(float),1,fid) ;
00168          if (do_swap) UDF_swap(&fx,sizeof(float),1) ;
00169          x=(double)fx ;
00170        } else {
00171          fread(&x,sizeof(double),1,fid) ;
00172          if (do_swap) UDF_swap(&x,sizeof(double),1) ;
00173        }
00174      } 
00175      if (i<DIM) {
00176        X[i][j]=x ;
00177        Ext->XA[i]=min(Ext->XA[i],x) ;
00178        Ext->XE[i]=max(Ext->XE[i],x) ;
00179      }
00180    } //
00181 
00182  }
00183 }
00184 
00185 //
00186 // just read the data and extract some information about the scalars
00187 //
00188 template<int DIM>
00189 void UDFInformation<DIM>::Scan(FILE *fid) {
00190  int i,n ;
00191  char s[1000], fd[100],c ;
00192  float fx ;
00193  double x ;
00194 
00195  if (lastfile==fid) scan=true ;
00196 
00197   // Read Data
00198  fscanf(fid,"%s %s",s,s)       ; // POINT_DATA xx
00199  fscanf(fid,"%s %s %s",s,s,fd) ; // SCALARS bla
00200  fscanf(fid,"%s %s",s,s)       ; // LOOKUP_TABLE default
00201  fscanf(fid,"%c",&c)           ; // EOL 
00202   
00203  mi= HUGE_VAL ;
00204  ma=-HUGE_VAL ;
00205  for (i=0; i<nel; i++) {
00206 
00207    if (ascii) {
00208      fscanf(fid,"%le",&x) ;
00209     } else {
00210      if (strcmp(fd,"float")==0) { 
00211        fread(&fx,sizeof(float),1,fid) ;
00212        if (do_swap) UDF_swap(&fx,sizeof(float),1) ; 
00213        x=(double)fx ;
00214      } else {
00215        fread(&x,sizeof(double),1,fid) ;
00216        if (do_swap) UDF_swap(&x,sizeof(double),1) ;
00217      }
00218     } // binary
00219 
00220    mi=min(mi,x) ;
00221    ma=max(ma,x) ;
00222  } // i
00223  fclose(fid) ;
00224 }
00225 
00226 #endif
00227 

Generated at Mon Aug 19 10:02:32 2002 for AWFD by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001