[Scipy-svn] r2423 - trunk/Lib/sandbox/timeseries

scipy-svn at scipy.org scipy-svn at scipy.org
Fri Dec 15 16:28:49 EST 2006


Author: mattknox_ca
Date: 2006-12-15 15:28:45 -0600 (Fri, 15 Dec 2006)
New Revision: 2423

Modified:
   trunk/Lib/sandbox/timeseries/cseries.c
Log:
major revamp

Modified: trunk/Lib/sandbox/timeseries/cseries.c
===================================================================
--- trunk/Lib/sandbox/timeseries/cseries.c	2006-12-15 18:02:06 UTC (rev 2422)
+++ trunk/Lib/sandbox/timeseries/cseries.c	2006-12-15 21:28:45 UTC (rev 2423)
@@ -11,145 +11,143 @@
 
 
 static //PyArrayObject *
-setArrayItem(PyArrayObject **theArray, long index, PyObject *newVal)
+setArrayItem_1D(PyArrayObject **theArray, long index, PyObject *newVal)
 {
-	char *setptr;
+    if (index >= 0)
+    {
+        //set value in array
+        PyArray_SETITEM(*theArray, PyArray_GetPtr(*theArray, &index), newVal);
+    }
 
-	if (index >= 0)
-	{
-		//set value in array
-		setptr = (*theArray)->data + (index) * (*theArray)->strides[0];
-		PyArray_SETITEM(*theArray,setptr,newVal);
-	}
+}
 
-	//return theArray;
+static //PyArrayObject *
+setArrayItem_2D(PyArrayObject **theArray, long index_x, long index_y, PyObject *newVal)
+{
+    long idx[] = {index_x, index_y};
+
+    if (index_x >= 0 && index_y >= 0) {
+        //set value in array
+        PyArray_SETITEM(*theArray, PyArray_GetPtr(*theArray, idx), newVal);
+    }
+
 }
 
