Just a quick note to announce that the makeGlobcolourField and isin.convert functions have been added to the sinkr package. In addition, the makeGlobcolourField function now used the ncdf4 package to read the .nc files. Both functions are only set up to deal with the higher resolution 4 km data based on the ISIN grid ("L3b").
The following script is an example of extracting data for the Philippines, and produces a map of mean Chl1 values:
Example script:
Showing posts with label spatial. Show all posts
Showing posts with label spatial. Show all posts
Wednesday, February 17, 2016
Working with Globcolour data (Part 2)
Labels:
chlorophyll,
map,
map projection,
netcdf,
ocean color,
R,
remote sensing,
sinkr,
sparse data,
spatial
Wednesday, April 8, 2015
Map projection cheat sheet
Here's a cheat sheet for map projection settings for the mapproject function (mapproj package). I've repeated the same general procedure for each map: 1. create blank world map, 2. project colored polygons, 3. overlay country boundaries. Some specifications were needed for individual map projections (i.e. different orientation, lat/lon limits, additional parameters). Most projections work well as long as the limits and orientation are valid - in a few cases country boundaries ('square' and 'hex') were wrapped incorrectly. I was unable to get the two spheroid projections working due to the escape character in their names ('sp\_mercator', 'sp\_albers') (maybe someone has a suggestion here?).
In addition to the maps and mapproj packages, the sinkr package is also used for the calculation of global distance, value to color conversion, and polygon creation.
Script to reproduce (high resolution png and pdf versions):
Labels:
climate science,
graphics,
map,
map projection,
R,
sinkr,
spatial
Tuesday, October 30, 2012
DINEOF (Data Interpolating Empirical Orthogonal Functions)
I finally got around to reproducing the DINEOF method (Beckers and Rixon, 2003) for optimizing EOF analysis on gappy data fields - it is especially useful for remote sensing data where cloud cover can result in large gaps in data. Their paper gives a nice overview of some of the various methods that have been used for such data sets. One of these approaches, which I have written about before, involves deriving EOFs from a covariance matrix as calculated from available data. Unfortunately, as the author's point out, such covariance matrices are no longer positive-definite, which can lead to several problems. The DINEOF method seems to overcome several of these issues.
Labels:
algorithm,
DINEOF,
EOF,
function,
gappy data,
interpolation,
optimization,
PCA,
R,
sparse data,
spatial,
SVD
Friday, April 27, 2012
Create polygons from a matrix
The following function matrix.poly allows for the addition of polygons to a plot based on a matrix and defined matrix positions. I have used this function on occasion to highlight specific matrix locations (e.g. in the above figure). You can do the same by overlaying another image (left in above plot) but with this function you will have all other polygon plotting possibilities (e.g. borders etc.).
Monday, April 2, 2012
Working with Globcolour data
The Globcolour project (http://www.globcolour.info/)
provides relatively easy access to ocean color remote sensing data. Data is
provided at http://hermes.acri.fr/
and the following parameters are available:
· Chlorophyll-a (CHL1 and CHL2)
· Fully normalised water leaving radiances at 412, 443, 490,
510, 531, 550-565, 620, 665-
670, 681 and 709 nm (Lxxx)
· Coloured dissolved and detrital organic materials
absorption coefficient (CDM)
· Diffuse attenuation coefficient (Kd(490))
· Particulate back-scattering coefficient (bbp)
· Total Suspended Matter (TSM)
· Relative excess of radiance at 555 nm (EL555)
· Photosynthetic Available Radiation (PAR)
· Heated layer depth (ZHL)
· Secchi disk depth (ZSD)
· Primary production (PP)
· Aerosol optical thickness over water (T865)
· Cloud Fraction (CF)
Of particular interest to ecologists are the estimates of Chlorophyll a (chla) , which combines data from several satellites for better coverage - SeaWiFS (NASA), MODIS (NASA), MERIS (ESA). Data is available
at several temporal (daily, 8-days, and monthly averages) and spatial (4.63 km,
0.25°, and 1°) resolutions for the global domain. Several merged products are available: simple averaging (AV), weighted averaging (AVW), and Garver, Siegel, Maritorena Model (GSM) [for more information see the Product User Guide].
Due to the gappy nature of the data (e.g. due to land and clouds), many of the data products only provide values at grids where estimation was possible. For high resolution data, such as in 4.63 km resolution daily estimates, grids with values are often far fewer than the total number of ISIN grids (n=23,761,676) used by the product. This saves space in the files for download, but you may need to reconstruct the field (with NaN's included for grids without observations) for some analyses.
Due to the gappy nature of the data (e.g. due to land and clouds), many of the data products only provide values at grids where estimation was possible. For high resolution data, such as in 4.63 km resolution daily estimates, grids with values are often far fewer than the total number of ISIN grids (n=23,761,676) used by the product. This saves space in the files for download, but you may need to reconstruct the field (with NaN's included for grids without observations) for some analyses.
The following example shows how to retrieve Globcolour data
and process it using R. Global data is available, but I have provided
instructions for processing a smaller area of 4.63 km resolution chla data from
the Galapagos archipelago. One can define lat/lon limits for the desired area on the http://hermes.acri.fr/ interface. An ftp address will be sent via Email as to the location of the data when finished.
Labels:
chlorophyll,
climate science,
gappy data,
map,
map projection,
netcdf,
ocean color,
phytoplankton,
R,
remote sensing,
sparse data,
spatial
Add a frame to a map
Here is a function that adds a frame of alternating colors to a map (un-projected). One defines the extension of each bar (in degrees) and an optional width of the bars (in inches). It uses the "joinPolys" function of the package to trim the bars near the map corners where the axes meet.
the map.frame function:
Sunday, March 25, 2012
Canonical Correlation Analysis for finding patterns in coupled fields
![]() |
| First CCA pattern of Sea Level Pressure (SLP) and Sea Surface Temperature (SST) monthly anomalies for the region between -180 °W to -70 °W and +30 °N to -30 °S. |
This particular method was illustrated by Barnett and Preisendorfer (1997) - it constructs a CCA model based on a truncated subset of EOF coefficients (i.e. "principle components") instead of using the original field (as with MCA). This truncation has several benefits for the fitting of the model - First, one reduces the amount of noise in the problem by eliminating the higher EOF modes, which represent poorly organized, small-scale features of the fields. Second, by using orthogonal functions, the algebra of the problem is simplified (see von Storch and Zweiers 1999 for details). Bretherton etal. (1992) reviewed several techniques for diagnosing coupled patterns and found the Barnett and Preisendorfer method (hereafter "BPCCA") and MCA to be the most robust.
Labels:
CCA,
climate science,
correlation,
EOF,
gappy data,
map,
map projection,
MCA,
netcdf,
PCA,
pseudoinverse,
R,
sparse data,
spatial,
SVD
Wednesday, March 14, 2012
A ridiculous proof of concept: xyz interpolation
![]() |
| Ridiculous Orb |
This is really the last one on this theme for a while... I had alluded to a combination of methods regarding xyz interpolation at the end of my last post and wanted to demonstrate this in a final example.
The ridiculousness that you see above involved two interpolation steps. First, a thin plate spline interpolation ("Tps" function of the fields package) is applied to the original random xyz field of distance to Mecca. This fitted model is then used to predict values at a new grid of 2° resolution. Finally, in order to avoid plotting polygons for each grid (which can be slow for fine grids), I obtain their projected coordinates with the mapproject function. Using these projected coordinates and their respective z values, a second interpolation is done with the "interp" function of the akima package onto a relatively fine grid of 1000x1000 positions. The result is a smooth field that can then be overlayed on the map using the "image" function (very fast).
So you may ask - When is this even necessary? I would say that it really only makes sense for projecting a filled.contour-type plot for relatively sparse geographic data. Be warned - for large amounts of xyz data, the interpolation algorithms can take a long time.
A couple of functions, found within this blog, are needed to reproduce the plot (earth.dist, color.palette).
the code to reproduce the figure...
Labels:
contour,
filled.contour,
gappy data,
imaging,
interpolation,
map,
map projection,
R,
sparse data,
spatial,
thin plate spline
Wednesday, February 29, 2012
XYZ geographic data interpolation, part 2
Having recently received a comment on a post regarding geographic xyz data interpolation, I decided to return to my original "xyz.map" function and open it up for easier interpretation. This should make the method easier to adapt and follow.
The above graph shows the distance to Mecca as interpolated from 1000 randomly generated lat/lon data using the "interp" function of the akima package. Several functions, found within this blog, are needed to reproduce the plot (pos2coord, earth.dist, new.lon.lat, color.palette, val2col, image.scale). One thing you will notice is the strip of uninterpolated area within the stereographic projection. This is a problem that I have yet to resolve and has to do with the fact that the interpolation is not considering the connection along the 180° longitude line. This will probably require some other type of interpolation based on geographic distances rather than Cartesian coordinates.
R code to produce the above graph...
Tuesday, December 13, 2011
Maximum Covariance Analysis (MCA)
Maximum Correlation Analysis (MCA) is similar to Empirical Orthogonal Function Analysis (EOF) in that they both deal with the decomposition of a covariance matrix. In EOF, this is a covariance matrix based on a single spatio-temporal field, while MCA is based on the decomposition of a "cross-covariance" matrix derived from two fields.
Labels:
climate science,
EOF,
function,
gappy data,
graphics,
map,
MCA,
PCA,
R,
sparse data,
spatial
Thursday, November 24, 2011
Empirical Orthogonal Function (EOF) Analysis for gappy data
[Updates]: The following approach has serious shortcomings, which I have recently become aware of. In a comparison of gappy EOF approaches Taylor et al. (2013) [pdf] show that this traditional approach is not as accurate as others. Specifically, the approach of DINEOF (Data Interpolating Empirical Orthogonal Functions) proved to be the most accurate. I have outlined the DINEOF algorithm in another post [link]. and show a comparison of gappoy EOF methods here: http://menugget.blogspot.de/2014/09/pca-eof-for-data-with-missing-values.html. The R package "sinkr" now contains a version of the function ("eof") for easy installation: https://github.com/menugget/sinkr
-----------------
The following is a function for the calculation of Empirical Orthogonal Functions (EOF). For those coming from a more biologically-oriented background and are familiar with Principal Component Analysis (PCA), the methods are similar. In the climate sciences the method is usually used for the decomposition of a data field into dominant spatial-temporal modes.
Labels:
chlorophyll,
climate science,
EOF,
function,
gappy data,
map,
MCA,
ordination,
PCA,
phytoplankton,
R,
sparse data,
spatial
Monday, September 12, 2011
Converting values to color levels
Adding color to a plot is helpful in many situations for visualizing an additional dimension of the data. Again, I wrote the below function "val2col" in R after having coded this manually over and over in the past. It uses similar arguments as the image function in that one defines the colors to be used as well as optional break points or z-limits by which the data is binned into those colors. The typical situation where I use the function is with the mapping of climate data, yet the addition of color to an XY plot can often be easier on the eyes than adding an additional z-axis spatial dimension. In combination with the image.scale function, that I previously posted, the data can be quickly interpretted.
As an example, gridded sea level pressure is plotted above as projected polygons using the packages maps and mapproj. Values were converted to colors with the val2col function and the image.scale function plotted a corresponding scale. For those interested in using netcdf files, the example also uses the ncdf package for reading the data files into R.
Monday, May 30, 2011
map.xyz(): interpolation of XYZ data and projection onto a map
I am still struggling to get a grasp of R's mapping capabilities. Part of my frustration lies in the fact that I often work on areas near the poles, which complicates interpolation across the 180 degree line. For smaller areas, interpolation can be done using the interp() function in the package akima. I have taken the results from interp and projected the image onto a map. You will need the akima, maps, and mapproj packages and the functions new.lon.lat(), earth.dist(), and pos2coord().
As an example I have mapped the distance from Mecca, Saudi Arabia:
The function...
As an example I have mapped the distance from Mecca, Saudi Arabia:
The function...
Labels:
code,
function,
graphics,
interpolation,
map,
map projection,
R,
spatial
Sunday, May 29, 2011
R functions for Earth geographic coordinate calculations
Here are some functions that I regularly use for geographic data (e.g. binning, filtering, calculation of new positions etc.).
#distance in kilometers between two long/lat positions (from "fossil" package) earth.dist <- function (long1, lat1, long2, lat2) { rad <- pi/180 a1 <- lat1 * rad a2 <- long1 * rad b1 <- lat2 * rad b2 <- long2 * rad dlon <- b2 - a2 dlat <- b1 - a1 a <- (sin(dlat/2))^2 + cos(a1) * cos(b1) * (sin(dlon/2))^2 c <- 2 * atan2(sqrt(a), sqrt(1 - a)) R <- 6378.145 d <- R * c return(d) }
#degree bearing between two long/lat positions (from "fossil" package) earth.bear <- function (long1, lat1, long2, lat2) { rad <- pi/180 a1 <- lat1 * rad a2 <- long1 * rad b1 <- lat2 * rad b2 <- long2 * rad dlon <- b2 - a2 bear <- atan2(sin(dlon) * cos(b1), cos(a1) * sin(b1) - sin(a1) * cos(b1) * cos(dlon)) deg <- (bear%%(2 * pi)) * (180/pi) return(deg) }
new.lon.lat <- function (lon, lat, bearing, distance) { rad <- pi/180 a1 <- lat * rad a2 <- lon * rad tc <- bearing * rad d <- distance/6378.145 nlat <- asin(sin(a1) * cos(d) + cos(a1) * sin(d) * cos(tc)) dlon <- atan2(sin(tc) * sin(d) * cos(a1), cos(d) - sin(a1) * sin(nlat)) nlon <- ((a2 + dlon + pi)%%(2 * pi)) - pi npts <- cbind(nlon/rad, nlat/rad) return(npts) }
#tells which lon lat positions are within the defined limits to the west, east, north, and south lon.lat.filter <- function (lon_vector, lat_vector, west, east, north, south) { if(west>east) { lon_vector_new=replace(lon_vector, which(lon_vector<0), lon_vector[which(lon_vector<0)]+360) east_new=east+360 } else { lon_vector_new=lon_vector east_new=east } hits=which(lon_vector_new < east_new & lon_vector_new > west & lat_vector < north & lat_vector > south) return(hits) }
Subscribe to:
Comments (Atom)












