00001
00002
00003
00004
00005
00006
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #include <dsclim.h>
00055
00057 int
00058 read_obs_period(double **buffer, double **lon, double **lat, double *missing_value, data_struct *data, char *varname,
00059 int *year, int *month, int *day, int *nlon, int *nlat, int ntime) {
00078 double *buf = NULL;
00079 char *infile = NULL;
00080 int year1 = 0;
00081 int year2 = 0;
00082 double *timeval = NULL;
00083 char *cal_type = NULL;
00084 char *time_units = NULL;
00085 int ntime_obs;
00086 int found = FALSE;
00087 time_vect_struct *time_s = NULL;
00088
00089 info_field_struct *info = NULL;
00090 proj_struct *proj = NULL;
00091
00092 int tmpi;
00093 char *format = NULL;
00094
00095 int t;
00096 int tl;
00097 int var;
00098 int istat;
00099 int i;
00100 int j;
00101
00102 int ntime_file;
00103
00104 char *prev_infile = NULL;
00105
00106
00107 var = find_str_value(varname, data->conf->obs_var->netcdfname, data->conf->obs_var->nobs_var);
00108 if (var == -1) return -2;
00109
00110 infile = (char *) malloc(MAXPATH * sizeof(char));
00111 if (infile == NULL) alloc_error(__FILE__, __LINE__);
00112 prev_infile = (char *) malloc(MAXPATH * sizeof(char));
00113 if (prev_infile == NULL) alloc_error(__FILE__, __LINE__);
00114 (void) strcpy(prev_infile, "");
00115 format = (char *) malloc(MAXPATH * sizeof(char));
00116 if (format == NULL) alloc_error(__FILE__, __LINE__);
00117
00118 info = (info_field_struct *) malloc(sizeof(info_field_struct));
00119 if (info == NULL) alloc_error(__FILE__, __LINE__);
00120 proj = (proj_struct *) malloc(sizeof(proj_struct));
00121 if (proj == NULL) alloc_error(__FILE__, __LINE__);
00122
00123 *lat = NULL;
00124 *lon = NULL;
00125
00126 if (data->conf->obs_var->proj->name != NULL)
00127 (void) free(data->conf->obs_var->proj->name);
00128 data->conf->obs_var->proj->name = NULL;
00129 proj->name = NULL;
00130 if (data->conf->obs_var->proj->grid_mapping_name != NULL)
00131 (void) free(data->conf->obs_var->proj->grid_mapping_name);
00132 data->conf->obs_var->proj->grid_mapping_name = NULL;
00133 proj->grid_mapping_name = NULL;
00134
00135
00136 for (t=0; t<ntime; t++) {
00137
00138
00139 (void) strcpy(format, "%s/%s/");
00140 (void) strcat(format, data->conf->obs_var->template);
00141 if (data->conf->obs_var->month_begin != 1) {
00142
00143 if (month[t] < data->conf->obs_var->month_begin)
00144 year1 = year[t] - 1;
00145 else
00146 year1 = year[t];
00147 year2 = year1 + 1;
00148 if (data->conf->obs_var->year_digits == 4)
00149 (void) sprintf(infile, format, data->conf->obs_var->path, data->conf->obs_var->frequency,
00150 data->conf->obs_var->acronym[var], year1, year2);
00151 else {
00152 tmpi = year1 / 100;
00153 year1 = year1 - (tmpi*100);
00154 tmpi = year2 / 100;
00155 year2 = year2 - (tmpi*100);
00156 (void) sprintf(infile, format, data->conf->obs_var->path, data->conf->obs_var->frequency,
00157 data->conf->obs_var->acronym[var], year1, year2);
00158 }
00159 }
00160 else {
00161
00162 if (data->conf->obs_var->year_digits == 4) {
00163 year1 = year[t];
00164 (void) sprintf(infile, format, data->conf->obs_var->path, data->conf->obs_var->frequency,
00165 data->conf->obs_var->acronym[var], year1);
00166 }
00167 else {
00168 tmpi = year1 / 100;
00169 year1 = year1 - (tmpi*100);
00170 (void) sprintf(infile, format, data->conf->obs_var->path, data->conf->obs_var->frequency,
00171 data->conf->obs_var->acronym[var], year1);
00172 }
00173 }
00174
00175
00176 if ( strcmp(prev_infile, infile) ) {
00177 (void) printf("%s: Reading observation data %s from %s\n", __FILE__, varname, infile);
00178 if (time_s != NULL) {
00179 (void) free(time_s->year);
00180 (void) free(time_s->month);
00181 (void) free(time_s->day);
00182 (void) free(time_s->hour);
00183 (void) free(time_s->minutes);
00184 (void) free(time_s->seconds);
00185
00186 (void) free(time_s);
00187 (void) free(cal_type);
00188 (void) free(time_units);
00189 (void) free(timeval);
00190 }
00191
00192 time_s = (time_vect_struct *) malloc(sizeof(time_vect_struct));
00193 if (time_s == NULL) alloc_error(__FILE__, __LINE__);
00194
00195 istat = get_time_info(time_s, &timeval, &time_units, &cal_type, &ntime_obs, infile, data->conf->obs_var->timename, FALSE);
00196 if (istat < 0) {
00197 (void) free(time_s);
00198 (void) free(infile);
00199 (void) free(prev_infile);
00200 (void) free(format);
00201 (void) free(info);
00202 (void) free(proj);
00203 return -1;
00204 }
00205 }
00206
00207
00208 found = FALSE;
00209 tl = 0;
00210 while (tl<ntime_obs && found == FALSE) {
00211 if (year[t] == time_s->year[tl] && month[t] == time_s->month[tl] && day[t] == time_s->day[tl])
00212 found = TRUE;
00213 tl++;
00214 }
00215
00216 if (found == TRUE) {
00217
00218 tl--;
00219
00220
00221 istat = read_netcdf_var_3d_2d(&buf, info, proj, infile, data->conf->obs_var->acronym[var],
00222 data->conf->obs_var->dimxname, data->conf->obs_var->dimyname, data->conf->obs_var->timename,
00223 tl, nlon, nlat, &ntime_file, FALSE);
00224 *missing_value = info->fillvalue;
00225
00226 if (data->conf->obs_var->proj->name == NULL) {
00227
00228 data->conf->obs_var->proj->name = strdup(proj->name);
00229 data->conf->obs_var->proj->grid_mapping_name = strdup(proj->grid_mapping_name);
00230 data->conf->obs_var->proj->latin1 = proj->latin1;
00231 data->conf->obs_var->proj->latin2 = proj->latin2;
00232 data->conf->obs_var->proj->lonc = proj->lonc;
00233 data->conf->obs_var->proj->lat0 = proj->lat0;
00234 data->conf->obs_var->proj->false_easting = proj->false_easting;
00235 data->conf->obs_var->proj->false_northing = proj->false_northing;
00236 }
00237
00238 if ( (*lat) == NULL && (*lon) == NULL ) {
00239
00240 istat = read_netcdf_latlon(lon, lat, nlon, nlat, data->conf->obs_var->dimcoords, data->conf->obs_var->proj->coords,
00241 data->conf->obs_var->proj->name, data->conf->obs_var->lonname,
00242 data->conf->obs_var->latname, data->conf->obs_var->dimxname,
00243 data->conf->obs_var->dimyname, infile);
00244
00245
00246 *buffer = (double *) malloc((*nlon)*(*nlat)*ntime * sizeof(double));
00247 if ( (*buffer) == NULL) alloc_error(__FILE__, __LINE__);
00248 }
00249
00250
00251 for (j=0; j<(*nlat); j++)
00252 for (i=0; i<(*nlon); i++)
00253 if (buf[i+j*(*nlon)] != (*missing_value))
00254 (*buffer)[i+j*(*nlon)+t*(*nlon)*(*nlat)] = (buf[i+j*(*nlon)] * data->conf->obs_var->factor[var]) +
00255 data->conf->obs_var->delta[var];
00256 else
00257 (*buffer)[i+j*(*nlon)+t*(*nlon)*(*nlat)] = (*missing_value);
00258
00259
00260 (void) free(proj->name);
00261 (void) free(proj->grid_mapping_name);
00262
00263 (void) free(info->grid_mapping);
00264 (void) free(info->units);
00265 (void) free(info->height);
00266 (void) free(info->coordinates);
00267 (void) free(info->long_name);
00268
00269 (void) free(buf);
00270 }
00271 else {
00272 (void) fprintf(stderr, "%s: Fatal error in algorithm: date not found: %d %d %d %d!!\n", __FILE__, t, year[t],month[t],day[t]);
00273
00274
00275 (void) free(infile);
00276 (void) free(format);
00277
00278 (void) free(info);
00279 (void) free(proj);
00280
00281 if (time_s != NULL) {
00282 (void) free(time_s->year);
00283 (void) free(time_s->month);
00284 (void) free(time_s->day);
00285 (void) free(time_s->hour);
00286 (void) free(time_s->minutes);
00287 (void) free(time_s->seconds);
00288
00289 (void) free(time_s);
00290 (void) free(cal_type);
00291 (void) free(time_units);
00292 (void) free(timeval);
00293 }
00294
00295 return -1;
00296 }
00297 (void) strcpy(prev_infile, infile);
00298 }
00299
00300
00301 if (time_s != NULL) {
00302 (void) free(time_s->year);
00303 (void) free(time_s->month);
00304 (void) free(time_s->day);
00305 (void) free(time_s->hour);
00306 (void) free(time_s->minutes);
00307 (void) free(time_s->seconds);
00308
00309 (void) free(time_s);
00310 (void) free(cal_type);
00311 (void) free(time_units);
00312 (void) free(timeval);
00313 }
00314
00315 (void) free(info);
00316 (void) free(proj);
00317
00318 (void) free(infile);
00319 (void) free(prev_infile);
00320 (void) free(format);
00321
00322
00323 return 0;
00324 }