Commit 12ea0249 authored by saeed's avatar saeed
Browse files

first commit

parents
#include<stdio.h>
#include<stdlib.h>
char Str[32] = {'\0'};
char RStr[32] = {'\0'};
static void reverse(char *s, char *t, int sign, int npadzero){
char *tmp = s;
int len = 0;
while (*tmp !='\0' ){
tmp++;
len++;
}
tmp--;
if (sign < 0){
*t = '-';
t++;
}
while(npadzero--){
*t = '0';
t++;
}
while (len >0 ){
*t = *tmp;
tmp--;
len--;
t++;
}
*t = '\0';
}
static void intostr(int a, char *st){
int sign = 1;
if (a < 0){
sign = -1;
a = (-a);
}
int index = 0;
int r = -1 ;
int q = 0;
char cl[64] = {'\0'};
do {
r = a % 10;
q = (a - r) / 10;
*(cl+index) = (char)(r+'0');
a = q;
index++;
}while(a >0);
*(cl+index) = '\0';
reverse(cl,st,sign,0);
}
static void intostr_pad(int a, char *st, int npad){
int sign = 1;
if (a < 0){
sign = -1;
a = (-a);
}
int index = 0;
int r = -1 ;
int q = 0;
char cl[64] = {'\0'};
do {
r = a % 10;
q = (a - r) / 10;
*(cl+index) = (char)(r+'0');
a = q;
index++;
}while(a >0);
*(cl+index) = '\0';
reverse(cl,st,sign,npad);
}
static void floatostr(float a, char *str, int nafterdecimalpoint){
int sign = 1;
long long int ia = (long long int) (a);
if (a < 0) {
sign = -1;
a = (-a);
ia = (long long int) (a);
}
int npadzero = 0;
long long int n = 1;
float b = a - (float)ia;
float bt = b;
int hitnonzero = 0;
for(int i=0; i < nafterdecimalpoint;++i){
n *= 10L;
bt *= 10L;
if (((long long int)bt == 0) && (hitnonzero == 0)) npadzero++;
if ((long long int)bt != 0 ) hitnonzero = 1;
bt = bt - (long long int)bt;
}
if (nafterdecimalpoint == 1) npadzero = 0;
b *= n;
long long int ifrac = (long long int) b;
int index = 0;
long long int r = -1 ;
long long int q = 0;
char cl[64] = {'\0'};
char ctmp[64] = {'\0'};
do {
r = ia % 10;
q = (ia - r) / 10;
*(cl+index) = (char)(r+'0');
ia = q;
index++;
}while(ia >0);
*(cl+index) = '\0';
reverse(cl,ctmp,sign,0);
char ck[64] = {'\0'};
char ctmp1[64] = {'\0'};
r = -1 ;
q = 0;
index = 0;
do {
r = ifrac % 10;
q = (ifrac - r) / 10;
*(ck+index) = (char)(r+'0');
ifrac = q;
index++;
}while(ifrac >0);
*(ck+index) = '\0';
if (sign > 0){
reverse(ck,ctmp1,sign,npadzero);
}else{
reverse(ck,ctmp1,-sign,npadzero);
}
char *tmp = ctmp;
while(*tmp != '\0'){
*str = *tmp;
str++;
tmp++;
}
*str = '.';
str++;
tmp = ctmp1;
while(*tmp != '\0'){
*str = *tmp;
str++;
tmp++;
}
*str = '\0';
return;
}
typedef struct{
char *beg;
int size;
}string;
static long long int pow10(long long int n){
long long int d = 1;
if (n > 0){
for (long long int i=0; i < n;++i) d *= 10;
}
return d;
}
static long long int stoi_bg(char *beg, char *end){
int sign = 0;
long long int nc=0;
char *temp = beg;
while (temp<=end ){
if ( *temp != ' ') nc++;
temp++;
}
nc--;
long long int d = 0;
long long int c = 1;
char *st = beg;
while (st<=end ){
if (*st == '-'){
c = -1;
nc--;
st++;
sign += 1;
if (sign > 1 ) return 0;
}
else if (*st == '+'){
c = 1;
nc--;
st++;
sign += 1;
if (sign > 1 ) return 0;
}
else if( *st != ' ') {
d += ((int)(*st - '0')*pow10(nc));
st++;
nc--;
}
else {
st++;
}
}
return (c*d);
}
static int lenstr(char *str){
int result = 0;
if (str){
while(*str != '\0'){
result++;
str++;
}
}
return result;
}
static string find(char *str, char *sub){
string result = {NULL,0};
int lsub = lenstr(sub);
int count = 0;
int found = 0;
if (str && sub){
char *t1 = str;
char *t2 = sub;
while(*t1 != '\0'){
count = 0;
if (*t1 == *t2){
result.beg = t1;
found = 1;
count++;
char *n1 = t1 + 1;
char *n2 = t2 + 1;
while(*n1 != '\0' && *n2 != '\0'){
if (*n1 != *n2){
found = 0;
result.beg = NULL;
break;
}
count++;
n1++;
n2++;
}
if (found){
break;
}
}
t1++;
}
}
if (count != lsub) {
result.beg = NULL;
result.size = 0;
}
if (count == lsub && found){
result.size = (count);
}
return result;
}
static string extime(char *filename, char *prefix, char *suffix){
string result = {NULL,0};
string pre = find(filename, prefix);
string suf = find(filename, suffix);
if (pre.beg && suf.beg ){
result.beg = pre.beg + pre.size;
result.size = suf.beg - (pre.beg + pre.size);
}
return result;
}
static void show(string ss){
if (ss.beg && ss.size){
for (int i =0; i < ss.size;++i) printf("%c",*(ss.beg + i));
//printf("\nbeg = %c %p \n",*(ss.beg),(void *)(ss.beg));
//printf("end = %c %p \n",*(ss.beg + ss.size -1),(void *)(ss.beg + ss.size - 1));
}
printf("\n");
}
#if 0
int main(void){
char *filename = "NS01_20150310_1-24H.nc";
char *prefix = "NS01_";
char *suffix = "_1-24H.nc";
char *AA = "smhi_saqw8_sufm";
char *BB = "saqw8";
string name = find(AA,BB);
show(name);
string tid = extime(filename,prefix,suffix);
show(tid);
return 0;
}
#endif
static double DEG_TO_RAD = 0.0174532;
static double EARTH_RADIUS_IN_METERS = 6372797.560856;
struct Position {
double lat;
double lon;
};
static double haversine(struct Position *from, struct Position *to) {
double lat_arc = (from->lat - to->lat) * DEG_TO_RAD;
double lon_arc = (from->lon - to->lon) * DEG_TO_RAD;
double lat_h = sin(lat_arc * 0.5);
lat_h *= lat_h;
double lon_h = sin(lon_arc * 0.5);
lon_h *= lon_h;
double tmp = cos(from->lat*DEG_TO_RAD) * cos(to->lat*DEG_TO_RAD);
return (2.0 * asin(sqrt(lat_h + tmp*lon_h)));
}
static double distance_in_meters(struct Position *from, struct Position *to) {
return EARTH_RADIUS_IN_METERS*haversine(from, to);
}
#include<stdio.h>
#include<stdlib.h>
#include<netcdf.h>
#include<math.h>
#define nx (1238)
#define ny (1046)
#define ARRLEN(a) ((sizeof(a) / sizeof(a[0])))
static float nlon[ny*nx] = {0.0};
static float nlat[ny*nx] = {0.0};
static double dist[ny*nx] = {0.0};
static signed char mask[ny*nx] = {0};
struct Index{
int i;
int j;
double distance;
double lat;
double lon;
char name[512];
};
struct Point{
double lat;
double lon;
};
#ifdef __cplusplus
extern "C" {
#endif
#include"haversin.c"
static double minval(double *ds, int *io, int *jo,float maskval){
double mins = 0.0;
mins = *ds;
for (int j=0; j <ny;++j){
for (int i=0; i <nx;++i){
if ( *(ds + j * nx + i) != maskval){
mins = *(ds + j * nx + i);
break;
}
}
}
for (int j=0; j <ny;++j){
for (int i=0; i <nx;++i){
if ( mins >= *(ds + j * nx + i) && *(ds + j * nx + i) != maskval){
mins = *(ds + j * nx + i);
*io = i;
*jo = j;
}
}
}
return mins;
}
static void read_meshmask(char *filename){
int ncid, ilat, ilon, imask;
char *varlat = "nav_lat", *varlon = "nav_lon", *varmask = "tmaskutil";
nc_open(filename,NC_NOWRITE,&ncid);
nc_inq_varid(ncid, varmask, &imask);
nc_inq_varid(ncid, varlat, &ilat);
nc_inq_varid(ncid, varlon, &ilon);
nc_get_var(ncid, imask,&mask[0]);
nc_get_var(ncid, ilat,&nlat[0]);
nc_get_var(ncid, ilon,&nlon[0]);
nc_close(ncid);
}
static struct Index find_nearestindex(struct Point *point){
struct Position to = {0.0,0.0};
struct Position from = {0.0,0.0};
struct Index result = {0,0,0.0,0.0,0.0,{'\0'}};
double mindis = 0.0;
for (int j=0; j < ny;++j){
for (int i=0; i < nx;++i){
if (mask[j*nx + i] == 1){
to.lat = *(nlat + j * nx + i);
to.lon = *(nlon + j * nx + i);
from.lat = point->lat;
from.lon = point->lon;
*(dist+j * nx + i) = distance_in_meters(&from, &to);
}
else{
*(dist+j * nx + i) = -1.0;
}
}
}
result.distance = minval(dist, &result.i,&result.j,-1.0);
result.lat = *(nlat + result.j * nx + result.i);
result.lon = *(nlon + result.j * nx + result.i);
return result;
}
static void skipline_startwith(FILE *file, char cf){
int cc = 0;
while ((cc =fgetc(file)) != EOF){
if (cc == cf){
int cb = 0;
while((cb=fgetc(file)) != EOF){
if (cb == '\n') break;
}
}
else{
fseek(file,-1,SEEK_CUR);
break;
}
}
}
static void getfield(char *str,char fs,int nf,char *cout, int N){
if (str == NULL) return;
int ne = 0;
int nn = 0;
char *tmp = str;
while(*tmp != '\0'){
if (*tmp == fs) ne++;
tmp++;
}
if (ne == 0){
tmp = str;
while( *tmp != '\0' && (nn < (N-1))){
*(cout + nn ) = *tmp;
nn++;
tmp++;
}
cout[nn] = '\0';
return;
}
int ntotal = (ne + 1);
if (nf > ntotal) {
for (int i=0; i < N;++i) *(cout +i) = '\0';
return;
}
int nc = 0;
tmp = str;
char *beg = NULL;
char *end = NULL;
beg = tmp;
int ncount = 0;
while(*tmp != '\0'){
if (*tmp == fs){
nc++;
end = tmp -1;
if (ncount == 0) end = NULL;
if (nc == nf){
break;
}
else{
beg = tmp + 1;
}
}
tmp++;
ncount++;
}
if (nf == ntotal){
tmp = str;
while(*tmp != '\0'){
tmp++;
}
end = tmp -1;
}
nn = 0;
for (char *tmp = beg; tmp <= end && (nn < (N-1)) && (end != NULL);++tmp){
*(cout + nn ) = *tmp;
nn++;
}
cout[nn] = '\0';
return;
}
static float cddmmss_to_f(char *clat){
char degree[3] = {'\0'};
char minute[3] = {'\0'};
char second[3] = {'\0'};
degree[0] = clat[0];
degree[1] = clat[1];
degree[2] = '\0';
minute[0] = clat[2];
minute[1] = clat[3];
minute[2] = '\0';
second[0] = clat[4];
second[1] = clat[5];
second[2] = '\0';
float fdeg = atof(degree);
float fmin = atof(minute) / 60.0 ;
float fsec = atof(second) / 3600.0;
float result = fdeg + fmin + fsec;
return (result);
}
static void concat(char *str1, char *str2, char *str3, int N){
char *tmp = str1;
int nn = 0;
while(*tmp != '\0' && (nn < (N-1))){
*(str3+nn) = *tmp;
nn++;
tmp++;
}
tmp = str2;
while(*tmp != '\0' && (nn < (N-1))){
*(str3+nn) = *tmp;
nn++;
tmp++;
}
str3[nn] = '\0';
}
#include"wxml.c"
#ifdef __cplusplus
}
#endif
int main(int argc, char *argv[]){
if (argc == 2){
char *filename = argv[1];
FILE *file = NULL;
file = fopen(filename,"r");
skipline_startwith(file, '#');
long fcur = ftell(file);
char string[512] = {'\0'};
char string1[512] = {'\0'};
char string2[512] = {'\0'};
char string3[512] = {'\0'};
read_meshmask("mesh_mask.nc");
concat("latlon_",filename,string3,512);
FILE *fout = NULL;
fout = fopen(string3,"w+");
char *ciof = "iodef_point.xml";
char *cdom = "domaindef_point.xml";
int npt = 0;