File indexing completed on 2024-12-22 04:17:23

0001 /***************************************************************************
0002                           vector.h  -  description
0003                              -------------------
0004     begin                : Fri Sep 22 2000
0005     copyright            : (C) 2000 by cbn
0006     email                : netterfield@astro.utoronto.ca
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #ifndef VECTOR_H
0019 #define VECTOR_H
0020 
0021 #include <math.h>
0022 
0023 #include <QPointer>
0024 #include <QFile>
0025 
0026 #include "primitive.h"
0027 #include "scalar.h"
0028 #include "string_kst.h"
0029 #include "labelinfo.h"
0030 #include "kst_export.h"
0031 
0032 class QXmlStreamWriter;
0033 
0034 namespace Equation {
0035   class Node;
0036 }
0037 
0038 namespace Kst {
0039 
0040 class KstDataObject;
0041 
0042 // KST::interpolate is still too polluting
0043 KSTCORE_EXPORT double kstInterpolate(double *v, int _size, int in_i, int ns_i);
0044 //KSTCORE_EXPORT double kstInterpolateNoHoles(double *v, int _size, int in_i, int ns_i);
0045 
0046 class Vector;
0047 typedef SharedPtr<Vector> VectorPtr;
0048 
0049 #define MAX_N_DESPIKE_STAT 10000
0050 
0051 /**A class for handling data vectors for kst.
0052  *@author cbn
0053  */
0054 class KSTCORE_EXPORT Vector : public Primitive 
0055 {
0056     Q_OBJECT
0057 
0058   public:
0059     virtual const QString& typeString() const;
0060     static const QString staticTypeString;
0061     static const QString staticTypeTag;
0062 
0063 
0064   protected:
0065     Vector(ObjectStore *store);
0066     virtual ~Vector();
0067 
0068     friend class ObjectStore; 
0069 
0070     virtual void _initializeShortName();
0071 
0072 
0073   public:
0074     void change(QByteArray& data);
0075     void oldChange(QByteArray& data);
0076 
0077     inline int length() const { return _size; }
0078 
0079     /** Return V[i], interpolated/decimated to have ns_i total samples */
0080     double interpolate(int i, int ns_i) const;
0081 
0082     /** Return V[i], interpolated/decimated to have ns_i total samples, without any NaNs */
0083     double interpolateNoHoles(int i, int ns_i) const;
0084 
0085     /** Return V[i] uninterpolated */
0086     double value(int i) const;
0087     double noNanValue(int i);
0088 
0089     /** Return a pointer to data to be read */
0090     /** these might be modified for output */
0091     /** eg - by masking */
0092     double const *value() const { return _v_out;}
0093     double const *noNanValue();
0094 
0095     /** raw pointer for writing */
0096     /** reading it will not provide */
0097     /** mask filtering and is probably */
0098     /** not what you want. */
0099     double *raw_V_ptr() { return _v_raw;}
0100 
0101     /** Return Minimum value in Vector */
0102     inline double min() const { return _min; }
0103 
0104     /** Return max value in Vector */
0105     inline double max() const { return _max; }
0106 
0107     /** Return SpikeInsensitive max value in vector **/
0108     double ns_max(int ns_zoom_level);
0109 
0110     /** Return SpikeInsensitive min value in vector **/
0111     double ns_min(int ns_zoom_level);
0112 
0113     /** Return Mean value in Vector */
0114     inline double mean() const { return _mean; }
0115 
0116     /** Return Least Positive value in Vector */
0117     inline double minPos() const { return _minPos; }
0118 
0119     /** Number of new samples in the vector since last newSync */
0120     inline int numNew() const { return _numNew; }
0121 
0122     /** Number of samples  shifted since last newSync */
0123     inline int numShift() const { return _numShifted; }
0124 
0125     inline bool isRising() const { return _is_rising; }
0126 
0127     /** reset New Samples and Shifted samples */
0128     void newSync();
0129 
0130     virtual bool resize(int sz, bool init = true);
0131 
0132     /** dump the vector values to a raw binary file */
0133     bool saveToTmpFile(QFile &fp);
0134 
0135     virtual void setNewAndShift(int inNew, int inShift);
0136 
0137     /** Clear out the vector by setting everything to 0.0 */
0138     void zero();
0139 
0140     /** Make the vector truly empty.  All values set to NOPOINT (NaN). */
0141     void blank();
0142 
0143     /* Generally you don't need to call this */
0144     void updateScalars();
0145 
0146     /** return information for building a label for this vector */
0147     virtual LabelInfo labelInfo() const;
0148     virtual LabelInfo titleInfo() const;
0149     virtual void setLabelInfo(const LabelInfo &label_info);
0150     virtual void setTitleInfo(const LabelInfo &label_info);
0151 
0152     virtual int getUsage() const;
0153 
0154     /** Save vector information */
0155     virtual void save(QXmlStreamWriter &s);
0156 
0157     /** access functions for _isScalarList */
0158     bool isScalarList() const { return _isScalarList; }
0159 
0160     bool saveable() const;
0161 
0162     bool editable() const;
0163     void setEditable(bool editable);
0164 
0165     bool saveData() const;
0166     virtual void setSaveData(bool save);
0167 
0168     virtual QString descriptionTip() const;
0169 
0170     virtual QString sizeString() const;
0171 
0172     virtual void internalUpdate();
0173 
0174     // output primitives: statistics scalars, etc.
0175     ScalarMap scalars() const {return _scalars;}
0176     StringMap strings() const {return _strings;}
0177 
0178     virtual PrimitiveMap metas() const;
0179 
0180 
0181     virtual ObjectList<Primitive> outputPrimitives() const;
0182 
0183     virtual QString propertyString() const;
0184 
0185     // this is reimplemented but must not be virtual.
0186     QByteArray scriptInterface(QList<QByteArray>&command);
0187 
0188     /** get an binary array with a number of doubles.
0189       */
0190     QByteArray getBinaryArray() const;
0191 
0192     /** does the vector represent time? */
0193     virtual bool isTime() const {return false;}
0194 
0195     virtual ScriptInterface* createScriptInterface();
0196 
0197   protected:
0198     /** current number of samples */
0199     int _size;
0200 
0201     /** number of valid points */
0202     int _nsum;
0203 
0204     /** variables for SpikeInsensitiveAutoscale **/
0205     double _v_ns_stats[MAX_N_DESPIKE_STAT];
0206     int _n_ns_stats;
0207     bool _ns_stats_sorted;
0208 
0209 
0210     /** Where raw input data is held */
0211     double *_v_raw;
0212     bool _v_raw_managed; // if the vector manages the memory for _v_raw;
0213 
0214     /** _v_raw with flagged data replaced with NaNs */
0215     double *_v_flagged;
0216 
0217     /** points to either _v_raw or _v_flagged */
0218     double *_v_out;
0219 
0220     /** _v_out with nans removed **/
0221     double *_v_no_nans;
0222     bool _v_no_nans_dirty : 1;
0223     int _v_no_nans_size;
0224 
0225     /** number of samples shifted since last newSync */
0226     int _numShifted;
0227 
0228     /** number of new samples since last newSync */
0229     int _numNew;
0230 
0231     /** is the vector monotonically rising */
0232     bool _is_rising : 1;
0233 
0234     /** the vector has at least one NaN */
0235     bool _has_nan : 1;
0236 
0237     /** if true then we have a scalar list and do not want to be able to
0238     use it in a curve, display statistics for it, etc. */
0239     bool _isScalarList : 1;
0240 
0241     /** The user should not be permitted to edit the contents of the vector if
0242     this is false. */
0243     bool _editable : 1;
0244 
0245     /** can/should the vector be saved */
0246     bool _saveable : 1;
0247 
0248     /** should the vector data be saved? */
0249     bool _saveData : 1;
0250 
0251     double _min, _max, _mean, _minPos;
0252     int _imax, _imin;
0253 
0254     /** Scalar Maintenance methods */
0255     void CreateScalars(ObjectStore *store);
0256 
0257     virtual void deleteDependents();
0258 
0259     LabelInfo _labelInfo;
0260     LabelInfo _titleInfo;
0261 
0262     friend class DataObject;
0263     friend class Matrix;
0264     //virtual double* realloced(double *memptr, int newSize);
0265     virtual void setV(double *memptr, int newSize);
0266 
0267     ObjectMap<Scalar> _scalars;
0268     ObjectMap<String> _strings;
0269 
0270 private:
0271     void updateVNoNans();
0272 };
0273 
0274 
0275 typedef ObjectList<Vector> VectorList;
0276 typedef ObjectMap<Vector> VectorMap;
0277 
0278 }
0279 
0280 Q_DECLARE_METATYPE(Kst::Vector*)
0281 
0282 #endif
0283 // vim: ts=2 sw=2 et