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
|
#include "stdafx.h"
#include "iostream"
#include <stdio.h>
#include "stdlib.h"
#include "string.h"
#include "cmath"
#include <errno.h>
using namespace std;
/* Conversion useful */
#ifndef GRA2RAD
#define GRA2RAD ((22/7) / 180.0)
#endif
#ifndef RAD2GRA
#define RAD2GRA (180.0 / (22/7))
#endif
/*! \ struct graminseg
\ brief Structure to store the coordinates in the form (degree, minute, second) */
struct graminseg
{
int gra; /*!< sexagesimal degrees */
int min; /*!< minutes */
int seg; /*!< seconds*/
};
int globe_nombre(char *target, int *lon0, int *lat0, double tlon, double lat)
{
double lon;
if (tlon >= 180.0)
lon = tlon - 360.0;
else
lon = tlon;
if (lat >= 50.0 )
{
*lat0 = 90;
if (lon < -90.0)
{
strcpy(target, "a10g");
*lon0 = -180;
}
else if (lon < 0.0)
{
strcpy(target, "b10g");
*lon0 = -90;
}
else if (lon < 90.0)
{
strcpy(target, "c10g");
*lon0 = 0;
}
else if (lon < 180.0)
{
strcpy(target, "d10g");
*lon0 = 90;
}
else
return 0;
}
else if (lat >= 0.0)
{
*lat0 = 50;
if (lon < -90.0)
{
strcpy(target, "e10g");
*lon0 = -180;
}
else if (lon < 0.0)
{
strcpy(target, "f10g");
*lon0 = -90;
}
else if (lon < 90.0)
{
strcpy(target, "g10g");
*lon0 = 0;
}
else if (lon < 180.0)
{
strcpy(target, "h10g");
*lon0 = 90;
}
}
else if (lat >= -50.0)
{
*lat0 = 0;
if (lon < -90.0)
{
strcpy(target, "i10g");
*lon0 = -180;
}
else if (lon < 0.0)
{
strcpy(target, "j10g");
*lon0 = -90;
}
else if (lon < 90.0)
{
strcpy(target, "k10g");
*lon0 = 0;
}
else if (lon < 180.0)
{
strcpy(target, "l10g");
*lon0 = 90;
}
}
else if (lat >= -90.0)
{
*lat0 = -50;
if (lon < -90.0)
{
strcpy(target, "m10g");
*lon0 = -180;
}
else if (lon < 0.0)
{
strcpy(target, "n10g");
*lon0 = -90;
}
else if (lon < 90.0)
{
strcpy(target, "o10g");
*lon0 = 0;
}
else if (lon < 180.0)
{
strcpy(target, "p10g");
*lon0 = 90;
}
}
else
return 0;
return 1;
}
int rad_to_graminseg(struct graminseg *gms, double rad)
{
double a;
a = rad * RAD2GRA;
if ( a >= 0.0)
{
(gms->gra) = (int)floor(a);
a = (a -(double)(gms->gra)) * 60.0;
(gms->min) = (int)floor(a);
a = (a -(double)(gms->min)) * 60.0;
(gms->seg) = (int)floor(a);
}
else
{
a = -(a);
(gms->gra) = (int)floor(a);
a = (a -(double)(gms->gra)) * 60.0;
(gms->min) = (int)floor(a);
a = (a -(double)(gms->min)) * 60.0;
(gms->seg) = (int)floor(a);
(gms->gra) = -(gms->gra);
(gms->min) = -(gms->min);
(gms->seg) = -(gms->seg);
}
return 1;
}
int globe_nrow_gms(struct graminseg *gms, int lat0)
{
int i;
i = (120 * (lat0 - gms->gra) - 2 * gms->min - (gms->seg / 30));
if (lat0 >= 0)
i--;
if (i >= 0)
return i;
return 0;
}
int globe_ncol_gms(struct graminseg *gms, int lon0)
{
int i;
i = ((gms->gra - lon0) * 120 + 2 * gms->min + (gms->seg / 30));
if (lon0 < 0)
i--;
if (i >= 0)
return i;
/*if (i >= 10800) return 10799;*/
return 0;
}
int globe_lee_dato_s(FILE *g, int *dato, int row, int col)
{
off_t offset;
offset = 21600 * row + 2 * col;
if (fseek (g, offset, SEEK_SET))
{
return 0;
}
if ( fread(dato, sizeof(int), 1, g) != 1)
{
return 0;
}
return 1;
}
int main(int *alt, double *lat, double *lon, size_t ndat, const char *globe_path)// int argc, _TCHAR* argv[])
{
try
{
int lat0, lon0, nc, nr, nv = 0;
size_t i;
int dato;
char aux[128],file[256], oldfile[256];
FILE *globe = NULL;
struct graminseg gmslat, gmslon;
globe_path="D:/Data/";
// init some vars
file[0] = '\0';
oldfile[0] = '\0';
for (i = 0; i < ndat; i++)
{
/* what is the file to open ? */
strcpy(file, globe_path);
if (globe_nombre(aux, &lon0, &lat0, lon[i] * RAD2GRA, lat[i] * RAD2GRA))
strcat(file, aux);
else
{
if (globe != NULL)
fclose(globe);
return -1;
}
/* Row and column where the data */
rad_to_graminseg( &gmslat, lat[i]);
if (lon[i] > (22/7))
rad_to_graminseg( &gmslon, lon[i] - 2 * (22/7));
else
rad_to_graminseg( &gmslon, lon[i]);
nc = globe_ncol_gms(&gmslon, lon0);
nr = globe_nrow_gms(&gmslat, lat0);
/* Open the file if needed */
if (strlen(oldfile) == 0 || strcmp(oldfile,file))
{
if (globe != NULL)
fclose(globe);
if ((globe = fopen(file, "r")) == NULL )
{
return -1;
};
}
if (globe_lee_dato_s (globe, &dato, nr, nc))
{
alt[i] = (int) dato;
nv++;
}
else
alt[i] = -9999;
strcpy(oldfile, file);
}
}
catch (exception e)
{
throw e;
}
/*cout <<"Hello" << endl;
return 0;*/
}
|