This script is inspired by a real-world situation and is deliberately somewhat vague. The task is to understand the data, convert it into a suitable format, and find the tools that produce the desired output.
** Task: Produce temperature and precipitation map of Europe for January and July.**
It must be based on the most recet NOAA long term means data from NOAA webpage, the v401 format.
We need to download the data and produce such maps for temperature and precipitation (we are not using any of the tools on the website). For the purpose of this script, I have downloaded the data files and you can access it on this repositories by name of “air.mon.ltm.v401.nc” and “precip.mon.ltm.v401.nc”
# Load Libraries
library(tidyverse)
library(ncdf4)
library(ncdf4.helpers)
library(RColorBrewer)
#Opening the datafile donloaded from NOAA website
airtemp <- nc_open("air.mon.ltm.v401.nc")
rain <- nc_open("precip.mon.ltm.v401.nc")
#Finding Structure and summary of the data
#str(airtemp)
summary(airtemp)
## Length Class Mode
## filename 1 -none- character
## writable 1 -none- logical
## id 1 -none- numeric
## safemode 1 -none- logical
## format 1 -none- character
## is_GMT 1 -none- logical
## groups 1 -none- list
## fqgn2Rindex 1 -none- list
## ndims 1 -none- numeric
## natts 1 -none- numeric
## dim 4 -none- list
## unlimdimid 1 -none- numeric
## nvars 1 -none- numeric
## var 3 -none- list
lon#Looking at variable containing longitude
lonvalues <- airtemp$var$air$dim[[1]]$vals
summary(lonvalues)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.25 90.12 180.00 180.00 269.88 359.75
#Getting the value of the Longitude in a separate variable
lon <- ncvar_get(airtemp,"lon")
#Confirming and checking the data
str(lon) # 1d Array of 720 elements
## num [1:720(1d)] 0.25 0.75 1.25 1.75 2.25 2.75 3.25 3.75 4.25 4.75 ...
lat#Looking at variable containing longitude
latvalues <- airtemp$var$air$dim[[2]]$vals
summary(latvalues)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -89.75 -44.88 0.00 0.00 44.88 89.75
#Getting the value of Latitude in a separate variable
lat <- ncvar_get(airtemp,"lat")
#Confirming and checking the data
str(lat) # 1d Array of 360 elements
## num [1:360(1d)] 89.8 89.2 88.8 88.2 87.8 ...
#Getting Time data
timeval <- ncvar_get(airtemp,"time")
#Examinging the timeval variable
timeval #it has 12 entries and need conversion to the date format
## [1] -16646328 -16645584 -16644912 -16644168 -16643448 -16642704 -16641984
## [8] -16641240 -16640496 -16639776 -16639032 -16638312
Cleaning Time data
#Converting hours data to days data and fixing origin as mentioned in file
newtime <- as.Date((timeval/24),origin="1900-01-01")
newtime
## [1] "0000-12-30" "0001-01-30" "0001-02-27" "0001-03-30" "0001-04-29"
## [6] "0001-05-30" "0001-06-29" "0001-07-30" "0001-08-30" "0001-09-29"
## [11] "0001-10-30" "0001-11-29"
# Months of our interest January and July are 2nd and 8th indices
#of this array
newtime[2] # Gives January month
## [1] "0001-01-30"
newtime[8] # Gives July month
## [1] "0001-07-30"
air which has our temperature values in a variable# Getting values of air temperature data to tmp_array
temperature_array <- ncvar_get(airtemp,"air")
# Checking number of NA values and applying omits
length(na.omit(as.vector(temperature_array[,,1])))
## [1] 85794
#Arrange Latitude in increasing order
lat <- sort(lat)
month <- 2
#Slicing data only for January month whose index is 2
tmp_slice <- temperature_array[,,month]
#plotting using R's base image()
image(lon,lat,tmp_slice, col = rev(brewer.pal(10,"RdBu")))
#Observation: Since January is a winter month we see majority of blues
#representing cold temperatures.
#Arrange Latitude in increasing order
lat <- sort(lat)
month <- 8
#Slicing data only for July month whose index is 8
tmp_slice <- temperature_array[,,month]
#plotting using R's base image()
image(lon,lat,tmp_slice, col = rev(brewer.pal(10,"RdBu")))
#Observation: Since July is a summer month we see majority of reds
#representing warmer temperatures.
#Finding Structure and summary of the data
#str(rain)
summary(rain)
## Length Class Mode
## filename 1 -none- character
## writable 1 -none- logical
## id 1 -none- numeric
## safemode 1 -none- logical
## format 1 -none- character
## is_GMT 1 -none- logical
## groups 1 -none- list
## fqgn2Rindex 1 -none- list
## ndims 1 -none- numeric
## natts 1 -none- numeric
## dim 4 -none- list
## unlimdimid 1 -none- numeric
## nvars 1 -none- numeric
## var 3 -none- list
lon#Looking at variable containing longitude
lonvalues <- rain$var$precip$dim[[1]]$vals
summary(lonvalues)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.25 90.12 180.00 180.00 269.88 359.75
#Getting the value of the Longitude in a separate variable
lon <- ncvar_get(rain,"lon")
#Confirming and checking the data
str(lon) # 1d Array of 720 elements
## num [1:720(1d)] 0.25 0.75 1.25 1.75 2.25 2.75 3.25 3.75 4.25 4.75 ...
lat#Looking at variable containing longitude
latvalues <- rain$var$precip$dim[[2]]$vals
summary(latvalues)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -89.75 -44.88 0.00 0.00 44.88 89.75
#Getting the value of Latitude in a separate variable
lat <- ncvar_get(rain,"lat")
#Confirming and checking the data
str(lat) # 1d Array of 360 elements
## num [1:360(1d)] 89.8 89.2 88.8 88.2 87.8 ...
#Getting Time data
timeval <- ncvar_get(rain,"time")
#Examinging the timeval variable
timeval #it has 12 entries and need conversion to the date format
## [1] -16646328 -16645584 -16644912 -16644168 -16643448 -16642704 -16641984
## [8] -16641240 -16640496 -16639776 -16639032 -16638312
Cleaning Time data
#Converting hours data to days data and fixing origin as mentioned in file
newtime <- as.Date((timeval/24),origin="1900-01-01")
newtime
## [1] "0000-12-30" "0001-01-30" "0001-02-27" "0001-03-30" "0001-04-29"
## [6] "0001-05-30" "0001-06-29" "0001-07-30" "0001-08-30" "0001-09-29"
## [11] "0001-10-30" "0001-11-29"
# Months of our interest January and July are 2nd and 8th indices
#of this array
newtime[2] # Gives January month
## [1] "0001-01-30"
newtime[8] # Gives July month
## [1] "0001-07-30"
precip which has our precipitation values in a variable# Getting values of precipitation data to precipitation_array
precipitation_array <- ncvar_get(rain,"precip")
# Checking number of NA values and applying omits
length(na.omit(as.vector(precipitation_array[,,1])))
## [1] 85794
#Arrange Latitude in increasing order
lat <- sort(lat)
month <- 2
#Slicing data only for January month whose index is 2
precip_slice <- precipitation_array[,,month]
#plotting using R's base image()
image(lon,lat,precip_slice, col = rev(brewer.pal(10,"BrBG")))
#Observation: Since January is a typically rains in South America,
#same is shown in different shades of green in the image below
#Arrange Latitude in increasing order
lat <- sort(lat)
month <- 8
#Slicing data only for July month whose index is 8
precip_slice <- precipitation_array[,,month]
#plotting using R's base image()
image(lon,lat,precip_slice, col = rev(brewer.pal(10,"BrBG")))
#Observation: Since July is a rainy month for tropical countries, we can see
#different shades of green near India and pasrts of SE Asia