initial upload
This commit is contained in:
245
archive/grid2xyz/surfer2dat/common.h
Normal file
245
archive/grid2xyz/surfer2dat/common.h
Normal file
@@ -0,0 +1,245 @@
|
||||
#ifndef _COMMOND_H
|
||||
#define _COMMOND_H
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define ELE "-e"
|
||||
#define DAT "-d"
|
||||
#define FIE "-f"
|
||||
#define RANGE "-r"
|
||||
#define OUTPUT "-o"
|
||||
#define MAX 1e+20
|
||||
#define GRD ".grd"
|
||||
using namespace std;
|
||||
|
||||
int typeget(char *str, const char* str2,string &str3)
|
||||
{
|
||||
char* strp = strstr(str, str2); // 从字符串str中查找str2,
|
||||
if (strp == NULL)
|
||||
{
|
||||
return 0; // 如果没有找到,返回
|
||||
}
|
||||
string temp = str;
|
||||
str3=temp.substr(strlen(str2));//提出除str2外的子句
|
||||
return 1;
|
||||
}
|
||||
|
||||
string& replace_all(string& str,const string& old_value,const string& new_value,int &num) //替换str中所有lod_value为new_value,返回被替换的lod_value的个数
|
||||
{
|
||||
int count = 0;
|
||||
for(string::size_type pos(0);pos!=string::npos;pos+=new_value.length()){
|
||||
if((pos=str.find(old_value,pos))!=string::npos){
|
||||
str.replace(pos,old_value.length(),new_value);
|
||||
count++;
|
||||
}
|
||||
else break;
|
||||
}
|
||||
num = count;
|
||||
return str;
|
||||
}
|
||||
|
||||
int nameget(char *str, const char* str2,string &str3)//提出尾端的子句
|
||||
{
|
||||
char* strp = strstr(str, str2); // 从字符串str中查找str2,
|
||||
if (strp == NULL)
|
||||
{
|
||||
return 0; // 如果没有找到,返回
|
||||
}
|
||||
string temp = str;
|
||||
str3=temp.substr(0,strlen(str)-strlen(str2));//提出除str2外的子句
|
||||
return 1;
|
||||
}
|
||||
|
||||
class CON
|
||||
{
|
||||
public:
|
||||
CON();
|
||||
~CON();
|
||||
int run(string,string,string,int,double,double*);
|
||||
int readdata(string,string);
|
||||
int fie(double);
|
||||
int fileout(string,int,double*);
|
||||
private:
|
||||
int M,N;
|
||||
double xmin,xmax,ymin,ymax,zmin,zmax;
|
||||
double dx,dy;
|
||||
double* data;
|
||||
double* relief;
|
||||
};
|
||||
|
||||
CON::CON()
|
||||
{
|
||||
data = NULL;
|
||||
relief = NULL;
|
||||
}
|
||||
|
||||
CON::~CON()
|
||||
{
|
||||
if(data!=NULL) delete[] data;
|
||||
if(relief!=NULL) delete[] relief;
|
||||
}
|
||||
|
||||
int CON::run(string inname,string inname2,string outname,int type,double topo,double* range)
|
||||
{
|
||||
if(readdata(inname,inname2)) return 1;
|
||||
if (type==1&&inname2=="") fie(topo);
|
||||
fileout(outname,type,range);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CON::readdata(string inname,string inname2)
|
||||
{
|
||||
string head;
|
||||
int M2,N2;
|
||||
double xmin2,xmax2,ymin2,ymax2,zmin2,zmax2;
|
||||
|
||||
const char* name1 = inname.c_str();
|
||||
ifstream grdin(name1);
|
||||
if (!grdin)
|
||||
{
|
||||
cout<<inname<<" not found, program stopped..."<<endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
grdin>>head>>N>>M>>xmin>>xmax>>ymin>>ymax>>zmin>>zmax;
|
||||
dx = (xmax-xmin)/M+1;
|
||||
dy = (ymax-ymin)/N+1;
|
||||
data = new double [M*N];
|
||||
for (int i = 0; i < M*N; i++)
|
||||
{
|
||||
grdin>>data[i];
|
||||
}
|
||||
grdin.close();
|
||||
}
|
||||
|
||||
if (inname2=="")
|
||||
{
|
||||
relief = new double [M*N];
|
||||
for (int i = 0; i < M*N; i++)
|
||||
{
|
||||
relief[i]=0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const char* name2 = inname2.c_str();
|
||||
ifstream grd2in(name2);
|
||||
if (!grd2in)
|
||||
{
|
||||
cout<<inname2<<" not found, program stopped..."<<endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
grd2in>>head>>N2>>M2>>xmin2>>xmax2>>ymin2>>ymax2>>zmin2>>zmax2;
|
||||
if (N!=N2||M!=M2)
|
||||
{
|
||||
cout<<"data martixs don't agree, program stopped..."<<endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
relief = new double [M*N];
|
||||
for (int i = 0; i < M*N; i++)
|
||||
{
|
||||
grd2in>>relief[i];
|
||||
}
|
||||
grd2in.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CON::fie(double topo)
|
||||
{
|
||||
for (int i = 0; i < M*N; i++)
|
||||
{
|
||||
relief[i]=topo;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CON::fileout(string outname,int type,double* range)
|
||||
{
|
||||
double xtemp,ytemp;
|
||||
if ((*range)!=MAX)
|
||||
{
|
||||
double xmin_r = *range;
|
||||
double xmax_r = *(range+1);
|
||||
double ymin_r = *(range+2);
|
||||
double ymax_r = *(range+3);
|
||||
if (type==1)
|
||||
{
|
||||
const char* name = (outname+".fie").c_str();
|
||||
ofstream outfile(name);
|
||||
int count=0;
|
||||
for (int i = 0; i < M*N; i++)
|
||||
{
|
||||
xtemp = (i%N)*dx+xmin;
|
||||
ytemp = (i/N)*dy+ymin;
|
||||
if (xtemp>=xmin_r&&xtemp<=xmax_r&&ytemp>=ymin_r&&ytemp<=ymax_r)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
outfile<<count<<endl;
|
||||
for (int i = 0; i < M*N; i++)
|
||||
{
|
||||
xtemp = (i%N)*dx+xmin;
|
||||
ytemp = (i/N)*dy+ymin;
|
||||
if (xtemp>=xmin_r&&xtemp<=xmax_r&&ytemp>=ymin_r&&ytemp<=ymax_r)
|
||||
{
|
||||
outfile<<setprecision(12)<<xtemp<<" "<<ytemp<<" "<<relief[i]<<" "<<data[i]<<endl;
|
||||
}
|
||||
}
|
||||
outfile.close();
|
||||
}
|
||||
else if (type==2)
|
||||
{
|
||||
const char* name = (outname+".dat").c_str();
|
||||
ofstream outfile(name);
|
||||
for (int i = 0; i < M*N; i++)
|
||||
{
|
||||
xtemp = (i%N)*dx+xmin;
|
||||
ytemp = (i/N)*dy+ymin;
|
||||
if (xtemp>=xmin_r&&xtemp<=xmax_r&&ytemp>=ymin_r&&ytemp<=ymax_r)
|
||||
{
|
||||
outfile<<setprecision(12)<<xtemp<<" "<<ytemp<<" "<<data[i]<<endl;
|
||||
}
|
||||
}
|
||||
outfile.close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (type==1)
|
||||
{
|
||||
const char* name = (outname+".fie").c_str();
|
||||
ofstream outfile(name);
|
||||
outfile<<M*N<<endl;
|
||||
for (int i = 0; i < M*N; i++)
|
||||
{
|
||||
outfile<<setprecision(12)<<(i%N)*dx+xmin<<" "<<(i/N)*dy+ymin<<" "<<relief[i]<<" "<<data[i]<<endl;
|
||||
}
|
||||
outfile.close();
|
||||
}
|
||||
else if (type==2)
|
||||
{
|
||||
const char* name = (outname+".dat").c_str();
|
||||
ofstream outfile(name);
|
||||
for (int i = 0; i < M*N; i++)
|
||||
{
|
||||
outfile<<setprecision(12)<<(i%N)*dx+xmin<<" "<<(i/N)*dy+ymin<<" "<<data[i]<<endl;
|
||||
}
|
||||
outfile.close();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
120
archive/grid2xyz/surfer2dat/main.cpp
Normal file
120
archive/grid2xyz/surfer2dat/main.cpp
Normal file
@@ -0,0 +1,120 @@
|
||||
#include "common.h"
|
||||
|
||||
void disp_help()
|
||||
{
|
||||
cout<<"surfer2dat 0.5 - convert surfer's grid file into .dat & .fie files"<<endl<<endl
|
||||
<<"usage: surfer2dat input-file -e<input-file2>|-f<elevation|radius>|-d [-r<xmin>/<xmax>/<ymin>/<ymax>] [-o<output-file>]"<<endl
|
||||
<<" -e specify each point's elevation or radius by input-file2 which should be as the same size as input-file"<<endl
|
||||
<<" -f convert to .fie file, elevation or radius of the input data must be given"<<endl
|
||||
<<" -d convert to .dat file"<<endl
|
||||
<<" -r specify the interest area"<<endl
|
||||
<<" -o specify the ouput-name, the input-file's name will be used if -o is absent"<<endl<<endl
|
||||
<<"example: surfer2dat in.grd -d example_out"<<endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (argc==1)
|
||||
{
|
||||
disp_help();
|
||||
}
|
||||
else if (argc==2)
|
||||
{
|
||||
cout<<"too few arguments, program stopped..."<<endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
CON c;
|
||||
int space_num;
|
||||
string temp = "";
|
||||
string input_name = "";
|
||||
string input_name2 = "";
|
||||
string output_name = "";
|
||||
stringstream stemp;
|
||||
int out_type = -1;
|
||||
double range[4] = {MAX,MAX,MAX,MAX};
|
||||
double topo = MAX;
|
||||
|
||||
input_name = argv[1];
|
||||
|
||||
for (int i = 2; i < argc; i++)
|
||||
{
|
||||
if (typeget(argv[i],OUTPUT,temp))//取得输出名
|
||||
{
|
||||
output_name = temp;
|
||||
temp = "";
|
||||
}
|
||||
|
||||
if (typeget(argv[i],ELE,temp))
|
||||
{
|
||||
if (temp=="")
|
||||
{
|
||||
cout<<"no input_name for '-e' command, program stopped..."<<endl;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
input_name2 = temp;
|
||||
temp = "";
|
||||
out_type = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeget(argv[i],FIE,temp))
|
||||
{
|
||||
if (temp=="")
|
||||
{
|
||||
cout<<"no input for '-f' command, program stopped..."<<endl;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
stemp.clear();
|
||||
stemp.str(temp);
|
||||
stemp>>topo;
|
||||
out_type = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!strcmp(argv[i],DAT)) out_type = 2;
|
||||
|
||||
if (typeget(argv[i],RANGE,temp))
|
||||
{
|
||||
replace_all(temp,"/"," ",space_num);
|
||||
stemp.clear();
|
||||
stemp.str(temp);
|
||||
if (space_num==3)
|
||||
{
|
||||
stemp>>range[0]>>range[1]>>range[2]>>range[3];
|
||||
if (range[0]>range[1])
|
||||
{
|
||||
cout<<"wrong x range, program stopped..."<<endl;
|
||||
return 0;
|
||||
}
|
||||
else if (range[2]>range[3])
|
||||
{
|
||||
cout<<"wrong y range, program stopped..."<<endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"insufficient attributes information, program stoped..."<<endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(output_name=="")
|
||||
{
|
||||
char* fullname;
|
||||
int len = input_name.length();
|
||||
fullname=(char *)malloc((len+1)*sizeof(char));
|
||||
input_name.copy(fullname,len,0);
|
||||
nameget(fullname,GRD,output_name);
|
||||
}
|
||||
if(c.run(input_name,input_name2,output_name,out_type,topo,range)) return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user