Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4bff009cfc | ||
|
|
ba1b5ab29a | ||
|
|
8bfce31f2e | ||
|
|
bca818e734 | ||
|
|
c95015c234 |
@@ -89,7 +89,7 @@ android {
|
||||
applicationId "com.mowesta.android.app"
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 29
|
||||
versionCode 12
|
||||
versionCode 13
|
||||
versionName "${version}"
|
||||
buildConfigField "long", "BUILD_TIME", System.currentTimeMillis() + "L"
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ allprojects {
|
||||
|
||||
// set version for our project
|
||||
group = 'com.mowesta'
|
||||
version = '1.0.12'
|
||||
version = '1.0.13'
|
||||
|
||||
// set versions for our libraries
|
||||
ext {
|
||||
|
||||
@@ -29,7 +29,7 @@ import io.swagger.annotations.ApiResponses;
|
||||
|
||||
/**
|
||||
* The api to query and fetch locations.
|
||||
*
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
@Api
|
||||
@@ -40,7 +40,7 @@ public interface ILocationService {
|
||||
|
||||
/**
|
||||
* Returns a page of locations that match the query.
|
||||
*
|
||||
*
|
||||
* @param pageConfig The page configuration.
|
||||
* @param context The search context.
|
||||
* @param term The search term.
|
||||
@@ -53,16 +53,16 @@ public interface ILocationService {
|
||||
@ApiResponses({
|
||||
@ApiResponse(code=HttpServletResponse.SC_OK, message="Location hierarchy page retrieved successfully.", response=LocationHierarchyPage.class),
|
||||
@ApiResponse(code=HttpServletResponse.SC_BAD_REQUEST, message="Invalid request parameter.")
|
||||
})
|
||||
})
|
||||
@PermitAll
|
||||
Response searchLocations(@BeanParam @Valid PageConfig pageConfig, @BeanParam @Valid SearchContext context,
|
||||
@ApiParam(value="The search term.", required=true) @QueryParam("term") @NotNull @Valid String term);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns a page of cities that are inside a particular area defined by two coordinates.
|
||||
*
|
||||
*
|
||||
* @param pageConfig The page configuration.
|
||||
* @param longitude1 The longitude of the first coordinate.
|
||||
* @param latitude1 The latitude of the first coordinate.
|
||||
@@ -76,18 +76,18 @@ public interface ILocationService {
|
||||
@ApiResponses({
|
||||
@ApiResponse(code=HttpServletResponse.SC_OK, message="The pages.", response=CityPage.class),
|
||||
@ApiResponse(code=HttpServletResponse.SC_BAD_REQUEST, message="Invalid request parameter.")
|
||||
})
|
||||
})
|
||||
@PermitAll
|
||||
Response searchCities(@BeanParam @Valid PageConfig pageConfig,
|
||||
@ApiParam(value = "The longitude of the first coordinate.", required = true) @QueryParam("longitude1") double longitude1,
|
||||
Response searchCities(@BeanParam @Valid PageConfig pageConfig,
|
||||
@ApiParam(value = "The longitude of the first coordinate.", required = true) @QueryParam("longitude1") double longitude1,
|
||||
@ApiParam(value = "The latitude of the first coordinate.", required = true) @QueryParam("latitude1") double latitude1,
|
||||
@ApiParam(value = "The longitude of the second coordinate.", required = true) @QueryParam("longitude2") double longitude2,
|
||||
@ApiParam(value = "The longitude of the second coordinate.", required = true) @QueryParam("longitude2") double longitude2,
|
||||
@ApiParam(value = "The latitude of the second coordinate.", required = true) @QueryParam("latitude2") double latitude2);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns a page of cities.
|
||||
*
|
||||
*
|
||||
* @param pageConfig The page configuration.
|
||||
* @return A page of cities.
|
||||
*/
|
||||
@@ -97,10 +97,10 @@ public interface ILocationService {
|
||||
@ApiResponses({
|
||||
@ApiResponse(code=HttpServletResponse.SC_OK, message="City page retrieved successfully.", response=CityPage.class),
|
||||
@ApiResponse(code=HttpServletResponse.SC_BAD_REQUEST, message="Invalid request parameter.")
|
||||
})
|
||||
})
|
||||
@PermitAll
|
||||
Response getCities(@BeanParam @Valid PageConfig pageConfig);
|
||||
|
||||
|
||||
/**
|
||||
* Returns a particular city.
|
||||
*
|
||||
@@ -119,8 +119,8 @@ public interface ILocationService {
|
||||
|
||||
/**
|
||||
* Returns a geojson representation of the city boundaries.
|
||||
*
|
||||
* @param id The id of the city.
|
||||
*
|
||||
* @param id The id of the city.
|
||||
* @return The boundaries of geojson or an error code.
|
||||
*/
|
||||
@GET
|
||||
@@ -134,8 +134,8 @@ public interface ILocationService {
|
||||
@PermitAll
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
Response getCityBoundary(@ApiParam(value = "The city id.", required = true) @PathParam("id") @Valid @Min(0) long id);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns a page of states.
|
||||
*
|
||||
@@ -151,7 +151,7 @@ public interface ILocationService {
|
||||
})
|
||||
@PermitAll
|
||||
Response getStates(@BeanParam @Valid PageConfig pageConfig);
|
||||
|
||||
|
||||
/**
|
||||
* Returns a particular state.
|
||||
*
|
||||
@@ -167,15 +167,15 @@ public interface ILocationService {
|
||||
})
|
||||
@PermitAll
|
||||
Response getState(@ApiParam(value = "The state id.", required = true) @PathParam("id") @Valid @Min(0) long id);
|
||||
|
||||
|
||||
/**
|
||||
* Returns a geojson representation of the state boundaries.
|
||||
*
|
||||
* @param id The id of the state.
|
||||
* @return The boundaries of geojson or an error code.
|
||||
* @return The boundaries as geojson or an error code.
|
||||
*/
|
||||
@GET
|
||||
@Path("state/{id: [0-9]+}/boundary")
|
||||
@Path("states/{id: [0-9]+}/boundary")
|
||||
@ApiOperation(value = "Gets the state boundary for a state with a particular id.", tags = {Tags.DEFAULT, Tags.LOCATION})
|
||||
@ApiResponses({
|
||||
@ApiResponse(code = HttpServletResponse.SC_OK, message = "State boundary retrieved successfully.", response=Geometry.class),
|
||||
@@ -185,7 +185,28 @@ public interface ILocationService {
|
||||
@PermitAll
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
Response getStateBoundary(@ApiParam(value = "The state id.", required = true) @PathParam("id") @Valid @Min(0) long id);
|
||||
|
||||
|
||||
/**
|
||||
* The deprecated version of the method that has a typo in the path name (state instead of states).
|
||||
* To be removed, if Android Apps < V14 are no longer present.
|
||||
*
|
||||
* @param id The id of the state.
|
||||
* @return The boundaries as geojson or an error code.
|
||||
*/
|
||||
@GET
|
||||
@Path("state/{id: [0-9]+}/boundary")
|
||||
@ApiOperation(value = "Deprecated: Gets the state boundary for a state with a particular id.", tags = {Tags.DEFAULT, Tags.LOCATION})
|
||||
@ApiResponses({
|
||||
@ApiResponse(code = HttpServletResponse.SC_OK, message = "State boundary retrieved successfully.", response=Geometry.class),
|
||||
@ApiResponse(code = HttpServletResponse.SC_NO_CONTENT, message = "The state boundary are not available."),
|
||||
@ApiResponse(code = HttpServletResponse.SC_NOT_FOUND, message = "The state does not exist."),
|
||||
})
|
||||
@PermitAll
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
@Deprecated
|
||||
Response getStateBoundaryDeprecated(@ApiParam(value = "The state id.", required = true) @PathParam("id") @Valid @Min(0) long id);
|
||||
|
||||
|
||||
/**
|
||||
* Returns a page of cities for a state ordered by importance.
|
||||
*
|
||||
@@ -202,10 +223,10 @@ public interface ILocationService {
|
||||
})
|
||||
@PermitAll
|
||||
Response getStateCities(@ApiParam(value = "The state id.", required = true) @PathParam("id") @Valid @Min(0) long id, @BeanParam @Valid PageConfig pageConfig);
|
||||
|
||||
|
||||
/**
|
||||
* Returns a page of countries.
|
||||
*
|
||||
*
|
||||
* @param pageConfig The page configuration.
|
||||
* @return A page of countries.
|
||||
*/
|
||||
@@ -215,10 +236,10 @@ public interface ILocationService {
|
||||
@ApiResponses({
|
||||
@ApiResponse(code=HttpServletResponse.SC_OK, message="Country page retrieved successfully.", response=CountryPage.class),
|
||||
@ApiResponse(code=HttpServletResponse.SC_BAD_REQUEST, message="Invalid request parameter.")
|
||||
})
|
||||
})
|
||||
@PermitAll
|
||||
Response getCountries(@BeanParam @Valid PageConfig pageConfig);
|
||||
|
||||
|
||||
/**
|
||||
* Returns a particular country.
|
||||
*
|
||||
@@ -237,8 +258,8 @@ public interface ILocationService {
|
||||
|
||||
/**
|
||||
* Returns a geojson representation of the country boundaries.
|
||||
*
|
||||
* @param id The id of the country.
|
||||
*
|
||||
* @param id The id of the country.
|
||||
* @return The boundaries of geojson or an error code.
|
||||
*/
|
||||
@GET
|
||||
@@ -252,7 +273,7 @@ public interface ILocationService {
|
||||
@PermitAll
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
Response getCountryBoundary(@ApiParam(value = "The country id.", required = true) @PathParam("id") @Valid @Min(0) long id);
|
||||
|
||||
|
||||
/**
|
||||
* Returns a page of cities for a country ordered by importance.
|
||||
*
|
||||
@@ -270,5 +291,5 @@ public interface ILocationService {
|
||||
@PermitAll
|
||||
Response getCountryCities(@ApiParam(value = "The country id.", required = true) @PathParam("id") @Valid @Min(0) long id, @BeanParam @Valid PageConfig pageConfig);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.mowesta.data.ftp.FileStatusListener;
|
||||
import com.mowesta.data.ftp.FtpFolderSync;
|
||||
import com.mowesta.data.grib2.Grib2Data;
|
||||
import com.mowesta.data.grib2.Grib2File;
|
||||
import com.mowesta.data.grib2.decoder.BitmapInterpreter;
|
||||
import com.mowesta.data.grib2.decoder.RegularGridDecoder;
|
||||
import com.mowesta.data.grib2.decoder.RotatedGridDecoder;
|
||||
import com.mowesta.data.grib2.decoder.SimpleDataDecoder;
|
||||
@@ -31,16 +32,15 @@ import com.mowesta.data.process.util.FileNameComparator;
|
||||
* Represents a numeric weather prediction from DWD.
|
||||
*/
|
||||
public class NumericPrediction {
|
||||
|
||||
|
||||
/**
|
||||
* The extension of the data files.
|
||||
*/
|
||||
private static final String GRIB_EXTENSION = "grib2.bz2";
|
||||
|
||||
|
||||
/**
|
||||
* A enum to describe the coordinate system used
|
||||
* to represent locations.
|
||||
*
|
||||
* A enum to describe the coordinate system used to represent locations.
|
||||
*
|
||||
* @author Marcus
|
||||
*
|
||||
*/
|
||||
@@ -57,55 +57,54 @@ public class NumericPrediction {
|
||||
* A icosahedral system.
|
||||
*/
|
||||
ICOSAHEDRAL("icosahedral", false);
|
||||
|
||||
|
||||
/**
|
||||
* The string to filter files.
|
||||
*/
|
||||
private final String filter;
|
||||
|
||||
|
||||
/**
|
||||
* A flag to indicate whether the values
|
||||
* are computed from a grid definition.
|
||||
* A flag to indicate whether the values are computed from a grid definition.
|
||||
*/
|
||||
private final boolean computed;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new coordinate system.
|
||||
*
|
||||
* @param filter The keyword to filter.
|
||||
* @param computed A boolean to indicate whether the
|
||||
* values are computed.
|
||||
*
|
||||
* @param filter
|
||||
* The keyword to filter.
|
||||
* @param computed
|
||||
* A boolean to indicate whether the values are computed.
|
||||
*/
|
||||
private CoordinateSystem(String filter, boolean computed) {
|
||||
this.filter = filter;
|
||||
this.computed = computed;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the keyword to filter files.
|
||||
*
|
||||
*
|
||||
* @return The filter.
|
||||
*/
|
||||
public String getFilter() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if the values of the positions are
|
||||
* computed from a grid definition. False if they shall
|
||||
* be read from a file.
|
||||
*
|
||||
* Returns true if the values of the positions are computed from a grid
|
||||
* definition. False if they shall be read from a file.
|
||||
*
|
||||
* @return True if the coordinates are computed, false otherwise.
|
||||
*/
|
||||
public boolean isComputed() {
|
||||
return computed;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The predictions that we use to download data.
|
||||
*
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public enum PredictionType {
|
||||
@@ -116,12 +115,13 @@ public class NumericPrediction {
|
||||
/**
|
||||
* The european icon prediction.
|
||||
*/
|
||||
ICON_EUROPE("opendata.dwd.de", "weather/nwp/icon-eu/grib/00/", 0, CoordinateSystem.REGULAR, 93),
|
||||
ICON_EUROPE("opendata.dwd.de", "weather/nwp/icon-eu/grib/00/", 0, CoordinateSystem.REGULAR, 93),
|
||||
/**
|
||||
* The cosmo d2 prediction (the 3 o'clock prediction has more data, but sizes vary across variables).
|
||||
* The cosmo d2 prediction (the 3 o'clock prediction has more data, but sizes
|
||||
* vary across variables).
|
||||
*/
|
||||
COSMO_EUROPE("opendata.dwd.de", "weather/nwp/cosmo-d2/grib/03/", 3, CoordinateSystem.ROTATED, 46);
|
||||
|
||||
COSMO_EUROPE("opendata.dwd.de", "weather/nwp/icon-d2/grib/03/", 3, CoordinateSystem.ICOSAHEDRAL, 46);
|
||||
|
||||
/**
|
||||
* The number of files expected in variant data folders.
|
||||
*/
|
||||
@@ -138,19 +138,23 @@ public class NumericPrediction {
|
||||
* The server name.
|
||||
*/
|
||||
private String server;
|
||||
|
||||
|
||||
/**
|
||||
* The hour when the prediction is generated.
|
||||
*/
|
||||
private int hour;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new prediction.
|
||||
*
|
||||
* @param server The server name.
|
||||
* @param folder The remote folder.
|
||||
* @param system The coordinate system used by the simulation.
|
||||
* @param files The number of files in for time variant variables.
|
||||
*
|
||||
* @param server
|
||||
* The server name.
|
||||
* @param folder
|
||||
* The remote folder.
|
||||
* @param system
|
||||
* The coordinate system used by the simulation.
|
||||
* @param files
|
||||
* The number of files in for time variant variables.
|
||||
*/
|
||||
private PredictionType(String server, String folder, int hour, CoordinateSystem system, int files) {
|
||||
this.server = server;
|
||||
@@ -159,58 +163,57 @@ public class NumericPrediction {
|
||||
this.system = system;
|
||||
this.files = files;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the hour when the prediction is made.
|
||||
*
|
||||
*
|
||||
* @return The hour.
|
||||
*/
|
||||
public int getHour() {
|
||||
return hour;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The number of files for time variant variables.
|
||||
*
|
||||
*
|
||||
* @return The number of files for time variant variables.
|
||||
*/
|
||||
public int getFiles() {
|
||||
return files;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The folder on the server.
|
||||
*
|
||||
*
|
||||
* @return The folder on the server.
|
||||
*/
|
||||
public String getFolder() {
|
||||
return folder;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The name of the ftp server.
|
||||
*
|
||||
*
|
||||
* @return The name of the ftp server.
|
||||
*/
|
||||
public String getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the coordinate system used by the simulation.
|
||||
*
|
||||
*
|
||||
* @return The coordinate system.
|
||||
*/
|
||||
public CoordinateSystem getSystem() {
|
||||
return system;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* An enumeration with the variables.
|
||||
*
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public enum Variable {
|
||||
@@ -274,65 +277,67 @@ public class NumericPrediction {
|
||||
* The total precipitation (accumulated (!)).
|
||||
*/
|
||||
PREC("tot_prec", false),
|
||||
|
||||
|
||||
/**
|
||||
* The zonal wind. at 10m height.
|
||||
*/
|
||||
U10M("u_10m", false),
|
||||
|
||||
|
||||
/**
|
||||
* The meridional wind at 10m height.
|
||||
*/
|
||||
V10M("v_10m", false),
|
||||
|
||||
|
||||
/**
|
||||
* The weather interpretation type.
|
||||
*/
|
||||
WW("ww", false);
|
||||
|
||||
|
||||
/**
|
||||
* The name of the folder.
|
||||
*/
|
||||
private final String folder;
|
||||
|
||||
|
||||
/**
|
||||
* Determines whether the variable is time invariant.
|
||||
*/
|
||||
private final boolean invariant;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new variable.
|
||||
*
|
||||
* @param folder The folder name.
|
||||
* @param invariant True if time invariant.
|
||||
*
|
||||
* @param folder
|
||||
* The folder name.
|
||||
* @param invariant
|
||||
* True if time invariant.
|
||||
*/
|
||||
private Variable(String folder, boolean invariant) {
|
||||
this.folder = folder;
|
||||
this.invariant = invariant;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the folder name.
|
||||
*
|
||||
*
|
||||
* @return The folder name.
|
||||
*/
|
||||
public String getFolder() {
|
||||
return folder;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if the variable is time invariant.
|
||||
*
|
||||
*
|
||||
* @return True if time invariant, false otherwise.
|
||||
*/
|
||||
public boolean isInvariant() {
|
||||
return invariant;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Utility class used to represent time stamps.
|
||||
*
|
||||
* Utility class used to represent time stamps.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public static class Time implements Comparable<Time> {
|
||||
@@ -348,28 +353,33 @@ public class NumericPrediction {
|
||||
* The day of the month (1-31).
|
||||
*/
|
||||
public final int day;
|
||||
|
||||
/**
|
||||
* Creates a new time instance with the specified
|
||||
* year, month and day.
|
||||
*
|
||||
* @param year The year.
|
||||
* @param month The month.
|
||||
* @param day The day.
|
||||
* Creates a new time instance with the specified year, month and day.
|
||||
*
|
||||
* @param year
|
||||
* The year.
|
||||
* @param month
|
||||
* The month.
|
||||
* @param day
|
||||
* The day.
|
||||
*/
|
||||
public Time(int year, int month, int day) {
|
||||
this.year = year;
|
||||
this.month = month;
|
||||
this.day = day;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of the string.
|
||||
*
|
||||
* @param s The string.
|
||||
*
|
||||
* @param s
|
||||
* The string.
|
||||
* @return The time or null, if the string does not represent time.
|
||||
*/
|
||||
public static Time valueOf(String s) {
|
||||
if (s == null || s.length() != 8) return null;
|
||||
if (s == null || s.length() != 8)
|
||||
return null;
|
||||
try {
|
||||
int year = Integer.parseInt(s.substring(0, 4));
|
||||
int month = Integer.parseInt(s.substring(4, 6));
|
||||
@@ -379,11 +389,12 @@ public class NumericPrediction {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null || obj.getClass() != getClass()) return false;
|
||||
Time o = (Time)obj;
|
||||
if (obj == null || obj.getClass() != getClass())
|
||||
return false;
|
||||
Time o = (Time) obj;
|
||||
return o.year == year && o.month == month && o.day == day;
|
||||
}
|
||||
|
||||
@@ -391,7 +402,7 @@ public class NumericPrediction {
|
||||
public int hashCode() {
|
||||
return year << 16 | month << 8 | day;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int compareTo(Time o) {
|
||||
int c = Integer.compare(o.year, year);
|
||||
@@ -403,24 +414,24 @@ public class NumericPrediction {
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%04d%02d%02d", year, month, day);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The logger to generate debug output.
|
||||
*/
|
||||
protected Logger log = Logger.getLogger(getClass().getName());
|
||||
|
||||
|
||||
/**
|
||||
* The local folder that contains the data.
|
||||
*/
|
||||
protected File localFolder;
|
||||
|
||||
|
||||
/**
|
||||
* The variables that are synchronized.
|
||||
*/
|
||||
@@ -429,120 +440,147 @@ public class NumericPrediction {
|
||||
* The prediction.
|
||||
*/
|
||||
protected PredictionType type;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a numerical prediction from the specified prediction model.
|
||||
*
|
||||
* @param type The prediction model.
|
||||
* @param localFolder The local folder.
|
||||
*
|
||||
* @param type
|
||||
* The prediction model.
|
||||
* @param localFolder
|
||||
* The local folder.
|
||||
*/
|
||||
public NumericPrediction(PredictionType type, File localFolder) {
|
||||
this.type = type;
|
||||
this.localFolder = localFolder;
|
||||
variables = new Variable[] {
|
||||
type.getSystem().isComputed()?Variable.RLAT:Variable.CLAT,
|
||||
type.getSystem().isComputed()?Variable.RLON:Variable.CLON,
|
||||
Variable.HSURF, Variable.PLCOV, Variable.SOILTYP, Variable.PREC, Variable.T2M, Variable.CLCT, Variable.CLDEPTH,
|
||||
Variable.PMSL, Variable.WW, Variable.U10M, Variable.V10M, Variable.RELHUM2M, Variable.HSNOW, Variable.PS
|
||||
};
|
||||
|
||||
variables = new Variable[] { Variable.HSURF, Variable.PLCOV, Variable.SOILTYP, Variable.PREC, Variable.T2M,
|
||||
Variable.CLCT, Variable.CLDEPTH, Variable.PMSL, Variable.WW, Variable.U10M, Variable.V10M,
|
||||
Variable.RELHUM2M, Variable.HSNOW, Variable.PS,
|
||||
type.getSystem().isComputed() ? Variable.RLAT : Variable.CLAT,
|
||||
type.getSystem().isComputed() ? Variable.RLON : Variable.CLON };
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the prediction.
|
||||
*
|
||||
*
|
||||
* @return The prediction.
|
||||
*/
|
||||
public PredictionType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the local folder.
|
||||
*
|
||||
*
|
||||
* @return The local folder.
|
||||
*/
|
||||
public File getLocalFolder() {
|
||||
return localFolder;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the variables.
|
||||
*
|
||||
*
|
||||
* @return The variables.
|
||||
*/
|
||||
public Variable[] getVariables() {
|
||||
return variables;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the longitudes.
|
||||
*
|
||||
* @param time The time.
|
||||
*
|
||||
* @param time
|
||||
* The time.
|
||||
* @return The double array with the longitudes.
|
||||
* @throws IOException Thrown if something fails.
|
||||
* @throws IOException
|
||||
* Thrown if something fails.
|
||||
*/
|
||||
public DoubleArray getLongitues(Time time) throws IOException {
|
||||
if (type.getSystem().isComputed()) {
|
||||
Grib2File file = new Grib2File(createStream(getFile(Variable.RLON, time)));
|
||||
try {
|
||||
switch (type.getSystem()) {
|
||||
case REGULAR:
|
||||
RegularGridDecoder regular = new RegularGridDecoder(file.nextData().getGridSection());
|
||||
return regular.getLongitudes();
|
||||
case ROTATED:
|
||||
RotatedGridDecoder rotated = new RotatedGridDecoder(file.nextData().getGridSection());
|
||||
return rotated.getLongitudes();
|
||||
default:
|
||||
throw new IOException("Failed to decode unknown computed coordinate system.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IOException("Failed to read data.", e);
|
||||
} finally {
|
||||
file.close();
|
||||
Grib2File filterFile = new Grib2File(createStream(getFile(Variable.HSURF, time)));
|
||||
try {
|
||||
Grib2Data data = filterFile.nextData();
|
||||
BitmapInterpreter interpreter = new BitmapInterpreter(data.getBitmapSection(),
|
||||
data.getGridSection().getPointCount());
|
||||
DoubleArray result = null;
|
||||
if (type.getSystem().isComputed()) {
|
||||
Grib2File gridFile = new Grib2File(createStream(getFile(Variable.RLON, time)));
|
||||
try {
|
||||
switch (type.getSystem()) {
|
||||
case REGULAR:
|
||||
RegularGridDecoder regular = new RegularGridDecoder(gridFile.nextData().getGridSection());
|
||||
result = regular.getLongitudes();
|
||||
break;
|
||||
case ROTATED:
|
||||
RotatedGridDecoder rotated = new RotatedGridDecoder(gridFile.nextData().getGridSection());
|
||||
result = rotated.getLongitudes();
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Failed to decode unknown computed coordinate system.");
|
||||
}
|
||||
} finally {
|
||||
gridFile.close();
|
||||
}
|
||||
} else {
|
||||
result = getValues(getFile(Variable.CLON, time));
|
||||
}
|
||||
} else {
|
||||
return getValues(getFile(Variable.CLON, time));
|
||||
return interpreter.filter(result);
|
||||
} catch (Exception e) {
|
||||
throw new IOException("Failed to read data.", e);
|
||||
} finally {
|
||||
filterFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the latitudes.
|
||||
*
|
||||
* @param time The time.
|
||||
*
|
||||
* @param time
|
||||
* The time.
|
||||
* @return The double array with the latitudes.
|
||||
* @throws IOException Thrown if something fails.
|
||||
* @throws IOException
|
||||
* Thrown if something fails.
|
||||
*/
|
||||
public DoubleArray getLatitudes(Time time) throws IOException {
|
||||
if (type.getSystem().isComputed()) {
|
||||
Grib2File file = new Grib2File(createStream(getFile(Variable.RLAT, time)));
|
||||
try {
|
||||
switch (type.getSystem()) {
|
||||
case REGULAR:
|
||||
RegularGridDecoder regular = new RegularGridDecoder(file.nextData().getGridSection());
|
||||
return regular.getLatitudes();
|
||||
case ROTATED:
|
||||
RotatedGridDecoder rotated = new RotatedGridDecoder(file.nextData().getGridSection());
|
||||
return rotated.getLatitudes();
|
||||
default:
|
||||
throw new IOException("Failed to decode unknown computed coordinate system.");
|
||||
Grib2File filterFile = new Grib2File(createStream(getFile(Variable.HSURF, time)));
|
||||
try {
|
||||
Grib2Data data = filterFile.nextData();
|
||||
BitmapInterpreter interpreter = new BitmapInterpreter(data.getBitmapSection(),
|
||||
data.getGridSection().getPointCount());
|
||||
DoubleArray result = null;
|
||||
if (type.getSystem().isComputed()) {
|
||||
Grib2File gridFile = new Grib2File(createStream(getFile(Variable.RLAT, time)));
|
||||
try {
|
||||
switch (type.getSystem()) {
|
||||
case REGULAR:
|
||||
RegularGridDecoder regular = new RegularGridDecoder(gridFile.nextData().getGridSection());
|
||||
result = regular.getLatitudes();
|
||||
break;
|
||||
case ROTATED:
|
||||
RotatedGridDecoder rotated = new RotatedGridDecoder(gridFile.nextData().getGridSection());
|
||||
result = rotated.getLatitudes();
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Failed to decode unknown computed coordinate system.");
|
||||
}
|
||||
} finally {
|
||||
gridFile.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IOException("Failed to read data.", e);
|
||||
} finally {
|
||||
file.close();
|
||||
} else {
|
||||
result = getValues(getFile(Variable.CLAT, time));
|
||||
}
|
||||
} else {
|
||||
return getValues(getFile(Variable.CLAT, time));
|
||||
return interpreter.filter(result);
|
||||
} catch (Exception e) {
|
||||
throw new IOException("Failed to read data.", e);
|
||||
} finally {
|
||||
filterFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns a list of files that is available locally for a particular
|
||||
* variable.
|
||||
*
|
||||
* @param variable The variable.
|
||||
* Returns a list of files that is available locally for a particular variable.
|
||||
*
|
||||
* @param variable
|
||||
* The variable.
|
||||
* @return The list of files.
|
||||
*/
|
||||
public List<File> getFiles(Variable variable) {
|
||||
@@ -550,7 +588,7 @@ public class NumericPrediction {
|
||||
File[] files = folder.listFiles();
|
||||
List<File> result = new ArrayList<File>();
|
||||
if (files != null) {
|
||||
for (File f: files) {
|
||||
for (File f : files) {
|
||||
if (f.isFile() && f.getName().endsWith(GRIB_EXTENSION)) {
|
||||
result.add(f);
|
||||
}
|
||||
@@ -559,71 +597,80 @@ public class NumericPrediction {
|
||||
result.sort(new FileNameComparator());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the files that are available for a particular variable
|
||||
* at a particular time.
|
||||
*
|
||||
* @param variable The variable.
|
||||
* @param time The time.
|
||||
* Returns the files that are available for a particular variable at a
|
||||
* particular time.
|
||||
*
|
||||
* @param variable
|
||||
* The variable.
|
||||
* @param time
|
||||
* The time.
|
||||
* @return The list of files.
|
||||
*/
|
||||
public List<File> getFiles(Variable variable, Time time) {
|
||||
List<File> data = getFiles(variable);
|
||||
List<File> result = new ArrayList<>();
|
||||
for (File f: data) {
|
||||
for (File f : data) {
|
||||
if (f.getName().contains("_" + time)) {
|
||||
result.add(f);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the first file for the variable and time
|
||||
* or null, if the file is not available.
|
||||
*
|
||||
* @param variable The variable.
|
||||
* @param time The time
|
||||
* Returns the first file for the variable and time or null, if the file is not
|
||||
* available.
|
||||
*
|
||||
* @param variable
|
||||
* The variable.
|
||||
* @param time
|
||||
* The time
|
||||
* @return The first file or null, if there is no such file.
|
||||
*/
|
||||
public File getFile(Variable variable, Time time) {
|
||||
List<File> data = getFiles(variable, time);
|
||||
if (data.size() > 0) return data.get(0);
|
||||
if (data.size() > 0)
|
||||
return data.get(0);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file that represents the specified hour on
|
||||
* the specified day or null, if the file does not exist.
|
||||
*
|
||||
* @param variable The variable.
|
||||
* @param time The time.
|
||||
* @param hour The hour to search.
|
||||
* Returns the file that represents the specified hour on the specified day or
|
||||
* null, if the file does not exist.
|
||||
*
|
||||
* @param variable
|
||||
* The variable.
|
||||
* @param time
|
||||
* The time.
|
||||
* @param hour
|
||||
* The hour to search.
|
||||
* @return The file or null.
|
||||
*/
|
||||
public File getFile(Variable variable, Time time, int hour) {
|
||||
List<File> data = getFiles(variable, time);
|
||||
String search = String.format("_%03d_", hour);
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
if (data.get(i).getName().contains(search)) return data.get(i);
|
||||
if (data.get(i).getName().contains(search))
|
||||
return data.get(i);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Determines whether the data for a particular time is completely
|
||||
* available.
|
||||
*
|
||||
* @param time The time stamp.
|
||||
* Determines whether the data for a particular time is completely available.
|
||||
*
|
||||
* @param time
|
||||
* The time stamp.
|
||||
* @return True if all expected files are available. False otherwise.
|
||||
*/
|
||||
public boolean isAvailable(Time time) {
|
||||
boolean success = true;
|
||||
for (Variable v: variables) {
|
||||
for (Variable v : variables) {
|
||||
int count = getFiles(v, time).size();
|
||||
int expected = v.isInvariant()?1:(type.getFiles() - 1); // we exclude hour 0 since it is not available for cosmo ww
|
||||
int expected = v.isInvariant() ? 1 : (type.getFiles() - 1); // we exclude hour 0 since it is not available
|
||||
// for cosmo ww
|
||||
if (count != expected) {
|
||||
// do not return immediately to generate full debug log of variable mismatches
|
||||
log.log(Level.INFO, "Variable mismatch in " + v + ", expected " + expected + " was " + count + ".");
|
||||
@@ -631,65 +678,73 @@ public class NumericPrediction {
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a remote synchronization.
|
||||
*
|
||||
* @param listener An optional listener to keep track of changes.
|
||||
*
|
||||
* @param listener
|
||||
* An optional listener to keep track of changes.
|
||||
*/
|
||||
public void synchronize(FileStatusListener listener) {
|
||||
log.log(Level.INFO, "Synchronizing " + type.getServer() + type.getFolder() + " to " + localFolder.getAbsolutePath() + ".");
|
||||
for (Variable v: variables) {
|
||||
log.log(Level.INFO,
|
||||
"Synchronizing " + type.getServer() + type.getFolder() + " to " + localFolder.getAbsolutePath() + ".");
|
||||
for (Variable v : variables) {
|
||||
log.log(Level.INFO, "Synchronizing files for variable " + v.name());
|
||||
String f = v.getFolder();
|
||||
Predicate<File> fileFilter = new Predicate<File>() {
|
||||
@Override
|
||||
public boolean test(File t) {
|
||||
String substring = type.getSystem().getFilter();
|
||||
return (t.exists() || t.getName().contains(substring)) && getHour(t.getName()) != 0 && t.getName().endsWith(GRIB_EXTENSION);
|
||||
return (t.exists() || t.getName().contains(substring))
|
||||
&& (getHour(t.getName()) != 0 || v.isInvariant()) && t.getName().endsWith(GRIB_EXTENSION);
|
||||
}
|
||||
};
|
||||
FtpFolderSync sync = new FtpFolderSync(new File(localFolder, f), type.getServer(), type.getFolder() + f + "/", fileFilter);
|
||||
};
|
||||
FtpFolderSync sync = new FtpFolderSync(new File(localFolder, f), type.getServer(),
|
||||
type.getFolder() + f + "/", fileFilter);
|
||||
sync.setStatusListener(new FileStatusListener() {
|
||||
@Override
|
||||
public void onFileStatusChanged(FtpFolderSync source, FileStatus status) {
|
||||
log.log(Level.FINE, status.getAction().name() + ": " + status.getFile().getName());
|
||||
if (listener != null) listener.onFileStatusChanged(source, status);
|
||||
log.log(Level.INFO, status.getAction().name() + ": " + status.getFile().getName());
|
||||
if (listener != null)
|
||||
listener.onFileStatusChanged(source, status);
|
||||
}
|
||||
});
|
||||
try {
|
||||
sync.synchronize();
|
||||
} catch (IOException e) {
|
||||
log.log(Level.WARNING, "File synchronization failed for "+ v.name() + ".");
|
||||
log.log(Level.WARNING, "File synchronization failed for " + v.name() + ".");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extracts the forecast hour from the file.
|
||||
*
|
||||
* @param input The input file.
|
||||
*
|
||||
* @param input
|
||||
* The input file.
|
||||
* @return The forecast hour.
|
||||
* @throws IOException Thrown if the file cannot be read.
|
||||
* @throws IOException
|
||||
* Thrown if the file cannot be read.
|
||||
*/
|
||||
public int getForecastHour(File input) throws IOException {
|
||||
Grib2File file = new Grib2File(createStream(input));
|
||||
try {
|
||||
Grib2Data data = file.nextData();
|
||||
ForecastProduct product = (ForecastProduct)data.getProductSection().getTemplate();
|
||||
ForecastProduct product = (ForecastProduct) data.getProductSection().getTemplate();
|
||||
return product.time / 60;
|
||||
} catch (Exception e) {
|
||||
throw new IOException("Failed to extract forecast hour.", e);
|
||||
} finally {
|
||||
file.close();
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extracts the hour from the file name.
|
||||
*
|
||||
* @param filename The file name.
|
||||
*
|
||||
* @param filename
|
||||
* The file name.
|
||||
* @return The hour.
|
||||
*/
|
||||
public static int getHour(String filename) {
|
||||
@@ -705,43 +760,47 @@ public class NumericPrediction {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reads the values of the first data section of the file into a double array.
|
||||
*
|
||||
* @param input The input.
|
||||
*
|
||||
* @param input
|
||||
* The input.
|
||||
* @return The values of the first data section.
|
||||
* @throws IOException Thrown if the data cannot be loaded.
|
||||
* @throws IOException
|
||||
* Thrown if the data cannot be loaded.
|
||||
*/
|
||||
public DoubleArray getValues(File input) throws IOException {
|
||||
Grib2File file = new Grib2File(createStream(input));
|
||||
try {
|
||||
Grib2Data data = file.nextData();
|
||||
SimpleDataDecoder decoder = new SimpleDataDecoder(data);
|
||||
DoubleArray result = new DoubleArray((int)data.getGridSection().getPointCount());
|
||||
int s = (int)data.getGridSection().getPointCount();
|
||||
for (int i = 0; i < s; i++) {
|
||||
result.setValue(i, decoder.readNext());
|
||||
DoubleArray result = new DoubleArray((int) decoder.getPoints());
|
||||
int current = 0;
|
||||
for (int i = 0, s = (int) decoder.getSize(); i < s; i++) {
|
||||
boolean valid = decoder.hasNext();
|
||||
double value = decoder.readNext();
|
||||
if (valid)
|
||||
result.setValue(current++, value);
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
throw new IOException("Failed to read data.", e);
|
||||
} finally {
|
||||
file.close();
|
||||
file.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates an input stream from a bzip2 compressed file.
|
||||
*
|
||||
* @param input The input file.
|
||||
*
|
||||
* @param input
|
||||
* The input file.
|
||||
* @return The stream to the file data.
|
||||
* @throws IOException Thrown if the stream cannot be created.
|
||||
* @throws IOException
|
||||
* Thrown if the stream cannot be created.
|
||||
*/
|
||||
public InputStream createStream(File input) throws IOException {
|
||||
return new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(input)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
package com.mowesta.data.grib2.decoder;
|
||||
|
||||
import com.mowesta.data.grib2.section.BitmapSection;
|
||||
import com.mowesta.data.process.util.DoubleArray;
|
||||
|
||||
/**
|
||||
* Creates a new bitmap handler.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public class BitmapInterpreter {
|
||||
|
||||
/**
|
||||
* The bitmap or null.
|
||||
*/
|
||||
private byte[] bitmap;
|
||||
|
||||
/**
|
||||
* The grid size.
|
||||
*/
|
||||
private long size;
|
||||
|
||||
/**
|
||||
* The current index.
|
||||
*/
|
||||
private int current = 0;
|
||||
|
||||
/**
|
||||
* Creates a new bitmap handler.
|
||||
*
|
||||
* @param section The section.
|
||||
* @param size The point count.
|
||||
*/
|
||||
public BitmapInterpreter(BitmapSection section, long size) {
|
||||
this.size = size;
|
||||
if (section.getBitmapIndicator().getValue() == 0) {
|
||||
bitmap = section.getBitmap();
|
||||
} else {
|
||||
bitmap = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if there is a bitmap.
|
||||
*
|
||||
* @return True if there is a bitmap.
|
||||
*/
|
||||
public boolean hasBitmap() {
|
||||
return bitmap != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The total point count which may include
|
||||
* invalid points.
|
||||
*
|
||||
* @return The total point count.
|
||||
*/
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of valid points.
|
||||
*
|
||||
* @return The points.
|
||||
*/
|
||||
public long getPoints() {
|
||||
if (bitmap == null) {
|
||||
return size;
|
||||
} else {
|
||||
int points = 0;
|
||||
for (long i = 0, s = size; i < s; i++) {
|
||||
if (isSet(bitmap, i)) points += 1;
|
||||
}
|
||||
return points;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The number of bits in the bitmap.
|
||||
*
|
||||
* @return The number of bits in the bitmap.
|
||||
*/
|
||||
public int getBits() {
|
||||
return bitmap.length * 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the next value is present.
|
||||
*
|
||||
* @return True if the next value is present.
|
||||
*/
|
||||
public boolean hasNext() {
|
||||
if (current < size) {
|
||||
if (bitmap == null) {
|
||||
return true;
|
||||
} else {
|
||||
return isSet(this.bitmap, current);
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the next value.
|
||||
*
|
||||
* @return True if we moved.
|
||||
*/
|
||||
public boolean moveNext() {
|
||||
if (current < size) {
|
||||
current += 1;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters an existing array using the bitmap.
|
||||
*
|
||||
* @param input The input.
|
||||
* @return The filtered array.
|
||||
*/
|
||||
public DoubleArray filter(DoubleArray input) {
|
||||
if (input.getSize() != size) throw new RuntimeException("Unexpected filter input size.");
|
||||
DoubleArray result = new DoubleArray((int)getPoints());
|
||||
int next = 0;
|
||||
for (int i = 0, s = (int)size; i < s; i++) {
|
||||
if (bitmap == null || isSet(bitmap, i)) {
|
||||
result.setValue(next, input.getValue(i));
|
||||
next += 1;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines whether the i-th bit is set.
|
||||
*
|
||||
* @param bitmap The bitmap.
|
||||
* @param index The index of the bit.
|
||||
* @return True if the bit is set, false otherwise.
|
||||
*/
|
||||
public static boolean isSet(byte[] bitmap, long index) {
|
||||
int idx = (int)index / 8;
|
||||
if (idx < 0 || idx >= bitmap.length) return false;
|
||||
return ((0x80 & (bitmap[idx] & 0xff << index - idx * 8)) != 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,26 +6,26 @@ import com.mowesta.data.process.util.DoubleArray;
|
||||
|
||||
/**
|
||||
* A decoder for regular grids.
|
||||
*
|
||||
*
|
||||
* @author Marcus
|
||||
*
|
||||
*/
|
||||
public class RegularGridDecoder {
|
||||
|
||||
|
||||
/**
|
||||
* The typical devider for latitudes and longitudes.
|
||||
* Grib2 recommendation is micro-degrees.
|
||||
*/
|
||||
private static final double DIVIDER = 1e6;
|
||||
|
||||
|
||||
/**
|
||||
* The grid template.
|
||||
*/
|
||||
private AbstractFixedLatLonGrid grid;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new decoder.
|
||||
*
|
||||
*
|
||||
* @param section The grid section defining the grid.
|
||||
* @throws IllegalArgumentException Thrown if the section does
|
||||
* not contain a regular grid or if the scanning mode is not supported.
|
||||
@@ -38,7 +38,7 @@ public class RegularGridDecoder {
|
||||
throw new IllegalArgumentException("Decoder requires fixed latitude longitude grid.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Performs a sanity check on the coordinates.
|
||||
*/
|
||||
@@ -54,10 +54,10 @@ public class RegularGridDecoder {
|
||||
if (Math.abs(dlon - grid.di) > 1) // allow minor rounding errors (present in dwd output)
|
||||
throw new IllegalArgumentException("Unsupported di specification, expected " + grid.di + " computed " + dlon + " (unsupported scan mode?).");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the latitudes.
|
||||
*
|
||||
*
|
||||
* @return The latitudes.
|
||||
*/
|
||||
public DoubleArray getLatitudes() {
|
||||
@@ -74,10 +74,10 @@ public class RegularGridDecoder {
|
||||
}
|
||||
return latitudes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the longitude and corrects its values.
|
||||
*
|
||||
*
|
||||
* @param value The value.
|
||||
* @return The longitude.
|
||||
*/
|
||||
@@ -87,11 +87,11 @@ public class RegularGridDecoder {
|
||||
else if (result < -180) result += 360;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns the longitudes.
|
||||
*
|
||||
*
|
||||
* @return The longitudes.
|
||||
*/
|
||||
public DoubleArray getLongitudes() {
|
||||
@@ -109,10 +109,10 @@ public class RegularGridDecoder {
|
||||
}
|
||||
return longitudes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the latitude and corrects its values.
|
||||
*
|
||||
*
|
||||
* @param value The value.
|
||||
* @return The latitude.
|
||||
*/
|
||||
@@ -122,6 +122,6 @@ public class RegularGridDecoder {
|
||||
if (result < -90) result += 180;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ import com.mowesta.data.grib2.io.DataStream;
|
||||
import com.mowesta.data.grib2.template.data.SimpleData;
|
||||
|
||||
/**
|
||||
* A streaming decoder for data with simple packing.
|
||||
*
|
||||
* A streaming decoder for data with simple packing.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public class SimpleDataDecoder implements StreamDecoder {
|
||||
@@ -37,15 +37,21 @@ public class SimpleDataDecoder implements StreamDecoder {
|
||||
* The number of octets (= bits / 8).
|
||||
*/
|
||||
private int octets;
|
||||
|
||||
|
||||
/**
|
||||
* The bitmap interpreter.
|
||||
*/
|
||||
private BitmapInterpreter bitmap;
|
||||
|
||||
/**
|
||||
* Creates a simple decoder for the data.
|
||||
*
|
||||
*
|
||||
* @param data The data.
|
||||
* @throws RuntimeException Thrown if the decoder does not support the format.
|
||||
*/
|
||||
public SimpleDataDecoder(Grib2Data data) {
|
||||
SimpleData simpleData = (SimpleData)data.getRepresentationSection().getTemplate();
|
||||
this.bitmap = new BitmapInterpreter(data.getBitmapSection(), data.getGridSection().getPointCount());
|
||||
reference = simpleData.getReferenceValue();
|
||||
bscale = Math.pow(2, simpleData.getBinaryScaleFactor());
|
||||
dscale = Math.pow(10, -simpleData.getDecimalScaleFactor());
|
||||
@@ -54,9 +60,39 @@ public class SimpleDataDecoder implements StreamDecoder {
|
||||
if (bits % 8 != 0 || octets > 8) throw new RuntimeException("Unsupported bit count (" + bits + ")");
|
||||
stream = data.getStream();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the total number of valid and invalid points.
|
||||
*
|
||||
* @return The total number of points.
|
||||
*/
|
||||
public long getSize() {
|
||||
return bitmap.getSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of valid points.
|
||||
*
|
||||
* @return The number of valid points.
|
||||
*/
|
||||
public long getPoints() {
|
||||
return bitmap.getPoints();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the next value is valid.
|
||||
*
|
||||
* @return True if the next value is valid.
|
||||
*/
|
||||
public boolean hasNext() {
|
||||
return bitmap.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readNext() throws IOException {
|
||||
boolean next = bitmap.hasNext();
|
||||
bitmap.moveNext();
|
||||
if (!next) return Double.NaN;
|
||||
// special case, if bits is 0, return only reference value
|
||||
if (bits == 0) return reference;
|
||||
long longBits = 0;
|
||||
@@ -68,5 +104,5 @@ public class SimpleDataDecoder implements StreamDecoder {
|
||||
}
|
||||
return (longBits * bscale + reference) * dscale;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,17 +4,18 @@ import java.io.IOException;
|
||||
|
||||
/**
|
||||
* An interface for streaming decoders.
|
||||
*
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public interface StreamDecoder {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Reads the next value from the stream.
|
||||
*
|
||||
*
|
||||
* @return The next value.
|
||||
* @throws IOException Thrown by the stream.
|
||||
*/
|
||||
double readNext() throws IOException;
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class VideoGeneratorExample {
|
||||
* @throws IOException Thrown if the files cannot be read.
|
||||
*/
|
||||
public static void main(String[] args) throws IOException {
|
||||
Time time = new Time(2021, 1, 15);
|
||||
Time time = new Time(2021, 2, 14);
|
||||
|
||||
NumericPrediction[] predictions = new NumericPrediction[] {
|
||||
new NumericPrediction(PredictionType.ICON_GLOBAL, new File("dwd-data/global")),
|
||||
|
||||
@@ -6,6 +6,7 @@ import javax.ejb.EJB;
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.EntityTag;
|
||||
import javax.ws.rs.core.Request;
|
||||
@@ -23,7 +24,7 @@ import com.mowesta.service.session.LocationSessionBean;
|
||||
|
||||
/**
|
||||
* Implements the location service.
|
||||
*
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
@RequestScoped
|
||||
@@ -52,8 +53,8 @@ public class LocationResource implements ILocationService {
|
||||
*/
|
||||
@Inject
|
||||
Logger log;
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Response searchLocations(PageConfig pageConfig, SearchContext context, String term) {
|
||||
EntityTag tag = new EntityTag(Long.toString(locationSession.getLastModifiedLocation()));
|
||||
@@ -63,7 +64,7 @@ public class LocationResource implements ILocationService {
|
||||
}
|
||||
return b.tag(tag).build();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Response searchCities(@Valid PageConfig pageConfig, double longitude1, double latitude1, double longitude2, double latitude2) {
|
||||
EntityTag tag = new EntityTag(Long.toString(locationSession.getLastModifiedLocation()));
|
||||
@@ -73,7 +74,7 @@ public class LocationResource implements ILocationService {
|
||||
}
|
||||
return b.tag(tag).build();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Response getCities(PageConfig pageConfig) {
|
||||
@@ -91,7 +92,7 @@ public class LocationResource implements ILocationService {
|
||||
ResponseBuilder b = request.evaluatePreconditions(tag);
|
||||
if (b == null) {
|
||||
try {
|
||||
b = Response.ok(locationSession.getCity(id, false));
|
||||
b = Response.ok(locationSession.getCity(id, false));
|
||||
} catch (EntityNotFoundException e) {
|
||||
b = Response.status(Status.NOT_FOUND);
|
||||
}
|
||||
@@ -108,7 +109,7 @@ public class LocationResource implements ILocationService {
|
||||
String boundary = locationSession.getCityBoundary(id);
|
||||
if (boundary == null)
|
||||
b = Response.noContent();
|
||||
else
|
||||
else
|
||||
b = Response.ok(boundary);
|
||||
} catch (EntityNotFoundException e) {
|
||||
b = Response.status(Status.NOT_FOUND);
|
||||
@@ -133,7 +134,7 @@ public class LocationResource implements ILocationService {
|
||||
ResponseBuilder b = request.evaluatePreconditions(tag);
|
||||
if (b == null) {
|
||||
try {
|
||||
b = Response.ok(locationSession.getState(id, false));
|
||||
b = Response.ok(locationSession.getState(id, false));
|
||||
} catch (EntityNotFoundException e) {
|
||||
b = Response.status(Status.NOT_FOUND);
|
||||
}
|
||||
@@ -150,15 +151,20 @@ public class LocationResource implements ILocationService {
|
||||
String boundary = locationSession.getStateBoundary(id);
|
||||
if (boundary == null)
|
||||
b = Response.noContent();
|
||||
else
|
||||
else
|
||||
b = Response.ok(boundary);
|
||||
} catch (EntityNotFoundException e) {
|
||||
b = Response.status(Status.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
return b.tag(tag).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response getStateBoundaryDeprecated(@Valid @Min(0) long id) {
|
||||
return getStateBoundary(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response getStateCities(long id, PageConfig pageConfig) {
|
||||
EntityTag tag = new EntityTag(Long.toString(locationSession.getLastModifiedLocation()));
|
||||
@@ -173,7 +179,7 @@ public class LocationResource implements ILocationService {
|
||||
return b.tag(tag).build();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Response getCountries(PageConfig pageConfig) {
|
||||
EntityTag tag = new EntityTag(Long.toString(locationSession.getLastModifiedLocation()));
|
||||
@@ -183,21 +189,22 @@ public class LocationResource implements ILocationService {
|
||||
}
|
||||
return b.tag(tag).build();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Response getCountry(long id) {
|
||||
EntityTag tag = new EntityTag(Long.toString(locationSession.getLastModifiedCountry(id)));
|
||||
ResponseBuilder b = request.evaluatePreconditions(tag);
|
||||
if (b == null) {
|
||||
try {
|
||||
b = Response.ok(locationSession.getCountry(id, false));
|
||||
b = Response.ok(locationSession.getCountry(id, false));
|
||||
} catch (EntityNotFoundException e) {
|
||||
b = Response.status(Status.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
return b.tag(tag).build();
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public Response getCountryBoundary(long id) {
|
||||
EntityTag tag = new EntityTag(Long.toString(locationSession.getLastModifiedCountry(id)));
|
||||
@@ -207,7 +214,7 @@ public class LocationResource implements ILocationService {
|
||||
String boundary = locationSession.getCountryBoundary(id);
|
||||
if (boundary == null)
|
||||
b = Response.noContent();
|
||||
else
|
||||
else
|
||||
b = Response.ok(boundary);
|
||||
} catch (EntityNotFoundException e) {
|
||||
b = Response.status(Status.NOT_FOUND);
|
||||
@@ -215,7 +222,7 @@ public class LocationResource implements ILocationService {
|
||||
}
|
||||
return b.tag(tag).build();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Response getCountryCities(long id, PageConfig pageConfig) {
|
||||
EntityTag tag = new EntityTag(Long.toString(locationSession.getLastModifiedLocation()));
|
||||
|
||||
@@ -17,15 +17,15 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* A servlet that redirects requests to files and folders that do
|
||||
* not exist to a specific file. This will enable HTML5 style routes
|
||||
* A servlet that redirects requests to files and folders that do
|
||||
* not exist to a specific file. This will enable HTML5 style routes
|
||||
* produced by the Angular router when using the default settings.
|
||||
*
|
||||
*
|
||||
* See https://angular.io/guide/router#browser-url-styles for more
|
||||
* information on this topic. An alternative approach would be to
|
||||
* fallback to hashes, but the document provides some arguments,
|
||||
* why using HTML5 routes is a good idea.
|
||||
*
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public class AngularRouterServlet extends HttpServlet {
|
||||
@@ -34,36 +34,43 @@ public class AngularRouterServlet extends HttpServlet {
|
||||
* The serialization version.
|
||||
*/
|
||||
private static final long serialVersionUID = 3929749263522914374L;
|
||||
|
||||
|
||||
/**
|
||||
* A 4kb buffer to copy data.
|
||||
*/
|
||||
public static final int DEFAULT_BUFFER_SIZE = 4096;
|
||||
|
||||
|
||||
/**
|
||||
* The parameter to define a pattern to match.
|
||||
*/
|
||||
public static final String PARAMETER_PATTERN = "pattern";
|
||||
|
||||
|
||||
/**
|
||||
* The default pattern if nothing is configured.
|
||||
*/
|
||||
public static final String DEFAULT_PATTERN = ".*";
|
||||
|
||||
|
||||
/**
|
||||
* The parameter to define the output file that will be
|
||||
* delivered, if the resource with the specified name
|
||||
* delivered, if the resource with the specified name
|
||||
* cannot be found. This means that the angular app will
|
||||
* have to handle 404.
|
||||
* have to handle 404.
|
||||
*/
|
||||
public static final String PARAMETER_FILE = "file";
|
||||
|
||||
|
||||
/**
|
||||
* The default file that will be delivered, if the file
|
||||
* parameter is not configured.
|
||||
*/
|
||||
public static final String DEFAULT_FILE = "/index.html";
|
||||
|
||||
|
||||
/**
|
||||
* The path name segments of public files
|
||||
*/
|
||||
public static final String[] PUBLIC_FILES = new String[] {
|
||||
"app/mowesta-forecast", "app/mowesta-search"
|
||||
};
|
||||
|
||||
/**
|
||||
* The pattern to match.
|
||||
*/
|
||||
@@ -75,7 +82,7 @@ public class AngularRouterServlet extends HttpServlet {
|
||||
* handle unexploded war deployments as well.
|
||||
*/
|
||||
private URL file;
|
||||
|
||||
|
||||
@Override
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
super.init(config);
|
||||
@@ -91,33 +98,33 @@ public class AngularRouterServlet extends HttpServlet {
|
||||
throw new ServletException("Cannot access file.", e);
|
||||
}
|
||||
try {
|
||||
this.pattern = Pattern.compile(pattern);
|
||||
this.pattern = Pattern.compile(pattern);
|
||||
} catch (PatternSyntaxException e) {
|
||||
throw new ServletException("Cannot parse pattern.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected long getLastModified(HttpServletRequest req) {
|
||||
URL url = getFile(req);
|
||||
if (url != null) return getLastModified(url);
|
||||
else return -1;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
URL url = getFile(req);
|
||||
if (url != null) {
|
||||
setCorsHeader(url, resp);
|
||||
sendFile(req.getServletContext(), url, resp);
|
||||
sendFile(req.getServletContext(), url, resp);
|
||||
} else {
|
||||
sendNotFound(resp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the modification date of the file.
|
||||
*
|
||||
*
|
||||
* @param url The url pointing to a file.
|
||||
* @return The modification date.
|
||||
*/
|
||||
@@ -127,19 +134,19 @@ public class AngularRouterServlet extends HttpServlet {
|
||||
c.setDoInput(true);
|
||||
c.setDoOutput(false);
|
||||
try {
|
||||
return c.getLastModified();
|
||||
return c.getLastModified();
|
||||
} finally {
|
||||
c.getInputStream().close();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log("Failed to get modification date.", e);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the file that should be delivered for the request.
|
||||
*
|
||||
*
|
||||
* @param req The request.
|
||||
* @return The file.
|
||||
*/
|
||||
@@ -161,14 +168,14 @@ public class AngularRouterServlet extends HttpServlet {
|
||||
if (! pattern.matcher(requestPath).matches()) {
|
||||
return null;
|
||||
} else {
|
||||
return file;
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Copies the input to the output stream, no questions asked.
|
||||
* Note that the streams will not be closed after the operation.
|
||||
*
|
||||
*
|
||||
* @param in The input stream to read from.
|
||||
* @param out The output stream to write to.
|
||||
* @throws IOException Thrown if something fails.
|
||||
@@ -188,19 +195,20 @@ public class AngularRouterServlet extends HttpServlet {
|
||||
* @param resp The response.
|
||||
*/
|
||||
protected void setCorsHeader(URL url, HttpServletResponse resp) {
|
||||
boolean setCorsHeader = url.getPath().contains("weather-forecast-today")
|
||||
|| url.getPath().contains("weather-search");
|
||||
if (setCorsHeader) {
|
||||
resp.setHeader("Access-Control-Allow-Origin", "*");
|
||||
resp.setHeader("Access-Control-Allow-Methods", "origin, content-type, accept");
|
||||
resp.setHeader("Access-Control-Allow-Headers", "GET");
|
||||
resp.setHeader("Access-Control-Max-Age", "3600");
|
||||
}
|
||||
for (String file: PUBLIC_FILES) {
|
||||
if (url.getPath().contains(file)) {
|
||||
resp.setHeader("Access-Control-Allow-Origin", "*");
|
||||
resp.setHeader("Access-Control-Allow-Methods", "origin, content-type, accept");
|
||||
resp.setHeader("Access-Control-Allow-Headers", "GET");
|
||||
resp.setHeader("Access-Control-Max-Age", "3600");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sends the file to the response.
|
||||
*
|
||||
*
|
||||
* @param context The servlet context.
|
||||
* @param file The file to send.
|
||||
* @param resp The response.
|
||||
@@ -220,23 +228,23 @@ public class AngularRouterServlet extends HttpServlet {
|
||||
try {
|
||||
OutputStream out = resp.getOutputStream();
|
||||
try {
|
||||
copy(in, out);
|
||||
copy(in, out);
|
||||
} finally {
|
||||
out.close();
|
||||
out.close();
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sends a not found error to the response.
|
||||
*
|
||||
*
|
||||
* @param resp The response.
|
||||
* @throws IOException Thrown if status code cannot be sent.
|
||||
*/
|
||||
protected void sendNotFound(HttpServletResponse resp) throws IOException {
|
||||
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user