[Scipy-svn] r2537 - trunk/Lib/sandbox/timeseries/src

scipy-svn at scipy.org scipy-svn at scipy.org
Thu Jan 11 12:26:05 CST 2007


Author: mattknox_ca
Date: 2007-01-11 12:26:01 -0600 (Thu, 11 Jan 2007)
New Revision: 2537

Modified:
   trunk/Lib/sandbox/timeseries/src/cseries.c
Log:
added support for additional frequencies. Fixed some bugs

Modified: trunk/Lib/sandbox/timeseries/src/cseries.c
===================================================================
--- trunk/Lib/sandbox/timeseries/src/cseries.c	2007-01-11 15:53:21 UTC (rev 2536)
+++ trunk/Lib/sandbox/timeseries/src/cseries.c	2007-01-11 18:26:01 UTC (rev 2537)
@@ -7,6 +7,13 @@
 
 static char cseries_doc[] = "Speed sensitive time series operations";
 
+/*
+these are the earliest values at each frequency that can be converted
+to frequencies higher than daily (ie. Hourly, Minutely, Secondly)
+*/
+
+static long minval_D_toHighFreq = 719163;
+
 ///////////////////////////////////////////////////////////////////////
 
 // helpers for frequency conversion routines //
@@ -83,137 +90,6 @@
 // frequency specifc conversion routines
 // each function must take an integer fromDate and a char relation ('B' or 'A' for 'BEFORE' or 'AFTER')
 
-//************ FROM ANNUAL ***************
-
-static long asfreq_AtoQ(long fromDate, char relation) {
-    if (relation == 'B') { return fromDate * 4 + 1; }
-    else {                 return (fromDate + 1) * 4; }
-}
-
-static long asfreq_AtoM(long fromDate, char relation) {
-    if (relation == 'B') { return fromDate * 12 + 1; }
-    else {                 return (fromDate + 1) * 12; }
-}
-
-static long asfreq_AtoW(long fromDate, char relation) { return 0; }
-
-static long asfreq_AtoB(long fromDate, char relation) {
-    if (relation == 'B') { return busday_before(fromDate,1,1); }
-    else {                 return busday_after(fromDate+1,1,1); }
-}
-
-static long asfreq_AtoD(long fromDate, char relation) {
-    if (relation == 'B') { return absdate_from_ymd(fromDate,1,1); }
-    else {                 return absdate_from_ymd(fromDate+1,1,1) - 1; }
-}
-
-static long asfreq_AtoH(long fromDate, char relation) { return 0; }
-static long asfreq_AtoT(long fromDate, char relation) { return 0; }
-static long asfreq_AtoS(long fromDate, char relation) { return 0; }
-
-
-//************ FROM QUARTERLY ***************
-
-static long asfreq_QtoA(long fromDate, char relation) { return (fromDate - 1) / 4; }
-
-static long asfreq_QtoM(long fromDate, char relation) {
-    if (relation == 'B') { return fromDate * 3 - 2; }
-    else {                 return fromDate  * 3; }
-}
-
-static long asfreq_QtoW(long fromDate, char relation) { return 0; }
-
-static void QtoD_ym(long fromDate, long *y, long *m) {
-    *y = (fromDate - 1) / 4;
-    *m = fromDate * 3 - 12 * (*y) - 2;
-}
-
-static long asfreq_QtoB(long fromDate, char relation) {
-
-    long y, m;
-
-    if (relation == 'B') {
-        QtoD_ym(fromDate, &y, &m);
-        return busday_before(y,m,1);
-    } else {
-        QtoD_ym(fromDate+1, &y, &m);
-        return busday_after(y, m, 1);
-    }
-}
-
-static long asfreq_QtoD(long fromDate, char relation) {
-
-    long y, m;
-
-    if (relation == 'B') {
-        QtoD_ym(fromDate, &y, &m);
-        return absdate_from_ymd(y, m, 1);
-    } else {
-        QtoD_ym(fromDate+1, &y, &m);
-        return absdate_from_ymd(y, m, 1) - 1;
-    }
-}
-
-static long asfreq_QtoH(long fromDate, char relation) { return 0; }
-static long asfreq_QtoT(long fromDate, char relation) { return 0; }
-static long asfreq_QtoS(long fromDate, char relation) { return 0; }
-
-
-//************ FROM MONTHLY ***************
-
-static long asfreq_MtoA(long fromDate, char relation) { return (fromDate - 1) / 12; }
-static long asfreq_MtoQ(long fromDate, char relation) { return (fromDate - 1) / 3 + 1; }
-
-static long asfreq_MtoW(long fromDate, char relation) { return 0; }
-
-static void MtoD_ym(long fromDate, long *y, long *m) {
-    *y = (fromDate - 1) / 12;
-    *m = fromDate  - 12 * (*y);
-}
-
-static long asfreq_MtoB(long fromDate, char relation) {
-
-    long y, m;
-
-    if (relation == 'B') {
-        MtoD_ym(fromDate, &y, &m);
-        return busday_before(y,m,1);
-    } else {
-        MtoD_ym(fromDate+1, &y, &m);
-        return busday_after(y, m, 1);
-    }
-}
-
-static long asfreq_MtoD(long fromDate, char relation) {
-
-    long y, m;
-
-    if (relation == 'B') {
-        MtoD_ym(fromDate, &y, &m);
-        return absdate_from_ymd(y, m, 1);
-    } else {
-        MtoD_ym(fromDate+1, &y, &m);
-        return absdate_from_ymd(y, m, 1) - 1;
-    }
-}
-
-static long asfreq_MtoH(long fromDate, char relation) { return 0; }
-static long asfreq_MtoT(long fromDate, char relation) { return 0; }
-static long asfreq_MtoS(long fromDate, char relation) { return 0; }
-
-
-//************ FROM WEEKLY ***************
-
-static long asfreq_WtoA(long fromDate, char relation) { return 0; }
-static long asfreq_WtoQ(long fromDate, char relation) { return 0; }
-static long asfreq_WtoM(long fromDate, char relation) { return 0; }
-static long asfreq_WtoB(long fromDate, char relation) { return 0; }
-static long asfreq_WtoD(long fromDate, char relation) { return 0; }
-static long asfreq_WtoH(long fromDate, char relation) { return 0; }
-static long asfreq_WtoT(long fromDate, char relation) { return 0; }
-static long asfreq_WtoS(long fromDate, char relation) { return 0; }
-
-
 //************ FROM DAILY ***************
 
 static long asfreq_DtoA(long fromDate, char relation) {
@@ -248,7 +124,7 @@
 }
 
 
