Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
}
if (!is.null(attribute_backup[['variables']][[var]][['dim']][[dim]][['vals']])) {
if (!result_is_irregular) {
attribute_backup[['variables']][[var]][['dim']][[dim]][['vals']] <- new_vals
} else {
attribute_backup[['variables']][[var]][['dim']][[dim]][['vals']] <- 1:length(new_vals)
}
}
}
}
}
}
if (!is_irregular && result_is_irregular) {
attribute_backup[['coordinates']] <- paste(lon_var_name, lat_var_name)
} else if (is_irregular && !result_is_irregular) {
attribute_backup[['coordinates']] <- NULL
}
}
}
attributes(result_array) <- attribute_backup
lons_attr_bk[['dim']] <- dim(found_lons)
if (!is.null(lons_attr_bk[['variables']]) && (length(lons_attr_bk[['variables']]) > 0)) {
for (var in 1:length(lons_attr_bk[['variables']])) {
if (length(lons_attr_bk[['variables']][[var]][['dim']]) > 0) {
dims_to_remove <- NULL
for (dim in 1:length(lons_attr_bk[['variables']][[var]][['dim']])) {
dim_name <- NULL
if ('name' %in% names(lons_attr_bk[['variables']][[var]][['dim']][[dim]])) {
dim_name <- lons_attr_bk[['variables']][[var]][['dim']][[dim]][['name']]
if (dim_name %in% c(lon_dim, lat_dim)) {
if (dim_name == lon_dim) {
lons_attr_bk[['variables']][[var]][['dim']][[dim]][['name']] <- new_lon_name
} else {
lons_attr_bk[['variables']][[var]][['dim']][[dim]][['name']] <- new_lat_name
}
}
} else if (!is.null(names(lons_attr_bk[['variables']][[var]][['dim']]))) {
dim_name <- names(lons_attr_bk[['variables']][[var]][['dim']])[dim]
if (dim_name %in% c(lon_dim, lat_dim)) {
if (dim_name == lon_dim) {
names(lons_attr_bk[['variables']][[var]][['dim']])[which(names(lons_attr_bk[['variables']][[var]][['dim']]) == lon_dim)] <- new_lon_name
} else {
names(lons_attr_bk[['variables']][[var]][['dim']])[which(names(lons_attr_bk[['variables']][[var]][['dim']]) == lat_dim)] <- new_lat_name
}
}
}
if (!is.null(dim_name)) {
if (dim_name %in% c(lon_dim, lat_dim)) {
if (dim_name == lon_dim) {
new_vals <- found_lons[TRUE]
} else if (dim_name == lat_dim) {
new_vals <- found_lats[TRUE]
if (!result_is_irregular) {
dims_to_remove <- c(dims_to_remove, dim)
}
}
if (!is.null(lons_attr_bk[['variables']][[var]][['dim']][[dim]][['len']])) {
lons_attr_bk[['variables']][[var]][['dim']][[dim]][['len']] <- length(new_vals)
}
if (!is.null(lons_attr_bk[['variables']][[var]][['dim']][[dim]][['vals']])) {
if (!result_is_irregular) {
lons_attr_bk[['variables']][[var]][['dim']][[dim]][['vals']] <- new_vals
} else {
lons_attr_bk[['variables']][[var]][['dim']][[dim]][['vals']] <- 1:length(new_vals)
}
}
}
}
}
if (length(dims_to_remove) > 1) {
lons_attr_bk[['variables']][[var]][['dim']] <- lons_attr_bk[['variables']][[var]][['dim']][[-dims_to_remove]]
}
}
}
names(lons_attr_bk[['variables']])[1] <- lon_var_name
lons_attr_bk[['variables']][[1]][['units']] <- 'degrees_east'
}
attributes(found_lons) <- lons_attr_bk
lats_attr_bk[['dim']] <- dim(found_lats)
if (!is.null(lats_attr_bk[['variables']]) && (length(lats_attr_bk[['variables']]) > 0)) {
for (var in 1:length(lats_attr_bk[['variables']])) {
if (length(lats_attr_bk[['variables']][[var]][['dim']]) > 0) {
dims_to_remove <- NULL
for (dim in 1:length(lats_attr_bk[['variables']][[var]][['dim']])) {
dim_name <- NULL
if ('name' %in% names(lats_attr_bk[['variables']][[var]][['dim']][[dim]])) {
dim_name <- lats_attr_bk[['variables']][[var]][['dim']][[dim]][['name']]
if (dim_name %in% c(lon_dim, lat_dim)) {
if (dim_name == lon_dim) {
lons_attr_bk[['variables']][[var]][['dim']][[dim]][['name']] <- new_lon_name
} else {
lons_attr_bk[['variables']][[var]][['dim']][[dim]][['name']] <- new_lat_name
}
}
} else if (!is.null(names(lats_attr_bk[['variables']][[var]][['dim']]))) {
dim_name <- names(lats_attr_bk[['variables']][[var]][['dim']])[dim]
if (dim_name %in% c(lon_dim, lat_dim)) {
if (dim_name == lon_dim) {
names(lats_attr_bk[['variables']][[var]][['dim']])[which(names(lats_attr_bk[['variables']][[var]][['dim']]) == lon_dim)] <- new_lon_name
} else {
names(lats_attr_bk[['variables']][[var]][['dim']])[which(names(lats_attr_bk[['variables']][[var]][['dim']]) == lat_dim)] <- new_lat_name
}
}
}
if (!is.null(dim_name)) {
if (dim_name %in% c(lon_dim, lat_dim)) {
if (dim_name == lon_dim) {
new_vals <- found_lons[TRUE]
if (!result_is_irregular) {
dims_to_remove <- c(dims_to_remove, dim)
}
} else if (dim_name == lat_dim) {
new_vals <- found_lats[TRUE]
}
if (!is.null(lats_attr_bk[['variables']][[var]][['dim']][[dim]][['len']])) {
lats_attr_bk[['variables']][[var]][['dim']][[dim]][['len']] <- length(new_vals)
}
if (!is.null(lats_attr_bk[['variables']][[var]][['dim']][[dim]][['vals']])) {
if (!result_is_irregular) {
lats_attr_bk[['variables']][[var]][['dim']][[dim]][['vals']] <- new_vals
} else {
lats_attr_bk[['variables']][[var]][['dim']][[dim]][['vals']] <- 1:length(new_vals)
}
}
}
}
}
if (length(dims_to_remove) > 1) {
lats_attr_bk[['variables']][[var]][['dim']] <- lats_attr_bk[['variables']][[var]][['dim']][[-dims_to_remove]]
}
}
}
names(lats_attr_bk[['variables']])[1] <- lat_var_name
lats_attr_bk[['variables']][[1]][['units']] <- 'degrees_north'
}
attributes(found_lats) <- lats_attr_bk
}
list(data_array = if (return_array) {
if (interpolation_needed) {
result_array
} else {
data_array
}
} else {
NULL
},
lons = found_lons, lats = found_lats)
}