-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeaSurfaceTemperature.m
110 lines (61 loc) · 1.88 KB
/
SeaSurfaceTemperature.m
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
classdef SeaSurfaceTemperature
properties (SetAccess = private)
DataDir = fullfile(fileparts(which(mfilename)),'data')
DataURL = 'https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/v2.1/access/avhrr/YYYYMM/oisst-avhrr-v02r01.YYYYMMDD.nc'
end % private props
properties
Latitude (1,1) = 42
Longitude (1,1) = 290 % Boston harbour
Date (1,1) datetime = datetime('01-Jan-2019')
end % props
methods
function self = SeaSurfaceTemperature()
if exist(self.DataDir,'dir') == 0
mkdir(self.DataDir)
end
end % constructor
function data = fetch(self)
% figure out the day we need
[YYYYMM, YYYYMMDD] = SeaSurfaceTemperature.month2filename(self.Date);
URL = strrep(self.DataURL,'YYYYMMDD',YYYYMMDD);
URL = strrep(URL,'YYYYMM',YYYYMM);
savename = fullfile(self.DataDir, YYYYMMDD);
savename = [savename '.nc'];
if exist(savename,'file') ~= 2
disp('Downloading data from NOAA website...')
try
websave(savename,URL);
catch err
warning('Could not download data from NOAA website')
end
end
% read data from this file
lat = ncread(savename,'lat');
lon = ncread(savename,'lon');
sst = ncread(savename,'sst');
[ActualLat,latidx] = min(abs(self.Latitude - lat));
[ActualLon,lonidx] = min(abs(self.Longitude - lon));
data.Temperature = sst(lonidx,latidx);
data.Longitude = ActualLat;
data.Latitide = ActualLon;
data.Date = datetime(YYYYMMDD,'InputFormat','yyyyMMdd');
data.SST = sst;
end % fetch data
end % methods
methods (Static)
function [YYYYMM, YYYYMMDD] = month2filename(M)
download_this = datevec(M);
YYYY = mat2str(download_this(1));
MM = mat2str(download_this(2));
DD = mat2str(download_this(3));
if length(MM) == 1
MM = ['0' MM];
end
if length(DD) == 1
DD = ['0' DD];
end
YYYYMM = [YYYY MM];
YYYYMMDD = [YYYY MM DD];
end
end % static methods
end % classdef