00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00020 
00022 
00033 
00034 
00036 
00037 #include <stdexcept>
00038 #include <math.h>
00039 #include <cfloat>
00040 #include "arts.h"
00041 #include "matpackI.h"
00042 #include "array.h"
00043 #include "messages.h"
00044 #include "file.h"
00045 
00046 
00047 
00049 
00051 
00053 
00064 void filename_ascii(
00065               String&  filename,
00066         const String&  varname )
00067 {
00068   if ( "" == filename )
00069   {
00070     extern const String out_basename;                       
00071     filename = out_basename+"."+varname+".aa";
00072   }
00073 }
00074 
00075 
00076 
00078 
00080 
00082 
00091 void open_output_file(ofstream& file, const String& name)
00092 {
00093   
00094   
00095   
00096   
00097   
00098   file.exceptions(ios::badbit |
00099                   ios::failbit);
00100   
00101   
00102   file.open(name.c_str() );
00103 
00104   
00105   
00106   
00107   
00108   
00109   if (!file)
00110     {
00111       ostringstream os;
00112       os << "Cannot open output file: " << name << '\n'
00113          << "Maybe you don't have write access "
00114          << "to the directory or the file?";
00115       throw runtime_error(os.str());
00116     }
00117 }
00118 
00119 
00120 
00122 
00130 void open_input_file(ifstream& file, const String& name)
00131 {
00132   
00133   
00134   
00135   
00136   file.exceptions(ios::badbit);
00137 
00138   
00139   file.open(name.c_str() );
00140 
00141   
00142   
00143   
00144   if (!file)
00145     {
00146       ostringstream os;
00147       os << "Cannot open input file: " << name << '\n'
00148          << "Maybe the file does not exist?";
00149       throw runtime_error(os.str());
00150     }
00151 }
00152 
00153 
00154 
00156 
00165 void read_text_from_stream(ArrayOfString& text, istream& is)
00166 {
00167   String linebuffer;
00168 
00169   
00170   
00171   
00172   
00173   while (is && !is.eof())
00174     {
00175       
00176       getline(is,linebuffer);
00177 
00178       
00179       text.push_back(linebuffer);
00180     }
00181   
00182   
00183   
00184   
00185   if ( !is.eof() ) {
00186     ostringstream os;
00187     os << "Read Error. Last line read:\n" << linebuffer;
00188     throw runtime_error(os.str());
00189   }
00190 
00191 }
00192 
00193 
00194 
00196 
00206 void read_text_from_file(ArrayOfString& text, const String& name)
00207 {
00208   ifstream ifs;
00209 
00210   
00211   open_input_file(ifs, name);
00212   
00213   
00214 
00215   
00216   
00217   
00218   try
00219     {
00220       read_text_from_stream(text,ifs);
00221     }
00222   catch (runtime_error x)
00223     {
00224       ostringstream os;
00225       os << "Error reading file: " << name << '\n'
00226          << x.what();
00227       throw runtime_error(os.str());
00228     }
00229 }
00230 
00231 
00232 
00234 
00242 void replace_all(String& s, const String& what, const String& with)
00243 {
00244   Index j = s.find(what);
00245   while ( j != s.npos )
00246     {
00247       s.replace(j,1,with);
00248       j = s.find(what,j+with.size());
00249     }
00250 }
00251 
00252 
00253 
00255 
00257 
00259 
00268 void write_array_of_matrix_to_stream(ostream& os,
00269                                      const ArrayOfMatrix& am)
00270 {
00271   extern const String full_name;
00272 
00273   
00274   
00275   Index precision;
00276   switch (sizeof(Numeric)) {
00277   case sizeof(float)  : precision = FLT_DIG; break;
00278   case sizeof(double) : precision = DBL_DIG; break;
00279   default: out0 << "Numeric must be double or float\n"; exit(1);
00280   }
00281 
00282   os << "# Generated by "
00283      << full_name << ", "
00284      << __DATE__ << ", "
00285      << __TIME__ << "\n";
00286 
00287   
00288   const Index n = am.nelem();
00289   os << n << '\n';
00290 
00291   for (Index i=0; i<n; ++i)
00292     {
00293       
00294       os << am[i].nrows() << ' ' << am[i].ncols() << '\n';
00295 
00296       os << setprecision(precision);
00297       
00298       for (Index r=0; r<am[i].nrows(); ++r)
00299         {
00300           os << am[i](r,0);
00301       
00302           for (Index c=1; c<am[i].ncols(); ++c)
00303             {
00304               os << " " << am[i](r,c);
00305             }
00306 
00307           os << '\n';
00308         }
00309     }
00310 }
00311 
00312 
00313 
00315 
00322 void write_array_of_matrix_to_file(const String& filename,
00323                                    const ArrayOfMatrix& am)
00324 {
00325   ofstream of;
00326 
00327   out2 << "  Writing " << filename << '\n';
00328   open_output_file(of, filename);
00329 
00330   
00331   write_array_of_matrix_to_stream(of,am);
00332 }
00333 
00343 void skip_comments(istream & is)
00344 {
00345   bool comments=true;
00346   char c;
00347   String linebuffer;
00348   while (comments)
00349     {
00350       is >> ws;
00351       is.get(c);
00352       if ('#'==c)
00353         getline(is,linebuffer);
00354       else if ('<'==c)
00355         throw runtime_error ("Invalid character (<) in input stream.\nAre you probably trying to read an XML file?");
00356       else
00357         {
00358           is.unget();
00359           comments = false;
00360         }
00361     }
00362 }
00363 
00365 
00371 void read_array_of_matrix_from_stream(ArrayOfMatrix& am,
00372                                       istream& is)
00373 {
00374   Index n;
00375   skip_comments(is);   
00376   is >> n;
00377   
00378   am.resize(n);
00379   for( Index i=0; i<n; ++i )
00380     {
00381       
00382       {
00383         Index nr,nc;
00384         skip_comments(is);   
00385         is >> nr >> nc;
00386         
00387         
00388         am[i].resize(nr,nc);
00389         
00390         for(Index ir=0; ir<nr; ++ir)
00391           for(Index ic=0; ic<nc; ++ic)
00392             {
00393               
00394               skip_comments(is);   
00395               is >> am[i](ir,ic);
00396             }
00397       }
00398     }
00399 
00400   if ( is.fail() || is.bad() )
00401     throw runtime_error("Stream gave fail or bad.");
00402 
00403   is >> ws;
00404 
00405   if ( !is.eof() )
00406     throw runtime_error("Input finished, but end of stream not reached.");
00407 
00408   
00409   out3 << "  Dimensions:\n";
00410   out3 << "     "<< am.nelem() << "\n";
00411   for ( Index i=0; i<am.nelem(); ++i )
00412     out3 << "     " << am[i].nrows() << ", " << am[i].ncols() << "\n";
00413 }
00414 
00415 
00416 
00418 
00425 void read_array_of_matrix_from_file(ArrayOfMatrix& am,
00426                                     const String& filename)
00427 {
00428   ifstream ifs;
00429 
00430   out2 << "  Reading " << filename << '\n';
00431   
00432   
00433   open_input_file(ifs, filename);
00434   
00435   
00436 
00437   
00438   
00439   
00440   try
00441     {
00442       read_array_of_matrix_from_stream(am,ifs);
00443     }
00444   catch (runtime_error x)
00445     {
00446       ostringstream os;
00447       os << "Error reading file: " << filename << '\n'
00448          << x.what();
00449       throw runtime_error(os.str());
00450     }
00451 }
00452 
00453 
00454 
00456 
00458 
00460 
00469 void write_array_of_String_to_stream(
00470               ostream&         os,
00471         const ArrayOfString&   as )
00472 {
00473   extern const String full_name;
00474 
00475   os << "# Generated by " << full_name << "\n";
00476 
00477   
00478   const Index n = as.nelem();
00479   os << n << '\n';
00480 
00481   for (Index i=0; i<n; ++i)
00482     os << as[i] << '\n';
00483 }
00484 
00485 
00486 
00488 
00497 void write_array_of_String_to_file(
00498         const String&          filename,
00499         const ArrayOfString&   as )
00500 {
00501   ofstream of;
00502 
00503   out2 << "  Writing " << filename << '\n';
00504   open_output_file(of, filename);
00505 
00506   
00507   write_array_of_String_to_stream(of,as);
00508 }
00509 
00510 
00511 
00513 
00522 void read_array_of_String_from_stream(
00523         ArrayOfString&   as,
00524         istream&         is )
00525 {
00526   
00527   
00528   bool comments=true;
00529   char c;
00530   String linebuffer;
00531   while (comments)
00532     {
00533       is >> ws;
00534       is.get(c);
00535       if ('#'==c)
00536           getline(is,linebuffer);
00537 
00538       else
00539         {
00540           is.unget();
00541           comments = false;
00542         }
00543     }
00544 
00545   
00546   {
00547     Index n;
00548     is >> n;
00549     
00550     as.resize(n);
00551     for( Index i=0; i<n; ++i )
00552       {
00553         
00554         is >> as[i];
00555       }
00556   }
00557 
00558   if ( is.fail() || is.bad() )
00559     throw runtime_error("Stream gave fail or bad.");
00560 
00561   is >> ws;
00562   if ( !is.eof() )
00563     throw runtime_error("Input finished, but end of stream not reached.");
00564 
00565   
00566   out3 << "  Dimension: "
00567        << as.nelem() << ", ";
00568 }
00569 
00570 
00571 
00573 
00582 void read_array_of_String_from_file(
00583            ArrayOfString&   as,
00584      const String&          filename )
00585 {
00586   ifstream ifs;
00587 
00588   out2 << "  Reading " << filename << '\n';
00589   
00590   
00591   open_input_file(ifs, filename);
00592   
00593   
00594 
00595   
00596   
00597   
00598   try
00599     {
00600       read_array_of_String_from_stream(as,ifs);
00601     }
00602   catch (runtime_error x)
00603     {
00604       ostringstream os;
00605       os << "Error reading file: " << filename << '\n'
00606          << x.what();
00607       throw runtime_error(os.str());
00608     }
00609 }
00610 
00612 
00621 void write_tag_groups_species_to_stream(
00622               ostream&     os,
00623         const TagGroups&   tgs )
00624 {
00625   extern const String full_name;
00626   extern const Array<SpeciesRecord> species_data;
00627       
00628   os << "# Generated by " << full_name << "\n";
00629 
00630   
00631   const Index n = tgs.nelem();
00632   os << n << '\n';
00633 
00634   for (Index i=0; i<n; ++i)
00635     {
00636       if ( tgs[i].nelem() > 0 )
00637         {
00638           
00639           const  SpeciesRecord& spr = species_data[tgs[i][0].Species()];
00640           os << spr.Name() << '\n';
00641         }
00642     }
00643 }
00644 
00645 
00647 
00656 void write_tag_groups_species_to_file(
00657         const String&          filename,
00658         const TagGroups&   tgs )
00659 {
00660   ofstream of;
00661 
00662   out2 << "  Writing " << filename << '\n';
00663   open_output_file(of, filename);
00664 
00665   
00666   write_tag_groups_species_to_stream(of,tgs);
00667 }