wcreate_hfile.hh

Go to the documentation of this file.
00001 #ifndef __GRIDPP_WCREATE__
00002 #define __GRIDPP_WCREATE__
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 wcreate_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=10;
00050     int n2=21;
00051     int n3=41;
00052     float d1=0.2;
00053     float d2=0.1;
00054     float d3=0.1;
00055     float o1= 0.0;
00056     float o2=-1.0;
00057     float o3=-2.0;
00058 
00059     fprintf(fp,"n1=%d\n",n1);
00060     fprintf(fp,"d1=%e\n",d1);
00061     fprintf(fp,"o1=%e\n",o1);
00062       
00063     fprintf(fp,"n2=%d\n",n2);
00064     fprintf(fp,"d2=%e\n",d2);
00065     fprintf(fp,"o2=%e\n",o2);
00066       
00067     fprintf(fp,"n3=%d\n",n3);
00068     fprintf(fp,"d3=%e\n",d3);
00069     fprintf(fp,"o3=%e\n",o3);
00070 
00071     if (prec) fprintf(fp,"data_format=native_double\n");
00072     else fprintf(fp,"data_format=native_float\n");
00073 
00074     fprintf(fp,"data_type = fungus\n");
00075     fprintf(fp,"in=%s\n",dfile.c_str());
00076       
00077     iwave_fclose(fp);
00078     fflush(fp);
00079 
00080     strcpy(fname,dfile.c_str());
00081     float * buf = NULL;
00082     double * dbuf = NULL;
00083     if (prec) {
00084       dbuf=(double *)malloc(n1*n2*n3*sizeof(double));
00085       for (int i=0;i<n1*n2*n3;i++) dbuf[i]=0.0;
00086     }
00087     else {
00088       buf=(float *)malloc(n1*n2*n3*sizeof(float));
00089       for (int i=0;i<n1*n2*n3;i++) buf[i]=0.0;
00090     }
00091       
00092     //      FILE * 
00093     fp = iwave_fopen(&fname,"w",NULL,stderr);
00094     if (!fp) {
00095       RVLException e;
00096       e<<"Error: create_hfile\n";
00097       e<<"file testgrid.rsf not opened\n";
00098       throw e;
00099     }
00100     if (prec) fwrite(dbuf,sizeof(double),n1*n2*n3,fp);
00101     else fwrite(buf,sizeof(float),n1*n2*n3,fp);
00102 
00103     iwave_fclose(fp);
00104 
00105     fflush(fp);
00106 
00107     // reopen prototype files for read
00108     strcpy(fname,hfile.c_str());
00109     fp = iwave_fopen(&fname,"r",NULL,stderr);
00110     strcpy(fname,dfile.c_str());
00111     fp = iwave_fopen(&fname,"r",NULL,stderr);
00112 
00113   }
00114 
00115   free(fname);
00116 }
00117 
00118 #endif

Generated on 5 Jan 2017 for IWAVEGRID by  doxygen 1.4.7