コード例 #1
0
ファイル: TimeSeriesDS.cs プロジェクト: jirikadlec2/hydrodata
        /// <summary>
        /// Retrieves a time series of observations from the database
        /// (only measured, non-zero values are retrieved)
        /// </summary>
        /// <param name="stationId"></param>
        /// <param name="variableId"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="scaleFactor"></param>
        /// <param name="observations"></param>
        public static void LoadObservations(int stationId, int variableId, DateTime start, DateTime end,
            TimeStep step, IObservationList observations)
        {
            //observations.Clear();
            SqlCommand cmd = DataUtils.CreateCommand();
            cmd.CommandText = "plaveninycz.new_query_observations";
            cmd.CommandType = CommandType.StoredProcedure;
            SetCmdParameters(cmd, stationId, variableId, start, end, step);
            SqlDataReader rdr;
            double val;
            DateTime t;

            try
            {
                cmd.Connection.Open();
                rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (rdr.Read())
                {
                    if (!rdr.IsDBNull(1))
                    {
                        t = Convert.ToDateTime(rdr[0]);
                        val = Convert.ToDouble(rdr[1]);
                        if (val > 0)
                        {
                            observations.AddObservation(t, val);
                        }
                        else
                        {
                            observations.AddUnknownValue(t);
                        }
                    }
                }
                rdr.Close();
            }
            finally
            {
                cmd.Connection.Close();
            }
        }
コード例 #2
0
        public static void ReadBinaryFileDaily(string fileName, DateTime startTime, DateTime endTime, 
            bool includeNA, IObservationList observations)
        {
            int SIZEOF_FLOAT = 4;
            int SIZEOF_LONG = 8;

            //clear out any existing obs.
            observations.Clear();
            try
            {
                using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    //read the startDate
                    byte[] startDateBytes = new byte[SIZEOF_LONG];
                    stream.Read(startDateBytes, 0, startDateBytes.Length);
                    long[] startDateBinary = new long[1];
                    Buffer.BlockCopy(startDateBytes, 0, startDateBinary, 0, SIZEOF_LONG);
                    DateTime startDateFromFile = DateTime.FromBinary(startDateBinary[0]);

                    //check start time
                    if (startTime < startDateFromFile)
                    {
                        startTime = startDateFromFile;
                    }

                    //find position of query start time
                    int startTimePositionDays = (int)((startTime - startDateFromFile).TotalDays);
                    if (startTimePositionDays < 0)
                        return;
                    int numDaysInFile = (int)((stream.Length - SIZEOF_LONG) / SIZEOF_FLOAT);
                    DateTime endDateFromFile = startDateFromFile.AddDays(numDaysInFile);

                    if (endTime < startDateFromFile)
                        return;
                    if (startTime > endDateFromFile)
                        return;

                    long startTimePositionInBytes = SIZEOF_LONG + startTimePositionDays * SIZEOF_FLOAT;
                    int numDaysStartEnd = (int)((endTime - startTime).TotalDays);
                    long numBytesStartEnd = numDaysStartEnd * SIZEOF_FLOAT;
                    if (startTimePositionInBytes + numBytesStartEnd > stream.Length)
                    {
                        numBytesStartEnd = stream.Length - startTimePositionInBytes;
                        numDaysStartEnd = (int)(numBytesStartEnd / SIZEOF_FLOAT);
                    }
                    long endTimePositionInBytes = startTimePositionInBytes + numBytesStartEnd;

                    byte[] resultBytes = new byte[numBytesStartEnd];

                    stream.Seek(startTimePositionInBytes, SeekOrigin.Begin);
                    stream.Read(resultBytes, 0, resultBytes.Length);

                    float[] result = new float[numDaysStartEnd];
                    Buffer.BlockCopy(resultBytes, 0, result, 0, resultBytes.Length);

                    DateTime curTime = startTime;
                    for (int i = 0; i < result.Length; i++)
                    {
                        if (result[i] == -9999.0)
                        {
                            if (includeNA)
                            {
                                observations.AddUnknownValue(curTime);
                            }
                        }
                        else
                        {
                            observations.AddObservation(curTime, result[i]);
                        }
                        curTime = curTime.AddDays(1);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }