VLab Issue #3722 - SWPC delivery for 14.3.1

Upgrades to Geomag and solar image processing

Change-Id: Ibe33d00843419e34998274a87734f3f29d2b63dc

Former-commit-id: 5da74a455d [formerly 5da74a455d [formerly c9bcc3d15adcea014696e5cd68b9688506ee07d9]]
Former-commit-id: 4d2154d2de
Former-commit-id: c3b1cda7c5
This commit is contained in:
Stephen Gilbert 2014-05-15 15:43:44 -04:00
parent 7730224fe9
commit bcc628c19f
29 changed files with 3840 additions and 4157 deletions

View file

@ -7,12 +7,12 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: com.raytheon.edex.common;bundle-version="1.12.1174",
javax.persistence;bundle-version="1.0.0"
Export-Package: gov.noaa.nws.ncep.common.dataplugin.geomag,
gov.noaa.nws.ncep.common.dataplugin.geomag.calculation,
gov.noaa.nws.ncep.common.dataplugin.geomag.dao,
gov.noaa.nws.ncep.common.dataplugin.geomag.exception,
gov.noaa.nws.ncep.common.dataplugin.geomag.request,
gov.noaa.nws.ncep.common.dataplugin.geomag.table,
gov.noaa.nws.ncep.common.dataplugin.geomag.util
Import-Package: com.raytheon.uf.common.localization,
gov.noaa.nws.ncep.common.dataplugin.geomag,
com.raytheon.uf.common.serialization.comm,
org.apache.commons.logging

View file

