Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

JulianizeVMatrix.cc

Go to the documentation of this file.
00001 00002 // -*- C++ -*- 00003 00004 // JulianizeVMatrix.cc 00005 // 00006 // Copyright (C) 2003 Nicolas Chapados 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 /* ******************************************************* 00037 * $Id: JulianizeVMatrix.cc,v 1.9 2004/07/21 16:30:55 chrish42 Exp $ 00038 ******************************************************* */ 00039 00041 #include "JulianizeVMatrix.h" 00042 #include <plearn/base/PDate.h> 00043 #include <plearn/base/PDateTime.h> 00044 00045 namespace PLearn { 00046 using namespace std; 00047 00048 00049 PLEARN_IMPLEMENT_OBJECT(JulianizeVMatrix, "ONE LINE DESCR", 00050 "JulianizeVMatrix provides a conversion from a VMat containing dates\n" 00051 "in an explicit 3-column (YYYY,MM,DD) or 6-column (YYYY,MM,DD,HH,MM,SS)\n" 00052 "format to a Julian day number format (including fractional part to\n" 00053 "represent the hour within the day). The dates can be at any columns,\n" 00054 "not only columns 0-2 (or 0-5). More than a single date can be\n" 00055 "converted.\n"); 00056 00057 00058 JulianizeVMatrix::JulianizeVMatrix() 00059 : inherited() 00060 /* all other compiler-supplied defaults are reasonable */ 00061 { } 00062 00063 JulianizeVMatrix::JulianizeVMatrix(VMat underlying, 00064 DateCode date_code, 00065 int starting_column) 00066 : inherited(underlying->length(), newWidth(underlying, date_code)), 00067 underlying_(underlying), 00068 cols_codes_(1), und_row_(underlying.width()) 00069 { 00070 cols_codes_[0] = make_pair(starting_column, date_code); 00071 setVMFields(); 00072 } 00073 00074 00075 void JulianizeVMatrix::getNewRow(int i, const Vec& v) const 00076 { 00077 underlying_->getRow(i, und_row_); 00078 00079 Vec::iterator 00080 src_beg = und_row_.begin(), 00081 src_it = und_row_.begin(), 00082 src_end = und_row_.end(), 00083 dst_it = v.begin(); 00084 vector< pair<int,DateCode> >::const_iterator 00085 codes_it = cols_codes_.begin(), 00086 codes_end = cols_codes_.end(); 00087 00088 for ( ; codes_it < codes_end ; ++codes_it ) { 00089 // Copy what comes before the current date 00090 dst_it = copy(src_it, src_beg + codes_it->first, dst_it); 00091 src_it = src_beg + codes_it->first; 00092 00093 // Now convert the date per se 00094 double converted_date = 0; 00095 int YYYY,MM,DD,hh,mm,ss; 00096 YYYY = int(*src_it++); 00097 MM = int(*src_it++); 00098 DD = int(*src_it++); 00099 switch(codes_it->second) { 00100 case Date: 00101 converted_date = PDate(YYYY,MM,DD).toJulianDay(); 00102 break; 00103 case DateTime: 00104 hh = int(*src_it++); 00105 mm = int(*src_it++); 00106 ss = int(*src_it++); 00107 converted_date = PDateTime(YYYY,MM,DD,hh,mm,ss).toJulianDay(); 00108 break; 00109 } 00110 *dst_it++ = converted_date; 00111 } 00112 00113 // And now copy what comes after the last date 00114 copy(src_it, src_end, dst_it); 00115 } 00116 00117 void JulianizeVMatrix::declareOptions(OptionList& ol) 00118 { 00119 // ### Declare all of this object's options here 00120 // ### For the "flags" of each option, you should typically specify 00121 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00122 // ### OptionBase::tuningoption. Another possible flag to be combined with 00123 // ### is OptionBase::nosave 00124 00125 // ### ex: 00126 // declareOption(ol, "myoption", &JulianizeVMatrix::myoption, OptionBase::buildoption, 00127 // "Help text describing this option"); 00128 // ... 00129 00130 // Now call the parent class' declareOptions 00131 inherited::declareOptions(ol); 00132 } 00133 00134 void JulianizeVMatrix::build_() 00135 { 00136 // No options to build at this point 00137 } 00138 00139 // ### Nothing to add here, simply calls build_ 00140 void JulianizeVMatrix::build() 00141 { 00142 inherited::build(); 00143 build_(); 00144 } 00145 00146 void JulianizeVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00147 { 00148 inherited::makeDeepCopyFromShallowCopy(copies); 00149 deepCopyField(underlying_, copies); 00150 deepCopyField(und_row_, copies); 00151 // cols_codes_ already deep-copied since it is an STL vector 00152 } 00153 00154 void JulianizeVMatrix::setVMFields() 00155 { 00156 Array<VMField>& orig_fields = underlying_->getFieldInfos(); 00157 int new_field = 0; 00158 int cur_field = 0, end_field = orig_fields.size(); 00159 vector< pair<int,DateCode> >::iterator it = cols_codes_.begin(), 00160 end = cols_codes_.end(); 00161 00162 for ( ; cur_field < end_field ; ++cur_field, ++new_field) { 00163 // We've got a date field 00164 if (it != end && it->first == cur_field) { 00165 switch(it->second) { 00166 case Date: 00167 this->declareField(new_field, "Date", VMField::Date); 00168 break; 00169 case DateTime: 00170 this->declareField(new_field, "DateTime", VMField::Date); 00171 break; 00172 } 00173 cur_field += dateCodeWidth(it->second)-1; 00174 ++it; 00175 } 00176 else { 00177 this->declareField(new_field, orig_fields[cur_field].name, 00178 orig_fields[cur_field].fieldtype); 00179 } 00180 } 00181 } 00182 00183 00184 } // end of namespace PLearn 00185

Generated on Tue Aug 17 15:56:14 2004 for PLearn by doxygen 1.3.7