ExaGeoStat
ExaGeoStat is a parallel high performance unified framework for geostatistics on manycore systems.
nc_file.c
Go to the documentation of this file.
1 
20 #include "../include/nc_file.h"
21 //***************************************************************************************
22 int openFileNC(MLE_data *data, char *filename)
24 
29 {
30  VERBOSE("Open the given NetCDF file... ");
31 
32  int ncid, retval;
33  if ((retval = nc_open(filename, NC_NOWRITE, &ncid)))
34  printf("Error: %s\n", nc_strerror(retval));
35  VERBOSE("Done \n");
36  return ncid;
37 
38 }
39 
40 
41 int countlinesNC(int ncid, char *dim1, char *dim2)
43 
48 {
49  int retval;
50  size_t lat_len;
51  int lat_varid;
52  size_t lon_len;
53  int lon_varid;
54  //double *lats_in, *lons_in;
55 
56  //Retrieve latitiude dim length.
57  if ((retval = nc_inq_dimid(ncid, dim1, &lat_varid)))
58  printf("Error: %s\n", nc_strerror(retval));
59  if ((retval = nc_inq_dimlen(ncid, lat_varid, &lat_len)))
60  printf("Error: %s\n", nc_strerror(retval));
61  //Retrieve longitude dim length.
62  if ((retval = nc_inq_dimid(ncid, dim2 , &lon_varid)))
63  printf("Error: %s\n", nc_strerror(retval));
64  if ((retval = nc_inq_dimlen(ncid, lon_varid, &lon_len)))
65  printf("Error: %s\n", nc_strerror(retval));
66 
67  return lat_len * lon_len;
68 }
69 
70 void readLocsNC_1d(MLE_data *data, int ncid)
72 
77 {
78  int retval;
79  size_t lat_len;
80  int lat_varid;
81  size_t lon_len;
82  int lon_varid;
83  double *lats_in, *lons_in;
84  int i, j, n, index;
85  location *locations = &data->l1;
86 
87  VERBOSE("Read LOCS from the given NetCDF file... ");
88 
89  //Retrieve latitiude dim length.
90  if ((retval = nc_inq_dimid(ncid,"latitude",&lat_varid)))
91  printf("Error: %s\n", nc_strerror(retval));
92  if ((retval = nc_inq_dimlen(ncid, lat_varid, &lat_len)))
93  printf("Error: %s\n", nc_strerror(retval));
94  //Retrieve longitude dim length.
95  if ((retval = nc_inq_dimid(ncid,"longitude", &lon_varid)))
96  printf("Error: %s\n", nc_strerror(retval));
97  if ((retval = nc_inq_dimlen(ncid, lon_varid, &lon_len)))
98  printf("Error: %s\n", nc_strerror(retval));
99 
100  n = lat_len * lon_len;
101  //Aloocate Memory
102  lats_in = (double *) malloc(lat_len * sizeof(double));
103  lons_in = (double *) malloc(lon_len * sizeof(double));
104  locations->x = (double *) malloc(n * sizeof(double));
105  locations->y = (double *) malloc(n * sizeof(double));
106 
107  //Get the varids of the latitude and longitude coordinate variables.
108  if ((retval = nc_inq_varid(ncid, "latitude", &lat_varid)))
109  printf("Error: %s\n", nc_strerror(retval));
110  if ((retval = nc_inq_varid(ncid, "longitude", &lon_varid)))
111  printf("Error: %s\n", nc_strerror(retval));
112 
113  //Retrieve lats and lons values from th given file.
114  if ((retval = nc_get_var_double(ncid, lat_varid, &lats_in[0])))
115  printf("Error: %s\n", nc_strerror(retval));
116  if ((retval = nc_get_var_double(ncid, lon_varid, &lons_in[0])))
117  printf("Error: %s\n", nc_strerror(retval));
118 
119  index = 0;
120  for (i = 0; i < lat_len ; i++)
121  for (j = 0; j < lon_len ; j++)
122  {
123  locations->x[index] = lats_in[i];
124  locations->y[index++] = lons_in[j];
125  }
126 
127  VERBOSE("Done \n");
128  //zsort_locations(n,locations);
129  free(lats_in);
130  free(lons_in);
131 }
132 
133 
134 void readLocsNC_2d(MLE_data *data, int ncid)
136 
141 {
142  int retval;
143  size_t sn_len;
144  int lat_varid;
145  size_t we_len;
146  int lon_varid;
147  //int i, j, index;
148  int n;
149  location *locations = &data->l1;
150 
151  VERBOSE("Read LOCS from the given NetCDF file... ");
152 
153  //Retrieve latitiude dim length.
154  if ((retval = nc_inq_dimid(ncid,"west_east",&lat_varid)))
155  printf("Error: %s\n", nc_strerror(retval));
156  if ((retval = nc_inq_dimlen(ncid, lat_varid, &we_len)))
157  printf("Error: %s\n", nc_strerror(retval));
158  //Retrieve longitude dim length.
159  if ((retval = nc_inq_dimid(ncid,"south_north", &lon_varid)))
160  printf("Error: %s\n", nc_strerror(retval));
161  if ((retval = nc_inq_dimlen(ncid, lon_varid, &sn_len)))
162  printf("Error: %s\n", nc_strerror(retval));
163 
164  n = we_len * sn_len;
165  //Aloocate Memory
166  locations->x = (double *) malloc(n * sizeof(double));
167  locations->y = (double *) malloc(n * sizeof(double));
168 
169  //Get the varids of the latitude and longitude coordinate variables.
170  if ((retval = nc_inq_varid(ncid, "XLAT", &lat_varid)))
171  printf("Error: %s\n", nc_strerror(retval));
172  if ((retval = nc_inq_varid(ncid, "XLONG", &lon_varid)))
173  printf("Error: %s\n", nc_strerror(retval));
174 
175  //Retrieve lats and lons values from th given file.
176  if ((retval = nc_get_var_double(ncid, lat_varid, locations->x)))
177  printf("Error: %s\n", nc_strerror(retval));
178  if ((retval = nc_get_var_double(ncid, lon_varid, locations->y)))
179  printf("Error: %s\n", nc_strerror(retval));
180 
181 /*
182  for(index = 0 ; index < 10; index++)
183  printf("%f - %f - %f -%f\n", locations->x[index], locations->y[index]);
184 */
185 
186  VERBOSE("Done \n");
187  //zsort_locations(n,locations);
188 }
189 
190 void readVarNCs(MLE_data *data, int ncid, char *varname, double *data_in , char *dim1, char *dim2)
192 
201 {
202  int varid;
203  int retval;
204  size_t lat_len;
205  int lat_varid;
206  size_t lon_len;
207  int lon_varid;
208  //int n;
209 
210  VERBOSE("Read observations from the given NetCDF variable... ");
211  //Retrieve latitiude dim length.
212  if ((retval = nc_inq_dimid(ncid, dim1, &lat_varid)))
213  printf("Error: %s\n", nc_strerror(retval));
214  if ((retval = nc_inq_dimlen(ncid,lat_varid,&lat_len)))
215  printf("Error: %s\n", nc_strerror(retval));
216  //Retrieve longitude dim length.
217  if ((retval = nc_inq_dimid(ncid, dim2,&lon_varid)))
218  printf("Error: %s\n", nc_strerror(retval));
219  if ((retval = nc_inq_dimlen(ncid,lon_varid,&lon_len)))
220  printf("Error: %s\n", nc_strerror(retval));
221 
222  //n = lat_len * lon_len;
223  size_t start[] = {0, 0, 0}; /* start at first value */
224  size_t count[] = {1, lat_len, lon_len}; //wind_speed
225 // size_t count[] = {1, lon_len, lat_len}; //rain, temp
226  //Get the varid of the data variable, based on its name.
227  if ((retval = nc_inq_varid(ncid, varname, &varid)))
228  printf("Error: %s\n", nc_strerror(retval));
229  if ((retval = nc_get_vara_double(ncid, varid, start, count, data_in)))
230  printf("Error: %s\n", nc_strerror(retval));
231 
232  VERBOSE("DONE\n");
233 
234 /*
235  int i;
236  for(i =0;i<500;i++)
237  printf("%e\n", data_in[i]);
238 */
239 }
240 
241 
242 void closeFileNC(MLE_data *data, int ncid)
244 
248 {
249  int retval;
250 
251  VERBOSE("Close the NetCDF file... ");
252  if ((retval = nc_close(ncid)))
253  printf("Error: %s\n", nc_strerror(retval));
254  VERBOSE("Done\n");
255 }
256 
void readLocsNC_1d(MLE_data *data, int ncid)
Read 1D locations from a given.
Definition: nc_file.c:70
void readLocsNC_2d(MLE_data *data, int ncid)
Read 2D locations from a given.
Definition: nc_file.c:134
int countlinesNC(int ncid, char *dim1, char *dim2)
Retrieve number of measurements.
Definition: nc_file.c:41
double * y
Values in Y dimension.
Definition: MLE_misc.h:83
double * x
Values in X dimension.
Definition: MLE_misc.h:82
location l1
2D locations for the first dataset.
Definition: MLE_misc.h:117
void readVarNCs(MLE_data *data, int ncid, char *varname, double *data_in, char *dim1, char *dim2)
Read Read observations from.
Definition: nc_file.c:190
void closeFileNC(MLE_data *data, int ncid)
Close the NetCDF file.
Definition: nc_file.c:242
#define VERBOSE(str)
Definition: MLE_misc.h:182
int openFileNC(MLE_data *data, char *filename)
Open the NetCDF file.
Definition: nc_file.c:22