+
 static int
 freqVal(char freq)
 {
-	switch(freq)
-	{
-		case 'A':
-			//annual
-			return 1;
-		case 'Q':
-			//quarterly
-			return 2;
-		case 'M':
-			//monthly
-			return 3;
-		case 'B':
-			//business
-			return 4;
-		case 'D':
-			//daily
-			return 5;
-		default:
-			return 0;
-	}
+    switch(freq)
+    {
+        case 'A':
+            //annual
+            return 1;
+        case 'Q':
+            //quarterly
+            return 2;
+        case 'M':
+            //monthly
+            return 3;
+        case 'B':
+            //business
+            return 4;
+        case 'D':
+            //daily
+            return 5;
+        default:
+            return 0;
+    }
 }
 
 static long
 toDaily(long fromDate, char fromFreq)
 {
-    long absdate, origin, secondorigin;
+    long absdate;
     int y,m,d;
 
-	mxDateTimeObject *theDate;
+    mxDateTimeObject *theDate;
 
-    origin = 675333;
-    secondorigin = 722814;
-
-	//convert fromDate to days since (0 AD - 1 day)
+    //convert fromDate to days since (0 AD - 1 day)
     switch(fromFreq)
     {
         case 'D':
             absdate = fromDate;
             break;
         case 'B':
-            absdate = (fromDate/5)*7 + fromDate%5;
+            absdate = ((fromDate-1)/5)*7 + (fromDate-1)%5 + 1;
             break;
         case 'M':
-			y = fromDate/12 + 1;
-			m = fromDate%12;
+            y = fromDate/12;
+            m = fromDate%12;
 
-			if (m == 0)
-			{
-				m = 12;
-				y--;
-			}
-			d=1;
-			break;
+            if (m == 0)
+            {
+                m = 12;
+                y--;
+            }
+            d=1;
+            break;
         case 'Q':
-        	y = fromDate/4 + 1;
-        	m = (fromDate%4) * 3 - 2;
+            y = fromDate/4;
+            m = (fromDate%4) * 3 - 2;
 
-			if (m < 1)
-			{
-				m += 12;
-				y--;
-			}
-			else if (m == 12)
-			{
-				m = 1;
-				y++;
-			}
-			d=1;
-			break;
+            if (m < 1)
+            {
+                m += 12;
+                y--;
+            }
+            else if (m == 12)
+            {
+                m = 1;
+                y++;
+            }
+            d=1;
+            break;
         case 'A':
-        	y = fromDate-1;
-        	m = 1;
-        	d = 1;
-        	break;
+            y = fromDate;
+            m = 1;
+            d = 1;
+            break;
         default:
             return -1;
     }
 
-	if (freqVal(fromFreq) < 4)
-	{
-		//switch to years from 0 for mxDateTime
-		y+= 1849;
+    if (freqVal(fromFreq) < 4)
+    {
+        theDate = (mxDateTimeObject *)mxDateTime.DateTime_FromDateAndTime(y,m,d,0,0,0);
+        absdate = (long)(theDate->absdate);
+    }
 
-		theDate = (mxDateTimeObject *)mxDateTime.DateTime_FromDateAndTime(y,m,d,0,0,0);
-		absdate = (long)(theDate->absdate);
-	}
-	else
-	{
-		//days from 0 for mxDateTime
-		absdate += origin;
-	}
+    return absdate;
 
-	return absdate;
-
 }
 
 
 static long
 getDateInfo_sub(long dateNum, char freq, char info) {
 
-	long monthNum;
-	mxDateTimeObject *convDate;
-	convDate = (mxDateTimeObject *)mxDateTime.DateTime_FromAbsDateAndTime(toDaily(dateNum,freq),0);
+    long monthNum;
+    mxDateTimeObject *convDate;
+    convDate = (mxDateTimeObject *)mxDateTime.DateTime_FromAbsDateAndTime(toDaily(dateNum,freq),0);
 
     switch(info)
     {
         case 'Y': //year
 
-			return (long)(convDate->year);
+            return (long)(convDate->year);
 
         case 'Q': //quarter
-			monthNum = (long)(convDate->month);
-			return ((monthNum-1)/3)+1;
+            monthNum = (long)(convDate->month);
+            return ((monthNum-1)/3)+1;
 
         case 'M': //month
-			return (long)(convDate->month);
+            return (long)(convDate->month);
 
         case 'D': //day
-			return (long)(convDate->day);
+            return (long)(convDate->day);
 
         case 'W': //day of week
-			return (long)(convDate->day_of_week);
+            return (long)(convDate->day_of_week);
         default:
             return -1;
     }
@@ -160,8 +158,8 @@
 static PyObject *
 cseries_getDateInfo(PyObject *self, PyObject *args)
 {
-	char *freq;
-	char *info;
+    char *freq;
+    char *info;
 
     PyArrayObject *array;
     PyArrayObject *tempArray;
@@ -171,91 +169,88 @@
     PyObject *val;
     long i, lngVal, dInfo, dim;
 
-	if (!PyArg_ParseTuple(args, "Oss:getDateInfo(array, freq, info)", &tempArray, &freq, &info)) return NULL;
+    if (!PyArg_ParseTuple(args, "Oss:getDateInfo(array, freq, info)", &tempArray, &freq, &info)) return NULL;
 
     array = PyArray_GETCONTIGUOUS(tempArray);
 
-	dim = array->dimensions[0];
+    dim = array->dimensions[0];
 
-	//initialize new array
+    //initialize new array
     newArray = (PyArrayObject*)PyArray_SimpleNew(array->nd, &dim, array->descr->type_num);
 
     for (i = 0; i < array->dimensions[0]; i++)
     {
-		getptr = array->data + i*array->strides[0];
-		val = PyArray_GETITEM(array, getptr);
-		lngVal = PyInt_AsLong(val);
-		dInfo = getDateInfo_sub(lngVal, *freq, *info);
+        getptr = array->data + i*array->strides[0];
+        val = PyArray_GETITEM(array, getptr);
+        lngVal = PyInt_AsLong(val);
+        dInfo = getDateInfo_sub(lngVal, *freq, *info);
 
-		setArrayItem(&newArray, i, PyInt_FromLong(dInfo));
-	}
+        setArrayItem_1D(&newArray, i, PyInt_FromLong(dInfo));
+    }
 
-	return (PyObject *) newArray;
+    return (PyObject *) newArray;
 
 }
 
 
