I haven't found a java library to directly work with the files yet but there is a way to (indirectly) do this in java:
The Generic Mapping Tools can be used via command line in Windows to convert said files.
Installing it is easy: Download their exe from e.g. github and run it. This is going to install everything needed and also set the necessary environmental variable (has to be ticked during the installation process).
Afterwards the conversion can be run in cmd
via a short command, e.g.:
gmt grdconvert in.nc out.grd=sf -V
The input format is detected automatically (NetCDF in my case). sf
("Golden Software Surfer format 6") states the desired output format. An overview of all supported input/output formats can be found here. I also tested it with sd
("Golden Software Surfer format 7") but that's indeed read-only (as stated in the link).
The above command requires you to cd
to the input file first and is then going to create the output file in the same folder. Instead you can also skip the cd
and give it the path directly:
gmt grdconvert C:\myfolderpath1\in.nc C:\myfolderpath2\out.grd=sf -V
It is possible to use cmd
in Java, as shown e.g. here. The full example in my case would look like this:
Process p = null;
try {
//final String command = "gmt grdconvert "+in+" "+out+"=sf -V"; //Version 1: cd to folder first, input folder == output folder
final String command = "gmt grdconvert "+inpath+" "+outpath+"=sf -V"; //Version 2: No cd, different folders possible
ProcessBuilder builder = new ProcessBuilder(
//"cmd.exe", "/c", "cd \""+path+"\" && "+command); // Version 1
"cmd.exe", "/c", command); //Version 2
builder.redirectErrorStream(true);
p = builder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = br.readLine();
if (line == null) { break; }
System.out.println(line);
}
br.close();
} catch(IOException e) {
e.printStackTrace();
} finally {
if(p!=null) { p.destroy(); }
}
This also prints everything that was output because of -V
to console.