Next changeset 1:008cbcc1e3f6 (2018-08-09) |
Commit message:
Uploaded |
added:
clim_data.R raster_getdata.xml |
b |
diff -r 000000000000 -r bdf7d9f2ddae clim_data.R --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clim_data.R Thu Aug 09 05:01:04 2018 -0400 |
[ |
@@ -0,0 +1,136 @@ +#!/usr/bin/env Rscript + + +args <- commandArgs(trailingOnly = TRUE) + +#Rscript clim_data.R 'worldclim' 'var' 'resolution' 'OutputFormat' #'FRA' #'prec1' +if (length(args)==0 | args[1]=="-h" | args[1]=="--help"){ + print ("general script execution : Rscript clim_data.R \'worldclim\' \'var\' resolution \'OutputFormat\' #\'variable-to-plot\' #\'region_code\'") + print ("eg : Rscript clim_data.R \'worldclim\' \'prec\' 10 \'raster\' #\'prec1' #\'FRA\'") + q('no') +} + + +#Climatic variables dictionaries +months<-c("January","February","March","April","May","June","July","August","September","October","November","December") +bioclimatic_vars<-c("Annual Mean Temperature", "Mean Diurnal Range (Mean of monthly (max temp - min temp))", "Isothermality (BIO2/BIO7) (x 100)","Temperature Seasonality (standard deviation x100)","Max Temperature of Warmest Month","Min Temperature of Coldest Month","Temperature Annual Range (BIO5-BIO6)","Mean Temperature of Wettest Quarter","Mean Temperature of Driest Quarter","Mean Temperature of Warmest Quarter","Mean Temperature of Coldest Quarter","Annual Precipitation","Precipitation of Wettest Month","Precipitation of Driest Month","Precipitation Seasonality (Coefficient of Variation)","Precipitation of Wettest Quarter","Precipitation of Driest Quarter","Precipitation of Warmest Quarter","Precipitation of Coldest Quarter") + +#Function to create custom plot title +get_plot_title<-function(usr_var,usr_var_to_plot){ + match<-str_extract(usr_var_to_plot,"[0-9]+") + if(usr_var %in% c("prec","tmin","tmax")){ + printable_var<-(months[as.integer(match)]) + if(usr_var=="prec"){ + printable_var<-paste(printable_var," precipitations (mm)",sep="") + }else if(usr_var=="tmin"){ + printable_var<-paste(printable_var," minimum temperature (°C *10)",sep="") + }else{ + printable_var<-paste(printable_var," maximum temperature (°C *10)",sep="") + } + }else if(usr_var=="bio"){ + printable_var<-(bioclimatic_vars[as.integer(match)]) + printable_var<-paste("Bioclimatic variable - ",printable_var,sep="") + } + title<-paste("Worldclim data - ",printable_var,".",sep="") + return(title) +} + + +#Call libraries +library('raster',quietly=TRUE) +library(sp,quietly = TRUE, warn.conflicts = FALSE) +library(ncdf4,quietly = TRUE, warn.conflicts = FALSE) +library(rgdal,quietly = TRUE, warn.conflicts = FALSE) #To save as geotif +library(stringr) + + +#Get args +usr_data=args[1] +usr_var=args[2] +usr_res=as.numeric(args[3]) +usr_of=args[4] + + +# Retrieve 'var' data from WorldClim +global.var <- getData(usr_data, download = TRUE, var = usr_var, res = usr_res) + +# Check if we actualy get some +if (length(global.var)==0){ + cat("No data found.") +}else{ + writeRaster(global.var, "output_writeRaster", format=usr_of,overwrite=TRUE) + final_msg<-paste("WorldClim data for ", usr_var, " at resolution ", usr_res, " in ", usr_of, " format\n", sep="") + cat(final_msg) +} + + + + + + +################# +##Visualisation## +################# + +#Get args +if(length(args[5])>=0 && length(args[6])>=0){ + usr_var_to_plot=args[5] + usr_plot_region=args[6] +}else{q('no')} + +list_region_mask<-c("FRA","DEU","GBR","ESP","ITA") + +if(usr_plot_region %in% list_region_mask){ +#Country mask + region <- getData("GADM",country=usr_plot_region,level=0) + region_mask <- mask(global.var, region) + region_var_to_plot_expression<-paste("region_mask$",usr_var_to_plot,sep="") +}else{ #All map and resize manualy + region_var_to_plot_expression<-paste("global.var$",usr_var_to_plot,sep="") +} + + +region_var_to_plot<-eval(parse(text=region_var_to_plot_expression)) + +#PLotmap +jpeg(file="worldclim_plot_usr_region.jpeg",bg="white") + +title<-get_plot_title(usr_var,usr_var_to_plot) + + +if(usr_plot_region=="FRA"){ + #FRA + plot(region_var_to_plot, xlim = c(-7, 12), ylim = c(40, 52), axes=TRUE,xlab="Longitude",ylab="Latitude",main=title) +}else if(usr_plot_region=="GBR"){ + #GBR + plot(region_var_to_plot, xlim = c(-10, 5), ylim = c(46, 63), axes=TRUE,xlab="Longitude",ylab="Latitude",main=title) +}else if(usr_plot_region=="NA"){ + #North America : + plot(region_ar_to_plot,xlim=c(-180,-50),ylim=c(10,75),xlab="Longitude",ylab="Latitude",main=title) +}else if(usr_plot_region=="EU"){ + #Europe + plot(region_var_to_plot,xlim=c(-28,48),ylim=c(34,72),xlab="Longitude",ylab="Latitude",main=title) +}else if(usr_plot_region=="DEU"){ + #DEU + plot(region_var_to_plot, xlim = c(5, 15), ylim = c(45, 57),axes=TRUE,xlab="Longitude",ylab="Latitude",main=title) +}else if(usr_plot_region=="ESP"){ + #ESP + plot(region_var_to_plot, xlim = c(-10, 6), ylim = c(35, 45), axes=TRUE,xlab="Longitude",ylab="Latitude", main=title) +}else if(usr_plot_region=="ITA"){ + #ITA + plot(region_var_to_plot, xlim = c(4, 20), ylim = c(35, 48), axes=TRUE,xlab="Longitude",ylab="Latitude", main=title) +}else if(usr_plot_region=="WM"){ + #Worldmap + plot(region_var_to_plot,xlab="Longitude",ylab="Latitude",main=title) +}else if(usr_plot_region=="AUS"){ + #AUS + plot(region_var_to_plot,xlim=c(110,155),ylim=c(-45,-10),xlab="Longitude",ylab="Latitude",axes=TRUE,main=title) +}else{ + write("Error with country code.", stderr()) + q('no') +} + +garbage_output<-dev.off + +#Exit +q('no') |
b |
diff -r 000000000000 -r bdf7d9f2ddae raster_getdata.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/raster_getdata.xml Thu Aug 09 05:01:04 2018 -0400 |
[ |
b'@@ -0,0 +1,210 @@\n+<tool id="raster_getdata" name="Get climatic data from Worldclim" version="0.1.0">\n+ <requirements>\n+\n+<!--\n+ <requirement type="package" >libgcc-ng</requirement>\n+ <requirement type="package" >libiconv</requirement>\n+ <requirement type="package" >jpeg</requirement>\n+ <requirement type="package" >libstdcxx-ng</requirement>\n+ <requirement type="package" >libkml</requirement>\n+\n+\n+-->\n+ <requirement type="package" version="1.2_18">r-rgdal</requirement> \n+ <requirement type="package" version="1.20.2">r-getopt</requirement>\n+ <requirement type="package" version="1.1.6">r-ncdf4</requirement>\n+ <requirement type="package" version="2.6_7">r-raster</requirement>\n+ <requirement type="package" version="1.1.7">r-stringr</requirement>\n+\n+ </requirements>\n+ <command detect_errors="exit_code"><![CDATA[\n+ Rscript \n+ \'$__tool_directory__/clim_data.R\'\n+ \'worldclim\'\n+ \'$usr_var\'\n+ $usr_res\n+ \'$usr_of\'\n+\n+ #if $condi_plot.plot==\'yes_plot\'\n+ \'$usr_var$condi_plot.var_to_plot\'\n+ \'$condi_plot.usr_country\' \n+ #end if\n+\n+ ]]></command>\n+ <inputs>\n+ <param name="usr_var" type="select" label="Variable" help="Temperature minimum, maximum, precipitations and bioclimatic variables.">\n+ <option value="tmin">tmin</option>\n+ <option value="tmax">tmax</option>\n+ <option value="prec">prec</option>\n+ <option value="bio">bio</option>\n+ </param>\n+ <param name="usr_res" type="select" label="Resolution" help="In minutes of a degree.">\n+ <option value="2.5">2.5</option>\n+ <option value="5">5</option>\n+ <option value="10">10</option>\n+ </param>\n+ <param name="usr_of" type="select" label="Output raster format" >\n+ <option value="raster">Native raster package format .grd</option>\n+ <option value="CDF">netCDF .nc</option>\n+ <option value="GTiff">GeoTiff .tif</option>\n+\n+<!--\n+ <option value="ascii">ESRI Ascii .asc</option>\n+ <option value="SAGA">SAGA GIS .sdat</option>\n+ <option value="IDRISI">IDRISI .rst</option>\n+ <option value="ENVI">ENVI .hdr Labelled .envi</option>\n+ <option value="EHdr">ESRI .hdr Labelled .bil</option>\n+ <option value="HFA">Erdas Imagine Images .img</option>\n+-->\n+\n+ </param>\n+ <conditional name="condi_plot">\n+ <param name="plot" type="select" label="Return a layers visualisation">\n+ <option value="yes_plot">Yes</option>\n+ <option value="no_plot">No</option>\n+ </param>\n+ <when value="yes_plot">\n+ <param name="var_to_plot" type="select" label="Select what you want to visualize" help="Be careful to select a layer that is present in the dowloaded raster. eg : bioclimatic variable (bio) and layer bio:Isothermality.">\n+ <option value="1">[tmin|tmax|prec]:January</option>\n+ <option value="2">[tmin|tmax|prec]:February</option>\n+ <option value="3">[tmin|tmax|prec]:March</option>\n+ <option value="4">[tmin|tmax|prec]:April</option>\n+ <option value="5">[tmin|tmax|prec]:May</option>\n+ <option value="6">[tmin|tmax|prec]:June</option>\n+ <option value="7">[tmin|tmax|prec]:July</option>\n+ <option value="8">[tmin|tmax|prec]:August</option>\n+ <option value="9">[tmin|tmax|prec]:September</option>\n+ <option value="10">[tmin|tmax|prec]:October</option>\n+ <option value="11">[tmin|tmax|prec]:November</option>\n+ <option value="12">[tmin|tmax|prec]:December</option>\n+ <option value="1">bio:Annual Mean Tempera'..b'value="13">bio:Precipitation of Wettest Month</option>\n+ <option value="14">bio:Precipitation of Driest Month</option>\n+ <option value="15">bio:Precipitation Seasonality (Coefficient of Variation)</option>\n+ <option value="16">bio:Precipitation of Wettest Quarter</option>\n+ <option value="17">bio:Precipitation of Driest Quarter</option>\n+ <option value="18">bio:Precipitation of Warmest Quarter</option>\n+ <option value="19">bio:Precipitation of Coldest Quarter</option>\n+ </param>\n+\n+ <param name="usr_country" type="select" label="Select a worldmap vision or a more precise region" >\n+ <option value="WM">Worldmap</option>\n+ <option value="NA">North America</option>\n+ <option value="EU">Europe</option>\n+ <option value="AUS">Australia</option>\n+ <option value="FRA">France</option>\n+ <option value="DEU">Germany</option>\n+ <option value="GBR">United Kingdom</option>\n+ <option value="ESP">Spain</option>\n+ <option value="ITA">Italy</option>\n+ </param>\n+ </when>\n+ <when value="no_plot">\n+ </when>\n+ </conditional>\n+ </inputs>\n+ <outputs>\n+ <data name="output_df" from_work_dir="output_writeRaster.nc" label="WorldClim data - NetCDF format">\n+ <filter> usr_of==\'CDF\'</filter>\n+ </data>\n+ <data name="output_grd" from_work_dir="output_writeRaster.grd" label="WorldClim data - Native raster pkg format .grd">\n+ <filter> usr_of==\'raster\'</filter>\n+ </data>\n+ <data name="output_gri" from_work_dir="output_writeRaster.gri" label="WorldClim data - Native raster pkg format .gri">\n+ <filter> usr_of==\'raster\'</filter>\n+ </data>\n+ <data name="plot_jpeg" from_work_dir="worldclim_plot_usr_region.jpeg" format="jpg" label="WorldClim data - Visualisation">\n+ <filter> plot==\'yes_plot\'</filter>\n+ </data>\n+ </outputs>\n+ <help><![CDATA[\n+\n+.. class:: infomark\n+\n+=======================================\n+Get global climate data from Worldclim\n+=======================================\n+\n+|\n+\n+**What it does**\n+\n+This tool retrieve global climate data from Wordclim using the R raster package.\n+\n+The climatic data available are : \n+\n+ * Minimum temperature (\xc2\xb0C *10)\n+\n+ * Maximum temperature (\xc2\xb0C *10)\n+\n+ * Precipitation (mm)\n+\n+ * Bioclimatic variables :\n+ \n+ * BIO1 = Annual Mean Temperature\n+\n+ * BIO2 = Mean Diurnal Range (Mean of monthly (max temp - min temp))\n+\n+ * BIO3 = Isothermality (BIO2/BIO7) (x 100)\n+\n+ * BIO4 = Temperature Seasonality (standard deviation x100)\n+\n+ * BIO5 = Max Temperature of Warmest Month\n+\n+ * BIO6 = Min Temperature of Coldest Month\n+\n+ * BIO7 = Temperature Annual Range (BIO5-BIO6)\n+\n+ * BIO8 = Mean Temperature of Wettest Quarter\n+\n+ * BIO9 = Mean Temperature of Driest Quarter\n+\n+ * BIO10 = Mean Temperature of Warmest Quarter\n+\n+ * BIO11 = Mean Temperature of Coldest Quarter\n+\n+ * BIO12 = Annual Precipitation\n+\n+ * BIO13 = Precipitation of Wettest Month\n+\n+ * BIO14 = Precipitation of Driest Month\n+\n+ * BIO15 = Precipitation Seasonality (Coefficient of Variation)\n+\n+ * BIO16 = Precipitation of Wettest Quarter\n+\n+ * BIO17 = Precipitation of Driest Quarter\n+\n+ * BIO18 = Precipitation of Warmest Quarter\n+\n+ * BIO19 = Precipitation of Coldest Quarter\n+\n+|\n+\n+**How to use it**\n+\n+Simply select the variable to return and the resolution : 2.5, 5 or 10 minutes of a degree.\n+\n+Available output format are : netcdf and grd (Native raster package format).\n+\n+|\n+\n+**Sources**\n+\n+Worldclim : http://www.worldclim.org/\n+\n+Raster package : https://cran.r-project.org/web/packages/raster/index.html\n+\n+ ]]></help>\n+</tool>\n' |