rdprofile.c 5.98 KB
Newer Older
saeed's avatar
saeed committed
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#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;
    while(fscanf(file,"%s %s %s",string,string1,string2) && !feof(file)) npt++;
    struct Index *ind_pt = (struct Index *)malloc(sizeof(struct Index) * npt);
    int ntotal = npt;
    fseek(file,fcur,SEEK_SET);
    npt = 0;
    while(fscanf(file,"%s %s %s",string,string1,string2) && !feof(file)){
      char clat[64] = {'\0'};
      char clon[64] = {'\0'};
      getfield(string1,',',1,clat,64);
      getfield(string1,',',2,clon,64);
      float flat = cddmmss_to_f(clat);
      float flon = cddmmss_to_f(clon);
      fprintf(fout,"%-16s\t%f\t%f\n",string2, flon, flat);
      struct Point point = {(double)flat,(double)flon};
      struct Index ind  = find_nearestindex(&point);
      printf("%-16s\t%f\t%f\t%d\t%d\t%f\t%f\t%f\t%d\n",string2, flon, flat,ind.i,ind.j,ind.lon,ind.lat,ind.distance,mask[ind.j*nx + ind.i]);
      *(ind_pt+npt) = ind;
      
      char *ct =  (ind_pt+npt)->name;
      char *cs = string2;
      while(*cs != '\0' ){
	*ct = *cs;
	ct++;
	cs++;
      }
      *ct='\0';
      npt++;
    }
    fclose(file);
    fclose(fout);
    writetoxml(ciof,cdom,ind_pt,ntotal);
    free(ind_pt);
  }
  return 0;
}