Hello All, I am new to Julia, coming from python. I have collections of observational data and output from GCM model in netcdf format, but of different “types” (different grids, frequency, conventions etc, not julia types), which require slightly different treatment after reading from disk, before feeding these data to analysis utils. This may include renaming coordinates, adding missing grid metrics which is required by analysis tools, etc. A reading function should be able to take any “type” of collection as input and return NCDataset (or similar) object with all required metadata/metrics included and coordinates/variable names following the same convention. No regridding/resampling is needed at this step, just putting data to same format. I can think of several ways of how to do this in julia:
- Using ‘if’ blocks,
function read_collection(colname, ...; coltype="LATLON")
if coltype=="LATLON"
ds=read_latlon(colname)
elseif coltype="Tripolar"
ds=read_tripolar(colname)
......................
end
return ds
end
- Using dictionary
function read_collection(colname, ...; coltype="LATLON")
readers=Dict("LATLON" => read_latlon, "Tripolar" => read_tripolar, ...)
return readers[coltype](colname)
end
I do something like this in python.
3. Dispatch on a dummy type
struct LatLon
end
struct Tripolar
end
function read_collection(colname, ::LatLon)
return read_latlon(colname)
end
function read_collection(colname, ::Tripolar)
return read_tripolar(colname)
end
- Dispatch on a value type
function read_collection(colname, ::Val{"LATLON"})
return read_latlon(colname)
end
function read_collection(colname, ::Val{"Tripolar"})
return read_tripolar(colname)
end
Which way would be preferable in julia in terms of performance and ease of expanding to more collection types in the future?