I want calculate SPI from CMIP6 monthly precipitation climate data using Rstudio but have only seen methods of doing this when txt files or csv files are used. I have tried CLIMPACT2 but had no luck and can't see to find another way of doing it.
You could try to use the SPEI package directly in R: https://spei.csic.es/tools.html
FYI, I never used CLIMPACT2, but on their website I read this: Known issues
When using the GUI or batch script, plots of the SPEI and SPI indices do not get printed into the aggregated PDF. Individual plot files are still made.
Due to an error in the SPEI package that Climpact2 uses, SPEI and SPI will not be correct if your data contain missing values (eg. they are based on observations). This only applies to netCDF data; if using the GUI then this is not an issue.
Thank you! The issue I have is that my data is in NetCDF format and I'm not sure how to calculate it from this as I've only seen help with it when it is in csv format.
Chloe Spraggs Check out the R script and the NC file I used attached along with this Answer. I Calculated the SPI for a rectangular area over India using the ERA5 monthly averaged total precipitation(from 1979-2020). I used the SPEI package (with which climapact2 is integrated to as well) for that.
The thing about this R function (spi) is that it requires an "R time series object" in order to be able to calculate the Indices, It does not matter if you're creating the time series object from a csv file, text file or a NetCDF file.
So, since your problem is just the extraction of that time series object from a NetCDF file, you just need to use the "ncdf4 package" which I have in the attached script as well.
Here I just calculated the SPI for Spatial mean of the entire rectangular area, In case you want to calculate it for a single point (or a city), you can just tweak a couple of lines in SEC 7 of the code and obtain the desired result.
Also you might wanna make some basic changes like setting the working directory, changing the file name, variable etc. in SEC 2,5, and 7 according to your CMIP6 NetCDF file.
Mohammed Cassim Sannan The code works great for my netcdf file, thank you!. However, since my last post post I am now using NA-CORDEX data and want to crop it to the southwest region (Arizona, Utah, New Mexico, Colorado, California and Nevada) as this is my study area. Obviously it is the same principle with the code, but I was wondering about the cropping to just this region? You mentioned tweaking the code on section 7 but I am quite a novice in R and was wondering which bit I would change? I have used the extent() function before but not sure where that would come in here?
So, Chloe Spraggs the "extent" function is actually used to clip a raster object into a square box and then Work on it. Suppose you identify the min/max co-ordinates to draw the box and clip the raster, that wont be a technically correct SPI for the Southwest USA region as the Ocean on the west coast will also get included in the box you're cutting out using "extent".
Also, If you needed the Data for a specific city located in lon=70,lat=49 you'd just make the section 7 look like this: