orsa_file.h

Go to the documentation of this file.
00001 /* 
00002    ORSA - Orbit Reconstruction, Simulation and Analysis
00003    Copyright (C) 2002-2004 Pasquale Tricarico
00004    
00005    This program is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU General Public License
00007    as published by the Free Software Foundation; either version 2
00008    of the License, or (at your option) any later version.
00009    
00010    As a special exception, Pasquale Tricarico gives permission to
00011    link this program with Qt commercial edition, and distribute the
00012    resulting executable, without including the source code for the Qt
00013    commercial edition in the source distribution.
00014    
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019    
00020    You should have received a copy of the GNU General Public License
00021    along with this program; if not, write to the Free Software
00022    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 */
00024 
00025 #ifndef _ORSA_FILE_H_
00026 #define _ORSA_FILE_H_
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include "config.h"
00030 #endif
00031 
00032 #include "orsa_analysis.h"
00033 #include "orsa_config.h"
00034 #include "orsa_orbit.h"
00035 #ifdef HAVE_GSL
00036 #include "orsa_orbit_gsl.h"
00037 #endif // HAVE_GSL
00038 #include "orsa_universe.h"
00039 #include "orsa_body.h"
00040 #include "orsa_file_jpl.h"
00041 
00042 #ifdef HAVE_LIBZ
00043 #include <zlib.h>
00044 #endif
00045 
00046 #include <string>
00047 #include <map>
00048 #include <list>
00049 #include <cstdio>
00050 
00051 #ifdef HAVE_LIBZ
00052 #define FILE_TYPE gzFile
00053 #define OPEN_FILE gzopen
00054 #define CLOSE_FILE gzclose
00055 #define REWIND_FILE gzrewind
00056 #define GETS_FILE(buffer,length,file) gzgets((file),(buffer),(length))
00057 #define PUTS_FILE(buffer,file) gzputs((file),(buffer))
00058 #define READ_FILE(buffer,size,num,file) gzread((file),(buffer),(size)*(num))
00059 #define WRITE_FILE(buffer,size,num,file) gzwrite((file),(buffer),(size)*(num))
00060 #define SEEK_FILE(file,offset,whence) gzseek((file),(offset),(whence))
00061 #define FLUSH_FILE(file) gzflush((file),Z_FULL_FLUSH)
00062 #define OPEN_READ "rb"
00063 #define OPEN_WRITE "wb"
00064 #else
00065 #define FILE_TYPE FILE*
00066 #define OPEN_FILE fopen
00067 #define CLOSE_FILE fclose
00068 #define REWIND_FILE rewind
00069 #define GETS_FILE(buffer,length,file) fgets((buffer),(length),(file))
00070 #define PUTS_FILE(buffer,file) fputs((buffer),(file))
00071 #define READ_FILE(buffer,size,num,file) fread((buffer),(size),(num),(file))
00072 #define WRITE_FILE(buffer,size,num,file) fwrite((buffer),(size),(num),(file))
00073 #define SEEK_FILE(file,offset,whence) fseek((file),(offset),(whence))
00074 #define FLUSH_FILE(file) fflush((file))
00075 #define OPEN_READ "r"
00076 #define OPEN_WRITE "w"
00077 #endif
00078 
00079 namespace orsa {
00080   
00081   enum FILE_STATUS {CLOSE,OPEN_R,OPEN_W};
00082   
00083   //! File base class
00084   class File {
00085   public:
00086     File() { 
00087       status = CLOSE; 
00088       file   = 0;
00089     }
00090     
00091     virtual ~File() { 
00092       Close(); 
00093     };
00094     
00095   public:
00096     void Close();
00097     
00098   public:
00099     inline virtual std::string GetFileName () const { return filename; }
00100     
00101     inline virtual void SetFileName (std::string name_in) {
00102       if (status != CLOSE) Close();
00103       filename = name_in;
00104     }
00105     
00106     inline virtual void SetFileName (char * name_in) {
00107       std::string n = name_in;
00108       SetFileName (n);
00109     }
00110     
00111   protected:
00112     std::string filename;
00113     FILE_TYPE   file;
00114     FILE_STATUS status;
00115   };
00116   
00117   // intermediate classes 
00118   
00119   //! Read-only files class
00120   class ReadFile : public File {
00121   public:
00122     ReadFile() : File() { }
00123     void Open();
00124     virtual void Read() = 0;
00125   };
00126   
00127   //! Write-only files class
00128   class WriteFile : public File {
00129   public:
00130     WriteFile() : File() { }
00131     void Open();
00132     virtual void Write() = 0;
00133   };
00134   
00135   //! Read and write files class
00136   class ReadWriteFile : public File  {
00137   public:
00138     ReadWriteFile() : File() { }
00139     void Open(const FILE_STATUS st = OPEN_R);
00140     virtual void Read() = 0;
00141     virtual void Write() = 0;
00142   };
00143   
00144   // user-ready classes
00145   
00146   enum M5COLS {C7,C10};
00147   
00148   //! Mercury 5 integration input files
00149   class Mercury5IntegrationFile : public ReadFile { 
00150   
00151   public:
00152     Mercury5IntegrationFile(OrbitStream&);
00153     Mercury5IntegrationFile(OrbitStream&, M5COLS);
00154 
00155   public:
00156     void Read();
00157     
00158     M5COLS cols;
00159     
00160   public:
00161     // bool GuessCorrectType() { return true; }
00162    
00163   private:
00164     OrbitStream *os;
00165   };
00166   
00167   //! Modified Radau input files
00168   class RadauModIntegrationFile : public ReadFile { 
00169     
00170   public:
00171     RadauModIntegrationFile(OrbitStream&);
00172     
00173   public:
00174     // bool GuessCorrectType() { return true; }
00175     
00176   public:
00177     void Read();
00178     
00179   private:
00180     OrbitStream *os;
00181   }; 
00182   
00183   //! SWIFT integration file
00184   class SWIFTFile : public ReadFile {
00185   public:
00186     SWIFTFile(OrbitStream&);
00187     
00188   public:
00189     void Read();
00190     int  AsteroidsInFile();
00191     
00192   public:
00193     // bool GuessCorrectType() { return true; }
00194     
00195   public:
00196     OrbitStream *os;
00197   };
00198   
00199   //! Locations of the observatories
00200   class LocationFile : public ReadFile {
00201   public:
00202     LocationFile();
00203     
00204   public:
00205     void Read();
00206     
00207   public:
00208     Vector ObsPos(const std::string, const Date&);
00209     
00210   public:
00211     std::map<std::string,Location> locations;
00212     
00213   public:
00214     // vector<std::string> codes;
00215     std::list<std::string> codes;
00216   };
00217   
00218   // world visible LocationFile // defined in orsa_universe.cc
00219   extern LocationFile * location_file;
00220   
00221   //! MPC observation file
00222   class MPCObsFile : public ReadFile {
00223   public:
00224     // MPCObsFile(vector<Observation> *obs_in);
00225     MPCObsFile();
00226     
00227   public:
00228     void Read();
00229     
00230   public:
00231     bool ReadNominalOrbit(OrbitWithEpoch &);
00232     
00233   public:
00234     std::vector<Observation> obs;
00235   };
00236   
00237   //! AstDyS observation file, usually with a .rwo extension
00238   class RWOFile : public ReadFile {
00239   public:
00240     RWOFile();
00241     
00242   public:
00243     void Read();
00244     
00245   public:
00246     std::vector<Observation> obs;
00247   };
00248   
00249   //! Read-only asteroid files class
00250   class AsteroidDatabaseFile : public ReadFile {
00251   public:       
00252     AsteroidDatabaseFile() : ReadFile() { db = 0; }
00253   public:
00254     AsteroidDatabase *db;
00255   public:     
00256     inline virtual void read_progress(int, bool&, bool&) { };
00257     inline virtual void read_finished() { };
00258   };
00259   
00260   
00261   //! NEODyS and AstDyS .ctc and .ctm files
00262   class AstDySMatrixFile : public AsteroidDatabaseFile {
00263   public:       
00264     AstDySMatrixFile();
00265     ~AstDySMatrixFile();
00266   public:
00267     void Read();
00268   };
00269   
00270   
00271   //! NEODyS and AstDyS .cat file
00272   class NEODYSCAT : public AsteroidDatabaseFile {
00273   public:
00274     NEODYSCAT();
00275     ~NEODYSCAT();
00276   public:
00277     void Read();
00278   };
00279   
00280   // JPL DASTCOM files
00281   class JPLDastcomNumFile : public AsteroidDatabaseFile {
00282   public:
00283     JPLDastcomNumFile();
00284     virtual ~JPLDastcomNumFile();
00285     void Read();
00286   };  
00287   
00288   class JPLDastcomUnnumFile : public AsteroidDatabaseFile {
00289   public:
00290     JPLDastcomUnnumFile();
00291     virtual ~JPLDastcomUnnumFile();
00292     void Read();
00293   };  
00294    
00295   class JPLDastcomCometFile : public AsteroidDatabaseFile {
00296   public:
00297     JPLDastcomCometFile();
00298     virtual ~JPLDastcomCometFile();
00299     void Read();
00300   };  
00301   
00302   //! Lowell asteroids database file
00303   /* 
00304      class AstorbFile_base {
00305      public:
00306      virtual ~AstorbFile_base() { };
00307      
00308      public:
00309      virtual void read_progress(int,bool&,bool&) { };
00310      virtual void read_finished() { };
00311      };
00312   */
00313   
00314   //! Lowell asteroids database file
00315   // class AstorbFile : public AstorbFile_base, public ReadFile {
00316   // class AstorbFile : public ReadFile {
00317   class AstorbFile : public AsteroidDatabaseFile {
00318     
00319   public:
00320     AstorbFile();
00321     virtual ~AstorbFile();
00322     
00323   public: 
00324     void Read();
00325     //
00326     // inline virtual void read_progress(int, bool &bool_pause, bool &bool_stop) { };
00327     // inline virtual void read_finished() { };
00328     
00329   public:
00330     // bool GuessCorrectType();
00331     
00332   public: 
00333     // Date GetEpoch();
00334     // int GetSize();
00335     
00336     /* public:
00337        AsteroidDatabase *db;
00338     */
00339   };
00340   
00341   //! MPC asteroids database file
00342   // class MPCOrbFile : public ReadFile {
00343   class MPCOrbFile : public AsteroidDatabaseFile {
00344   public:
00345     MPCOrbFile();
00346     ~MPCOrbFile();
00347     
00348   public:
00349     void Read();
00350     
00351   public:
00352     // bool GuessCorrectType();
00353     
00354     /* 
00355        public:
00356        AsteroidDatabase *db;
00357     */
00358   };
00359   
00360   //! MPC comets database file
00361   // class MPCCometFile : public ReadFile {
00362   class MPCCometFile : public AsteroidDatabaseFile {
00363   public:
00364     MPCCometFile();
00365     ~MPCCometFile();
00366     
00367   public:
00368     void Read();
00369     
00370   public:
00371     // bool GuessCorrectType();
00372     
00373     /* 
00374        public:
00375        AsteroidDatabase *db;
00376     */
00377   };
00378   
00379   // NEVER change the numbers
00380   // values are compatible with the zlib return values
00381   enum OrsaFileDataType { 
00382     OFDT_END_OF_FILE=0,
00383     OFDT_UNIVERSE=1,
00384     OFDT_EVOLUTION=2,
00385     OFDT_FRAME=3,
00386     OFDT_BODY=4
00387   };
00388   
00389   inline void convert(OrsaFileDataType &ofdt, const unsigned int i)  {
00390     switch(i) {
00391     case 0: ofdt = OFDT_END_OF_FILE; break;
00392     case 1: ofdt = OFDT_UNIVERSE;    break;
00393     case 2: ofdt = OFDT_EVOLUTION;   break;
00394     case 3: ofdt = OFDT_FRAME;       break;
00395     case 4: ofdt = OFDT_BODY;        break;
00396       //
00397     default:
00398       ORSA_ERROR("conversion problem: i = %i",i);    
00399       break;
00400     }
00401   }
00402   
00403   //! orsa default input-output file  
00404   class OrsaFile : public ReadWriteFile {
00405     
00406   public:
00407     OrsaFile();
00408     
00409   public:
00410     void Read();
00411     void Write();
00412     
00413   public:
00414     static bool GoodFile(const std::string&);
00415     
00416   private:
00417     virtual void make_new_universe(Universe**,length_unit,mass_unit,time_unit,UniverseType,ReferenceSystem,TimeScale);
00418     virtual void make_new_evolution(Evolution**);
00419     
00420   protected:
00421     void Write(Universe**);
00422     void Read( Universe**);
00423     void Write(Evolution**);
00424     void Read( Evolution**);
00425     void Write(Frame*,bool=false);
00426     void Read( Frame*,bool=false);
00427     void Write(Body*);
00428     void Read( Body*);
00429     void Write(BodyWithEpoch*);
00430     void Read( BodyWithEpoch*);
00431     // void Write(Integrator**);
00432     void Write(const Integrator *);
00433     void Read( Integrator**); 
00434     // void Write(Interaction**);
00435     void Write(const Interaction *);
00436     void Read( Interaction**);
00437     void Write(std::string*);
00438     void Read( std::string*);
00439     void Write(orsa::Vector*);
00440     void Read( orsa::Vector*);
00441     void Write(bool*);
00442     void Read( bool*);
00443     void Write(unsigned int*);
00444     void Read( unsigned int*);
00445     void Write(int*);
00446     void Read( int*);
00447     void Write(double*);
00448     void Read( double*);
00449     void Write(IntegratorType*);
00450     void Read( IntegratorType*);
00451     void Write(InteractionType*);
00452     void Read( InteractionType*);
00453     void Write(time_unit*);
00454     void Read( time_unit*);
00455     void Write(length_unit*);
00456     void Read( length_unit*);
00457     void Write(mass_unit*);
00458     void Read( mass_unit*);
00459     void Write(Date*);
00460     void Read( Date*);
00461     void Write(UniverseTypeAwareTime*);
00462     void Read( UniverseTypeAwareTime*);
00463     void Write(UniverseTypeAwareTimeStep*);
00464     void Read( UniverseTypeAwareTimeStep*);
00465     void Write(ReferenceSystem*);
00466     void Read( ReferenceSystem*);
00467     void Write(UniverseType*);
00468     void Read( UniverseType*);
00469     void Write(TimeScale*);
00470     void Read( TimeScale*);
00471     void Write(OrsaFileDataType*);
00472     void Read( OrsaFileDataType*);
00473     void Write(JPL_planets*);
00474     void Read( JPL_planets*);
00475     void Write(TimeStep*);
00476     void Read( TimeStep*);
00477     
00478   private:
00479     size_t read_swap(void *ptr, unsigned int size);
00480     
00481   private:
00482     unsigned int     byte_order;
00483     std::string      orsa_version;
00484     OrsaFileDataType last_ofdt_read;
00485     bool             swap_bytes;
00486   };
00487   
00488   //! orsa configuration file
00489   class OrsaConfigFile : public ReadWriteFile {
00490   public:
00491     OrsaConfigFile();
00492     // OrsaConfigFile(Config*);
00493     
00494     void Read();
00495     void Write();
00496     
00497   private:
00498     std::list<ConfigEnum> list_enum; 
00499     
00500     // private:
00501     // Config *conf;
00502   };
00503   
00504   // a std::string util, to be moved somewhere else in future
00505   inline void remove_leading_trailing_spaces(std::string &s) {
00506     
00507     const int first = s.find_first_not_of(" ");
00508     s.erase(0,first);
00509     
00510     const int last  = s.find_last_not_of(" ");
00511     s.erase(last+1,s.size());
00512   }
00513   
00514   /* 
00515      class OrsaPaths {
00516      public:
00517      OrsaPaths();
00518      static const char * work_path() { return path; }
00519      static char path_separator() { return _path_separator; }
00520      //
00521      static void set_path(char * new_path) { path = strdup(new_path); }
00522      private:
00523      static char * path;
00524      static char _path_separator;
00525      };
00526   */
00527   
00528   class OrsaPaths {
00529   public:
00530     OrsaPaths();
00531     OrsaPaths(const std::string &config_path); // set config path
00532     static const char * work_path() { return path; }
00533     static char path_separator() { return _path_separator; }
00534     //
00535     // static void set_path(char * new_path) { path = strdup(new_path); }
00536   private:
00537     void set_path_separator();
00538     void set_path();
00539     
00540   private:
00541     static char * path;
00542     static char _path_separator;
00543   };
00544   
00545   extern OrsaPaths *orsa_paths;
00546   
00547   //! This class is used to read TLE 
00548   //! (Two Lines Elements) files describing
00549   //! Earth's artificial satellites.
00550   class TLEFile : public ReadFile {
00551   public:
00552     TLEFile();
00553     void Read();
00554   public:
00555     inline virtual void read_progress(int) { };
00556   public:
00557     std::vector<BodyWithEpoch> sat;
00558   };
00559   
00560 } // namespace orsa
00561 
00562 #endif // _ORSA_FILE_H_

Generated on Fri Nov 3 20:37:42 2006 for liborsa by  doxygen 1.4.7