-static long asfreq_DtoW(long fromDate, char relation) { return 0; }
+static long asfreq_DtoW(long fromDate, char relation) { return -1; }
 
 static long asfreq_DtoB(long fromDate, char relation) {
     mxDateTimeObject *mxDate;
@@ -281,11 +157,67 @@
     return result;
 }
 
+static long asfreq_DtoHIGHFREQ(long fromDate, char relation, long periodsPerDay) {
+    if (fromDate >= minval_D_toHighFreq) {
+        if (relation == 'B') { return (fromDate - minval_D_toHighFreq)*(periodsPerDay) + 1; }
+        else                 { return (fromDate - minval_D_toHighFreq + 1)*(periodsPerDay); }
+    } else { return -1; }
+}
 
-static long asfreq_DtoH(long fromDate, char relation) { return 0; }
-static long asfreq_DtoT(long fromDate, char relation) { return 0; }
-static long asfreq_DtoS(long fromDate, char relation) { return 0; }
+static long asfreq_DtoH(long fromDate, char relation) { return asfreq_DtoHIGHFREQ(fromDate, relation, 24); }
+static long asfreq_DtoT(long fromDate, char relation) { return asfreq_DtoHIGHFREQ(fromDate, relation, 24*60); }
+static long asfreq_DtoS(long fromDate, char relation) { return asfreq_DtoHIGHFREQ(fromDate, relation, 24*60*60); }
 
