-
Notifications
You must be signed in to change notification settings - Fork 2
/
Argosingle.R
95 lines (59 loc) · 3.43 KB
/
Argosingle.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#########################################################
# Extract data from BioArgo to dataframe
#".nc" files are selected to process
#########################################################
ExtractBioArgo<- function(bioarg) {
library(RNetCDF)
#Read .nc files
argo<- read.nc(open.nc(bioarg))
if(is.null(argo$CHLA)){stop("Chlorophyll missing:-Not a BioArgo")}
ifelse(test = sum(is.na(argo$CHLA[,5]>0)),yes = chl<- argo$CHLA[,4],no = chl<- argo$CHLA[,5] )
oxy<- c(na.omit(argo$DOXY[,4]),na.omit(argo$DOXY[,3]))
psal<- c(na.omit(argo$PSAL[,2]),na.omit(argo$PSAL[,1]))
ifelse(test = !is.null(argo$TEMP),
yes =temp<- c(na.omit(argo$TEMP[,2]),na.omit(argo$TEMP[,1])),
no = temp<- c(na.omit(argo$TEMP_DOXY[,4]),na.omit(argo$TEMP_DOXY[,3])) )
Most_freq <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
# if salinity not available
#================================================================================================================
if (is.null(psal)){
warning("salinity missing")
para<- list(chl,oxy,temp,pres)
#find least length
trimfac<- min(as.numeric(lapply(para, function(x)length(x))))
date<- rep(as.Date(as.numeric(unique(argo$JULD)),origin="1950-01-01"),trimfac)
lat<- rep(as.numeric(Most_freq(argo$LATITUDE)),trimfac)
lon<- rep(as.numeric(Most_freq(argo$LONGITUDE)),trimfac)
#Trim everthing with trimfac for making a dataframe
assign(paste("Bioargo",unique(argo$CYCLE_NUMBER),unique(argo$PLATFORM_NUMBER),sep = "-"),
data.frame(Date=date[1:trimfac],latitude=lat[1:trimfac],longitude=lon[1:trimfac],cycle.no=cycle.no[1:trimfac],pressure=pres[1:trimfac],
temperature=temp[1:trimfac],
oxygen=oxy[1:trimfac],chlorophyll=chl[1:trimfac]),envir = .GlobalEnv)
data0<- data.frame(Date=date[1:trimfac],latitude=lat[1:trimfac],longitude=lon[1:trimfac],cycle.no=cycle.no[1:trimfac],pressure=pres[1:trimfac],
temperature=temp[1:trimfac],oxygen=oxy[1:trimfac],chlorophyll=chl[1:trimfac])
return(data0)
}
else
message("Horray!!, All paramters available")
{ para<- list(chl,psal,oxy,temp,pres)
#find least length
trimfac<- min(as.numeric(lapply(para, function(x)length(x))))
date<- rep(as.Date(as.numeric(unique(argo$JULD)),origin="1950-01-01"),trimfac)
lat<- rep(as.numeric(Most_freq(argo$LATITUDE)),trimfac)
lon<- rep(as.numeric(Most_freq(argo$LONGITUDE)),trimfac)
cycle.no<- rep(as.numeric(unique(argo$CYCLE_NUMBER)),trimfac)
#Trim everthing with trimfac for making a dataframe
assign(paste("Bioargo",unique(argo$CYCLE_NUMBER),unique(argo$PLATFORM_NUMBER),sep = "-"),
data.frame(Date=date[1:trimfac],latitude=lat[1:trimfac],longitude=lon[1:trimfac],cycle.no=cycle.no[1:trimfac],pressure=pres[1:trimfac],
temperature=temp[1:trimfac],salinity=psal[1:trimfac],
oxygen=oxy[1:trimfac],chlorophyll=chl[1:trimfac]),envir = .GlobalEnv)
#Data for listing
data1<- data.frame(Date=date[1:trimfac],latitude=lat[1:trimfac],longitude=lon[1:trimfac],cycle.no=cycle.no[1:trimfac],pressure=pres[1:trimfac],
temperature=temp[1:trimfac],salinity=psal[1:trimfac],
oxygen=oxy[1:trimfac],chlorophyll=chl[1:trimfac])
return(data1)
}
}