-//fromDate is periods since Dec 31, 1849
 static long
 convert(long fromDate, char fromFreq, char toFreq, int notStartInd)
 {
-    long absdate, origin, secondorigin, secsInDay;
+    long absdate, secsInDay;
     long converted;
-    int rem;
-    int y,m,d;
+    int y,m;
 
-	mxDateTimeObject *convDate;
+    mxDateTimeObject *convDate;
 
-    origin = 675333;
-    secondorigin = 722814;
     secsInDay = 86400;
 
-	absdate = toDaily(fromDate, fromFreq);
+    absdate = toDaily(fromDate, fromFreq);
 
-	convDate = (mxDateTimeObject *)mxDateTime.DateTime_FromAbsDateAndTime(absdate,0);
+    convDate = (mxDateTimeObject *)mxDateTime.DateTime_FromAbsDateAndTime(absdate,0);
 
-	//switch back to days and years since 1849 for pyTSA Date
-	absdate -= origin;
-	y = convDate->year - 1849;
-	m = convDate->month;
+    y = convDate->year;
+    m = convDate->month;
 
-	//convert convDate to appropriate # of periods according to toFreq
+    //convert convDate to appropriate # of periods according to toFreq
     switch(toFreq)
     {
         case 'D':
             converted = absdate;
             break;
         case 'B':
-        	rem = absdate%7;
-            if (rem > 4) //is weekend day
+            if (convDate->day_of_week > 4) //is weekend day
             {
-				if (notStartInd == 1 && freqVal(fromFreq) > 4)
-				{
-					return -1;
-				}
-				else
-				{
-					d = convDate->day;
-					d -= rem - 4;	//change to friday before weekend
-					if (d < 1) d += 3; //if friday was prev. month, change to monday instead
-					absdate = absdate - convDate->day + d;
-					converted = (long)((absdate / 7 * 5.0) + absdate%7);
-				}
-			}
-			else
-			{
-				converted = (long)((absdate / 7 * 5.0) + rem);
-			}
+                if (notStartInd == 1 && freqVal(fromFreq) > 4)
+                {
+                    return -1;
+                }
+                else
+                {
+                    if (convDate->day - (convDate->day_of_week - 4) < 1) {
+                        //change to Monday after weekend
+                        absdate += (7 - convDate->day_of_week);
+                    } else {
+                        //change to friday before weekend
+                        absdate -= (convDate->day_of_week - 4);
+                    }
+
+                    converted = (long)((absdate / 7 * 5.0) + absdate%7);
+                }
+            }
+            else
+            {
+                converted = (long)((absdate / 7 * 5.0) + absdate%7);
+            }
             break;
         case 'M':
-        	converted = (long)((y-1)*12 + m);
-        	break;
-       	case 'Q':
-       		converted = (long)((y-1)*4 + ((m-1)/3) + 1);
-       		break;
-       	case 'A':
-       		converted = (long)(y+1);
-       		break;
+            converted = (long)(y*12 + m);
+            break;
+        case 'Q':
+            converted = (long)(y*4 + ((m-1)/3) + 1);
+            break;
+        case 'A':
+            converted = (long)(y);
+            break;
         default:
             return -1;
     }
@@ -263,143 +258,171 @@
     return converted;
 }
 
