Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/misc/netcdf/src/GridDataMapper/cs2cs_interface.c
3206 views
1
/***** Vili Forsell, 28.6.2011
2
* Function for interfacing with cs2cs (i.e. PROJ.4)
3
* See "/user/include/proj_api.h" for cs2cs application interface
4
*/
5
#include <stdlib.h>
6
#include <proj_api.h>
7
#include <stdio.h>
8
9
char const MSG_HEADER[] = "GridDataMapper: CS2CS coordinate system transformation:";
10
11
// Add boolean values
12
enum bool {
13
true = 1,
14
false = 0
15
};
16
typedef enum bool bool;
17
18
/* Function: cs2cs_transform()
19
*** Transforms the given input from a coordinate system to another with cs2cs
20
@param coord The input (Elmer) coordinates to be transformed
21
@param hasZ If 0, the coord parameter contains 3rd dimension, otherwise 2D
22
@param isRad If 0, then uses radians during transformation, otherwise degrees
23
@param elmer_proj The cs2cs parameter string for Elmer grid coordinate system (from)
24
@param netcdf_proj The cs2cs parameter string for NetCDF grid coordinate system (to)
25
@param res The resulting (NetCDF) data values for further indexing
26
*/
27
void cs2cs_transform( double coord[3], int hasZ, int isRad, char elmer_proj[], char netcdf_proj[], double res[3] ) {
28
projPJ pj_elmer = NULL, pj_netcdf = NULL; // Coordinate system definitions
29
bool hasErrors = false;
30
int transf_val = 0; // For return value from the transformation predefined by cs2cs as 0, if no errors, and as error code otherwise
31
bool isZset = true; // True, if z coordinate is used
32
bool isInputRad = false; // True, if the given input is in radians
33
34
// Set constants
35
long point_count = 1; // Number of processed points (x,y,z can be arrays)
36
int point_offset = 1; // Step size, if multiple points (f.ex. every second) processed
37
38
// Input information
39
if ( hasZ == 0 ) isZset = false;
40
if ( isRad != 0 ) isRad = true;
41
42
/* DEBUG PRINTOUTS
43
printf("(%.2f, %.2f, %.2f) ; hasZ = %d ; isRad = %d\n",coord[0],coord[1],coord[2],hasZ,isRad);
44
fprintf(stdout,"\n\"%s\"\n",elmer_proj);
45
fprintf(stdout,"\n\"%s\"\n",netcdf_proj);
46
*/
47
48
// Initializes the coordinate systems for the read Elmer data point and the NetCDF data
49
pj_elmer = pj_init_plus(elmer_proj);
50
if ( pj_elmer == NULL ) {
51
printf("%s Failed to initialize Elmer coordinate system with parameters: %s!\n", MSG_HEADER, elmer_proj);
52
hasErrors = true;
53
}
54
pj_netcdf = pj_init_plus(netcdf_proj);
55
if ( pj_netcdf == NULL ) {
56
printf("%s Failed to initialize NetCDF coordinate system with parameters: %s!\n", MSG_HEADER, netcdf_proj);
57
hasErrors = true;
58
}
59
60
// Transformation from degrees to radians, if necessary
61
if ( !isInputRad ) {
62
printf("%s Converting values from degrees to radians.\n", MSG_HEADER);
63
coord[0] *= DEG_TO_RAD;
64
coord[1] *= DEG_TO_RAD;
65
coord[2] *= DEG_TO_RAD;
66
}
67
68
// Performs the transformation (according to input dimensions)
69
if ( hasErrors != true ) {
70
if (isZset) transf_val = pj_transform(elmer_proj,netcdf_proj,point_count,point_offset,&coord[0],&coord[1],&coord[2]);
71
else transf_val = pj_transform(elmer_proj,netcdf_proj,point_count,point_offset,&coord[0],&coord[1],NULL);
72
if ( transf_val != 0 ) {
73
printf("%s Failed to transform coordinates; Proj.4 error code %d.\n",MSG_HEADER,transf_val);
74
hasErrors = true;
75
}
76
}
77
78
// Saves output, if no errors
79
if ( hasErrors == true ) {
80
res[0] = 0;
81
res[1] = 0;
82
res[2] = 0;
83
abort(); // Immediate abort, since this is likely not intended and affects all results
84
} else {
85
res[0] = coord[0];
86
res[1] = coord[1];
87
res[2] = coord[2];
88
}
89
90
/* DEBUG PRINTOUTS
91
printf("Done with (%.2f, %.2f)\n\n", coord[0], coord[1]);
92
*/
93
}
94
95