+//************ FROM SECONDLY ***************
+
+static long asfreq_StoD(long fromDate, char relation) { return (fromDate - 1)/(60*60*24) + minval_D_toHighFreq; }
+
+static long asfreq_StoA(long fromDate, char relation) { return asfreq_DtoA(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoQ(long fromDate, char relation) { return asfreq_DtoQ(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoM(long fromDate, char relation) { return asfreq_DtoM(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoW(long fromDate, char relation) { return asfreq_DtoW(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoB(long fromDate, char relation) { return asfreq_DtoB(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoB_forConvert(long fromDate, char relation)
+    { return asfreq_DtoB_forConvert(asfreq_StoD(fromDate, relation), relation); }
+static long asfreq_StoT(long fromDate, char relation) { return (fromDate - 1)/60 + 1; }
+static long asfreq_StoH(long fromDate, char relation) { return (fromDate - 1)/(60*60) + 1; }
+
+//************ FROM MINUTELY ***************
+
+static long asfreq_TtoD(long fromDate, char relation) { return (fromDate - 1)/(60*24) + minval_D_toHighFreq; }
+
+static long asfreq_TtoA(long fromDate, char relation) { return asfreq_DtoA(asfreq_TtoD(fromDate, relation), relation); }
+static long asfreq_TtoQ(long fromDate, char relation) { return asfreq_DtoQ(asfreq_TtoD(fromDate, relation), relation); }
+static long asfreq_TtoM(long fromDate, char relation) { return asfreq_DtoM(asfreq_TtoD(fromDate, relation), relation); }
+static long asfreq_TtoW(long fromDate, char relation) { return asfreq_DtoW(asfreq_TtoD(fromDate, relation), relation); }
+static long asfreq_TtoB(long fromDate, char relation) { return asfreq_DtoB(asfreq_TtoD(fromDate, relation), relation); }
+
+static long asfreq_TtoB_forConvert(long fromDate, char relation)
+    { return asfreq_DtoB_forConvert(asfreq_TtoD(fromDate, relation), relation); }
+
+static long asfreq_TtoH(long fromDate, char relation) { return (fromDate - 1)/60 + 1; }
+static long asfreq_TtoS(long fromDate, char relation) {
+    if (relation == 'B') {  return fromDate*60 - 59; }
+    else                 {  return fromDate*60;      }}
+
+//************ FROM HOURLY ***************
+
+static long asfreq_HtoD(long fromDate, char relation) { return (fromDate - 1)/24 + minval_D_toHighFreq; }
+static long asfreq_HtoA(long fromDate, char relation) { return asfreq_DtoA(asfreq_HtoD(fromDate, relation), relation); }
+static long asfreq_HtoQ(long fromDate, char relation) { return asfreq_DtoQ(asfreq_HtoD(fromDate, relation), relation); }
+static long asfreq_HtoM(long fromDate, char relation) { return asfreq_DtoM(asfreq_HtoD(fromDate, relation), relation); }
+static long asfreq_HtoW(long fromDate, char relation) { return asfreq_DtoW(asfreq_HtoD(fromDate, relation), relation); }
+static long asfreq_HtoB(long fromDate, char relation) { return asfreq_DtoB(asfreq_HtoD(fromDate, relation), relation); }
+
+static long asfreq_HtoB_forConvert(long fromDate, char relation)
+    { return asfreq_DtoB_forConvert(asfreq_HtoD(fromDate, relation), relation); }
+
+// calculation works out the same as TtoS, so we just call that function for HtoT
+static long asfreq_HtoT(long fromDate, char relation) { return asfreq_TtoS(fromDate, relation); }
+static long asfreq_HtoS(long fromDate, char relation) {
+    if (relation == 'B') {  return fromDate*60*60 - 60*60 + 1; }
+    else                 {  return fromDate*60*60;             }}
+
 //************ FROM BUSINESS ***************
 
 static long asfreq_BtoD(long fromDate, char relation) {
@@ -312,42 +244,135 @@
 static long asfreq_BtoS(long fromDate, char relation) {
     return asfreq_DtoS(asfreq_BtoD(fromDate, relation), relation); }
 
-//************ FROM HOURLY ***************
+//************ FROM WEEKLY **