-
-static long
-expand(long oldSize, char fromFr, char toFr)
-{
-	long newSize;
-	int fromFreq, toFreq;
-
-	if (fromFr == toFr) return oldSize;
-
-	fromFreq = freqVal(fromFr);
-	toFreq = freqVal(toFr);
-	if (fromFreq*toFreq == 0) return oldSize; //invalid frequency
-
-	newSize = oldSize;
-
-	while (toFreq > fromFreq)
-	{
-		if (fromFreq == 1)	//Annual
-		{
-			newSize *= 4; //quarters in year
-			fromFreq++;
-		}
-		else if (fromFreq == 2) //Quarterly
-		{
-			newSize *= 3; //months in quarter
-			fromFreq++;
-		}
-		else if (fromFreq == 3)	//Monthly
-		{
-			newSize *= 31; //max days in month
-			fromFreq++;
-		}
-		else if (fromFreq == 4)	//Business
-		{
-			newSize *= 2; //max d days for each b days
-			fromFreq++;
-		}
-	}
-
-
-	return newSize;
+static int validFreq(char freq) {
+    switch(freq)
+    {
+        case 'A':
+            return 1;
+        case 'Q':
+            return 1;
+        case 'M':
+            return 1;
+        case 'B':
+            return 1;
+        case 'D':
+            return 1;
+        default:
+            return 0;
+    }
 }
 
 
-///////////////////////////////////////////////////////////////////////
-/*
-OBSERVED
+static int
+expand(long oldSize, char fromFreq, char toFreq, long *newLen, long *newHeight)
+{
 
-from lower freq to higher freq
-----------------------
+    int maxBusDaysPerYear, maxBusDaysPerQuarter, maxBusDaysPerMonth;
+    int minBusDaysPerYear, minBusDaysPerQuarter, minBusDaysPerMonth;
 
-summed --	all values in period set as lower freq's value / # of values
+    int maxDaysPerYear, maxDaysPerQuarter, maxDaysPerMonth;
+    int minDaysPerYear, minDaysPerQuarter, minDaysPerMonth;
 
-rest --		all values in period set as lower freq's value
+    minBusDaysPerYear = 260;    maxBusDaysPerYear = 262;
+    minBusDaysPerQuarter = 64;  maxBusDaysPerQuarter = 66;
+    minBusDaysPerMonth = 20;    maxBusDaysPerMonth = 23;
 
-from higher freq to lower freq
-----------------------
-begin - 	lower freq's value set as first value in period
-end - 		lower freq's value set as end value in period
-summed -	lower freq's value set as sum of all values in period
-averaged -	lower freq's value set as average of all values in period
-high - 		lower freq's value set as largest value in period
-low - 		lower freq's value set as smallest value in period
+    minDaysPerYear = 365;       maxDaysPerYear = 366;
+    minDaysPerQuarter = 90;     maxDaysPerQuarter = 92;
+    minDaysPerMonth = 28;       maxDaysPerMonth = 31;
 
-*/
-///////////////////////////////////////////////////////////////////////
+    if (!validFreq(fromFreq)) return 0;
+    if (!validFreq(toFreq)) return 0;
 
-static void
-adjValForObsSet(PyArrayObject *theArray, char obs, PyObject **newVal, PyObject **newValMask, PyObject *val, PyObject *valMask,  long curPerLen)
-{
-	double dblVal;
-	long lngValMask, lngAllMasked;
+    if (fromFreq == toFreq) {
+        *newLen = oldSize;
+        *newHeight = 1;
+    } else {
 
-	lngValMask = PyInt_AsLong(valMask);
-	lngAllMasked = PyInt_AsLong(*newValMask);
+        switch(fromFreq)
+        {
+            case 'A': //annual
 
-	if (!lngValMask) {
+                switch(toFreq)
+                {
+                    case 'Q':
+                        *newLen = oldSize * 4;
+                        *newHeight = 1;
+                        break;
+                    case 'M':
+                        *newLen = oldSize * 12;
+                        *newHeight = 1;
+                        break;
+                    case 'B':
+                        *newLen = oldSize * maxBusDaysPerYear;
+                        *newHeight = 1;
+                        break;
+                    case 'D':
+                        *newLen = oldSize * maxDaysPerYear;
+                        *newHeight = 1;
+                        break;
+                }
+                break;
 
-		// if any value is not masked, then we shall not mask the aggregated result
-		*newValMask = valMask;
+            case 'Q': //quarterly
 
-		if (obs == 'B')
-		{
-			if (lngAllMasked) {
-				*newVal = val;
-			}
-		}
-		else if ( PyArray_ISFLOAT(theArray) && (obs=='S' || obs=='A') )
-		{
+                switch(toFreq)
+                {
+                    case 'A':
+                        *newLen = (oldSize / 4) + 2;
+                        *newHeight = 4;
+                        break;
+                    case 'M':
+                        *newLen = oldSize * 3;
+                        *newHeight = 1;
+                        break;
+                    case 'B':
+                        *newLen = oldSize * maxBusDaysPerQuarter;
+                        *newHeight = 1;
+                        break;
+                    case 'D':
+                        *newLen = oldSize * maxDaysPerQuarter;
+                        *newHeight = 1;
+                        break;
+                }
+                break;
 
-			if (obs == 'S')
-			{
-				//observed is summed
+            case 'M': //monthly
 
-				dblVal = PyFloat_AsDouble(*newVal);
-				dblVal += PyFloat_AsDouble(val);
-				*newVal = PyFloat_FromDouble(dblVal);
-			}
-			else
-			{
-				//observed is averaged
+                switch(toFreq)
+                {
+                    case 'A':
+                        *newLen = (oldSize / 12) + 2;
+                        *newHeight = 12;
+                        break;
+                    case 'Q':
+                        *newLen = (oldSize / 3) + 2;
+                        *newHeight = 3;
+                        break;
+                    case 'B':
+                        *newLen = oldSize * maxBusDaysPerMonth;
+                        *newHeight = 1;
+                        break;
+                    case 'D':
+                        *newLen = oldSize * maxDaysPerMonth;
+                        *newHeight = 1;
+                        break;
+                }
+                break;
 
-				dblVal = PyFloat_AsDouble(*newVal);
-				dblVal *= (curPerLen-1);
-				dblVal += PyFloat_AsDouble(val);
-				dblVal /= curPerLen;
-				*newVal = PyFloat_FromDouble(dblVal);
-			}
+            case 'B': //business
 
-		}
-		else if ( PyArray_ISNUMBER(theArray) && (obs=='H' || obs=='L') )
-		{
+                switch(toFreq)
+                {
+                    case 'A':
+                        *newLen = (oldSize / minBusDaysPerYear) + 2;
+                        *newHeight = maxBusDaysPerYear;
+                        break;
+                    case 'Q':
+                        *newLen = (oldSize / minBusDaysPerQuarter) + 2;
+                        *newHeight = maxBusDaysPerQuarter;
+                        break;
+                    case 'M':
+                        *newLen = (oldSize / minBusDaysPerMonth) + 2;
+                        *newHeight = maxBusDaysPerMonth;
+                        break;
+                    case 'D':
+                        *newLen = ((7 * oldSize)/5) + 2;
+                        *newHeight = 1;
+                        break;
+                }
+                break;
 
-			if (obs == 'H')
-			{
-				//observed is high
+            case 'D': //daily
 
-				if (PyFloat_AsDouble(val) > PyFloat_AsDouble(*newVal)) *newVal = val;
-			}
-			else if (obs == 'L')
-			{
-				//observed is low
+                switch(toFreq)
+                {
+                    case 'A':
+                        *newLen = (oldSize / minDaysPerYear) + 2;
+                        *newHeight = maxDaysPerYear;
+                        break;
+                    case 'Q':
+                        *newLen = (oldSize / minDaysPerQuarter) + 2;
+                        *newHeight = maxDaysPerQuarter;
+                        break;
+                    case 'M':
+                        *newLen = (oldSize / minDaysPerMonth) + 2;
+                        *newHeight = maxDaysPerMonth;
+                        break;
+                    case 'B':
+                        *newLen = ((5 * oldSize)/7) + 2;
+                        *newHeight = 1;
+                        break;                }
+                break;
+        }
+    }
 
-				if (PyFloat_AsDouble(val) < PyFloat_AsDouble(*newVal)) *newVal = val;
-			}
+    return 1;
 
-		}
-		else
-		{
-			//observed is not beginning and
-			//val is string or (val is date and observed is summed/averaged)
-			//or observed is end or not supported
-
-			*newVal = val;
-		}
-	}
-
 }
 
 
@@ -418,24 +441,21 @@
     PyObject *returnVal = NULL;
 
     int notStartInd;
-    long startIndex, newStart;
-    long i, curPerInd, nextPerInd, prevIndex, curIndex;
-    long dim;
-    long curPerLen;
-    long lngValMask;
-    char *fromFreq, *toFreq, *observed;
+    long startIndex, newStart, newStartYaxis;
+    long newLen, newHeight;
+    long i, currIndex, prevIndex;
+    long nd;
+    long *dim;
+    long currPerLen;
+    char *fromFreq, *toFreq, *position;
 
-    char *getptr;
-    PyObject *val, *newVal;
+    PyObject *val, *valMask;
 
-    char *getptrMask;
-    PyObject *valMask, *newValMask;
-
     int toFrVal, fromFrVal;
 
-	returnVal = PyDict_New();
+    returnVal = PyDict_New();
 
-	if (!PyArg_ParseTuple(args, "OssslO:reindex(array, fromfreq, tofreq, observed, startIndex,mask)", &tempArray, &fromFreq, &toFreq, &observed, &startIndex, &tempMask)) return NULL;
+    if (!PyArg_ParseTuple(args, "OssslO:reindex(array, fromfreq, tofreq, position, startIndex, mask)", &tempArray, &fromFreq, &toFreq, &position, &startIndex, &tempMask)) return NULL;
 
     if (toFreq[0] == fromFreq[0])
     {
@@ -446,135 +466,95 @@
         return returnVal;
     }
 
+    //get frequency numeric mapping
+    fromFrVal = freqVal(fromFreq[0]);
+    toFrVal = freqVal(toFreq[0]);
+
     array = PyArray_GETCONTIGUOUS(tempArray);
     mask = PyArray_GETCONTIGUOUS(tempMask);
 
-	//expand size to fit new values if needed
-	dim = expand(array->dimensions[0], fromFreq[0], toFreq[0]);
+    //expand size to fit new values if needed
+    if (!expand(array->dimensions[0], fromFreq[0], toFreq[0], &newLen, &newHeight)) return NULL;
 
-	//initialize new array
-    newArray = (PyArrayObject*)PyArray_SimpleNew(array->nd, &dim, array->descr->type_num);
-    newMask  = (PyArrayObject*)PyArray_SimpleNew(mask->nd, &dim, mask->descr->type_num);
-
-    for (i = 0; i < dim; i++)
-    {
-		setArrayItem(&newArray, i, PyInt_FromLong(1));
-		setArrayItem(&newMask, i, PyInt_FromLong(1));
-	}
-
-	//convert start index to new frequency
-	notStartInd = 0;
+    //convert start index to new frequency
+    notStartInd = 0;
     newStart = convert(startIndex, fromFreq[0], toFreq[0], notStartInd);
 
-	//initialize prevIndex
-	prevIndex = newStart - 1;
+    if (newHeight > 1) {
 
-	notStartInd = 1;
+        newStartYaxis = startIndex - convert(newStart, toFreq[0], fromFreq[0], notStartInd);
+        currPerLen = newStartYaxis;
 
-	//set values in the new array
-    for (i = 0; i < array->dimensions[0]; i++)
-    {
-		//find index for start of current period in new frequency
-        curPerInd = convert(startIndex + i, fromFreq[0], toFreq[0], notStartInd);
+        nd = 2;
+        dim = malloc(nd * sizeof(int));
+        dim[0] = newLen;
+        dim[1] = newHeight;
+    } else {
+        currPerLen = 0;
+        nd = 1;
+        dim = malloc(nd * sizeof(int));
+        dim[0] = newLen;
+    }
 
-		//get frequency numeric mapping
-		fromFrVal = freqVal(fromFreq[0]);
-		toFrVal = freqVal(toFreq[0]);
+    newArray = (PyArrayObject*)PyArray_SimpleNew(nd, dim, array->descr->type_num);
+    newMask  = (PyArrayObject*)PyArray_SimpleNew(nd, dim, mask->descr->type_num);
 
-		//get value from old array
-		getptr = array->data + i*array->strides[0];
-		val = PyArray_GETITEM(array,getptr);
+    free(dim);
 
-		//get the mask corresponding to the old value
-		getptrMask = mask->data + i*mask->strides[0];
-		valMask = PyArray_GETITEM(mask,getptrMask);
+    PyArray_FILLWBYTE(newArray,0);
+    PyArray_FILLWBYTE(newMask,1);
 
-        if (fromFrVal < toFrVal)
-        {
-			//from lower freq to higher freq
+    //initialize prevIndex
+    prevIndex = newStart;
 
-			newVal = val;
-			newValMask = valMask;
+    notStartInd = 1;
 
-			//find index for start of next period in new frequency
-			nextPerInd = convert(startIndex + i + 1, fromFreq[0], toFreq[0], notStartInd);
+    //set values in the new array
+    for (i = 0; i < array->dimensions[0]; i++)
+    {
 
-			//adjust for observed setting
-			if (observed[0] == 'S' && PyArray_ISFLOAT(array) && !( (fromFrVal == 4 && toFrVal == 5) || (fromFrVal == 5 && toFrVal == 4) ) )
-			{
-				//summed
+        //get value from old array
+        val = PyArray_GETITEM(array, PyArray_GetPtr(array, &i));
 
-				//all values in period set as old array's value / # of values
-				newVal = PyFloat_FromDouble( PyFloat_AsDouble(val) / (nextPerInd - curPerInd) );
-			}
+        //get the mask corresponding to the old value
+        valMask = PyArray_GETITEM(mask, PyArray_GetPtr(mask, &i));
 
-			//set each value in period
-			for (curIndex = curPerInd; curIndex < nextPerInd; curIndex++)
-			{
-				setArrayItem(&newArray, curIndex-newStart, newVal);
-				setArrayItem(&newMask, curIndex-newStart, newValMask);
-			}
-		}
-		else
-		{
+        //find index for start of current period in new frequency
+        if (newHeight == 1 && (position[0] == 'E' && !((fromFrVal == 4 && toFrVal == 5) || (fromFrVal == 5 && toFrVal == 4))) ) {
+            currIndex = convert(startIndex + i + 1, fromFreq[0], toFreq[0], notStartInd)-1;
+        } else {
+            currIndex = convert(startIndex + i, fromFreq[0], toFreq[0], notStartInd);
+        }
 
-			lngValMask = PyInt_AsLong(valMask);
+        if (newHeight > 1) {
 
-			//from higher freq to lower freq
+                if (currIndex != prevIndex)
+                {
+                    //reset period length
+                    currPerLen = 0;
+                    prevIndex = currIndex;
+                }
 
-			if (curPerInd != prevIndex)
-			{
-				//starting new period in old array
+                //set value in the new array
+                setArrayItem_2D(&newArray, currIndex-newStart, currPerLen, val);
+                setArrayItem_2D(&newMask, currIndex-newStart, currPerLen, valMask);
 
+                currPerLen++;
 
-				//set value in the new array
-				setArrayItem(&newArray, prevIndex-newStart, newVal);
-				setArrayItem(&newMask, prevIndex-newStart, newValMask);
+        } else {
 
-				//reset period length
-				curPerLen = 0;
+            setArrayItem_1D(&newArray, currIndex-newStart, val);
+            setArrayItem_1D(&newMask, currIndex-newStart, valMask);
 
+        }
 
-
-				if (!lngValMask) {
-					curPerLen++;
-				}
-
-
-
-				//store current index and value
-				prevIndex = curPerInd;
-				newVal = val;
-				newValMask = valMask;
-
-			}
-			else
-			{
-				//still in same period
-
-
-
-				if (!lngValMask) {
-					curPerLen++;
-				}
-
-				//adjust new value according to observed setting
-				adjValForObsSet(array, observed[0], &newVal, &newValMask, val, valMask, curPerLen);
-			}
-
-		}
-
     }
 
-	//set value of last item in the new array
-	setArrayItem(&newArray, curPerInd-newStart, newVal);
-	setArrayItem(&newMask, curPerInd-newStart, newValMask);
+    PyDict_SetItemString(returnVal, "values", (PyObject*)newArray);
+    PyDict_SetItemString(returnVal, "mask", (PyObject*)newMask);
 
-	PyDict_SetItemString(returnVal, "values", (PyObject*)newArray);
-	PyDict_SetItemString(returnVal, "mask", (PyObject*)newMask);
+    return returnVal;
 
-	return returnVal;
-
 }
 
 
@@ -589,8 +569,8 @@
 
     if (!PyArg_ParseTuple(args, "lss:convert(fromDate, fromfreq, tofreq)", &fromDate, &fromFreq, &toFreq)) return NULL;
 
-	//always want start of period (only matters when converting from lower freq to higher freq ie. m -> d)
-	notStartInd = 0;
+    //always want start of period (only matters when converting from lower freq to higher freq ie. m -> d)
+    notStartInd = 0;
 
     return PyInt_FromLong(convert(fromDate, fromFreq[0], toFreq[0], notStartInd));
 }
@@ -601,7 +581,7 @@
 static PyMethodDef cseries_methods[] = {
     {"reindex", cseries_reindex, METH_VARARGS, cseries_reindex_doc},
     {"convert", cseries_convert, METH_VARARGS, cseries_convert_doc},
-	{"getDateInfo", cseries_getDateInfo, METH_VARARGS, cseries_getDateInfo_doc},
+    {"getDateInfo", cseries_getDateInfo, METH_VARARGS, cseries_getDateInfo_doc},
     {NULL, NULL}
 };
 




More information about the Scipy-svn mailing list