-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod13_hdf.cpp
More file actions
158 lines (123 loc) · 4.33 KB
/
mod13_hdf.cpp
File metadata and controls
158 lines (123 loc) · 4.33 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
#include "mod13_hdf.h"
MOD13_hdf::MOD13_hdf()
{
// dataset 4 is nighttime LST
// dataset 0 is daytime LST
startl = 700 ;
starts = 1080 ;
pixspace = 180. / 3600. ;
startlat = 90.f - (startl * pixspace) ;
startlon = starts * pixspace ;
ns = 1320 ;
nl = 700 ;
ndvidata = new uint16 [ns * nl];
stackf = 0L ;
//stack = 0L ;
}
MOD13_hdf::~MOD13_hdf (){
if (ndvidata)
delete [] ndvidata ;
if (stackf)
delete [] stackf ;
}
void MOD13_hdf::openHDF (char *infile){
// for monthly LST MOD13 Product
// datasets
// 0 - LST_Day_CMG
// 5 - LST_Night_CMG
char name[240], attrb_name[240] ;
int32 n_datasets, n_fileattrs, sds_id, rank, icount, dim_sizes[3],num_type, attributes,count ;
int32 start[2], stride[2], edge[2] ;
int16 fillval, valrange[2] ;
double scaleval ;
start[0] = startl ;
start[1] = starts ;
edge[0] = nl ;
edge[1] = ns ;
stride[0] = 1 ;
stride[1] = 1 ;
QFile *qf = new QFile (infile) ;
if (!qf->exists()){
qDebug() << ("Uhoh") ;
}
int32 sd_id = SDstart (infile, DFACC_RDONLY) ;
if (sd_id<=0) {
qDebug()<< "Uhoh again";
}
SDfileinfo(sd_id, &n_datasets, &n_fileattrs);
qDebug () <<"datasets "<< n_datasets<< " file attrs "<< n_fileattrs ;
for (int i=0; i<n_datasets; i++){
sds_id = SDselect(sd_id, i);
SDgetinfo (sds_id, name, &rank, dim_sizes, &num_type, &attributes ) ;
qDebug() <<i << " " << name ;
if (i==0){
qDebug ()<<"Number of attributes : " << attributes ;
for (int is = 0; is< attributes ; is++) {
SDattrinfo (sds_id, is, name, &num_type, &icount) ;
qDebug () << name << " " << num_type << " " << icount ;
}
int attnum = SDfindattr (sds_id, "scale_factor") ;
SDreadattr (sds_id, attnum, &scaleval);
attnum = SDfindattr (sds_id, "_FillValue") ;
SDreadattr (sds_id, attnum, &fillval);
attnum = SDfindattr (sds_id, "valid_range") ;
SDreadattr (sds_id, attnum, &valrange[0]);
qDebug () << scaleval << " " << fillval << " " << valrange[0]<< " "<< valrange[1] ;
qDebug()<< dim_sizes[0] << " " << dim_sizes[1] << " " << rank ;
SDreaddata(sds_id, start, stride, edge, ndvidata) ;
}
for (int j=0; j<attributes; j++){
SDattrinfo (sds_id, j, name, &num_type, &count) ;
//qDebug () << j << " " << name<< " " << count ;
}
}
qDebug() << "ndvi val is " << ndvidata [(nl/2)*ns+(ns/2)] ;
SDend (sd_id) ;
}
int MOD13_hdf::getStack (char *infile) {
// The stack file is our flat file containing a band for each of the 20+ years, stacked as unsigned short arrays ns * nl * nyears
// No scaling was applied therefore to get degrees K needs to be multiplied by .02
uint16 *stack ;
long nbytes ;
int nyears ;
// this is the scaled LST data, clean out
if (stackf) {
delete [] stackf ;
}
string directory, stackfile;
string filename (infile) ;
const size_t last_slash_idx = filename.rfind('/');
if (std::string::npos != last_slash_idx)
{
directory = filename.substr(0, last_slash_idx);
}
qDebug () <<"Filename directory is "<< directory.c_str() ;
stackfile = directory + string("/outarr_day") ;
QFile qf (stackfile.c_str()) ;
qf.open (QIODevice::ReadOnly) ;
nbytes = qf.size() ;
// allocate memory for the stack
stack = new uint16 [nbytes/2] ;
stackf = new float [nbytes/2] ;
nyears = int(nbytes / (ns * nl * 2)) ;
qDebug () << "number of years "<< nyears ;
qf.read ((char *)stack, nbytes) ;
qf.close() ;
for (long i=0; i<nbytes/2; i++){
stackf[i] = stack[i] / 10000. ;
}
this->getYearMonth(filename);
qDebug() << "this year is " << stackf[long(ns * nl * 20)+350 * ns + 660] ;
delete [] stack ;
return (nyears) ;
}
void MOD13_hdf::getYearMonth (string fname){
size_t pos = fname.find ("A20") ;
string yearstring = fname.substr (pos+1, 4) ;
year = stoi (yearstring) ;
string daystring = fname.substr (pos+5, 3) ;
month = (stoi (daystring)+5) / 30 ;
qDebug() << "file date : " << year << " " << month ;
pos = fname.find ("MOD13C") ;
curfile = fname.substr (pos) ;
}