You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.4 KiB

2 years ago
#include "odm.h"
#include "GPL.h"
inline void Usage(const MString& arg0)
{
message(arg0 + " (<key>=<value>...)");
message("Keys are:");
message(" source. Required. May be: NEMO");
message(" var. Required. May be: temp, sal or chl");
message(" time. Time moment or regular expression. If present, timeb and timee must be absent");
message(" timeb, timee. Time interval. If present, time must be absent");
message(" out. Output file. Default: out.bin");
message(" Keys for source=NEMO");
message(" var can be only temp or sal");
message(" dataset. Can be DT, NRT or NRT6. Default: DT");
message(" cred. Login and password for COPERNICUS site. Default works");
message(" proxy. Proxy for access to COPERNICUS. Default works");
message(" layer and/or depth. Layer or depth of NEMO dataset. If depth is specified, layer is ignored. Default: layer=0");
message(" lonb, lone, latb, late. Required. Region of interest");
}
int main(int argc, char** argv)
{
if(argc == 1)
{
Usage(argv[0]);
return 2;
}
auto args = ParseArgs(argc, argv);
if(!args.contains("source"))
{
errmessage("No source specified!");
return 1;
}
VarType vtype(args["var"]);
if(!vtype)
{
errmessage("Incorrect or no variable specified");
return 1;
}
michlib_internal::ParameterListEx pars;
pars.UsePrefix("");
pars.SetParameter("source", args["source"]);
pars.SetParameter("variable", vtype.Name());
Data data;
if(args["source"] == "NEMO")
{
if(!vtype.isSupported<NEMOData>())
{
errmessage("Variable " + args["var"] + " is unsupported by NEMO");
return 1;
}
GPL.UsePrefix("AVISO");
NEMOData ndata;
MString dataset = args["dataset"];
if(!dataset.Exist()) dataset = "DT";
MString cred = args["cred"];
if(!cred.Exist()) cred = GPL.ParameterSValue("COPERNICUS_USER", "");
MString proxy;
if(args.contains("proxy"))
MString proxy = args["proxy"];
else
proxy = GPL.ParameterSValue("COPERNICUS_PROXY", "");
if(!ndata.Open(dataset, cred, proxy))
{
errmessage("Can't open NEMO dataset");
return 1;
}
size_t layer = 0;
if(args.contains("layer")) layer = args["layer"].ToInteger<size_t>();
if(!args.contains("depth") && layer >= ndata.NDepths())
{
errmessage(MString("Layer ") + layer + " is too deep!");
return 1;
}
real depth = args.contains("depth") ? args["depth"].ToReal() : ndata.Depth(layer);
if(!(args.contains("lonb") && args.contains("lone") && args.contains("latb") && args.contains("late")))
{
errmessage("Region not specified (lonb, lone, latb, late)");
return 1;
}
if(!ndata.SetRegion(args["lonb"].ToReal(), args["lone"].ToReal(), args["latb"].ToReal(), args["late"].ToReal(), depth))
{
errmessage("Can't set region");
return 1;
}
pars.SetParameter("depth", depth);
pars.SetParameter("layer", ndata.Layer());
pars.SetParameter("dataset", dataset);
pars.SetParameter("lonb", args["lonb"].ToReal());
pars.SetParameter("latb", args["latb"].ToReal());
pars.SetParameter("lone", args["lone"].ToReal());
pars.SetParameter("late", args["late"].ToReal());
data = DataVariants(std::move(ndata));
}
else
{
errmessage("Unknown source " + args["source"]);
return 1;
}
if(args.contains("time") && (args.contains("timeb") || args.contains("timee")))
{
errmessage("Time must be set via time parameter or timeb and timee parameter but not via both");
return 1;
}
if(!(args.contains("time") || (args.contains("timeb") && args.contains("timee"))))
{
errmessage("Time must be set via time parameter or timeb and timee parameter");
return 1;
}
BFileW fw;
MString name = args["out"];
if(!name.Exist()) name = "out.bin";
fw.Create(name, 3);
/*
if(!fw.Create(name,3))
{
errmessage("Can't create file "+name);
return 1;
}
*/
fw.SetParameters(pars);
fw.UsePrefix("");
if(args.contains("time"))
{
MDateTime time;
if(time.FromString(args["time"])) // One date
{
auto it = GetTIndex(data, time);
fw.SetParameter("time", data.Time(it).ToString());
data.Write(fw, vtype, std::vector<size_t>(1, it));
}
else // Regular expression
{
fw.SetParameter("timeregex", args["time"]);
data.Write(fw, vtype, GetTIndexes(data, args["time"]));
}
}
else // Bdate, edate
{
MDateTime tb(args["timeb"]), te(args["timee"]);
fw.SetParameter("timeb", tb.ToString());
fw.SetParameter("timee", te.ToString());
data.Write(fw, vtype, GetTIndexes(data, tb, te));
}
fw.Finalize();
fw.Close();
return 0;
}