salt_temp.R 6.89 KB
Newer Older
peguerin's avatar
peguerin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
###############################################################################
## Librairies
library("raster")
library("rgdal")
library("ncdf4")

###############################################################################
## Functions

extract_variable <- function(ma_variable, repro_period, depth_range ){
	sa=ma_variable[,repro_period,depth_range]
	sa_max=apply(sa,1, function(x) max(x,na.rm=T))
	sa_mean=apply(sa,1, function(x) mean(x,na.rm=T))
	sa_min=apply(sa,1, function(x) min(x,na.rm=T))
	return(data.frame(max=sa_max,mean=sa_mean,min=sa_min))
}



###############################################################################
## Load data

peguerin's avatar
peguerin committed
23
fichier="01-infos/salinity_et_temperature/CMEMS-MedSea_Reanalysis_Phys_006_004_0to210m_1987-2015_1m_votemper_vosaline_SampleLoc.nc"
peguerin's avatar
peguerin committed
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

samplecsv=read.csv("01-sample_coords/samples_labels.csv",sep="\t",header=F)
labels=as.character(samplecsv[,1])

br <- nc_open(fichier)
## to know the value of each "level" depth
brdepth = ncvar_get(br, "depth")

temperatures=ncvar_get(br,"GenTemp")
salinities=ncvar_get(br,"GenSalt")

january=seq(1,(348-11),12)
february=seq(2,(348-10),12)
march=seq(3,(348-9),12)
april=seq(4,(348-8),12)
may=seq(5,(348-7),12)
june=seq(6,(348-6),12)
july=seq(7,(348-5),12)
august=seq(8,(348-4),12)
september=seq(9,(348-3),12)
october=seq(10,(348-2),12)
november=seq(11,348-1,12)
december=seq(12,348,12)

###############################################################################
## extraction


## surface whole year
repro_period=cbind(january,february,march,april,may,june,july,august,september,october,november,december)
depth_range=which(brdepth <2)
temp_surface_wyear=extract_variable(temperatures,repro_period,depth_range)
sal_surface_wyear=extract_variable(salinities,repro_period,depth_range)

## depth range 0-200 whole year
repro_period=cbind(january,february,march,april,may,june,july,august,september,october,november,december)
depth_range=which(brdepth <300)
temp_0_200_wyear=extract_variable(temperatures,repro_period,depth_range)
sal_0_200_wyear=extract_variable(salinities,repro_period,depth_range)

## depth range 5-50 whole year
repro_period=cbind(january,february,march,april,may,june,july,august,september,october,november,december)
depth_range=which(brdepth < 51 & brdepth > 4)
temp_5_50_wyear=extract_variable(temperatures,repro_period,depth_range)
sal_5_50_wyear=extract_variable(salinities,repro_period,depth_range)

## depth range 20-90 whole year
repro_period=cbind(january,february,march,april,may,june,july,august,september,october,november,december)
depth_range=which(brdepth < 91 & brdepth > 19)
temp_20_90_wyear=extract_variable(temperatures,repro_period,depth_range)
sal_20_90_wyear=extract_variable(salinities,repro_period,depth_range)

## surface march july
repro_period=cbind(march,april,may,june,july)
depth_range=which(brdepth <2)
temp_surface_march_july=extract_variable(temperatures,repro_period,depth_range)
sal_surface_march_july=extract_variable(salinities,repro_period,depth_range)

## surface march june
repro_period=cbind(march,april,may,june)
depth_range=which(brdepth <2)
temp_surface_march_june=extract_variable(temperatures,repro_period,depth_range)
sal_surface_march_june=extract_variable(salinities,repro_period,depth_range)

## surface june november
repro_period=cbind(june,july,august,september,october,november)
depth_range=which(brdepth <2)
temp_surface_june_nov=extract_variable(temperatures,repro_period,depth_range)
sal_surface_june_nov=extract_variable(salinities,repro_period,depth_range)

## 0-10 march july
repro_period=cbind(march,april,may,june,july)
depth_range=which(brdepth < 11)
temp_0_10_march_july=extract_variable(temperatures,repro_period,depth_range)
sal_0_10_march_july=extract_variable(salinities,repro_period,depth_range)

