gctl_toolkits/archive/sph2random/sph2random.h
2024-09-10 20:25:18 +08:00

210 lines
5.7 KiB
C++

#ifndef _SPHINTERPOLATE_H
#define _SPHINTERPOLATE_H
#include "sysDefine.h"
class sphInterpolate
{
public:
sphInterpolate(){}
~sphInterpolate(){}
int readGrid(char*,char*,char*);
int readRandom(char*,char*);
int outRes();
void interpolate(char*);
private:
spointArray gridData;
spointArray randomData;
double lonmin,lonmax,latmin,latmax;
double dlon,dlat;
int lonNum,latNum;
};
int sphInterpolate::readGrid(char* filepara,char* intervals,char* use_spherical_coor)
{
char filename[1024];
int orders[3] = {0,1,2}; //默认的读入的数据列为前三列
//解析文件名中是否含有+d标示 如果有则将+d以前解释为filename 之后为需要读入的数据列 默认为逗号分隔
//否则将filepara赋值为filename
if (4 != sscanf(filepara,"%[^\+]+d%d,%d,%d",filename,&orders[0],&orders[1],&orders[2]))
strcpy(filename,filepara);
ifstream infile;
if (open_infile(infile,filename)) return -1;
//解析网格数据间隔
if (2!=sscanf(intervals,"%lf/%lf",&dlon,&dlat))
{
cerr << BOLDRED << "error ==> " << RESET << "wrong grid data spacing: " << intervals << endl;
return -1;
}
lonmin = latmin = MAX_DBL;
lonmax = latmax = MIN_BDL;
string temp_str;
stringstream temp_ss;
double temp_d;
_1dArray temp_row;
spoint temp_vert;
while(getline(infile,temp_str))
{
if (*(temp_str.begin()) == '#') continue;
temp_ss.str(""); temp_ss.clear(); temp_ss << temp_str;
if(!temp_row.empty()) temp_row.clear();
while(temp_ss >> temp_d)
temp_row.push_back(temp_d);
temp_vert.lon = temp_row[orders[0]];
if (!strcmp(use_spherical_coor,"yes")){
if (temp_vert.lon < 0){
temp_vert.lon += 360.0;
}
}
temp_vert.lat = temp_row[orders[1]];
temp_vert.rad = defaultR;
temp_vert.val = temp_row[orders[2]];
gridData.push_back(temp_vert);
if (temp_vert.lon < lonmin) lonmin = temp_vert.lon;
if (temp_vert.lon > lonmax) lonmax = temp_vert.lon;
if (temp_vert.lat < latmin) latmin = temp_vert.lat;
if (temp_vert.lat > latmax) latmax = temp_vert.lat;
}
infile.close();
lonNum = round((lonmax-lonmin)/dlon+1);
latNum = round((latmax-latmin)/dlat+1);
return 0;
}
int sphInterpolate::readRandom(char* filepara,char* use_spherical_coor)
{
char filename[1024];
int orders[3] = {0,1,-1}; //默认的读入的数据列为前三列
//解析文件名中是否含有+d标示 如果有则将+d以前解释为filename 之后为需要读入的数据列 默认为逗号分隔
//否则将filepara赋值为filename
if (4 != sscanf(filepara,"%[^\+]+d%d,%d,%d",filename,&orders[0],&orders[1],&orders[2]))
{
if (3 != sscanf(filepara,"%[^\+]+d%d,%d",filename,&orders[0],&orders[1]))
strcpy(filename,filepara);
}
ifstream infile;
if (open_infile(infile,filename)) return -1;
string temp_str;
stringstream temp_ss;
double temp_d;
_1dArray temp_row;
spoint temp_vert;
while(getline(infile,temp_str))
{
if (*(temp_str.begin()) == '#') continue;
temp_ss.str(""); temp_ss.clear(); temp_ss << temp_str;
if(!temp_row.empty()) temp_row.clear();
while(temp_ss >> temp_d)
temp_row.push_back(temp_d);
temp_vert.lon = temp_row[orders[0]];
if (!strcmp(use_spherical_coor,"yes")){
if (temp_vert.lon < 0){
temp_vert.lon += 360.0;
temp_vert.convert = 1;
}
}
temp_vert.lat = temp_row[orders[1]];
if (orders[2] == -1)
temp_vert.rad = 0.0;
else
temp_vert.rad = temp_row[orders[2]];
randomData.push_back(temp_vert);
temp_vert.convert = 0;
}
infile.close();
return 0;
}
int sphInterpolate::outRes()
{
for (int i = 0; i < randomData.size(); i++)
{
if (randomData[i].convert && randomData[i].val != MAX_DBL){
cout << setprecision(12) << randomData[i].lon - 360.0 << " " << randomData[i].lat << " " << randomData[i].rad << " " << randomData[i].val << endl;
}
else if (randomData[i].val != MAX_DBL){
cout << setprecision(12) << randomData[i].lon << " " << randomData[i].lat << " " << randomData[i].rad << " " << randomData[i].val << endl;
}
}
return 0;
}
void sphInterpolate::interpolate(char* startCorner)
{
int tempM,tempN;
double temp_val,lon1,lon2,lat1,lat2;
if (!strcmp(startCorner,"leftTopo")) //数据从左上角开始的情况
{
for (int i = 0; i < randomData.size(); i++)
{
tempM = floor((latmax-randomData[i].lat)/dlat);
tempN = floor((randomData[i].lon-lonmin)/dlon);
if (tempM == (latNum-1)) tempM -= 1;
if (tempN == (lonNum-1)) tempN -= 1;
if (tempM >= 0 && tempN >= 0 && tempM <= latNum-2 && tempN <= lonNum-2)
{
lon1 = lonmin+tempN*dlon;
lon2 = lonmin+(tempN+1)*dlon;
lat1 = latmax-(tempM+1)*dlat;
lat2 = latmax-tempM*dlat;
temp_val = SphBiInterp_deg(90-lat1,90-lat2,lon1,lon2,
90-randomData[i].lat,randomData[i].lon,
gridData[(tempM+1)*lonNum+tempN].val,
gridData[(tempM+1)*lonNum+tempN+1].val,
gridData[tempM*lonNum+tempN].val,
gridData[tempM*lonNum+tempN+1].val);
randomData[i].val = temp_val;
}
}
}
else if (!strcmp(startCorner,"leftDown")) //数据从左下角开始的情况
{
for (int i = 0; i < randomData.size(); i++)
{
tempM = floor((randomData[i].lat-latmin)/dlat);
tempN = floor((randomData[i].lon-lonmin)/dlon);
if (tempM == (latNum-1)) tempM -= 1;
if (tempN == (lonNum-1)) tempN -= 1;
if (tempM >= 0 && tempN >= 0 && tempM <= latNum-2 && tempN <= lonNum-2)
{
lon1 = lonmin+tempN*dlon;
lon2 = lonmin+(tempN+1)*dlon;
lat1 = latmin+tempM*dlat;
lat2 = latmin+(tempM+1)*dlat;
temp_val = SphBiInterp_deg(90-lat1,90-lat2,lon1,lon2,
90-randomData[i].lat,randomData[i].lon,
gridData[tempM*lonNum+tempN].val,
gridData[tempM*lonNum+tempN+1].val,
gridData[(tempM+1)*lonNum+tempN].val,
gridData[(tempM+1)*lonNum+tempN+1].val);
randomData[i].val = temp_val;
}
}
}
return;
}
#endif