create_hfile.hh

Go to the documentation of this file.
00001 #ifndef __GRIDPP_CREATE__
00002 #define __GRIDPP_CREATE__
00003 
00004 #include "usempi.h"
00005 #include "iwave_fopen.h"
00006 #include "except.hh"
00007 #include "std_cpp_includes.hh"
00008 
00009 using namespace RVL;
00010 using namespace std;
00011 
00012 // prec=0: float
00013 // prec!=0: double
00014 
00015 void create_hfile(string hfile,int prec=0) {
00016 
00017   if (hfile.size()>120) {
00018     RVLException e;
00019     e<<"Error: create_hfile\n";
00020     e<<"filename "<<hfile<<" longer than 120 chars\n";
00021     throw e;
00022   }
00023 
00024   char * fname=(char *)malloc(128*sizeof(char));
00025   FILE * fp;
00026   string dfile = hfile+"@";
00027 
00028   // first try to open for read
00029   strcpy(fname,hfile.c_str());
00030   if (fp = iwave_fopen(&fname,"r",NULL,stderr)) {
00031     strcpy(fname,dfile.c_str());
00032     fp = iwave_fopen(&fname,"r",NULL,stderr);
00033   }
00034 
00035   // if either header or data file not present, create both
00036 
00037   if (!fp) {
00038 
00039     strcpy(fname,hfile.c_str());
00040     
00041     fp = iwave_fopen(&fname,"w",NULL,stderr);
00042     if (!fp) {
00043       RVLException e;
00044       e<<"Error: create_hfile\n";
00045       e<<"file testgrid.rsf not opened\n";
00046       throw e;
00047     }
00048 
00049     int n1=11;
00050     int n2=21;
00051     int n3=41;
00052     float d1=0.1;
00053     float d2=0.1;
00054     float d3=1.0;
00055     float o1=-0.5;
00056     float o2=-1.0;
00057     float o3=0.0;
00058 
00059     int dim = 2;
00060     int gdim = 3;
00061 
00062     fprintf(fp,"n1=%d\n",n1);
00063     fprintf(fp,"d1=%e\n",d1);
00064     fprintf(fp,"o1=%e\n",o1);
00065       
00066     fprintf(fp,"n2=%d\n",n2);
00067     fprintf(fp,"d2=%e\n",d2);
00068     fprintf(fp,"o2=%e\n",o2);
00069       
00070     fprintf(fp,"n3=%d\n",n3);
00071     fprintf(fp,"d3=%e\n",d3);
00072     fprintf(fp,"o3=%e\n",o3);
00073 
00074     fprintf(fp,"dim=%d\n",dim);
00075     fprintf(fp,"gdim=%d\n",gdim);
00076 
00077     fprintf(fp,"data_format=native_float\n");
00078 
00079     if (prec) fprintf(fp,"data_format=native_double\n");
00080     else fprintf(fp,"data_format=native_float\n");
00081 
00082     fprintf(fp,"data_type = fungus\n");
00083     fprintf(fp,"in=%s\n",dfile.c_str());
00084       
00085     iwave_fclose(fp);
00086     fflush(fp);
00087 
00088     strcpy(fname,dfile.c_str());
00089     float * buf = NULL;
00090     double * dbuf = NULL;
00091     if (prec) {
00092       dbuf=(double *)malloc(n1*n2*n3*sizeof(double));
00093       for (int i=0;i<n1*n2*n3;i++) dbuf[i]=0.0;
00094     }
00095     else {
00096       buf=(float *)malloc(n1*n2*n3*sizeof(float));
00097       for (int i=0;i<n1*n2*n3;i++) buf[i]=0.0;
00098     }
00099       
00100     //      FILE * 
00101     fp = iwave_fopen(&fname,"w",NULL,stderr);
00102     if (!fp) {
00103       RVLException e;
00104       e<<"Error: create_hfile\n";
00105       e<<"file testgrid.rsf not opened\n";
00106       throw e;
00107     }
00108     if (prec) fwrite(dbuf,sizeof(double),n1*n2*n3,fp);
00109     else fwrite(buf,sizeof(float),n1*n2*n3,fp);
00110 
00111     iwave_fclose(fp);
00112 
00113     fflush(fp);
00114 
00115     // reopen prototype files for read
00116     strcpy(fname,hfile.c_str());
00117     fp = iwave_fopen(&fname,"r",NULL,stderr);
00118     strcpy(fname,dfile.c_str());
00119     fp = iwave_fopen(&fname,"r",NULL,stderr);
00120 
00121   }
00122 
00123   free(fname);
00124 }
00125 
00126 float dot() {
00127   int n1=11;
00128   int n2=21;
00129   int n3=41;
00130   float d1=0.1;
00131   float d2=0.1;
00132   float d3=0.1;
00133   return n1*n2*n3*d1*d2*d3;
00134 }
00135 
00136 #endif

Generated on 5 Jan 2017 for IWAVEGRID by  doxygen 1.4.7