-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathSampleNetCDF2.cpp
More file actions
188 lines (147 loc) · 4.77 KB
/
Copy pathSampleNetCDF2.cpp
File metadata and controls
188 lines (147 loc) · 4.77 KB
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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright (c) 2004-2006 by Gerard Gorman
* Copyright (c) 2006- Imperial College London
* See COPYING file for copying and redistribution conditions.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Contact info: gerard.j.gorman@gmail.com/g.gorman@imperial.ac.uk
*/
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
#ifdef USING_VTK
#include <vtkImageData.h>
#include <vtkShortArray.h>
#include <vtkXMLImageDataWriter.h>
#include <vtkPointData.h>
#include <vtkZLibDataCompressor.h>
#include <vtkBMPWriter.h>
#endif
#include "SampleNetCDF2.h"
using namespace std;
SampleNetCDF2::SampleNetCDF2(){
VerboseOff();
is_constant = true;
}
SampleNetCDF2::~SampleNetCDF2(){}
SampleNetCDF2::SampleNetCDF2(string filename){
VerboseOff();
SetFile(filename);
return;
}
bool SampleNetCDF2::HasPoint(double longitude, double latitude) const{
if(verbose)
cout<<"double SampleNetCDF2::HasValue("<<longitude<<", "<<latitude<<") const\n";
if(longitude<x_range[0])
return false;
if(longitude>x_range[1])
return false;
if(latitude<y_range[0])
return false;
if(latitude>y_range[1])
return false;
return true;
}
double SampleNetCDF2::GetValue(int ix, int iy) const{
if(verbose)
cout<<"double SampleNetCDF2::GetValue("<<ix<<", "<<iy<<") const";
assert(!is_constant);
//assert(ix>=0);
ix = std::max(ix, 0);
//assert(ix<dimension[0]);
ix = std::min(ix, dimension[0]-1);
//assert(iy>=0);
iy = std::max(iy, 0);
//assert(iy<dimension[1]);
iy = std::min(iy, dimension[1]-1);
if(verbose)
cout<<" = "<<data[iy*dimension[0]+ix]<<endl;
return data[iy*dimension[0]+ix];
}
double SampleNetCDF2::GetValue(double longitude, double latitude) const{
if(verbose)
cout<<"double SampleNetCDF2::GetValue("<<longitude<<", "<<latitude<<") const\n";
if(is_constant)
return 1.0;
longitude = std::min(std::max(x_range[0], longitude), x_range[1]);
latitude = std::min(std::max(y_range[0], latitude), y_range[1]);
if(verbose)
cout<<"Shifted coordinate = "<<longitude<<" "<<latitude<<endl;
int i0 = (int)floor((longitude - x_range[0])/spacing[0]);
int i1 = (int)ceil((longitude - x_range[0])/spacing[0]);
int j0 = (int)floor((latitude - y_range[0])/spacing[1]);
int j1 = (int)ceil((latitude - y_range[0])/spacing[1]);
double x0 = x_range[0] + i0*spacing[0];
double x1 = x_range[0] + i1*spacing[0];
double y0 = y_range[0] + j0*spacing[1];
double y1 = y_range[0] + j1*spacing[1];
if(verbose)
cout<<"longitude "<<x0<<" < "<<longitude<<" < "<<x1<<endl
<<"latitude "<<y0<<" < "<<latitude <<" < "<<y1<<endl
<<"spacing "<<spacing[0]<<", "<<spacing[1]<<endl
<<"x range "<<x_range[0]<<", "<<x_range[1]<<endl
<<"y range "<<y_range[0]<<", "<<y_range[1]<<endl;
double z00 = GetValue(i0, j0);
double z01 = GetValue(i0, j1);
double z10 = GetValue(i1, j0);
double z11 = GetValue(i1, j1);
double val;
if(i0==i1){ // No interpolation along longitude
if(verbose)
cout<<"Case 1\n";
if(j0==j1){
val = z00;
}else{
val = z00 + (latitude-y0)*(z01-z00)/(y1-y0);
}
if(verbose)
cout<<"z00, z, z01 = "<<z00<<", "<<val<<", "<<z01<<endl;
}else if(j0==j1){ // No interpolation along latitude
if(verbose)
cout<<"Case 2\n";
val = z00 + (longitude-x0)*(z10-z00)/(x1-x0);
if(verbose)
cout<<"z00, z, z10 = "<<z00<<", "<<val<<", "<<z10<<endl;
}else{ // Bi-linear interpolation
if(verbose)
cout<<"Case 3"<<endl;
double dx = x1-x0;
double dy = y1-y0;
val = (z00*(x1-longitude)*(y1-latitude) +
z10*(longitude-x0)*(y1-latitude) +
z01*(x1-longitude)*(latitude-y0) +
z11*(longitude-x0)*(latitude-y0))/(dx*dy);
if(verbose)
cout<<"z00, z10, z01, z11, val = "
<<z00<<", "<<z10<<", "<<z01<<", "<<z11<<", "<<val<<endl;
}
return val;
}
void SampleNetCDF2::SetFile(string filename){
if(filename.empty()){
is_constant=true;
return;
}
NetCDF_reader reader(filename.c_str(), verbose);
reader.GetXRange(x_range[0], x_range[1]);
reader.GetYRange(y_range[0], y_range[1]);
reader.GetSpacing(spacing[0], spacing[1]);
reader.GetDimension(dimension[0], dimension[1]);
reader.Read(data);
is_constant = false;
}
void SampleNetCDF2::VerboseOff(){
verbose=false;
}
void SampleNetCDF2::VerboseOn(){
verbose=true;
}