|
| 1 | + |
| 2 | +CFStdName(n::AbstractString) = CFStdName(Symbol(n)) |
| 3 | + |
| 4 | +macro CF_str(n) |
| 5 | + CFStdName(n) |
| 6 | +end |
| 7 | + |
| 8 | +import Base.string |
| 9 | +Base.string(n::CFStdName) = string(n.name) |
| 10 | + |
| 11 | + |
| 12 | +""" |
| 13 | + ncvar = NCDatasets.ancillaryvariables(ncv::NCDatasets.CFVariable,modifier) |
| 14 | +
|
| 15 | +Return the first ancillary variables from the NetCDF variable `ncv` with the |
| 16 | +standard name modifier `modifier`. It can be used for example to access |
| 17 | +related variable like status flags. |
| 18 | +""" |
| 19 | +function ancillaryvariables(ncv::CFVariable,modifier) |
| 20 | + ds = dataset(ncv) |
| 21 | + varname = name(ncv) |
| 22 | + |
| 23 | + if !haskey(ncv.attrib,"ancillary_variables") |
| 24 | + return nothing |
| 25 | + end |
| 26 | + |
| 27 | + ancillary_variables = split(ncv.attrib["ancillary_variables"]) |
| 28 | + |
| 29 | + for ancillary_variable in ancillary_variables |
| 30 | + ncv_ancillary = ds[ancillary_variable] |
| 31 | + if occursin(modifier,ncv_ancillary.attrib["standard_name"]) |
| 32 | + @debug ancillary_variable |
| 33 | + return ncv_ancillary |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + # nothing found |
| 38 | + return nothing |
| 39 | +end |
| 40 | + |
| 41 | + |
| 42 | +allowmissing(x::AbstractArray{T}) where {T} = convert(AbstractArray{Union{T, Missing}}, x) |
| 43 | + |
| 44 | +""" |
| 45 | + data = filter(ncv, indices...; accepted_status_flags = nothing) |
| 46 | +
|
| 47 | +Load and filter observations by replacing all variables without an acepted status |
| 48 | +flag to `missing`. It is used the attribute `ancillary_variables` to identify |
| 49 | +the status flag. |
| 50 | +
|
| 51 | +``` |
| 52 | +# da["data"] is 2D matrix |
| 53 | +good_data = NCDatasets.filter(ds["data"],:,:, accepted_status_flags = ["good_data","probably_good_data"]) |
| 54 | +``` |
| 55 | +
|
| 56 | +""" |
| 57 | +function filter(ncv::AbstractVariable, indices...; accepted_status_flags = nothing) |
| 58 | +#function filter_(ncv, indices...) |
| 59 | +# accepted_status_flags = ("good_value", "probably_good_value") |
| 60 | + data = allowmissing(ncv[indices...]) |
| 61 | + |
| 62 | + if (accepted_status_flags != nothing) |
| 63 | + ncv_ancillary = ancillaryvariables(ncv,"status_flag"); |
| 64 | + if ncv_ancillary == nothing |
| 65 | + error("no variable with the attribute status_flag as standard_name among $(ancillary_variables) found") |
| 66 | + end |
| 67 | + |
| 68 | + flag_values = ncv_ancillary.attrib["flag_values"] |
| 69 | + flag_meanings = ncv_ancillary.attrib["flag_meanings"]::String |
| 70 | + if typeof(flag_meanings) <: AbstractString |
| 71 | + flag_meanings = split(flag_meanings) |
| 72 | + end |
| 73 | + |
| 74 | + accepted_status_flag_values = zeros(eltype(flag_values),length(accepted_status_flags)) |
| 75 | + for i = eachindex(accepted_status_flags,accepted_status_flag_values) |
| 76 | + tmp = findfirst(accepted_status_flags[i] .== flag_meanings) |
| 77 | + |
| 78 | + if tmp == nothing |
| 79 | + error("cannot recognise flag $(accepted_status_flags[i])") |
| 80 | + end |
| 81 | + accepted_status_flag_values[i] = flag_values[tmp] |
| 82 | + end |
| 83 | + #@debug accepted_status_flag_values |
| 84 | + |
| 85 | + dataflag = ncv_ancillary.var[indices...]; |
| 86 | + for i in eachindex(data) |
| 87 | + good = false; |
| 88 | + for accepted_status_flag_value in accepted_status_flag_values |
| 89 | + good = good || (dataflag[i] .== accepted_status_flag_value) |
| 90 | + end |
| 91 | + if !good |
| 92 | + #@show i,dataflag[i] |
| 93 | + data[i] = missing |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + return data |
| 99 | +end |
| 100 | + |
| 101 | + |
| 102 | +""" |
| 103 | + cv = coord(v::Union{CFVariable,Variable},standard_name) |
| 104 | +
|
| 105 | +Find the coordinate of the variable `v` by the standard name `standard_name` |
| 106 | +or some [standardized heuristics based on units](https://web.archive.org/web/20190918144052/http://cfconventions.org/cf-conventions/cf-conventions.html#latitude-coordinate). If the heuristics fail to detect the coordinate, |
| 107 | +consider to modify the netCDF file to add the `standard_name` attribute. |
| 108 | +All dimensions of the coordinate must also be dimensions of the variable `v`. |
| 109 | +
|
| 110 | +## Example |
| 111 | +```julia |
| 112 | +using NCDatasets |
| 113 | +ds = NCDataset("file.nc") |
| 114 | +ncv = ds["SST"] |
| 115 | +lon = coord(ncv,"longitude")[:] |
| 116 | +lat = coord(ncv,"latitude")[:] |
| 117 | +v = ncv[:] |
| 118 | +close(ds) |
| 119 | +``` |
| 120 | +""" |
| 121 | +function coord(v::AbstractVariable,standard_name) |
| 122 | + matches = Dict( |
| 123 | + "time" => [r".*since.*"], |
| 124 | + # It is great to have choice! |
| 125 | + # https://web.archive.org/web/20190918144052/http://cfconventions.org/cf-conventions/cf-conventions.html#latitude-coordinate |
| 126 | + "longitude" => [r"degree east",r"degrees east",r"degrees_east", |
| 127 | + r"degree_east", r"degree_E", r"degrees_E", |
| 128 | + r"degreeE", r"degreesE"], |
| 129 | + "latitude" => [r"degree north",r"degrees north",r"degrees_north", |
| 130 | + r"degree_north", r"degree_N", r"degrees_N", r"degreeN", |
| 131 | + r"degreesN"], |
| 132 | + ) |
| 133 | + |
| 134 | + ds = dataset(v) |
| 135 | + dims = Set(dimnames(v)) |
| 136 | + |
| 137 | + # find by standard name |
| 138 | + for coord in varbyattrib(ds,standard_name = standard_name) |
| 139 | + if Set(dimnames(coord)) ⊆ dims |
| 140 | + return coord |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + # find by units |
| 145 | + if haskey(matches,standard_name) |
| 146 | + # prefer e.g. vectors over scalars |
| 147 | + # this is necessary for ROMS model output |
| 148 | + coordfound = nothing |
| 149 | + coordndims = -1 |
| 150 | + |
| 151 | + for (_,coord) in ds |
| 152 | + units = get(coord.attrib,"units","") |
| 153 | + |
| 154 | + for re in matches[standard_name] |
| 155 | + if match(re,units) != nothing |
| 156 | + if Set(dimnames(coord)) ⊆ dims |
| 157 | + if ndims(coord) > coordndims |
| 158 | + coordfound = coord |
| 159 | + coordndims = ndims(coord) |
| 160 | + end |
| 161 | + end |
| 162 | + end |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + return coordfound |
| 167 | + end |
| 168 | + |
| 169 | + return nothing |
| 170 | +end |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | +""" |
| 176 | + b = bounds(ncvar::NCDatasets.CFVariable) |
| 177 | +
|
| 178 | +Return the CFVariable corresponding to the `bounds` attribute of the variable `ncvar`. |
| 179 | +The time units and calendar from the `ncvar` are used but not the |
| 180 | +attributes controling the |
| 181 | +packing of data `scale_factor`, `add_offset` and `_FillValue`. |
| 182 | +""" |
| 183 | +function bounds(ncvar::CFVariable) |
| 184 | + ds = dataset(ncvar) |
| 185 | + varname = ncvar.attrib["bounds"] |
| 186 | + return ds[varname] |
| 187 | +end |
0 commit comments