clip.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Licensed Materials - Property of HCL
  3. *
  4. * IBM Informix DataBlade Module
  5. * (C) Copyright International Business Machines Corporation 2002.
  6. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  7. *
  8. * COPYRIGHT LICENSE:
  9. * This information contains sample application programs in source language,
  10. * which illustrate programming techniques on various operating platforms.
  11. * You may copy, modify, and distribute these sample programs in any form
  12. * without payment to IBM, for the purposes of developing, using, marketing
  13. * or distributing application programs conforming to the application
  14. * programming interface for the operating platform for which the sample
  15. * programs are written. These examples have not been thoroughly tested under
  16. * all conditions. IBM, therefore, cannot guarantee or imply reliability,
  17. * serviceability, or function of these programs. You may copy, modify, and
  18. * distribute these sample programs in any form without payment to IBM for
  19. * the purposes of developing, using, marketing, or distributing application
  20. * programs conforming to IBM's application programming interfaces.
  21. * Each copy or any portion of these sample programs or any derivative work,
  22. * must include a copyright notice as follows:
  23. * © (your company name) (year). Portions of this code are derived from
  24. * IBM Corp. Sample Programs. © Copyright IBM Corp. (enter the year or
  25. * years). All rights reserved.
  26. *
  27. */
  28. #include "tseries.h"
  29. /*
  30. * Example code for clipping a timeseries
  31. */
  32. ts_timeseries *
  33. clip(ts_timeseries *ts, mi_datetime *start, mi_datetime *end, MI_FPARAM *fParam)
  34. {
  35. ts_tsdesc *tsdesc;
  36. ts_tsdesc *new_tsdesc;
  37. ts_tscan *tsscan;
  38. ts_tselem elem;
  39. mi_integer ret;
  40. mi_integer nelems;
  41. mi_integer off;
  42. mi_boolean isreg;
  43. ts_timeseries *new_ts;
  44. MI_TYPEID *ts_typeid;
  45. MI_CONNECTION *conn;
  46. conn = mi_open(NULL, NULL, NULL);
  47. /* get the type info for the input timeseries */
  48. ts_typeid = mi_fp_argtype(fParam, 0);
  49. /* open the input timeseries */
  50. tsdesc = ts_open(conn, ts, ts_typeid, 0);
  51. /*
  52. * not really needed, figure out how many elements
  53. * could possibly be in result timeseries. This guess
  54. * will likely be way too big. Might be better just
  55. * to use 0.
  56. */
  57. nelems = ts_nelems(tsdesc);
  58. /*
  59. * create the new timeseries. Make it look like
  60. * the original timeseries - regularity, calendar, container
  61. */
  62. isreg = !TS_IS_IRREGULAR(ts);
  63. new_ts = ts_create(conn,
  64. ts_get_calname(ts),
  65. start,
  66. ts_get_threshold(ts),
  67. isreg ? 0: TS_CREATE_IRR,
  68. ts_typeid,
  69. nelems,
  70. ts_get_containername(ts));
  71. /* open the output timeseries */
  72. new_tsdesc = ts_open(conn, new_ts, ts_typeid, 0);
  73. /* start scanning the input timeseries between the given dates */
  74. off = -1;
  75. tsscan = ts_begin_scan(tsdesc, 0, start, end);
  76. while ((ret = ts_next(tsscan, &elem)) != TS_SCAN_EOS) {
  77. off++;
  78. if (ret == TS_SCAN_NULL)
  79. /* just skip null elements */
  80. continue;
  81. /* add the data */
  82. if (isreg)
  83. ts_put_nth_elem(new_tsdesc, elem, off);
  84. else
  85. ts_put_elem(new_tsdesc, elem, ts_current_timestamp(tsscan));
  86. }
  87. /* clean up */
  88. ts_end_scan(tsscan);
  89. ts_close(tsdesc);
  90. ts_close(new_tsdesc);
  91. /* return the result timeseries */
  92. return(new_ts);
  93. }