File indexing completed on 2024-12-22 04:18:01

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *   copyright : (C) 2005  University of British Columbia                  *
0006  *                   dscott@phas.ubc.ca                                    *
0007  *                                                                         *
0008  *   This program is free software; you can redistribute it and/or modify  *
0009  *   it under the terms of the GNU General Public License as published by  *
0010  *   the Free Software Foundation; either version 2 of the License, or     *
0011  *   (at your option) any later version.                                   *
0012  *                                                                         *
0013  ***************************************************************************/
0014 
0015 
0016  /* FIXME: rename to something more clear */
0017  /* differences even samples from odd samples.  Used for LFI, whose 
0018     Dicky-switch produces even samples looking at a cold load, and
0019     odd samples looking at the sky */
0020 
0021 
0022 #include "chop.h"
0023 #include "objectstore.h"
0024 #include "ui_chopconfig.h"
0025 
0026 static const QString& VECTOR_IN = "Vector In";
0027 static const QString& VECTOR_OUT_ODD = "Odd Vector";
0028 static const QString& VECTOR_OUT_EVEN = "Even Vector";
0029 static const QString& VECTOR_OUT_DIFFERENCE = "Difference Vector";
0030 static const QString& VECTOR_OUT_INDEX = "Index Vector";
0031 
0032 class ConfigWidgetChopPlugin : public Kst::DataObjectConfigWidget, public Ui_ChopConfig {
0033   public:
0034     ConfigWidgetChopPlugin(QSettings* cfg) : DataObjectConfigWidget(cfg), Ui_ChopConfig() {
0035       _store = 0;
0036       setupUi(this);
0037     }
0038 
0039     ~ConfigWidgetChopPlugin() {}
0040 
0041     void setObjectStore(Kst::ObjectStore* store) { 
0042       _store = store; 
0043       _vector->setObjectStore(store); 
0044     }
0045 
0046     void setupSlots(QWidget* dialog) {
0047       if (dialog) {
0048         connect(_vector, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified()));
0049       }
0050     }
0051 
0052     Kst::VectorPtr selectedVector() { return _vector->selectedVector(); };
0053     void setSelectedVector(Kst::VectorPtr vector) { return _vector->setSelectedVector(vector); };
0054 
0055     virtual void setupFromObject(Kst::Object* dataObject) {
0056       if (ChopSource* source = static_cast<ChopSource*>(dataObject)) {
0057         setSelectedVector(source->vector());
0058       }
0059     }
0060 
0061     virtual bool configurePropertiesFromXml(Kst::ObjectStore *store, QXmlStreamAttributes& attrs) {
0062       Q_UNUSED(store);
0063       Q_UNUSED(attrs);
0064 
0065       bool validTag = true;
0066 
0067 //       QStringRef av;
0068 //       av = attrs.value("value");
0069 //       if (!av.isNull()) {
0070 //         _configValue = QVariant(av.toString()).toBool();
0071 //       }
0072 
0073       return validTag;
0074     }
0075 
0076   public slots:
0077     virtual void save() {
0078       if (_cfg) {
0079         _cfg->beginGroup("Chop DataObject Plugin");
0080         _cfg->setValue("Input Vector", _vector->selectedVector()->Name());
0081         _cfg->endGroup();
0082       }
0083     }
0084 
0085     virtual void load() {
0086       if (_cfg && _store) {
0087         _cfg->beginGroup("Chop DataObject Plugin");
0088         QString vectorName = _cfg->value("Input Vector").toString();
0089         Kst::Object* object = _store->retrieveObject(vectorName);
0090         Kst::Vector* vector = static_cast<Kst::Vector*>(object);
0091         if (vector) {
0092           setSelectedVector(vector);
0093         }
0094         _cfg->endGroup();
0095       }
0096     }
0097 
0098   private:
0099     Kst::ObjectStore *_store;
0100 
0101 };
0102 
0103 
0104 ChopSource::ChopSource(Kst::ObjectStore *store)
0105 : Kst::BasicPlugin(store) {
0106 }
0107 
0108 
0109 ChopSource::~ChopSource() {
0110 }
0111 
0112 
0113 QString ChopSource::_automaticDescriptiveName() const {
0114   return tr("Chop Plugin Object");
0115 }
0116 
0117 
0118 void ChopSource::change(Kst::DataObjectConfigWidget *configWidget) {
0119   if (ConfigWidgetChopPlugin* config = static_cast<ConfigWidgetChopPlugin*>(configWidget)) {
0120     setInputVector(VECTOR_IN, config->selectedVector());
0121   }
0122 }
0123 
0124 
0125 void ChopSource::setupOutputs() {
0126   setOutputVector(VECTOR_OUT_ODD, "");
0127   setOutputVector(VECTOR_OUT_EVEN, "");
0128   setOutputVector(VECTOR_OUT_DIFFERENCE, "");
0129   setOutputVector(VECTOR_OUT_INDEX, "");
0130 }
0131 
0132 
0133 bool ChopSource::algorithm() {
0134   Kst::VectorPtr inputVector = _inputVectors[VECTOR_IN];
0135   Kst::VectorPtr outputVectorOdd = _outputVectors[VECTOR_OUT_ODD];
0136   Kst::VectorPtr outputVectorEven = _outputVectors[VECTOR_OUT_EVEN];
0137   Kst::VectorPtr outputVectorDifference = _outputVectors[VECTOR_OUT_DIFFERENCE];
0138   Kst::VectorPtr outputVectorIndex = _outputVectors[VECTOR_OUT_INDEX];
0139 
0140   //Make sure there is at least 1 element in the input vector
0141   if (inputVector->length() < 1) {
0142     _errorString = tr("Error:  Input Vector invalid size");
0143     return false;
0144   }
0145 
0146   int iLength = inputVector->length();
0147   int iLengthNew = (int)ceil(iLength / 2.0);
0148 
0149   outputVectorOdd->resize(iLengthNew, false);
0150   outputVectorEven->resize(iLengthNew, false);
0151   outputVectorDifference->resize(iLengthNew, false);
0152   outputVectorIndex->resize(iLengthNew, false);
0153 
0154   for (int i = 0; i < iLength; i+=2) {
0155     outputVectorOdd->raw_V_ptr()[i/2] = inputVector->raw_V_ptr()[i];
0156     outputVectorEven->raw_V_ptr()[i/2] = inputVector->raw_V_ptr()[i+1];
0157     outputVectorDifference->raw_V_ptr()[i/2] = inputVector->raw_V_ptr()[i] - inputVector->raw_V_ptr()[i+1];
0158     outputVectorIndex->raw_V_ptr()[i/2] = i/2;
0159   }
0160 
0161   return true;
0162 }
0163 
0164 
0165 Kst::VectorPtr ChopSource::vector() const {
0166   return _inputVectors[VECTOR_IN];
0167 }
0168 
0169 
0170 QStringList ChopSource::inputVectorList() const {
0171   return QStringList( VECTOR_IN );
0172 }
0173 
0174 
0175 QStringList ChopSource::inputScalarList() const {
0176   return QStringList( /*SCALAR_IN*/ );
0177 }
0178 
0179 
0180 QStringList ChopSource::inputStringList() const {
0181   return QStringList( /*STRING_IN*/ );
0182 }
0183 
0184 
0185 QStringList ChopSource::outputVectorList() const {
0186   QStringList vectorList(VECTOR_OUT_ODD);
0187   vectorList += VECTOR_OUT_EVEN;
0188   vectorList += VECTOR_OUT_DIFFERENCE;
0189   vectorList += VECTOR_OUT_INDEX;
0190   return vectorList;
0191 }
0192 
0193 
0194 QStringList ChopSource::outputScalarList() const {
0195   return QStringList( /*SCALAR_OUT*/ );
0196 }
0197 
0198 
0199 QStringList ChopSource::outputStringList() const {
0200   return QStringList( /*STRING_OUT*/ );
0201 }
0202 
0203 
0204 void ChopSource::saveProperties(QXmlStreamWriter &s) {
0205   Q_UNUSED(s);
0206 //   s.writeAttribute("value", _configValue);
0207 }
0208 
0209 
0210 QString ChopPlugin::pluginName() const { return tr("Chop"); }
0211 QString ChopPlugin::pluginDescription() const { return tr("Chops a given data set into odd, even, difference, and Index data sets."); }
0212 
0213 
0214 Kst::DataObject *ChopPlugin::create(Kst::ObjectStore *store, Kst::DataObjectConfigWidget *configWidget, bool setupInputsOutputs) const {
0215 
0216   if (ConfigWidgetChopPlugin* config = static_cast<ConfigWidgetChopPlugin*>(configWidget)) {
0217 
0218     ChopSource* object = store->createObject<ChopSource>();
0219 
0220     if (setupInputsOutputs) {
0221       object->setupOutputs();
0222       object->setInputVector(VECTOR_IN, config->selectedVector());
0223     }
0224 
0225     object->setPluginName(pluginName());
0226 
0227     object->writeLock();
0228     object->registerChange();
0229     object->unlock();
0230 
0231     return object;
0232   }
0233   return 0;
0234 }
0235 
0236 
0237 Kst::DataObjectConfigWidget *ChopPlugin::configWidget(QSettings *settingsObject) const {
0238   ConfigWidgetChopPlugin *widget = new ConfigWidgetChopPlugin(settingsObject);
0239   return widget;
0240 }
0241 
0242 #ifndef QT5
0243 Q_EXPORT_PLUGIN2(kstplugin_ChopPlugin, ChopPlugin)
0244 #endif
0245 
0246 // vim: ts=2 sw=2 et