update source for windows

This commit is contained in:
2021-08-26 21:27:06 +08:00
parent 83c03e5231
commit 2209673ee5
6 changed files with 94 additions and 43 deletions

View File

@@ -1,7 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <fstream>
#include <iostream> // Added by Zhang Yi on 2021-08-26
#include <string>
#include "../lib/constants.h"
#include "../lib/parsers.h"
@@ -11,6 +13,11 @@
#define GRID_FORMAT "%lf %lf %f %lf"
#if defined(_MSC_VER) /* Added for windows by Yi Zhang on 2021-08-26*/
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
// TODO conversion of input/output units nT/km pT/km nT/m pT/m
@@ -106,14 +113,17 @@ int main(int argc, char**argv)
double bz[MAX_GRID_POINTS];
//read file with bx
char * line = NULL;
//char * line = NULL;
std::string line;
size_t len = 0;
ssize_t read;
FILE * bxfp = fopen(args.gridbx_fn, "r");
if (bxfp == NULL)
//FILE * bxfp = fopen(args.gridbx_fn, "r");
//if (bxfp == NULL)
std::ifstream bxfp(args.gridbx_fn, std::ios::in); // Updated by Yi Zhang on 2021-08-26
if (!bxfp)
{
printf("ERROR: Can not open file with Bx values.\n");
exit(EXIT_FAILURE);
@@ -123,10 +133,12 @@ int main(int argc, char**argv)
while ((read = getline(&line, &len, bxfp )) != -1)
//while ((read = getline(&line, &len, bxfp )) != -1)
while (getline(bxfp, line))
{
if ((line[0] != '#') && (strlen(line) > 2))
//if ((line[0] != '#') && (strlen(line) > 2))
if ((line[0] != '#') && (line.length() > 2))
{
n_lines++;
if (n_lines>MAX_GRID_POINTS)
@@ -135,11 +147,12 @@ int main(int argc, char**argv)
exit(EXIT_FAILURE);
}
sscanf(line, GRID_FORMAT, &lons[n_lines-1], &lats[n_lines-1], &alts[n_lines-1], &bx[n_lines-1]);
sscanf(line.c_str(), GRID_FORMAT, &lons[n_lines-1], &lats[n_lines-1], &alts[n_lines-1], &bx[n_lines-1]);
}
}
fclose(bxfp);
//fclose(bxfp);
bxfp.close();
/*number of grid points*/
@@ -183,27 +196,33 @@ int main(int argc, char**argv)
/* read other grids */
// By
FILE * byfp = fopen(args.gridby_fn, "r");
if (byfp == NULL)
//FILE * byfp = fopen(args.gridby_fn, "r");
//if (byfp == NULL)
std::ifstream byfp(args.gridby_fn, std::ios::in);
if (!byfp)
{
printf("ERROR: Can not open file with Bx values.\n");
exit(EXIT_FAILURE);
}
int n_lines2 = 0;
while ((read = getline(&line, &len, byfp )) != -1)
//while ((read = getline(&line, &len, byfp )) != -1)
while (getline(byfp, line))
{
if ((line[0] != '#') && (strlen(line) > 2))
{
n_lines2++;
//printf("%s", line);
double dummy1, dummy2;
float dummy3;
sscanf(line, GRID_FORMAT , &dummy1, &dummy2, &dummy3, &by[n_lines2-1]);
}
//if ((line[0] != '#') && (strlen(line) > 2))
if ((line[0] != '#') && (line.length() > 2))
{
n_lines2++;
//printf("%s", line);
double dummy1, dummy2;
float dummy3;
sscanf(line.c_str(), GRID_FORMAT , &dummy1, &dummy2, &dummy3, &by[n_lines2-1]);
}
}
fclose(byfp);
//fclose(byfp);
byfp.close();
if (n_lines2 != n_lines)
{
@@ -212,29 +231,33 @@ int main(int argc, char**argv)
}
// Bz
FILE * bzfp = fopen(args.gridbz_fn, "r");
if (bzfp == NULL)
//FILE * bzfp = fopen(args.gridbz_fn, "r");
//if (bzfp == NULL)
std::ifstream bzfp(args.gridbz_fn, std::ios::in);
if (!bzfp)
{
printf("ERROR: Can not open file with Bx values.\n");
exit(EXIT_FAILURE);
}
n_lines2 = 0;
while ((read = getline(&line, &len, bzfp )) != -1)
//while ((read = getline(&line, &len, bzfp )) != -1)
while (getline(bzfp, line))
{
if ((line[0] != '#') && (strlen(line) > 2))
if ((line[0] != '#') && (line.length() > 2))
{
n_lines2++;
//printf("%s", line);
double dummy1, dummy2;
float dummy3;
double bz_curr;
sscanf(line, GRID_FORMAT, &dummy1, &dummy2, &dummy3,&bz_curr);
sscanf(line.c_str(), GRID_FORMAT, &dummy1, &dummy2, &dummy3,&bz_curr);
bz[n_lines2-1] = args.bz_NEU_NED* bz_curr; //COORDINATE SYSTEM NEU or NED
}
}
fclose(byfp);
//fclose(byfp);
byfp.close();
if (n_lines2 != n_lines)
{