@ -9,7 +9,6 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
//import javax.persistence.UniqueConstraint;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@ -22,6 +21,8 @@ import com.raytheon.uf.common.dataplugin.persist.PersistableDataObject;
import com.raytheon.uf.common.serialization.annotations.DynamicSerialize;
import com.raytheon.uf.common.serialization.annotations.DynamicSerializeElement;
//import javax.persistence.UniqueConstraint;
/**
* Record implementation for geomag avg.
*
@ -30,6 +31,7 @@ import com.raytheon.uf.common.serialization.annotations.DynamicSerializeElement;
* Date Ticket# Engineer Description
* ------------ ---------- ---------------- --------------------------
* 08/14/2013 T989 qzhou Initial creation.
* 03/03/2014 qzhou modified get/set
* </pre>
*
* @author qzhou
@ -44,11 +46,11 @@ import com.raytheon.uf.common.serialization.annotations.DynamicSerializeElement;
@DynamicSerialize
public class GeoMagAvg extends PersistableDataObject<Object> {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final String ID_GEN = "idgen";
/** The id */
@ -83,7 +85,7 @@ public class GeoMagAvg extends PersistableDataObject<Object> {
/**
* H data Hour Average
*/
@Column(length=16)
@Column
@XmlAttribute
@DynamicSerializeElement
private float hHrAvg;
@ -91,12 +93,11 @@ public class GeoMagAvg extends PersistableDataObject<Object> {
/**
* D data Hour Average
*/
@Column(length=16)
@Column
@XmlAttribute
@DynamicSerializeElement
private float dHrAvg;
public GeoMagAvg() {
}
@ -108,22 +109,22 @@ public class GeoMagAvg extends PersistableDataObject<Object> {
/**
* @return the hHrAvg
*/
public float getHHrAvg() {
public float gethHrAvg() {
return hHrAvg;
}
public void setHHrAvg(float hHrAvg) {
public void sethHrAvg(float hHrAvg) {
this.hHrAvg = hHrAvg;
}
/**
* @return the dHrAvg
*/
public float getDHrAvg() {
public float getdHrAvg() {
return dHrAvg;
}
public void setDHrAvg(float dHrAvg) {
public void setdHrAvg(float dHrAvg) {
this.dHrAvg = dHrAvg;
}
@ -150,7 +151,7 @@ public class GeoMagAvg extends PersistableDataObject<Object> {
}
/**
* @return the timeTag
* @return the insert time
*/
public Date getInsertTime() {
return insertTime;

View file

@ -29,6 +29,7 @@ import com.raytheon.uf.common.serialization.annotations.DynamicSerializeElement;
* Date Ticket# Engineer Description
* ------------ ---------- ---------------- --------------------------
* 08/14/2013 T989 qzhou Initial creation.
* 03/03/2014 #1110 qzhou modified get/set
* </pre>
*
* @author qzhou
@ -47,6 +48,7 @@ public class GeoMagK1min extends PersistableDataObject<Object> {
*
*/
private static final long serialVersionUID = 1L;
public static final String ID_GEN = "idgen";
/** The id */
@ -169,8 +171,6 @@ public class GeoMagK1min extends PersistableDataObject<Object> {
@DynamicSerializeElement
private float ks;
public GeoMagK1min() {
}
@ -180,7 +180,7 @@ public class GeoMagK1min extends PersistableDataObject<Object> {
}
/**
* @return the hHrAvg
* @return the kestIndex
*/
public int getKestIndex() {
return kestIndex;
@ -191,7 +191,7 @@ public class GeoMagK1min extends PersistableDataObject<Object> {
}
/**
* @return the dHrAvg
* @return the kestReal
*/
public float getKestReal() {
return kestReal;
@ -202,7 +202,7 @@ public class GeoMagK1min extends PersistableDataObject<Object> {
}
/**
* @return the hHrAvg
* @return the kestGamma
*/
public float getKestGamma() {
return kestGamma;
@ -213,90 +213,90 @@ public class GeoMagK1min extends PersistableDataObject<Object> {
}
/**
* @return the hHrAvg
* @return the hkReal
*/
public float getHkReal() {
return hkReal;
}
public void setHkReal(float hkReal) {
this.hkReal = hkReal;
}
/**
* @return the hkGamma
*/
public float getHkGamma() {
return hkGamma;
}
public void setHkGamma(float hkGamma) {
this.hkGamma = hkGamma;
}
/**
* @return the hkIndex
*/
public int getHkIndex() {
return hkIndex;
}
public void setHKIndex(int hkIndex) {
public void setHkIndex(int hkIndex) {
this.hkIndex = hkIndex;
}
/**
* @return the dHrAvg
* @return the dkIndex
*/
public float getHKReal() {
return hkReal;
}
public void setHKReal(float hkReal) {
this.hkReal = hkReal;
}
/**
* @return the hHrAvg
*/
public float getHKGamma() {
return hkGamma;
}
public void setHKGamma(float hkGamma) {
this.hkGamma = hkGamma;
}
/**
* @return the hHrAvg
*/
public int getDKIndex() {
public int getDkIndex() {
return dkIndex;
}
public void setDKIndex(int dkIndex) {
public void setDkIndex(int dkIndex) {
this.dkIndex = dkIndex;
}
/**
* @return the dHrAvg
* @return the dkReal
*/
public float getDKReal() {
public float getDkReal() {
return dkReal;
}
public void setDKReal(float dkReal) {
public void setDkReal(float dkReal) {
this.dkReal = dkReal;
}
/**
* @return the hHrAvg
* @return the dkGamma
*/
public float getDKGamma() {
public float getDkGamma() {
return dkGamma;
}
public void setDKGamma(float dkGamma) {
public void setDkGamma(float dkGamma) {
this.dkGamma = dkGamma;
}
/**
* @return the dHrAvg
* @return the hCount
*/
public float getHCount() {
public int gethCount() {
return hCount;
}
public void setHCount(int hCount) {
public void sethCount(int hCount) {
this.hCount = hCount;
}
/**
* @return the dHrAvg
* @return the dCount
*/
public int getDCount() {
public int getdCount() {
return dCount;
}
public void setDCount(int dCount) {
public void setdCount(int dCount) {
this.dCount = dCount;
}

View file

@ -44,11 +44,9 @@ public class GeoMagPathProvider extends DefaultPathProvider {
Date time = (Date) pdo.getDataTime().getRefTime().clone();
time.setHours(0);
// //System.out.println("***Path "+pdo.getDataTime().getRefTime() +" "+ fileNameFormat.get().format(pdo.getDataTime().getRefTime()));
sb.append(fileNameFormat.get().format(time));
sb.append(".h5");
}
return sb.toString();
}
}

View file

@ -1,6 +1,5 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
@ -21,6 +20,7 @@ import com.raytheon.uf.common.dataplugin.persist.IHDFFilePathProvider;
import com.raytheon.uf.common.dataplugin.persist.PersistablePluginDataObject;
import com.raytheon.uf.common.serialization.annotations.DynamicSerialize;
import com.raytheon.uf.common.serialization.annotations.DynamicSerializeElement;
/**
* Record implementation for geomag plugin.
*
@ -52,46 +52,81 @@ public class GeoMagRecord extends PersistablePluginDataObject {
private static final long serialVersionUID = 1L;
public static final String OBS_TIME = "Obs_Time";// h5
public static final String Comp_Insert_Time = "Comp_Insert_Time";
public static final String component1 = "Component1";
public static final String component2 = "Component2";
public static final String component3 = "Component3";
public static final String component4 = "Component4";
public static final String H_HR_AVG = "Hr_Avg_H";
public static final String D_HR_AVG = "Hr_Avg_D";
public static final String K_Index = "P3h_K_Index";
public static final String K_Real = "P3h_K_Real";
public static final String K_Gamma = "P3h_Gamma";
public static final String Kest_Index = "P3h_Kest_Index";
public static final String Kest_Real = "P3h_Kest_Real";
public static final String Kest_Gamma = "P3h_Kest_Gamma";
public static final String KH_Real = "P3h_Hk_Real";
public static final String KH_Gamma = "P3h_H_Gamma";
public static final String KD_Real = "P3h_Dk_Real";
public static final String KD_Gamma = "P3h_D_Gamma";
public static final String A_Running = "P3h_A_Running";
public static final String A_Final_Running = "P3h_A_Final_Running";
public static final String KH_Int = "P3h_Hk_Int";
public static final String KD_Int = "P3h_Dk_Int";
public static final String Last_Update = "P3h_Last_Update";
public static final String Kest_Index_1m = "P1m_Kest_Index";
public static final String Kest_Real_1m = "P1m_Kest_Real";
public static final String Kest_Gamma_1m = "P1m_Kest_Gamma";
public static final String KH_Real_1m = "P1m_Hk_Real";
public static final String KH_Gamma_1m = "P1m_H_Gamma";
public static final String KH_Index_1m = "P1m_Hk_Index";
public static final String KD_Real_1m = "P1m_Dk_Real";
public static final String KD_Gamma_1m = "P1m_D_Gamma";
public static final String KD_Index_1m = "P1m_Dk_Index";
public static final String KH_Count = "P1m_H_Count";
public static final String KD_Count = "P1m_D_Count";
public static final String KH_Dev = "P1m_H_Dev";
public static final String KD_Dev = "P1m_D_Dev";
public static final String A_est = "P1m_A_est";
public static final String K_s = "P1m_K_s";
public static final String Last_Update_1m = "P1m_Last_Update";
/**
@ -121,14 +156,6 @@ public class GeoMagRecord extends PersistablePluginDataObject {
@XmlAttribute
private String reportType;
// /**
// * index for data in hdf5, 0-1439
// */
// @Column
// @DynamicSerializeElement
// @XmlAttribute
// private int idx;
/**
* flag to indicate bad data point
*/
@ -223,77 +250,6 @@ public class GeoMagRecord extends PersistablePluginDataObject {
@Transient
private float[] comp4_data;
// /**
// * Following for HrAvg values
// */
// @Transient
// private float[] hrAvgH;
//
// @Transient
// private float[] hrAvgD;
//
// @Transient
// private long[] comp_InsertTime;
//
// /**
// * Following for K values
// */
// // 3hr
// @Transient
// private int[] kK_Index;
// @Transient
// private float[] kK_Real;
// @Transient
// private float[] kK_Gamma;
// @Transient
// private int[] kest_Index;
// @Transient
// private float[] kest_Real;
// @Transient
// private float[] kest_Gamma;
// @Transient
// private float[] hK_Real;
// @Transient
// private float[] hK_Gamma;
// @Transient
// private float[] dK_Real;
// @Transient
// private float[] dK_Gamma;
//
// // 1 min
// @Transient
// private int[] h_Count;
// @Transient
// private int[] d_Count;
// @Transient
// private int[] kest_Index_1m;
// @Transient
// private float[] kest_Real_1m;
// @Transient
// private float[] kest_Gamma_1m;
// @Transient
// private float[] hK_Real_1m;
// @Transient
// private float[] hK_Gamma_1m;
// @Transient
// private float[] dK_Real_1m;
// @Transient
// private float[] dK_Gamma_1m;
// @Transient
// private int[] hK_Index_1m;
// @Transient
// private int[] dK_Index_1m;
// @Transient
// private float[] h_Dev;
// @Transient
// private float[] d_Dev;
// @Transient
// private float[] Ks;
// @Transient
// private int[] a_est;
// @Transient
// private float[] update_1m;
/**
* No-arg Constructor
*/
@ -349,7 +305,8 @@ public class GeoMagRecord extends PersistablePluginDataObject {
}
/**
* @param the badDataPoint to set
* @param the
* badDataPoint to set
*/
public void setBadDataPoint(int badDataPoint) {
this.badDataPoint = badDataPoint;
@ -376,6 +333,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
public long[] getObsTimes() {
return obs_times;
}
public void setObsTimes(long[] obs_times) {
this.obs_times = obs_times;
}
@ -504,252 +462,11 @@ public class GeoMagRecord extends PersistablePluginDataObject {
this.cadenceId = cadenceId;
}
// /*
// * @param k index related
// */
// // 3hr
// public int[] getKKIndex() {
// return kK_Index;
// }
//
// public void setKKIndex(int[] kK_Index) {
// this.kK_Index = kK_Index;
// }
//
// public float[] getKKReal() {
// return kK_Real;
// }
//
// public void setKKReal(float[] kK_Real) {
// this.kK_Real = kK_Real;
// }
//
// public float[] getKKGamma() {
// return kK_Gamma;
// }
//
// public void setKKGamma(float[] kK_Gamma) {
// this.kK_Gamma = kK_Gamma;
// }
//
//
// public float[] getHKReal() {
// return hK_Real;
// }
//
// public void setHKReal(float[] hK_Real) {
// this.hK_Real = hK_Real;
// }
//
// public float[] getDKReal() {
// return dK_Real;
// }
//
// public void setDKReal(float[] dK_Real) {
// this.dK_Real = dK_Real;
// }
//
// public int[] getKestIndex() {
// return kest_Index;
// }
//
// public void setKestIndex(int[] kest_Index) {
// this.kest_Index = kest_Index;
// }
//
// public float[] getKestReal() {
// return kest_Real;
// }
//
// public void setKestReal(float[] kest_Real) {
// this.kest_Real = kest_Real;
// }
//
// public float[] getKestGamma() {
// return kest_Gamma;
// }
//
// public void setKestGamma(float[] kest_Gamma) {
// this.kest_Gamma = kest_Gamma;
// }
//
// public float[] getHKGamma() {
// return hK_Gamma;
// }
//
// public void setHKGamma(float[] hK_Gamma) {
// this.hK_Gamma = hK_Gamma;
// }
//
// public float[] getDKGamma() {
// return dK_Gamma;
// }
//
// public void setDKGamma(float[] dK_Gamma) {
// this.dK_Gamma = dK_Gamma;
// }
//
// // 1min
// public float[] getHKReal1m() {
// return hK_Real_1m;
// }
//
// public void setHKReal1m(float[] hK_Real_1m) {
// this.hK_Real_1m = hK_Real_1m;
// }
//
// public float[] getDKReal1m() {
// return dK_Real_1m;
// }
//
// public void setDKReal1m(float[] dK_Real_1m) {
// this.dK_Real_1m = dK_Real_1m;
// }
//
// public int[] getKestIndex1m() {
// return kest_Index_1m;
// }
//
// public void setKestIndex1m(int[] kest_Index_1m) {
// this.kest_Index_1m = kest_Index_1m;
// }
//
// public float[] getKestReal1m() {
// return kest_Real_1m;
// }
//
// public void setKestReal1m(float[] kest_Real_1m) {
// this.kest_Real_1m = kest_Real_1m;
// }
//
// public float[] getKestGamma1m() {
// return kest_Gamma_1m;
// }
//
// public void setKestGamma1m(float[] kest_Gamma_1m) {
// this.kest_Gamma_1m = kest_Gamma_1m;
// }
//
// public float[] getHKGamma1m() {
// return hK_Gamma_1m;
// }
//
// public void setHKGamma1m(float[] hK_Gamma_1m) {
// this.hK_Gamma_1m = hK_Gamma_1m;
// }
//
// public float[] getDKGamma1m() {
// return dK_Gamma_1m;
// }
//
// public void setDKGamma1m(float[] dK_Gamma_1m) {
// this.dK_Gamma_1m = dK_Gamma_1m;
// }
//
// public int[] getHKIndex1m() {
// return hK_Index_1m;
// }
//
// public void setHKIndex1m(int[] hK_Index_1m) {
// this.hK_Index_1m = hK_Index_1m;
// }
//
// public int[] getDKIndex1m() {
// return dK_Index_1m;
// }
//
// public void setDKIndex1m(int[] dK_Index_1m) {
// this.dK_Index_1m = dK_Index_1m;
// }
//
// public int[] getHCount() {
// return h_Count;
// }
//
// public void setHCount(int[] h_Count) {
// this.h_Count = h_Count;
// }
//
// public int[] getDCount() {
// return d_Count;
// }
//
// public void setDCount(int[] d_Count) {
// this.d_Count = d_Count;
// }
//
// public float[] getHDev() {
// return h_Dev;
// }
//
// public void setHDev(float[] h_Dev) {
// this.h_Dev = h_Dev;
// }
//
// public float[] getDDev() {
// return d_Dev;
// }
//
// public void setDDev(float[] d_Dev) {
// this.d_Dev = d_Dev;
// }
//
// public float[] getKs() {
// return Ks;
// }
//
// public void setKs(float[] Ks) {
// this.Ks = Ks;
// }
//
// public int[] getAest() {
// return a_est;
// }
//
// public void setAest(int[] a_est) {
// this.a_est = a_est;
// }
//
// public float[] getLastUpdate() {
// return update_1m;
// }
//
// public void setLastUpdate(float[] update_1m) {
// this.update_1m = update_1m;
// }
@Override
public IDecoderGettable getDecoderGettable() {
return null;
}
// public void retrieveFromDataStore(IDataStore dataStore) {
//
// try {
// IDataRecord[] dataRec = dataStore.retrieve(getDataURI());
// for (int i = 0; i < dataRec.length; i++) {
// if (dataRec[i].getName().equals(GeoMagRecord.OBS_TIME)) {
// obs_times = (((LongDataRecord) dataRec[i]).getLongData());
// }
// if (dataRec[i].getName().equals(GeoMagRecord.component1)) {
// comp1_data = (((FloatDataRecord) dataRec[i]).getFloatData());
// }
// if (dataRec[i].getName().equals(GeoMagRecord.component2)) {
// comp2_data = (((FloatDataRecord) dataRec[i]).getFloatData());
// }
// if (dataRec[i].getName().equals(GeoMagRecord.component3)) {
// comp3_data = (((FloatDataRecord) dataRec[i]).getFloatData());
// }
// if (dataRec[i].getName().equals(GeoMagRecord.component4)) {
// comp4_data = (((FloatDataRecord) dataRec[i]).getFloatData());
// }
// }
//
// } catch (Exception se) {
// se.printStackTrace();
// }
// }
@Override
@Column
@Access(AccessType.PROPERTY)

View file

@ -0,0 +1,483 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag.calculation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
* The calculation of k, 1 minute related.
*
* <pre>
* SOFTWARE HISTORY
*
* ate Ticket# Engineer Description
* ----------- ---------- ---------- --------------------------
* 05/14/2013 #989 qzhou Initial Creation
* 03/18/2014 #1123 qzhou Add getHdevOrDDev
* 04/09/2014 #1123 qzhou Modified getKIndex for gamma value
* </pre>
*
* @author qzhou
* @version 1
*/
public class CalcEach1min {
private static final float MISSING_VAL = 99999.99f;
private static final int MAX_GAP_LENGTH = 15;
private static final int SMOOTH_WINDOW = 60;//
private static final int TRANSITION_TIME = 60;
private static final int PHASE_POWER = 3;
private static final int HARM_ORDER = 5;
private static int HOURS = 24;
private static int MINUTES = 60;
/*
* @param dataIn -- data of 4320
*/
public static float[] fillGaps(float[] dataIn) {
float[] data = dataIn.clone();
int i = 0;
int size = data.length;
while (i < size) {
// Find the next missing value
int flag = 0; // flag used for break
while (i < size && flag == 0) {
if (data[i] == MISSING_VAL)
flag = 1;
else
i++;
}
// If a gap was found handle it
if (i < size) {
int gapIndex = i; // index of first missing value
// Find the last missing point
flag = 0;
while (i < size && flag == 0) {
if (data[i] != MISSING_VAL)
flag = 1;
else
i++;
}
// Interpolate the gap if possible. We cannot extrapolate
if ((gapIndex > 0) && (i < size)) {
// Now i is the index of first non-missing value
// and GapIndex is the index of first missing value
int gapLength = i - gapIndex; // i is index of first
// non-missing value
// Interpolate if the gap is small enough
if (gapLength < MAX_GAP_LENGTH) {
float value1 = data[gapIndex - 1];
float value2 = data[i];
for (int j = 1; j < gapLength + 1; j++)
data[gapIndex++] = value1 + (j * (value2 - value1))
/ (gapLength + 1);
}
}
}
}
return data;
}
/*
* 24 element floating point array. (DefLength + 30 + kLength) Find out how
* many points are used to get the centered hour average
*/
public static float[] getFitLength(float[] defLength, float[] kIndex,
float[] kLength) {
float[] fitLength = new float[HOURS];
int[] ind = new int[HOURS];
float[] curK = new float[HOURS];
for (int i = 0; i < HOURS; i++) {
fitLength[i] = 30.0f + defLength[i];
ind[i] = (int) Math.floor(i / 3);
curK[i] = kIndex[ind[i]];
if (curK[i] != MISSING_VAL)
fitLength[i] += kLength[(int) curK[i]];
if (fitLength[i] > 1440)
fitLength[i] = 1440;
}
return fitLength;
}
/*
* @param data (hhdata, dddata), float[4320]
*
* @return -- 24 element floating point array. Calculate averages centered
* on each hour of the day
*/
public static float[] getCentHourAvg(float[] data, float[] fitLength,
float[] defLength) {
float[] HrAvg = new float[HOURS]; // double
Arrays.fill(HrAvg, MISSING_VAL);
for (int ihr = 0; ihr < HOURS; ihr++) {
// take middle interval
int center = 1440 + ihr * MINUTES + 30;
int start = center - Math.round(fitLength[ihr]);
int end = center + Math.round(fitLength[ihr]);
int missing = 0;
double sum = 0;
// if data[i] have no missing value
for (int i = start; i < end + 1; i++) {
if (data[i] != MISSING_VAL) {
sum += data[i];
} else {
missing++;
break;// this loop
}
}
if (missing == 0) // no missing value
HrAvg[ihr] = (float) sum / (end - start + 1);
}
// if HrAvg have missing value
// Extrapolate the first missing points--missing beginning
int hr0 = 0;
int flag = 0;
while (hr0 < HOURS && flag == 0) {
if (HrAvg[hr0] != MISSING_VAL)
flag = 1;
else {
hr0++;
}
}
if (hr0 > 0 && hr0 < HOURS)
for (int i = 0; i < hr0; i++)
HrAvg[i] = HrAvg[hr0];
// Extrapolate the last missing points--missing end
int hr1 = 23;
while ((hr1 > hr0) && (HrAvg[hr1] == MISSING_VAL))
hr1--;
if (hr1 < 23)
for (int i = hr1 + 1; i < HOURS; i++)
HrAvg[i] = HrAvg[hr1];
// Interpolate the missing points between hour0 and hour1
// Both hour0 and hour1 are hours where data exists
while (hr0 < hr1) {
do {
hr0++;
} while (hr0 < hr1 && HrAvg[hr0] != MISSING_VAL);
if (hr0 < hr1) {
int hr = hr0; // first missing hour
while ((hr0 < hr1) && (HrAvg[hr0] == MISSING_VAL))
hr0++;
int gapLength = hr0 - hr;
float value1 = HrAvg[hr - 1];// not missing
float value2 = HrAvg[hr0]; // not missing
for (int i = 1; i < gapLength + 1; i++)
HrAvg[hr++] = value1 + (i * (value2 - value1))
/ (gapLength + 1);
}
}
return HrAvg;
}
/*
* @param hrAvg -- QHA data 1440
*/
public static float[] getHarmonicFit(float[] hrCentAvg) {
float[] fitCurve = new float[1440];
int delta = MINUTES; // minutes between points in HrAvg
int t0 = 30; // time tag for first point in HrAvg
float t1 = (HOURS - 1) * delta + t0; // time tag for last point in HrAvg
// Rotate HrAvg so that 1st and last points are equal, store in HA
float r_coeff = (hrCentAvg[HOURS - 1] - hrCentAvg[0]) / (t1 - t0);
float[] hrA = new float[HOURS];// 0.0*HrAvg
for (int i = 0; i < HOURS; i++)
hrA[i] = hrCentAvg[i] - r_coeff * (i * delta);
// Calculate first Fourier series coefficients up to Horder
float[] reA = new float[HARM_ORDER + 1]; // real part of the Fourier
// Series Coefficients
// (initially 0)
float[] imA = new float[HARM_ORDER + 1]; // imaginary part of Fourier
// Series Coefficients
// (initially 0)
for (int i = 0; i < HARM_ORDER + 1; i++) {
for (int j = 0; j < HOURS; j++) {
reA[i] += hrA[j] * Math.cos(2 * (Math.PI) * j * i / HOURS);
imA[i] -= hrA[j] * Math.sin(2 * (Math.PI) * j * i / HOURS);
}
}
// Derive FitCurve as harmonic fit using inverse transform
for (int t = 0; t < HOURS * delta; t++) { // t is minute of the day
float theta = (float) (2 * (Math.PI) * (t - t0) / (HOURS * delta));
fitCurve[t] = reA[0] / HOURS;
for (int i = 1; i < HARM_ORDER + 1; i++)
fitCurve[t] += (2 * reA[i] * Math.cos(i * theta) - 2 * imA[i]
* Math.sin(i * theta))
/ HOURS;
// Derotate FitCurve by same amount as HrAvg
fitCurve[t] += r_coeff * (t - t0);
}
return fitCurve;
}
/*
* @param hdev,ddev -- float[1440]
*/
public static List getKIndex(float[] hdev, float[] ddev, int[] kLimit,
int missingFlag) {
List<float[]> list = new ArrayList<float[]>();
// Initialize the return data with MissingValue
float[] kIndex = new float[8];
float[] hk = new float[8];
float[] dk = new float[8];
float[] gamma = new float[8];
float[] hGamma = new float[8];
float[] dGamma = new float[8];
Arrays.fill(kIndex, MISSING_VAL);
Arrays.fill(hk, MISSING_VAL);
Arrays.fill(dk, MISSING_VAL);
Arrays.fill(gamma, MISSING_VAL);
Arrays.fill(hGamma, MISSING_VAL);
Arrays.fill(dGamma, MISSING_VAL);
// Check for bad input data
int npts = hdev.length;
if (npts != ddev.length)
return list;
if (npts < 1261 || npts > 1440) // 21*60+1
return list;
// Step through each three hourly interval
for (int ipd = 0; ipd < 8; ipd++) {
int istart = ipd * 180;
int iend = istart + 180 - 1;
if (iend >= npts)
iend = npts - 1; // allow for partial interval on the end
if (iend < istart)
continue; // should never happen...
// Check for missing data
int i = 0;
int ii = 0;
int npdpts = iend - istart + 1; // number of possible points in the
// period, =180
float[] hhdev = new float[npdpts];
float[] dddev = new float[npdpts];
for (int j = istart; j < iend + 1; j++) {
hhdev[j - istart] = hdev[j];
dddev[j - istart] = ddev[j];
}
// get hdevGood
for (i = npdpts - 1; i >= 0; i--)
if (hhdev[i] != MISSING_VAL && hhdev[i] != 0)
break;
for (ii = npdpts - 1; ii >= 0; ii--)
if (dddev[ii] != MISSING_VAL && dddev[ii] != 0)
break;
// i, ii are the last data that is not missing
float[] hdevGood = new float[i + 1];
float[] ddevGood = new float[ii + 1];
if (i > -1)
for (int j = 0; j < i + 1; j++)
hdevGood[j] = hhdev[j];
if (ii > -1)
for (int j = 0; j < ii + 1; j++)
ddevGood[j] = dddev[j];
if (missingFlag == 0 || (i > -1 && ii > -1)) {
if (hdevGood != null && hdevGood.length != 0)
hGamma[ipd] = CalcUtil.maxValue(hdevGood)
- CalcUtil.minValue(hdevGood);
if (ddevGood != null && ddevGood.length != 0)
dGamma[ipd] = CalcUtil.maxValue(ddevGood)
- CalcUtil.minValue(ddevGood);
if (hGamma[ipd] != MISSING_VAL)
hk[ipd] = CalcUtil.getKfromTable(kLimit, hGamma[ipd]);
if (dGamma[ipd] != MISSING_VAL)
dk[ipd] = CalcUtil.getKfromTable(kLimit, dGamma[ipd]);
// get bigger one
if (hGamma[ipd] >= dGamma[ipd] && hGamma[ipd] != MISSING_VAL) {
kIndex[ipd] = hk[ipd];
gamma[ipd] = hGamma[ipd];
} else if (dGamma[ipd] >= hGamma[ipd]
&& dGamma[ipd] != MISSING_VAL) {
kIndex[ipd] = dk[ipd];
gamma[ipd] = dGamma[ipd];
}
}
}
list.add(0, kIndex);
list.add(1, gamma);
list.add(2, hk);
list.add(3, hGamma);
list.add(4, dk);
list.add(5, dGamma);
return list;
}
/*
* Force QHAQDC and QDAQDC to be continuous between the last and the first
* value using a +/- SMOOTH_WINDOW
*/
public static float[] getQHAQDC(float[] qdc) {
float[] data = qdc.clone(); // new float[1440];
if (qdc.length != 1440)
return data;
float jump = qdc[0] - qdc[1439];
for (int i = 0; i < SMOOTH_WINDOW; i++) {
data[1440 - SMOOTH_WINDOW - i] += i / (SMOOTH_WINDOW - 1) * 0.5
* jump;
data[i] -= (1 - i / (SMOOTH_WINDOW - 1)) * 0.5 * jump;
}
return data;
}
/*
* find index in hhdata that indicates current time currTimeIndex = first
* 1440 minutes + prev day minutes + curr day minutes
*/
public static int getCurrTimeIndex(int hour, int min, int epHour) {
if (epHour == 0)
epHour = 24;
int currTimeIndex = HOURS * MINUTES + (HOURS - epHour) * MINUTES + hour
* MINUTES + min;
return currTimeIndex;
}
/*
*
*/
public static float[] getExtrapolation(float[] dataIn, float[] qhaQdc,
int currTimeIndex) { // 4320
float[] data = dataIn.clone();
int j0 = currTimeIndex;// Last good H or D index
if (data.length != 4320 || qhaQdc.length != 1440)
return data;
if (data[j0] != MISSING_VAL) {
for (int j = j0 + 1; j < 4320; j++) {
int w2 = j - j0 - 1; // from .pro
int w1 = TRANSITION_TIME - w2;
if (w1 < 0)
w1 = 0;
data[j] = (w1 * data[j0] + w2 * qhaQdc[j % 1440]) / (w1 + w2);
}
}
return data;
}
public static float[] getDev(float[] data, float[] qdc) {
float[] dev = new float[1440];
if (data.length != 4320 || qdc.length != 1440)
return dev;
for (int i = 0; i < 1440; i++) {
if (data[i + 1440] != MISSING_VAL && qdc[i] != MISSING_VAL)
dev[i] = data[i + 1440] - qdc[i];
else
dev[i] = MISSING_VAL;
}
return dev;
}
public static float[] adjustHrCentAvg(float[] hcAIn, float[] qha,
float[] gamma, int[] kLimit) {
float[] hcA = hcAIn.clone();
float wh = 0;
if (hcA.length != HOURS || gamma.length != 8)
return hcA;
for (int ipd = 0; ipd < 8; ipd++) {
if (gamma[ipd] < kLimit[4])
wh = 1;
else if (gamma[ipd] >= kLimit[4] && gamma[ipd] < kLimit[6])
wh = (float) Math.pow(
((kLimit[6] - gamma[ipd]) / (kLimit[6] - kLimit[4])),
PHASE_POWER);
else
wh = 0;
for (int j = 0; j < 3; j++) {
hcA[ipd * 3 + j] = wh * hcA[ipd * 3 + j] + (1 - wh)
* qha[ipd * 3 + j];// ?
}
}
return hcA;
}
/*
* wraper function for a few functions in this class.
*
* @param -- hdata, H or D data
*
* @param -- hQdc, H or D quiet day curve. float[1440]
*
* @param -- currTimeIndex, current time index in the array
*
* @return -- hDev or dDev. float[1440]
*/
public static float[] getHdevOrDDev(float[] hdata, float[] hQdc,
int currTimeIndex) {
float[] hDev = null;
float[] hhdata = CalcEach1min.fillGaps(hdata);
hDev = CalcEach1min.getDev(hhdata, hQdc);// [1440]
return hDev;
}
}

View file

@ -0,0 +1,392 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag.calculation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/*
* The calculation of k, 3 hour related.
*
* <pre>
* SOFTWARE HISTORY
*
* ate Ticket# Engineer Description
* ----------- ---------- ---------- --------------------------
* 05/14/2013 #989 qzhou Initial Creation
* 03/18/2014 #1123 qzhou Add getHQdcOrDQdc
* </pre>
*
* @author qzhou
* @version 1
*/
public class CalcEach3hr {
private static final float MISSING_VAL = 99999.99f;
private static final int NIGHT_LENGTH = 90; // min
private static final int DAWN_LENGTH = 60;
private static final int DAY_LENGTH = 0;
private static final int DUSK_LENGTH = 60;
private static int DAYS = 30;
private static int HOURS = 24;
private static int MINUTES = 60;
/*
* calculate hrAvgs for this hour
*
* @param bestList -- contains 1 hour data
*/
public static float[] getSimpleHourAvg(List bestList) {
float[] simpHrAvg = new float[2];
float simpHrAvg1 = 0;
float simpHrAvg2 = 0;
double sum1 = 0;
double sum2 = 0;
int rec1 = 0;
int rec2 = 0;
for (int i = 0; i < bestList.size(); i++) {
List<Float> list = (List<Float>) bestList.get(i);
float comp1 = (Float) list.get(1);
float comp2 = (Float) list.get(2);
if (comp1 != MISSING_VAL) {
sum1 += comp1;
rec1++;
}
if (comp2 != MISSING_VAL) {
sum2 += comp2;
rec2++;
}
}
if (rec1 > 30) // less than half missing value
simpHrAvg1 = (float) sum1 / rec1;
else
simpHrAvg1 = MISSING_VAL;
if (rec2 > 30) // less than half missing value
simpHrAvg2 = (float) sum2 / rec2;
else
simpHrAvg2 = MISSING_VAL;
simpHrAvg[0] = simpHrAvg1;
simpHrAvg[1] = simpHrAvg2;
return simpHrAvg;
}
/*
* calculate hrAvgs for this day.
*
* @param data -- data of one day, 1440
*/
public static float[] getSimpleHourAvg(float[] data) { // data 1440
float[] simpHrAvg = new float[HOURS];
for (int ihr = 0; ihr < HOURS; ihr++) {
double sum = 0;
int missing = 0;
for (int i = ihr * MINUTES; i < ihr * MINUTES + MINUTES; i++) {
if (data[i] != MISSING_VAL)
sum += data[i];
else
missing++;
}
if (missing < 30) // less than half missing value
simpHrAvg[ihr] = (float) sum / (MINUTES - missing);
else
simpHrAvg[ihr] = MISSING_VAL;
}
return simpHrAvg;
}
/*
* calculate hrAvgs for this hour in data array
*
* @param data -- data of one day, 1440
*/
public static float getSimpleHourAvg(float[] data, int hour) { // one day
// 1440, avg
// for hour-1
float simpHrAvg = 0;
double sum = 0;
int rec = 0;
if (data.length <= hour * MINUTES + MINUTES)
for (int i = hour * MINUTES; i < data.length; i++) {
if (data[i] != MISSING_VAL) {
sum += data[i];
rec++;
}
}
else
for (int i = hour * MINUTES; i < hour * MINUTES + MINUTES; i++) {
if (data[i] != MISSING_VAL) {
sum += data[i];
rec++;
}
}
if (rec > 30) // less than half missing value
simpHrAvg = (float) sum / (rec);
else
simpHrAvg = MISSING_VAL;
return simpHrAvg;
}
/*
* @param simpHrAvgH -- data of 30 intervals(720 hours)
*
* @return disturbance levels for 30 intervals
*/
public static float[] getDisturbanceLevel(float[] simpHrAvgH,
float[] simpHrAvgD) {
float[] dB = new float[30];
for (int j = 0; j < DAYS; j++) {
double sum = 0;
int missing = 0;
int endOfArray = simpHrAvgH.length;
int endTime = (endOfArray > j * HOURS + HOURS) ? j * HOURS + HOURS
: endOfArray;
for (int i = j * HOURS; i < endTime - 1; i++) {
if (simpHrAvgH[i] != MISSING_VAL
&& simpHrAvgD[i] != MISSING_VAL
&& simpHrAvgH[i + 1] != MISSING_VAL
&& simpHrAvgD[i + 1] != MISSING_VAL) {
sum += Math.sqrt(Math.pow(
(simpHrAvgH[i + 1] - simpHrAvgH[i]), 2)
+ Math.pow((simpHrAvgD[i + 1] - simpHrAvgD[i]), 2));
} else
missing++;
}
if (missing <= 12) // not 12 or more missing
dB[j] = (float) sum / (HOURS - 1 - missing);
else
dB[j] = MISSING_VAL;
}
return dB;
}
/*
* @param dB -- float[30 ]
*
* @return --5 smallest disturbance levels
*/
public static Map getSmallDisturbanceLevel(float[] dB) {
// create a map that key=dBIndex and value=dBValue.
// create a duplicate array dBDup. Sort it.
// take 5 smallest dBDup[i]. Then find its index and value from the dB.
// Put them to the map
Map<Integer, Float> dBSmall = new HashMap<Integer, Float>();
float[] dBDup = new float[dB.length];
for (int i = 0; i < dBDup.length; i++) {
dBDup[i] = dB[i];
}
Arrays.sort(dBDup);
float dupIndex = (int) MISSING_VAL;
float wk = 0;
// take 5 smallest dBDup
for (int j = 0; j < 5; j++) {
for (int i = 0; i < dB.length; i++) {
if (dB[i] == dBDup[j] && i != dupIndex) { // for duplicated
// values
dBSmall.put(i, dB[i]);
dupIndex = i;
break;
}
}
}
return dBSmall;
}
/*
* @param -- dBSmall, 5 set map
*
* @param -- simpHrAvg, -- float[720]
*
* @rturn -- quietLevelHourAvg, float[24]
*/
public static float[] getQuietLevelHourAvg(Map<Integer, Float> dBSmall,
float[] simpHrAvg) {
if (dBSmall.entrySet().size() < 5)
return simpHrAvg;
float[] quietHrAvg = new float[24];
Arrays.fill(quietHrAvg, MISSING_VAL);
int[] index = new int[5];
float[] dB = new float[5];
int k = 0;
Iterator<?> iter = dBSmall.entrySet().iterator();
while (iter.hasNext()) {
@SuppressWarnings("unchecked")
Map.Entry<Integer, Float> mEntry = (Map.Entry<Integer, Float>) iter
.next(); // sorted on key
index[k] = mEntry.getKey();
dB[k] = mEntry.getValue();
k++;
}
// construct smallHrAvg array (24*5) from simpHrAvg (24*30)
float[] smallHrAvg = new float[24 * 5];
for (int j = 0; j < 5; j++) { // k=5
int endOfArray = smallHrAvg.length;
int endTime = (endOfArray > j * HOURS + HOURS) ? j * HOURS + HOURS
: endOfArray;
for (int i = j * HOURS; i < endTime; i++) {
smallHrAvg[i] = simpHrAvg[index[j] * HOURS + i % HOURS]; // 700
}
}
for (int ihr = 0; ihr < HOURS; ihr++) {
float sumAvg = 0;
float sumWk = 0;
float wk = 0;
for (int jk = 0; jk < 5; jk++) {
int ind = jk * HOURS + ihr;
if (dB[jk] < 1)
wk = 1;
else
wk = 1 / (dB[jk] * dB[jk]);
if (smallHrAvg[ind] != MISSING_VAL) {
sumAvg += wk * smallHrAvg[ind];
sumWk += wk;
}
}
if (sumWk > 0)
quietHrAvg[ihr] = sumAvg / sumWk;
}
return quietHrAvg;
}
/*
* @param -- quietHrAvg, float[24]
*
* @return -- shifted quietLevelHourAvg, float[24]
*/
public static float[] getQHA(float[] quietHrAvg) {
float[] QHA = new float[24];
if (quietHrAvg.length != 24)
return quietHrAvg;
for (int ihr = 0; ihr < 24; ihr++) {
QHA[ihr] = quietHrAvg[(ihr + 3) % 24];
}
return QHA;
}
/*
* @return -- 24 element floating point array. Default fitting lengths.
* (one for each hour of the 24 hour interval that ends at EPtime).
*/
public static float[] getDefLength(String station, int epHour) {
float[] defLength = new float[24];
float lon = CalcUtil.getLongitude(station);
int UTdiff = Math.round(1440.0f * lon / 360.0f);
int minute0 = epHour * MINUTES;
for (int ihr = 0; ihr < HOURS; ihr++) {
float sum = 0;
for (int imin = 0; imin < MINUTES; imin++) {
int curMin = (minute0 + ihr * MINUTES + imin) % 1440;
int localMin = (curMin + UTdiff) % 1440;
if (localMin >= 0 && localMin < 180)
sum += NIGHT_LENGTH;
else if (localMin >= 180 && localMin < 360)
sum += DAWN_LENGTH;
else if (localMin >= 360 && localMin < 1080)
sum += DAY_LENGTH;
else if (localMin >= 1080 && localMin < 1260)
sum += DUSK_LENGTH;
else if (localMin >= 1260 && localMin < 1440)
sum += NIGHT_LENGTH;
}
defLength[ihr] = sum / MINUTES;
}
return defLength;
}
/*
* wraper function for a few functions in this class.
*
* @param -- hHrAvgs, hourly average for H. float[720]
*
* @param -- dHrAvgs, hourly average for D. float[720]
*
* @return -- if hHrAvgs is first param, return hQdc; if dHrAvgs is first
* param, return dQdc. float[1440]
*/
public static float[] getHQdcOrDQdc(float[] hHrAvgs, float[] dHrAvgs) {
float[] hQdc = null;
float[] qhaQdc = null;
float[] dB = CalcEach3hr.getDisturbanceLevel(hHrAvgs, dHrAvgs);
@SuppressWarnings("unchecked")
Map<Integer, Float> dBsmall = CalcEach3hr.getSmallDisturbanceLevel(dB);
float[] quietHHrAvg = CalcEach3hr
.getQuietLevelHourAvg(dBsmall, hHrAvgs);
// added from FMIQDCRT11_3hr.pro
for (int k = 0; k < quietHHrAvg.length; k++) {
if (quietHHrAvg[k] == MISSING_VAL) {
quietHHrAvg[k] = CalcUtil.getMedian(quietHHrAvg);
}
}
float[] qha = CalcEach3hr.getQHA(quietHHrAvg);
hQdc = CalcEach1min.getHarmonicFit(qha);// [1440]
// qhaQdc = CalcEach1min.getQHAQDC(hQdc);// [1440]
return hQdc;
}
}

View file

@ -0,0 +1,382 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag.calculation;
import gov.noaa.nws.ncep.common.dataplugin.geomag.table.KsThree;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/*
* The calculation of Kp and related.
*
* <pre>
* SOFTWARE HISTORY
*
* ate Ticket# Engineer Description
* ----------- ---------- ---------- --------------------------
* 05/14/2013 #989 qzhou Initial Creation
* 03/18/2014 #1123 qzhou default k to 99999
* </pre>
*
* @author qzhou
* @version 1
*/
public class CalcKp {
private static final float MISSING_VAL = 99999.99f;
public CalcKp() {
}
public static float[] getKest(String station, float[] kIndex, float[] gamma) {
float[] kest = new float[8];
for (int i = 0; i < 8; i++) {
int[] gammaLimit = CalcUtil.getKLimit(station); // .getGammaFromK(station,
// gamma);
if (kIndex[i] < 9) {
kest[i] = kIndex[i]
+ (gamma[i] - gammaLimit[(int) kIndex[i]])
/ (gammaLimit[(int) kIndex[i] + 1] - gammaLimit[(int) kIndex[i]]);
} else if (kIndex[i] < 999)
kest[i] = 9.0f;
else
kest[i] = 99999f;
}
return kest;
}
public static float getKest(String station, int kIndex, float gamma) {
float kest = 99999f;
int[] gammaLimit = CalcUtil.getKLimit(station);
if (kIndex < 9)
kest = kIndex + (gamma - gammaLimit[kIndex])
/ (gammaLimit[kIndex + 1] - gammaLimit[kIndex]);
else if (kIndex < 999)
kest = 9.0f;
return kest;
}
/*
* list of the station coefficient values in the order of 00-03, 03-06...
*/
public static ArrayList<KsThree> getKsThreeList(String station) {
ArrayList<KsThree> threeKsList = CalcUtil.getStationCoeff()
.getStationByCode(station).getKsThree();// size 24
return threeKsList;
}
public static List<Integer> getKsThree(Date time, String station, int k) {
List<Integer> ks = new ArrayList<Integer>();
// KsThree ksThree = null;
ArrayList<KsThree> ksThreeList = getKsThreeList(station);
if (ksThreeList != null && !ksThreeList.isEmpty()) {
int hour = CalcUtil.getSPTime(time).getHours();
int period = hour / 3;// 24 -> 8
KsThree ksThree = ksThreeList.get(period);
if (ksThree != null)
ks.add(getKsOfKsThree(k, ksThree));
ksThree = ksThreeList.get(period + 8);
if (ksThree != null)
ks.add(getKsOfKsThree(k, ksThree));
ksThree = ksThreeList.get(period + 16);
if (ksThree != null)
ks.add(getKsOfKsThree(k, ksThree));
}
return ks;
}
private static int getKsOfKsThree(int k, KsThree ksThree) {
int ks = 99999;
if (k == 0)
ks = ksThree.getK0();
else if (k == 1)
ks = ksThree.getK1();
else if (k == 2)
ks = ksThree.getK2();
else if (k == 3)
ks = ksThree.getK3();
else if (k == 4)
ks = ksThree.getK4();
else if (k == 5)
ks = ksThree.getK5();
else if (k == 6)
ks = ksThree.getK6();
else if (k == 7)
ks = ksThree.getK7();
else if (k == 8)
ks = ksThree.getK8();
else if (k == 9)
ks = ksThree.getK9();
return ks;
}
public static float getKs(String station, int k, Date time)
throws ParseException {
float a = 0;
float b = 0;
float ks = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// int year = time.getYear();113
Calendar cal = Calendar.getInstance();
cal.setTime(time);
int year = cal.get(Calendar.YEAR);
Date date1 = sdf.parse(year + "-01-01");
Date date2 = sdf.parse(year + "-02-14");
Date date3 = sdf.parse(year + "-02-24");
Date date4 = sdf.parse(year + "-03-06");
Date date5 = sdf.parse(year + "-03-16");
Date date6 = sdf.parse(year + "-04-16");
Date date7 = sdf.parse(year + "-04-26");
Date date8 = sdf.parse(year + "-05-06");
Date date9 = sdf.parse(year + "-05-16");
Date date10 = sdf.parse(year + "-08-17");
Date date11 = sdf.parse(year + "-08-27");
Date date12 = sdf.parse(year + "-09-06");
Date date13 = sdf.parse(year + "-09-16");
Date date14 = sdf.parse(year + "-10-17");
Date date15 = sdf.parse(year + "-10-27");
Date date16 = sdf.parse(year + "-11-06");
Date date17 = sdf.parse(year + "-11-16");
Date date18 = sdf.parse(year + "-12-31");
Date date2Leep = sdf.parse(year + "-02-15");
Date date3Leep = sdf.parse(year + "-02-25");
List<Integer> ksThree = getKsThree(time, station, k);
if (time.compareTo(date1) >= 0 && time.compareTo(date2) < 0) {
ks = (float) ksThree.get(0) / 3;
} else if (time.compareTo(date4) >= 0 && time.compareTo(date5) < 0) {
ks = (float) (0.25f * ksThree.get(0) + 0.75f * ksThree.get(1)) / 3;
} else if (time.compareTo(date5) >= 0 && time.compareTo(date6) < 0) {
ks = (float) ksThree.get(1) / 3;
} else if (time.compareTo(date6) >= 0 && time.compareTo(date7) < 0) {
ks = (float) (0.75f * ksThree.get(1) + 0.25f * ksThree.get(2)) / 3;
} else if (time.compareTo(date7) >= 0 && time.compareTo(date8) < 0) {
ks = (float) (0.5f * ksThree.get(1) + 0.5f * ksThree.get(2)) / 3;
} else if (time.compareTo(date8) >= 0 && time.compareTo(date9) < 0) {
ks = (float) (0.25f * ksThree.get(1) + 0.75f * ksThree.get(2)) / 3;
} else if (time.compareTo(date9) >= 0 && time.compareTo(date10) < 0) {
ks = (float) ksThree.get(2) / 3;
} else if (time.compareTo(date10) >= 0 && time.compareTo(date11) < 0) {
ks = (float) (0.75f * ksThree.get(2) + 0.25f * ksThree.get(1)) / 3;
} else if (time.compareTo(date11) >= 0 && time.compareTo(date12) < 0) {
ks = (float) (0.5f * ksThree.get(2) + 0.5f * ksThree.get(1)) / 3;
} else if (time.compareTo(date12) >= 0 && time.compareTo(date13) < 0) {
ks = (float) (0.25f * ksThree.get(2) + 0.75f * ksThree.get(1)) / 3;
} else if (time.compareTo(date13) >= 0 && time.compareTo(date14) < 0) {
ks = (float) ksThree.get(1) / 3;
} else if (time.compareTo(date14) >= 0 && time.compareTo(date15) < 0) {
ks = (float) (0.75f * ksThree.get(1) + 0.25f * ksThree.get(0)) / 3;
} else if (time.compareTo(date15) >= 0 && time.compareTo(date16) < 0) {
ks = (float) (0.5f * ksThree.get(1) + 0.5f * ksThree.get(0)) / 3;
} else if (time.compareTo(date16) >= 0 && time.compareTo(date17) < 0) {
ks = (float) (0.25f * ksThree.get(1) + 0.75f * ksThree.get(0)) / 3;
} else if (time.compareTo(date17) >= 0 && time.compareTo(date18) <= 0) {
ks = (float) ksThree.get(0) / 3;
} else if (CalcUtil.isLeapYear(year)) {
if (time.compareTo(date2Leep) >= 0 && time.compareTo(date3Leep) < 0) {
ks = (float) (0.75f * ksThree.get(0) + 0.25f * ksThree.get(1)) / 3;
} else if (time.compareTo(date3Leep) >= 0
&& time.compareTo(date4) < 0) {
ks = (float) (0.5f * ksThree.get(0) + 0.5f * ksThree.get(1)) / 3;
}
} else {
if (time.compareTo(date2) >= 0 && time.compareTo(date3) < 0) {
ks = (float) (0.75f * ksThree.get(0) + 0.25f * ksThree.get(1)) / 3;
} else if (time.compareTo(date3) >= 0 && time.compareTo(date4) < 0) {
ks = (float) (0.5f * ksThree.get(0) + 0.5f * ksThree.get(1)) / 3;
}
}
return ks;
}
// protected float[] getKs(String station, float[] kest) {
// float a = 0;
// float b = 0;
// float[] ks = new float[8];
//
// Map<Float, Float> abCoeff = CalcUtil.getCoeffAandB(station);
// if (abCoeff.size() != 8)
// return ks;
//
// int i = 0;
// Iterator<?> iter = abCoeff.entrySet().iterator();
// while (iter.hasNext()) {
// @SuppressWarnings("unchecked")
// Map.Entry<Float, Float> mEntry = (Map.Entry<Float, Float>) iter.next();
//
// a = mEntry.getKey();
// b = mEntry.getValue();
// ks[i] = a + b * kest[i];
// i++;
// }
//
// return ks;
// }
public static float getKs(String station, float kest, String timePrd) {
float a = 0;
float b = 0;
float ks = 0;
Map<Float, Float> abCoeff = CalcUtil.getCoeffAandB(station);
if (abCoeff.size() != 8)
return ks;
int j = 0;
if (timePrd.equalsIgnoreCase("00-03"))
j = 0;
else if (timePrd.equalsIgnoreCase("03-06"))
j = 1;
else if (timePrd.equalsIgnoreCase("06-09"))
j = 2;
else if (timePrd.equalsIgnoreCase("09-12"))
j = 3;
else if (timePrd.equalsIgnoreCase("12-15"))
j = 4;
else if (timePrd.equalsIgnoreCase("15-18"))
j = 5;
else if (timePrd.equalsIgnoreCase("18-21"))
j = 6;
else if (timePrd.equalsIgnoreCase("21-24"))
j = 7;
int i = 0;
Iterator<?> iter = abCoeff.entrySet().iterator();
while (iter.hasNext()) {
@SuppressWarnings("unchecked")
Map.Entry<Float, Float> mEntry = (Map.Entry<Float, Float>) iter
.next();
if (i == j) {
a = mEntry.getKey();
b = mEntry.getValue();
ks = a + b * kest;
break;
}
i++;
}
return ks;
}
public static int getAest(String station, int kIndex) {
return CalcUtil.getK2a(kIndex);
}
@SuppressWarnings("unchecked")
public static float[] getKpEst(String[] station, float[] ks) {
float kpEst[] = new float[ks.length];
float[][] wcoeff = new float[station.length][ks.length];
if (ks.length != 8)
return kpEst;
for (int i = 0; i < station.length; i++) {
Map<String, Float> coeff = CalcUtil.getCoeffW(station[i]);
int j = 0;
Iterator<?> iter = coeff.entrySet().iterator();
while (iter.hasNext()) {
wcoeff[i][j] = ((Map.Entry<String, Float>) iter.next())
.getValue();
j++;
}
}
float sumW = 0;
float sumWK = 0;
for (int j = 0; j < ks.length; j++) {
for (int i = 0; i < station.length; i++) {
sumW += wcoeff[i][j];
sumWK += wcoeff[i][j] * ks[i];
}
// kpEst[i] = (float) (Math.round(3 * sumWK / sumW)) / 3;
kpEst[j] = sumWK / sumW;
kpEst[j] = (int) kpEst[j] + CalcUtil.getThird(kpEst[j]);
}
return kpEst;
}
@SuppressWarnings("unchecked")
public static float getKpEst(String[] station, float ks, String fitTime) {
float kpEst = 0;
float[] wcoeff = new float[8];
for (int i = 0; i < station.length; i++) {
Map<String, Float> coeff = CalcUtil.getCoeffW(station[i]);
int j = 0;
Iterator<?> iter = coeff.entrySet().iterator();
while (iter.hasNext()) {
if (((Map.Entry<String, Float>) iter.next()).getKey()
.equalsIgnoreCase(fitTime)) {
wcoeff[i] = ((Map.Entry<String, Float>) iter.next())
.getValue();
break;
}
j++;
}
}
float sumW = 0;
float sumWK = 0;
for (int i = 0; i < station.length; i++) {
sumW += wcoeff[i];
sumWK += wcoeff[i] * ks;
}
kpEst = sumWK / sumW;
kpEst = (int) kpEst + CalcUtil.getThird(kpEst);
return kpEst;
}
public static String[] getKp(float kpEst[], String[] kpModifier) {
String[] kp = new String[kpEst.length];
if (kpEst.length != kpModifier.length)
return kp;
for (int i = 0; i < kpEst.length; i++) {
int k = Math.round(kpEst[i]);
kp[i] = k + kpModifier[i];
}
return kp;
}
public static String getKp(float kpEst, String kpModifier) {
int kp = Math.round(kpEst);
return kp + kpModifier;
}
}

View file

@ -0,0 +1,550 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag.calculation;
import gov.noaa.nws.ncep.common.dataplugin.geomag.table.KFitTime;
import gov.noaa.nws.ncep.common.dataplugin.geomag.util.KStationCoefficientLookup;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
* The k index and decoder calculation utility.
*
* <pre>
* SOFTWARE HISTORY
*
* ate Ticket# Engineer Description
* ----------- ---------- ---------- --------------------------
* 05/14/2013 #989 qzhou Initial Creation
* </pre>
*
* @author qzhou
* @version 1
*/
public class CalcUtil {
private static final float MISSING_VAL = 99999.99f;
private static final float K_EXPONENT = 3.3f;
private static KStationCoefficientLookup stationCoeff = KStationCoefficientLookup
.getInstance();
// Gamma limit table
private static enum Limit {
K0(0), K1(5), K2(10), K3(20), K4(40), K5(70), K6(120), K7(200), K8(330), K9(
500);
private final int kConst;
private Limit(int kConst) {
this.kConst = kConst;
}
}
public static int getKConst(int k) {
int kConst = 0;
if (k == 0)
kConst = Limit.K0.kConst;
else if (k == 1)
kConst = Limit.K1.kConst;
else if (k == 2)
kConst = Limit.K2.kConst;
else if (k == 3)
kConst = Limit.K3.kConst;
else if (k == 4)
kConst = Limit.K4.kConst;
else if (k == 5)
kConst = Limit.K5.kConst;
else if (k == 6)
kConst = Limit.K6.kConst;
else if (k == 7)
kConst = Limit.K7.kConst;
else if (k == 8)
kConst = Limit.K8.kConst;
else if (k == 9)
kConst = Limit.K9.kConst;
return kConst;
}
// A-index table
private static enum K2a {
a0(0), a1(3), a2(7), a3(15), a4(27), a5(48), a6(80), a7(140), a8(240), a9(
400);
private final int a;
private K2a(int a) {
this.a = a;
}
}
public static int getK2a(int k) {
int a = 0;
if (k == 0)
a = K2a.a0.a;
else if (k == 1)
a = K2a.a1.a;
else if (k == 2)
a = K2a.a2.a;
else if (k == 3)
a = K2a.a3.a;
else if (k == 4)
a = K2a.a4.a;
else if (k == 5)
a = K2a.a5.a;
else if (k == 6)
a = K2a.a6.a;
else if (k == 7)
a = K2a.a7.a;
else if (k == 8)
a = K2a.a8.a;
else if (k == 9)
a = K2a.a9.a;
return a;
}
public static KStationCoefficientLookup getStationCoeff() {
return stationCoeff;
}
public static int getK9Limit(String station) throws NumberFormatException {
int k9 = 0;
String k9Limit = getStationCoeff().getStationByCode(station)
.getK9Limit();
k9 = Integer.parseInt(k9Limit);
return k9;
}
public static float getLongitude(String station)
throws NumberFormatException {
float lon = 0;
if (station != null && !station.equalsIgnoreCase("")) {
String longitude = getStationCoeff().getStationByCode(station)
.getLongitude();
lon = Float.parseFloat(longitude);
}
return lon;
}
/*
* map of the A and the B values in the order of 00-03, 03-06...
*/
public static Map<Float, Float> getCoeffAandB(String station) {
Map<Float, Float> abCoeff = new HashMap<Float, Float>();
List<KFitTime> fitTime = getStationCoeff().getStationByCode(station)
.getKFitTime();
if (fitTime.size() != 8)
return abCoeff;
for (int i = 0; i < 8; i++) {
float a = fitTime.get(i).getCoeffA();
float b = fitTime.get(i).getCoeffB();
abCoeff.put(a, b);
}
return abCoeff;
}
/*
* map of the time period and the W values in the order of 00-03, 03-06...
*/
public static Map<String, Float> getCoeffW(String station) {
Map<String, Float> wCoeff = new HashMap<String, Float>();
List<KFitTime> fitTime = getStationCoeff().getStationByCode(station)
.getKFitTime();
if (fitTime.size() != 8)
return wCoeff;
for (int i = 0; i < 8; i++) {
String a = fitTime.get(i).getKey();
float b = fitTime.get(i).getCoeffW();
wCoeff.put(a, b);
}
return wCoeff;
}
public static int[] getKLimit(String station) {
int[] kLimit = new int[10];
int k9Limit = getK9Limit(station);
for (int i = 0; i < kLimit.length; i++) {
kLimit[i] = Math.round(k9Limit * getKConst(i) / 500);
}
return kLimit;
}
// public static int[] getAIndex(String station, float[] k-index) {
// int[] aIndex = new int[10];
// //int k9Limit = getK9Limit(station);
// for (int i = 0; i < kLimit.length; i++) {
// aIndex[i] = Math.round( getK2a(i));
// }
// return aIndex;
// }
public static int getKfromTable(int[] kLimit, float gamma) {
int kIndex;
int i = 0;
for (i = 0; i < 10; i++) {
if (gamma > kLimit[i])
continue;
else
break;
}
// take the lower of i. this step eq. K_limit = K9limit * [5, 10, 20,
// 40...
if (i > 0)
i = i - 1;
if (i <= 9)
kIndex = i;
else
kIndex = 9;
return kIndex;
}
public static int getGammaFromK(String station, int kIndex) {
int gamma = getK9Limit(station) * getKConst(kIndex) / 500;
return gamma;
}
// assume db time format yyyy-mm-dd hh:mm:ss
public static Date getSPTime(Date currTime) {
Date spTime = currTime;
int hour = currTime.getHours();
if (hour >= 0 && hour < 3)
hour = 0;
else if (hour >= 3 && hour < 6)
hour = 3;
else if (hour >= 6 && hour < 9)
hour = 6;
else if (hour >= 9 && hour < 12)
hour = 9;
else if (hour >= 12 && hour < 15)
hour = 12;
else if (hour >= 15 && hour < 18)
hour = 15;
else if (hour >= 18 && hour < 21)
hour = 18;
else if (hour >= 21 && hour < 24)
hour = 21;
spTime.setHours(hour);
spTime.setMinutes(0);
spTime.setSeconds(0);
return spTime;
}
public static Date getEPTime(Date currTime) {
Date epTime = (Date) currTime.clone();
int hour = currTime.getHours();
if (hour >= 0 && hour < 3)
hour = 3;
else if (hour >= 3 && hour < 6)
hour = 6;
else if (hour >= 6 && hour < 9)
hour = 9;
else if (hour >= 9 && hour < 12)
hour = 12;
else if (hour >= 12 && hour < 15)
hour = 15;
else if (hour >= 15 && hour < 18)
hour = 18;
else if (hour >= 18 && hour < 21)
hour = 21;
else if (hour >= 21 && hour < 24)
hour = 0;
if (hour != 0)
epTime.setHours(hour);
else {
int day = currTime.getDate() + 1;
epTime.setDate(day);
epTime.setHours(hour);
}
epTime.setMinutes(0);
epTime.setSeconds(0);
return epTime;
}
public static boolean isHalfMissing(float[] items) {
boolean halfMissaing = false;
int i = 0;
for (i = 0; i < items.length; i++) {
if (items[i] == MISSING_VAL)
i++;
}
if (i > items.length / 2)
halfMissaing = true;
return halfMissaing;
}
public static float getThird(float kpEst) {
float half = 0.333333f / 2;
float x = kpEst - (int) kpEst; // get decimal fraction
if (x >= 0 && x <= half)
x = 0;
else if (x >= half && x <= 2 * half)
x = 0.333333f;
else if (x >= 2 * half && x <= 3 * half)
x = 0.333333f;
else if (x >= 3 * half && x <= 4 * half)
x = 0.666666f;
else if (x >= 4 * half && x <= 5 * half)
x = 0.666666f;
else if (x >= 5 * half && x <= 6 * half)
x = 1;
return x;
}
public static float maxValue(float[] dev) {
float max = -99999;
for (int i = 0; i < dev.length; i++) {
if (dev[i] > max && dev[i] < MISSING_VAL) {
max = dev[i];
}
}
return max;
}
public static float minValue(float[] dev) {
float min = 99999;
for (int i = 0; i < dev.length; i++) {
if (dev[i] < min && dev[i] > -MISSING_VAL) {
min = dev[i];
}
}
return min;
}
/*
* 10 element floating point array
*/
public static float[] geKLength() {
float[] kLength = new float[10];
kLength[0] = 0;
for (int i = 1; i < 10; i++) {
kLength[i] = (float) Math.exp(K_EXPONENT * Math.log(i));
if (kLength[i] > 1080)
kLength[i] = 1080;
}
return kLength;
}
// uri: /geomag/2013-05-20_00:00:00.0/HAD/101/GEOMAG
public static String getSourceFromUri(String uri) {
if (uri != null && uri.length() >= 37)
return uri.substring(34, 37);
else
return "";
}
public static String getStationFromUri(String uri) {
if (uri != null && uri.length() >= 37)
return uri.substring(30, 33);
else
return "";
}
public static Date getTimeFromUri(String uri) throws ParseException {
String format = "yyyy-MM-dd'_'HH:mm:ss.s";
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (uri != null && uri.length() >= 37) {
String time = uri.substring(8, 29);
Date date = sdf.parse(time);
return date;
} else
return new Date();
}
// get the front part before the source in the uri
public static String separateSourceFrontUri(String uri) {
if (uri != null && uri.length() >= 37)
return uri.substring(0, 34);
else
return "";
}
public static float[] toFloatArray(List<Float> list) {
float[] ret = new float[list.size()];
int i = 0;
for (Float e : list)
ret[i++] = e.floatValue();
return ret;
}
public static int[] toIntArray(List<Integer> list) {
int[] ret = new int[list.size()];
int i = 0;
for (Integer e : list)
ret[i++] = e.intValue();
return ret;
}
public static boolean isLeapYear(int year) {
boolean isLeap;
if (year / 400 == 0)
isLeap = true;
else if (year / 100 == 0)
isLeap = false;
else if (year / 4 == 0)
isLeap = true;
else
isLeap = false;
return isLeap;
}
// public static String getMonthDayFromNumber(int year, int number) {
// //CL22013041.min
// String temp = "";
// String month = "";
// String day = "";
// String monthDay = "";
// Boolean isLeapYear = isLeapYear( year);
// int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
// int[] leapDays = {31,29,31,30,31,30,31,31,30,31,30,31};
// Calendar cal = Calendar.getInstance();
// cal.get(Calendar.DAY_OF_MONTH);
// cal.get(Calendar.MONTH);
// cal.get(Calendar.DAY_OF_YEAR);
// cal.set(Calendar.DAY_OF_YEAR, number);
// int[] num =
// if (isLeapYear) {
//
// }
// else {
//
// }
// if (number<=31){ //JEJ, m130212.txt
// month = "01";
// day = String.valueOf(number);
// }
// else if (number > 31 && number <= 59){
// month = "02";
// day = String.valueOf(number-31);
// }
// else if (number > 31 && number <= 59){
// month = "03";
// day = String.valueOf(number-31);
// }
// else if (fileName.startsWith("ha")){ CNB,NGK, WNG
// temp = fileName.substring(3, 10);
// year = temp.substring(0, 4);
// }
//
// return monthDay;
// }
public static String getTimeFromFileName(String fileName) { // CL22013041.min
String time = "";
String temp = "";
String year = "";
String month = "";
String day = "";
String num = "";
Calendar cal = Calendar.getInstance();
if (fileName.startsWith("m")) { // JEJ, m130212.txt
temp = fileName.substring(1, 7);
year = "20" + temp.substring(4, 6);
month = temp.substring(2, 4);
day = temp.substring(0, 2);
} else if (fileName.startsWith("ha")) {
temp = fileName.substring(2, 9);
year = temp.substring(3, 7);
num = temp.substring(0, 3);
try {
cal.set(Calendar.DAY_OF_YEAR, Integer.parseInt(num));
} catch (NumberFormatException e) {
}
month = String.valueOf(cal.get(Calendar.MONTH));
day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
} else if (fileName.startsWith("BOU") || fileName.startsWith("CL2")
|| fileName.startsWith("CMO") || fileName.startsWith("OTT")
|| fileName.startsWith("MEA")) {
temp = fileName.substring(3, 10);
year = temp.substring(0, 4);
num = temp.substring(4, 7);
try {
cal.set(Calendar.DAY_OF_YEAR, Integer.parseInt(num));
} catch (NumberFormatException e) {
}
month = String.valueOf(cal.get(Calendar.MONTH) + 1);
day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
} else if (fileName.startsWith("ha") || fileName.startsWith("CNB")
|| fileName.startsWith("OTT") || fileName.startsWith("WNG")) {
temp = fileName.substring(3, 10);
year = temp.substring(0, 4);
month = temp.substring(4, 6);
day = temp.substring(6, 8);
}
if (month.length() == 1)
month = "0" + month;
if (day.length() == 1)
day = "0" + day;
time = year + "-" + month + "-" + day;
return time;
}
public static float getMedian(float[] array) {
float median = 0;
if (array.length <= 1)
return array[0];
float[] arraySort = array.clone();
Arrays.sort(arraySort);
// remove missing data
List<Float> newArray = new ArrayList<Float>();
for (int k = 0; k < arraySort.length - 1; k++)
if (arraySort[k] != MISSING_VAL)
newArray.add(arraySort[k]);
else
break; // to sorted arraySort
int size = newArray.size();
if (size / 2 == 0)
median = (newArray.get(size / 2) + newArray.get(size / 2 - 1)) / 2;
else
median = newArray.get((size - 1) / 2);
return median;
}
}

View file

@ -1,5 +1,7 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag.dao;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagAvg;
import java.util.Date;
import java.util.List;
@ -11,10 +13,10 @@ import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagAvg;
import com.raytheon.uf.edex.database.DataAccessLayerException;
import com.raytheon.uf.edex.database.dao.CoreDao;
import com.raytheon.uf.edex.database.dao.DaoConfig;
import com.raytheon.uf.edex.database.query.DatabaseQuery;
/**
* Record implementation for geomag avgDao.
@ -24,6 +26,7 @@ import com.raytheon.uf.edex.database.dao.DaoConfig;
* Date Ticket# Engineer Description
* ------------ ---------- ---------------- --------------------------
* 08/14/2013 T989 qzhou Initial creation.
* 03/13/2014 sgurung Added method purgeDataByRefTime()
* </pre>
*
* @author qzhou
@ -49,13 +52,14 @@ public class GeoMagAvgDao extends CoreDao {
return (GeoMagAvg) super.queryById(id);
}
public int getAreaId (int id){
return queryById(id).getId();
}
/**
* Retrieves data from postGres
*
* @return Criteria list
*/
@SuppressWarnings("unchecked")
public List<GeoMagAvg> getAvgForStation(final String stationCode, final Date start, final Date end) {
public List<GeoMagAvg> getAvgForStation(final String stationCode,
final Date start, final Date end) {
return (List<GeoMagAvg>) txTemplate.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
@ -64,11 +68,8 @@ public class GeoMagAvgDao extends CoreDao {
Criteria crit = sess.createCriteria(GeoMagAvg.class);
Criterion where1 = Restrictions.eq("stationCode", stationCode);
crit.add(where1);
// Criterion where2 = Restrictions.gt("avgTime", start);
// crit.add(where2);
// Criterion where3 = Restrictions.le("avgTime", end);
// crit.add(where3);
Criterion where2 = Restrictions.between("avgTime", start, end);//include bounds, but don't need bounds
Criterion where2 = Restrictions.between("avgTime", start, end);
crit.add(where2);
return crit.list();
}
@ -76,7 +77,8 @@ public class GeoMagAvgDao extends CoreDao {
}
@SuppressWarnings("unchecked")
public List<GeoMagAvg> getSingleAvg(final String stationCode, final Date date) {
public List<GeoMagAvg> getSingleAvg(final String stationCode,
final Date date) {
return (List<GeoMagAvg>) txTemplate.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
@ -91,4 +93,12 @@ public class GeoMagAvgDao extends CoreDao {
}
});
}
public int purgeDataByRefTime(Date refTime) throws DataAccessLayerException {
DatabaseQuery deleteStmt = new DatabaseQuery(this.daoClass);
// add 30 minutes to get hourly average reference time
Date avgTime = new Date(refTime.getTime() + (30 * 60000));
deleteStmt.addQueryParam("avgTime", avgTime);
return this.deleteByCriteria(deleteStmt);
}
}

View file

@ -1,8 +1,13 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag.dao;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagAvg;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagK1min;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagK3hr;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagRecord;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.raytheon.uf.common.dataplugin.PluginException;
import com.raytheon.uf.common.dataplugin.persist.IPersistable;
@ -12,14 +17,11 @@ import com.raytheon.uf.common.datastorage.records.AbstractStorageRecord;
import com.raytheon.uf.common.datastorage.records.FloatDataRecord;
import com.raytheon.uf.edex.database.DataAccessLayerException;
import com.raytheon.uf.edex.database.plugin.PluginDao;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagAvg;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagK1min;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagK3hr;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagRecord;
import com.raytheon.uf.edex.database.purge.PurgeLogger;
/**
* This is a Data Access Object (DAO) driver to interact with geomag database table and HDF5 data store.
* This is a Data Access Object (DAO) driver to interact with geomag database
* table and HDF5 data store.
*
* <pre>
* SOFTWARE HISTORY
@ -37,13 +39,45 @@ public class GeoMagDao extends PluginDao {
/** The source data access object */
private GeoMagAvgDao avgDao = new GeoMagAvgDao();
private GeoMagK3hrDao k3hrDao = new GeoMagK3hrDao();
private GeoMagK1minDao k1minDao = new GeoMagK1minDao();
public GeoMagDao(String pluginName) throws PluginException {
super(pluginName);
}
/**
* Overridden to purge expired data from geomag_k1min, geomag_houravg and
* geomag_k3hr tables as well
*/
@Override
public int purgeDataByRefTime(Date refTime,
Map<String, String> productKeys, boolean trackHdf5,
boolean trackToUri, Map<String, List<String>> hdf5FileToUriPurged)
throws DataAccessLayerException {
int results = super.purgeDataByRefTime(refTime, productKeys, trackHdf5,
trackToUri, hdf5FileToUriPurged);
// delete expired data from geomag_k1min, geomag_houravg and geomag_k3hr
// tables
try {
avgDao.purgeDataByRefTime(refTime);
k1minDao.purgeDataByRefTime(refTime);
k3hrDao.purgeDataByRefTime(refTime);
} catch (Exception e) {
PurgeLogger
.logError(
"Purging expired data from the secondary tables for this plugin failed.",
this.pluginName);
}
return results;
}
// @Override
// public StorageStatus persistToHDF5(PluginDataObject... records)
// throws PluginException {
@ -52,9 +86,11 @@ public class GeoMagDao extends PluginDao {
// // NOTE: currently making the assumption that models aren't
// // mixed in the records aggregate. If this isn't true,
// // some pre-processing will be needed.
// Map<PointDataContainer, List<PointDataView>> containerMap = new HashMap<PointDataContainer, List<PointDataView>>(
// Map<PointDataContainer, List<PointDataView>> containerMap = new
// HashMap<PointDataContainer, List<PointDataView>>(
// records.length);
// Map<PointDataContainer, File> fileMap = new HashMap<PointDataContainer, File>();
// Map<PointDataContainer, File> fileMap = new HashMap<PointDataContainer,
// File>();
//
// for (PluginDataObject p : records) {
// if (p instanceof IPointData) {
@ -184,42 +220,18 @@ public class GeoMagDao extends PluginDao {
}
@Override
public IDataStore populateDataStore(IDataStore dataStore, IPersistable record)
throws StorageException {
//return null;
public IDataStore populateDataStore(IDataStore dataStore,
IPersistable record) throws StorageException {
GeoMagRecord magRecord = (GeoMagRecord) record;
// change to 00:00:00.0. "/geomag/2013-04-01_00:00:00.0/BOU/102/GEOMAG";
// String headUri = magRecord.getDataURI();
// headUri = headUri.substring(0, 18) +"_00:00:00.0/" + headUri.substring(30);
/*
* Write observation times to HDF5.
*/
// if (magRecord.getObsTimes() != null ) {
// AbstractStorageRecord storageRecord = new LongDataRecord(GeoMagRecord.OBS_TIME,
// headUri, (long[]) magRecord.getObsTimes(), 1,
// new long[] {magRecord.getObsTimes().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
// if (magRecord.getCompIdx() != null) {
// AbstractStorageRecord storageRecord = new IntegerDataRecord(GeoMagRecord.CompIdx,
// magRecord.getDataURI(), (int[]) magRecord.getCompIdx(), 1,
// new long[] {magRecord.getCompIdx().length});
//
// storageRecord.setCorrelationObject(record);
// dataStore.addDataRecord(storageRecord);
// }
/*
* Write component1 data to HDF5.
*/
if (magRecord.getComp1Data() != null) {
AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.component1,
magRecord.getDataURI(), (float[]) magRecord.getComp1Data(), 1,
AbstractStorageRecord storageRecord = new FloatDataRecord(
GeoMagRecord.component1, magRecord.getDataURI(),
(float[]) magRecord.getComp1Data(), 1,
new long[] { magRecord.getComp1Data().length });
storageRecord.setCorrelationObject(magRecord);
dataStore.addDataRecord(storageRecord);
@ -230,308 +242,15 @@ public class GeoMagDao extends PluginDao {
* Write component2 data to HDF5.
*/
if (magRecord.getComp2Data() != null) {
AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.component2,
magRecord.getDataURI(), (float[]) magRecord.getComp2Data(), 1,
AbstractStorageRecord storageRecord = new FloatDataRecord(
GeoMagRecord.component2, magRecord.getDataURI(),
(float[]) magRecord.getComp2Data(), 1,
new long[] { magRecord.getComp2Data().length });
storageRecord.setCorrelationObject(magRecord);
dataStore.addDataRecord(storageRecord);
}
// /*
// * Write component3 data to HDF5.
// */
// if ( magRecord.getComp3Data() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.component3,
// magRecord.getDataURI(), (float[]) magRecord.getComp3Data(), 1,
// new long[] {magRecord.getComp3Data().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// /*
// * Write component4 data to HDF5.
// */
// if ( magRecord.getComp4Data() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.component4,
// magRecord.getDataURI(), (float[]) magRecord.getComp4Data(), 1,
// new long[] {magRecord.getComp4Data().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
// if (magRecord.getHrAvgIdx() != null) {
// AbstractStorageRecord storageRecord = new IntegerDataRecord(GeoMagRecord.HrAvgIdx,
// magRecord.getDataURI(), (int[]) magRecord.getHrAvgIdx(), 1,
// new long[] {magRecord.getHrAvgIdx().length});
//
// storageRecord.setCorrelationObject(record);
// dataStore.addDataRecord(storageRecord);
// }
//
// /*
// * Write H_HR_AVG data to HDF5.
// */
// if ( magRecord.getHrAvgH() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.H_HR_AVG,
// magRecord.getDataURI(), (float[]) magRecord.getHrAvgH(), 1,
// new long[] {magRecord.getHrAvgH().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// /*
// * Write D_HR_AVG data to HDF5.
// */
// if ( magRecord.getHrAvgD() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.D_HR_AVG,
// magRecord.getDataURI(), (float[]) magRecord.getHrAvgD(), 1,
// new long[] {magRecord.getHrAvgD().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
// /*
// * Write K_Index data to HDF5.
// */
// // 3hr
// if ( magRecord.getKKIndex() != null ) {
// AbstractStorageRecord storageRecord = new IntegerDataRecord(GeoMagRecord.K_Index,
// magRecord.getDataURI(), (int[]) magRecord.getKKIndex(), 1,
// new long[] {magRecord.getKKIndex().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getKKGamma() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.K_Gamma,
// magRecord.getDataURI(), (float[]) magRecord.getKKGamma(), 1,
// new long[] {magRecord.getKKGamma().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getKKReal() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.K_Real,
// magRecord.getDataURI(), (float[]) magRecord.getKKReal(), 1,
// new long[] {magRecord.getKKReal().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getKestIndex() != null ) {
// AbstractStorageRecord storageRecord = new IntegerDataRecord(GeoMagRecord.Kest_Index,
// magRecord.getDataURI(), (int[]) magRecord.getKestIndex(), 1,
// new long[] {magRecord.getKestIndex().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getKestGamma() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.Kest_Gamma,
// magRecord.getDataURI(), (float[]) magRecord.getKestGamma(), 1,
// new long[] {magRecord.getKestGamma().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getKestReal() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.Kest_Real,
// magRecord.getDataURI(), (float[]) magRecord.getKestReal(), 1,
// new long[] {magRecord.getKestReal().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getHKGamma() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.KH_Gamma,
// magRecord.getDataURI(), (float[]) magRecord.getHKGamma(), 1,
// new long[] {magRecord.getHKGamma().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getHKReal() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.KH_Real,
// magRecord.getDataURI(), (float[]) magRecord.getHKReal(), 1,
// new long[] {magRecord.getHKReal().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getDKGamma() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.KD_Gamma,
// magRecord.getDataURI(), (float[]) magRecord.getDKGamma(), 1,
// new long[] {magRecord.getDKGamma().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getDKReal() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.KD_Real,
// magRecord.getDataURI(), (float[]) magRecord.getDKReal(), 1,
// new long[] {magRecord.getDKReal().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
//
//
// // 1 min
// if ( magRecord.getKestIndex1m() != null ) {
// AbstractStorageRecord storageRecord = new IntegerDataRecord(GeoMagRecord.Kest_Index_1m,
// magRecord.getDataURI(), (int[]) magRecord.getKestIndex1m(), 1,
// new long[] {magRecord.getKestIndex1m().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getKestGamma1m() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.Kest_Gamma_1m,
// magRecord.getDataURI(), (float[]) magRecord.getKestGamma1m(), 1,
// new long[] {magRecord.getKestGamma1m().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getKestReal1m() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.Kest_Real_1m,
// magRecord.getDataURI(), (float[]) magRecord.getKestReal1m(), 1,
// new long[] {magRecord.getKestReal1m().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getHKGamma1m() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.KH_Gamma_1m,
// magRecord.getDataURI(), (float[]) magRecord.getHKGamma1m(), 1,
// new long[] {magRecord.getHKGamma1m().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getHKReal1m() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.KH_Real_1m,
// magRecord.getDataURI(), (float[]) magRecord.getHKReal1m(), 1,
// new long[] {magRecord.getHKReal1m().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getDKGamma1m() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.KD_Gamma_1m,
// magRecord.getDataURI(), (float[]) magRecord.getDKGamma1m(), 1,
// new long[] {magRecord.getDKGamma1m().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getDKReal1m() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.KD_Real_1m,
// magRecord.getDataURI(), (float[]) magRecord.getDKReal1m(), 1,
// new long[] {magRecord.getDKReal1m().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
// if ( magRecord.getHKIndex1m() != null ) {
// AbstractStorageRecord storageRecord = new IntegerDataRecord(GeoMagRecord.KH_Index_1m,
// magRecord.getDataURI(), (int[]) magRecord.getHKIndex1m(), 1,
// new long[] {magRecord.getHKIndex1m().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getDKIndex1m() != null ) {
// AbstractStorageRecord storageRecord = new IntegerDataRecord(GeoMagRecord.KD_Index_1m,
// magRecord.getDataURI(), (int[]) magRecord.getDKIndex1m(), 1,
// new long[] {magRecord.getDKIndex1m().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getHCount() != null ) {
// AbstractStorageRecord storageRecord = new IntegerDataRecord(GeoMagRecord.KH_Count,
// magRecord.getDataURI(), (int[]) magRecord.getHCount(), 1,
// new long[] {magRecord.getHCount().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getDCount() != null ) {
// AbstractStorageRecord storageRecord = new IntegerDataRecord(GeoMagRecord.KD_Count,
// magRecord.getDataURI(), (int[]) magRecord.getDCount(), 1,
// new long[] {magRecord.getDCount().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getHDev() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.KH_Dev,
// magRecord.getDataURI(), (float[]) magRecord.getHDev(), 1,
// new long[] {magRecord.getHDev().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if ( magRecord.getDDev() != null ) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.KD_Dev,
// magRecord.getDataURI(), (float[]) magRecord.getDDev(), 1,
// new long[] {magRecord.getDDev().length});
// storageRecord.setCorrelationObject(magRecord);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if (magRecord.getKs() != null) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.K_s,
// magRecord.getDataURI(), (float[]) magRecord.getKs(), 1,
// new long[] {magRecord.getKs().length});
// storageRecord.setCorrelationObject(record);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if (magRecord.getAest() != null) {
// AbstractStorageRecord storageRecord = new IntegerDataRecord(GeoMagRecord.A_est,
// magRecord.getDataURI(), (int[]) magRecord.getAest(), 1,
// new long[] {magRecord.getAest().length});
// storageRecord.setCorrelationObject(record);
// dataStore.addDataRecord(storageRecord);
//
// }
//
// if (magRecord.getLastUpdate() != null) {
// AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.Last_Update_1m,
// magRecord.getDataURI(), (float[]) magRecord.getLastUpdate(), 1,
// new long[] {magRecord.getLastUpdate().length});
// storageRecord.setCorrelationObject(record);
// dataStore.addDataRecord(storageRecord);
//
// }
return dataStore;
}
@ -542,16 +261,11 @@ public class GeoMagDao extends PluginDao {
return (GeoMagAvg) avgDao.queryById(avgTime);
}
// public Integer getGeoMagSourceId(String sourceName) throws DataAccessLayerException {
// return avgDao.getSourceId(sourceName);
// }
public GeoMagAvgDao getGeoMagAvgDao() {
return avgDao;
}
public void setGeoMagAvgDao(
GeoMagAvgDao avgDao) {
public void setGeoMagAvgDao(GeoMagAvgDao avgDao) {
this.avgDao = avgDao;
}
@ -559,15 +273,14 @@ public class GeoMagDao extends PluginDao {
* Get GeoMagK1min from ID
*/
public GeoMagK1min getGeoMagDateK1min(int id) {
return k1minDao.queryById(id);
return (GeoMagK1min) k1minDao.queryById(id);
}
public GeoMagK1minDao getGeoMagK1minDao() {
return k1minDao;
}
public void setGeoMagK1minDao(
GeoMagK1minDao k1minDao) {
public void setGeoMagK1minDao(GeoMagK1minDao k1minDao) {
this.k1minDao = k1minDao;
}
@ -575,15 +288,14 @@ public class GeoMagDao extends PluginDao {
* Get GeoMagK1min from ID
*/
public GeoMagK3hr getGeoMagDateK3hr(int id) {
return k3hrDao.queryById(id);
return (GeoMagK3hr) k3hrDao.queryById(id);
}
public GeoMagK3hrDao getGeoMagK3hrDao() {
return k3hrDao;
}
public void setGeoMagK3hrDao(
GeoMagK3hrDao k3hrDao) {
public void setGeoMagK3hrDao(GeoMagK3hrDao k3hrDao) {
this.k3hrDao = k3hrDao;
}
}

View file

@ -1,8 +1,7 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag.dao;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagK1min;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@ -14,10 +13,10 @@ import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagK1min;
import com.raytheon.uf.edex.database.DataAccessLayerException;
import com.raytheon.uf.edex.database.dao.CoreDao;
import com.raytheon.uf.edex.database.dao.DaoConfig;
import com.raytheon.uf.edex.database.query.DatabaseQuery;
/**
* Record implementation for geomag k1minDao.
@ -27,6 +26,8 @@ import com.raytheon.uf.edex.database.dao.DaoConfig;
* Date Ticket# Engineer Description
* ------------ ---------- ---------------- --------------------------
* 08/14/2013 T989 qzhou Initial creation.
* 03/03/2014 #1110 qzhou Added method getRangeK1min(), Cleaned code
* 03/13/2014 sgurung Added method purgeDataByRefTime()
* </pre>
*
* @author qzhou
@ -35,37 +36,56 @@ import com.raytheon.uf.edex.database.dao.DaoConfig;
public class GeoMagK1minDao extends CoreDao {
/**
* Creates a new GribModelDao
* Creates a new GeoMagK1minDao
*/
public GeoMagK1minDao() {
super(DaoConfig.forClass(GeoMagK1min.class));
}
/**
* Retrieves a GeoMagAvgId based on the given id
* Retrieves data from postGres
*
* @param id
* The given ID number
* @return The GeoMagAvgId
* @return Criteria list
*/
public GeoMagK1min queryById(int id) {
return (GeoMagK1min) super.queryById(id);
}
public int getAreaId (int id){
return queryById(id).getId();
}
@SuppressWarnings("unchecked")
public List<GeoMagK1min> getSingleK1min(final String stationCode, final Date date) {
return (List<GeoMagK1min>) txTemplate.execute(new TransactionCallback() {
public List<GeoMagK1min> getRangeK1min(final String stationCode,
final Date start, final Date end) {
return (List<GeoMagK1min>) txTemplate
.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
HibernateTemplate ht = getHibernateTemplate();
Session sess = ht.getSessionFactory().getCurrentSession();
Session sess = ht.getSessionFactory()
.getCurrentSession();
Criteria crit = sess.createCriteria(GeoMagK1min.class);
Criterion where1 = Restrictions.eq("stationCode", stationCode);
Criterion where1 = Restrictions.eq("stationCode",
stationCode);
crit.add(where1);
Criterion where2 = Restrictions.ge("refTime", start);
crit.add(where2);
Criterion where3 = Restrictions.lt("refTime", end);
crit.add(where3);
return crit.list();
}
});
}
@SuppressWarnings("unchecked")
public List<GeoMagK1min> getSingleK1min(final String stationCode,
final Date date) {
return (List<GeoMagK1min>) txTemplate
.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
HibernateTemplate ht = getHibernateTemplate();
Session sess = ht.getSessionFactory()
.getCurrentSession();
Criteria crit = sess.createCriteria(GeoMagK1min.class);
Criterion where1 = Restrictions.eq("stationCode",
stationCode);
crit.add(where1);
Criterion where2 = Restrictions.eq("refTime", date);
crit.add(where2);
@ -73,4 +93,10 @@ public class GeoMagK1minDao extends CoreDao {
}
});
}
public int purgeDataByRefTime(Date refTime) throws DataAccessLayerException {
DatabaseQuery deleteStmt = new DatabaseQuery(this.daoClass);
deleteStmt.addQueryParam("refTime", refTime);
return this.deleteByCriteria(deleteStmt);
}
}

View file

@ -1,5 +1,6 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag.dao;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagK3hr;
import java.util.Date;
import java.util.List;
@ -12,10 +13,10 @@ import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagK3hr;
import com.raytheon.uf.edex.database.DataAccessLayerException;
import com.raytheon.uf.edex.database.dao.CoreDao;
import com.raytheon.uf.edex.database.dao.DaoConfig;
import com.raytheon.uf.edex.database.query.DatabaseQuery;
/**
* Record implementation for geomag k3hrDao.
@ -25,6 +26,7 @@ import com.raytheon.uf.edex.database.dao.DaoConfig;
* Date Ticket# Engineer Description
* ------------ ---------- ---------------- --------------------------
* 08/14/2013 T989 qzhou Initial creation.
* 03/13/2014 sgurung Added method purgeDataByRefTime()
* </pre>
*
* @author qzhou
@ -40,22 +42,13 @@ public class GeoMagK3hrDao extends CoreDao {
}
/**
* Retrieves a GeoMagAvgId based on the given id
* Retrieves data from postGres
*
* @param id
* The given ID number
* @return The GeoMagAvgId
* @return Criteria list
*/
public GeoMagK3hr queryById(int id) {
return (GeoMagK3hr) super.queryById(id);
}
public int getId (int id){
return queryById(id).getId();
}
@SuppressWarnings("unchecked")
public List<GeoMagK3hr> getRangeK3hr(final String stationCode, final Date start, final Date end) {
public List<GeoMagK3hr> getRangeK3hr(final String stationCode,
final Date start, final Date end) {
return (List<GeoMagK3hr>) txTemplate.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
@ -75,7 +68,8 @@ public class GeoMagK3hrDao extends CoreDao {
}
@SuppressWarnings("unchecked")
public List<GeoMagK3hr> getSingleK3hr(final String stationCode, final Date time) {
public List<GeoMagK3hr> getSingleK3hr(final String stationCode,
final Date time) {
return (List<GeoMagK3hr>) txTemplate.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
@ -90,5 +84,10 @@ public class GeoMagK3hrDao extends CoreDao {
}
});
}
}
public int purgeDataByRefTime(Date refTime) throws DataAccessLayerException {
DatabaseQuery deleteStmt = new DatabaseQuery(this.daoClass);
deleteStmt.addQueryParam("refTime", refTime);
return this.deleteByCriteria(deleteStmt);
}
}

View file

@ -0,0 +1,314 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag.request;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagAvg;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagK1min;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagK3hr;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagRecord;
import gov.noaa.nws.ncep.common.dataplugin.geomag.calculation.CalcUtil;
import gov.noaa.nws.ncep.common.dataplugin.geomag.dao.GeoMagAvgDao;
import gov.noaa.nws.ncep.common.dataplugin.geomag.dao.GeoMagDao;
import gov.noaa.nws.ncep.common.dataplugin.geomag.dao.GeoMagK1minDao;
import gov.noaa.nws.ncep.common.dataplugin.geomag.dao.GeoMagK3hrDao;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import com.raytheon.uf.common.dataquery.db.QueryParam;
import com.raytheon.uf.edex.database.DataAccessLayerException;
import com.raytheon.uf.edex.database.query.DatabaseQuery;
/**
*
* Retrieve data from database utility for a given dataURI
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* 2014/02/12 #1123 qzhou Moved from edex to here
*
* </pre>
*
* @author qzhou
* @version 1.0
*/
public class DatabaseUtil {
private static final int AVG_DATA_RANGE = 30;
private static final float MISSING_VAL = 99999.99f;
/*
* from geomag
*/
public static List<?> retrieveUriForAvg(GeoMagDao dao, String dataUri,
Date time) {
String station = CalcUtil.getStationFromUri(dataUri);
DatabaseQuery query = new DatabaseQuery(GeoMagRecord.class.getName());
query.addReturnedField("component_1");
query.addReturnedField("component_2");
query.addReturnedField("dataTime.refTime");
query.addReturnedField("badDataPoint");
query.addReturnedField("sourceId");
// called only when time is 59min, so include it.
query.addQueryParam("dataTime.refTime", time,
QueryParam.QueryOperand.LESSTHANEQUALS);
Calendar cal = Calendar.getInstance();
cal.setTime(time);
cal.add(Calendar.HOUR_OF_DAY, -1);
query.addQueryParam("dataTime.refTime", cal.getTime(),
QueryParam.QueryOperand.GREATERTHAN);
query.addQueryParam("stationCode", station);
List<?> resultsList = null;
try {
resultsList = dao.queryByCriteria(query); // 60
} catch (DataAccessLayerException e1) {
e1.printStackTrace();
}
return resultsList;
}
/*
* from geomag_houravg
*/
public static List<GeoMagAvg> retrieveSingleAvg(String dataUri, Date time) {
GeoMagAvgDao avgDao = new GeoMagAvgDao();
String station = CalcUtil.getStationFromUri(dataUri);
List<GeoMagAvg> resultsList = null;
resultsList = avgDao.getSingleAvg(station, time);
return resultsList;
}
/*
* from geomag_houravg
*/
public static List<GeoMagAvg> retrieveUriBy3hr(String dataUri, Date spTime) {
GeoMagAvgDao avgDao = new GeoMagAvgDao();
String station = CalcUtil.getStationFromUri(dataUri);
Calendar cal = Calendar.getInstance();
cal.setTime(spTime);
cal.add(Calendar.DAY_OF_YEAR, -AVG_DATA_RANGE); // at least one day is
// needed for gt, lt
// since avg have min=30, cal.getTime() and spTime are not included
List<GeoMagAvg> resultsList = null;
resultsList = avgDao.getAvgForStation(station, cal.getTime(), spTime); // 720
return resultsList;
}
/*
* from geomag
*/
public static List<?> retrieveUriForK1min(GeoMagDao dao, String dataUri,
Date time) {
String station = CalcUtil.getStationFromUri(dataUri);
DatabaseQuery query = new DatabaseQuery(GeoMagRecord.class.getName());
query.addReturnedField("component_1");
query.addReturnedField("component_2");
query.addReturnedField("dataTime.refTime");
query.addReturnedField("badDataPoint");
query.addReturnedField("sourceId");
// Document uses epTime-1minute. Consider 3 sources, we use current time
query.addQueryParam("dataTime.refTime", time,
QueryParam.QueryOperand.LESSTHANEQUALS);
Date epTime = CalcUtil.getEPTime(time);
Calendar cal = Calendar.getInstance();
cal.setTime(epTime);
cal.add(Calendar.HOUR_OF_DAY, -48);
// start time is epTime-48hour. So use GREATERTHANEQUALS
query.addQueryParam("dataTime.refTime", cal.getTime(),
QueryParam.QueryOperand.GREATERTHANEQUALS);
query.addQueryParam("stationCode", station);
List<?> resultsList = null;
try {
resultsList = dao.queryByCriteria(query); // 2880
} catch (DataAccessLayerException e1) {
e1.printStackTrace();
}
return resultsList;
}
/*
* from geomag_k1min
*/
public static List<GeoMagK1min> retrieveSingleK1min(String dataUri,
Date time) {
GeoMagK1minDao k1minDao = new GeoMagK1minDao();
String station = CalcUtil.getStationFromUri(dataUri);
List<GeoMagK1min> resultsList = null;
resultsList = k1minDao.getSingleK1min(station, time);
return resultsList;
}
/*
* from geomag_k3hr
*/
public static List<GeoMagK3hr> retrieveUriForK3hr(String dataUri,
Date epTime) {
GeoMagK3hrDao k3hrDao = new GeoMagK3hrDao();
String station = CalcUtil.getStationFromUri(dataUri);
Calendar cal = Calendar.getInstance();
cal.setTime(epTime);
cal.add(Calendar.DAY_OF_YEAR, -1);
List<GeoMagK3hr> resultsList = null;
resultsList = k3hrDao.getRangeK3hr(station, cal.getTime(), epTime); // 1
return resultsList;
}
/*
* from geomag_k3hr
*/
public static List<GeoMagK3hr> retrieveSingleK3hr(String dataUri,
Date epTime) {
GeoMagK3hrDao k3hrDao = new GeoMagK3hrDao();
String station = CalcUtil.getStationFromUri(dataUri);
List<GeoMagK3hr> resultsList = null;
resultsList = k3hrDao.getSingleK3hr(station, epTime);
return resultsList;
}
/*
* sort n lists
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void sort(List... lists) {
assert lists.length > 0;
Object[][] objects = new Object[lists[0].size()][lists.length];
for (int i = 0; i < lists.length; i++) {
int j = 0;
for (Object object : lists[i]) {
objects[j++][i] = object;
}
}
Arrays.sort(objects, new Comparator<Object[]>() {
public int compare(Object[] o1, Object[] o2) {
return ((Comparable) o1[0]).compareTo(o2[0]);
}
});
for (int i = 0; i < lists.length; i++) {
lists[i].clear();
for (Object[] tuple : objects) {
lists[i].add(tuple[i]);
}
}
}
/*
* fill time tag gaps, return fullBestList
*/
public static void fillHrAvgTimeGaps(List<GeoMagAvg> dataList,
List<Date> dateListFinal, List<Float> hHrAvgListFinal,
List<Float> dHrAvgListFinal, Date spTime) {
List<Date> dateList = new ArrayList<Date>();
List<Float> hHrAvgList = new ArrayList<Float>();
List<Float> dHrAvgList = new ArrayList<Float>();
for (int i = 0; i < dataList.size(); i++) { // 1 extra
GeoMagAvg row = dataList.get(i);
dateList.add((Date) row.getAvgTime());
hHrAvgList.add((Float) row.gethHrAvg());
dHrAvgList.add((Float) row.getdHrAvg());
}
DatabaseUtil.sort(dateList, hHrAvgList, dHrAvgList);
/*
* fill missing
*/
// fill missing in the beginning
Date date = (Date) dateList.get(0);
int hr0 = date.getHours();
if (hr0 != spTime.getHours()) {
for (int k = 0; k < hr0; k++) {
Date dateNew = (Date) date.clone();
dateNew.setHours(k); // change setMinutes to setHours
dateListFinal.add(dateNew);
hHrAvgListFinal.add(MISSING_VAL);
dHrAvgListFinal.add(MISSING_VAL);
}
}
// fill missing in the middle
for (int i = 0; i < dateList.size(); i++) {
Date date0 = dateList.get(i);
dateListFinal.add(date0); // change from data to data0
hHrAvgListFinal.add(hHrAvgList.get(i));
dHrAvgListFinal.add(dHrAvgList.get(i));
if (i + 1 < dateList.size()) {
Date date1 = (Date) dateList.get(i + 1);
int diffHr = (int) (date1.getTime() - date0.getTime())
/ (3600 * 1000);
if (diffHr != 1) {
for (int j = 0; j < diffHr - 1; j++) {
dateListFinal.add(new Date(date0.getTime() + 3600
* 1000 * (j + 1)));
// append after i, i+1
hHrAvgListFinal.add(MISSING_VAL);
dHrAvgListFinal.add(MISSING_VAL);
}
}
}
}
// fill missing in the end // changed ending from 24 * AVG_DATA_RANGE to
// 23(end of the day)
int latest = dateListFinal.size();
if (latest < 24 * AVG_DATA_RANGE) {
for (int k = latest; k < 24 * AVG_DATA_RANGE; k++) {
dateListFinal.add(new Date(dateListFinal.get(latest - 1)
.getTime() + 3600 * 1000 * (k + 1)));
hHrAvgListFinal.add(MISSING_VAL);
dHrAvgListFinal.add(MISSING_VAL);
}
}
}
}

View file

@ -0,0 +1,72 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag.request;
import java.util.Date;
import com.raytheon.uf.common.serialization.annotations.DynamicSerialize;
import com.raytheon.uf.common.serialization.annotations.DynamicSerializeElement;
import com.raytheon.uf.common.serialization.comm.IServerRequest;
/**
*
* Request for a GeoMagAvg for the given dataURI
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* 2014/02/12 #1110 qzhou Init
*
* </pre>
*
* @author qzhou
* @version 1.0
*/
@DynamicSerialize
public class RetrieveHrAvgRequest implements IServerRequest {
@DynamicSerializeElement
private String stationCode;
@DynamicSerializeElement
private Date startTime;
@DynamicSerializeElement
private Date endTime;
public RetrieveHrAvgRequest() {
}
public RetrieveHrAvgRequest(String stationCode, Date startTime, Date endTime) {
super();
this.stationCode = stationCode;
this.startTime = startTime;
this.endTime = endTime;
}
public String getStationCode() {
return stationCode;
}
public void setStationCode(String stationCode) {
this.stationCode = stationCode;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
}

View file

@ -0,0 +1,72 @@
package gov.noaa.nws.ncep.common.dataplugin.geomag.request;
import java.util.Date;
import com.raytheon.uf.common.serialization.annotations.DynamicSerialize;
import com.raytheon.uf.common.serialization.annotations.DynamicSerializeElement;
import com.raytheon.uf.common.serialization.comm.IServerRequest;
/**
*
* Request for a GeoMagk1min for the given dataURI
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* 2014/02/12 #1110 qzhou Init
*
* </pre>
*
* @author qzhou
* @version 1.0
*/
@DynamicSerialize
public class RetrieveK1minRequest implements IServerRequest {
@DynamicSerializeElement
private String stationCode;
@DynamicSerializeElement
private Date startTime;
@DynamicSerializeElement
private Date endTime;
public RetrieveK1minRequest() {
}
public RetrieveK1minRequest(String stationCode, Date startTime, Date endTime) {
super();
this.stationCode = stationCode;
this.startTime = startTime;
this.endTime = endTime;
}
public String getStationCode() {
return stationCode;
}
public void setStationCode(String stationCode) {
this.stationCode = stationCode;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
}

View file

@ -38,4 +38,20 @@
</camelContext>
<bean id="RetrieveHrAvgHandler"
class="gov.noaa.nws.ncep.edex.plugin.geomag.handler.RetrieveHrAvgRequestHandler" />
<bean factory-bean="handlerRegistry" factory-method="register">
<constructor-arg
value="gov.noaa.nws.ncep.common.dataplugin.geomag.request.RetrieveHrAvgRequest" />
<constructor-arg ref="RetrieveHrAvgHandler" />
</bean>
<bean id="RetrieveK1minHandler"
class="gov.noaa.nws.ncep.edex.plugin.geomag.handler.RetrieveK1minRequestHandler" />
<bean factory-bean="handlerRegistry" factory-method="register">
<constructor-arg
value="gov.noaa.nws.ncep.common.dataplugin.geomag.request.RetrieveK1minRequest" />
<constructor-arg ref="RetrieveK1minHandler" />
</bean>
</beans>

View file

@ -1,454 +0,0 @@
package gov.noaa.nws.ncep.edex.plugin.geomag.calculation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
* The calculation of k, 1 minute related.
*
* <pre>
* SOFTWARE HISTORY
*
* ate Ticket# Engineer Description
* ----------- ---------- ---------- --------------------------
* 05/14/2013 #989 qzhou Initial Creation
* </pre>
*
* @author qzhou
* @version 1
*/
public class CalcEach1min {
private static final float MISSING_VAL = 99999.99f;
private static final int MAX_GAP_LENGTH = 15;
private static final int SMOOTH_WINDOW = 60;//
private static final int TRANSITION_TIME = 60;
private static final int PHASE_POWER = 3;
private static final int HARM_ORDER = 5;
private static int HOURS = 24;
private static int MINUTES = 60;
/*
* @param dataIn -- data of 4320
*/
public static float[] fillGaps(float[] dataIn){
float[] data = dataIn.clone();
int i = 0;
int size = data.length;
// if (data.length != 4320 )
// return data;
while (i < size) {
// Find the next missing value
int flag = 0; //flag used for break
while (i < size && flag == 0) {
if (data[i] == MISSING_VAL)
flag=1;
else
i++;
}
// If a gap was found handle it
if (i < size) {
int gapIndex = i; //index of first missing value
//Find the last missing point
flag = 0;
while (i < size && flag ==0) {
if (data[i] != MISSING_VAL)
flag=1;
else
i++;
}
// Interpolate the gap if possible. We cannot extrapolate
if ((gapIndex > 0) && (i < size)) {
//Now i is the index of first non-missing value
// and GapIndex is the index of first missing value
int gapLength = i-gapIndex; //i is index of first non-missing value
// Interpolate if the gap is small enough
if (gapLength < MAX_GAP_LENGTH) {
float value1 = data[gapIndex-1];
float value2 = data[i];
for (int j=1; j < gapLength+1; j++)
data[gapIndex++] = value1 + (j * (value2-value1)) / (gapLength+1);
}
}
}
}
return data;
}
/*
* 24 element floating point array. (DefLength + 30 + kLength)
* Find out how many points are used to get the centered hour average
*/
public static float[] getFitLength(float[] defLength, float[] kIndex, float[] kLength) {
float[] fitLength = new float[HOURS];
int[] ind = new int[HOURS];
float[] curK = new float[HOURS];
for (int i = 0; i < HOURS; i++) {
fitLength[i] = 30.0f + defLength[i];
ind[i] = (int) Math.floor(i/3);
curK[i] = kIndex[ind[i]];
if (curK[i] != MISSING_VAL )
fitLength[i] += kLength[(int)curK[i]];
//System.out.println("***fitLength "+fitLength[i] +" "+curK[i]+" "+defLength[i]);
if (fitLength[i] >1440)
fitLength[i] = 1440;
}
return fitLength;
}
/* @param data (hhdata, dddata), float[4320]
* @return -- 24 element floating point array. Calculate averages centered on each hour of the day
*/
public static float[] getCentHourAvg(float[] data, float[] fitLength, float[] defLength) {
float[] HrAvg = new float[HOURS]; //double
Arrays.fill(HrAvg, MISSING_VAL);
for (int ihr = 0; ihr < HOURS; ihr++) {
// take middle interval
int center = 1440 + ihr * MINUTES + 30;
int start = center - Math.round(fitLength[ihr]);
int end = center + Math.round(fitLength[ihr]);
int missing = 0;
double sum = 0;
// if data[i] have no missing value
for ( int i = start; i < end+1; i++) {
//System.out.print("**here2 "+data[i] +" ");
if (data[i] != MISSING_VAL) {
sum += data[i];
}
else {
missing++;
break;//this loop
}
}
if (missing == 0) // no missing value
HrAvg[ihr] = (float) sum /(end - start + 1);
}
// if HrAvg have missing value
// Extrapolate the first missing points--missing beginning
int hr0 = 0;
int flag = 0;
while (hr0 < HOURS && flag == 0) {
if (HrAvg[hr0] != MISSING_VAL)
flag=1;
else {
hr0++;
//System.out.println("**here2");
}
}
if (hr0 > 0 && hr0 < HOURS)
for (int i = 0; i < hr0; i++)
HrAvg[i] = HrAvg[hr0];
// Extrapolate the last missing points--missing end
int hr1 = 23;
while ((hr1 > hr0) && (HrAvg[hr1] == MISSING_VAL))
hr1--;
if (hr1 < 23)
for (int i = hr1+1; i < HOURS; i++)
HrAvg[i] = HrAvg[hr1];
// Interpolate the missing points between hour0 and hour1
// Both hour0 and hour1 are hours where data exists
while (hr0 < hr1) {
//System.out.println("**here4");
do {
hr0++;
} while (hr0 < hr1 && HrAvg[hr0] != MISSING_VAL );
if (hr0 < hr1) {
int hr = hr0; //first missing hour
while ((hr0 < hr1) && (HrAvg[hr0] == MISSING_VAL))
hr0++;
int gapLength = hr0-hr;
float value1 = HrAvg[hr-1];//not missing
float value2 = HrAvg[hr0]; //not missing
//System.out.println("***gapLength "+gapLength +" "+value1 +" "+value2);
for (int i=1; i < gapLength+1; i++)
HrAvg[hr++] = value1+(i*(value2-value1))/(gapLength+1);
}
}
// for (int i=0;i<HrAvg.length;i++)
// System.out.print("***centHrAvg[i] "+HrAvg[i] +" ");
// while (hour0 lt hour1) do begin
// repeat begin
// hour0++
// found = (HrAvg[hour0] eq MissingValue)
// endrep until ((hour0 ge hour1) or (found))
// if (found) then begin
// hour = hour0 ; first missing hour
// while ((hour0 lt hour1) and (HrAvg[hour0] eq MissingValue)) do hour0++
// GapLength = hour0-hour
// Value1 = HrAvg[hour-1]
// Value2 = HrAvg[hour0]
// for i=1,GapLength do HrAvg[hour++] = Value1+(i*(Value2-Value1))/(GapLength+1)
// endif
// endwhile
return HrAvg;
}
/*
* @param hrAvg -- QHA data 1440
*/
public static float[] getHarmonicFit(float[] hrCentAvg) {
float[] fitCurve = new float[1440];
int delta = MINUTES; // minutes between points in HrAvg
int t0 = 30; // time tag for first point in HrAvg
float t1 = (HOURS-1)*delta + t0; // time tag for last point in HrAvg
// Rotate HrAvg so that 1st and last points are equal, store in HA
float r_coeff = (hrCentAvg[HOURS-1] - hrCentAvg[0])/(t1 - t0);
float[] hrA = new float[HOURS];//0.0*HrAvg
for (int i = 0; i < HOURS; i++)
hrA[i] = hrCentAvg[i] - r_coeff*(i*delta);
// Calculate first Fourier series coefficients up to Horder
float[] reA = new float[HARM_ORDER +1]; // real part of the Fourier Series Coefficients (initially 0)
float[] imA = new float[HARM_ORDER +1]; // imaginary part of Fourier Series Coefficients (initially 0)
for (int i = 0; i < HARM_ORDER +1; i++) {
for (int j=0; j < HOURS; j++) {
reA[i] += hrA[j] * Math.cos(2 * (Math.PI) * j * i / HOURS);
imA[i] -= hrA[j] * Math.sin(2 * (Math.PI) * j * i / HOURS);
}
}
// Derive FitCurve as harmonic fit using inverse transform
for (int t=0; t < HOURS*delta; t++) { // t is minute of the day
float theta = (float) (2 * (Math.PI) * (t-t0) / (HOURS*delta));
fitCurve[t] = reA[0]/HOURS;
for (int i=1; i < HARM_ORDER +1; i++)
fitCurve[t] += (2 * reA[i] * Math.cos(i*theta) - 2*imA[i] * Math.sin(i*theta)) / HOURS;
// Derotate FitCurve by same amount as HrAvg
fitCurve[t] += r_coeff*(t-t0);
}
return fitCurve;
}
/*
* @param hdev,ddev -- float[1440]
*/
public static List getKIndex(float[] hdev, float[] ddev, int[] kLimit, int missingFlag) {
List<float[]> list = new ArrayList<float[]>();
// Initialize the return data with MissingValue
float[] kIndex = new float[8];
float[] hk = new float[8];
float[] dk = new float[8];
float[] gamma = new float[8];
float[] hGamma = new float[8];
float[] dGamma = new float[8];
Arrays.fill(kIndex, MISSING_VAL);
Arrays.fill(hk, MISSING_VAL);
Arrays.fill(dk, MISSING_VAL);
Arrays.fill(gamma, MISSING_VAL);
Arrays.fill(hGamma, MISSING_VAL);
Arrays.fill(dGamma, MISSING_VAL);
// Check for bad input data
int npts = hdev.length;
if (npts != ddev.length)
return list;
if (npts < 1261 || npts > 1440) //21*60+1
return list;
// Step through each three hourly interval
for (int ipd=0; ipd < 8; ipd++) {
int istart = ipd * 180;
int iend = istart + 180 - 1;
if (iend >= npts)
iend = npts - 1 ; //allow for partial interval on the end
if (iend < istart)
continue ; //should never happen...
// hhdev = hdev[istart:iend]
int i = 0;
int ii = 0;
int npdpts = iend - istart + 1 ; //number of possible points in the period, =180
float[] hhdev = new float[npdpts];
float[] dddev = new float[npdpts];
for (int j = istart; j < iend+1; j++) {
hhdev[j-istart] = hdev[j];
dddev[j-istart] = ddev[j];
}
// get hdevGood
for (i = npdpts-1; i >=0; i--)
if (hhdev[i] != MISSING_VAL && hhdev[i] != 0)
break;
//System.out.println("***iii "+i);
for (ii = npdpts-1; ii >=0; ii--)
if (dddev[ii] != MISSING_VAL && dddev[i] != 0)
break;
// i, ii are the last data that is not missing
float[] hdevGood = new float[i +1];
float[] ddevGood = new float[ii+1];
if (i >-1)
for (int j = 0; j < i+1; j++)
hdevGood[j] = hhdev[j];
if (ii > -1)
for (int j = 0; j < ii+1; j++)
ddevGood[j] = dddev[j];
if ( missingFlag == 0 || (i > -1 && ii > -1)) {
if (hdevGood != null && hdevGood.length != 0)
hGamma[ipd] = CalcUtil.maxValue(hdevGood) - CalcUtil.minValue(hdevGood);
if (ddevGood != null && ddevGood.length != 0)
dGamma[ipd] = CalcUtil.maxValue(ddevGood) - CalcUtil.minValue(ddevGood);
if (hGamma[ipd] != MISSING_VAL)
hk[ipd] = CalcUtil.getKfromTable(kLimit, hGamma[ipd]);
if (dGamma[ipd] != MISSING_VAL)
dk[ipd] = CalcUtil.getKfromTable(kLimit, dGamma[ipd]);
// get bigger one
if (hk[ipd] >= dk[ipd] && hGamma[ipd] != MISSING_VAL) {
kIndex[ipd] = hk[ipd];
gamma[ipd] = hGamma[ipd];
}
else if (dk[ipd] >= hk[ipd] && dGamma[ipd] != MISSING_VAL){
kIndex[ipd] = dk[ipd];
gamma[ipd] = dGamma[ipd];
}
}
}
list.add(0, kIndex);
list.add(1, gamma);
list.add(2, hk);
list.add(3, hGamma);
list.add(4, dk);
list.add(5, dGamma);
return list;
}
/*
* Force QHAQDC and QDAQDC to be continuous between the last and the first value using a +/- SMOOTH_WINDOW
*/
public static float[] getQHAQDC(float[] qdc){
float[] data = qdc.clone(); //new float[1440];
if (qdc.length != 1440)
return data;
float jump = qdc[0] - qdc[1439];
for (int i = 0; i < SMOOTH_WINDOW; i++) {
data[1440-SMOOTH_WINDOW-i] += i/(SMOOTH_WINDOW-1) * 0.5 * jump;
data[i] -= (1 - i/(SMOOTH_WINDOW-1)) * 0.5 * jump;
}
return data;
}
/*
* find index in hhdata that indicates current time
* currTimeIndex = first 1440 minutes + prev day minutes + curr day minutes
*/
public static int getCurrTimeIndex(int hour, int min, int epHour) {
if (epHour == 0)
epHour = 24;
int currTimeIndex = HOURS*MINUTES + (HOURS-epHour) * MINUTES
+ hour * MINUTES + min;
//System.out.println("***index "+currTimeIndex+ " "+cal.get(Calendar.MINUTE)+" "+cal.get(Calendar.HOUR_OF_DAY));
return currTimeIndex;
}
/*
*
*/
public static float[] getExtrapolation(float[] dataIn, float[] qhaQdc, int currTimeIndex){ //4320
float[] data = dataIn.clone();
int j0 = currTimeIndex;//Last good H or D index
if (data.length != 4320 || qhaQdc.length != 1440)
return data;
if (data[j0] != MISSING_VAL) {
for (int j = j0 +1; j < 4320; j++) {
int w2 = j - j0 -1; //from .pro
int w1 = TRANSITION_TIME - w2;
if (w1 < 0)
w1 = 0;
//System.out.println("**qhaQdc "+qhaQdc[j % 1440]);
data[j] = (w1 * data[j0] + w2 * qhaQdc[j % 1440]) / (w1 + w2);
}
}
return data;
}
public static float[] getDev(float[] data, float[] qdc){
float[] dev = new float[1440];
if (data.length != 4320 || qdc.length != 1440)
return dev;
for (int i = 0; i < 1440; i++) {
//System.out.print("***data-qdc "+i+" " +data[i+1440] +" "+qdc[i] +" ");
if (data[i+1440] != MISSING_VAL && qdc[i] != MISSING_VAL)
dev[i] = data[i+1440] - qdc[i];
else
dev[i] = MISSING_VAL;
}
return dev;
}
public static float[] adjustHrCentAvg(float[] hcAIn, float [] qha, float[] gamma, int[] kLimit){
float[] hcA = hcAIn.clone();
float wh = 0;
if (hcA.length != HOURS || gamma.length != 8)
return hcA;
for (int ipd = 0; ipd < 8; ipd++) {
if (gamma[ipd] < kLimit[4])
wh = 1;
else if (gamma[ipd] >= kLimit[4] && gamma[ipd] < kLimit[6])
wh = (float) Math.pow( ((kLimit[6] - gamma[ipd]) /(kLimit[6] - kLimit[4])), PHASE_POWER);
else
wh = 0;
for (int j = 0; j < 3; j++) {
hcA[ipd*3+j] = wh * hcA[ipd*3+j] + (1-wh) * qha[ipd*3+j];//?
//System.out.println("***WH "+wh+" "+hcA[ipd*3+j]);
}
}
return hcA;
}
}

View file

@ -1,335 +0,0 @@
package gov.noaa.nws.ncep.edex.plugin.geomag.calculation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/*
* The calculation of k, 3 hour related.
*
* <pre>
* SOFTWARE HISTORY
*
* ate Ticket# Engineer Description
* ----------- ---------- ---------- --------------------------
* 05/14/2013 #989 qzhou Initial Creation
* </pre>
*
* @author qzhou
* @version 1
*/
public class CalcEach3hr {
private static final float MISSING_VAL = 99999.99f;
private static final int NIGHT_LENGTH = 90; //min
private static final int DAWN_LENGTH = 60;
private static final int DAY_LENGTH = 0;
private static final int DUSK_LENGTH = 60;
private static int DAYS = 30;
private static int HOURS = 24;
private static int MINUTES = 60;
/*
* calculate hrAvgs for this hour
* @param bestList -- contains 1 hour data
*/
public static float[] getSimpleHourAvg(List bestList){
float[] simpHrAvg = new float[2];
float simpHrAvg1 = 0;
float simpHrAvg2 = 0;
double sum1 = 0;
double sum2 = 0;
int rec1 = 0;
int rec2 = 0;
for (int i = 0; i < bestList.size(); i++) {
List<Float> list = (List<Float>) bestList.get(i);
float comp1 = (Float)list.get(1);
float comp2 = (Float)list.get(2);
if ( comp1 != MISSING_VAL) {
sum1 += comp1;
rec1++;
}
if ( comp2 != MISSING_VAL) {
sum2 += comp2;
rec2++;
}
}
if (rec1 > 30) // less than half missing value
simpHrAvg1 = (float) sum1 / rec1;
else
simpHrAvg1 = MISSING_VAL;
if (rec2 > 30) // less than half missing value
simpHrAvg2 = (float) sum2 / rec2;
else
simpHrAvg2 = MISSING_VAL;
simpHrAvg[0] = simpHrAvg1;
simpHrAvg[1] = simpHrAvg2;
return simpHrAvg;
}
/*
* calculate hrAvgs for this day.
* @param data -- data of one day, 1440
*/
public static float[] getSimpleHourAvg(float[] data){ //data 1440
float[] simpHrAvg = new float[HOURS];
for (int ihr = 0; ihr < HOURS; ihr++) {
double sum = 0;
int missing = 0;
for ( int i = ihr*MINUTES; i < ihr*MINUTES+MINUTES; i++) {
if (data[i] != MISSING_VAL)
sum += data[i];
else
missing++;
}
if (missing < 30) // less than half missing value
simpHrAvg[ihr] = (float) sum / (MINUTES-missing);
else
simpHrAvg[ihr] = MISSING_VAL;
}
return simpHrAvg;
}
/*
* calculate hrAvgs for this hour in data array
* @param data -- data of one day, 1440
*/
public static float getSimpleHourAvg(float[] data, int hour){ //one day 1440, avg for hour-1
float simpHrAvg = 0;
double sum = 0;
int rec = 0;
if (data.length <= hour*MINUTES+MINUTES)
for (int i = hour*MINUTES; i < data.length; i++) {
if ( data[i] != MISSING_VAL) {
sum += data[i];
rec++;
}
}
else
for (int i = hour*MINUTES; i < hour*MINUTES+MINUTES; i++) {
if ( data[i] != MISSING_VAL) {
sum += data[i];
rec++;
}
}
if (rec > 30) // less than half missing value
simpHrAvg = (float) sum / (rec);
else
simpHrAvg = MISSING_VAL;
return simpHrAvg;
}
/*
* @param simpHrAvgH -- data of 30 intervals(720 hours)
* @return disturbance levels for 30 intervals
*/
public static float[] getDisturbanceLevel(float[] simpHrAvgH, float[] simpHrAvgD){
float[] dB = new float[30];
for (int j = 0; j < DAYS; j++) {
double sum = 0;
int missing = 0;
int endOfArray = simpHrAvgH.length;
int endTime = (endOfArray > j*HOURS+HOURS) ? j*HOURS+HOURS :endOfArray;
for ( int i = j*HOURS; i < endTime-1; i++) {
if (simpHrAvgH[i] != MISSING_VAL && simpHrAvgD[i] != MISSING_VAL &&
simpHrAvgH[i+1] != MISSING_VAL && simpHrAvgD[i+1] != MISSING_VAL ) {
sum += Math.sqrt( Math.pow((simpHrAvgH[i+1] - simpHrAvgH[i]), 2)
+ Math.pow((simpHrAvgD[i+1] - simpHrAvgD[i]), 2) );
}
else
missing++;
}
if (missing <= 12) // not 12 or more missing
dB[j] = (float) sum / (HOURS-1-missing);
else
dB[j] = MISSING_VAL;
}
return dB;
}
/*
* @param dB -- float[30 ]
* @return --5 smallest disturbance levels
*/
public static Map getSmallDisturbanceLevel(float[] dB){
//create a map that key=dBIndex and value=dBValue.
//create a duplicate array dBDup. Sort it.
//take 5 smallest dBDup[i]. Then find its index and value from the dB. Put them to the map
Map<Integer, Float> dBSmall = new HashMap<Integer, Float>();
float[] dBDup = new float[dB.length];
for (int i = 0; i < dBDup.length; i++) {
dBDup[i] = dB[i];
}
Arrays.sort(dBDup);
float dupIndex = (int)MISSING_VAL ;
float wk = 0;
//take 5 smallest dBDup
for (int j = 0; j < 5; j++) {
for (int i = 0; i < dB.length; i++) {
if (dB[i] == dBDup[j] && i != dupIndex) { //for duplicated values
//System.out.println("***dBDup[j] "+dBDup[j] +" "+wk+" "+i +" "+j);
dBSmall.put(i, dB[i]);
dupIndex = i;
break;
}
}
}
return dBSmall;
}
/*
* @param -- dBSmall, 5 set map
* @param -- simpHrAvg, -- float[720]
* @rturn -- quietLevelHourAvg, float[24]
*/
public static float[] getQuietLevelHourAvg(Map<Integer, Float> dBSmall, float[] simpHrAvg){
if (dBSmall.entrySet().size() < 5)
return simpHrAvg;
float[] quietHrAvg = new float[24];
Arrays.fill(quietHrAvg, MISSING_VAL);
int[] index = new int [5];
float[] dB = new float[5];
int k = 0;
Iterator<?> iter = dBSmall.entrySet().iterator();
while (iter.hasNext()) {
@SuppressWarnings("unchecked")
Map.Entry<Integer, Float> mEntry = (Map.Entry<Integer, Float>) iter.next(); //sorted on key
index[k] = mEntry.getKey();
dB[k] = mEntry.getValue();
k++;
}
//construct smallHrAvg array (24*5) from simpHrAvg (24*30)
float[] smallHrAvg = new float[24*5];
for (int j = 0; j < 5; j++) { //k=5
int endOfArray = smallHrAvg.length;
int endTime = (endOfArray > j*HOURS+HOURS) ? j*HOURS+HOURS :endOfArray;
for (int i = j*HOURS; i < endTime; i++) {
smallHrAvg[i] = simpHrAvg[ index[j]*HOURS + i%HOURS ]; //700
}
}
for (int ihr = 0; ihr < HOURS; ihr++) {
float sumAvg = 0;
float sumWk = 0;
float wk = 0;
for (int jk = 0; jk < 5; jk++) {
int ind = jk*HOURS+ihr;
if (dB[jk] < 1)
wk = 1;
else
wk = 1 / (dB[jk] *dB[jk]);
if (smallHrAvg[ind] != MISSING_VAL){
sumAvg += wk * smallHrAvg[ind];
sumWk += wk;
}
}
if (sumWk >0)
quietHrAvg[ihr] = sumAvg / sumWk;
}
return quietHrAvg;
}
/*
* @param -- quietHrAvg, float[24]
* @return -- shifted quietLevelHourAvg, float[24]
*/
public static float[] getQHA(float[] quietHrAvg){
float[] QHA = new float[24];
if (quietHrAvg.length != 24)
return quietHrAvg;
for (int ihr = 0; ihr < 24; ihr++) {
QHA[ihr] = quietHrAvg[ (ihr+3) %24 ];
}
return QHA;
}
/*
* @return -- 24 element floating point array. Default fitting lengths.
* (one for each hour of the 24 hour interval that ends at EPtime).
*/
public static float[] getDefLength(String station, int epHour) {
float[] defLength = new float[24];
float lon = CalcUtil.getLongitude(station);
int UTdiff = Math.round(1440.0f * lon / 360.0f);
int minute0 = epHour * MINUTES;
for (int ihr = 0; ihr < HOURS; ihr++) {
float sum = 0;
for (int imin = 0; imin < MINUTES; imin++) {
int curMin = (minute0 + ihr*MINUTES + imin) % 1440;
int localMin = (curMin + UTdiff) % 1440;
if (localMin >= 0 && localMin < 180)
sum += NIGHT_LENGTH;
else if (localMin >= 180 && localMin < 360)
sum += DAWN_LENGTH;
else if (localMin >= 360 && localMin < 1080)
sum += DAY_LENGTH;
else if (localMin >= 1080 && localMin < 1260)
sum += DUSK_LENGTH;
else if (localMin >= 1260 && localMin < 1440)
sum += NIGHT_LENGTH;
}
defLength[ihr] = sum / MINUTES;
}
return defLength;
}
}

View file

@ -1,443 +0,0 @@
package gov.noaa.nws.ncep.edex.plugin.geomag.calculation;
import gov.noaa.nws.ncep.common.dataplugin.geomag.table.KsThree;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/*
* The calculation of Kp and related.
*
* <pre>
* SOFTWARE HISTORY
*
* ate Ticket# Engineer Description
* ----------- ---------- ---------- --------------------------
* 05/14/2013 #989 qzhou Initial Creation
* </pre>
*
* @author qzhou
* @version 1
*/
public class CalcKp {
private static final float MISSING_VAL = 99999.99f;
public CalcKp() {
}
public static float[] getKest(String station, float[] kIndex, float[] gamma) {
float[] kest = new float[8];
for (int i=0; i<8; i++) {
int[] gammaLimit = CalcUtil.getKLimit(station); //.getGammaFromK(station, gamma);
if (kIndex[i] < 9) {
kest[i] = kIndex[i] + (gamma[i] - gammaLimit[(int)kIndex[i]]) / (gammaLimit[(int)kIndex[i]+1] - gammaLimit[(int)kIndex[i]]);}
else if (kIndex[i] == MISSING_VAL)
kest[i] = MISSING_VAL;
else
kest[i] = 9.0f;
//System.out.println("**kest "+kest[i] +" "+kIndex[i]+" "+gamma[i]);
}
return kest;
}
public static float getKest(String station, int kIndex, float gamma) {
float kest = 0;
int[] gammaLimit = CalcUtil.getKLimit(station);
if (kIndex < 9)
kest = kIndex + (gamma - gammaLimit[kIndex])/(gammaLimit[kIndex+1] - gammaLimit[kIndex]);
else if (kIndex == MISSING_VAL)
kest = MISSING_VAL;
else
kest = 9.0f;
// System.out.println("****kest "+kest +" "+kIndex);
return kest;
}
// public static float getKest(String station,int[] kLimit, float gamma) {
// float kest = 0;
// int kIndex = CalcUtil.getKfromTable(kLimit, gamma);
//
// int gammaLimit = CalcUtil.getGammaFromK(station, kIndex+1); //kLimit[kIndex]; //getGammaLimit( station, kIndex);
// int gammaLimit1 = CalcUtil.getGammaFromK(station, kIndex+1); //kLimit[kIndex+1]; //getKLimit( station, kIndex+1);;
//
// if (gammaLimit1 > gammaLimit)
// kest = kIndex + (gamma - gammaLimit) / (gammaLimit1 - gammaLimit);
//
// return kest;
// }
// protected float[] getKest(String station, float[] gamma) {
// float kest = 0;
// int kIndex[] = CalcUtil.getKfromTable(station, gamma);
//
// int gammaLimit = CalcUtil.getGammaFromK(station, kIndex+1); //kLimit[kIndex]; //getGammaLimit( station, kIndex);
// int gammaLimit1 = CalcUtil.getGammaFromK(station, kIndex+1); //kLimit[kIndex+1]; //getKLimit( station, kIndex+1);;
//
// if (gammaLimit1 > gammaLimit)
// kest = kIndex + (gamma - gammaLimit) / (gammaLimit1 - gammaLimit);
//
// return kest;
// }
/*
* list of the station coefficient values in the order of 00-03, 03-06...
*/
public static ArrayList<KsThree> getKsThreeList(String station) {
ArrayList<KsThree> threeKsList = CalcUtil.getStationCoeff().getStationByCode(station).getKsThree();//size 24
return threeKsList;
}
public static List<Integer> getKsThree(Date time, String station, int k) {
List<Integer> ks = new ArrayList<Integer>();;
//KsThree ksThree = null;
ArrayList<KsThree> ksThreeList = getKsThreeList(station);
if (ksThreeList != null && !ksThreeList.isEmpty()) {
int hour = CalcUtil.getSPTime(time).getHours();
int period = hour/3;// 24 -> 8
KsThree ksThree = ksThreeList.get(period);
//System.out.println("**kss "+ksThree.getK1()+" "+ksThree.getK2()+" "+ksThree.getK3()+" "+ksThree.getK5()+" "+ksThree.getK6()+" "+getKsOfKsThree(k, ksThree));
if (ksThree != null)
ks.add(getKsOfKsThree(k, ksThree));
ksThree = ksThreeList.get(period + 8);
//System.out.println("**kss "+ksThree.getK1()+" "+ksThree.getK2()+" "+ksThree.getK3()+" "+ksThree.getK5()+" "+ksThree.getK6()+" "+getKsOfKsThree(k, ksThree));
if (ksThree != null)
ks.add(getKsOfKsThree(k, ksThree));
ksThree = ksThreeList.get(period + 16);
if (ksThree != null)
ks.add(getKsOfKsThree(k, ksThree));
// ksThree = ksThreeList.get(period);
// if (ksThree != null)
// ks.add(getKsOfKsThree(k, ksThree));
//
// if (mon == 1 || mon == 2 || mon == 11 || mon == 12) {
// ksThree = ksThreeList.get(hour/3 -1);
// ks = getKsOfKsThree(k, ksThree);
// }
// else if (mon == 3 || mon == 4 || mon == 9 || mon == 10) {
// ksThree = ksThreeList.get(8 + hour/3 -1);
// ks = getKsOfKsThree(k, ksThree);
// }
// else if (mon >= 5 && mon <= 8) {
// ksThree = ksThreeList.get(16 + hour/3 -1);
// ks = getKsOfKsThree(k, ksThree);
// }
}
// for (int i = 0; i < threeKs.size(); i++) {
// String season = threeKs.get(i).getSeason();
// String period = threeKs.get(i).getPeriod();
// int k1 = threeKs.get(i).getK1();
// int k2 = threeKs.get(i).getK2();
// int k3 = threeKs.get(i).getK3();
// int k4 = threeKs.get(i).getK4();
// int k5 = threeKs.get(i).getK5();
// int k6 = threeKs.get(i).getK6();
// int k7 = threeKs.get(i).getK7();
// int k8 = threeKs.get(i).getK8();
// int k9 = threeKs.get(i).getK9();
// int k10 = threeKs.get(i).getK10();
// System.out.println("**k1 k2 "+season+" "+period+" "+k1+" "+k2);
// }
return ks;
}
private static int getKsOfKsThree(int k, KsThree ksThree) {
int ks = 0;
if (k == 0)
ks = ksThree.getK0();
else if (k == 1)
ks = ksThree.getK1();
else if (k == 2)
ks = ksThree.getK2();
else if (k == 3)
ks = ksThree.getK3();
else if (k == 4)
ks = ksThree.getK4();
else if (k == 5)
ks = ksThree.getK5();
else if (k == 6)
ks = ksThree.getK6();
else if (k == 7)
ks = ksThree.getK7();
else if (k == 8)
ks = ksThree.getK8();
else if (k == 9)
ks = ksThree.getK9();
return ks;
}
public static float getKs(String station, int k, Date time) throws ParseException {
float a = 0;
float b = 0;
float ks = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//int year = time.getYear();113
Calendar cal = Calendar.getInstance();
cal.setTime(time);
int year = cal.get(Calendar.YEAR);
Date date1 = sdf.parse(year+"-01-01");
Date date2 = sdf.parse(year+"-02-14");
Date date3 = sdf.parse(year+"-02-24");
Date date4 = sdf.parse(year+"-03-06");
Date date5 = sdf.parse(year+"-03-16");
Date date6 = sdf.parse(year+"-04-16");
Date date7 = sdf.parse(year+"-04-26");
Date date8 = sdf.parse(year+"-05-06");
Date date9 = sdf.parse(year+"-05-16");
Date date10 = sdf.parse(year+"-08-17");
Date date11 = sdf.parse(year+"-08-27");
Date date12 = sdf.parse(year+"-09-06");
Date date13 = sdf.parse(year+"-09-16");
Date date14 = sdf.parse(year+"-10-17");
Date date15 = sdf.parse(year+"-10-27");
Date date16 = sdf.parse(year+"-11-06");
Date date17 = sdf.parse(year+"-11-16");
Date date18 = sdf.parse(year+"-12-31");
Date date2Leep = sdf.parse(year+"-02-15");
Date date3Leep = sdf.parse(year+"-02-25");
List<Integer> ksThree = getKsThree(time, station, k );
if(time.compareTo(date1) >= 0 && time.compareTo(date2) < 0){
ks = (float) ksThree.get(0) /3;
}
else if (time.compareTo(date4) >= 0 && time.compareTo(date5) < 0){
ks = (float)(0.25f*ksThree.get(0) + 0.75f*ksThree.get(1)) /3;
}
else if (time.compareTo(date5) >= 0 && time.compareTo(date6) < 0){
ks = (float)ksThree.get(1) /3;
}
else if (time.compareTo(date6) >= 0 && time.compareTo(date7) < 0){
ks = (float) (0.75f*ksThree.get(1) + 0.25f*ksThree.get(2)) /3;
}
else if (time.compareTo(date7) >= 0 && time.compareTo(date8) < 0){
ks = (float) (0.5f*ksThree.get(1) + 0.5f*ksThree.get(2)) /3;
}
else if (time.compareTo(date8) >= 0 && time.compareTo(date9) < 0){
ks = (float) (0.25f*ksThree.get(1) + 0.75f*ksThree.get(2)) /3;
}
else if (time.compareTo(date9) >= 0 && time.compareTo(date10) < 0){
ks = (float) ksThree.get(2) /3;
}
else if (time.compareTo(date10) >= 0 && time.compareTo(date11) < 0){
ks = (float) (0.75f*ksThree.get(2) + 0.25f*ksThree.get(1)) /3;
}
else if (time.compareTo(date11) >= 0 && time.compareTo(date12) < 0){
ks = (float) (0.5f*ksThree.get(2) + 0.5f*ksThree.get(1)) /3;
}
else if (time.compareTo(date12) >= 0 && time.compareTo(date13) < 0){
ks = (float) (0.25f*ksThree.get(2) + 0.75f*ksThree.get(1)) /3;
}
else if (time.compareTo(date13) >= 0 && time.compareTo(date14) < 0){
ks = (float) ksThree.get(1) /3;
}
else if (time.compareTo(date14) >= 0 && time.compareTo(date15) < 0){
ks = (float) (0.75f*ksThree.get(1) + 0.25f*ksThree.get(0)) /3;
}
else if (time.compareTo(date15) >= 0 && time.compareTo(date16) < 0){
ks = (float) (0.5f*ksThree.get(1) + 0.5f*ksThree.get(0)) /3;
}
else if (time.compareTo(date16) >= 0 && time.compareTo(date17) < 0){
ks = (float) (0.25f*ksThree.get(1) + 0.75f*ksThree.get(0)) /3;
}
else if (time.compareTo(date17) >= 0 && time.compareTo(date18) <= 0){
ks = (float) ksThree.get(0) /3;
}
else if (CalcUtil.isLeapYear(year)) {
if(time.compareTo(date2Leep) >= 0 && time.compareTo(date3Leep) < 0){
ks = (float) (0.75f*ksThree.get(0) + 0.25f*ksThree.get(1)) /3;
}
else if(time.compareTo(date3Leep) >= 0 && time.compareTo(date4) < 0){
ks = (float) (0.5f*ksThree.get(0) + 0.5f*ksThree.get(1)) /3;
}
}
else {
if(time.compareTo(date2) >= 0 && time.compareTo(date3) < 0){
ks = (float) (0.75f*ksThree.get(0) + 0.25f*ksThree.get(1)) /3;
}
else if(time.compareTo(date3) >= 0 && time.compareTo(date4) < 0){
ks = (float) (0.5f*ksThree.get(0) + 0.5f*ksThree.get(1)) /3;
}
}
return ks;
}
// protected float[] getKs(String station, float[] kest) {
// float a = 0;
// float b = 0;
// float[] ks = new float[8];
//
// Map<Float, Float> abCoeff = CalcUtil.getCoeffAandB(station);
// if (abCoeff.size() != 8)
// return ks;
//
// int i = 0;
// Iterator<?> iter = abCoeff.entrySet().iterator();
// while (iter.hasNext()) {
// @SuppressWarnings("unchecked")
// Map.Entry<Float, Float> mEntry = (Map.Entry<Float, Float>) iter.next();
//
// a = mEntry.getKey();
// b = mEntry.getValue();
// ks[i] = a + b * kest[i];
// i++;
// }
//
// return ks;
// }
public static float getKs(String station, float kest, String timePrd) {
float a = 0;
float b = 0;
float ks = 0;
Map<Float, Float> abCoeff = CalcUtil.getCoeffAandB(station);
if (abCoeff.size() != 8)
return ks;
int j = 0;
if (timePrd.equalsIgnoreCase("00-03"))
j = 0;
else if (timePrd.equalsIgnoreCase("03-06"))
j = 1;
else if (timePrd.equalsIgnoreCase("06-09"))
j = 2;
else if (timePrd.equalsIgnoreCase("09-12"))
j = 3;
else if (timePrd.equalsIgnoreCase("12-15"))
j = 4;
else if (timePrd.equalsIgnoreCase("15-18"))
j = 5;
else if (timePrd.equalsIgnoreCase("18-21"))
j = 6;
else if (timePrd.equalsIgnoreCase("21-24"))
j = 7;
int i = 0;
Iterator<?> iter = abCoeff.entrySet().iterator();
while (iter.hasNext()) {
@SuppressWarnings("unchecked")
Map.Entry<Float, Float> mEntry = (Map.Entry<Float, Float>) iter.next();
if (i == j) {
a = mEntry.getKey();
b = mEntry.getValue();
ks = a + b * kest;
break;
}
i++;
}
return ks;
}
public static int getAest(String station, int kIndex) {
return CalcUtil.getK2a(kIndex);
}
@SuppressWarnings("unchecked")
public static float[] getKpEst(String[] station, float[] ks ) {
float kpEst[] = new float[ks.length];
float[][] wcoeff = new float[station.length][ks.length];
if (ks.length != 8)
return kpEst;
for (int i = 0; i < station.length; i++) {
Map<String, Float> coeff = CalcUtil.getCoeffW(station[i]);
int j = 0;
Iterator<?> iter = coeff.entrySet().iterator();
while (iter.hasNext()) {
wcoeff[i][j] = ((Map.Entry<String, Float>) iter.next()).getValue();
j++;
}
}
float sumW = 0;
float sumWK = 0;
for (int j = 0; j < ks.length; j++) {
for (int i = 0; i < station.length; i++) {
sumW += wcoeff[i][j];
sumWK += wcoeff[i][j] * ks[i];
}
//kpEst[i] = (float) (Math.round(3 * sumWK / sumW)) / 3;
kpEst[j] = sumWK / sumW;
kpEst[j] = (int)kpEst[j] + CalcUtil.getThird(kpEst[j]);
}
return kpEst;
}
@SuppressWarnings("unchecked")
public static float getKpEst(String[] station, float ks, String fitTime ) {
float kpEst = 0;
float[] wcoeff = new float[8];
for (int i = 0; i < station.length; i++) {
Map<String, Float> coeff = CalcUtil.getCoeffW(station[i]);
int j = 0;
Iterator<?> iter = coeff.entrySet().iterator();
while (iter.hasNext()) {
if (((Map.Entry<String, Float>) iter.next()).getKey().equalsIgnoreCase(fitTime)) {
wcoeff[i] = ((Map.Entry<String, Float>) iter.next()).getValue();
break;
}
j++;
}
}
float sumW = 0;
float sumWK = 0;
for (int i = 0; i < station.length; i++) {
sumW += wcoeff[i];
sumWK += wcoeff[i] * ks;
}
kpEst = sumWK / sumW;
kpEst = (int)kpEst + CalcUtil.getThird(kpEst);
return kpEst;
}
public static String[] getKp(float kpEst[], String[] kpModifier ) {
String[] kp = new String[kpEst.length];
if (kpEst.length != kpModifier.length)
return kp;
for (int i = 0; i < kpEst.length; i++) {
int k = Math.round(kpEst[i]);
kp[i] = k + kpModifier[i];
}
return kp;
}
public static String getKp(float kpEst, String kpModifier ) {
int kp = Math.round(kpEst);
return kp + kpModifier;
}
}

View file

@ -1,715 +0,0 @@
package gov.noaa.nws.ncep.edex.plugin.geomag.calculation;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import gov.noaa.nws.ncep.common.dataplugin.geomag.table.KFitTime;
import gov.noaa.nws.ncep.common.dataplugin.geomag.table.KsThree;
import gov.noaa.nws.ncep.common.dataplugin.geomag.util.KStationCoefficientLookup;
/*
* The k index and decoder calculation utility.
*
* <pre>
* SOFTWARE HISTORY
*
* ate Ticket# Engineer Description
* ----------- ---------- ---------- --------------------------
* 05/14/2013 #989 qzhou Initial Creation
* </pre>
*
* @author qzhou
* @version 1
*/
public class CalcUtil {
private static final float MISSING_VAL = 99999.99f;
private static final float K_EXPONENT = 3.3f;
private static KStationCoefficientLookup stationCoeff = KStationCoefficientLookup.getInstance();
// public static enum Stations { BOU,FRD,FRN,NEW,SIT,HAD,CL2,CNB,OTT,MEA,NGK,WNG}
// BOU(500, 254.76f),
// CL2(500, 2.261f),
// CNB(450, 149.36f),
// FRD(500, 282.63f),
// FRN(350, 240.28f),
// HAD(500, 355.517f),
// NEW(700, 242.88f),
// SIT(1000, 224.67f),
// CMO(2500, 212.14f);
//
// private final int k9Limit;
// private final float longitude;
// private Stations (int k9Limit, float longitude) {
// this.k9Limit = k9Limit;
// this.longitude = longitude;
// }
// }
// Gamma limit table
private static enum Limit {
K0(0),
K1(5),
K2(10),
K3(20),
K4(40),
K5(70),
K6(120),
K7(200),
K8(330),
K9(500);
private final int kConst;
private Limit (int kConst) {
this.kConst = kConst;
}
}
// public static String getStations() {
// return Stations;
// }
public static int getKConst(int k) {
int kConst = 0;
if (k == 0)
kConst = Limit.K0.kConst;
else if (k == 1)
kConst = Limit.K1.kConst;
else if (k == 2)
kConst = Limit.K2.kConst;
else if (k == 3)
kConst = Limit.K3.kConst;
else if (k == 4)
kConst = Limit.K4.kConst;
else if (k == 5)
kConst = Limit.K5.kConst;
else if (k == 6)
kConst = Limit.K6.kConst;
else if (k == 7)
kConst = Limit.K7.kConst;
else if (k == 8)
kConst = Limit.K8.kConst;
else if (k == 9)
kConst = Limit.K9.kConst;
return kConst;
}
// A-index table
private static enum K2a {
a0(0),
a1(3),
a2(7),
a3(15),
a4(27),
a5(48),
a6(80),
a7(140),
a8(240),
a9(400);
private final int a;
private K2a (int a) {
this.a = a;
}
}
public static int getK2a(int k) {
int a = 0;
if (k == 0)
a = K2a.a0.a;
else if (k == 1)
a = K2a.a1.a;
else if (k == 2)
a = K2a.a2.a;
else if (k == 3)
a = K2a.a3.a;
else if (k == 4)
a = K2a.a4.a;
else if (k == 5)
a = K2a.a5.a;
else if (k == 6)
a = K2a.a6.a;
else if (k == 7)
a = K2a.a7.a;
else if (k == 8)
a = K2a.a8.a;
else if (k == 9)
a = K2a.a9.a;
return a;
}
public static KStationCoefficientLookup getStationCoeff() {
return stationCoeff;
}
public static int getK9Limit(String station) throws NumberFormatException {
int k9 = 0;
String k9Limit = getStationCoeff().getStationByCode(station).getK9Limit();
k9 = Integer.parseInt(k9Limit);
return k9;
}
public static float getLongitude(String station) throws NumberFormatException {
float lon = 0;
if (station != null && !station.equalsIgnoreCase("")) {
String longitude = getStationCoeff().getStationByCode(station).getLongitude();
lon = Float.parseFloat(longitude);
}
return lon;
}
/*
* map of the A and the B values in the order of 00-03, 03-06...
*/
public static Map<Float, Float> getCoeffAandB(String station) {
Map<Float, Float> abCoeff = new HashMap<Float, Float>();
List<KFitTime> fitTime = getStationCoeff().getStationByCode(station).getKFitTime();
if (fitTime.size() != 8)
return abCoeff;
for (int i = 0; i < 8; i++) {
float a = fitTime.get(i).getCoeffA();
float b = fitTime.get(i).getCoeffB();
abCoeff.put(a, b);
}
// KStationCoefficientLookup look = KStationCoefficientLookup.getInstance();
// Map<String, KStationCoefficient> stationMap = look.getStationsByCodeMap();
// int size = 1+31;
// stations = new ArrayList<List>();//String[]: station, uri0, ...uri31
// for (Map.Entry<String, KStationCoefficient> entry : stationMap.entrySet()) {
// List<String> astation = new ArrayList<String>();
// astation.add( entry.getKey());
// stations.add(astation);
//// System.out.println("***entry "+ entry.getKey());
// }
return abCoeff;
}
/*
* map of the time period and the W values in the order of 00-03, 03-06...
*/
public static Map<String, Float> getCoeffW(String station) {
Map<String, Float> wCoeff = new HashMap<String, Float>();
List<KFitTime> fitTime = getStationCoeff().getStationByCode(station).getKFitTime();
if (fitTime.size() != 8)
return wCoeff;
for (int i = 0; i < 8; i++) {
String a = fitTime.get(i).getKey();
float b = fitTime.get(i).getCoeffW();
wCoeff.put(a, b);
}
return wCoeff;
}
// public static int getK9Limit(String station) {
// int k9Limit = 0;
// if (station.equalsIgnoreCase(Stations.BOU.toString()))
// k9Limit = Stations.BOU.k9Limit;
// else if (station.equalsIgnoreCase(Stations.CL2.toString()))
// k9Limit = Stations.CL2.k9Limit;
// else if (station.equalsIgnoreCase(Stations.CNB.toString()))
// k9Limit = Stations.CNB.k9Limit;
// else if (station.equalsIgnoreCase(Stations.FRD.toString()))
// k9Limit = Stations.FRD.k9Limit;
// else if (station.equalsIgnoreCase(Stations.FRN.toString()))
// k9Limit = Stations.FRN.k9Limit;
// else if (station.equalsIgnoreCase(Stations.HAD.toString()))
// k9Limit = Stations.HAD.k9Limit;
// else if (station.equalsIgnoreCase(Stations.NEW.toString()))
// k9Limit = Stations.NEW.k9Limit;
// else if (station.equalsIgnoreCase(Stations.SIT.toString()))
// k9Limit = Stations.SIT.k9Limit;
// else if (station.equalsIgnoreCase(Stations.CMO.toString()))
// k9Limit = Stations.CMO.k9Limit;
//
// return k9Limit;
// }
//
// public static float getLongitude(String station) {
// float longitude = 0;
// if (station.equalsIgnoreCase(Stations.BOU.toString()))
// longitude = Stations.BOU.longitude;
// else if (station.equalsIgnoreCase(Stations.CL2.toString()))
// longitude = Stations.CL2.longitude;
// else if (station.equalsIgnoreCase(Stations.CNB.toString()))
// longitude = Stations.CNB.longitude;
// else if (station.equalsIgnoreCase(Stations.FRD.toString()))
// longitude = Stations.FRD.longitude;
// else if (station.equalsIgnoreCase(Stations.FRN.toString()))
// longitude = Stations.FRN.longitude;
// else if (station.equalsIgnoreCase(Stations.HAD.toString()))
// longitude = Stations.HAD.longitude;
// else if (station.equalsIgnoreCase(Stations.NEW.toString()))
// longitude = Stations.NEW.longitude;
// else if (station.equalsIgnoreCase(Stations.SIT.toString()))
// longitude = Stations.SIT.longitude;
// else if (station.equalsIgnoreCase(Stations.CMO.toString()))
// longitude = Stations.CMO.longitude;
//
// return longitude;
// }
public static int[] getKLimit(String station) {
int[] kLimit = new int[10];
int k9Limit = getK9Limit(station);
for (int i = 0; i < kLimit.length; i++) {
kLimit[i] = Math.round( k9Limit * getKConst(i) /500);
}
return kLimit;
}
// public static int[] getAIndex(String station, float[] k-index) {
// int[] aIndex = new int[10];
// //int k9Limit = getK9Limit(station);
// for (int i = 0; i < kLimit.length; i++) {
// aIndex[i] = Math.round( getK2a(i));
// }
// return aIndex;
// }
public static int getKfromTable(int[] kLimit, float gamma) {
int kIndex;
//int[] kLimit = getKLimit(station);
int i = 0;
for ( i = 0; i < 10; i++) {
if (gamma > kLimit[i])
continue;
else
break;
}
//take the lower of i. this step eq. K_limit = K9limit * [5, 10, 20, 40...
if (i > 0)
i = i-1;
if (i <= 9)
kIndex = i;
else
kIndex = 9;
return kIndex;
}
public static int getGammaFromK(String station, int kIndex) {
int gamma = getK9Limit(station) * getKConst(kIndex) / 500;
return gamma;
}
// // assume db time format yyyy-mm-dd hh:mm:ss
// public static int getYear(String time) {
// int year = 0;
//
// String t = time.substring(0, 4);
// try {
// year = Integer.parseInt(t);
// } catch (NumberFormatException e) {
// e.printStackTrace();
// }
//
// return year;
// }
//
// public static int getMonth(String time) {
// int month = 0;
//
// String t = time.substring(5, 7);
// try {
// month = Integer.parseInt(t);
// } catch (NumberFormatException e) {
// e.printStackTrace();
// }
// return month;
// }
//
// public static int getDay(String time) {
// int day = 0;
//
// String t = time.substring(8, 10);
// try {
// day = Integer.parseInt(t);
// } catch (NumberFormatException e) {
// e.printStackTrace();
// }
// return day;
// }
//
// public static int getHour(String time) {
// int hr = 0;
//
// String t = time.substring(11, 13);
// try {
// hr = Integer.parseInt(t);
// } catch (NumberFormatException e) {
// e.printStackTrace();
// }
// return hr;
// }
//
// public static int getMin(String time) {
// int min = 0;
//
// String t = time.substring(14, 16);
// try {
// min = Integer.parseInt(t);
// } catch (NumberFormatException e) {
// e.printStackTrace();
// }
// return min;
// }
//
// public static int getSecond(String time) {
// int ss = 0;
//
// String t = time.substring(17);
// try {
// ss = Integer.parseInt(t);
// } catch (NumberFormatException e) {
// e.printStackTrace();
// }
// return ss;
// }
// assume db time format yyyy-mm-dd hh:mm:ss
public static Date getSPTime(Date currTime ){
Date spTime = currTime;
int hour = currTime.getHours();
if (hour >= 0 && hour < 3)
hour = 0;
else if (hour >= 3 && hour < 6)
hour = 3;
else if (hour >= 6 && hour < 9)
hour = 6;
else if (hour >= 9 && hour < 12)
hour = 9;
else if (hour >= 12 && hour < 15)
hour = 12;
else if (hour >= 15 && hour < 18)
hour = 15;
else if (hour >= 18 && hour < 21)
hour = 18;
else if (hour >= 21 && hour < 24)
hour = 21;
spTime.setHours(hour);
spTime.setMinutes(0);
spTime.setSeconds(0);
// spTime = String.valueOf(currTime.getYear()) + "-" +
// String.valueOf(currTime.getMonth()) + "-" +
// String.valueOf(currTime.getDay()) + " " +
// hour + ":" + "00:00.0";
return spTime;
}
public static Date getEPTime(Date currTime ){
Date epTime = (Date)currTime.clone();
int hour = currTime.getHours();
if (hour >= 0 && hour < 3)
hour = 3;
else if (hour >= 3 && hour < 6)
hour = 6;
else if (hour >= 6 && hour < 9)
hour = 9;
else if (hour >= 9 && hour < 12)
hour = 12;
else if (hour >= 12 && hour < 15)
hour = 15;
else if (hour >= 15 && hour < 18)
hour = 18;
else if (hour >= 18 && hour < 21)
hour = 21;
else if (hour >= 21 && hour < 24)
hour = 0;
if (hour != 0)
epTime.setHours(hour);
else {
int day = currTime.getDate() +1;
epTime.setDate(day);
epTime.setHours(hour);
}
epTime.setMinutes(0);
epTime.setSeconds(0);
return epTime;
}
public static boolean isHalfMissing(float[] items) {
boolean halfMissaing = false;
int i = 0;
for (i = 0; i < items.length; i++) {
if (items[i] == MISSING_VAL)
i++;
}
if (i > items.length/2)
halfMissaing = true;
return halfMissaing;
}
public static float getThird(float kpEst) {
float half = 0.333333f/2;
float x = kpEst - (int) kpEst; //get decimal fraction
if (x >= 0 && x <= half)
x = 0;
else if (x >= half && x <= 2*half)
x = 0.333333f;
else if (x >= 2*half && x <= 3*half)
x = 0.333333f;
else if (x >= 3*half && x <= 4*half)
x = 0.666666f;
else if (x >= 4*half && x <= 5*half)
x = 0.666666f;
else if (x >= 5*half && x <= 6*half)
x = 1;
return x;
}
public static float maxValue(float[] dev) {
float max = -99999;
for (int i = 0; i < dev.length; i++) {
if (dev[i] > max && dev[i] < MISSING_VAL) {
max = dev[i];
}
}
return max;
}
public static float minValue(float[] dev) {
float min = 99999;
for (int i = 0; i < dev.length; i++) {
if (dev[i] < min && dev[i] > -MISSING_VAL) {
min = dev[i];
}
}
return min;
}
/*
* 10 element floating point array
*/
public static float[] geKLength() {
float[] kLength = new float[10];
kLength[0] = 0;
for (int i = 1; i < 10; i++) {
kLength[i] = (float) Math.exp( K_EXPONENT * Math.log(i));
if (kLength[i] > 1080)
kLength[i] = 1080;
}
return kLength;
}
// uri: /geomag/2013-05-20_00:00:00.0/HAD/101/GEOMAG
public static String getSourceFromUri(String uri) {
if (uri != null && uri.length() >= 37)
return uri.substring(34, 37);
else
return "";
}
public static String getStationFromUri(String uri) {
if (uri != null && uri.length() >= 37)
return uri.substring(30, 33);
else
return "";
}
public static Date getTimeFromUri(String uri) throws ParseException {
String format = "yyyy-MM-dd'_'HH:mm:ss.s";
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (uri != null && uri.length() >= 37) {
String time = uri.substring(8, 29);
Date date = sdf.parse(time);
return date;
}
else
return new Date();
}
// get the front part before the source in the uri
public static String separateSourceFrontUri(String uri) {
if (uri != null && uri.length() >= 37)
return uri.substring(0, 34);
else
return "";
}
public static float[] toFloatArray(List<Float> list) {
float[] ret = new float[list.size()];
int i = 0;
for (Float e : list)
ret[i++] = e.floatValue();
return ret;
}
public static int[] toIntArray(List<Integer> list) {
int[] ret = new int[list.size()];
int i = 0;
for (Integer e : list)
ret[i++] = e.intValue();
return ret;
}
public static boolean isLeapYear(int year) {
boolean isLeap;
if (year/400 ==0)
isLeap = true;
else if (year/100 == 0)
isLeap = false;
else if (year/4 == 0)
isLeap = true;
else
isLeap = false;
return isLeap;
}
// public static String getMonthDayFromNumber(int year, int number) { //CL22013041.min
// String temp = "";
// String month = "";
// String day = "";
// String monthDay = "";
// Boolean isLeapYear = isLeapYear( year);
// int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
// int[] leapDays = {31,29,31,30,31,30,31,31,30,31,30,31};
// Calendar cal = Calendar.getInstance();
// cal.get(Calendar.DAY_OF_MONTH);
// cal.get(Calendar.MONTH);
// cal.get(Calendar.DAY_OF_YEAR);
// cal.set(Calendar.DAY_OF_YEAR, number);
// int[] num =
// if (isLeapYear) {
//
// }
// else {
//
// }
// if (number<=31){ //JEJ, m130212.txt
// month = "01";
// day = String.valueOf(number);
// }
// else if (number > 31 && number <= 59){
// month = "02";
// day = String.valueOf(number-31);
// }
// else if (number > 31 && number <= 59){
// month = "03";
// day = String.valueOf(number-31);
// }
// else if (fileName.startsWith("ha")){ CNB,NGK, WNG
// temp = fileName.substring(3, 10);
// year = temp.substring(0, 4);
// }
//
// return monthDay;
// }
public static String getTimeFromFileName(String fileName) { //CL22013041.min
String time = "";
String temp = "";
String year = "";
String month = "";
String day = "";
String num = "";
Calendar cal = Calendar.getInstance();
if (fileName.startsWith("m")){ //JEJ, m130212.txt
temp = fileName.substring(1, 7);
year = "20"+temp.substring(4, 6);
month = temp.substring(2, 4);
day = temp.substring(0, 2);
}
else if (fileName.startsWith("ha")){
temp = fileName.substring(2, 9);
year = temp.substring(3, 7);
num = temp.substring(0, 3);
try {
cal.set(Calendar.DAY_OF_YEAR, Integer.parseInt(num));
} catch (NumberFormatException e) {
}
month = String.valueOf(cal.get(Calendar.MONTH));
day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
}
else if (fileName.startsWith("BOU") || fileName.startsWith("CL2") || fileName.startsWith("CMO")
|| fileName.startsWith("OTT") || fileName.startsWith("MEA")) {
temp = fileName.substring(3, 10);
year = temp.substring(0, 4);
num = temp.substring(4, 7);
try {
cal.set(Calendar.DAY_OF_YEAR, Integer.parseInt(num));
} catch (NumberFormatException e) {
}
month = String.valueOf(cal.get(Calendar.MONTH) +1);
day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
}
else if (fileName.startsWith("ha") || fileName.startsWith("CNB")
|| fileName.startsWith("OTT") || fileName.startsWith("WNG")) {
temp = fileName.substring(3, 10);
year = temp.substring(0, 4);
month = temp.substring(4, 6);
day = temp.substring(6, 8);
}
if (month.length() == 1)
month = "0"+ month;
if (day.length() == 1)
day = "0" + day;
time = year + "-" + month + "-" + day;
return time;
}
public static float getMedian(float[] array) {
float median = 0;
if (array.length <= 1)
return array[0];
float[] arraySort = array.clone();
Arrays.sort(arraySort);
// remove missing data
List<Float> newArray = new ArrayList<Float>();
for ( int k = 0; k < arraySort.length-1; k++ )
if (arraySort[k] != MISSING_VAL)
newArray.add(arraySort[k]);
else
break; //to sorted arraySort
int size = newArray.size();
if (size/2 == 0)
median = (newArray.get(size/2) + newArray.get(size/2 -1) ) / 2;
else
median = newArray.get((size-1)/2);
return median;
}
}

View file

@ -0,0 +1,58 @@
package gov.noaa.nws.ncep.edex.plugin.geomag.handler;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagAvg;
import gov.noaa.nws.ncep.common.dataplugin.geomag.dao.GeoMagAvgDao;
import gov.noaa.nws.ncep.common.dataplugin.geomag.request.RetrieveHrAvgRequest;
import java.util.List;
import java.util.logging.Logger;
import com.raytheon.uf.common.serialization.comm.IRequestHandler;
/**
*
* Handler for RetrieveHrAvgRequest. Retrieves the GeoMagAvg for the given
* datauri
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* 2014/02/12 #1110 qzhou Init
*
* </pre>
*
* @author qzhou
* @version 1.0
*/
public class RetrieveHrAvgRequestHandler implements
IRequestHandler<RetrieveHrAvgRequest> {
private static Logger logger = Logger
.getLogger(RetrieveHrAvgRequestHandler.class.toString());
private GeoMagAvgDao dao;
@Override
public Object handleRequest(RetrieveHrAvgRequest request) throws Exception {
List<GeoMagAvg> resultsList = null;
logger.info("RetrieveHrAvgRequest for " + request.getStationCode());
try {
dao = new GeoMagAvgDao(); // PluginFactory.getInstance().getPluginDao(GeoMag);
resultsList = dao.getAvgForStation(request.getStationCode(),
request.getStartTime(), request.getEndTime());
// logger.info("resultsList.size() " + request.getStationCode());
} catch (Exception e) {
logger.warning("Error retrieving hourly average record for "
+ request.getStationCode());
}
return resultsList;
}
}

View file

@ -0,0 +1,56 @@
package gov.noaa.nws.ncep.edex.plugin.geomag.handler;
import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagK1min;
import gov.noaa.nws.ncep.common.dataplugin.geomag.dao.GeoMagK1minDao;
import gov.noaa.nws.ncep.common.dataplugin.geomag.request.RetrieveK1minRequest;
import java.util.List;
import java.util.logging.Logger;
import com.raytheon.uf.common.serialization.comm.IRequestHandler;
/**
*
* Handler for RetrieveKiminRequest. Retrieves the GeoMagK1min for the given
* datauri
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* 2014/02/12 #1110 qzhou Init
*
* </pre>
*
* @author qzhou
* @version 1.0
*/
public class RetrieveK1minRequestHandler implements
IRequestHandler<RetrieveK1minRequest> {
private static Logger logger = Logger
.getLogger(RetrieveK1minRequestHandler.class.toString());
private GeoMagK1minDao dao;
@Override
public Object handleRequest(RetrieveK1minRequest request) throws Exception {
List<GeoMagK1min> resultsList = null;
logger.info("RetrieveK1minRequest for " + request.getStationCode());
try {
dao = new GeoMagK1minDao();
resultsList = dao.getRangeK1min(request.getStationCode(),
request.getStartTime(), request.getEndTime());
} catch (Exception e) {
logger.warning("Error retrieving K1min record for "
+ request.getStationCode());
}
return resultsList;
}
}

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<purgeRuleSet>
<defaultRule>
<period>30-00:00:00</period>
<period>60-00:00:00</period>
</defaultRule>
</purgeRuleSet>

View file

@ -30,6 +30,7 @@ import com.raytheon.uf.common.time.DataTime;
* default "NA". Add "Site" for Halpha.
* Jan 28, 2013 865 qzhou Changed float to double for intTime.
* Aug 30, 2013 2298 rjpeter Make getPluginName abstract
* Mar 18, 2014 qzhou Modified getObservationTime(BasicHDU).
* </pre>
*
* @author qzhou, sgurung
@ -98,7 +99,6 @@ public class SolarImageDecoder extends AbstractDecoder {
}
} catch (FitsException e) {
// TODO Auto-generated catch block. Please revise as appropriate.
logger.error(e);
// return new PluginDataObject[0];
}
@ -193,11 +193,15 @@ public class SolarImageDecoder extends AbstractDecoder {
}
}
if ((dateObs != null) && (dateObs.length() <= 10)) { // LASCO
// dateObs=Date
// +Time
// LASCO dateObs = Date + Time
if ((dateObs != null) && (dateObs.length() <= 10)) {
SimpleDateFormat sdfAlt = new SimpleDateFormat(DATE_TIME_FORMAT_ALT);
dateObs = dateObs + "T" + hdu.getTrimmedString("TIME-OBS");
String time = hdu.getTrimmedString("TIME-OBS"); // 12:00:00.000
if (time.length() == 8) // 12:00:00
time = time + ".000";
dateObs = dateObs + "T" + time;
return sdfAlt.parse(dateObs);
}

View file

@ -768,12 +768,12 @@
</solarImageMatches>
<imageStyle>
<range scale="LINEAR">
<minValue>400</minValue>
<maxValue>16000</maxValue>
<minValue>-300</minValue>
<maxValue>300</maxValue>
</range>
<defaultColormap>SolarImage/Gray</defaultColormap>
<colorbarLabeling>
<values>2000 6000 10000 14000</values>
<values>-200 -100 0 100 200</values>
</colorbarLabeling>
</imageStyle>
</styleRule>
@ -784,16 +784,15 @@
</solarImageMatches>
<imageStyle>
<range scale="LINEAR">
<minValue>400</minValue>
<maxValue>16000</maxValue>
<minValue>-600</minValue>
<maxValue>600</maxValue>
</range>
<defaultColormap>SolarImage/Gray</defaultColormap>
<colorbarLabeling>
<values>2000 6000 10000 14000</values>
<values>-400 -200 0 200 400</values>
</colorbarLabeling>
</imageStyle>
</styleRule>
<styleRule>
<solarImageMatches>
<instrument>SECCHI</instrument>

View file

@ -23,6 +23,7 @@ import nom.tam.util.BufferedDataInputStream;
* 01/07/2014 1046 qzhou Fixed imagefunction rotation problem. Added constructor for cylindrical.
* Fixed imageToWorld and worldToImage reverse problem.
* Added WorldToImageSamp method for sampling
* 03/19/2014 qzhou Fixed some Lasco image located at left bottom corner
* </pre>
*
* @author q.zhou
@ -95,11 +96,15 @@ public class WCSConverter {
for (int n = 0; n < dim; n++) {
String ij = Integer.toString(n + 1);
crpix[n] = header.getDoubleValue("CRPIX" + ij, 0.0); // if null, 0.0
crpix[n] = header.getDoubleValue("CRPIX" + ij, 0.0);
cdelt[n] = header.getDoubleValue("CDELT" + ij, 1.0);
crval[n] = header.getDoubleValue("CRVAL" + ij, 0.0);
crota[n] = header.getDoubleValue("CROTA" + ij, 0.0);
naxis[n] = header.getDoubleValue("NAXIS" + ij, 0.0);
if (Math.abs(naxis[n] - crpix[n]) / naxis[n] < 0.2)
crpix[n] = naxis[n] / 2;
}
for (int m = 0; m < dim; m++) {