/* * public TimeSeriesResponseType GetValues( * locationParam Location, * VariableParam Variable, * W3CDateTime? BeginDateTime, W3CDateTime? EndDateTime) */ public override WaterOneFlow.Service.Response.v1_1.TimeSeriesResponseType GetTimeSeries( locationParam lp, VariableParam vp, Nullable <W3CDateTime> startDate, Nullable <W3CDateTime> endDate) { // put the date check up front. // a start date can be older than 31 days, as long as the endDate is not older than 31 days. if (endDate.HasValue && endDate.Value.DateTime < DateTime.Today.AddDays(-31)) { throw new WaterOneFlowException("No Data. EndDate must be less than that 31 days from present."); } TimeSeriesResponseType result = null; string[] StationsList = new string[] { lp.SiteCode }; result = CuahsiBuilder.CreateTimeSeriesObject(); result.queryInfo.criteria.locationParam = lp.ToString(); result.queryInfo.criteria.variableParam = vp.ToString(); // not fully correct, but just choose the first one. VariableInfoType[] vits = DataInfoService.GetVariableInfoObject(vp); result.timeSeries.variable = vits[0]; string aUrl = UnitValues(startDate, endDate, new string[] { vp.Code }, StationsList); try { // refactor too much abstraction //CreateRealTimeSeriesObject(result, RealTime(StationsList)); result.timeSeries.values = new TsValuesSingleVariableType[1]; result.timeSeries.values[0] = USGSCommon.CreateTimeSeriesValuesElement(vp, aUrl, true // add provisional flag ); } catch (Exception e) { log.Error(e.Message + e.StackTrace); throw new WaterOneFlowException("An External resource failed.", e); } // all data are provisional List <NoteType> notes = new List <NoteType>(); NoteType pNote = new NoteType(); pNote.title = "USGS Data Provisional"; pNote.href = "http://waterdata.usgs.gov/nwis/help/?provisional"; pNote.Value = "All data are provisional, and subject to revision"; notes.Add(pNote); NoteType urlNote = new NoteType(); urlNote.title = "USGS URL"; urlNote.Value = aUrl; notes.Add(urlNote); result.queryInfo.note = notes.ToArray(); return(new WaterOneFlow.Service.v1_1.xsd.TimeSeriesResponse(result)); }
public virtual string GetValues(string location, string variable, string startDate, string endDate, String authToken) { TimeSeriesResponseType aSite = GetValuesObject(location, variable, startDate, endDate, null); return(WSUtils.ConvertToXml(aSite, typeof(TimeSeriesResponseType))); }
/// <summary> /// Creates the time series object. /// Use CreateTimeSeriesObjectSingleValue to get an object populated with values object /// </summary> /// <returns></returns> public static TimeSeriesResponseType CreateTimeSeriesObject() { TimeSeriesResponseType result = new TimeSeriesResponseType(); result.queryInfo = new QueryInfoType(); result.queryInfo.criteria = new QueryInfoTypeCriteria(); result.queryInfo.criteria.timeParam = new QueryInfoTypeCriteriaTimeParam(); result.timeSeries = new TimeSeriesType(); SiteInfoType site = new SiteInfoType(); site.siteCode = new SiteInfoTypeSiteCode[1]; SiteInfoTypeSiteCode siteId = new SiteInfoTypeSiteCode(); site.siteCode[0] = siteId; result.timeSeries.sourceInfo = site; result.timeSeries.variable = CreateVariableDescriptionType();//new VariablesTypeVariable[1]; //result.timeseries.variable[0] = new VariableDescriptionType(); // new VariablesTypeVariable(); //result.timeseries.variable.variableCode = new VariableDescriptionTypeVariableID[1];//added to simplify population //result.timeseries.variable.variableCode[0] = new VariableDescriptionTypeVariableID(); return result; }
/// <summary> /// GetValues custom implementation /// </summary> /// <param name="SiteNumber">network:SiteCode</param> /// <param name="Variable">vocabulary:VariableCode</param> /// <param name="StartDate">yyyy-MM-dd</param> /// <param name="EndDate">yyyy-MM-dd</param> /// <returns></returns> public TimeSeriesResponseType GetValues(string SiteNumber, string Variable, string StartDate, string EndDate) { Stopwatch timer = System.Diagnostics.Stopwatch.StartNew(); // queryLog.Info("GetValues|" + SiteNumber + "|" + Variable + "|" + StartDate + "|" + EndDate); //String network,method,location, variable, start, end, , processing time,count queryLog2.LogValuesStart(CustomLogging.Methods.GetValues, // method SiteNumber, //locaiton Variable, //variable StartDate, // startdate EndDate, //enddate appContext.Request.UserHostName); //get siteId, variableId string siteId = SiteNumber.Substring(SiteNumber.LastIndexOf(":") + 1); string variableId = Variable.Substring(Variable.LastIndexOf(":") + 1); DateTime startDateTime = new DateTime(2000, 1, 1); DateTime endDateTime = DateTime.Now; if (StartDate != String.Empty) { startDateTime = DateTime.Parse(StartDate); } if (EndDate != String.Empty) { endDateTime = DateTime.Parse(EndDate); } //TimeSeriesResponseType resp = obj.getValues(SiteNumber, Variable, StartDate, EndDate); TimeSeriesResponseType resp = new TimeSeriesResponseType(); resp.timeSeries = new TimeSeriesType[1]; resp.timeSeries[0] = new TimeSeriesType(); resp.timeSeries[0].sourceInfo = WebServiceUtils.GetSiteFromDb2(siteId); resp.timeSeries[0].variable = WebServiceUtils.GetVariableInfoFromDb(variableId); resp.timeSeries[0].values = new TsValuesSingleVariableType[1]; resp.timeSeries[0].values[0] = WebServiceUtils.GetValuesFromDb(siteId, variableId, startDateTime, endDateTime); //set the query info resp.queryInfo = new QueryInfoType(); resp.queryInfo.criteria = new QueryInfoTypeCriteria(); resp.queryInfo.creationTime = DateTime.UtcNow; resp.queryInfo.creationTimeSpecified = true; resp.queryInfo.criteria.locationParam = SiteNumber; resp.queryInfo.criteria.variableParam = Variable; resp.queryInfo.criteria.timeParam = CuahsiBuilder.createQueryInfoTimeCriteria(StartDate, EndDate); queryLog2.LogValuesEnd(CustomLogging.Methods.GetValues, SiteNumber, //locaiton Variable, //variable StartDate, // startdate EndDate, //enddate timer.ElapsedMilliseconds, // processing time // assume one for now resp.timeSeries[0].values[0].value.Length, // count appContext.Request.UserHostName); return(resp); }
private static void AddQueryInfo(string StartDate, string EndDate, string Variable, string SiteNumber, TimeSeriesResponseType response) { response.queryInfo = new QueryInfoType(); response.queryInfo.criteria = new QueryInfoTypeCriteria(); response.queryInfo.creationTime = DateTime.UtcNow; response.queryInfo.creationTimeSpecified = true; response.queryInfo.criteria.locationParam = SiteNumber; response.queryInfo.criteria.variableParam = Variable; response.queryInfo.criteria.timeParam = CuahsiBuilder.createQueryInfoTimeCriteria(StartDate, EndDate); NoteType sourceNote = CuahsiBuilder.createNote("OD Web Service"); response.queryInfo.note = CuahsiBuilder.addNote(response.queryInfo.note, sourceNote); }
public TimeSeriesResponseType GetValuesForSiteVariable(string SiteNumber, string StartDate, string EndDate) { TimeSeriesResponseType response = new TimeSeriesResponseType(); response.timeSeries = new List<TimeSeriesType>( GetTimesSeriesTypeForSiteVariable(SiteNumber, StartDate, EndDate)).ToArray(); response.queryInfo = CuahsiBuilder.CreateQueryInfoType("GetValuesForASite", new string[] {SiteNumber}, null, null, StartDate, EndDate); CuahsiBuilder.addNote(response.queryInfo.note, CuahsiBuilder.createNote("AllValuesForASite")); return response; }
private OutputExchangeItem buildExchangeItemFromFolder(string folder) { //The routine will construct an OpenMI exchange item from a folder of WaterML files. //assumptions: (1) all files within the folder are waterML files // (2) all files within folder have the same variable element // // --- MAPPING BETWEEN OPENMI AND WATERML // Qunatity <-- from the first file in the directory // ID [REQUIRED] = WaterML's variableParam element inner text // Description [optional] = WaterML's variableName element inner text // ValueType [hard coded] = Scalar // Unit // ID [optional]= WaterML's units element attribute unitsAbbreviation // ConversionFactortoSI [optional] = not in WaterML // ConverstionOffsettoSI [optional] = not in WaterML // Dimension // Power [optional] = not in WaterML // ElementSet // ID [REQUIRED] = folder name // Description [REQUIRED] = folder relative path // ElementType [hard coded] = XYPoint or IDBased // Element // ID [optional]= WaterML's SiteCode element inner text [changing to locationParam] // Vertex // X = WaterML's longitude element inner text // Y = WaterML's latitude element inner text // ... // TimeHorizon <-- union of all file-level time horizons //get list of files within the folder string[] files = Directory.GetFiles(folder); //load the first file in the directory as an XML document XmlDocument xmldoc = new XmlDocument(); // load the first xml file in the directory StreamReader sr = new StreamReader(files[0]); //deserialize XmlSerializer xml_reader = new XmlSerializer(typeof(TimeSeriesResponseType)); TimeSeriesResponseType tsr = (TimeSeriesResponseType)xml_reader.Deserialize(sr); Quantity quantity = new Quantity(); Unit unit = new Unit(); Dimension dimension = new Dimension(); ElementSet elementset = new ElementSet(); OutputExchangeItem outputexchangeitem = new OutputExchangeItem(); //Quantity ID -- REQUIRED try { quantity.ID = tsr.queryInfo.criteria.variableParam; } catch { throw new Exception("waterML document must contain a variableParam element"); } //Quantity Description -- optional try { quantity.Description = tsr.timeSeries.variable.variableName; } catch { quantity.Description = ""; } //Quantity Variable Type -- hard coded quantity.ValueType = global::OpenMI.Standard.ValueType.Scalar; //Unit ID -- optional try { unit.ID = tsr.timeSeries.variable.units.unitsAbbreviation; } catch { unit.ID = ""; } //Unit Converstion Factor to SI //TODO WaterML does not include conversion factors to SI //unit.ConversionFactorToSI = 0; //Unit Converstion Offset to SI //TODO WaterML does not include conversion offest to SI //unit.OffSetToSI = 0; quantity.Unit = unit; //Dimension Powers -- optional //TODO WaterML does not include dimension info for units //Examples below ... //dimension.SetPower(DimensionBase.Length, 3); //dimension.SetPower(DimensionBase.Time, -1); quantity.Dimension = dimension; //ElementSet ID -- folder name elementset.ID = new DirectoryInfo(folder).Name; //ElementSet Description -- folder relative path elementset.Description = folder; //ElementSet ElementType -- hard coded elementset.ElementType = ElementType.XYPoint; // ------------------------------------------------------------------- // The remaining objects require access to all files in the directory. // ------------------------------------------------------------------- foreach (string fileName in files) { //load the first file in the directory as an XML document sr = new StreamReader(fileName); tsr = (TimeSeriesResponseType)xml_reader.Deserialize(sr); Element element = new Element(); Vertex vertex = new Vertex(); //Element ID -- optional try { element.ID = tsr.queryInfo.criteria.locationParam; } catch { element.ID = ""; } //Vertex X and Y -- optional //tsr.timeSeries. TODO fix this. //if (xml_location != null && xml_location["longitude"] != null && xml_location["latitude"] != null) //{ // vertex.x = Convert.ToDouble(xml_location["longitude"].InnerText); // vertex.y = Convert.ToDouble(xml_location["latitude"].InnerText); //} //else { vertex.x = double.NaN; vertex.y = double.NaN; elementset.ElementType = ElementType.IDBased; } element.AddVertex(vertex); elementset.AddElement(element); //TimeHorizon -- REQUIRED //if (_earliestInputTime == 0.0) // { string beginDateTimeString; try { beginDateTimeString = tsr.queryInfo.criteria.timeParam.beginDateTime; } catch { throw new Exception("waterML document must contain a beginDateTime element"); } string endDateTimeString; try { endDateTimeString = tsr.queryInfo.criteria.timeParam.endDateTime; } catch { throw new Exception("waterML document must contain an endDateTime element"); } DateTime beginDateTime = Convert.ToDateTime(beginDateTimeString); DateTime endDateTime = Convert.ToDateTime(endDateTimeString); double beginDateTimeDouble = CalendarConverter.Gregorian2ModifiedJulian(beginDateTime); double endDateTimeDouble = CalendarConverter.Gregorian2ModifiedJulian(endDateTime); //update time horizon to be inclusive of this time horizon if (_earliestInputTime == 0.0) { _earliestInputTime = beginDateTimeDouble; } if (beginDateTimeDouble < _earliestInputTime) { _earliestInputTime = beginDateTimeDouble; } ; if (endDateTimeDouble > _latestInputTime) { _latestInputTime = endDateTimeDouble; } ; // } } outputexchangeitem.Quantity = quantity; outputexchangeitem.ElementSet = elementset; // add data operations and return return(addDataOperations(outputexchangeitem)); }
public virtual string GetValues(string locationParam, string VariableCode, string StartDate, string EndDate, String authToken) { TimeSeriesResponseType aSite = GetValuesObject(locationParam, VariableCode, StartDate, EndDate, null); return(WSUtils.ConvertToXml(aSite, typeof(TimeSeriesResponseType))); }
/// <summary> /// Creates the time series object. /// Use CreateTimeSeriesObjectSingleValue to get an object populated with values object /// </summary> /// <returns></returns> public static TimeSeriesResponseType CreateTimeSeriesObject(int numberOfSeries) { TimeSeriesResponseType result = new TimeSeriesResponseType(); result.queryInfo = new QueryInfoType(); result.queryInfo.criteria = new QueryInfoTypeCriteria(); result.queryInfo.criteria.timeParam = new QueryInfoTypeCriteriaTimeParam(); result.timeSeries = new TimeSeriesType[numberOfSeries]; for (int i = 0; i < result.timeSeries.Length; i++) { result.timeSeries[i] = TimeSeriesObjectSingleValue(); } SiteInfoType site = new SiteInfoType(); site.siteCode = new SiteInfoTypeSiteCode[1]; SiteInfoTypeSiteCode siteId = new SiteInfoTypeSiteCode(); site.siteCode[0] = siteId; result.timeSeries[0].sourceInfo = site; result.timeSeries[0].variable = CreateVariableDescriptionType();//new VariablesTypeVariable[1]; //result.timeseries.variable[0] = new VariableDescriptionType(); // new VariablesTypeVariable(); //result.timeseries.variable.variableCode = new VariableDescriptionTypeVariableID[1];//added to simplify population //result.timeseries.variable.variableCode[0] = new VariableDescriptionTypeVariableID(); return result; }
public static TimeSeriesResponseType CreateTimeSeriesObjectSingleValue(int numberOfSeries) { TimeSeriesResponseType result = CreateTimeSeriesObject(numberOfSeries); return(result); }
// Return number of records inserted or found public static int HandleDataValueInfo(SqlConnection sqlConn, OD_SeriesCatalog odSC, OD_1_1_1DataSet.SeriesCatalogRow scRow, SiteInfoType siteInfo, seriesCatalogTypeSeries scts, TimeSeriesResponseType tsRt) { DataValuesTableAdapter dvAdapter = new DataValuesTableAdapter(); dvAdapter.Connection = sqlConn; dvAdapter.Adapter.UpdateBatchSize = Program.DbUpdateBatchSize; dvAdapter.Adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None; // required to do batch insertion string siteCode = siteInfo.siteCode[0].network + "|" + siteInfo.siteCode[0].Value; string varCode = scts.variable.variableCode[0].Value; string cond; OD_1_1_1DataSet.DataValuesDataTable tblDataValues = new OD_1_1_1DataSet.DataValuesDataTable(); Console.WriteLine(">>>Parsing and inserting DATAVALUE"); // Get site ID if (scRow.SiteID == -1) { cond = "SiteCode = '" + siteCode + "'"; scRow.SiteID = OD_Utils.GetPrimaryKey("Sites", "SiteID", cond, sqlConn); } // Get variable ID if (scRow.VariableID == -1) { string[] vars = Regex.Split(varCode, "/"); string dataType = tsRt.timeSeries[0].variable.dataType; cond = "VariableCode = '" + vars[0] + "_" + dataType + "'"; scRow.VariableID = OD_Utils.GetPrimaryKey("Variables", "VariableID", cond, sqlConn); if (scRow.VariableID == -1) { Console.WriteLine("Failed to get variable ID from WS TimeSeries info (code: {0} type: {1}).", varCode, dataType); cond = "VariableCode = '" + vars[0] + "_" + scRow.DataType + "'"; scRow.VariableID = OD_Utils.GetPrimaryKey("Variables", "VariableID", cond, sqlConn); if (scRow.VariableID == -1) { Console.WriteLine("Also failed to get variable ID with code: {0} and type: {1}. Give up.", varCode, scRow.DataType); return(0); } else { Console.WriteLine("Found variable ID {0} with code: {1} and type: {2} from database.", scRow.VariableID, varCode, scRow.DataType); } } } // Update IDs if modified OD_SeriesCatalog odSC, if (scRow.RowState == DataRowState.Modified) { odSC.Adapter.Update(scRow); } // Walk through each data value if ((tsRt.timeSeries[0].values[0].value == null) || (tsRt.timeSeries[0].values[0].value.Count() == 0)) { Console.WriteLine("No values in WS response."); return(0); } int valueCount = tsRt.timeSeries[0].values[0].value.Count(); int currCount = 0, idx0 = 0, dupCount = 0; ValueSingleVariable dvInfo0 = null; bool dup; // Begin database transaction to make sure the end data time and value count // in SeriesCatalog and DataValues tables are consistent. SqlTransaction sqlTrans = sqlConn.BeginTransaction(); for (int i = 0; i < valueCount; i++) { dup = false; ValueSingleVariable dvInfo = tsRt.timeSeries[0].values[0].value[i]; if (dvInfo0 != null) { // We have seen many duplicate dvInfo which caused following DataValue insertion failure. // "Violation of UNIQUE KEY constraint 'UNIQUE_DataValues'. Cannot insert duplicate key in object 'dbo.DataValues'" // Have to skip the duplicate to avoid the whole batch insertion failure. // Simplify the dup check by only comparing dateTime. if (dvInfo0.dateTime == dvInfo.dateTime) { dupCount++; Console.WriteLine("* Index {0} has duplicate time {1:s} with {2}, skip count {3}!", i, dvInfo.dateTime, idx0, dupCount); dup = true; } } if (!dup) { InsertOneDataValue(tblDataValues, scRow.SiteID, scRow.VariableID, dvInfo); idx0 = i; dvInfo0 = dvInfo; currCount++; } if ((currCount == Program.DbUpdateBatchSize) || (i + 1 == valueCount)) { try { dvAdapter.Transaction = sqlTrans; dvAdapter.Update(tblDataValues); scRow.EndDateTime = dvInfo.dateTime; scRow.EndDateTimeUTC = scRow.EndDateTime.ToUniversalTime(); scRow.ValueCount += currCount; odSC.Adapter.Transaction = sqlTrans; odSC.Adapter.Update(scRow); sqlTrans.Commit(); } catch (Exception e) { Console.WriteLine("!!!!!! Got exception: {0}.", e.Message); Console.WriteLine("* Inserted {0} of {1} records for site {2} variable {3}", i + 1 - currCount, valueCount, siteCode, varCode); Console.WriteLine("* Rollback {0} records with {0} of {1} completed!", currCount, i + 1 - currCount, valueCount); sqlTrans.Rollback(); tblDataValues.Clear(); return(i + 1 - currCount); } currCount = 0; tblDataValues.Clear(); if (i + 1 != valueCount) { sqlTrans = sqlConn.BeginTransaction(); } } } dvAdapter.Transaction = null; odSC.Adapter.Transaction = null; return(valueCount - dupCount); }
public object GetValuesObject(string location, string variable, string startDate, string endDate, String authToken) { if (!useODForValues) { throw new SoapException( "GetValues implemented external to this service. Call GetSiteInfo, and SeriesCatalog includes the service Wsdl for GetValues. Attribute:serviceWsdl on Element:seriesCatalog XPath://seriesCatalog/[@serviceWsdl]", new XmlQualifiedName("ServiceException")); } Stopwatch timer = System.Diagnostics.Stopwatch.StartNew(); queryLog2.LogValuesStart(Logging.Methods.GetValues, // method location, //location variable, //variable startDate, // startdate endDate, //enddate Context.Request.UserHostName); try { WaterOneFlowImpl.locationParam lParam = new locationParam(location); VariableParam vparam = new VariableParam(variable); W3CDateTime? startDt = null; W3CDateTime? endDt = null; int estimateCount = 0; if (!String.IsNullOrEmpty(startDate)) { startDt = new W3CDateTime(DateTime.Parse(startDate)); } if (!String.IsNullOrEmpty(endDate)) { endDt = new W3CDateTime(DateTime.Parse(endDate)); } try { if (startDt.HasValue && endDt.HasValue) { estimateCount = (endDt.Value - startDt.Value).Hours * 4; log.DebugFormat("{0}/{1}:estimated count: {2}", endDt.Value, startDt.Value, estimateCount); } } catch { } // USGSwebService.TimeSeriesResponseType res = //(USGSwebService.TimeSeriesResponseType)dvSvc.GetTimeSeries(lParam, vparam, startDt, endDt); TimeSeriesResponseType res = (TimeSeriesResponseType)dvSvc.GetTimeSeries(lParam, vparam, startDt, endDt); if (res.timeSeries != null && res.timeSeries.values != null && res.timeSeries.values.value != null) { queryLog2.LogValuesEnd(Logging.Methods.GetValues, location, //location variable, //variable startDate, // startdate endDate, //enddate timer.ElapsedMilliseconds, res.timeSeries.values.value.Length, Context.Request.UserHostName); } else { queryLog2.LogValuesEnd(Logging.Methods.GetValues, location, //location variable, //variable startDate, // startdate endDate, //enddate timer.ElapsedMilliseconds, estimateCount, Context.Request.UserHostName); } // return new NWISUVTimeSeriesResponse(res);// this one is an original class TrackEvent(location, variable, startDate, endDate, estimateCount, Context); return(new WaterOneFlow.Service.v1_0.xsd.TimeSeriesResponse(res)); } catch (Exception we) { log.Warn(we.Message); TrackEvent(location, variable, startDate, endDate, null, Context); //queryLog2.LogValuesEnd(Logging.Methods.GetValues, // location, //locaiton // variable, //variable // startDate, // startdate // endDate, //enddate // timer.ElapsedMilliseconds, // processing time // -9999, // count // Context.Request.UserHostName // ); throw SoapExceptionGenerator.WOFExceptionToSoapException(we); } }
public override object GetTimeSeries( locationParam Location, VariableParam Variable, W3CDateTime?BeginDateTime, W3CDateTime?EndDateTime) { string siteNum; string parameterCode; string statisticCode; string agencyCode; DateTime startDateTime; DateTime endDateTime; if (Location.isGeometry) { throw new WaterOneFlowException("Geometry not supported "); } if (Location != null) { siteNum = Location.SiteCode; } else { throw new WaterOneFlowException("Missing SiteCode "); } if (Variable != null) { parameterCode = Variable.Code; statisticCode = option2UsgsStatCode(Variable); agencyCode = option2AgencyCode(Variable); } else { throw new WaterOneFlowException("Missing Parameter "); } DateTime startMinDate = DateTime.Now.AddDays(-maxRequestDays); startDateTime = startMinDate; if (BeginDateTime.HasValue) { startDateTime = BeginDateTime.Value.DateTime; if (startDateTime <= startMinDate) { startDateTime = startMinDate; } } DateTime endDateMax = DateTime.Now; endDateTime = endDateMax; if (EndDateTime.HasValue) { endDateTime = EndDateTime.Value.DateTime; if (endDateTime >= endDateMax) { endDateTime = endDateMax; } } if (endDateTime < DateTime.Now.AddDays(-maxRequestDays)) { throw new WaterOneFlowException("Only Properties.Settings.Default.UVDaysAvailable days back is supported in the NWIS UV service"); } if (endDateTime < startDateTime) { endDateTime = DateTime.Now; } //TimeSpan span = endDateTime - startDateTime; //if (span.Days > 31 ) //{ // span = new TimeSpan(31, 0, 0); //} try { //http://waterservices.usgs.gov/WOF/InstantaneousValues?location=06869950&variable=00065&period=P1D //string urlFormat = "http://{0}/{1}?location={2}&variable={3}&period=P{4}D"; //string url = string.Format(urlFormat, "waterservices.usgs.gov", // "WOF/InstantaneousValues", // siteNum, // parameterCode, // span.Days); string urlFormat = "{0}?location={1}&variable={2}&startDate={3}&endDate={4}"; string url = string.Format(urlFormat, USGSUVBaseUrl, siteNum, parameterCode, startDateTime.ToString("yyyy-MM-dd"), endDateTime.ToString("yyyy-MM-dd")); Uri uri = new Uri(url); using (WebClient web = new WebClient()) { TimeSeriesResponseType response = (TimeSeriesResponseType)serializer.Deserialize(web.OpenRead(url)); //TimeSeriesResponseType response // = new Passthrough(web.OpenRead(url)); return(response); } } catch (Exception ex) { log.Info("USGS Connection Error " + ex.Message); throw new WaterOneFlowSourceException("Error connecting to USGS"); } }
private SmartBuffer CreateBuffer(string elementSet) { Dictionary <DateTime, ArrayList> dict = new Dictionary <DateTime, ArrayList>(); SmartBuffer smartbuffer = new SmartBuffer(); //Move to the .dll directory try { Directory.SetCurrentDirectory(_fullPath); } catch (System.IO.IOException) { } elementSet = _dbPath + "\\" + elementSet; string[] files = Directory.GetFiles(elementSet); //read all files within element set foreach (string file in files) { //load the first file in the directory as an XML document XmlDocument xmldoc = new XmlDocument(); // load the first xml file in the directory StreamReader sr = new StreamReader(file); //deserialize XmlSerializer xml_reader = new XmlSerializer(typeof(TimeSeriesResponseType)); TimeSeriesResponseType tsr = (TimeSeriesResponseType)xml_reader.Deserialize(sr); ValueSingleVariable[] values = tsr.timeSeries.values.value; foreach (ValueSingleVariable value in values) { DateTime dt = value.dateTime; double v = Convert.ToDouble(value.Value); //check to see if time/value combination has been already added if (dict.ContainsKey(dt)) { ArrayList a = dict[dt]; a.Add(v); } //Add key to dictionary else { ArrayList a = new ArrayList(); a.Add(v); dict.Add(dt, a); } } } //put values in oder, starting with the earliest time (from http://dotnetperls.com/sort-dictionary-values) var items = from k in dict.Keys orderby dict[k] ascending select k; //load values into the smart buffer foreach (KeyValuePair <DateTime, ArrayList> kvp in dict) { ITimeStamp time_stmp = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(kvp.Key)); double[] valueset = (double[])kvp.Value.ToArray(typeof(double)); smartbuffer.AddValues(time_stmp, new ScalarSet(valueset)); } return(smartbuffer); }
public override WaterOneFlow.Service.Response.v1_1.TimeSeriesResponseType GetTimeSeries( locationParam Location, VariableParam Variable, W3CDateTime?BeginDateTime, W3CDateTime?EndDateTime) { string siteNum; string parameterCode; string statisticCode; string agencyCode; string startDateTime = null; string endDateTime = null; if (Location != null) { siteNum = Location.SiteCode; } else { throw new WaterOneFlowException("Missing SiteCode "); } if (Variable != null) { parameterCode = Variable.Code; statisticCode = option2UsgsStatCode(Variable); agencyCode = option2AgencyCode(Variable); } else { throw new WaterOneFlowException("Missing Parameter "); } if (BeginDateTime.HasValue) { startDateTime = BeginDateTime.Value.DateTime.ToString("yyyy-MM-dd"); } if (EndDateTime.HasValue) { endDateTime = EndDateTime.Value.DateTime.ToString("yyyy-MM-dd"); } String dailyValues = null; try { //dailyValues = svc.getDV(siteNum, // parameterCode, statisticCode, // startDateTime, endDateTime, // agencyCode); gov.usgs.nwis.dailyValues.TimeSeriesResponseType response = svc.getDV(siteNum, parameterCode, statisticCode, startDateTime, endDateTime, agencyCode); StringBuilder sb = new StringBuilder(); // Create an XmlRootAttribute overloaded constructer //and set its namespace. XmlRootAttribute tsXmlRootAttribute = new XmlRootAttribute("timeSeriesResponse"); tsXmlRootAttribute.Namespace = Constants.XML_SCHEMA_NAMSPACE; XmlSerializer xs = new XmlSerializer( typeof(gov.usgs.nwis.dailyValues.TimeSeriesResponseType), tsXmlRootAttribute); XmlWriter writer = XmlWriter.Create(sb); xs.Serialize(writer, response); dailyValues = sb.ToString(); } catch { log.Info("USGS DailyValue Connection Error "); throw new WaterOneFlowSourceException("Error connecting to USGS"); } dailyValues.Replace("ns2:", ""); if (dailyValues.Contains("<Error>")) { log.Info("Error from USGS: " + dailyValues); throw new WaterOneFlowException("Error message from USGS: " + dailyValues); } //String dailyValues = // File.ReadAllText( // "D:\\dev2005\\BasicOneFlowWebService\\BasicWebService\\NwisWOFService\\usgs_samples\\USGSNwisResponse_20071016_modified.xml"); // section rewritten to use passthrough TimeSeriesResponseType res = reserializeResponse(dailyValues); try { res.timeSeries.values[0].count = res.timeSeries.values[0].value.Length; } catch { log.Info("DailyValue response with no values: " + Location.ToString() + " variable:" + Variable); } List <NoteType> notes; if (res.queryInfo.note != null) { notes = new List <NoteType>(res.queryInfo.note); } else { notes = new List <NoteType>(); } NoteType urlNote = new NoteType(); urlNote.title = "CUAHSI Data Source"; urlNote.href = svc.Url; urlNote.Value = "Retrieved from USGS WaterML Soap Interface"; notes.Add(urlNote); res.queryInfo.note = notes.ToArray(); ////TimeSeriesResponsePassthrough pass= new TimeSeriesResponsePassthrough(dailyValues); // NwisWOFService.pass.TimeSeriesResponseType pass = new NwisWOFService.pass.TimeSeriesResponseType(dailyValues); // WaterOneFlow.Service.Schema.v1.TimeSeriesResponseType res = (WaterOneFlow.Service.Schema.v1.TimeSeriesResponseType)pass; return(new WaterOneFlow.Service.v1_1.xsd.TimeSeriesResponse(res)); }
/// <summary> /// GetValues custom implementation /// </summary> /// <param name="SiteNumber">network:SiteCode</param> /// <param name="Variable">vocabulary:VariableCode</param> /// <param name="StartDate">yyyy-MM-dd</param> /// <param name="EndDate">yyyy-MM-dd</param> /// <returns></returns> public TimeSeriesResponseType GetValues(string SiteNumber, string Variable, string StartDate, string EndDate) { Stopwatch timer = System.Diagnostics.Stopwatch.StartNew(); // queryLog.Info("GetValues|" + SiteNumber + "|" + Variable + "|" + StartDate + "|" + EndDate); //String network,method,location, variable, start, end, , processing time,count queryLog2.LogValuesStart(CustomLogging.Methods.GetValues, // method SiteNumber, //locaiton Variable, //variable StartDate, // startdate EndDate, //enddate appContext.Request.UserHostName); //get siteId, variableId string siteId = SiteNumber.Substring(SiteNumber.LastIndexOf(":") + 1); string variableCode = Variable.Substring(Variable.LastIndexOf(":") + 1); //get startDateTime, endDateTime DateTime startDateTime = new DateTime(2000, 1, 1); DateTime endDateTime = DateTime.Now.AddYears(1); if (StartDate != string.Empty) { startDateTime = DateTime.Parse(StartDate); } if (EndDate != string.Empty) { endDateTime = DateTime.Parse(EndDate); } //TimeSeriesResponseType resp = obj.getValues(SiteNumber, Variable, StartDate, EndDate); TimeSeriesResponseType resp = new TimeSeriesResponseType(); resp.timeSeries = new TimeSeriesType[1]; resp.timeSeries[0] = new TimeSeriesType(); resp.timeSeries[0].sourceInfo = WebServiceUtils.GetSiteFromDb2(siteId); resp.timeSeries[0].variable = WebServiceUtils.GetVariableInfoFromDb(variableCode); resp.timeSeries[0].values = new TsValuesSingleVariableType[1]; resp.timeSeries[0].values[0] = WebServiceUtils.GetValuesFromDb(siteId, variableCode, startDateTime, endDateTime); //set the query info resp.queryInfo = new QueryInfoType(); resp.queryInfo.criteria = new QueryInfoTypeCriteria(); resp.queryInfo.creationTime = DateTime.UtcNow; resp.queryInfo.creationTimeSpecified = true; resp.queryInfo.criteria.locationParam = SiteNumber; resp.queryInfo.criteria.variableParam = Variable; resp.queryInfo.criteria.timeParam = CuahsiBuilder.createQueryInfoTimeCriteria(StartDate, EndDate); queryLog2.LogValuesEnd(CustomLogging.Methods.GetValues, SiteNumber, //locaiton Variable, //variable StartDate, // startdate EndDate, //enddate timer.ElapsedMilliseconds, // processing time // assume one for now resp.timeSeries[0].values[0].value.Length, // count appContext.Request.UserHostName); return resp; }
/* * public TimeSeriesResponseType GetValues( * locationParam Location, * VariableParam Variable, * W3CDateTime? BeginDateTime, W3CDateTime? EndDateTime) */ public override WaterOneFlow.Service.Response.v1_1.TimeSeriesResponseType GetTimeSeries( locationParam lp, VariableParam vp, Nullable <W3CDateTime> startDate, Nullable <W3CDateTime> endDate) { TimeSeriesResponseType result = null; //string Network; //string SiteCode; ////WSUtils.ParseSiteId(siteId, out Network, out SiteCode); //try //{ // locationParam sq = new locationParam(siteNumber); // Network = sq.Network; // SiteCode = sq.SiteCode; // if (sq.isGeometry) // { // throw new WaterOneFlowException("Location by Geometry not accepted: " + siteNumber); // } //} //catch (WaterOneFlowException we) //{ // log.Info("Bad SiteID:" + siteNumber); // throw; //} //catch (Exception e) //{ // log.Error("Uncaught exception:" + e.Message); // throw new WaterOneFlowException("Sorry. Your submitted site ID for this getSiteInfo request caused an problem that we failed to catch programmatically: " + e.Message); //} //// string WaterQualityList = "¶meter_cd=" + variable; //string[] StationsList = new string[] { SiteCode }; string[] StationsList = new string[] { lp.SiteCode }; //VariableParam vp; //try //{ // vp = new VariableParam(variable); //} //catch (WaterOneFlowException we) //{ // log.Info("Bad Variable Request '" + variable + "'"); // throw; //} //catch (Exception e) //{ // log.Error("uncaught exception duing Variable Request '" + variable + "'"); // throw new WaterOneFlowException("Sorry. Your variable parameter caused an exception that we failed to catch:" + e.Message); //} result = CuahsiBuilder.CreateTimeSeriesObject(); //// all data are provisional //List<note> notes = new List<note>(); //note urlNote = new note(); //urlNote.title = "USGS Data Provisional"; //urlNote.href = "http://waterdata.usgs.gov/nwis/help/?provisional"; //urlNote.Value = "All data are provisional, and subject to revision"; //notes.Add(urlNote); //result.queryInfo.note = notes.ToArray(); result.queryInfo.criteria.locationParam = lp.ToString(); result.queryInfo.criteria.variableParam = vp.ToString(); // result.queryInfo.criteria.timeParam = CuahsiBuilder.createQueryInfoTimeCriteria(startDate, endDate); // look for siteInfoType in cache, and use if it is there /* SiteInfoType sit = (SiteInfoType)appCache[sitCache + SiteCode]; * if (sit == null) * { * // just use the values for now. * ((SiteInfoType)result.timeSeries.sourceInfo).siteCode[0].Value = SiteCode; * ((SiteInfoType)result.timeSeries.sourceInfo).siteCode[0].network = "NWIS"; * * } * else * { * // in the cache, use it * result.timeSeries.sourceInfo = sit; * } * */ result.timeSeries.sourceInfo = DataInfoService.GetSite(lp); // vaiable info // result.timeSeries.variable = vp.getVariableSchemaType(); //result.timeSeries.variable = NwisVariableCatalog.getVariable(vp.Code, variableDataSet); //result.timeSeries.variable = ODvariables.getVariable(vp.Code, variableDataSet)[0]; // not fully correct, but just choose the first one. VariableInfoType[] vits = DataInfoService.GetVariableInfoObject(vp); result.timeSeries.variable = vits[0]; string aURL = GroundWater(startDate, endDate, StationsList); try { // refactor too much abstraction //CreateRealTimeSeriesObject(result, RealTime(StationsList)); result.timeSeries.values = new TsValuesSingleVariableType[1]; result.timeSeries.values[0] = CreateGWTimeSeriesObject(vp, aURL); } catch (Exception e) { log.Error(e.Message + e.StackTrace); throw new WaterOneFlowException("An External resource failed.", e); } List <NoteType> notes = new List <NoteType>(); NoteType urlNote = new NoteType(); urlNote.title = "USGS URL"; urlNote.Value = aURL; notes.Add(urlNote); result.queryInfo.note = notes.ToArray(); return(new WaterOneFlow.Service.v1_1.xsd.TimeSeriesResponse(result)); }
//TODO implement this function public TimeSeriesResponseType GetValuesForASite(string site, string startDate, string endDate) { { Stopwatch timer = System.Diagnostics.Stopwatch.StartNew(); //String network,method,location, variable, start, end, , processing time,count queryLog2.LogValuesStart(CustomLogging.Methods.GetValuesForSiteObject, // method site, //locaiton "ALL", //variable startDate, // startdate endDate, //enddate appContext.Request.UserHostName); //TimeSeriesResponseType resp = obj.GetValuesForSiteVariable(site, startDate, endDate); TimeSeriesResponseType resp = new TimeSeriesResponseType(); // //String network,method,location, variable, start, end, , processing time,count // queryLog.InfoFormat("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}", //System.Configuration.ConfigurationManager.AppSettings["network"], // network //"GetValues", // method //SiteNumber, //locaiton //Variable, //variable //StartDate, // startdate //StartDate, //enddate //timer.ElapsedMilliseconds, // processing time //resp.timeSeries.values.value.Length // count //, // appContext.Request.UserHostName); queryLog2.LogValuesEnd(CustomLogging.Methods.GetValuesForSiteObject, site, //locaiton "ALL", //variable startDate, // startdate endDate, //enddate timer.ElapsedMilliseconds, // processing time // assume one for now -9999, // May need to count all. appContext.Request.UserHostName); return resp; } }
public override object GetValuesObject(string location, string variable, string startDate, string endDate, String authToken) { if (!useODForValues) { throw new SoapException("GetValues implemented external to this service. Call GetSiteInfo, and SeriesCatalog includes the service Wsdl for GetValues. Attribute:serviceWsdl on Element:seriesCatalog XPath://seriesCatalog/[@serviceWsdl]", new XmlQualifiedName("ServiceException")); } Stopwatch timer = System.Diagnostics.Stopwatch.StartNew(); queryLog2.LogValuesStart(Logging.Methods.GetValues, // method location, //location variable, //variable startDate, // startdate startDate, //enddate Context.Request.UserHostName); try { WaterOneFlowImpl.locationParam lParam = new locationParam(location); VariableParam vparam = new VariableParam(variable); W3CDateTime? startDt = null; W3CDateTime? endDt = null; if (!String.IsNullOrEmpty(startDate)) { startDt = new W3CDateTime(DateTime.Parse(startDate)); } if (!String.IsNullOrEmpty(endDate)) { endDt = new W3CDateTime(DateTime.Parse(endDate)); } TimeSeriesResponseType res = (TimeSeriesResponseType)dvSvc.GetTimeSeries(lParam, vparam, startDt, endDt); if (res != null && res.timeSeries != null && res.timeSeries.values != null) { queryLog2.LogEnd(Logging.Methods.GetValues, location, timer.ElapsedMilliseconds.ToString(), res.timeSeries.values.value.Length.ToString(), Context.Request.UserHostName); } else { queryLog2.LogEnd(Logging.Methods.GetValues, location, timer.ElapsedMilliseconds.ToString(), 0.ToString(), Context.Request.UserHostName); } // return new NWISTimeSeriesResponse(res); return(new WaterOneFlow.Service.v1_0.xsd.TimeSeriesResponse(res)); } catch (Exception we) { log.Warn(we.Message); queryLog2.LogValuesEnd(Logging.Methods.GetValues, location, //locaiton variable, //variable startDate, // startdate startDate, //enddate timer.ElapsedMilliseconds, // processing time -9999, // count Context.Request.UserHostName ); throw SoapExceptionGenerator.WOFExceptionToSoapException(we); } }
public override TimeSeriesResponseType GetTimeSeries( locationParam Location, VariableParam Variable, W3CDateTime?BeginDateTime, W3CDateTime?EndDateTime) { string siteNum; string parameterCode; string statisticCode; string agencyCode; string startDateTime = null; string endDateTime = null; if (Location != null) { siteNum = Location.SiteCode; } else { throw new WaterOneFlowException("Missing SiteCode "); } if (Variable != null) { parameterCode = Variable.Code; } else { throw new WaterOneFlowException("Missing Parameter "); } if (BeginDateTime.HasValue) { startDateTime = BeginDateTime.Value.DateTime.ToString("yyyy-MM-dd"); } if (EndDateTime.HasValue) { endDateTime = EndDateTime.Value.DateTime.ToString("yyyy-MM-dd"); } edu.sdsc.river.TimeSeriesResponseType valuesResponse = null; try { valuesResponse = svc.GetValuesObject(Location.ToString(), Variable.ToString(), startDateTime, endDateTime, null ); } catch { log.Info("DailyValue Connection Error "); throw new WaterOneFlowSourceException("Error connecting to Values Service"); } Stream xStream = new MemoryStream(); serializer.Serialize(xStream, valuesResponse); TimeSeriesResponseType res = (TimeSeriesResponseType)serializer.Deserialize(xStream); // this was for string responses //TimeSeriesResponseType res = reserializeResponse(WebServiceSerializer.Serialize(valuesResponse)); List <note> notes; if (res.queryInfo.note != null) { notes = new List <note>(res.queryInfo.note); } else { notes = new List <note>(); } note urlNote = new note(); urlNote.title = "CUAHSI Data Source"; urlNote.href = svc.Url; urlNote.Value = "Retrieved from WaterML Soap Interface"; notes.Add(urlNote); res.queryInfo.note = notes.ToArray(); return(res); }