1 read CN data from nc

# load required libraries
library(RNetCDF)
library(caTools)

in_directory<-"../Soil_BNU/CN/nc/"
files<-dir(in_directory,pattern=".nc",full.names = T)

# by default reading ALL the data
f_read_nc<-function(i,nc){
  
  name<-var.inq.nc(nc,i)$name
  print(name)
  a<-var.get.nc(nc,i)
  write.ENVI(a,paste(in_directory,"ENVI/",name,sep=""))
  }

# convert data to ENVI
for (i in c(1:length(files))){
  nc <- open.nc(files[i])
  print.nc(nc)
  dims<-file.inq.nc(nc) 
  lapply(c(3:(dims$nvars-1)),f_read_nc,nc=nc)
}
## dimensions:
##         depth = 8 ;
##         lat = 4320 ;
##         lon = 7560 ;
## variables:
##         float lon(lon) ;
##                 lon:long_name = "longitude" ;
##                 lon:units = "degrees_east" ;
##         float lat(lat) ;
##                 lat:long_name = "latitude" ;
##                 lat:units = "degrees_north" ;
##         float depth(depth) ;
##                 depth:long_name = "depth to the bottom of a soil layer" ;
##                 depth:units = "centimeter" ;
##         float BD(lon, lat, depth) ;
##                 BD:missing_value = -999 ;
##                 BD:units = "g/cm3" ;
##                 BD:long_name = "Bulk Density" ;
##         int RBD(lon, lat, depth) ;
##                 RBD:missing_value = -999 ;
##                 RBD:units = "" ;
##                 RBD:long_name = "quality control information of  BD" ;
## 
## // global attributes:
##                 :Conventions = "CF-1.0" ;
## [1] "BD"
## [1] "RBD"
## dimensions:
##         depth = 8 ;
##         lat = 4320 ;
##         lon = 7560 ;
## variables:
##         float lon(lon) ;
##                 lon:long_name = "longitude" ;
##                 lon:units = "degrees_east" ;
##         float lat(lat) ;
##                 lat:long_name = "latitude" ;
##                 lat:units = "degrees_north" ;
##         float depth(depth) ;
##                 depth:long_name = "depth to the bottom of a soil layer" ;
##                 depth:units = "centimeter" ;
##         float CL(lon, lat, depth) ;
##                 CL:missing_value = -999 ;
##                 CL:units = "% of weight" ;
##                 CL:long_name = "clay content" ;
## 
## // global attributes:
##                 :Conventions = "CF-1.0" ;
## [1] "CL"
## dimensions:
##         lat = 4320 ;
##         lon = 7560 ;
## variables:
##         float lon(lon) ;
##                 lon:long_name = "longitude" ;
##                 lon:units = "degrees_east" ;
##         float lat(lat) ;
##                 lat:long_name = "latitude" ;
##                 lat:units = "degrees_north" ;
##         float LDEP1(lon, lat) ;
##                 LDEP1:missing_value = -999 ;
##                 LDEP1:units = "Centimeter" ;
##                 LDEP1:long_name = "Average depth of K horizon" ;
##         float LDEP2(lon, lat) ;
##                 LDEP2:missing_value = -999 ;
##                 LDEP2:units = "Centimeter" ;
##                 LDEP2:long_name = "Average depth of O horizon" ;
##         float LDEP3(lon, lat) ;
##                 LDEP3:missing_value = -999 ;
##                 LDEP3:units = "Centimeter" ;
##                 LDEP3:long_name = "Average depth of H horizon" ;
##         float LDEP4(lon, lat) ;
##                 LDEP4:missing_value = -999 ;
##                 LDEP4:units = "Centimeter" ;
##                 LDEP4:long_name = "Average depth of A horizon" ;
##         float LDEP5(lon, lat) ;
##                 LDEP5:missing_value = -999 ;
##                 LDEP5:units = "Centimeter" ;
##                 LDEP5:long_name = "Average depth of E horizon" ;
##         float LDEP6(lon, lat) ;
##                 LDEP6:missing_value = -999 ;
##                 LDEP6:units = "Centimeter" ;
##                 LDEP6:long_name = "Average depth of AB horizon" ;
##         float LDEP7(lon, lat) ;
##                 LDEP7:missing_value = -999 ;
##                 LDEP7:units = "Centimeter" ;
##                 LDEP7:long_name = "Average depth of AC horizon" ;
##         float LDEP8(lon, lat) ;
##                 LDEP8:missing_value = -999 ;
##                 LDEP8:units = "Centimeter" ;
##                 LDEP8:long_name = "Average depth of B horizon" ;
##         float LDEP9(lon, lat) ;
##                 LDEP9:missing_value = -999 ;
##                 LDEP9:units = "Centimeter" ;
##                 LDEP9:long_name = "Average depth of BC horizon" ;
##         float LDEP10(lon, lat) ;
##                 LDEP10:missing_value = -999 ;
##                 LDEP10:units = "Centimeter" ;
##                 LDEP10:long_name = "Average depth of C horizon" ;
##         float LDEP11(lon, lat) ;
##                 LDEP11:missing_value = -999 ;
##                 LDEP11:units = "Centimeter" ;
##                 LDEP11:long_name = "Average depth of R horizon" ;
## 
## // global attributes:
##                 :Conventions = "CF-1.0" ;
## [1] "LDEP2"
## [1] "LDEP3"
## [1] "LDEP4"
## [1] "LDEP5"
## [1] "LDEP6"
## [1] "LDEP7"
## [1] "LDEP8"
## [1] "LDEP9"
## [1] "LDEP10"
## [1] "LDEP11"
## dimensions:
##         lat = 4320 ;
##         lon = 7560 ;
## variables:
##         float lon(lon) ;
##                 lon:long_name = "longitude" ;
##                 lon:units = "degrees_east" ;
##         float lat(lat) ;
##                 lat:long_name = "latitude" ;
##                 lat:units = "degrees_north" ;
##         float PDEP1(lon, lat) ;
##                 PDEP1:missing_value = -999 ;
##                 PDEP1:units = "Centimeter" ;
##                 PDEP1:long_name = "Soil profile depth estimated from all profiles of a type" ;
##         int RPDEP1(lon, lat) ;
##                 RPDEP1:missing_value = -999 ;
##                 RPDEP1:units = "Centimeter" ;
##                 RPDEP1:long_name = "Quality contorl infromation of PDEP1" ;
##         int PNUM1(lon, lat) ;
##                 PNUM1:missing_value = -999 ;
##                 PNUM1:units = "Centimeter" ;
##                 PNUM1:long_name = "Number of profiles to estimate PDEP1" ;
##         float PDEP2(lon, lat) ;
##                 PDEP2:missing_value = -999 ;
##                 PDEP2:units = "Centimeter" ;
##                 PDEP2:long_name = "Soil profile depth estimated from all profiles of a type" ;
##         int RPDEP2(lon, lat) ;
##                 RPDEP2:missing_value = -999 ;
##                 RPDEP2:units = "Centimeter" ;
##                 RPDEP2:long_name = "Quality contorl infromation of PDEP2" ;
##         int PNUM2(lon, lat) ;
##                 PNUM2:missing_value = -999 ;
##                 PNUM2:units = "Centimeter" ;
##                 PNUM2:long_name = "Number of profiles to estimate PDEP2" ;
## 
## // global attributes:
##                 :Conventions = "CF-1.0" ;
## [1] "RPDEP1"
## [1] "PNUM1"
## [1] "PDEP2"
## [1] "RPDEP2"
## [1] "PNUM2"
## dimensions:
##         depth = 8 ;
##         lat = 4320 ;
##         lon = 7560 ;
## variables:
##         float lon(lon) ;
##                 lon:long_name = "longitude" ;
##                 lon:units = "degrees_east" ;
##         float lat(lat) ;
##                 lat:long_name = "latitude" ;
##                 lat:units = "degrees_north" ;
##         float depth(depth) ;
##                 depth:long_name = "depth to the bottom of a soil layer" ;
##                 depth:units = "centimeter" ;
##         float POR(lon, lat, depth) ;
##                 POR:missing_value = -999 ;
##                 POR:units = "cm3/100cm3" ;
##                 POR:long_name = "Porosity" ;
##         int RPOR(lon, lat, depth) ;
##                 RPOR:missing_value = -999 ;
##                 RPOR:units = "" ;
##                 RPOR:long_name = "quality control information of  POR" ;
## 
## // global attributes:
##                 :Conventions = "CF-1.0" ;
## [1] "POR"
## [1] "RPOR"
## dimensions:
##         depth = 8 ;
##         lat = 4320 ;
##         lon = 7560 ;
## variables:
##         float lon(lon) ;
##                 lon:long_name = "longitude" ;
##                 lon:units = "degrees_east" ;
##         float lat(lat) ;
##                 lat:long_name = "latitude" ;
##                 lat:units = "degrees_north" ;
##         float depth(depth) ;
##                 depth:long_name = "depth to the bottom of a soil layer" ;
##                 depth:units = "centimeter" ;
##         float SA(lon, lat, depth) ;
##                 SA:missing_value = -999 ;
##                 SA:units = "% of weight" ;
##                 SA:long_name = "sand content" ;
##         int RSA(lon, lat, depth) ;
##                 RSA:missing_value = -999 ;
##                 RSA:units = "" ;
##                 RSA:long_name = "quality control information of SA" ;
## 
## // global attributes:
##                 :Conventions = "CF-1.0" ;
## [1] "SA"
## [1] "RSA"
## dimensions:
##         depth = 8 ;
##         lat = 4320 ;
##         lon = 7560 ;
## variables:
##         float lon(lon) ;
##                 lon:long_name = "longitude" ;
##                 lon:units = "degrees_east" ;
##         float lat(lat) ;
##                 lat:long_name = "latitude" ;
##                 lat:units = "degrees_north" ;
##         float depth(depth) ;
##                 depth:long_name = "depth to the bottom of a soil layer" ;
##                 depth:units = "centimeter" ;
##         float SI(lon, lat, depth) ;
##                 SI:missing_value = -999 ;
##                 SI:units = "% of weight" ;
##                 SI:long_name = "silt content" ;
## 
## // global attributes:
##                 :Conventions = "CF-1.0" ;
## [1] "SI"