## 0-10 march june
repro_period=cbind(march,april,may,june)
depth_range=which(brdepth < 11)
temp_0_10_march_june=extract_variable(temperatures,repro_period,depth_range)
sal_0_10_march_june=extract_variable(salinities,repro_period,depth_range)

## 60-110 june november
repro_period=cbind(june,july,august,september,october,november)
depth_range=which(brdepth < 111 & brdepth > 59)
temp_60_110_june_nov=extract_variable(temperatures,repro_period,depth_range)
sal_60_110_june_nov=extract_variable(salinities,repro_period,depth_range)


salt_temp=data.frame(labels=labels,
	salinity_surface_whole_year_max=sal_surface_wyear$max,
	salinity_surface_whole_year_mean=sal_surface_wyear$mean,
	salinity_surface_whole_year_min=sal_surface_wyear$min,
	salinity_0_200_whole_year_max=sal_0_200_wyear$max,
	salinity_0_200_whole_year_mean=sal_0_200_wyear$mean,
	salinity_0_200_whole_year_min=sal_0_200_wyear$min,
	salinity_5_50_whole_year_max=sal_5_50_wyear$max,
	salinity_5_50_whole_year_mean=sal_5_50_wyear$mean,
	salinity_5_50_whole_year_min=sal_5_50_wyear$min,
	salinity_20_90_whole_year_max=sal_20_90_wyear$max,
	salinity_20_90_whole_year_mean=sal_20_90_wyear$mean,
	salinity_20_90_whole_year_min=sal_20_90_wyear$min,
	salinity_surface_march_july_max=sal_surface_march_july$max,
	salinity_surface_march_july_mean=sal_surface_march_july$mean,
	salinity_surface_march_july_min=sal_surface_march_july$min,
	salinity_surface_march_june_max=sal_surface_march_june$max,
	salinity_surface_march_june_mean=sal_surface_march_june$mean,
	salinity_surface_march_june_min=sal_surface_march_june$min,
	salinity_surface_june_november_max=sal_surface_june_nov$max,
	salinity_surface_june_november_mean=sal_surface_june_nov$mean,
	salinity_surface_june_november_min=sal_surface_june_nov$min,
	salinity_0_10_march_july_max=sal_0_10_march_july$max,
	salinity_0_10_march_july_mean=sal_0_10_march_july$mean,
	salinity_0_10_march_july_min=sal_0_10_march_july$min,
	salinity_0_10_march_june_max=sal_0_10_march_june$max,
	salinity_0_10_march_june_mean=sal_0_10_march_june$mean,
	salinity_0_10_march_june_min=sal_0_10_march_june$min,
	salinity_60_110_june_november_max=sal_60_110_june_nov$max,
	salinity_60_110_june_november_mean=sal_60_110_june_nov$mean,
	salinity_60_110_june_november_min=sal_60_110_june_nov$min,

	temperature_surface_whole_year_max=temp_surface_wyear$max,
	temperature_surface_whole_year_mean=temp_surface_wyear$mean,
	temperature_surface_whole_year_min=temp_surface_wyear$min,
	temperature_surface_march_july_max=temp_surface_march_july$max,
	temperature_surface_march_july_mean=temp_surface_march_july$mean,
	temperature_surface_march_july_min=temp_surface_march_july$min,
	temperature_surface_march_june_max=temp_surface_march_june$max,
	temperature_surface_march_june_mean=temp_surface_march_june$mean,
	temperature_surface_march_june_min=temp_surface_march_june$min,
	temperature_surface_june_november_max=temp_surface_june_nov$max,
	temperature_surface_june_november_mean=temp_surface_june_nov$mean,
peguerin's avatar
peguerin committed
156
	temperature_surface_june_november_min=temp_surface_june_nov$min
peguerin's avatar
peguerin committed
157
158
159
160
	)

salt_temp[salt_temp==-Inf] <- NA
salt_temp[salt_temp==Inf] <- NA
peguerin's avatar
peguerin committed
161
salt_temp[salt_temp==NaN] <- NA
peguerin's avatar
peguerin committed
162
163

write.table(salt_temp, file="03-results/all_depth_repro_temperature_salinity.tsv",quote=F,sep="\t",col.names=T,row.names=F)