public static HttpResponseMessage GetHttpResponse(ResponseMessage daymetResponse) { HttpResponseMessage httpResponse = new HttpResponseMessage(); httpResponse.Content = daymetResponse.Content; if (daymetResponse.StatusCode == ResponseStatus.OK) { httpResponse.StatusCode = HttpStatusCode.OK; } else if (daymetResponse.StatusCode == ResponseStatus.NotFound) { httpResponse.StatusCode = HttpStatusCode.NotFound; } else if (daymetResponse.StatusCode == ResponseStatus.InternalServerError) { httpResponse.StatusCode = HttpStatusCode.InternalServerError; } else if (daymetResponse.StatusCode == ResponseStatus.BadRequest) { httpResponse.StatusCode = HttpStatusCode.BadRequest; } else { httpResponse.StatusCode = HttpStatusCode.Continue; } return httpResponse; }
public static ResponseMessage GetWatershedSingleVaporPresDataPointNetCDFFile(CancellationToken ct, string sourceNetCDFFileNamePatternToMatch, string workingRootDirPath, DateTime simulationStartDate, DateTime simulationEndDate) { ResponseMessage response = new ResponseMessage(); if (ct.IsCancellationRequested) { return GetCancellationResponse(); } string wsDEMFileName = UEB.UEBSettings.WATERSHED_DEM_RASTER_FILE_NAME; string watershedFilePath = string.Empty; string inputWSDEMRasterFile = string.Empty; string targetPythonScriptFile = string.Empty; string inputVpDaymetDataFilePath = string.Empty; string outNetCDFVpDataFilePath = string.Empty; string outNetCDFVpDataFileName = UEB.UEBSettings.WATERSHED_SINGLE_VP_NETCDF_FILE_NAME; string outVpDataRasterFilePath = string.Empty; targetPythonScriptFile = Path.Combine(UEB.UEBSettings.PYTHON_SCRIPT_DIR_PATH, "CalculateWatershedDaymetVPDGDAL.py"); inputVpDaymetDataFilePath = UEB.UEBSettings.DAYMET_RESOURCE_VP_DIR_PATH; outNetCDFVpDataFilePath = Path.Combine(workingRootDirPath, UEB.UEBSettings.DAYMET_NETCDF_OUTPUT_VP_SUB_DIR_PATH); outVpDataRasterFilePath = Path.Combine(workingRootDirPath, UEB.UEBSettings.DAYMET_RASTER_OUTPUT_VP_SUB_DIR_PATH); watershedFilePath = workingRootDirPath; // UEB.UEBSettings.WORKING_DIR_PATH; // if resampled version of the ws DEM file is available, then use that inputWSDEMRasterFile = Path.Combine(watershedFilePath, wsDEMFileName); if (!File.Exists(inputWSDEMRasterFile)) { string errMsg = string.Format("DEM file ({0}) for the watershed was not found.", inputWSDEMRasterFile); logger.Error(errMsg); response.StatusCode = ResponseStatus.NotFound; response.Content = new StringContent(errMsg); return response; } if (string.IsNullOrEmpty(sourceNetCDFFileNamePatternToMatch)) { string errMsg = "Input Daymet vapor pressure data file name pattern is missing"; logger.Error(errMsg); response.StatusCode = ResponseStatus.BadRequest; response.Content = new StringContent(errMsg); return response; } // validate simluation start and end dates if (simulationStartDate >= simulationEndDate) { string errMsg = "Simulation end date must be a date after simulation start date."; logger.Error(errMsg); response.StatusCode = ResponseStatus.BadRequest; response.Content = new StringContent(errMsg); return response; } // check if the python script file exists if (!File.Exists(targetPythonScriptFile)) { string errMsg = string.Format("Python script file ({0}) to generate vapor pressure data for the watershed was not found.", targetPythonScriptFile); logger.Error(errMsg); response.StatusCode = ResponseStatus.NotFound; response.Content = new StringContent(errMsg); return response; } // if the output dir paths do not exist, then create those paths if (!Directory.Exists(outNetCDFVpDataFilePath)) { Directory.CreateDirectory(outNetCDFVpDataFilePath); } if (!Directory.Exists(outVpDataRasterFilePath)) { Directory.CreateDirectory(outVpDataRasterFilePath); } // get names of all the input vapor pressure netcdf files from the _inputVpDaymetDataFilePath DirectoryInfo di = new DirectoryInfo(inputVpDaymetDataFilePath); var vpNetcdfFiles = di.GetFiles(sourceNetCDFFileNamePatternToMatch); // e. g. "vp*.nc" if (vpNetcdfFiles.Count() == 0) { string errMsg = "No vapor pressure data files were found in the specified directory:" + inputVpDaymetDataFilePath; logger.Error(errMsg); response.StatusCode = ResponseStatus.NotFound; response.Content = new StringContent(errMsg); return response; } string vpNetCDFFileListString = string.Empty; foreach (FileInfo file in vpNetcdfFiles) { vpNetCDFFileListString += file.Name + ";"; } // remove the last semicolon from the file list string vpNetCDFFileListString = vpNetCDFFileListString.Remove(vpNetCDFFileListString.LastIndexOf(';')); try { //create the list of arguments for the python script List<string> arguments = new List<string>(); arguments.Add(EnvironmentSettings.PythonExecutableFile); arguments.Add(targetPythonScriptFile); arguments.Add(inputVpDaymetDataFilePath); arguments.Add(outNetCDFVpDataFilePath); arguments.Add(outNetCDFVpDataFileName); arguments.Add(outVpDataRasterFilePath); arguments.Add(inputWSDEMRasterFile); arguments.Add(vpNetCDFFileListString); arguments.Add(simulationStartDate.ToString("yyyy/MM/dd")); arguments.Add(simulationEndDate.ToString("yyyy/MM/dd")); // create a string containing all the argument items separated by a space string commandString = string.Join(" ", arguments); object command = commandString; // execute python script Python.PythonHelper.ExecuteCommand(command); string responseMsg = "Watershed Daymet vapor pressure NetCDF file was created."; response.Content = new StringContent(responseMsg); response.StatusCode = ResponseStatus.OK; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/text"); logger.Info(responseMsg); } catch (Exception ex) { response.Content = new StringContent(ex.Message); response.StatusCode = ResponseStatus.InternalServerError; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/text"); logger.Fatal(ex.Message); } return response; }
public static ResponseMessage GetWatershedMultiplePrecpDataPointsNetCDFFile(CancellationToken ct, int timeStep, string workingRootDirPath, DateTime simulationStartDate) { ResponseMessage response = new ResponseMessage(); if (ct.IsCancellationRequested) { return GetCancellationResponse(); } string targetPythonScriptFile = string.Empty; string inputSingleDailyPrecpDataFile = string.Empty; string inputSingleDailyPrecpDataFilePath = string.Empty; string outNetCDFMultipleDailyPrecpDataFile = string.Empty; string outNetCDFMultipleDailyPrecpDataFilePath = string.Empty; string destinationPathForNetCDFMultipleDailyPrecpDataFile = string.Empty; // validate timeStep value List<int> allowedTimeStepValues = new List<int> { 1, 2, 3, 4, 6 }; if (allowedTimeStepValues.Contains(timeStep) == false) { string errMsg = string.Format("Provided time step value ({0}) is invalid", timeStep); response.Content = new StringContent(errMsg); response.StatusCode = ResponseStatus.BadRequest; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/text"); logger.Fatal(errMsg); return response; } targetPythonScriptFile = Path.Combine(UEB.UEBSettings.PYTHON_SCRIPT_DIR_PATH, "GenerateWatershedDaymetMultiplePrecpDataPointsPerDay.py"); inputSingleDailyPrecpDataFilePath = Path.Combine(workingRootDirPath, UEB.UEBSettings.DAYMET_NETCDF_OUTPUT_PRECP_SUB_DIR_PATH); inputSingleDailyPrecpDataFile = Path.Combine(inputSingleDailyPrecpDataFilePath, UEB.UEBSettings.WATERSHED_SINGLE_PRECP_NETCDF_FILE_NAME); outNetCDFMultipleDailyPrecpDataFilePath = Path.Combine(workingRootDirPath, UEB.UEBSettings.DAYMET_NETCDF_OUTPUT_PRECP_SUB_DIR_PATH); outNetCDFMultipleDailyPrecpDataFile = Path.Combine(outNetCDFMultipleDailyPrecpDataFilePath, UEB.UEBSettings.WATERSHED_MULTIPLE_PRECP_NETCDF_FILE_NAME); destinationPathForNetCDFMultipleDailyPrecpDataFile = workingRootDirPath; // check if the python script file exists if (!File.Exists(targetPythonScriptFile)) { string errMsg = string.Format("Python script file ({0}) to generate multiple daily precipitation data for the watershed was not found.", targetPythonScriptFile); logger.Error(errMsg); response.StatusCode = ResponseStatus.NotFound; response.Content = new StringContent(errMsg); return response; } // check if the daily single precipitation netcdf input file exists if (!File.Exists(inputSingleDailyPrecpDataFile)) { string errMsg = string.Format("Watershed daily single precipitation data netcdf file ({0}) was nout found.", inputSingleDailyPrecpDataFile); logger.Error(errMsg); response.StatusCode = ResponseStatus.NotFound; response.Content = new StringContent(errMsg); return response; } // if the output dir paths do not exist, then create those paths if (!Directory.Exists(destinationPathForNetCDFMultipleDailyPrecpDataFile)) { Directory.CreateDirectory(destinationPathForNetCDFMultipleDailyPrecpDataFile); } if (!Directory.Exists(outNetCDFMultipleDailyPrecpDataFilePath)) { Directory.CreateDirectory(outNetCDFMultipleDailyPrecpDataFilePath); } try { //create the list of arguments for the python script List<string> arguments = new List<string>(); arguments.Add(EnvironmentSettings.PythonExecutableFile); arguments.Add(targetPythonScriptFile); arguments.Add(inputSingleDailyPrecpDataFile); arguments.Add(outNetCDFMultipleDailyPrecpDataFile); arguments.Add(destinationPathForNetCDFMultipleDailyPrecpDataFile); arguments.Add(timeStep.ToString()); arguments.Add(simulationStartDate.ToString("yyyy/MM/dd")); // create a string containing all the argument items separated by a space string commandString = string.Join(" ", arguments); object command = commandString; // execute the python script Python.PythonHelper.ExecuteCommand(command); string responseMsg = string.Format("Watershed Daymet daily multiple precipitation ({0}) NetCDF file was created.", outNetCDFMultipleDailyPrecpDataFile); response.Content = new StringContent(responseMsg); response.StatusCode = ResponseStatus.OK; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/text"); logger.Info(responseMsg); } catch (Exception ex) { response.Content = new StringContent(ex.Message); response.StatusCode = ResponseStatus.InternalServerError; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/text"); logger.Fatal(ex.Message); } return response; }
private static ResponseMessage GetCancellationResponse() { ResponseMessage response = new ResponseMessage(); response.StatusCode = ResponseStatus.BadRequest; response.Content = new StringContent("Job has been cancelled."); return response; }