Path: blob/devel/misc/netcdf/src/GridDataMapper/cs2cs_interface.c
3206 views
/***** Vili Forsell, 28.6.20111* Function for interfacing with cs2cs (i.e. PROJ.4)2* See "/user/include/proj_api.h" for cs2cs application interface3*/4#include <stdlib.h>5#include <proj_api.h>6#include <stdio.h>78char const MSG_HEADER[] = "GridDataMapper: CS2CS coordinate system transformation:";910// Add boolean values11enum bool {12true = 1,13false = 014};15typedef enum bool bool;1617/* Function: cs2cs_transform()18*** Transforms the given input from a coordinate system to another with cs2cs19@param coord The input (Elmer) coordinates to be transformed20@param hasZ If 0, the coord parameter contains 3rd dimension, otherwise 2D21@param isRad If 0, then uses radians during transformation, otherwise degrees22@param elmer_proj The cs2cs parameter string for Elmer grid coordinate system (from)23@param netcdf_proj The cs2cs parameter string for NetCDF grid coordinate system (to)24@param res The resulting (NetCDF) data values for further indexing25*/26void cs2cs_transform( double coord[3], int hasZ, int isRad, char elmer_proj[], char netcdf_proj[], double res[3] ) {27projPJ pj_elmer = NULL, pj_netcdf = NULL; // Coordinate system definitions28bool hasErrors = false;29int transf_val = 0; // For return value from the transformation predefined by cs2cs as 0, if no errors, and as error code otherwise30bool isZset = true; // True, if z coordinate is used31bool isInputRad = false; // True, if the given input is in radians3233// Set constants34long point_count = 1; // Number of processed points (x,y,z can be arrays)35int point_offset = 1; // Step size, if multiple points (f.ex. every second) processed3637// Input information38if ( hasZ == 0 ) isZset = false;39if ( isRad != 0 ) isRad = true;4041/* DEBUG PRINTOUTS42printf("(%.2f, %.2f, %.2f) ; hasZ = %d ; isRad = %d\n",coord[0],coord[1],coord[2],hasZ,isRad);43fprintf(stdout,"\n\"%s\"\n",elmer_proj);44fprintf(stdout,"\n\"%s\"\n",netcdf_proj);45*/4647// Initializes the coordinate systems for the read Elmer data point and the NetCDF data48pj_elmer = pj_init_plus(elmer_proj);49if ( pj_elmer == NULL ) {50printf("%s Failed to initialize Elmer coordinate system with parameters: %s!\n", MSG_HEADER, elmer_proj);51hasErrors = true;52}53pj_netcdf = pj_init_plus(netcdf_proj);54if ( pj_netcdf == NULL ) {55printf("%s Failed to initialize NetCDF coordinate system with parameters: %s!\n", MSG_HEADER, netcdf_proj);56hasErrors = true;57}5859// Transformation from degrees to radians, if necessary60if ( !isInputRad ) {61printf("%s Converting values from degrees to radians.\n", MSG_HEADER);62coord[0] *= DEG_TO_RAD;63coord[1] *= DEG_TO_RAD;64coord[2] *= DEG_TO_RAD;65}6667// Performs the transformation (according to input dimensions)68if ( hasErrors != true ) {69if (isZset) transf_val = pj_transform(elmer_proj,netcdf_proj,point_count,point_offset,&coord[0],&coord[1],&coord[2]);70else transf_val = pj_transform(elmer_proj,netcdf_proj,point_count,point_offset,&coord[0],&coord[1],NULL);71if ( transf_val != 0 ) {72printf("%s Failed to transform coordinates; Proj.4 error code %d.\n",MSG_HEADER,transf_val);73hasErrors = true;74}75}7677// Saves output, if no errors78if ( hasErrors == true ) {79res[0] = 0;80res[1] = 0;81res[2] = 0;82abort(); // Immediate abort, since this is likely not intended and affects all results83} else {84res[0] = coord[0];85res[1] = coord[1];86res[2] = coord[2];87}8889/* DEBUG PRINTOUTS90printf("Done with (%.2f, %.2f)\n\n", coord[0], coord[1]);91*/92}939495