public override object GetTimeSeries( locationParam lp, VariableParam vp, Nullable <W3CDateTime> startDate, Nullable <W3CDateTime> endDate) { if (lp.isGeometry) { throw new WaterOneFlowException("Geometry not supported "); } TimeSeriesResponseType result = null; string[] StationsList = new string[] { lp.SiteCode }; result = CuahsiBuilder.CreateTimeSeriesObject(); result.queryInfo.criteria.locationParam = lp.ToString(); result.queryInfo.criteria.variableParam = vp.ToString(); result.timeSeries.sourceInfo = DataInfoService.GetSite(lp); // not fully correct, but just choose the first one. VariableInfoType[] vits = DataInfoService.GetVariableInfoObject(vp); result.timeSeries.variable = vits[0]; string aURL = InstantaneousData(startDate, endDate, new string[] { vp.Code }, StationsList, USGSCommon.option2AgencyCode(lp)); try { result.timeSeries.values = CreateWQTimeSeriesObject(vp, aURL); } catch (WaterOneFlowException e) { throw; } catch (WaterOneFlowSourceException e) { throw; } 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(result); }
public SiteInfoResponseType GetSiteInfo(locationParam locationParameter) { string Network; string SiteCode; SiteInfoResponseType result = createSiteInfoResponse(new locationParam[] { locationParameter }); // put a no result check here result.queryInfo.criteria.locationParam = locationParameter.ToString(); // to string returns orignal string if (result.site[0].siteInfo != null) { result.site[0].siteInfo.siteCode[0].network = System.Configuration.ConfigurationManager.AppSettings["network"]; } else { // we only have one site... so throw the exception throw new WaterOneFlowException("Site '" + locationParameter.ToString() + "' Not Found"); } return(result); }
public SiteInfoResponseType GetSiteInfoResponse(locationParam locationParameter) { string Network; string SiteCode; SiteInfoResponseType result = createSiteInfoResponse(new locationParam[] { locationParameter }, true); // put a no result check here //result.queryInfo.criteria.locationParam = locationParameter.ToString(); // to string returns orignal string result.queryInfo = CuahsiBuilder.CreateQueryInfoType("GetSiteInfo", new string[] { locationParameter.ToString() }, null, null, null, null); if (result.site[0].siteInfo != null) { result.site[0].siteInfo.siteCode[0].network = System.Configuration.ConfigurationManager.AppSettings["network"]; } else { // we only have one site... so throw the exception // result.queryInfo.note = CuahsiBuilder.addNote(result.queryInfo.note,CuahsiBuilder.createNote("Site not found")); throw new WaterOneFlowException("Site '" + locationParameter.ToString() + "' Not Found"); } return result; }
public SiteInfoResponseType GetSiteInfoResponse(locationParam locationParameter) { string Network; string SiteCode; SiteInfoResponseType result = createSiteInfoResponse(new locationParam[] { locationParameter }, true); // put a no result check here //result.queryInfo.criteria.locationParam = locationParameter.ToString(); // to string returns orignal string result.queryInfo = CuahsiBuilder.CreateQueryInfoType("GetSiteInfo", new string[] { locationParameter.ToString() }, null, null, null, null); if (result.site[0].siteInfo != null) { result.site[0].siteInfo.siteCode[0].network = System.Configuration.ConfigurationManager.AppSettings["network"]; } else { // we only have one site... so throw the exception // result.queryInfo.note = CuahsiBuilder.addNote(result.queryInfo.note,CuahsiBuilder.createNote("Site not found")); throw new WaterOneFlowException("Site '" + locationParameter.ToString() + "' Not Found"); } return(result); }
public override object GetTimeSeries( locationParam lp, VariableParam vp, Nullable <W3CDateTime> startDate, Nullable <W3CDateTime> endDate) { if (lp.isGeometry) { throw new WaterOneFlowException("Geometry not supported "); } 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, USGSCommon.option2AgencyCode(lp)); try { // refactor too much abstraction //CreateRealTimeSeriesObject(result, RealTime(StationsList)); result.timeSeries.values = CreateGWTimeSeriesObject(vp, aURL); } catch (WaterOneFlowException e) { //log.Error(e.Message + e.StackTrace); throw; } catch (WaterOneFlowSourceException e) { //log.Error(e.Message + e.StackTrace); throw; } catch (WebException e) { log.Error(e.Message + e.StackTrace); throw new WaterOneFlowException("Connecting to External resource failed.", e); } catch (Exception e) { log.Error(e.Message + e.StackTrace); throw new WaterOneFlowException("An External resource failed.", e); } result.timeSeries.sourceInfo = DataInfoService.GetSite(lp); 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(result); }
public override object 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.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 "); } if (BeginDateTime.HasValue) { startDateTime = BeginDateTime.Value.DateTime.ToString("yyyy-MM-dd"); } else { startDateTime = DateTime.Now.AddDays(-29).ToString("yyyy-MM-dd"); } if (EndDateTime.HasValue) { endDateTime = EndDateTime.Value.DateTime.ToString("yyyy-MM-dd"); } else { endDateTime = DateTime.Now.ToString("yyyy-MM-dd"); } String dailyValues = null; try { //dailyValues = svc.getDV(siteNum, // parameterCode, statisticCode, // startDateTime, endDateTime, // agencyCode); //NWISWS.TimeSeriesResponseType response // = svc.GetValuesObject(siteNum, // parameterCode, // startDateTime, endDateTime, // "*****@*****.**"); NWISWS.TimeSeriesResponseType response = svc.GetValuesObject(Location.ToString(), Variable.ToString(), startDateTime, endDateTime, "*****@*****.**"); return(response); } catch (SoapHeaderException she) { log.Info("USGS " + she.Message); throw new WaterOneFlowSourceException("Error connecting to USGS;" + she.Message); } catch (Exception ex) { log.Info("USGS DailyValue Connection Error " + ex.Message); throw new WaterOneFlowSourceException("Error connecting to USGS"); } }
public override object GetTimeSeries( locationParam lp, variableParamVersion vp, W3CDateTime?startDate, W3CDateTime?endDate) { if ((endDate.HasValue && startDate.HasValue) && endDate.Value.DateTime < startDate.Value.DateTime) { throw new WaterOneFlowException("No Data. End Date must be greater than Start Date"); } TimeSeriesResponseType result = null; result = CuahsiBuilder.CreateTimeSeriesObject(); string[] StationsList = new string[] { lp.SiteCode }; SiteInfoType sit = null; try { sit = DataInfoService.GetSite(lp); result.timeSeries.sourceInfo = sit; } catch (WaterOneFlowException ex) { log.Info("EPA Bad Site Request" + vp.ToString()); throw ex; } catch (WaterOneFlowServerException ex) { log.Error("EPA server issued with Site Request" + vp.ToString()); throw ex; } catch (WaterOneFlowSourceException ex) { log.Error("EPA source issued with Site Request" + vp.ToString()); throw ex; } catch (Exception ex) { log.Error("EPA source issued with Site Request" + vp.ToString()); throw new WaterOneFlowServerException("Uncaught Exception in EPA Time series site reuqest", ex); } result.queryInfo.criteria.locationParam = lp.ToString(); result.queryInfo.criteria.variableParam = vp.ToString(); // not fully correct, but just choose the first one. VariableInfoType[] vits = null; try { vits = DataInfoService.GetVariableInfoObject(vp); result.timeSeries.variable = vits[0]; } catch (WaterOneFlowException ex) { log.Info("EPA Bad variable Request" + vp.ToString()); throw ex; } catch (WaterOneFlowServerException ex) { log.Error("EPA server issued with variable Request" + vp.ToString()); throw ex; } catch (WaterOneFlowSourceException ex) { log.Error("EPA source issued with variable Request" + vp.ToString()); throw ex; } catch (Exception ex) { log.Error("EPA source issued with variable Request" + vp.ToString()); throw new WaterOneFlowServerException("Uncaught Exception in EPA Time series site reuqest", ex); } String variableName = result.timeSeries.variable.variableName; string agency = GetAgencyEpaSiteCode(lp); string siteCode = GetSiteIDEpaSiteCode(lp); EPAResults.StoretResultFlatDataTable table = WqxResultsToDataset.GetStationResults( agency, siteCode, variableName, startDate, endDate); if (table.Rows.Count == 0) { // no retults, just go back result.timeSeries.values = new TsValuesSingleVariableType(); result.timeSeries.values.count = "0"; return(result); } DataRowCollection unitNameRows = EPAResponseUnits(table); // for now if (unitNameRows.Count > 1) { //throw new WaterOneFlowSourceException( // " EPA Returned more than one unit for request." // + " This is not supported at the present time" ); // no longer want an error. return data log.Info("EPA STATION WITH MORE THAN ONE UNIT: '" + lp.ToString()); } /* Logic neded for multiple units * select one with max count or latest one? * if max get count for each unit * For the selected unit, * * create a subtable, * * populate values * * Future: * for each unit * *collect a subtable, populate values * */ /* need to creat a units element * The units returned, are not necessarily the ones in the series catalog */ //string unitName = unitNameRows[0].ItemArray[0].ToString().Trim(); //result.timeSeries.variable.units = CuahsiBuilder.CreateUnitsElement(null, null, unitName, unitName); foreach (DataRow row in unitNameRows) { string unitName = row.ItemArray[0].ToString().Trim(); result.timeSeries.variable.units = CuahsiBuilder.CreateUnitsElement(null, null, unitName, unitName); StringBuilder select = new StringBuilder(); select.AppendFormat("[VariableUnitsAbbreviation]='{0}'", WaterOneFlowImpl.WSUtils.SqlEncode(unitName)); if (startDate.HasValue) { select.AppendFormat(" AND [localDateTime] > '{0}' ", startDate.Value.DateTime.ToString("s")); } if (endDate.HasValue) { select.AppendFormat(" AND [localDateTime] < '{0}' ", endDate.Value.DateTime.AddDays(1).ToString("s")); } EPAResults.StoretResultFlatRow[] rowsWithUnit = (EPAResults.StoretResultFlatRow[])table.Select(select.ToString()); result.timeSeries.values = DataValuesBuilder.CreateValuesElement(rowsWithUnit); result.timeSeries.values.unitsAbbreviation = unitName; // abbreviated name is presently returned } /* for now, just adding the methods, without the reference to the * value they came from. That will take a bit of work. * need to generate unique methodID's for distinct methods * then populate the methodID in the table, * then send to the value generator * */ /***** This can cause more method, if nore than one parameter is returned * eg pH presently returns phosphorous * */ DataRowCollection methodNameRows = EPAResponseMethods(table); if (methodNameRows != null && methodNameRows.Count > 0) { List <MethodType> methods; if (result.timeSeries.values.method == null) { methods = new List <MethodType>(); } else { methods = new List <MethodType>(result.timeSeries.values.method); } foreach (DataRow row in methodNameRows) { MethodType method = new MethodType(); method.MethodDescription = row.ItemArray[0].ToString(); methods.Add(method); } result.timeSeries.values.method = methods.ToArray(); } return(result); }
public override object GetTimeSeries( locationParam Location, VariableParam Variable, W3CDateTime?BeginDateTime, W3CDateTime?EndDateTime) { string siteNum; string parameterCode; string statisticCode; string agencyCode = "USGS"; W3CDateTime startDateTime; W3CDateTime endDateTime; if (Location.isGeometry) { throw new WaterOneFlowException("Geometry not supported "); } if (Location != null) { siteNum = Location.SiteCode; agencyCode = option2AgencyCode(Location); } else { throw new WaterOneFlowException("Missing SiteCode "); } if (Variable != null) { parameterCode = Variable.Code; statisticCode = option2UsgsStatCode(Variable); //agencyCode = option2AgencyCode(Variable); } else { throw new WaterOneFlowException("Missing Parameter "); } W3CDateTime startMinDate = W3CDateTime.Now.AddDays(-maxRequestDays); startDateTime = startMinDate; if (BeginDateTime.HasValue) { startDateTime = BeginDateTime.Value; if (startDateTime <= startMinDate) { startDateTime = startMinDate; } } W3CDateTime endDateMax = W3CDateTime.Now; endDateTime = endDateMax; if (EndDateTime.HasValue) { endDateTime = EndDateTime.Value; if (endDateTime >= endDateMax) { endDateTime = endDateMax; } } if (endDateTime < W3CDateTime.Now.AddDays(-maxRequestDays)) { throw new WaterOneFlowException("Only Properties.Settings.Default.UVDaysAvailable days back is supported in the NWIS UV service"); } if (endDateTime < startDateTime) { endDateTime = W3CDateTime.Now; } //TimeSpan span = endDateTime - startDateTime; //if (span.Days > 31 ) //{ // span = new TimeSpan(31, 0, 0); //} //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}"; /* he site number may be optionally prefixed by the agency code followed by a colon, which ensures the site is unique. Examples: ?site=06306300 or ?sites=USGS:06306300. There is no default if this parameter is used. For real-time streamflow sites, the site number is normally 8 characters. Site numbers range from 8 to 15 character. */ string urlFormat = "{0}?sites={1}¶meterCd={2}&startDT={3}&endDT={4}"; string url = string.Format(urlFormat, USGSUVBaseUrl, locationToUvRest(Location), parameterCode, startDateTime.ToString("W"), // startDateTime.ToString("yyyy-MM-dd"), endDateTime.ToString("W") // endDateTime.ToString("yyyy-MM-dd") ); try { Uri uri = new Uri(url); using (WebClient web = new WebClient()) { TimeSeriesResponseType response; using (XmlReader reader = new XmlTextReader(web.OpenRead(url))) { XmlWriter writer = null; MemoryStream memoryStream = new MemoryStream(); try { writer = XmlWriter.Create(memoryStream); // Create the XsltArgumentList. XsltArgumentList argList = new XsltArgumentList(); argList.AddParam("network", "", Location.Network); argList.AddParam("vocabulary", "", Variable.Vocabulary); argList.AddParam("location", "", Location.ToString()); argList.AddParam("variable", "", Variable.ToString()); argList.AddParam("starttime", "", startDateTime.ToString("W")); argList.AddParam("endtime", "", endDateTime.ToString("W")); xslt.Transform(reader, argList, writer); memoryStream.Position = 0; using (var reader2 = XmlReader.Create(memoryStream)) { response = (TimeSeriesResponseType)serializer.Deserialize(reader2); } } finally { memoryStream.Close(); } } //TimeSeriesResponseType response // = new Passthrough(web.OpenRead(url)); return(response); } } catch (WebException ex) { if (ex.Response is HttpWebResponse) { switch (((HttpWebResponse)ex.Response).StatusCode) { case HttpStatusCode.NotFound: log.Info("USGS Not Found" + ex.Message); throw new WaterOneFlowSourceException("USGS Site/Variable combination Not Found"); break; case HttpStatusCode.ServiceUnavailable: log.Info("USGS Service Not Available " + ex.Message); throw new WaterOneFlowSourceException("USGS Service Not Available. Please try later"); break; default: log.Info("USGS Connection Error " + ex.Message); throw new WaterOneFlowSourceException("Error connecting to USGS"); } } log.Info("USGS Connection Error " + ex.Message); throw new WaterOneFlowSourceException("Error connecting to USGS: WebException: " + url); } catch (XmlException ex) { log.Info("Error in communication with USGS " + ex.Message); throw new WaterOneFlowSourceException("Error in communication with USGS: XmlExpcetion "); } catch (XsltException ex) { log.Info("Error in communication with USGS " + ex.Message); throw new WaterOneFlowSourceException("Error in communication with USGS: XsltException "); } catch (Exception ex) { log.Info("Error in communication with USGS " + ex.Message); throw new WaterOneFlowSourceException("Error in communication with USGS: Unknown Exception "); } }
public override object GetTimeSeries( locationParam Location, WaterOneFlowImpl.v1_0.VariableParam Variable, W3CDateTime?BeginDateTime, W3CDateTime?EndDateTime) { // new method. Generate a list of query parameters and values, // tack that onto the end of the base url var queryParameters = new Dictionary <string, string>(); string siteNum; string parameterCode; string statisticCode; string agencyCode = "EPA"; W3CDateTime?startDateTime = null; W3CDateTime?endDateTime = null; if (Location.isGeometry) { throw new WaterOneFlowException("Geometry not supported "); } if (Location != null) { siteNum = Location.SiteCode; agencyCode = option2AgencyCode(Location); queryParameters.Add("organization", agencyCode); queryParameters.Add("siteid", siteNum); } else { throw new WaterOneFlowException("Missing SiteCode "); } if (Variable != null) { parameterCode = Variable.Code; queryParameters.Add("characteristicName", parameterCode); queryParameters.Add("sampleMedia", siteNum); } else { throw new WaterOneFlowException("Missing Parameter "); } if (BeginDateTime.HasValue) { //tartDateTime = BeginDateTime.Value; queryParameters.Add("startDateLo", BeginDateTime.HasValue ? BeginDateTime.Value.ToString("W"): ""); } if (EndDateTime.HasValue) { //ndDateTime = EndDateTime.Value; queryParameters.Add("startDateHi", EndDateTime.HasValue ? EndDateTime.Value.ToString("W"): ""); } if (EndDateTime.HasValue && BeginDateTime.HasValue && EndDateTime.Value < BeginDateTime.Value) { endDateTime = W3CDateTime.Now; queryParameters["startDateHi"] = W3CDateTime.Now.ToString("W"); } // http://storetnwis.epa.gov/storetqw /* he site number may be optionally prefixed by the agency code followed by a colon, which ensures the site is unique. Examples: ?site=06306300 or ?sites=USGS:06306300. There is no default if this parameter is used. For real-time streamflow sites, the site number is normally 8 characters. Site numbers range from 8 to 15 character. */ string urlFormat = "&{0}={1}"; StringBuilder url = new StringBuilder(baseUrl); foreach (var q in queryParameters) { url.AppendFormat("&{0}={1}", q.Key, q.Value); } try { Uri uri = new Uri(url.ToString()); using (WebClient web = new WebClient()) { TimeSeriesResponseType response; using (XmlReader reader = new XmlTextReader(web.OpenRead(uri))) { XmlWriter writer = null; MemoryStream memoryStream = new MemoryStream(); try { writer = XmlWriter.Create(memoryStream); // Create the XsltArgumentList. XsltArgumentList argList = new XsltArgumentList(); argList.AddParam("network", "", Location.Network); argList.AddParam("vocabulary", "", Variable.Vocabulary); argList.AddParam("location", "", Location.ToString()); argList.AddParam("variable", "", Variable.ToString()); argList.AddParam("starttime", "", BeginDateTime.HasValue? BeginDateTime.Value.ToString("W"): ""); argList.AddParam("endtime", "", EndDateTime.HasValue? EndDateTime.Value.ToString("W"): ""); xslt.Transform(reader, argList, writer); memoryStream.Position = 0; using (var reader2 = XmlReader.Create(memoryStream)) { response = (TimeSeriesResponseType)serializer.Deserialize(reader2); } } finally { memoryStream.Close(); } } //TimeSeriesResponseType response // = new Passthrough(web.OpenRead(url)); return(response); } } catch (WebException ex) { if (ex.Response is HttpWebResponse) { switch (((HttpWebResponse)ex.Response).StatusCode) { case HttpStatusCode.NotFound: log.Info("WebSite Not Found" + ex.Message); throw new WaterOneFlowSourceException("Site/Variable combination Not Found"); break; case HttpStatusCode.ServiceUnavailable: log.Info("WebSite Service Not Available " + ex.Message); throw new WaterOneFlowSourceException("Service Not Available. Please try later"); break; default: log.Info("WebSite Connection Error " + ex.Message); throw new WaterOneFlowSourceException("Error connecting to WebSite"); } } log.Info("WebSite Connection Error " + ex.Message); throw new WaterOneFlowSourceException("Error connecting to WebSite: WebException: " + url); } catch (XmlException ex) { log.Info("Error in communication with WebSite " + ex.Message); throw new WaterOneFlowSourceException("Error in communication with WebSite: XmlExpcetion "); } catch (XsltException ex) { log.Info("Error in communication with WebSite " + ex.Message); throw new WaterOneFlowSourceException("Error in communication with WebSite: XsltException "); } catch (Exception ex) { log.Info("Error in communication with WebSite " + ex.Message); throw new WaterOneFlowSourceException("Error in communication with WebSite: Unknown Exception "); } }
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)); }
/* * 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 override object GetTimeSeries( locationParam Location, VariableParam Variable, W3CDateTime?BeginDateTime, W3CDateTime?EndDateTime) { string siteNum; string parameterCode; string statisticCode; string agencyCode = "USGS"; string startDateTime = ""; string endDateTime = ""; if (Location.isGeometry) { throw new WaterOneFlowException("Geometry not supported "); } if (Location != null) { siteNum = Location.SiteCode; if (Location.options.ContainsKey("agency")) { agencyCode = Location.options["agency"].Trim(); } } 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"); } //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 urlFormat = "{0}?SiteNum={1}&ParameterCode={2}&StartDate={3}&EndDate={4}&StatisticCode={5}&AgencyCode={6}"; string url = string.Format(urlFormat, USGSUVBaseUrl, locationParam.SiteCodeRemoveOption(siteNum), parameterCode, startDateTime, endDateTime, statisticCode, agencyCode); try { Uri uri = new Uri(url); using (WebClient web = new WebClient()) { TimeSeriesResponseType response; using (XmlReader reader = new XmlTextReader(web.OpenRead(url))) { XmlWriter writer = null; MemoryStream memoryStream = new MemoryStream(); try { writer = XmlWriter.Create(memoryStream); // Create the XsltArgumentList. XsltArgumentList argList = new XsltArgumentList(); argList.AddParam("network", "", Location.Network); argList.AddParam("vocabulary", "", Variable.Vocabulary); argList.AddParam("location", "", Location.ToString()); argList.AddParam("variable", "", Variable.ToString()); argList.AddParam("starttime", "", startDateTime); argList.AddParam("endtime", "", endDateTime); ; xslt.Transform(reader, argList, writer); memoryStream.Position = 0; using (var reader2 = XmlReader.Create(memoryStream)) { response = (TimeSeriesResponseType)serializer.Deserialize(reader2); } } finally { memoryStream.Close(); } } return(response); } } catch (WebException ex) { if (ex.Response is HttpWebResponse) { switch (((HttpWebResponse)ex.Response).StatusCode) { case HttpStatusCode.NotFound: log.Info("USGS Not Found" + ex.Message); throw new WaterOneFlowSourceException("USGS Site/Variable combination Not Found"); break; case HttpStatusCode.ServiceUnavailable: log.Info("USGS Service Not Available " + ex.Message); throw new WaterOneFlowSourceException("USGS Service Not Available. Please try later"); break; default: log.Info("USGS Connection Error " + ex.Message); throw new WaterOneFlowSourceException("Error connecting to USGS"); } } log.Info("USGS Connection Error " + ex.Message); throw new WaterOneFlowSourceException("Error connecting to USGS: WebException: " + url); } catch (XmlException ex) { log.Info("Error in communication with USGS " + ex.Message); throw new WaterOneFlowSourceException("Error in communication with USGS: XmlExpcetion "); } catch (XsltException ex) { log.Info("Error in communication with USGS " + ex.Message); throw new WaterOneFlowSourceException("Error in communication with USGS: XsltException "); } catch (Exception ex) { log.Info("Error in communication with USGS " + ex.Message); throw new WaterOneFlowSourceException("Error in communication with USGS: Unknown Exception "); } }
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); }