13 #include <gsl/gsl_errno.h>
14 #include <gsl/gsl_interp2d.h>
15 #include <gsl/gsl_spline2d.h>
19 #define ANC_STAT_2T_END \
22 #define ANC_STAT_2T_START \
26 #define ANC_STAT_CLIM 0
29 #define OZ_KG_M2_TO_DU 1. / 2.1415e-5
34 #define ANC_SRC_TYP_ECMWF 0
35 #define ANC_SRC_TYP_STD_HDF 1
36 #define ANC_SRC_TYP_OLCI 2
37 #define ANC_SRC_TYP_GMAO 3
38 #define ANC_SRC_TYP_BAD -1
39 #define FILE_CHECK(file_name) if (access(file_name, F_OK) || access(file_name, R_OK)) { \
40 printf("--Error-- : Input file '%s' does not exist or cannot open.\n.See line %d in %s\n", \
41 file_name, __LINE__, __FILE__); \
84 static met_sto_str met_sto[
NPRM];
131 char *prm_nm_met[] = {
"sp",
"tcwv",
"msl",
"u10",
"v10",
"t2m",
"d2m"};
132 char *prm_nm_oz[] = {
"tco3"};
133 int32_t n_prm_met = 7, n_prm_oz = 1, sto_ix;
134 static char file_olci_str[FILENAME_MAX];
135 char *file_olci = (
char *)0;
141 char *ptr = strrchr(file_olci_str,
'/');
144 strcat(file_olci_str,
"/tie_meteo.nc");
146 strcpy(file_olci_str,
"tie_meteo.nc");
148 file_olci = file_olci_str;
150 proc_land =
input->proc_land;
157 if ((anc_id[0] == -1) || (anc_id[1] == -1))
180 if ((anc_id[0] == -1) || (anc_id[1] == -1))
213 int status, ncid, fstat, tie_pt;
214 char *str_attr, lcl_fil[FILENAME_MAX];
220 fprintf(
stderr,
"-E- %s %d: First anc file name is null\n", __FILE__, __LINE__);
229 if ((file_olci != (
char *)0) && (strcmp(
upcase(lcl_fil),
"OLCI_TIE_METEO") == 0)) {
231 "-I- %s %d: Ancillary file will be taken from OLCI metadata tie point meteo file: %s\n",
232 __FILE__, __LINE__, file_olci);
243 if (nc_open(
file, 0, &ncid) != NC_NOERR) {
251 if (file_olci != (
char *)0) {
252 status = nc_inq_attlen(ncid, NC_GLOBAL,
"title", &attlen);
254 str_attr = (
char *)calloc(attlen + 1,
sizeof(
char));
255 status = nc_get_att_text(ncid, NC_GLOBAL,
"title", str_attr);
257 fprintf(
stderr,
"-I- %s %d: nc_get_att_string error, file: %s\n", __FILE__, __LINE__,
262 if (strcmp(str_attr,
"OLCI Level 1b Product, Tie-Point Meteo Data Set") == 0) {
275 status = nc_inq_attlen(ncid, NC_GLOBAL,
"title", &attlen);
278 str_attr = (
char *)calloc(attlen + 1,
sizeof(
char));
279 status = nc_get_att_text(ncid, NC_GLOBAL,
"title", str_attr);
281 fprintf(
stderr,
"-I- %s %d: nc_get_att_string error, file: %s\n", __FILE__, __LINE__,
285 if (strstr(str_attr,
"GMAO") != 0) {
346 if ((strcmp(
files[0],
files[2]) == 0) && (f12_mtch == 0)) {
347 printf(
"%s, %d E: ANC1 and 3 match while ANC2 different\n", __FILE__, __LINE__);
350 if ((f12_mtch == 1) && (f23_mtch == 1))
352 else if ((f12_mtch == 1) && (f23_mtch == 0))
354 else if ((f12_mtch == 0) && (f23_mtch == 1))
364 }
else if (n_anc == 2) {
373 if (prioritize_files) {
413 static int32_t firstcall = 0;
414 static int32_t ntim_int;
415 static float r2d = 57.29577951;
416 static int32_t anc_id = -1;
417 static gen_int_str *met_int, *met_tim;
420 static char file_olci_str[FILENAME_MAX];
421 char *file_olci = (
char *)0;
424 int32_t nlvl_int = 1;
425 int32_t ilvl = 0, nlvl = 1;
426 int32_t itim, ilon,
npix, iprm, t_interp, data_ix[2];
428 float val, wt_t1, uwnd, vwnd, unc, u_unc, v_unc, ws_2;
429 double l_time, anc_times[3],
lat,
lon, last_time;
432 if (firstcall == 0) {
439 char *ptr = strrchr(file_olci_str,
'/');
442 strcat(file_olci_str,
"/tie_meteo.nc");
444 strcpy(file_olci_str,
"tie_meteo.nc");
446 file_olci = file_olci_str;
460 if ((met_int = (gen_int_str *)malloc(n_met * nlvl_int * ntim_int *
sizeof(gen_int_str))) ==
NULL) {
461 fprintf(
stderr,
"-E- %s %d: Unable to allocate met interpolation array\n", __FILE__, __LINE__);
474 for (itim = 0; itim < ntim_int; itim++) {
475 met_tim = met_int + itim * n_met;
479 if (last_time > met_tim[0].anc_time) {
480 fprintf(
stderr,
"-E- %s %d: met file times are out of sequence\n", __FILE__, __LINE__);
483 last_time = met_tim[0].anc_time;
488 fprintf(
stderr,
"-E- %s %d: Error reading ancillary file: %s\n", __FILE__, __LINE__,
files[0]);
497 l_time =
l1rec->scantime;
501 for (iprm = 0; iprm < n_met; iprm++) {
503 for (itim = 0; itim < ntim_int; itim++)
504 anc_times[itim] = met_int[iprm + n_met * itim].anc_time;
511 for (ilon = 0; ilon <
npix; ilon++) {
515 if (lon < -180.0 || lon > 180.0 || lat < -90.0 || lat > 90.0 || isnan(
lat) || isnan(
lon)) {
523 if(field_skip == 0 ) {
524 if (
anc_acq_eval_pt(met_int, iprm, ilvl,
lat,
lon, t_interp, data_ix, wt_t1, ntim_int, nlvl,
525 n_met, &
val, &unc) != 0) {
526 fprintf(
stderr,
"-E- %s %d: Error interpolating to file: %s\n", __FILE__, __LINE__,
files[0]);
534 if (
l1rec->uncertainty)
535 l1rec->uncertainty->dzw[ilon] = unc;
539 if (
l1rec->uncertainty)
540 l1rec->uncertainty->dmw[ilon] = unc;
542 uwnd =
l1rec->zw[ilon];
543 vwnd =
l1rec->mw[ilon];
544 if (
l1rec->uncertainty) {
545 u_unc =
l1rec->uncertainty->dzw[ilon];
546 v_unc =
l1rec->uncertainty->dmw[ilon];
548 if (
input->windspeed != -2000)
549 l1rec->ws[ilon] = sqrt(pow(uwnd, 2.) + pow(vwnd, 2.));
550 if (
input->windangle != -2000)
551 l1rec->wd[ilon] = atan2f(-uwnd, -vwnd) * r2d;
555 ws_2 = uwnd * uwnd + vwnd * vwnd;
556 if (
l1rec->uncertainty) {
557 if ((uwnd + vwnd) > 0.05 * (u_unc + v_unc)) {
558 l1rec->uncertainty->dws[ilon] =
559 sqrt((uwnd * uwnd * u_unc * u_unc + vwnd * vwnd * v_unc * v_unc) / ws_2);
560 l1rec->uncertainty->dwd[ilon] =
561 sqrt(vwnd * vwnd * u_unc * u_unc + uwnd * uwnd * v_unc * v_unc) / ws_2;
562 if (
l1rec->uncertainty->dwd[ilon] >
M_PI)
565 l1rec->uncertainty->dws[ilon] = sqrt(0.5 * (u_unc * u_unc + v_unc * v_unc));
568 l1rec->uncertainty->dwd[ilon] *= r2d;
572 if (
input->pressure != -2000) {
573 if (proc_land && (
l1rec->height[ilon] != 0))
574 val *= exp(-
l1rec->height[ilon] / 8434.);
576 if (
l1rec->uncertainty)
577 l1rec->uncertainty->dpr[ilon] = unc;
581 if (
input->watervapor != -2000)
583 if (
l1rec->uncertainty)
584 l1rec->uncertainty->dwv[ilon] = unc / 10.;
587 if (
input->relhumid != -2000)
589 if (
l1rec->uncertainty)
590 l1rec->uncertainty->drh[ilon] = unc;
603 if( !
l1rec->land[ilon] ) {
610 if(
l1rec->land[ilon] ) {
623 static int32_t firstcall = 0;
624 static int32_t ntim_int;
625 static gen_int_str *aer_int, *aer_tim;
629 int32_t itim, ilon,
npix, iprm, t_interp, data_ix[2];
630 int32_t ilvl = 0, nlvl = 1;
632 float val, wt_t1, unc;
633 double l_time, anc_times[3],
lat,
lon, last_time;
634 anc_aer_struc *anc_aerosol;
638 if (firstcall == 0) {
645 printf(
"\nOpening ancillary aerosol files.\n");
646 printf(
" anc_aerosol1 = %s\n",
input->anc_aerosol1);
647 printf(
" anc_aerosol2 = %s\n",
input->anc_aerosol2);
648 printf(
" anc_aerosol3 = %s\n",
input->anc_aerosol3);
659 fprintf(
stderr,
"-E- %s %d: Ancillary met profile climatology not implemented yet\n", __FILE__,
665 if ((aer_int = (gen_int_str *)malloc(n_met * nlvl * ntim_int *
sizeof(gen_int_str))) ==
NULL) {
666 fprintf(
stderr,
"-E- %s %d: Unable to allocate profile interpolation array\n", __FILE__,
673 for (itim = 0; itim < ntim_int; itim++) {
674 aer_tim = aer_int + itim * n_met;
678 if (last_time > aer_tim[0].anc_time) {
679 fprintf(
stderr,
"-E- %s %d: met file times are out of sequence\n", __FILE__, __LINE__);
682 last_time = aer_tim[0].anc_time;
687 anc_aerosol =
l1rec->anc_aerosol;
688 if (anc_aerosol ==
NULL) {
696 l_time =
l1rec->scantime;
699 for (iprm = 0; iprm < n_met; iprm++) {
701 for (itim = 0; itim < ntim_int; itim++)
702 anc_times[itim] = aer_int[iprm + n_met * itim].anc_time;
709 for (ilon = 0; ilon <
npix; ilon++) {
713 if (lon < -180.0 || lon > 180.0 || lat < -90.0 || lat > 90.0 || isnan(
lat) || isnan(
lon)) {
716 if (
anc_acq_eval_pt(aer_int, iprm, ilvl,
lat,
lon, t_interp, data_ix, wt_t1, ntim_int, nlvl,
717 n_met, &
val, &unc) != 0) {
718 fprintf(
stderr,
"-E- %s %d: Error interpolating to file: %s\n", __FILE__, __LINE__,
files[0]);
724 l1rec->anc_aerosol->black_carbon_ext[ilon] =
val;
727 l1rec->anc_aerosol->black_carbon_scat[ilon] =
val;
730 l1rec->anc_aerosol->dust_ext[ilon] =
val;
733 l1rec->anc_aerosol->dust_scat[ilon] =
val;
736 l1rec->anc_aerosol->sea_salt_ext[ilon] =
val;
739 l1rec->anc_aerosol->sea_salt_scat[ilon] =
val;
742 l1rec->anc_aerosol->sulphur_ext[ilon] =
val;
745 l1rec->anc_aerosol->sulphur_scat[ilon] =
val;
748 l1rec->anc_aerosol->organic_carbon_ext[ilon] =
val;
751 l1rec->anc_aerosol->organic_carbon_scat[ilon] =
val;
754 l1rec->anc_aerosol->total_aerosol_ext[ilon] =
val;
757 l1rec->anc_aerosol->total_aerosol_scat[ilon] =
val;
760 l1rec->anc_aerosol->total_aerosol_angstrom[ilon] =
val;
791 static int32_t firstcall = 0;
792 static int32_t ntim_int;
793 static gen_int_str *prof_int, *prof_tim;
797 int32_t nlvl =
l1rec->l1file->nlvl;
799 int32_t itim, ilon, ilvl,
npix, iprm,
loc, t_interp, data_ix[2];
800 float val, wt_t1, unc;
801 double l_time, anc_times[3],
lat,
lon, last_time;
806 if (firstcall == 0) {
813 printf(
"\nOpening meteorological profile files.\n");
814 printf(
" anc_profile1 = %s\n",
input->anc_profile1);
815 printf(
" anc_profile2 = %s\n",
input->anc_profile2);
816 printf(
" anc_profile3 = %s\n",
input->anc_profile3);
827 fprintf(
stderr,
"-E- %s %d: Ancillary met profile climatology not implemented yet\n", __FILE__,
834 if ((prof_int = (gen_int_str *)malloc(n_met * nlvl * ntim_int *
sizeof(gen_int_str))) ==
NULL) {
835 fprintf(
stderr,
"-E- %s %d: Unable to allocate profile interpolation array\n", __FILE__,
843 for (itim = 0; itim < ntim_int; itim++) {
844 prof_tim = prof_int + n_met * nlvl * itim;
848 if (last_time > prof_tim[0].anc_time) {
849 fprintf(
stderr,
"-E- %s %d: met file times are out of sequence\n", __FILE__, __LINE__);
852 last_time = prof_tim[0].anc_time;
857 anc_add =
l1rec->anc_add;
858 if (anc_add ==
NULL) {
867 l_time =
l1rec->scantime;
870 for (iprm = 0; iprm < n_met; iprm++) {
871 for (ilvl = 0; ilvl < nlvl; ilvl++) {
873 for (itim = 0; itim < ntim_int; itim++) {
874 loc = iprm + n_met * (ilvl + nlvl * itim);
875 anc_times[itim] = prof_int[
loc].anc_time;
883 for (ilon = 0; ilon <
npix; ilon++) {
887 if (lon < -180.0 || lon > 180.0 || lat < -90.0 || lat > 90.0 || isnan(
lat) || isnan(
lon)) {
890 if (
anc_acq_eval_pt(prof_int, iprm, ilvl,
lat,
lon, t_interp, data_ix, wt_t1, ntim_int, nlvl,
891 n_met, &
val, &unc) != 0) {
892 fprintf(
stderr,
"-E- %s %d: Error interpolating file: %s\n", __FILE__, __LINE__,
897 loc = ilvl + nlvl * ilon;
929 static int32_t firstcall = 0;
930 static size_t ntim_int, ntime_step;
931 static gen_int_str *rad_int, *rad_tim;
932 static float time_hours[72];
937 if (firstcall == 0) {
939 size_t file_count = 1;
944 printf(
"\nOpening ancillary RAD files.\n");
945 printf(
" rad1 = %s\n",
input->cld_rad1);
946 printf(
" rad2 = %s\n",
input->cld_rad2);
947 printf(
" rad3 = %s\n",
input->cld_rad3);
949 size_t file_offset = 0;
951 char isodate1[32], isodate2[32], isodate3[32];
952 memset(isodate1,
'\0', 32);
953 memset(isodate2,
'\0', 32);
954 memset(isodate3,
'\0', 32);
962 int status = nc_inq_dimid((rad_file).fid,
"time", &dim_id);
964 status = nc_inq_dimlen((rad_file).fid, dim_id, &ntime_step);
966 printf(
"--Error--: Dimension %s could not be read. See line %d in file %s.\nExiting...\n ",
"time",__LINE__, __FILE__);
970 nc_get_att_text(rad_file.fid, NC_GLOBAL,
"time_coverage_start", isodate1);
974 nc_get_att_text(rad_file.fid, NC_GLOBAL,
"time_coverage_start", isodate2);
978 nc_get_att_text(rad_file.fid, NC_GLOBAL,
"time_coverage_start", isodate3);
984 if (strcmp(isodate1, isodate2) != 0) {
985 if (st_time1 > st_time2) {
986 printf(
"-Error-: Wrong time ordering for GMAO RAD inputs 1 and 2\n");
993 if (strcmp(isodate3, isodate2) != 0) {
994 if (st_time2 > st_time3) {
995 printf(
"-Error-: Wrong time ordering for GMAO RAD ancillary inputs 2 and 3\n");
1000 ntim_int = ntime_step * file_count;
1004 if ((rad_int = (gen_int_str *)malloc(n_rad * ntim_int *
sizeof(gen_int_str))) ==
NULL) {
1005 fprintf(
stderr,
"-E- %s %d: Unable to allocate profile interpolation array\n", __FILE__,
1014 float time_range[ntim_int];
1015 float zero_time_offset;
1016 switch (file_count) {
1018 zero_time_offset = rad_int[0].anc_time;
1021 if (file_offset == 1)
1022 zero_time_offset = rad_int[0].anc_time;
1024 zero_time_offset = (rad_int + ntime_step * n_rad)[0].anc_time;
1027 zero_time_offset = (rad_int + ntime_step * n_rad)[0].anc_time;
1030 zero_time_offset = rad_tim[0].anc_time;
1034 for (
size_t itim = 0; itim < ntime_step; itim++) {
1035 size_t time_slice_start = ntime_step *
ifile;
1036 gen_int_str *rad_tim = rad_int + (itim + time_slice_start) * n_rad;
1038 time_range[time_slice_start + itim] = (rad_tim[0].anc_time - zero_time_offset) / 3600.0;
1042 for (
size_t itim = 0; itim < ntim_int; itim++) {
1043 time_hours[itim] = time_range[itim];
1050 for (
size_t iprm = 0; iprm < n_rad; iprm++) {
1051 for (
size_t ip = 0; ip <
npix; ip++) {
1052 for (
size_t itim = 0; itim < ntim_int; itim++) {
1057 if (lon < -180.0 || lon > 180.0 || lat < -90.0 || lat > 90.0 || isnan(
lat) || isnan(
lon))
1060 fprintf(
stderr,
"-E- %s %d: Error interpolating from file: %s\n", __FILE__, __LINE__,
1061 files[itim % ntime_step]);
1067 l1rec->cld_rad->taucld[ip][itim] =
val;
1070 l1rec->cld_rad->cfcld[ip][itim] =
val;
1082 if ((
l1rec->anc_aerosol = (anc_aer_struc *)malloc(
sizeof(anc_aer_struc))) ==
NULL) {
1083 fprintf(
stderr,
"-E- %s %d: Unable to allocate additional ancillary data storage 1\n", __FILE__,
1087 if (((
l1rec->anc_aerosol->black_carbon_ext = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1088 ((
l1rec->anc_aerosol->black_carbon_scat = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1089 ((
l1rec->anc_aerosol->dust_ext = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1090 ((
l1rec->anc_aerosol->dust_scat = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1091 ((
l1rec->anc_aerosol->sea_salt_ext = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1092 ((
l1rec->anc_aerosol->sea_salt_scat = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1093 ((
l1rec->anc_aerosol->sulphur_ext = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1094 ((
l1rec->anc_aerosol->sulphur_scat = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1095 ((
l1rec->anc_aerosol->organic_carbon_ext = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1096 ((
l1rec->anc_aerosol->organic_carbon_scat = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1097 ((
l1rec->anc_aerosol->total_aerosol_ext = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1098 ((
l1rec->anc_aerosol->total_aerosol_scat = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
NULL) ||
1099 ((
l1rec->anc_aerosol->total_aerosol_angstrom = (
float *)malloc(
l1rec->npix *
sizeof(
float))) ==
1101 fprintf(
stderr,
"-E- %s %d: Unable to allocate additional ancillary data storage 2\n", __FILE__,
1109 if ((
l1rec->cld_rad = (cld_rad_struct *)malloc(
sizeof(cld_rad_struct))) ==
NULL) {
1110 fprintf(
stderr,
"-E- %s %d: Unable to allocate additional ancillary data storage for RAD data 1\n",
1111 __FILE__, __LINE__);
1115 l1rec->cld_rad->cfcld = (
float **)malloc(
sizeof(
float *) *
npix);
1116 l1rec->cld_rad->taucld = (
float **)malloc(
sizeof(
float *) *
npix);
1117 l1rec->cld_rad->ntimes = times_dim;
1118 l1rec->cld_rad->timecldrange = (
float *)malloc(
sizeof(
float) * times_dim);
1119 for (
size_t itim = 0; itim < times_dim; itim++) {
1120 l1rec->cld_rad->timecldrange[itim] = time_range[itim];
1122 for (
size_t ip = 0; ip <
npix; ip++) {
1123 if (((
l1rec->cld_rad->cfcld[ip] = (
float *)malloc(
sizeof(
float) * times_dim)) ==
NULL) ||
1124 ((
l1rec->cld_rad->taucld[ip] = (
float *)malloc(
sizeof(
float) * times_dim)) ==
NULL)) {
1126 "-E- %s %d: Unable to allocate additional ancillary data storage for RAD data 2\n",
1127 __FILE__, __LINE__);
1156 int32_t
npix, nlvl = 42;
1160 if ((
l1rec->anc_add = (anc_struc *)malloc(
sizeof(anc_struc))) ==
NULL) {
1161 fprintf(
stderr,
"-E- %s %d: Unable to allocate additional ancillary data storage 1\n", __FILE__,
1165 if (((
l1rec->anc_add->prof_temp = (
float *)malloc(nlvl *
npix *
sizeof(
float))) ==
NULL) ||
1166 ((
l1rec->anc_add->prof_rh = (
float *)malloc(nlvl *
npix *
sizeof(
float))) ==
NULL) ||
1167 ((
l1rec->anc_add->prof_height = (
float *)malloc(nlvl *
npix *
sizeof(
float))) ==
NULL) ||
1168 ((
l1rec->anc_add->prof_q = (
float *)malloc(nlvl *
npix *
sizeof(
float))) ==
NULL) ||
1169 ((
l1rec->anc_add->prof_o3 = (
float *)malloc(nlvl *
npix *
sizeof(
float))) ==
NULL)) {
1170 fprintf(
stderr,
"-E- %s %d: Unable to allocate additional ancillary data storage 2\n", __FILE__,
1199 static int32_t firstcall = 0;
1200 static int32_t ntim_int;
1201 static int32_t anc_id = -1;
1202 static gen_int_str *oz_int, *oz_tim;
1205 static char file_olci_str[FILENAME_MAX];
1206 char *file_olci = (
char *)0;
1208 int32_t nlvl_int = 1;
1209 int32_t ilvl = 0, nlvl = 1;
1210 int32_t itim, ilon,
npix, iprm, t_interp, data_ix[2];
1211 float val, wt_t1, unc;
1212 double l_time, anc_times[3],
lat,
lon, last_time;
1215 if (firstcall == 0) {
1222 char *ptr = strrchr(file_olci_str,
'/');
1225 strcat(file_olci_str,
"/tie_meteo.nc");
1227 strcpy(file_olci_str,
"tie_meteo.nc");
1229 file_olci = file_olci_str;
1243 if ((oz_int = (gen_int_str *)malloc(n_oz * nlvl_int * ntim_int *
sizeof(gen_int_str))) ==
NULL) {
1244 fprintf(
stderr,
"-E- %s %d: Unable to allocate met interpolation array\n", __FILE__, __LINE__);
1257 for (itim = 0; itim < ntim_int; itim++) {
1258 oz_tim = oz_int + itim * n_oz;
1262 if (last_time > oz_tim[0].anc_time) {
1263 fprintf(
stderr,
"-E- %s %d: ozone file times are out of sequence\n", __FILE__, __LINE__);
1266 last_time = oz_tim[0].anc_time;
1271 fprintf(
stderr,
"-E- %s %d: Error reading file: %s\n", __FILE__, __LINE__,
files[0]);
1280 l_time =
l1rec->scantime;
1284 for (iprm = 0; iprm < n_oz; iprm++) {
1286 for (itim = 0; itim < ntim_int; itim++)
1287 anc_times[itim] = oz_int[iprm + n_oz * itim].anc_time;
1294 if (
input->ozone != -2000) {
1295 for (ilon = 0; ilon <
npix; ilon++) {
1299 if (lon < -180.0 || lon > 180.0 || lat < -90.0 || lat > 90.0 || isnan(
lat) || isnan(
lon)) {
1302 if (
anc_acq_eval_pt(oz_int, iprm, ilvl,
lat,
lon, t_interp, data_ix, wt_t1, ntim_int, nlvl,
1303 n_oz, &
val, &unc) != 0) {
1304 fprintf(
stderr,
"-E- %s %d: Error interpolating file: %s\n", __FILE__, __LINE__,
1311 if (
l1rec->uncertainty)
1312 l1rec->uncertainty->doz[ilon] = unc / 1000.;
1333 int32_t ntime_step) {
1335 unsigned char *
qual;
1336 double *lat_coord, *lon_coord, *
ddata;
1339 static float *
data = 0;
1340 const char *rad_vars[] = {
"TAUTOT",
"CLDTOT"};
1341 const size_t n_rad_vars = nrad;
1342 for (
size_t iprm = 0; iprm < n_rad_vars; iprm++) {
1344 &
time, &lon_coord, &lat_coord) != 0)
1348 if ((
ddata = (
double *)malloc(nv *
sizeof(
double))) ==
NULL) {
1349 fprintf(
stderr,
"-E- %s %d: Unable to allocate array ddata\n", __FILE__, __LINE__);
1355 size_t time_slice_start = ntime_step *
ifile;
1356 if (ntime_step != ntime) {
1357 printf(
"Error reading time dimension\n");
1360 for (iv = 0; iv < nv; iv++)
1362 for (
size_t itim = 0; itim < ntime_step; itim++)
1365 gen_int_str *rad_tim = rad_int + (itim + time_slice_start) * n_rad_vars;
1366 rad_tim[iprm].accel_lat = gsl_interp_accel_alloc();
1367 rad_tim[iprm].accel_lon = gsl_interp_accel_alloc();
1368 rad_tim[iprm].int_id = gsl_spline2d_alloc(gsl_interp2d_bilinear,
nlon,
nlat);
1369 rad_tim[iprm].lat_coord = lat_coord;
1370 rad_tim[iprm].lon_coord = lon_coord;
1372 if (gsl_spline2d_init(rad_tim[iprm].int_id, rad_tim[iprm].lon_coord, rad_tim[iprm].lat_coord,
1374 fprintf(
stderr,
"-E- %s %d: GSL 2-D initialization failed, file: %s\n", __FILE__, __LINE__,
1382 rad_tim[iprm].anc_time = start_time +
time[itim] * 60.0;
1383 rad_tim[iprm].qual =
qual;
1384 rad_tim[iprm].nlat =
nlat;
1385 rad_tim[iprm].nlon =
nlon;
1421 char *ob_gmao_prm_nm[] = {
"U10M",
"V10M",
"SLP",
"TQV",
"PS",
"PS",
"PS",
"T10M",
"FRSEAICE",
"FRSNO"};
1424 int32_t n_raw_gmao = 10;
1425 int32_t iprm,
nlat,
nlon, nlvl, iv, nv;
1426 float *data2, *data3, *data_rh, p_lcl;
1427 unsigned char *
qual, *qual2;
1428 double time, *lat_coord, *lon_coord, *
ddata;
1429 double *lat_coord2, *lon_coord2;
1430 static float *
data = 0;
1449 for (iprm = 0; iprm < n_raw_gmao; iprm++) {
1452 &
nlat, &nlvl, &lon_coord, &lat_coord) != 0)
1456 if ((
ddata = (
double *)malloc(nv *
sizeof(
double))) ==
NULL) {
1457 fprintf(
stderr,
"-E- %s %d: Unable to allocate array ddata\n", __FILE__, __LINE__);
1468 for (iv = 0; iv < nv; iv++) {
1469 if (*(
qual + iv) == 0) {
1470 p_lcl = *(
data + iv);
1472 *(
data + iv) = p_lcl;
1478 &lon_coord2, &lat_coord2) != 0)
1482 for (iv = 0; iv < nv; iv++)
1483 *(
qual + iv) = *(
qual + iv) | *(qual2 + iv);
1487 &lon_coord2, &lat_coord2) != 0)
1491 for (iv = 0; iv < nv; iv++)
1492 *(
qual + iv) = *(
qual + iv) | *(qual2 + iv);
1495 if ((data_rh = (
float *)malloc(
nlon *
nlat *
sizeof(
float))) ==
NULL) {
1496 fprintf(
stderr,
"-E- %s, %d: malloc failure\n", __FILE__, __LINE__);
1504 for (iv = 0; iv < nv; iv++)
1505 *(
data + iv) = *(data_rh + iv);
1509 for (iv = 0; iv < nv; iv++) {
1510 if (*(
qual + iv) == 0) {
1511 p_lcl = *(
data + iv);
1513 *(
data + iv) = p_lcl;
1519 &lon_coord2, &lat_coord2) != 0)
1523 for (iv = 0; iv < nv; iv++)
1524 *(
qual + iv) = *(
qual + iv) | *(qual2 + iv);
1528 &lon_coord2, &lat_coord2) != 0)
1532 for (iv = 0; iv < nv; iv++)
1533 *(
qual + iv) = *(
qual + iv) | *(qual2 + iv);
1536 if ((data_rh = (
float *)malloc(
nlon *
nlat *
sizeof(
float))) ==
NULL) {
1537 fprintf(
stderr,
"-E- %s, %d: malloc failure\n", __FILE__, __LINE__);
1545 for (iv = 0; iv < nv; iv++)
1546 *(
data + iv) = *(data_rh + iv);
1550 for (iv = 0; iv < nv; iv++) {
1551 if (*(
qual + iv) == 0) {
1552 p_lcl = *(
data + iv);
1554 *(
data + iv) = p_lcl;
1563 for( iv=0; iv < nv; iv++ ) {
1564 if( *(
qual + iv) != 0 ) {
1571 fprintf(
stderr,
"-E- %s %d: Unknown output identifier: %d\n", __FILE__, __LINE__, iprm);
1577 met_int[iprm].accel_lat = gsl_interp_accel_alloc();
1578 met_int[iprm].accel_lon = gsl_interp_accel_alloc();
1579 met_int[iprm].int_id = gsl_spline2d_alloc(gsl_interp2d_bilinear,
nlon,
nlat);
1580 met_int[iprm].lat_coord = lat_coord;
1581 met_int[iprm].lon_coord = lon_coord;
1583 for (iv = 0; iv < nv; iv++)
1585 if (gsl_spline2d_init(met_int[iprm].int_id, met_int[iprm].lon_coord, met_int[iprm].lat_coord,
ddata,
1587 fprintf(
stderr,
"-E- %s %d: GSL 2-D initialization failed, file: %s\n", __FILE__, __LINE__,
file);
1594 met_int[iprm].anc_time =
time;
1595 met_int[iprm].qual =
qual;
1596 met_int[iprm].nlat =
nlat;
1597 met_int[iprm].nlon =
nlon;
1632 char *ob_gmao_prm_nm[] = {
"T",
"RH",
"H",
"QV",
"O3"};
1633 int32_t n_raw_gmao = 5;
1634 int32_t iprm,
nlat,
nlon, nlvl, ilvl,
loc, iv, nv, ntot;
1636 unsigned char *
qual;
1637 double time, *lat_coord, *lon_coord, *
ddata;
1638 static float *
data = 0;
1650 for (iprm = 0; iprm < n_raw_gmao; iprm++) {
1653 &lon_coord, &lat_coord) != 0)
1657 if (nlvl != nlvl_expect) {
1658 fprintf(
stderr,
"-E- %s %d: unexpected # profile levels: %d were read from file: %s\n", __FILE__,
1659 __LINE__, nlvl,
file);
1663 if (iprm ==
TPROF) {
1665 for (iv = 0; iv < ntot; iv++) {
1666 if (*(
qual + iv) == 0) {
1667 p_lcl = *(
data + iv);
1669 *(
data + iv) = p_lcl;
1673 else if (iprm ==
RHPROF) {
1675 for (iv = 0; iv < ntot; iv++) {
1676 if (*(
qual + iv) == 0) {
1677 *(
data + iv) *= 100.;
1683 if ((
ddata = (
double *)malloc(nv *
sizeof(
double))) ==
NULL) {
1684 fprintf(
stderr,
"-E- %s %d: Unable to allocate array ddata\n", __FILE__, __LINE__);
1687 for (ilvl = 0; ilvl < nlvl; ilvl++) {
1688 loc = iprm + n_raw_gmao * ilvl;
1693 prof_int[
loc].accel_lat = gsl_interp_accel_alloc();
1694 prof_int[
loc].accel_lon = gsl_interp_accel_alloc();
1695 prof_int[
loc].int_id = gsl_spline2d_alloc(gsl_interp2d_bilinear,
nlon,
nlat);
1696 prof_int[
loc].lat_coord = lat_coord;
1697 prof_int[
loc].lon_coord = lon_coord;
1699 for (iv = 0; iv < nv; iv++)
1701 if (gsl_spline2d_init(prof_int[
loc].int_id, prof_int[
loc].lon_coord, prof_int[
loc].lat_coord,
1703 fprintf(
stderr,
"-E- %s %d: GSL 2-D initialization failed, file: %s\n", __FILE__, __LINE__,
1707 prof_int[
loc].anc_time =
time;
1708 prof_int[
loc].qual =
qual + nv * ilvl;
1749 unsigned char *
qual;
1750 double time, *lat_coord, *lon_coord, *
ddata;
1751 static float *
data = 0;
1759 if ((
ddata = (
double *)malloc(nv *
sizeof(
double))) ==
NULL) {
1760 fprintf(
stderr,
"-E- %s %d: Unable to allocate array ddata\n", __FILE__, __LINE__);
1767 oz_int[iprm].accel_lat = gsl_interp_accel_alloc();
1768 oz_int[iprm].accel_lon = gsl_interp_accel_alloc();
1769 oz_int[iprm].int_id = gsl_spline2d_alloc(gsl_interp2d_bilinear,
nlon,
nlat);
1770 oz_int[iprm].lat_coord = lat_coord;
1771 oz_int[iprm].lon_coord = lon_coord;
1773 for (iv = 0; iv < nv; iv++)
1775 if (gsl_spline2d_init(oz_int[iprm].int_id, oz_int[iprm].lon_coord, oz_int[iprm].lat_coord,
ddata,
nlon,
1777 fprintf(
stderr,
"-E- %s %d: GSL 2-D initialization failed, file: %s\n", __FILE__, __LINE__,
file);
1783 oz_int[iprm].anc_time =
time;
1784 oz_int[iprm].qual =
qual;
1785 oz_int[iprm].nlat =
nlat;
1786 oz_int[iprm].nlon =
nlon;
1797 unsigned char *
qual;
1798 double time, *lat_coord, *lon_coord, *
ddata;
1799 static float *
data = 0;
1802 char *ob_gmao_prm_nm[] = {
"BCEXTTAU",
"BCSCATAU",
"DUEXTTAU",
"DUSCATAU",
"SSEXTTAU",
1803 "SSSCATAU",
"SUEXTTAU",
"SUSCATAU",
"OCEXTTAU",
"OCSCATAU",
1804 "TOTEXTTAU",
"TOTSCATAU",
"TOTANGSTR"};
1806 int32_t n_raw_gmao = 13;
1808 for (iprm = 0; iprm < n_raw_gmao; iprm++) {
1810 &lon_coord, &lat_coord) != 0)
1814 if ((
ddata = (
double *)malloc(nv *
sizeof(
double))) ==
NULL) {
1815 fprintf(
stderr,
"-E- %s %d: Unable to allocate array ddata\n", __FILE__, __LINE__);
1822 aer_int[iprm].accel_lat = gsl_interp_accel_alloc();
1823 aer_int[iprm].accel_lon = gsl_interp_accel_alloc();
1824 aer_int[iprm].int_id = gsl_spline2d_alloc(gsl_interp2d_bilinear,
nlon,
nlat);
1825 aer_int[iprm].lat_coord = lat_coord;
1826 aer_int[iprm].lon_coord = lon_coord;
1828 for (iv = 0; iv < nv; iv++)
1830 if (gsl_spline2d_init(aer_int[iprm].int_id, aer_int[iprm].lon_coord, aer_int[iprm].lat_coord,
ddata,
1832 fprintf(
stderr,
"-E- %s %d: GSL 2-D initialization failed, file: %s\n", __FILE__, __LINE__,
file);
1838 aer_int[iprm].anc_time =
time;
1839 aer_int[iprm].qual =
qual;
1840 aer_int[iprm].nlat =
nlat;
1841 aer_int[iprm].nlon =
nlon;
1849 int32_t
anc_rad_eval_pt(gen_int_str *rad_int, int32_t iprm, int32_t itim, int32_t nrad,
float lat,
float lon,
1851 gen_int_str *rad_tim = rad_int + (itim)*nrad;
1853 gsl_interp_accel *accel_lon = rad_tim[iprm].accel_lon;
1854 gsl_interp_accel *accel_lat = rad_tim[iprm].accel_lat;
1855 if (gsl_spline2d_eval_e(rad_tim[iprm].int_id,
lon,
lat, accel_lon, accel_lat, &val_get) != 0) {
1856 fprintf(
stderr,
"-E- %s %d: gsl_spline2d_eval_e error: %d, %d \n", __FILE__, __LINE__, itim, iprm);
1859 size_t nlat = rad_tim[iprm].nlat;
1860 size_t nlon = rad_tim[iprm].nlon;
1862 size_t ilat = gsl_interp_accel_find(accel_lat, rad_tim[iprm].lat_coord,
nlat,
lat);
1863 size_t ilon = gsl_interp_accel_find(accel_lon, rad_tim[iprm].lon_coord,
nlon,
lon);
1864 if ((rad_tim[iprm].
qual[ilon +
nlon * ilat] == 1) || (rad_tim[iprm].
qual[ilon + 1 +
nlon * ilat] == 1) ||
1865 (rad_tim[iprm].
qual[ilon +
nlon * (ilat + 1)] == 1) ||
1866 (rad_tim[iprm].
qual[ilon + 1 +
nlon * (ilat + 1)] == 1))
1873 int32_t t_interp, int32_t *data_ix,
float wt_t1, int32_t ntim_int, int32_t nlvl,
1874 int32_t nprm,
float *final_val,
float *unc)
1917 gsl_interp_accel *accel_lat, *accel_lon;
1918 int32_t met_ptr, tim_ix, itim, ntim;
1924 ntim = t_interp + 1;
1926 for (itim = 0; itim < ntim; itim++) {
1927 tim_ix = data_ix[itim];
1928 met_ptr = iprm + nprm * (ilvl + nlvl * tim_ix);
1929 accel_lon = met_int[met_ptr].accel_lon;
1930 accel_lat = met_int[met_ptr].accel_lat;
1931 nlat = met_int[met_ptr].nlat;
1932 nlon = met_int[met_ptr].nlon;
1934 if (gsl_spline2d_eval_e(met_int[met_ptr].int_id,
lon,
lat, accel_lon, accel_lat, &
val) != 0) {
1935 fprintf(
stderr,
"-E- %s %d: gsl_spline2d_eval_e error: %d, %d, %d\n", __FILE__, __LINE__, itim,
1940 ilat = gsl_interp_accel_find(accel_lat, met_int[met_ptr].lat_coord,
nlat,
lat);
1941 ilon = gsl_interp_accel_find(accel_lon, met_int[met_ptr].lon_coord,
nlon,
lon);
1943 if ((met_int[met_ptr].
qual[ilon +
nlon * ilat] == 1) ||
1944 (met_int[met_ptr].
qual[ilon + 1 +
nlon * ilat] == 1) ||
1945 (met_int[met_ptr].
qual[ilon +
nlon * (ilat + 1)] == 1) ||
1946 (met_int[met_ptr].
qual[ilon + 1 +
nlon * (ilat + 1)] == 1))
1956 *unc = fabsf(*final_val - (
float)
val);
1957 *final_val = *final_val * wt_t1 +
val * wt_t2;
1969 int32_t *data_ix,
float *wt)
2007 int32_t data1_ix = 0, data2_ix = 0;
2015 if (s_time < anc_time[0]) {
2020 }
else if (s_time > anc_time[1]) {
2029 *wt = (anc_time[1] - s_time) / (anc_time[1] - anc_time[0]);
2032 if (s_time < anc_time[0]) {
2036 }
else if (s_time > anc_time[2]) {
2046 *wt = (anc_time[2] - s_time) / (anc_time[2] - anc_time[0]);
2049 if (s_time < anc_time[0]) {
2053 }
else if (s_time > anc_time[2]) {
2057 }
else if (s_time < anc_time[1]) {
2062 *wt = (anc_time[1] - s_time) / (anc_time[1] - anc_time[0]);
2068 *wt = (anc_time[2] - s_time) / (anc_time[2] - anc_time[1]);
2073 printf(
"%s, %d: Undefined anc_f_stat - should not happen\n", __FILE__, __LINE__);
2076 *data_ix = data1_ix;
2077 *(data_ix + 1) = data2_ix;
2098 double *start_time, int32_t *ntime, int32_t *
nlon, int32_t *
nlat,
int **
time,
2099 double **lon_coord,
double **lat_coord) {
2100 static char lcl_fil[FILENAME_MAX] =
"";
2102 int var_id, var_id_lat, var_id_lon, var_id_time;
2104 int32_t nv, nlon_pre, il, ip;
2105 float *data_tmp, fillv, missv;
2107 if (strcmp(
file, lcl_fil) != 0) {
2108 if (lcl_fil[0] != 0) {
2109 if (nc_close(ncid) != NC_NOERR) {
2110 fprintf(
stderr,
"-E- %s %d: nc_close error, file: %s\n", __FILE__, __LINE__,
file);
2114 if (nc_open(
file, 0, &ncid) != NC_NOERR) {
2115 fprintf(
stderr,
"-E- %s %d: file: %s is not netcdf, not acceptable GMAO file\n", __FILE__,
2124 fprintf(
stderr,
"-E- %s %d: file: %s error reading lat, lon and time dimension size\n", __FILE__,
2128 *
nlon = nlon_pre + 1;
2131 memset(isodate,
'\0', 32);
2132 nc_get_att_text(ncid, NC_GLOBAL,
"time_coverage_start", isodate);
2135 if (((*
data = (
float *)malloc(nv *
sizeof(
float))) ==
NULL) ||
2136 ((*lon_coord = (
double *)malloc(*
nlon *
sizeof(
double))) ==
NULL) ||
2137 ((*lat_coord = (
double *)malloc(*
nlat *
sizeof(
double))) ==
NULL) ||
2138 ((*
time = (
int *)malloc(*ntime *
sizeof(
int))) ==
NULL) ||
2139 ((*qa = (
unsigned char *)calloc(nv,
sizeof(
char))) ==
NULL) ||
2140 ((data_tmp = (
float *)malloc(nlon_pre * *
nlat * *ntime *
sizeof(
float))) ==
NULL)) {
2141 fprintf(
stderr,
"-E- %s %d: file: %s error allocating gmao parameter allocation\n", __FILE__,
2145 if ((nc_inq_varid(ncid, var_name, &var_id) != NC_NOERR) ||
2146 (nc_inq_varid(ncid,
"lat", &var_id_lat) != NC_NOERR) ||
2147 (nc_inq_varid(ncid,
"lon", &var_id_lon) != NC_NOERR) ||
2148 (nc_inq_varid(ncid,
"time", &var_id_time) != NC_NOERR)) {
2149 fprintf(
stderr,
"-E- %s %d: file: %s error setting an id for product: %s\n", __FILE__, __LINE__,
file,
2153 if (((nc_get_var_double(ncid, var_id_lat, *lat_coord)) != NC_NOERR) ||
2154 ((nc_get_var_double(ncid, var_id_lon, *lon_coord)) != NC_NOERR) ||
2155 ((nc_get_var_int(ncid, var_id_time, *
time)) != NC_NOERR)) {
2156 fprintf(
stderr,
"-E- %s %d: file: %s error reading the scales and parameter\n", __FILE__, __LINE__,
2163 if ((nc_get_att_float(ncid, var_id,
"_FillValue", &fillv) != NC_NOERR) ||
2164 (nc_get_att_float(ncid, var_id,
"missing_value", &missv) != NC_NOERR)) {
2165 printf(
"%s, %d: nc_get_att_float error for parm %s\n", __FILE__, __LINE__, var_name);
2169 if (nc_get_var_float(ncid, var_id, data_tmp) != NC_NOERR) {
2170 printf(
"%s, %d: nc_get_var_float error for parm %s\n", __FILE__, __LINE__, var_name);
2177 for (int32_t it = 0; it < *ntime; it++) {
2178 for (il = 0; il < *
nlat; il++) {
2179 for (ip = 0; ip < *
nlon; ip++) {
2181 if (ip < nlon_pre) {
2182 *(*
data +
loc) = *(data_tmp + ip + nlon_pre * (il + *
nlat * it));
2184 *(*
data +
loc) = *(data_tmp + nlon_pre * (il + *
nlat * it));
2194 (*lon_coord)[nlon_pre] = 180.;
2198 float **
data,
unsigned char **qa,
double *
time, int32_t *
nlon, int32_t *
nlat,
2199 int32_t *nlvl,
double **lon_coord,
double **lat_coord)
2233 static char lcl_fil[FILENAME_MAX] =
"";
2235 int var_id, var2_id, var3_id, dim_id;
2237 int32_t nv, ilvl, nlon_pre, il, ip;
2239 float *data_tmp, fillv, missv;
2242 if (strcmp(
file, lcl_fil) != 0) {
2243 if (lcl_fil[0] != 0) {
2244 if (nc_close(ncid) != NC_NOERR) {
2245 fprintf(
stderr,
"-E- %s %d: nc_close error, file: %s\n", __FILE__, __LINE__,
file);
2249 if (nc_open(
file, 0, &ncid) != NC_NOERR) {
2250 fprintf(
stderr,
"-E- %s %d: file: %s is not netcdf, not acceptable GMAO file\n", __FILE__,
2258 fprintf(
stderr,
"-E- %s %d: file: %s error reading lat, lon dimension size\n", __FILE__, __LINE__,
2263 if (nc_inq_dimid(ncid,
"lev", &dim_id) != NC_NOERR) {
2266 if (nc_inq_dimlen(ncid, dim_id, &tlvl) != NC_NOERR) {
2267 fprintf(
stderr,
"-E- %s %d: file: %s error reading level dimension size\n", __FILE__, __LINE__,
2273 *
nlon = nlon_pre + 1;
2278 memset(isodate,
'\0', 32);
2279 nc_get_att_text(ncid, NC_GLOBAL,
"time_coverage_start", isodate);
2283 if (((*
data = (
float *)malloc(nv *
sizeof(
float))) ==
NULL) ||
2284 ((*lon_coord = (
double *)malloc(*
nlon *
sizeof(
double))) ==
NULL) ||
2285 ((*lat_coord = (
double *)malloc(*
nlat *
sizeof(
double))) ==
NULL) ||
2286 ((*qa = (
unsigned char *)calloc(nv,
sizeof(
char))) ==
NULL) ||
2287 ((data_tmp = (
float *)malloc(nlon_pre * *
nlat * *nlvl *
sizeof(
float))) ==
NULL)) {
2288 fprintf(
stderr,
"-E- %s %d: file: %s error allocating gmao parameter allocation\n", __FILE__,
2294 if ((nc_inq_varid(ncid, ds_name, &var_id) != NC_NOERR) ||
2295 (nc_inq_varid(ncid,
"lat", &var2_id) != NC_NOERR) ||
2296 (nc_inq_varid(ncid,
"lon", &var3_id) != NC_NOERR)) {
2297 fprintf(
stderr,
"-E- %s %d: file: %s error setting an id for product: %s\n", __FILE__, __LINE__,
file,
2302 if (((nc_get_var_double(ncid, var2_id, *lat_coord)) != NC_NOERR) ||
2303 ((nc_get_var_double(ncid, var3_id, *lon_coord)) != NC_NOERR)) {
2304 fprintf(
stderr,
"-E- %s %d: file: %s error reading the scales and parameter\n", __FILE__, __LINE__,
2311 if ((nc_get_att_float(ncid, var_id,
"_FillValue", &fillv) != NC_NOERR) ||
2312 (nc_get_att_float(ncid, var_id,
"missing_value", &missv) != NC_NOERR)) {
2313 printf(
"%s, %d: nc_get_att_float error for parm %s\n", __FILE__, __LINE__, ds_name);
2319 if (nc_get_var_float(ncid, var_id, data_tmp) != NC_NOERR) {
2320 printf(
"%s, %d: nc_get_var_float error for parm %s\n", __FILE__, __LINE__, ds_name);
2323 for (ilvl = 0; ilvl < *nlvl; ilvl++) {
2324 for (il = 0; il < *
nlat; il++) {
2325 for (ip = 0; ip < *
nlon; ip++) {
2327 if (ip < nlon_pre) {
2328 *(*
data +
loc) = *(data_tmp + ip + nlon_pre * (il + *
nlat * ilvl));
2330 *(*
data +
loc) = *(data_tmp + nlon_pre * (il + *
nlat * ilvl));
2341 (*lon_coord)[nlon_pre] = 180.;
2413 int ids, iprm,
npix,
nlin, ilin, ipix;
2414 int t_days, t_hrs, lon_gt_180, ird;
2415 int dstpix, npix0, nlin0,
status;
2416 int npix_ext, nlin_ext, ntim, f12_mtch, f23_mtch,
anc_f_stat;
2417 int ncid, iprm_sto, nprm_sto;
2420 float *base_data, *
lat, *
lon, *comp1, *comp2;
2424 nprm_sto = (n_prm > 1) ? n_prm - 1 : n_prm;
2445 for (ids = 0; ids < 3; ids++) {
2452 if ((ids == 1) && (f12_mtch == 1))
2454 if ((ids == 2) && (f23_mtch == 1))
2457 if (Hishdf(
files[ids]))
2462 if (
status != NC_NOERR) {
2471 printf(
"%s, %d: nc_open failed on file: %s\n", __FILE__, __LINE__,
files[ids]);
2472 printf(
" Mismatch or bad ECMWF file\n");
2480 if (((npix0 =
ncio_dim_siz(ncid,
"longitude")) == -1) ||
2482 printf(
"%s, %d: ncio_dim_siz error reading longitude, latitude or time datasets\n", __FILE__,
2490 if ((
npix != npix0) || (
nlin != nlin0)) {
2491 printf(
"%s, %d: mismatch in size of MET array[%d]\n", __FILE__, __LINE__, ids);
2499 printf(
"%s, %d: Number of times > 1, can't deal with at this time\n", __FILE__, __LINE__);
2502 npix_ext =
npix + 2;
2503 nlin_ext =
nlin + 2;
2507 for (iprm = 0; iprm < nprm_sto; iprm++) {
2508 if ((met_sto[iprm + sto_ix].
data[ids] = (
float *)malloc(npix_ext * nlin_ext *
sizeof(
float))) ==
2510 printf(
"%s, %d: malloc failed for data[%d] in met_sto %d\n", __FILE__, __LINE__, ids, iprm);
2519 if (((base_data = (
float *)malloc(
npix *
nlin *
sizeof(
float))) ==
NULL) ||
2520 ((comp1 = (
float *)malloc(
npix *
nlin *
sizeof(
float))) ==
NULL) ||
2521 ((comp2 = (
float *)malloc(
npix *
nlin *
sizeof(
float))) ==
NULL)) {
2522 printf(
"%s, %d: malloc failed for base_data or comp arrays\n", __FILE__, __LINE__);
2526 if ((
lat = (
float *)malloc(
nlin *
sizeof(
float))) ==
NULL) {
2527 printf(
"%s, %d: malloc failed for latitude\n", __FILE__, __LINE__);
2530 if ((
lon = (
float *)malloc(
npix *
sizeof(
float))) ==
NULL) {
2531 printf(
"%s, %d: malloc failed for longitude\n", __FILE__, __LINE__);
2535 printf(
"%s, %d: ncio_grab_f_ds failed on latitude\n", __FILE__, __LINE__);
2539 printf(
"%s, %d: ncio_grab_f_ds failed on longitude\n", __FILE__, __LINE__);
2551 for (ipix = 0; ipix <
npix; ipix++) {
2552 if (
lon[ipix] > 180.) {
2565 printf(
"%s, %d: error reading the time in\n", __FILE__, __LINE__);
2578 for (iprm = 0; iprm < nprm_sto; iprm++) {
2579 iprm_sto = iprm + sto_ix;
2582 printf(
"%s, %d: ncio_grab_stdsclf_ds failed on %s\n", __FILE__, __LINE__, prm_nm[ird]);
2589 printf(
"%s, %d: ncio_grab_stdsclf_ds failed on %s or %s\n", __FILE__, __LINE__,
2590 prm_nm[ird], prm_nm[ird + 1]);
2599 printf(
"met_cvt_ttd_to_rh had an error\n");
2600 printf(
"%s, %d: met_cvt_ttd_to_rh failure\n", __FILE__, __LINE__);
2605 for (ilin = 0; ilin <
nlin; ilin++) {
2606 for (ipix = 0; ipix <
npix; ipix++) {
2607 dstpix = ipix - lon_gt_180;
2610 *(met_sto[iprm_sto].data[ids] + dstpix + 1 + (ilin + 1) * npix_ext) =
2611 *(base_data + ipix +
npix * ilin);
2616 for (ipix = 0; ipix <
npix; ipix++) {
2617 *(met_sto[iprm_sto].data[ids] + ipix + 1) =
2618 *(met_sto[iprm_sto].
data[ids] + ipix + 1 + npix_ext);
2619 *(met_sto[iprm_sto].data[ids] + ipix + 1 + (
nlin + 1) * npix_ext) =
2620 *(met_sto[iprm_sto].data[ids] + ipix + 1 +
nlin * npix_ext);
2623 for (ilin = 0; ilin < nlin_ext; ilin++) {
2624 *(met_sto[iprm_sto].data[ids] + ilin * npix_ext) =
2625 *(met_sto[iprm_sto].
data[ids] +
npix + ilin * npix_ext);
2626 *(met_sto[iprm_sto].data[ids] +
npix + 1 + ilin * npix_ext) =
2627 *(met_sto[iprm_sto].
data[ids] + 1 + ilin * npix_ext);
2630 met_sto[iprm_sto].s_lon =
s_lon;
2631 met_sto[iprm_sto].lon_step =
lon_step;
2632 met_sto[iprm_sto].nlon =
npix;
2633 met_sto[iprm_sto].e_lon =
e_lon;
2634 met_sto[iprm_sto].s_lat =
s_lat;
2635 met_sto[iprm_sto].lat_step =
lat_step;
2636 met_sto[iprm_sto].nlat =
nlin;
2637 met_sto[iprm_sto].e_lat =
e_lat;
2638 met_sto[iprm_sto].data_time[ids] =
data_time;
2675 float data_val, uwnd, vwnd, data_val1, data_val2, dx, dy, lon_frac, lat_frac;
2676 float trg_lon, trg_lat, wt_t1, wt_t2,
s_lon,
s_lat;
2678 int iprm, xbox_st, ybox_st, nx, ny, t_interp, data1_ix, data2_ix,
anc_f_stat;
2679 int npix, ipix, sto_st, sto_en;
2680 static float r2d = 57.29577951;
2684 if (anc_class == 0) {
2698 l_time += sec / 86400.0;
2717 if (l_time < met_sto[sto_st].
data_time[0]) {
2718 printf(
"%s, %d: data time is before the ancillary data start time\n", __FILE__, __LINE__);
2720 }
else if (l_time > met_sto[sto_st].
data_time[1]) {
2729 wt_t1 = (met_sto[sto_st].data_time[1] - l_time) /
2731 wt_t2 = (l_time - met_sto[sto_st].data_time[0]) /
2735 if (l_time < met_sto[sto_st].
data_time[0]) {
2739 }
else if (l_time > met_sto[sto_st].
data_time[2]) {
2741 printf(
"%s, %d: data time is after the ancillary data end time\n", __FILE__, __LINE__);
2748 wt_t1 = (met_sto[sto_st].data_time[2] - l_time) /
2750 wt_t2 = (l_time - met_sto[sto_st].data_time[0]) /
2754 if (l_time < met_sto[sto_st].
data_time[0]) {
2755 printf(
"%s, %d: data time is before the ancillary data start time\n", __FILE__, __LINE__);
2757 }
else if (l_time > met_sto[sto_st].
data_time[2]) {
2758 printf(
"%s, %d: data time is after the ancillary data end time\n", __FILE__, __LINE__);
2760 }
else if (l_time < met_sto[sto_st].
data_time[1]) {
2765 wt_t1 = (met_sto[sto_st].data_time[1] - l_time) /
2767 wt_t2 = (l_time - met_sto[sto_st].data_time[0]) /
2774 wt_t1 = (met_sto[sto_st].data_time[2] - l_time) /
2776 wt_t2 = (l_time - met_sto[sto_st].data_time[1]) /
2782 printf(
"%s, %d: Undefined anc_f_stat - should not happen\n", __FILE__, __LINE__);
2793 dx = met_sto[sto_st].lon_step;
2794 dy = met_sto[sto_st].lat_step;
2795 s_lon = met_sto[sto_st].s_lon;
2796 s_lat = met_sto[sto_st].s_lat;
2797 nx = met_sto[sto_st].nlon;
2798 ny = met_sto[sto_st].nlat;
2799 for (ipix = 0; ipix <
npix; ipix++) {
2800 trg_lat =
l1rec->lat[ipix];
2801 trg_lon =
l1rec->lon[ipix];
2813 xbox_st =
MAX(
MIN((
int)((trg_lon -
s_lon + dx) / dx), nx + 1), 0);
2814 ybox_st =
MAX(
MIN((
int)((trg_lat -
s_lat + dy) / dy), ny + 1), 0);
2816 lon_frac = (trg_lon -
s_lon) / dx - (
float)(xbox_st - 1);
2817 lat_frac = (trg_lat -
s_lat) / dy - (
float)(ybox_st - 1);
2819 for (iprm = sto_st; iprm < (sto_en + 1); iprm++) {
2820 data = met_sto[iprm].data[data1_ix];
2821 data_val1 =
bilin_interp(
data, xbox_st, (nx + 2), ybox_st, lon_frac, lat_frac);
2823 if (t_interp == 1) {
2824 data = met_sto[iprm].data[data2_ix];
2825 data_val2 =
bilin_interp(
data, xbox_st, (nx + 2), ybox_st, lon_frac, lat_frac);
2830 if (data_val1 <
ANCBAD + 1) {
2831 if (data_val2 <
ANCBAD + 1)
2834 data_val = data_val2;
2836 data_val = wt_t1 * data_val1 + wt_t2 * data_val2;
2838 data_val = data_val1;
2851 if (
input->pressure != -2000) {
2852 data_val = (data_val <
ANCBAD + 1) ?
ANCBAD : data_val / 100.;
2854 l1rec->pr[ipix] = 1013.25;
2855 else if (data_val < 250.)
2856 l1rec->pr[ipix] = 250.;
2857 else if (data_val > 1100.)
2858 l1rec->pr[ipix] = 1100.;
2860 l1rec->pr[ipix] = data_val;
2866 if (
input->watervapor != -2000)
2867 l1rec->wv[ipix] = (data_val <
ANCBAD + 1) ? 0. : data_val / 10.;
2874 if (
input->pressure != -2000) {
2875 data_val = (data_val <
ANCBAD + 1) ?
ANCBAD : data_val / 100.;
2877 l1rec->pr[ipix] = 1013.25;
2878 else if (data_val < 900.)
2879 l1rec->pr[ipix] = 900.;
2880 else if (data_val > 1100.)
2881 l1rec->pr[ipix] = 1100.;
2883 l1rec->pr[ipix] = data_val;
2887 if (proc_land &&
l1rec->height[ipix] != 0.0)
2888 l1rec->pr[ipix] *= exp(-
l1rec->height[ipix] / 8434);
2892 uwnd = (data_val <
ANCBAD + 1) ? 0. : data_val;
2893 l1rec->zw[ipix] = uwnd;
2896 vwnd = (data_val <
ANCBAD + 1) ? 0. : data_val;
2897 l1rec->mw[ipix] = vwnd;
2898 if (
input->windspeed != -2000)
2899 l1rec->ws[ipix] = sqrt(pow(uwnd, 2.) + pow(vwnd, 2.));
2900 if (
input->windangle != -2000)
2901 l1rec->wd[ipix] = atan2f(-uwnd, vwnd) * r2d;
2904 if (
input->relhumid != -2000)
2905 l1rec->rh[ipix] = (data_val <
ANCBAD + 1) ? 0. : data_val;
2908 if (
input->ozone != -2000) {
2945 static int ncid[2], varid[5];
2955 static int32_t firstcalls[2] = {1, 1}, pix_smp[2];
2956 static int32_t
npix, *
qual, *qual_met, *qual_oz;
2958 static size_t tie_nlin[2], tie_npix[2];
2959 static float *tie_data, *tie_met, *tie_oz;
2960 static float r2d = 57.29577951;
2961 int32_t anc_field_per_parm[5] = {1, 2, 1, 1, 1};
2962 int32_t anc_class_n_ds[2] = {4, 1}, class_off, n_ds_prm, ptr_prm;
2963 int32_t n_field_per_parm, ifld, ipx, px_tie1, px_tie2;
2964 int nid, lin_smp, ids, stat, ipx_dat;
2966 char *anc_prm_nm[] = {
"humidity",
"horizontal_wind",
"sea_level_pressure",
"total_columnar_water_vapour",
2968 float *ar, fr_dist, w1, w2;
2979 n_ds_prm = anc_class_n_ds[anc_class];
2981 if (firstcalls[anc_class]) {
2989 if (nc_open(
file, NC_NOWRITE, (ncid + anc_class)) != NC_NOERR) {
2990 fprintf(
stderr,
"-E- %s %d: Unable to open OLCI tie point anc file: %s\n", __FILE__, __LINE__,
2994 if (nc_get_att_int(ncid[anc_class], NC_GLOBAL,
"ac_subsampling_factor", &pix_smp[anc_class]) !=
2997 "-E- %s %d: Unable to read column sampling attrib from OLCI tie point anc file: %s\n",
2998 __FILE__, __LINE__,
file);
3001 if (nc_get_att_int(ncid[anc_class], NC_GLOBAL,
"al_subsampling_factor", &lin_smp) != NC_NOERR) {
3003 "-E- %s %d: Unable to read row sampling attrib from OLCI tie point anc file: %s\n",
3004 __FILE__, __LINE__,
file);
3010 if (nc_inq_dimid(ncid[anc_class],
"tie_columns", &nid) != NC_NOERR) {
3011 fprintf(
stderr,
"-E- %s %d: Unable to get OLCI tie point meteo # pixels\n", __FILE__, __LINE__);
3014 if (nc_inq_dimlen(ncid[anc_class], nid, &tie_npix[anc_class]) != NC_NOERR) {
3015 fprintf(
stderr,
"-E- %s %d: Unable to get OLCI tie point meteo # pixels\n", __FILE__, __LINE__);
3019 if (nc_inq_dimid(ncid[anc_class],
"tie_rows", &nid) != NC_NOERR) {
3020 fprintf(
stderr,
"-E- %s %d: Unable to get OLCI tie point meteo # lines\n", __FILE__, __LINE__);
3023 if (nc_inq_dimlen(ncid[anc_class], nid, &tie_nlin[anc_class]) != NC_NOERR) {
3024 fprintf(
stderr,
"-E- %s %d: Unable to get OLCI tie point meteo # lines\n", __FILE__, __LINE__);
3031 fprintf(
stderr,
"-E- %s %d: OLCI and tie point line sampling: %d, not = 1\n", __FILE__, __LINE__,
3035 tie_epix = pix_smp[anc_class] * (tie_npix[anc_class] - 1) + 1;
3036 if (tie_epix <
epix) {
3037 fprintf(
stderr,
"-E- %s %d: tie point range out to pixel %d is < data range of %d\n", __FILE__,
3038 __LINE__, tie_epix,
epix);
3039 fprintf(
stderr,
"tie point sampling: %d and # pixels: %d\n", pix_smp[anc_class],
3040 (
int)tie_npix[anc_class]);
3056 if ((tie_data = (
float *)malloc(tie_npix[anc_class] *
sizeof(
float))) ==
NULL) {
3057 fprintf(
stderr,
"-E- %s %d: Unable to allocate tie point data array\n", __FILE__, __LINE__);
3066 if ((
qual = (int32_t *)malloc(tie_npix[anc_class] *
sizeof(int32_t))) ==
NULL) {
3067 fprintf(
stderr,
"-E- %s %d: Unable to allocate tie point quality array\n", __FILE__, __LINE__);
3078 for (ids = 0; ids < n_ds_prm; ids++) {
3079 ptr_prm = ids + class_off;
3080 if (nc_inq_varid(ncid[anc_class], anc_prm_nm[ptr_prm], &varid[ptr_prm]) != NC_NOERR) {
3081 fprintf(
stderr,
"-E- %s %d: Can't find id for OLCI anc tie point dataset: %s\n", __FILE__,
3082 __LINE__, anc_prm_nm[ptr_prm]);
3085 if (nc_get_att_float(ncid[anc_class], varid[ptr_prm],
"_FillValue", &fill[ptr_prm]) != NC_NOERR) {
3086 fprintf(
stderr,
"-E- %s %d: Can't get _FillValue for OLCI anc tie point dataset: %s\n",
3087 __FILE__, __LINE__, anc_prm_nm[ptr_prm]);
3091 if (nc_get_att_float(ncid[anc_class], varid[ptr_prm],
"valid_min", &
valid_min[ptr_prm]) !=
3093 fprintf(
stderr,
"-E- %s %d: Can't get valid_min for OLCI anc tie point dataset: %s\n",
3094 __FILE__, __LINE__, anc_prm_nm[ptr_prm]);
3097 if (nc_get_att_float(ncid[anc_class], varid[ptr_prm],
"valid_max", &
valid_max[ptr_prm]) !=
3099 fprintf(
stderr,
"-E- %s %d: Can't get valid_max for OLCI anc tie point dataset: %s\n",
3100 __FILE__, __LINE__, anc_prm_nm[ptr_prm]);
3104 firstcalls[anc_class] = 0;
3112 for (ids = 0; ids < n_ds_prm; ids++) {
3113 ptr_prm = ids + class_off;
3140 count[1] = tie_npix[anc_class];
3144 tie_data = (anc_class == 0) ? tie_met : tie_oz;
3145 qual = (anc_class == 0) ? qual_met : qual_oz;
3147 n_field_per_parm = anc_field_per_parm[ptr_prm];
3148 for (ifld = 0; ifld < n_field_per_parm; ifld++) {
3157 if (
iscan < tie_nlin[anc_class]) {
3159 if ((stat = nc_get_vara_float(ncid[anc_class], varid[ptr_prm],
start,
count, tie_data)) !=
3161 fprintf(
stderr,
"-E- %s %d: Can't read OLCI anc tie point line, dataset: %s\n", __FILE__,
3162 __LINE__, anc_prm_nm[ptr_prm]);
3166 for (ipx = 0; ipx < tie_npix[anc_class]; ipx++) {
3167 if ((*(tie_data + ipx) == fill[ptr_prm]) || (*(tie_data + ipx) <
valid_min[ptr_prm]) ||
3168 (*(tie_data + ipx) >
valid_max[ptr_prm]))
3173 *(tie_data + ipx) *= 0.1;
3176 *(tie_data + ipx) *= 46.698;
3181 for (ipx = 0; ipx <
npix; ipx++) {
3184 px_tie1 = ipx_dat / pix_smp[anc_class];
3185 px_tie2 = px_tie1 + 1;
3187 if (px_tie2 > (tie_npix[anc_class] - 1)) {
3191 if ((*(
qual + px_tie1) == 1) || (*(
qual + px_tie2) == 1)) {
3197 fr_dist = (
float)(ipx_dat - px_tie1 * pix_smp[anc_class]) / (
float)pix_smp[anc_class];
3198 *(ar + ipx) = tie_data[px_tie1] * (1. - fr_dist) + tie_data[px_tie2] * fr_dist;
3203 for (ipx = 0; ipx <
npix; ipx++) {
3213 for (ipx = 0; ipx <
npix; ipx++) {
3214 w1 =
l1rec->zw[ipx];
3215 w2 =
l1rec->mw[ipx];
3216 if (
input->windspeed != -2000)
3217 l1rec->ws[ipx] = sqrt(w1 * w1 + w2 * w2);
3218 if (
input->windangle != -2000)
3219 l1rec->wd[ipx] = atan2f(-w1, w2) * r2d;
3226 for (ipx = 0; ipx <
npix; ipx++) {
3227 if (
l1rec->height[ipx] != 0.0)
3228 l1rec->pr[ipx] *= exp(-
l1rec->height[ipx] / 8434);
3279 float bilin_interp(
float *
data,
int xbox_st,
int nx,
int ybox_st,
float xfrac,
float yfrac)
3311 if ((*(
data + xbox_st + nx * ybox_st) < (
ANCBAD + 1)) ||
3312 (*(
data + xbox_st + nx * (ybox_st + 1)) < (
ANCBAD + 1)) ||
3313 (*(
data + (xbox_st + 1) + nx * ybox_st) < (
ANCBAD + 1)) ||
3314 (*(
data + (xbox_st + 1) + nx * (ybox_st + 1)) < (
ANCBAD + 1)))
3317 data_val = (1 - xfrac) * (1 - yfrac) * *(
data + xbox_st + nx * ybox_st) +
3318 (1 - xfrac) * yfrac * *(
data + xbox_st + nx * (ybox_st + 1)) +
3319 xfrac * (1 - yfrac) * *(
data + (xbox_st + 1) + nx * ybox_st) +
3320 xfrac * yfrac * *(
data + (xbox_st + 1) + nx * (ybox_st + 1));
3349 int64_t lyear, lmonth, lday,
jday;
3351 lyear = (int64_t)
year;
3352 lmonth = (int64_t)month;
3353 lday = (int64_t)
day;
3354 jday = (367 * lyear - 7 * (lyear + (lmonth + 9) / 12) / 4 + 275 * lmonth / 9 + lday + 1721014);
3359 jday =
jday + 15 - 3 * ((lyear + (lmonth - 9) / 7) / 100 + 1) / 4;
3389 int64_t v1,
v2, v3, v4;
3391 v2 = 4 * v1 / 146097;
3392 v1 = v1 - (146097 *
v2 + 3) / 4;
3393 v3 = 4000 * (v1 + 1) / 1461001;
3394 v1 = v1 - 1461 * v3 / 4 + 31;
3395 v4 = 80 * v1 / 2447;
3396 *
day = v1 - 2447 * v4 / 80;
3398 *month = v4 + 2 - 12 * v1;
3399 *
year = 100 * (
v2 - 49) + v3 + v1;