You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to convert String variables to java code to build a NetCDF file from strings. I have some Strings which contain the commands to write a netCDF. For example:
String Variable contains the following strings:
'Variable time; '
'time = ncfile.addVariable(null, "time", DataType.Double, "time");'
String DataA contains:
'ArrayDouble.D1 timeData = new ArrayDouble.D1(countLinesResult); '
'Index ima = timeData.getIndex();'
each string is public static String. These strings are in NetCDF_writer Class static void getDataNc method. Here is the code:
package meteoread;
/**
*
* @author Beata
*/
;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import static meteoread.DownLoad_Meteo.Detected;
import static meteoread.DownLoad_Meteo.ETime;
import static meteoread.DownLoad_Meteo.STime;
import static meteoread.DownLoad_Meteo.Values;
import static meteoread.DownLoad_Meteo.Values2;
import static meteoread.DownLoad_Meteo.id1;
import static meteoread.DownLoad_Meteo.id2;
import static meteoread.DownLoad_Meteo.listString;
import static meteoread.DownLoad_Meteo.res;
import static meteoread.SelectData.result;
import org.mdkt.compiler.InMemoryJavaCompiler;
import ucar.nc2.*;
import ucar.ma2.*;
public class NetCDF_writer {
private static String okW ="";
private static int is;
private static int il;
private static int di;
private static int countLinesResult;
public static String TableNC = DownLoad_Meteo.Table;
private static ArrayList<String> s = new ArrayList<String>();
private static ArrayList<String> s2 = new ArrayList<String>();
private static ArrayList<String> s3 = new ArrayList<String>();
private static ArrayList<String> s4 = new ArrayList<String>();
private static ArrayList<String> s5 = new ArrayList<String>();
private String[] colName;
private static ArrayList<String> ValueS = new ArrayList<String>();
private static ArrayList<Integer> ValueI = new ArrayList<Integer>();
private static ArrayList<Double> ValueD = new ArrayList<Double>();
public static String Variables;
public static String DataA;
public static String DataD;
public static String DataNC;
static void getDataNc(int id1, int id2, String listString, String location, String Stimenc, String ETimenc)throws Exception {
try{
Connection con = MeteoRead.getConnection();
PreparedStatement statement= con.prepareStatement("SELECT " + listString + " FROM " +TableNC + " WHERE TIME_ID BETWEEN " +"\"" + STime + "\"" +" AND " + "\"" +ETime + "\"" + " ORDER BY ID");
ResultSet result = statement.executeQuery();
ResultSetMetaData rsmd = result.getMetaData();
is = id1;
il = id2;
di = il-is;
if(is == 1){
countLinesResult = il;
}
else{
countLinesResult = di;}
result.first();
result.previous();
ColName = listString.split(",");
NetcdfFileWriter ncfile = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4, location, null);
Dimension timeDim = ncfile.addUnlimitedDimension("time");
s = SelectData.result.get(1);
s2 = SelectData.result.get(2);
s3 = SelectData.result.get(3);
s4 = SelectData.result.get(4);
s5 = SelectData.result.get(5);
Variables = "";
DataA = "";
DataD = "";
DataNC = "";
int n = 0;
while(n < s.size()-1){
Variables = Variables +" " + s.get(n) + "\n";
Variables = Variables +" " + s2.get(n) + "\n";
while(result.next()){
Date date2 = dateConv1(result.getString("TIME"));
double dateU = ToMATLABDate(date2);
if(rsmd.getColumnName(n+1).equals("TIME")){
ValueD.add(dateU);
}
if(rsmd.getColumnTypeName(n+1)== "VARCHAR" && !rsmd.getColumnName(n+1).equals("TIME")){
ValueS.add(result.getString(ColName[n]));
}
if(rsmd.getColumnTypeName(n+1)== "INTEGER"){
ValueI.add(result.getInt(ColName[n]));
}
if(rsmd.getColumnTypeName(n+1)== "DOUBLE"){
ValueD.add(result.getDouble(ColName[n]));
}
}
DataA = DataA + s3.get(n) + "\n";
DataD = DataD + s4.get(n) + "\n";
DataNC = DataNC + s5.get(n) + "\n";
n++; }
StringBuffer sourceCode = new StringBuffer();
sourceCode.append("package meteoread;\n");
sourceCode.append("public class NetCDF_writer {\n");
sourceCode.append(" static void getDataNc(int id1, int id2, String listString, String location, String Stimenc, String ETimenc)throws Exception {" +Variables +" ncfile.create();" + "\n" + DataA +" for (int timeIdx = 0; timeIdx < countLinesResult; timeIdx ++){" + DataD + "}"+ DataNC + "}");
sourceCode.append("}");
Class<?> Netcdf_writer = InMemoryJavaCompiler.compile("meteoread.NetCDF_writer", sourceCode.toString());
okW = "NC FILE BUILD SUCCESSFULLY";
okWindow o = new okWindow();
o.okWindow(okW);
o.setVisible(true);
} catch(Exception e){
e.printStackTrace();
okW = e.toString();
okWindow o = new okWindow();
o.okWindow(okW);
o.setVisible(true);}
}
public static Date dateConv1(String s){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateInString = s;
Date date = new Date();
try {
date = (Date)formatter.parse(dateInString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* Converts a java.util.Date into a MATLAB Serial Date Number in the local timezone. MATLAB Serial Date Number are
* doubles counting the number of days since January 0 0000. The time of day is represented in fractions of a whole
* day.
*
* @param date the date to convert
* @return the date as MATLAB Serial Date Number
*/
public static double ToMATLABDate(Date date) {
// Converts a java.util.Date into a MATLAB Serial Date Number taking into account timezones and DST.
Calendar cal = new GregorianCalendar();
cal.setTime(date);
double SerialDateNumber = (date.getTime() + cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / 1000.0 / 3600.0 / 24.0 ;
return SerialDateNumber;
}
}
Unfortunately, I got several cannot find symbol error messages. The compiler does not find the NetcdfFileWriter identifiers even though I imported ucar.nc2.* and ucar.ma2.* packages. Could you please write to me how I should fix this issue?
The text was updated successfully, but these errors were encountered:
I would like to convert String variables to java code to build a NetCDF file from strings. I have some Strings which contain the commands to write a netCDF. For example:
String Variable contains the following strings:
'Variable time; '
'time = ncfile.addVariable(null, "time", DataType.Double, "time");'
String DataA contains:
'ArrayDouble.D1 timeData = new ArrayDouble.D1(countLinesResult); '
'Index ima = timeData.getIndex();'
String DataD contains:
'timeData.setDouble(ima.set(timeIdx),ValueD.get(timeIdx)); '
String DataNC contains:
'ncfile.write(time, timeData); '
each string is public static String. These strings are in NetCDF_writer Class static void getDataNc method. Here is the code:
Unfortunately, I got several cannot find symbol error messages. The compiler does not find the NetcdfFileWriter identifiers even though I imported ucar.nc2.* and ucar.ma2.* packages. Could you please write to me how I should fix this issue?
The text was updated successfully, but these errors were encountered: