PLearn 0.1
|
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 5557 2006-05-10 20:36:58Z lamblin $ 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, 00050 "Converts dates in a several field format to a single" 00051 " number format", 00052 "JulianizeVMatrix provides a conversion from a VMat containing dates\n" 00053 "in an explicit 3-column (YYYY,MM,DD) or 6-column (YYYY,MM,DD,HH,MM,SS)\n" 00054 "format to a Julian day number format (including fractional part to\n" 00055 "represent the hour within the day). The dates can be at any columns,\n" 00056 "not only columns 0-2 (or 0-5). More than a single date can be\n" 00057 "converted.\n"); 00058 00059 00060 JulianizeVMatrix::JulianizeVMatrix(bool call_build_) 00061 : inherited(call_build_) 00062 /* all other compiler-supplied defaults are reasonable */ 00063 { 00064 if( call_build_ ) 00065 build_(); 00066 } 00067 00068 JulianizeVMatrix::JulianizeVMatrix(VMat the_source, 00069 DateCode date_code, 00070 int starting_column, 00071 bool call_build_) 00072 : inherited(the_source, 00073 the_source->length(), 00074 newWidth(the_source, date_code), 00075 call_build_), 00076 cols_codes(1), 00077 source_row(the_source.width()) 00078 { 00079 cols_codes[0] = make_pair(starting_column, date_code); 00080 setVMFields(); 00081 } 00082 00083 00084 void JulianizeVMatrix::getNewRow(int i, const Vec& v) const 00085 { 00086 source->getRow(i, source_row); 00087 00088 Vec::iterator 00089 src_beg = source_row.begin(), 00090 src_it = source_row.begin(), 00091 src_end = source_row.end(), 00092 dst_it = v.begin(); 00093 vector< pair<int,DateCode> >::const_iterator 00094 codes_it = cols_codes.begin(), 00095 codes_end = cols_codes.end(); 00096 00097 for ( ; codes_it < codes_end ; ++codes_it ) { 00098 // Copy what comes before the current date 00099 dst_it = copy(src_it, src_beg + codes_it->first, dst_it); 00100 src_it = src_beg + codes_it->first; 00101 00102 // Now convert the date per se 00103 double converted_date = 0; 00104 int YYYY,MM,DD,hh,mm,ss; 00105 YYYY = int(*src_it++); 00106 MM = int(*src_it++); 00107 DD = int(*src_it++); 00108 switch(codes_it->second) { 00109 case Date: 00110 converted_date = PDate(YYYY,MM,DD).toJulianDay(); 00111 break; 00112 case DateTime: 00113 hh = int(*src_it++); 00114 mm = int(*src_it++); 00115 ss = int(*src_it++); 00116 converted_date = PDateTime(YYYY,MM,DD,hh,mm,ss).toJulianDay(); 00117 break; 00118 } 00119 *dst_it++ = converted_date; 00120 } 00121 00122 // And now copy what comes after the last date 00123 copy(src_it, src_end, dst_it); 00124 } 00125 00126 void JulianizeVMatrix::declareOptions(OptionList& ol) 00127 { 00128 // ### Declare all of this object's options here 00129 // ### For the "flags" of each option, you should typically specify 00130 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00131 // ### OptionBase::tuningoption. Another possible flag to be combined with 00132 // ### is OptionBase::nosave 00133 00134 // ### ex: 00135 // declareOption(ol, "myoption", &JulianizeVMatrix::myoption, OptionBase::buildoption, 00136 // "Help text describing this option"); 00137 // ... 00138 00139 // Now call the parent class' declareOptions 00140 inherited::declareOptions(ol); 00141 } 00142 00143 void JulianizeVMatrix::build_() 00144 { 00145 // No options to build at this point 00146 setMetaInfoFromSource(); 00147 } 00148 00149 // ### Nothing to add here, simply calls build_ 00150 void JulianizeVMatrix::build() 00151 { 00152 inherited::build(); 00153 build_(); 00154 } 00155 00156 void JulianizeVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00157 { 00158 inherited::makeDeepCopyFromShallowCopy(copies); 00159 deepCopyField(source_row, copies); 00160 // cols_codes already deep-copied since it is an STL vector 00161 } 00162 00163 void JulianizeVMatrix::setVMFields() 00164 { 00165 Array<VMField>& orig_fields = source->getFieldInfos(); 00166 int new_field = 0; 00167 int cur_field = 0, end_field = orig_fields.size(); 00168 vector< pair<int,DateCode> >::iterator it = cols_codes.begin(), 00169 end = cols_codes.end(); 00170 00171 for ( ; cur_field < end_field ; ++cur_field, ++new_field) { 00172 // We've got a date field 00173 if (it != end && it->first == cur_field) { 00174 switch(it->second) { 00175 case Date: 00176 this->declareField(new_field, "Date", VMField::Date); 00177 break; 00178 case DateTime: 00179 this->declareField(new_field, "DateTime", VMField::Date); 00180 break; 00181 } 00182 cur_field += dateCodeWidth(it->second)-1; 00183 ++it; 00184 } 00185 else { 00186 this->declareField(new_field, orig_fields[cur_field].name, 00187 orig_fields[cur_field].fieldtype); 00188 } 00189 } 00190 } 00191 00192 00193 } // end of namespace PLearn 00194 00195 00196 /* 00197 Local Variables: 00198 mode:c++ 00199 c-basic-offset:4 00200 c-file-style:"stroustrup" 00201 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00202 indent-tabs-mode:nil 00203 fill-column:79 00204 End: 00205 */ 00206 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :