From a53796c073c143edf80de31ee1561a63a4210cde Mon Sep 17 00:00:00 2001 From: Stephen Gilbert Date: Thu, 15 May 2014 15:43:44 -0400 Subject: [PATCH] VLab Issue #3722 - SWPC delivery for 14.3.1 Upgrades to Geomag and solar image processing Change-Id: Ibe33d00843419e34998274a87734f3f29d2b63dc Former-commit-id: 07586158c9677818d60c2a4b97f84c22fcd57fbf [formerly 7330213de581a390687bd16aafc206b2095b0fa2 [formerly 9b470a605b1db454b9287098e84079e8438a2f2b]] Former-commit-id: 7330213de581a390687bd16aafc206b2095b0fa2 Former-commit-id: a46e2f6acc185c6622b5339e0853be0fec477076 --- .../META-INF/MANIFEST.MF | 6 +- .../common/dataplugin/geomag/GeoMagAvg.java | 79 +- .../common/dataplugin/geomag/GeoMagK1min.java | 230 +- .../dataplugin/geomag/GeoMagPathProvider.java | 16 +- .../dataplugin/geomag/GeoMagRecord.java | 431 +--- .../geomag/calculation/CalcEach1min.java | 483 ++++ .../geomag/calculation/CalcEach3hr.java | 392 ++++ .../dataplugin/geomag/calculation/CalcKp.java | 382 ++++ .../geomag/calculation/CalcUtil.java | 550 +++++ .../dataplugin/geomag/dao/GeoMagAvgDao.java | 52 +- .../dataplugin/geomag/dao/GeoMagDao.java | 686 ++---- .../dataplugin/geomag/dao/GeoMagK1minDao.java | 96 +- .../dataplugin/geomag/dao/GeoMagK3hrDao.java | 49 +- .../geomag/request/DatabaseUtil.java | 314 +++ .../geomag/request/RetrieveHrAvgRequest.java | 72 + .../geomag/request/RetrieveK1minRequest.java | 72 + .../{geomag-request => geomag-request.xml} | 18 +- .../edex/plugin/geomag/GeoMagDecoder.java | 4 +- .../edex/plugin/geomag/TrigKCalculation.java | 1966 ++++++++--------- .../geomag/calculation/CalcEach1min.java | 454 ---- .../geomag/calculation/CalcEach3hr.java | 335 --- .../plugin/geomag/calculation/CalcKp.java | 443 ---- .../plugin/geomag/calculation/CalcUtil.java | 715 ------ .../handler/RetrieveHrAvgRequestHandler.java | 58 + .../handler/RetrieveK1minRequestHandler.java | 56 + .../base/purge/geomagPurgeRules.xml | 2 +- .../plugin/solarimage/SolarImageDecoder.java | 14 +- .../styleRules/solarImageryStyleRules.xml | 15 +- .../viz/rsc/solarimage/wcs/WCSConverter.java | 7 +- 29 files changed, 3840 insertions(+), 4157 deletions(-) create mode 100644 ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcEach1min.java create mode 100644 ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcEach3hr.java create mode 100644 ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcKp.java create mode 100644 ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcUtil.java create mode 100644 ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/DatabaseUtil.java create mode 100644 ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/RetrieveHrAvgRequest.java create mode 100644 ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/RetrieveK1minRequest.java rename ncep/gov.noaa.nws.ncep.edex.plugin.geomag/res/spring/{geomag-request => geomag-request.xml} (67%) delete mode 100644 ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcEach1min.java delete mode 100644 ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcEach3hr.java delete mode 100644 ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcKp.java delete mode 100644 ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcUtil.java create mode 100644 ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/handler/RetrieveHrAvgRequestHandler.java create mode 100644 ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/handler/RetrieveK1minRequestHandler.java diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/META-INF/MANIFEST.MF b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/META-INF/MANIFEST.MF index cf8ca1880d..734660e9a0 100644 --- a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/META-INF/MANIFEST.MF +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/META-INF/MANIFEST.MF @@ -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 - - diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagAvg.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagAvg.java index b30cb712ff..5a4c008e4e 100644 --- a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagAvg.java +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagAvg.java @@ -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,14 +21,17 @@ 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. + * Record implementation for geomag avg. * *
  * SOFTWARE HISTORY
  * Date         Ticket#    Engineer           Description
  * ------------ ---------- ----------------   --------------------------
  * 08/14/2013   T989       qzhou              Initial creation.
+ * 03/03/2014              qzhou              modified get/set
  * 
* * @author qzhou @@ -44,26 +46,26 @@ import com.raytheon.uf.common.serialization.annotations.DynamicSerializeElement; @DynamicSerialize public class GeoMagAvg extends PersistableDataObject { - - /** + /** * */ - private static final long serialVersionUID = 1L; - public static final String ID_GEN = "idgen"; - - /** The id */ + private static final long serialVersionUID = 1L; + + public static final String ID_GEN = "idgen"; + + /** The id */ @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ID_GEN) private Integer id; - - /** + + /** * station code */ @Column @XmlAttribute @DynamicSerializeElement private String stationCode; - + /** * time tag */ @@ -71,7 +73,7 @@ public class GeoMagAvg extends PersistableDataObject { @XmlAttribute @DynamicSerializeElement private Date avgTime; - + /** * insert time tag */ @@ -79,54 +81,53 @@ public class GeoMagAvg extends PersistableDataObject { @XmlAttribute @DynamicSerializeElement private Date insertTime; - + /** * H data Hour Average */ - @Column(length=16) - @XmlAttribute + @Column + @XmlAttribute @DynamicSerializeElement - private float hHrAvg; - - /** + private float hHrAvg; + + /** * D data Hour Average */ - @Column(length=16) - @XmlAttribute + @Column + @XmlAttribute @DynamicSerializeElement private float dHrAvg; - - - public GeoMagAvg() { - - } - - public void generateId() { + + public GeoMagAvg() { + + } + + public void generateId() { this.id = hashCode(); } - - /** + + /** * @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; } - + /** * @return The id */ @@ -138,7 +139,7 @@ public class GeoMagAvg extends PersistableDataObject { this.id = id; } - /** + /** * @return the timeTag */ public Date getAvgTime() { @@ -148,9 +149,9 @@ public class GeoMagAvg extends PersistableDataObject { public void setAvgTime(Date avgTime) { this.avgTime = avgTime; } - + /** - * @return the timeTag + * @return the insert time */ public Date getInsertTime() { return insertTime; @@ -159,7 +160,7 @@ public class GeoMagAvg extends PersistableDataObject { public void setInsertTime(Date insertTime) { this.insertTime = insertTime; } - + /** * @return the stationCode */ diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagK1min.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagK1min.java index 56882d023f..dcc15f244d 100644 --- a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagK1min.java +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagK1min.java @@ -22,13 +22,14 @@ import com.raytheon.uf.common.serialization.annotations.DynamicSerialize; import com.raytheon.uf.common.serialization.annotations.DynamicSerializeElement; /** - * Record implementation for geomag k 1 min. + * Record implementation for geomag k 1 min. * *
  * SOFTWARE HISTORY
  * Date         Ticket#    Engineer           Description
  * ------------ ---------- ----------------   --------------------------
  * 08/14/2013   T989       qzhou              Initial creation.
+ * 03/03/2014   #1110      qzhou              modified get/set
  * 
* * @author qzhou @@ -43,25 +44,26 @@ import com.raytheon.uf.common.serialization.annotations.DynamicSerializeElement; @DynamicSerialize public class GeoMagK1min extends PersistableDataObject { - /** + /** * */ - private static final long serialVersionUID = 1L; - public static final String ID_GEN = "idgen"; - - /** The id */ + private static final long serialVersionUID = 1L; + + public static final String ID_GEN = "idgen"; + + /** The id */ @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ID_GEN) private Integer id; - - /** + + /** * station code */ @Column @XmlAttribute @DynamicSerializeElement private String stationCode; - + /** * time tag */ @@ -69,7 +71,7 @@ public class GeoMagK1min extends PersistableDataObject { @XmlAttribute @DynamicSerializeElement private Date refTime; - + /** * insert time tag */ @@ -77,110 +79,108 @@ public class GeoMagK1min extends PersistableDataObject { @XmlAttribute @DynamicSerializeElement private Date lastUpdate; - + /** * H data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement - private int kestIndex; - - /** + private int kestIndex; + + /** * D data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement private float kestReal; - - /** + + /** * D data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement private float kestGamma; - + /** * H data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement - private int hkIndex; - - /** + private int hkIndex; + + /** * D data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement private float hkReal; - - /** + + /** * D data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement private float hkGamma; - + /** * H data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement - private int dkIndex; - - /** + private int dkIndex; + + /** * D data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement private float dkReal; - - /** + + /** * D data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement private float dkGamma; - - /** + + /** * D data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement private int hCount; - - /** + + /** * D data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement private int dCount; - - /** + + /** * D data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement private int aest; - - /** + + /** * D data Hour Average */ - @Column(length=16) + @Column(length = 16) @DynamicSerializeElement private float ks; - - - - public GeoMagK1min() { - - } - - public void generateId() { + + public GeoMagK1min() { + + } + + public void generateId() { this.id = hashCode(); } - - /** - * @return the hHrAvg + + /** + * @return the kestIndex */ public int getKestIndex() { return kestIndex; @@ -189,9 +189,9 @@ public class GeoMagK1min extends PersistableDataObject { public void setKestIndex(int kestIndex) { this.kestIndex = kestIndex; } - + /** - * @return the dHrAvg + * @return the kestReal */ public float getKestReal() { return kestReal; @@ -200,9 +200,9 @@ public class GeoMagK1min extends PersistableDataObject { public void setKestReal(float kestReal) { this.kestReal = kestReal; } - + /** - * @return the hHrAvg + * @return the kestGamma */ public float getKestGamma() { return kestGamma; @@ -211,95 +211,95 @@ public class GeoMagK1min extends PersistableDataObject { public void setKestGamma(float kestGamma) { this.kestGamma = kestGamma; } - - /** - * @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 - */ - public float getHKReal() { - return hkReal; - } - public void setHKReal(float hkReal) { - this.hkReal = hkReal; - } - /** - * @return the hHrAvg + * @return the dkIndex */ - 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; } - + /** * @return the dHrAvg */ @@ -310,7 +310,7 @@ public class GeoMagK1min extends PersistableDataObject { public void setAest(int aest) { this.aest = aest; } - + /** * @return the dHrAvg */ @@ -321,7 +321,7 @@ public class GeoMagK1min extends PersistableDataObject { public void setKs(float ks) { this.ks = ks; } - + /** * @return The id */ @@ -333,7 +333,7 @@ public class GeoMagK1min extends PersistableDataObject { this.id = id; } - /** + /** * @return the timeTag */ public Date getRefTime() { @@ -343,7 +343,7 @@ public class GeoMagK1min extends PersistableDataObject { public void setRefTime(Date refTime) { this.refTime = refTime; } - + /** * @return the timeTag */ @@ -354,7 +354,7 @@ public class GeoMagK1min extends PersistableDataObject { public void setLastUpdate(Date lastUpdate) { this.lastUpdate = lastUpdate; } - + /** * @return the stationCode */ diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagPathProvider.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagPathProvider.java index d43f9fe9e1..2f27261f3e 100644 --- a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagPathProvider.java +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagPathProvider.java @@ -6,7 +6,7 @@ import com.raytheon.uf.common.dataplugin.persist.DefaultPathProvider; import com.raytheon.uf.common.dataplugin.persist.IPersistable; public class GeoMagPathProvider extends DefaultPathProvider { - private static GeoMagPathProvider instance = new GeoMagPathProvider(); + private static GeoMagPathProvider instance = new GeoMagPathProvider(); public static GeoMagPathProvider getInstance() { return instance; @@ -40,15 +40,13 @@ public class GeoMagPathProvider extends DefaultPathProvider { StringBuffer sb = new StringBuffer(64); sb.append(pluginName); - if (pdo.getDataTime() != null ) { - 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"); + if (pdo.getDataTime() != null) { + Date time = (Date) pdo.getDataTime().getRefTime().clone(); + time.setHours(0); + + sb.append(fileNameFormat.get().format(time)); + sb.append(".h5"); } return sb.toString(); } } - diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagRecord.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagRecord.java index fdd7be0dee..f47f784663 100644 --- a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagRecord.java +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/GeoMagRecord.java @@ -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,8 +20,9 @@ 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. + * Record implementation for geomag plugin. * *
  * SOFTWARE HISTORY
@@ -51,49 +51,84 @@ 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 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";
-    
+
     /**
      * station code
      */
@@ -116,19 +151,11 @@ public class GeoMagRecord extends PersistablePluginDataObject {
      * report type
      */
     @DataURI(position = 3)
-    @Column 
+    @Column
     @DynamicSerializeElement
     @XmlAttribute
     private String reportType;
-    
-//    /**
-//     * index for data in hdf5, 0-1439
-//     */
-//    @Column 
-//    @DynamicSerializeElement
-//    @XmlAttribute
-//    private int idx;
-    
+
     /**
      * flag to indicate bad data point
      */
@@ -136,7 +163,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     @DynamicSerializeElement
     @XmlAttribute
     private int badDataPoint;
- 
+
     /**
      * H or X values
      */
@@ -144,7 +171,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     @DynamicSerializeElement
     @XmlAttribute
     private float component_1;
-    
+
     /**
      * D or Y values
      */
@@ -152,7 +179,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     @DynamicSerializeElement
     @XmlAttribute
     private float component_2;
-    
+
     /**
      * Z values
      */
@@ -160,7 +187,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     @DynamicSerializeElement
     @XmlAttribute
     private float component_3;
-    
+
     /**
      * F values
      */
@@ -168,7 +195,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     @DynamicSerializeElement
     @XmlAttribute
     private float component_4;
-    
+
     /**
      * F values
      */
@@ -192,108 +219,37 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     @DynamicSerializeElement
     @XmlAttribute
     private int cadenceId;
-    
+
     /**
      * Observation Date and Time for the minute values
      */
     @Transient
     private long[] obs_times;
-    
+
     /**
      * H or X values
      */
     @Transient
     private float[] comp1_data;
-    
+
     /**
      * D or Y values
      */
     @Transient
     private float[] comp2_data;
-    
+
     /**
      * Z values
      */
     @Transient
     private float[] comp3_data;
-    
+
     /**
      * F values
      */
     @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
      */
@@ -325,7 +281,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     public void setStationCode(String stationCode) {
         this.stationCode = stationCode;
     }
-   
+
     /**
      * @return the sourceId
      */
@@ -340,7 +296,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     public void setSourceId(int sourceId) {
         this.sourceId = sourceId;
     }
-    
+
     /**
      * @return the badDataPoint
      */
@@ -349,12 +305,13 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     }
 
     /**
-     * @param the badDataPoint to set
+     * @param the
+     *            badDataPoint to set
      */
     public void setBadDataPoint(int badDataPoint) {
         this.badDataPoint = badDataPoint;
     }
-    
+
     /**
      * @return the reportType
      */
@@ -376,10 +333,11 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     public long[] getObsTimes() {
         return obs_times;
     }
+
     public void setObsTimes(long[] obs_times) {
         this.obs_times = obs_times;
     }
-    
+
     /**
      * @return the comp1_data array
      */
@@ -390,7 +348,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     public void setComp1Data(float[] h_data) {
         this.comp1_data = h_data;
     }
-    
+
     /**
      * @return the comp2_data array
      */
@@ -401,7 +359,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     public void setComp2Data(float[] d_data) {
         this.comp2_data = d_data;
     }
-    
+
     /**
      * @return the comp3_data
      */
@@ -412,18 +370,18 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     public void setComp3Data(float[] z_data) {
         this.comp3_data = z_data;
     }
-    
+
     /**
      * @return the comp4_data
      */
     public float[] getComp4Data() {
         return comp4_data;
     }
-   
+
     public void setComp4Data(float[] f_data) {
         this.comp4_data = f_data;
     }
-    
+
     /**
      * @return the component_1
      */
@@ -434,7 +392,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     public void setComponent_1(float component_1) {
         this.component_1 = component_1;
     }
-    
+
     /**
      * @return the component_2
      */
@@ -445,7 +403,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     public void setComponent_2(float component_2) {
         this.component_2 = component_2;
     }
-    
+
     /**
      * @return the component_3
      */
@@ -456,7 +414,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     public void setComponent_3(float component_3) {
         this.component_3 = component_3;
     }
-    
+
     /**
      * @return the component_4
      */
@@ -481,7 +439,7 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     public void setDistributionId(int distributionId) {
         this.distributionId = distributionId;
     }
-    
+
     /**
      * @return the d_HrAvg
      */
@@ -499,257 +457,16 @@ public class GeoMagRecord extends PersistablePluginDataObject {
     public int getCadenceId() {
         return cadenceId;
     }
-    
+
     public void setCadenceId(int cadenceId) {
         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)
diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcEach1min.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcEach1min.java
new file mode 100644
index 0000000000..2e210d2473
--- /dev/null
+++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcEach1min.java
@@ -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.
+ * 
+ * 
+ * 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
+ * 
+ * + * @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 list = new ArrayList(); + + // 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; + } +} diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcEach3hr.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcEach3hr.java new file mode 100644 index 0000000000..577d01d2e7 --- /dev/null +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcEach3hr.java @@ -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. + * + *
+ * SOFTWARE HISTORY
+ *                   
+ * ate          Ticket#     Engineer   Description
+ * -----------  ----------  ---------- --------------------------
+ * 05/14/2013   #989        qzhou      Initial Creation
+ * 03/18/2014   #1123       qzhou      Add getHQdcOrDQdc
+ * 
+ * + * @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 list = (List) 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 dBSmall = new HashMap(); + + 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 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 mEntry = (Map.Entry) 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 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; + } + +} diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcKp.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcKp.java new file mode 100644 index 0000000000..ba9c846a35 --- /dev/null +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcKp.java @@ -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. + * + *
+ * SOFTWARE HISTORY
+ *                   
+ * ate          Ticket#     Engineer   Description
+ * -----------  ----------  ---------- --------------------------
+ * 05/14/2013   #989        qzhou      Initial Creation
+ * 03/18/2014   #1123       qzhou      default k to 99999
+ * 
+ * + * @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 getKsThreeList(String station) { + + ArrayList threeKsList = CalcUtil.getStationCoeff() + .getStationByCode(station).getKsThree();// size 24 + + return threeKsList; + } + + public static List getKsThree(Date time, String station, int k) { + List ks = new ArrayList(); + + // KsThree ksThree = null; + ArrayList 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 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 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 mEntry = (Map.Entry) 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 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 mEntry = (Map.Entry) 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 coeff = CalcUtil.getCoeffW(station[i]); + int j = 0; + + Iterator iter = coeff.entrySet().iterator(); + while (iter.hasNext()) { + wcoeff[i][j] = ((Map.Entry) 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 coeff = CalcUtil.getCoeffW(station[i]); + int j = 0; + Iterator iter = coeff.entrySet().iterator(); + while (iter.hasNext()) { + if (((Map.Entry) iter.next()).getKey() + .equalsIgnoreCase(fitTime)) { + wcoeff[i] = ((Map.Entry) 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; + } +} diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcUtil.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcUtil.java new file mode 100644 index 0000000000..d40efbd256 --- /dev/null +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/calculation/CalcUtil.java @@ -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. + * + *
+ * SOFTWARE HISTORY
+ *                   
+ * ate          Ticket#     Engineer   Description
+ * -----------  ----------  ---------- --------------------------
+ * 05/14/2013   #989        qzhou      Initial Creation
+ * 
+ * + * @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 getCoeffAandB(String station) { + Map abCoeff = new HashMap(); + + List 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 getCoeffW(String station) { + Map wCoeff = new HashMap(); + + List 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 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 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 newArray = new ArrayList(); + 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; + } +} diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagAvgDao.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagAvgDao.java index bfa572f0f0..cc410a9ab2 100644 --- a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagAvgDao.java +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagAvgDao.java @@ -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,19 +13,20 @@ 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. + * Record implementation for geomag avgDao. * *
  * SOFTWARE HISTORY
  * Date         Ticket#    Engineer           Description
  * ------------ ---------- ----------------   --------------------------
  * 08/14/2013   T989       qzhou              Initial creation.
+ * 03/13/2014              sgurung            Added method purgeDataByRefTime()
  * 
* * @author qzhou @@ -31,16 +34,16 @@ import com.raytheon.uf.edex.database.dao.DaoConfig; */ public class GeoMagAvgDao extends CoreDao { - /** + /** * Creates a new GribModelDao */ public GeoMagAvgDao() { super(DaoConfig.forClass(GeoMagAvg.class)); } - + /** * Retrieves a GeoMagAvgId based on the given id - * + * * @param id * The given ID number * @return The GeoMagAvgId @@ -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 getAvgForStation(final String stationCode, final Date start, final Date end) { + public List getAvgForStation(final String stationCode, + final Date start, final Date end) { return (List) txTemplate.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { @@ -64,19 +68,17 @@ 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 - crit.add(where2); + + Criterion where2 = Restrictions.between("avgTime", start, end); + crit.add(where2); return crit.list(); } }); } - + @SuppressWarnings("unchecked") - public List getSingleAvg(final String stationCode, final Date date) { + public List getSingleAvg(final String stationCode, + final Date date) { return (List) txTemplate.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { @@ -86,9 +88,17 @@ public class GeoMagAvgDao extends CoreDao { Criterion where1 = Restrictions.eq("stationCode", stationCode); crit.add(where1); Criterion where2 = Restrictions.eq("avgTime", date); - crit.add(where2); + crit.add(where2); return crit.list(); } }); } + + 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); + } } diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagDao.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagDao.java index 804356dacc..5de25009b1 100644 --- a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagDao.java +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagDao.java @@ -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,21 +17,18 @@ 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. * *
  * SOFTWARE HISTORY
  * 
- * Date         Ticket#    	Engineer    Description
- * ------------ ---------- 	----------- --------------------------
- * 04/2013		975			S. Gurung   Initial Creation
+ * Date         Ticket#     Engineer    Description
+ * ------------ ----------  ----------- --------------------------
+ * 04/2013      975         S. Gurung   Initial Creation
  * 07/16/2013   975         Q. Zhou     Added fields.
  * 
* @@ -34,116 +36,150 @@ import gov.noaa.nws.ncep.common.dataplugin.geomag.GeoMagRecord; * @version 1.0 */ 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(); + + /** 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); } -// @Override -// public StorageStatus persistToHDF5(PluginDataObject... records) -// throws PluginException { -// long t0 = System.currentTimeMillis(); -// -// // 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> containerMap = new HashMap>( -// records.length); -// Map fileMap = new HashMap(); -// -// for (PluginDataObject p : records) { -// if (p instanceof IPointData) { -// PointDataView pdv = ((IPointData) p).getPointDataView(); -// List views = containerMap -// .get(pdv.getContainer()); -// if (views == null) { -// views = new ArrayList(); -// containerMap.put(pdv.getContainer(), views); -// } -// views.add(pdv); -// File file = fileMap.get(pdv.getContainer()); -// if (file == null) { -// file = getFullFilePath(p); -// System.out.println("***file " +file.toString()); -// fileMap.put(pdv.getContainer(), file); -// } -// -// }DataTime -// } -// -// List ssList = new ArrayList(); -// try { -// for (PointDataContainer container : containerMap.keySet()) { -// IDataStore ds = DataStoreFactory.getDataStore(fileMap -// .get(container)); -// StorageProperties sp = new StorageProperties(); -// String compression = PluginRegistry.getInstance() -// .getRegisteredObject(pluginName).getCompression(); -// if (compression != null) { -// sp.setCompression(StorageProperties.Compression -// .valueOf(compression)); -// } -// -// Set params = container.getParameters(); -// for (String param : params) { -// try { -// IDataRecord idr = container.getParameterRecord(param); -// ds.addDataRecord(idr, sp); -// } catch (StorageException e) { -// throw new PluginException("Error adding record", e); -// } -// } -// -// try { -// StorageStatus ss = ds.store(StoreOp.APPEND); -// if (ss.getOperationPerformed() == StoreOp.APPEND) { -// // increment the indices -// List views = containerMap.get(container); -// int idx = (int) ss.getIndexOfAppend()[0]; -// container.incrementIds(idx, views); -// } -// ssList.add(ss); -// } catch (StorageException e) { -// throw new PluginException("Error updating point file", e); -// } -// } -// // Aggregate the storage status errors -// StorageStatus aggregatedStatus = new StorageStatus(); -// List se = new ArrayList(); -// for (StorageStatus ss : ssList) { -// StorageException[] seArr = ss.getExceptions(); -// if (seArr != null) { -// se.addAll(Arrays.asList(seArr)); -// } -// } -// -// aggregatedStatus.setExceptions(se.toArray(new StorageException[se -// .size()])); -// return aggregatedStatus; -// } -// -// finally { -// System.out.println("Time spent in persist: " -// + (System.currentTimeMillis() - t0)); -// } -// } -// -// public File getFullFilePath(PluginDataObject p) { -// File file; -// String directory = p.getPluginName() + File.separator -// + pathProvider.getHDFPath(p.getPluginName(), (IPersistable) p); -// file = new File(directory -// + File.separator -// + pathProvider.getHDFFileName(p.getPluginName(), -// (IPersistable) p)); -// return file; -// } - + /** + * Overridden to purge expired data from geomag_k1min, geomag_houravg and + * geomag_k3hr tables as well + */ + @Override + public int purgeDataByRefTime(Date refTime, + Map productKeys, boolean trackHdf5, + boolean trackToUri, Map> 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 { + // long t0 = System.currentTimeMillis(); + // + // // 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> containerMap = new + // HashMap>( + // records.length); + // Map fileMap = new HashMap(); + // + // for (PluginDataObject p : records) { + // if (p instanceof IPointData) { + // PointDataView pdv = ((IPointData) p).getPointDataView(); + // List views = containerMap + // .get(pdv.getContainer()); + // if (views == null) { + // views = new ArrayList(); + // containerMap.put(pdv.getContainer(), views); + // } + // views.add(pdv); + // File file = fileMap.get(pdv.getContainer()); + // if (file == null) { + // file = getFullFilePath(p); + // System.out.println("***file " +file.toString()); + // fileMap.put(pdv.getContainer(), file); + // } + // + // }DataTime + // } + // + // List ssList = new ArrayList(); + // try { + // for (PointDataContainer container : containerMap.keySet()) { + // IDataStore ds = DataStoreFactory.getDataStore(fileMap + // .get(container)); + // StorageProperties sp = new StorageProperties(); + // String compression = PluginRegistry.getInstance() + // .getRegisteredObject(pluginName).getCompression(); + // if (compression != null) { + // sp.setCompression(StorageProperties.Compression + // .valueOf(compression)); + // } + // + // Set params = container.getParameters(); + // for (String param : params) { + // try { + // IDataRecord idr = container.getParameterRecord(param); + // ds.addDataRecord(idr, sp); + // } catch (StorageException e) { + // throw new PluginException("Error adding record", e); + // } + // } + // + // try { + // StorageStatus ss = ds.store(StoreOp.APPEND); + // if (ss.getOperationPerformed() == StoreOp.APPEND) { + // // increment the indices + // List views = containerMap.get(container); + // int idx = (int) ss.getIndexOfAppend()[0]; + // container.incrementIds(idx, views); + // } + // ssList.add(ss); + // } catch (StorageException e) { + // throw new PluginException("Error updating point file", e); + // } + // } + // // Aggregate the storage status errors + // StorageStatus aggregatedStatus = new StorageStatus(); + // List se = new ArrayList(); + // for (StorageStatus ss : ssList) { + // StorageException[] seArr = ss.getExceptions(); + // if (seArr != null) { + // se.addAll(Arrays.asList(seArr)); + // } + // } + // + // aggregatedStatus.setExceptions(se.toArray(new StorageException[se + // .size()])); + // return aggregatedStatus; + // } + // + // finally { + // System.out.println("Time spent in persist: " + // + (System.currentTimeMillis() - t0)); + // } + // } + // + // public File getFullFilePath(PluginDataObject p) { + // File file; + // String directory = p.getPluginName() + File.separator + // + pathProvider.getHDFPath(p.getPluginName(), (IPersistable) p); + // file = new File(directory + // + File.separator + // + pathProvider.getHDFFileName(p.getPluginName(), + // (IPersistable) p)); + // return file; + // } + /** * Retrieves an geomag report using the datauri . * @@ -152,7 +188,7 @@ public class GeoMagDao extends PluginDao { * @return The report record if it exists. */ public GeoMagRecord queryByDataURI(String dataURI) { - GeoMagRecord report = null; + GeoMagRecord report = null; List obs = null; try { obs = queryBySingleCriteria("dataURI", dataURI); @@ -184,406 +220,82 @@ 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, - new long[] {magRecord.getComp1Data().length}); - storageRecord.setCorrelationObject(magRecord); - dataStore.addDataRecord(storageRecord); - //StorageStatus ss = dataStore.store(StoreOp.APPEND); + if (magRecord.getComp1Data() != null) { + AbstractStorageRecord storageRecord = new FloatDataRecord( + GeoMagRecord.component1, magRecord.getDataURI(), + (float[]) magRecord.getComp1Data(), 1, + new long[] { magRecord.getComp1Data().length }); + storageRecord.setCorrelationObject(magRecord); + dataStore.addDataRecord(storageRecord); + // StorageStatus ss = dataStore.store(StoreOp.APPEND); } - + /* * Write component2 data to HDF5. */ - if ( magRecord.getComp2Data() != null ) { - AbstractStorageRecord storageRecord = new FloatDataRecord(GeoMagRecord.component2, - magRecord.getDataURI(), (float[]) magRecord.getComp2Data(), 1, - new long[] {magRecord.getComp2Data().length}); - storageRecord.setCorrelationObject(magRecord); - dataStore.addDataRecord(storageRecord); - + if (magRecord.getComp2Data() != null) { + 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; } - + /* - * Get GeoMagAvg from ID + * Get GeoMagAvg from ID */ public GeoMagAvg getGeoMagAvg(Date avgTime) { - return (GeoMagAvg) avgDao.queryById(avgTime); - } - -// public Integer getGeoMagSourceId(String sourceName) throws DataAccessLayerException { -// return avgDao.getSourceId(sourceName); -// } - - public GeoMagAvgDao getGeoMagAvgDao() { - return avgDao; + return (GeoMagAvg) avgDao.queryById(avgTime); + } + + public GeoMagAvgDao getGeoMagAvgDao() { + return avgDao; + } + + public void setGeoMagAvgDao(GeoMagAvgDao avgDao) { + this.avgDao = avgDao; } - public void setGeoMagAvgDao( - GeoMagAvgDao avgDao) { - this.avgDao = avgDao; - } - /* - * Get GeoMagK1min from ID + * Get GeoMagK1min from ID */ public GeoMagK1min getGeoMagDateK1min(int id) { - return k1minDao.queryById(id); + return (GeoMagK1min) k1minDao.queryById(id); } - + public GeoMagK1minDao getGeoMagK1minDao() { - return k1minDao; + return k1minDao; } - - public void setGeoMagK1minDao( - GeoMagK1minDao k1minDao) { - this.k1minDao = k1minDao; + + public void setGeoMagK1minDao(GeoMagK1minDao k1minDao) { + this.k1minDao = k1minDao; } /* - * Get GeoMagK1min from ID + * Get GeoMagK1min from ID */ public GeoMagK3hr getGeoMagDateK3hr(int id) { - return k3hrDao.queryById(id); - } - - public GeoMagK3hrDao getGeoMagK3hrDao() { - return k3hrDao; + return (GeoMagK3hr) k3hrDao.queryById(id); } - public void setGeoMagK3hrDao( - GeoMagK3hrDao k3hrDao) { - this.k3hrDao = k3hrDao; - } + public GeoMagK3hrDao getGeoMagK3hrDao() { + return k3hrDao; + } + + public void setGeoMagK3hrDao(GeoMagK3hrDao k3hrDao) { + this.k3hrDao = k3hrDao; + } } \ No newline at end of file diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagK1minDao.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagK1minDao.java index f4422cc57e..454c24d1c2 100644 --- a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagK1minDao.java +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagK1minDao.java @@ -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,19 +13,21 @@ 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. + * Record implementation for geomag k1minDao. * *
  * SOFTWARE HISTORY
  * 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()
  * 
* * @author qzhou @@ -34,43 +35,68 @@ 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 - * - * @param id - * The given ID number - * @return The GeoMagAvgId - */ - public GeoMagK1min queryById(int id) { - return (GeoMagK1min) super.queryById(id); - } - public int getAreaId (int id){ - return queryById(id).getId(); + /** + * Retrieves data from postGres + * + * @return Criteria list + */ + + @SuppressWarnings("unchecked") + public List getRangeK1min(final String stationCode, + final Date start, final Date end) { + return (List) 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.ge("refTime", start); + crit.add(where2); + Criterion where3 = Restrictions.lt("refTime", end); + crit.add(where3); + + return crit.list(); + } + }); } @SuppressWarnings("unchecked") - public List getSingleK1min(final String stationCode, final Date date) { - - return (List) 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); - return crit.list(); - } - }); + public List getSingleK1min(final String stationCode, + final Date date) { + + return (List) 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); + return crit.list(); + } + }); + } + + public int purgeDataByRefTime(Date refTime) throws DataAccessLayerException { + DatabaseQuery deleteStmt = new DatabaseQuery(this.daoClass); + deleteStmt.addQueryParam("refTime", refTime); + return this.deleteByCriteria(deleteStmt); } } diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagK3hrDao.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagK3hrDao.java index 5a01fad534..7fb3100db0 100644 --- a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagK3hrDao.java +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/dao/GeoMagK3hrDao.java @@ -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,19 +13,20 @@ 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. + * Record implementation for geomag k3hrDao. * *
  * SOFTWARE HISTORY
  * Date         Ticket#    Engineer           Description
  * ------------ ---------- ----------------   --------------------------
  * 08/14/2013   T989       qzhou              Initial creation.
+ * 03/13/2014              sgurung            Added method purgeDataByRefTime()
  * 
* * @author qzhou @@ -32,30 +34,21 @@ import com.raytheon.uf.edex.database.dao.DaoConfig; */ public class GeoMagK3hrDao extends CoreDao { - /** + /** * Creates a new GribModelDao */ public GeoMagK3hrDao() { super(DaoConfig.forClass(GeoMagK3hr.class)); } - - /** - * Retrieves a GeoMagAvgId based on the given id - * - * @param id - * The given ID number - * @return The GeoMagAvgId - */ - public GeoMagK3hr queryById(int id) { - return (GeoMagK3hr) super.queryById(id); - } - public int getId (int id){ - return queryById(id).getId(); - } - + /** + * Retrieves data from postGres + * + * @return Criteria list + */ @SuppressWarnings("unchecked") - public List getRangeK3hr(final String stationCode, final Date start, final Date end) { + public List getRangeK3hr(final String stationCode, + final Date start, final Date end) { return (List) txTemplate.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { @@ -64,10 +57,10 @@ public class GeoMagK3hrDao extends CoreDao { Criteria crit = sess.createCriteria(GeoMagK3hr.class); Criterion where1 = Restrictions.eq("stationCode", stationCode); crit.add(where1); - Criterion where2 = Restrictions.gt("refTime", start); + Criterion where2 = Restrictions.gt("refTime", start); crit.add(where2); - Criterion where3 = Restrictions.lt("refTime", end); - crit.add(where3); + Criterion where3 = Restrictions.lt("refTime", end); + crit.add(where3); return crit.list(); } @@ -75,7 +68,8 @@ public class GeoMagK3hrDao extends CoreDao { } @SuppressWarnings("unchecked") - public List getSingleK3hr(final String stationCode, final Date time) { + public List getSingleK3hr(final String stationCode, + final Date time) { return (List) 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); + } +} diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/DatabaseUtil.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/DatabaseUtil.java new file mode 100644 index 0000000000..5aa54ab389 --- /dev/null +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/DatabaseUtil.java @@ -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 + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * 2014/02/12   #1123       qzhou       Moved from edex to here
+ * 
+ * 
+ * + * @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 retrieveSingleAvg(String dataUri, Date time) { + GeoMagAvgDao avgDao = new GeoMagAvgDao(); + String station = CalcUtil.getStationFromUri(dataUri); + + List resultsList = null; + resultsList = avgDao.getSingleAvg(station, time); + + return resultsList; + + } + + /* + * from geomag_houravg + */ + public static List 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 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 retrieveSingleK1min(String dataUri, + Date time) { + GeoMagK1minDao k1minDao = new GeoMagK1minDao(); + String station = CalcUtil.getStationFromUri(dataUri); + + List resultsList = null; + resultsList = k1minDao.getSingleK1min(station, time); + + return resultsList; + + } + + /* + * from geomag_k3hr + */ + public static List 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 resultsList = null; + resultsList = k3hrDao.getRangeK3hr(station, cal.getTime(), epTime); // 1 + + return resultsList; + } + + /* + * from geomag_k3hr + */ + public static List retrieveSingleK3hr(String dataUri, + Date epTime) { + GeoMagK3hrDao k3hrDao = new GeoMagK3hrDao(); + String station = CalcUtil.getStationFromUri(dataUri); + + List 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() { + + 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 dataList, + List dateListFinal, List hHrAvgListFinal, + List dHrAvgListFinal, Date spTime) { + List dateList = new ArrayList(); + List hHrAvgList = new ArrayList(); + List dHrAvgList = new ArrayList(); + + 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); + } + } + + } + +} diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/RetrieveHrAvgRequest.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/RetrieveHrAvgRequest.java new file mode 100644 index 0000000000..0798ae07a9 --- /dev/null +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/RetrieveHrAvgRequest.java @@ -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 + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * 2014/02/12   #1110      qzhou       Init
+ * 
+ * 
+ * + * @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; + } + +} diff --git a/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/RetrieveK1minRequest.java b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/RetrieveK1minRequest.java new file mode 100644 index 0000000000..c71100793f --- /dev/null +++ b/ncep/gov.noaa.nws.ncep.common.dataplugin.geomag/src/gov/noaa/nws/ncep/common/dataplugin/geomag/request/RetrieveK1minRequest.java @@ -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 + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * 2014/02/12   #1110      qzhou       Init
+ * 
+ * 
+ * + * @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; + } + +} diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/res/spring/geomag-request b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/res/spring/geomag-request.xml similarity index 67% rename from ncep/gov.noaa.nws.ncep.edex.plugin.geomag/res/spring/geomag-request rename to ncep/gov.noaa.nws.ncep.edex.plugin.geomag/res/spring/geomag-request.xml index 119e8dbace..1f16936fd1 100644 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/res/spring/geomag-request +++ b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/res/spring/geomag-request.xml @@ -38,4 +38,20 @@ - \ No newline at end of file + + + + + + + + + + + + + diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/GeoMagDecoder.java b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/GeoMagDecoder.java index 8920baa079..0d99ea9916 100644 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/GeoMagDecoder.java +++ b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/GeoMagDecoder.java @@ -552,8 +552,8 @@ public class GeoMagDecoder extends AbstractDecoder { } try { - // obs_time, compx...//size 7 - dataRec = dataStore.retrieve(newUri); + // obs_time, compx...//size 7 + dataRec = dataStore.retrieve(newUri); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (StorageException e1) { diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/TrigKCalculation.java b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/TrigKCalculation.java index 2d802aaae4..908231edd9 100644 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/TrigKCalculation.java +++ b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/TrigKCalculation.java @@ -4,14 +4,15 @@ 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.CalcEach1min; +import gov.noaa.nws.ncep.common.dataplugin.geomag.calculation.CalcEach3hr; +import gov.noaa.nws.ncep.common.dataplugin.geomag.calculation.CalcKp; +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 gov.noaa.nws.ncep.edex.plugin.geomag.calculation.CalcEach1min; -import gov.noaa.nws.ncep.edex.plugin.geomag.calculation.CalcEach3hr; -import gov.noaa.nws.ncep.edex.plugin.geomag.calculation.CalcKp; -import gov.noaa.nws.ncep.edex.plugin.geomag.calculation.CalcUtil; +import gov.noaa.nws.ncep.common.dataplugin.geomag.request.DatabaseUtil; import java.io.FileNotFoundException; import java.text.ParseException; @@ -19,25 +20,20 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; -import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import com.raytheon.uf.common.dataplugin.message.DataURINotificationMessage; import com.raytheon.uf.common.dataplugin.PluginException; +import com.raytheon.uf.common.dataplugin.message.DataURINotificationMessage; import com.raytheon.uf.common.dataplugin.persist.IPersistable; -import com.raytheon.uf.common.dataquery.db.QueryParam; import com.raytheon.uf.common.datastorage.IDataStore; import com.raytheon.uf.common.datastorage.StorageException; import com.raytheon.uf.common.datastorage.records.IDataRecord; -import com.raytheon.uf.edex.database.DataAccessLayerException; import com.raytheon.uf.edex.database.plugin.PluginFactory; -import com.raytheon.uf.edex.database.query.DatabaseQuery; + +//import gov.noaa.nws.ncep.edex.plugin.geomag.request.DatabaseUtil; /** * This java class calculates magnetometer k index and related values. @@ -48,6 +44,7 @@ import com.raytheon.uf.edex.database.query.DatabaseQuery; * date Ticket# Engineer Description * ----------- ---------- ----------- -------------------------- * 06/07/2013 #989 qzhou Initial Creation, event driven + * 03/18/2014 #1123 qzhou Move some functions to common. Modified FillAvgTimeGap in the moved functions *
* * @author qzhou @@ -55,1120 +52,887 @@ import com.raytheon.uf.edex.database.query.DatabaseQuery; * */ public class TrigKCalculation { - private final Log logger = LogFactory.getLog(getClass()); - private static final String GeoMag = "geomag"; - private static final float MISSING_VAL = 99999.99f; - private static final int MISSING_INT = 99999; - - private static final int HOURS = 24; - private static final int MINUTES = 60; - private static final int AVG_DATA_RANGE = 30; - private static final int HD_DATA_RANGE = 3; - private static final int MAX_SOURCES = 3; - - private static final int ITERATIONS = 5; - private GeoMagDao dao; //PluginDao dao; - private float[] defLength = new float[HOURS]; - - String format = "yyyy-MM-dd'_'HH:mm:ss.s"; - SimpleDateFormat sdf = new SimpleDateFormat(format); - - - public TrigKCalculation() { - } - - /* - * trigger - */ - public void trig1min(Object obj) throws StorageException { - - if( !(obj instanceof DataURINotificationMessage) ){ - //logger.info("Received msg that is not a DataURINotificationMessage."); - return; - } - - DataURINotificationMessage uriMsg = (DataURINotificationMessage)obj; - String[] dataUris = uriMsg.getDataURIs(); - -// logger.info("******** Start meganetometer calculation trig."); -// long t0 = Calendar.getInstance().getTimeInMillis(); -// System.out.println("*****T0 "+ t0); - - // get geomag uri - List geomagUri = new ArrayList(); - - for (String dataURI : dataUris ) { - if (dataURI.contains("geomag")) - geomagUri.add(dataURI); - } - - String[] dataURIs = geomagUri.toArray(new String[geomagUri.size()]); - - //sort - Arrays.sort(dataURIs); - - try { - dao = (GeoMagDao) PluginFactory.getInstance().getPluginDao(GeoMag); - } catch (PluginException e) { - e.printStackTrace(); - } - - calcSimpleHourAvg(dataURIs); - calcK(dataURIs); - -// long t4 = Calendar.getInstance().getTimeInMillis(); -// System.out.println("*****T4 "+ t4); - } - - - /* - * from geomag - */ - public List retrieveUriForAvg(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 List retrieveSingleAvg(String dataUri, Date time) { - GeoMagAvgDao avgDao = new GeoMagAvgDao(); - String station = CalcUtil.getStationFromUri(dataUri); - - List resultsList = null; - resultsList = avgDao.getSingleAvg(station, time); - - return resultsList; - - } - - /* - * from geomag_houravg - */ - public List 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 resultsList = null; - resultsList = avgDao.getAvgForStation(station, cal.getTime(), spTime); //720 - - return resultsList; - } - - /* - * from geomag - */ - public List retrieveUriForK1min(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 List retrieveSingleK1min(String dataUri, Date time) { - GeoMagK1minDao k1minDao = new GeoMagK1minDao(); - String station = CalcUtil.getStationFromUri(dataUri); + private static final String GeoMag = "geomag"; - List resultsList = null; - resultsList = k1minDao.getSingleK1min(station, time); - - return resultsList; - - } - - /* - * from geomag_k3hr - */ - public List retrieveUriForK3hr(String dataUri, Date epTime){ - GeoMagK3hrDao k3hrDao = new GeoMagK3hrDao(); - String station = CalcUtil.getStationFromUri(dataUri); + private static final float MISSING_VAL = 99999.99f; - Calendar cal = Calendar.getInstance(); - cal.setTime(epTime); - cal.add(Calendar.DAY_OF_YEAR, -1); - - List resultsList = null; - resultsList = k3hrDao.getRangeK3hr(station, cal.getTime(), epTime); //1 - - return resultsList; - } - - /* - * from geomag_k3hr - */ - public List retrieveSingleK3hr(String dataUri, Date epTime){ - GeoMagK3hrDao k3hrDao = new GeoMagK3hrDao(); - String station = CalcUtil.getStationFromUri(dataUri); + private static final int HOURS = 24; - List resultsList = null; - resultsList = k3hrDao.getSingleK3hr(station, epTime); - - return resultsList; - } - - /* - * For hdf5 - */ - public IDataRecord[] getDataRecords(String uri){ - IDataRecord[] dataRec = null; - IDataStore dataStore = null; - - GeoMagRecord record = new GeoMagRecord(uri); - if (record != null) - dataStore = dao.getDataStore((IPersistable) record); - - try { - dataRec = dataStore.retrieve(uri); //obs_time, compx...//size 7 - } catch (FileNotFoundException e1) { - //e1.printStackTrace(); - System.out.println("This uri didn't find the records."); - } catch (StorageException e1) { - System.out.println("This uri didn't find place to store the records."); - //e1.printStackTrace(); - } - - return dataRec; - } - - /* - * sort n lists - */ - @SuppressWarnings({ "unchecked", "rawtypes" }) - static void sort(List... lists) { - assert lists.length > 0; - - Object[][] objects = new Object[lists[0].size()][lists.length]; + private static final int MINUTES = 60; - for (int i = 0; i < lists.length; i++) { - int j = 0; - for (Object object : lists[i]) { - objects[j++][i] = object; - } - } + private static final int HD_DATA_RANGE = 3; - Arrays.sort(objects, new Comparator() { - - public int compare(Object[] o1, Object[] o2) { - return ((Comparable)o1[0]).compareTo(o2[0]); - } - }); + private static final int ITERATIONS = 5; - for (int i = 0; i < lists.length; i++) { - lists[i].clear(); - for (Object[] tuple : objects) { - lists[i].add(tuple[i]); - } - } - } - - /* - * Input data of all source, output with higher priority source data - */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public List getBestObserv(List dataList ) { - - List comp1List = new ArrayList(); - List comp2List = new ArrayList(); - List badPointList = new ArrayList(); - List dateList = new ArrayList(); - List sourceList = new ArrayList(); - - List bestList = new ArrayList(); - - if (dataList != null ) { - for (int i = 0; i < dataList.size(); i++) { - - Object[] row = (Object[]) dataList.get(i); - - comp1List.add( (Float) row[0]); - comp2List.add( (Float) row[1]); - dateList.add( (Date) row[2]); - badPointList.add( (Integer) row[3]); - sourceList.add( (Integer) row[4]); - - } - - sort(dateList, sourceList, comp1List, comp2List, badPointList); - - int count = 0; - int size = dateList.size(); - - /* - * tempList combine all lists for the first 4 items. size=4 - * newList holds tempLists ordered by source. size=3 - * bestList construct newList with best source - * bestListFull filled time gaps - */ - for (int i = 0; i < size; i=i+count) { - count = 0; - - List tempList1 = new ArrayList(); - List tempList2 = new ArrayList(); - List tempList3 = new ArrayList(); - - List newList = new ArrayList(); - //init 3 - newList.add(0, new ArrayList()); - newList.add(1, new ArrayList()); - newList.add(2, new ArrayList()); - - tempList1.add( dateList.get(i)); - if (badPointList.get(i) != null && badPointList.get(i) != 0){ - tempList1.add(MISSING_VAL); - tempList1.add(MISSING_VAL); - } - else { - tempList1.add(comp1List.get(i)); - tempList1.add(comp2List.get(i)); - } - newList.set(sourceList.get(i)%100 -1, tempList1); - count++; - - if (i+1 < size && dateList.get(i).compareTo( dateList.get(i+1)) ==0) { - - tempList2.add(dateList.get(i+1)); - if (badPointList.get(i+1) != null && badPointList.get(i+1) != 0){ - tempList2.add(MISSING_VAL); - tempList2.add(MISSING_VAL); - } - else { - tempList2.add(comp1List.get(i+1)); - tempList2.add(comp2List.get(i+1)); - } - newList.set(sourceList.get(i+1)%100 -1, tempList2); - count++; - } - - if (i+2 < size && dateList.get(i).compareTo( dateList.get(i+2)) ==0) { - - tempList3.add(dateList.get(i+2)); - if (badPointList.get(i+2) != null && badPointList.get(i+2) != 0){ - tempList3.add(MISSING_VAL); - tempList3.add(MISSING_VAL); - } - else { - tempList3.add(comp1List.get(i+2)); - tempList3.add(comp2List.get(i+2)); - } - newList.set(sourceList.get(i+2)%100 -1, tempList3); - count++; - } - - if (newList.get(2) == null || newList.get(2).isEmpty()) //newList.get(0)= [3281750, 2013-05-06 00:00:00.0, 20829.85, -297.05] - newList.remove(2); - if (newList.get(1) == null || newList.get(1).isEmpty()) - newList.remove(1); - if (newList.get(0) == null || newList.get(0).isEmpty()) - newList.remove(0); - //System.out.println("***newList "+i+" "+count+" "+newList.size()+" "+newList.get(0)); - - // Now only check if comp2 (...get(2)) is MISSING_VAL. Could check both - if (newList.get(0).get(2) != null && (Float) newList.get(0).get(2) != MISSING_VAL ) { - bestList.add( newList.get(0)); - } - else if (newList.size() >1 && (Float) newList.get(0).get(2) == MISSING_VAL && i+1< size) { - // if date i = date(i+1) && comp1 (i+1) != missing - if ((Date) newList.get(0).get(1) == (Date) newList.get(1).get(1) && newList.get(1).get(2) != null && (Float) newList.get(1).get(2) != MISSING_VAL) { - bestList.add( newList.get(1)); - } - else if (newList.size() >2 && (Float) newList.get(1).get(2) == MISSING_VAL && i+2 < size) { - if ((Date) newList.get(0).get(1) == (Date) newList.get(2).get(1) && (Float) newList.get(2).get(2) != MISSING_VAL) { - bestList.add( newList.get(2)); - } - else { - bestList.add( newList.get(0)); - } - } - } - } - } - - return bestList; - } + private GeoMagDao dao; // PluginDao dao; - /* - * fill time tag gaps, return fullBestList - */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public List fillHDTimeGaps(List bestList) { - List fullBestList= new ArrayList(); - - // fill missing in the beginning - Date date = (Date) bestList.get(0).get(0); //bestList.get(i) eq. newList. - int min0 = date.getMinutes(); - - if ( min0 != 0 ) { - for (int k = 0; k < min0; k++) { - List newList2 = new ArrayList(); // eq. newList - - Date dateNew = (Date)date.clone(); - dateNew.setMinutes(k); + private float[] defLength = new float[HOURS]; - newList2.add(dateNew); - newList2.add(MISSING_VAL); - newList2.add(MISSING_VAL); - fullBestList.add( newList2); - - } - } - - // fill missing in the middle - for (int j = 0; j < bestList.size(); j++ ) { //i=0 first non missing data - - Date date0 = (Date) bestList.get(j).get(0);//dateList.get(i); - fullBestList.add( bestList.get(j)); - - if (j+1 < bestList.size()) { - Date date1 = (Date) bestList.get(j+1).get(0);//dateList.get(i+1); - int diffMin = (int)(date1.getTime() - date0.getTime())/ (60*1000); - - if (diffMin != 1) { - for (int k = 0; k < diffMin-1; k++) { - List newList2 = new ArrayList(); // eq. newList - - newList2.add(new Date(date0.getTime() + 60*1000*(k+1))); - newList2.add(MISSING_VAL); - newList2.add(MISSING_VAL); - fullBestList.add( newList2); - - } - } - } - } - -// // fill missing in the end -// int latest = fullBestList.size(); -// if (latest < HOURS*MINUTES*HD_DATA_RANGE) { -// for (int k = latest; k < HOURS*MINUTES*HD_DATA_RANGE; k++) { -// List newList2 = new ArrayList(); -// Date d = (Date)fullBestList.get(0).get(latest-1); -// -// newList2.add(new Date(d.getTime() + 60*1000*(k+1))); -// newList2.add(MISSING_VAL); -// newList2.add(MISSING_VAL); -// fullBestList.add( newList2); -// } -// } - - return fullBestList; - } - - - /* - * when uri time is 59 min past the hour, calculate the averages and write to geomat_houravg - */ - public void calcSimpleHourAvg(String[] dataURIs) throws StorageException { - - if (dao != null && dataURIs != null) { - for (String dataURI : dataURIs ) { - String stationCode = CalcUtil.getStationFromUri(dataURI); - - Date time = null;; - try { - time = CalcUtil.getTimeFromUri(dataURI); - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - int min = time.getMinutes(); - - List dataList = null; - if (min == 59) - dataList = retrieveUriForAvg(dataURI, time); - else - continue; - - - if (dataList != null && dataList.size() != 0) { - List bestList = getBestObserv( dataList ); - - float[] hrAvg = CalcEach3hr.getSimpleHourAvg(bestList); - - GeoMagAvg recAvg = new GeoMagAvg(); + String format = "yyyy-MM-dd'_'HH:mm:ss.s"; + + SimpleDateFormat sdf = new SimpleDateFormat(format); + + public TrigKCalculation() { - // look the avg table to see if the avg already exists - time.setMinutes(30); - List avgList = retrieveSingleAvg(dataURI, time); - - if (avgList != null && avgList.size() != 0) {//String newUri = dataURI.substring(0, 21) +":30:00.0"+ dataURI.substring(29, 34)+ "100/GEOMAG"; - for (int i = 0; i < avgList.size(); i++) { //1 - GeoMagAvg row = avgList.get(i); - List idList = new ArrayList(); - idList.add( (Integer) row.getId()); - recAvg.setId((int) idList.get(0)); - - } - } - - recAvg.setAvgTime(time); - recAvg.setInsertTime(Calendar.getInstance().getTime()); - recAvg.setStationCode(stationCode); - recAvg.setHHrAvg(hrAvg[0]); - recAvg.setDHrAvg(hrAvg[1]); - - GeoMagAvgDao avgDao = new GeoMagAvgDao(); - avgDao.persist(recAvg); - - } - } - } - } - - /* - * Write to geomag_k1min - */ - public void calcK(String[] dataURIs) { - - if (dataURIs != null ) { - for (String dataURI : dataURIs ) { - - String stationCode = CalcUtil.getStationFromUri(dataURI); - String source = CalcUtil.getSourceFromUri(dataURI); - - Date timeBy3 = null;; - try { - timeBy3 = CalcUtil.getTimeFromUri(dataURI); - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - int hour = timeBy3.getHours(); - int min = timeBy3.getMinutes(); - - /* - * Read average - */ - Date spTime = CalcUtil.getSPTime( timeBy3); - - List dataList = null; + /* + * trigger + */ + public void trig1min(Object obj) throws StorageException { - dataList = retrieveUriBy3hr(dataURI, CalcUtil.getSPTime(timeBy3)); + if (!(obj instanceof DataURINotificationMessage)) { - // dataList size (avg) < 24, can't calculate dB[j] - if (dataList.size() <= HOURS) - continue; + return; + } - - List dateList = new ArrayList(); - List hHrAvgList = new ArrayList(); - List dHrAvgList = new ArrayList(); - - if (dataList != null && dataList.size() >= 5) { - 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()); - - } - - sort(dateList, hHrAvgList, dHrAvgList); - - /* - * fill missing - */ - List dateListFinal = new ArrayList(); - List hHrAvgListFinal = new ArrayList(); - List dHrAvgListFinal = new ArrayList(); - - // 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.setMinutes(k); - - 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(date); - 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); + DataURINotificationMessage uriMsg = (DataURINotificationMessage) obj; + String[] dataUris = uriMsg.getDataURIs(); - if (diffHr != 1) { - for (int j = 0; j < diffHr-1; j++) { - dateListFinal.add( new Date(date.getTime() + 3600*1000*(j+1))); //append after i, i+1 - hHrAvgListFinal.add( MISSING_VAL); - dHrAvgListFinal.add( MISSING_VAL); - - } - } - } - } - - // fill missing in the end - int latest = dateListFinal.size(); - if (latest < HOURS*AVG_DATA_RANGE) { - for (int k = latest; k < HOURS*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); - } - } - - - float[] hHrAvgs = CalcUtil.toFloatArray(hHrAvgListFinal); - float[] dHrAvgs = CalcUtil.toFloatArray(dHrAvgListFinal); - - float[] dB = CalcEach3hr.getDisturbanceLevel(hHrAvgs, dHrAvgs); -// for ( int k = 0; k < 30; k++ ) -// System.out.print("*****dB "+ dB[k]+" "); -// System.out.println(""); - - @SuppressWarnings("unchecked") - Map dBsmall = CalcEach3hr.getSmallDisturbanceLevel(dB); - - float[] quietHHrAvg = CalcEach3hr.getQuietLevelHourAvg(dBsmall, hHrAvgs); - float[] quietDHrAvg = CalcEach3hr.getQuietLevelHourAvg(dBsmall, dHrAvgs); + // get geomag uri + List geomagUri = new ArrayList(); -// System.out.println("***hHrAvgs "+ CalcUtil.maxValue(hHrAvgs)+ " "+CalcUtil.minValue(hHrAvgs)+" "+hHrAvgs[0]+" "+hHrAvgs[1]+" "+hHrAvgs[2]); -// System.out.println("***dHrAvgs "+ CalcUtil.maxValue(dHrAvgs)+ " "+CalcUtil.minValue(dHrAvgs)+" "+dHrAvgs[0]+" "+dHrAvgs[10]); -// System.out.println("***quietHHrAvg "+ CalcUtil.maxValue(quietHHrAvg)+ " "+CalcUtil.minValue(quietHHrAvg)+" "+quietHHrAvg[0]+" "+quietHHrAvg[10]); -// System.out.println("***quietdHrAvg "+ CalcUtil.maxValue(quietDHrAvg)+ " "+CalcUtil.minValue(quietDHrAvg)+" "+quietDHrAvg[0]+" "+quietDHrAvg[10]); - - // added from FMIQDCRT11_3hr.pro - for ( int k = 0; k < quietHHrAvg.length; k++ ){ - if (quietHHrAvg[k] == MISSING_VAL || quietDHrAvg[k] == MISSING_VAL) { - quietHHrAvg[k] = CalcUtil.getMedian(quietHHrAvg); - quietDHrAvg[k] = CalcUtil.getMedian(quietDHrAvg); - } - } - - float[] qha = CalcEach3hr.getQHA(quietHHrAvg); - float[] qda = CalcEach3hr.getQHA(quietDHrAvg); -// System.out.println("***qha "+ CalcUtil.maxValue(qha)+ " "+CalcUtil.minValue(qha)+" "+qha[0]+" "+qha[10]); -// System.out.println("***qda "+ CalcUtil.maxValue(qda)+ " "+CalcUtil.minValue(qda)+" "+qda[0]+" "+qda[10]); - - float[] hQdc = CalcEach1min.getHarmonicFit(qha);//[1440] - float[] dQdc = CalcEach1min.getHarmonicFit(qda); -// System.out.println("***hQdc "+ CalcUtil.maxValue(hQdc)+ " "+CalcUtil.minValue(hQdc)+" "+hQdc[0]+" "+hQdc[10]); -// System.out.println("***dQdc "+ CalcUtil.maxValue(dQdc)+ " "+CalcUtil.minValue(dQdc)+" "+dQdc[0]+" "+dQdc[10]); - - float[] qhaQdc = CalcEach1min.getQHAQDC(hQdc);//[1440] - float[] qdaQdc = CalcEach1min.getQHAQDC(dQdc); -// System.out.println("***qhaQdc "+ CalcUtil.maxValue(qhaQdc)+ " "+CalcUtil.minValue(qhaQdc)+" "+qhaQdc[0]+" "+qhaQdc[10]+" "+station); -// System.out.println("***qdaQdc "+ CalcUtil.maxValue(qdaQdc)+ " "+CalcUtil.minValue(qdaQdc)+" "+qdaQdc[0]+" "+qdaQdc[10]); - - - /* - * Read H and D - */ - Map> kIndexMap = new HashMap>(); - - Date timeBy1 = null; - try { - timeBy1 = CalcUtil.getTimeFromUri(dataURI); - - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + for (String dataURI : dataUris) { + if (dataURI.contains("geomag")) + geomagUri.add(dataURI); + } - Date epTime = CalcUtil.getEPTime( timeBy1); - int epHour = epTime.getHours(); - - /* - * change epTime to current time - */ - List hdDataList = retrieveUriForK1min(dataURI, timeBy1); - - if (hdDataList != null && hdDataList.size() != 0) { - // if dataList <= 1440, can't calculate k-index - if (hdDataList.size() <= HOURS*MINUTES) - continue; + if (geomagUri.size() == 0) + return; - // gest best observation data - List bestList = getBestObserv( hdDataList ); - if (bestList.size() <= HOURS*MINUTES) - continue; - - List bestListFull = fillHDTimeGaps( bestList); - // System.out.println("**dataListBy1 size"+dataList.size()+" "+bestList.size()+" "+bestListFull.size()); - - // get hdata, ddata - float[] hdata = new float[HD_DATA_RANGE*HOURS*MINUTES]; - float[] ddata = new float[HD_DATA_RANGE*HOURS*MINUTES]; - - Arrays.fill(hdata, MISSING_VAL); - Arrays.fill(ddata, MISSING_VAL); - - for (int i = 0; i < bestListFull.size(); i++) { - List list = (List) bestListFull.get(i); - if (list != null && !list.isEmpty()) { - hdata[i] = list.get(1); - ddata[i] = list.get(2); - } - } - - // System.out.println("***hdata "+ CalcUtil.maxValue(hdata)+ " "+CalcUtil.minValue(hdata)+" "+hdata[0]+" "+hdata[2879]); - // System.out.println("***ddata "+ CalcUtil.maxValue(ddata)+ " "+CalcUtil.minValue(ddata)+" "+ddata[0]+" "+ddata[2879]); + String[] dataURIs = geomagUri.toArray(new String[geomagUri.size()]); - defLength = CalcEach3hr.getDefLength(stationCode, epHour); - - float[] hhdata = CalcEach1min.fillGaps(hdata); - float[] dddata = CalcEach1min.fillGaps(ddata); - - int currTimeIndex = CalcEach1min.getCurrTimeIndex(hour, min, epHour); - //System.out.println("**currTimeIndex "+currTimeIndex); - - hhdata = CalcEach1min.getExtrapolation(hhdata, qhaQdc, currTimeIndex); - dddata = CalcEach1min.getExtrapolation(dddata, qdaQdc, currTimeIndex); - // System.out.println("***hhdataExtr "+ CalcUtil.maxValue(hhdata)+ " "+CalcUtil.minValue(hhdata)+" "+hhdata[0]+" "+hhdata[10]); - // System.out.println("***dddataExtr "+ CalcUtil.maxValue(dddata)+ " "+CalcUtil.minValue(dddata)+" "+dddata[0]+" "+dddata[10]); - - float[] hDev = CalcEach1min.getDev(hhdata, hQdc);//[1440] - float[] dDev = CalcEach1min.getDev(dddata, dQdc); - // System.out.println("***hDev "+ CalcUtil.maxValue(hDev)+ " "+CalcUtil.minValue(hDev)+" "+hDev[0]+" "+hDev[10]); - // System.out.println("***dDev "+ CalcUtil.maxValue(dDev)+ " "+CalcUtil.minValue(dDev)+" "+dDev[0]+" "+dDev[10]); - - //already considered missing in getDev - - int[] kLimit = CalcUtil.getKLimit(stationCode); - - int missingFlag = 0; - List kList = CalcEach1min.getKIndex(hDev, dDev, kLimit, missingFlag);//[8] - - float[] kIndex = kList.get(0); - float[] gamma = kList.get(1); - - float[] kLength = CalcUtil.geKLength();//[8] - float[] fitLength = CalcEach1min.getFitLength(defLength, kIndex, kLength);//[24] - - - float[] hcA = CalcEach1min.getCentHourAvg(hhdata, fitLength, kIndex);//middle [24] - float[] dcA = CalcEach1min.getCentHourAvg(dddata, fitLength, kIndex); - // System.out.println("***hcA "+ CalcUtil.maxValue(hcA)+ " "+CalcUtil.minValue(hcA)+" "+hcA[0]+" "+hcA[10]+" "+stationCode); - // System.out.println("***dcA "+ CalcUtil.maxValue(dcA)+ " "+CalcUtil.minValue(dcA)+" "+dcA[0]+" "+dcA[10]); - - hcA = CalcEach1min.adjustHrCentAvg(hcA, qha, gamma, kLimit); - dcA = CalcEach1min.adjustHrCentAvg(dcA, qda, gamma, kLimit); - // System.out.println("***hcAAdj "+ CalcUtil.maxValue(hcA)+ " "+CalcUtil.minValue(hcA)+" "+hcA[0]+" "+hcA[10]); - // System.out.println("***dcAAdj "+ CalcUtil.maxValue(dcA)+ " "+CalcUtil.minValue(dcA)+" "+dcA[0]+" "+dcA[10]); - - // Harmonic Fit to derive the qdc - int foundMiss = 0; - for (int i=0; i k1minList = retrieveSingleK1min(dataURI, timeBy1); - - if (k1minList != null && k1minList.size() != 0) {//String newUri = dataURI.substring(0, 21) +":30:00.0"+ dataURI.substring(29, 34)+ "100/GEOMAG"; - for (int i = 0; i < k1minList.size(); i++) { //1 - GeoMagK1min row = k1minList.get(i); - - int id = (Integer) row.getId(); - if (id != 0) - recK1min.setId(id); - } - } - - recK1min.setRefTime(timeBy1); - recK1min.setLastUpdate(Calendar.getInstance().getTime()); - recK1min.setStationCode(stationCode); - recK1min.setKestIndex(kest_index); - recK1min.setKestReal(kest_real); - recK1min.setKestGamma(kest_gamma); - recK1min.setHKIndex(hkindex); - recK1min.setHKReal(hk_real); - recK1min.setHKGamma(hgamma); - recK1min.setDKIndex(dkindex); - recK1min.setDKReal(dk_real); - recK1min.setDKGamma(dgamma); - recK1min.setKs(ksArray); - recK1min.setAest(aestArray); - recK1min.setHCount(countH); - recK1min.setDCount(countD); - - GeoMagK1minDao k1minDao = new GeoMagK1minDao(); - k1minDao.persist(recK1min); - - calcK3h(dataURI, kest_index, kest_real, kest_gamma); - - } // end of for dataURI - } - } - } - } - - /* - * write to geomag_k3hr - */ - public void calcK3h(String dataURI, int kest_index, float kest_real, float kest_gamma){ - List idDb = new ArrayList(); - List dateDb = new ArrayList(); - List kIndexDb = new ArrayList(); - List kGammaDb = new ArrayList(); - List kestIndexDb = new ArrayList(); - - int aRun = 0; - - String stationCode = CalcUtil.getStationFromUri(dataURI); - - Date currTime = null; - try { - currTime = CalcUtil.getTimeFromUri(dataURI); - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - int hour = currTime.getHours(); - int min = currTime.getMinutes(); - Date epTime = CalcUtil.getEPTime(currTime); - - GeoMagK3hr recK3hr = new GeoMagK3hr(); + // sort + Arrays.sort(dataURIs); + try { + dao = (GeoMagDao) PluginFactory.getInstance().getPluginDao(GeoMag); + } catch (PluginException e) { + e.printStackTrace(); + } - List k3hrList = retrieveUriForK3hr(dataURI, epTime); //epTime not in the list - - if (k3hrList != null && k3hrList.size() != 0) { - - for (int i = 0; i < k3hrList.size(); i++) { - - GeoMagK3hr row = (GeoMagK3hr) k3hrList.get(i); - - dateDb.add (row.getRefTime()); - idDb.add ( row.getId()); - kIndexDb.add ( row.getKIndex()); - kGammaDb.add ( row.getKGamma()); - kestIndexDb.add (row.getKestIndex()); - - } - - sort(dateDb, idDb, kIndexDb, kGammaDb, kestIndexDb); - - } + calcSimpleHourAvg(dataURIs); + calcK(dataURIs); - List k3hrAtPoint = retrieveSingleK3hr(dataURI, epTime); - - if (k3hrAtPoint == null || k3hrAtPoint.size() == 0) { + } - // calculate aRunning, aFinalRunning - // only need first 7 k - int sum = 0; - for (int k = 0; k < kestIndexDb.size(); k++) { - int a_est = CalcKp.getAest(stationCode, kestIndexDb.get(k)); - sum += a_est; - } - sum += CalcKp.getAest(stationCode, kest_index); - aRun = (int) sum / (kestIndexDb.size()+1); - - recK3hr.setRefTime(epTime); - recK3hr.setLastUpdate(Calendar.getInstance().getTime()); - recK3hr.setStationCode(stationCode); - recK3hr.setKestIndex(kest_index); - recK3hr.setKestReal(kest_real); - recK3hr.setKestGamma(kest_gamma); - recK3hr.setARunning(aRun); - - GeoMagK3hrDao k3hrDao = new GeoMagK3hrDao(); - k3hrDao.persist(recK3hr); - } + /* + * For hdf5 + */ + public IDataRecord[] getDataRecords(String uri) { + IDataRecord[] dataRec = null; + IDataStore dataStore = null; - else { - GeoMagK3hr row = (GeoMagK3hr) k3hrAtPoint.get(0); - int idCurr = row.getId(); - int kIndexCurr = row.getKIndex(); - float kGammaCurr = row.getKGamma(); - float kRealCurr = row.getKReal(); - int aFinalRunCurr = row.getAFinalRunning(); - int manualCurr = row.getIsManual(); - - if ((hour+1)%3 == 0 && (min+1)%60 == 0) { - - // calculate aFinalRunning, aFinalRunning - int sumEnd = 0; - for (int k = 0; k < kIndexDb.size(); k++) { - int a_est = CalcKp.getAest(stationCode, kIndexDb.get(k)); - sumEnd += a_est; - } - -// if ((kIndexCurr == MISSING_INT || kIndexCurr == 0) && //manual=0 -// (kGammaCurr == MISSING_INT || kGammaCurr == 0) ) - if (manualCurr == 0) - sumEnd += CalcKp.getAest(stationCode, kest_index); - else - sumEnd += CalcKp.getAest(stationCode, kIndexCurr); - - int aFinalRun = (int) sumEnd / (kIndexDb.size()+1); - - recK3hr.setAFinalRunning(aFinalRun); - recK3hr.setIsManual(manualCurr); -// if (kIndexCurr == MISSING_INT || kIndexCurr == 0) //manual=0 -// recK3hr.setKIndex(kest_index); -// else -// recK3hr.setKIndex(kIndexCurr); - - if (manualCurr == 0) { - recK3hr.setKIndex(kest_index); - recK3hr.setKReal(kest_real); - recK3hr.setKGamma(kest_gamma); - } - else { - - recK3hr.setKIndex(kIndexCurr); - recK3hr.setKReal(kRealCurr); - recK3hr.setKGamma(kGammaCurr); - } - - } - else { - - recK3hr.setKIndex(kIndexCurr); - recK3hr.setKReal(kRealCurr); - recK3hr.setKGamma(kGammaCurr); - recK3hr.setAFinalRunning(aFinalRunCurr); - recK3hr.setIsManual(manualCurr); - } + GeoMagRecord record = new GeoMagRecord(uri); + if (record != null) + dataStore = dao.getDataStore((IPersistable) record); - // calculate aRunning, aFinalRunning - // only need first 7 k - int sum = 0; - for (int k = 0; k < kestIndexDb.size(); k++) { - int a_est = CalcKp.getAest(stationCode, kestIndexDb.get(k)); - sum += a_est; - } - sum += CalcKp.getAest(stationCode, kest_index); - aRun = (int) sum / (kestIndexDb.size()+1); - - - if (idCurr != 0) - recK3hr.setId(idCurr); - recK3hr.setRefTime(epTime); - recK3hr.setLastUpdate(Calendar.getInstance().getTime()); - recK3hr.setStationCode(stationCode); - recK3hr.setKestIndex(kest_index); - recK3hr.setKestReal(kest_real); - recK3hr.setKestGamma(kest_gamma); - recK3hr.setARunning(aRun); - - GeoMagK3hrDao k3hrDao = new GeoMagK3hrDao(); - k3hrDao.persist(recK3hr); - } + try { + dataRec = dataStore.retrieve(uri); // obs_time, compx...//size 7 + } catch (FileNotFoundException e1) { + // e1.printStackTrace(); + System.out.println("This uri didn't find the records."); + } catch (StorageException e1) { + System.out + .println("This uri didn't find place to store the records."); - } + } + + return dataRec; + } + + /* + * Input data of all source, output with higher priority source data + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + public List getBestObserv(List dataList) { + + List comp1List = new ArrayList(); + List comp2List = new ArrayList(); + List badPointList = new ArrayList(); + List dateList = new ArrayList(); + List sourceList = new ArrayList(); + + List bestList = new ArrayList(); + + if (dataList != null) { + for (int i = 0; i < dataList.size(); i++) { + + Object[] row = (Object[]) dataList.get(i); + + comp1List.add((Float) row[0]); + comp2List.add((Float) row[1]); + dateList.add((Date) row[2]); + badPointList.add((Integer) row[3]); + sourceList.add((Integer) row[4]); + + } + + DatabaseUtil.sort(dateList, sourceList, comp1List, comp2List, + badPointList); + + int count = 0; + int size = dateList.size(); + + /* + * tempList combine all lists for the first 4 items. size=4 newList + * holds tempLists ordered by source. size=3 bestList construct + * newList with best source bestListFull filled time gaps + */ + for (int i = 0; i < size; i = i + count) { + count = 0; + + List tempList1 = new ArrayList(); + List tempList2 = new ArrayList(); + List tempList3 = new ArrayList(); + + List newList = new ArrayList(); + // init 3 + newList.add(0, new ArrayList()); + newList.add(1, new ArrayList()); + newList.add(2, new ArrayList()); + + tempList1.add(dateList.get(i)); + if (badPointList.get(i) != null && badPointList.get(i) != 0) { + tempList1.add(MISSING_VAL); + tempList1.add(MISSING_VAL); + } else { + tempList1.add(comp1List.get(i)); + tempList1.add(comp2List.get(i)); + } + newList.set(sourceList.get(i) % 100 - 1, tempList1); + count++; + + if (i + 1 < size + && dateList.get(i).compareTo(dateList.get(i + 1)) == 0) { + + tempList2.add(dateList.get(i + 1)); + if (badPointList.get(i + 1) != null + && badPointList.get(i + 1) != 0) { + tempList2.add(MISSING_VAL); + tempList2.add(MISSING_VAL); + } else { + tempList2.add(comp1List.get(i + 1)); + tempList2.add(comp2List.get(i + 1)); + } + newList.set(sourceList.get(i + 1) % 100 - 1, tempList2); + count++; + } + + if (i + 2 < size + && dateList.get(i).compareTo(dateList.get(i + 2)) == 0) { + + tempList3.add(dateList.get(i + 2)); + if (badPointList.get(i + 2) != null + && badPointList.get(i + 2) != 0) { + tempList3.add(MISSING_VAL); + tempList3.add(MISSING_VAL); + } else { + tempList3.add(comp1List.get(i + 2)); + tempList3.add(comp2List.get(i + 2)); + } + newList.set(sourceList.get(i + 2) % 100 - 1, tempList3); + count++; + } + + if (newList.get(2) == null || newList.get(2).isEmpty()) // newList.get(0)= + // [3281750, + // 2013-05-06 + // 00:00:00.0, + // 20829.85, + // -297.05] + newList.remove(2); + if (newList.get(1) == null || newList.get(1).isEmpty()) + newList.remove(1); + if (newList.get(0) == null || newList.get(0).isEmpty()) + newList.remove(0); + + // Now only check if comp2 (...get(2)) is MISSING_VAL. Could + // check both + if (newList.get(0).get(2) != null + && (Float) newList.get(0).get(2) != MISSING_VAL) { + bestList.add(newList.get(0)); + } else if (newList.size() > 1 + && (Float) newList.get(0).get(2) == MISSING_VAL + && i + 1 < size) { + // if date i = date(i+1) && comp1 (i+1) != missing + if ((Date) newList.get(0).get(1) == (Date) newList.get(1) + .get(1) + && newList.get(1).get(2) != null + && (Float) newList.get(1).get(2) != MISSING_VAL) { + bestList.add(newList.get(1)); + } else if (newList.size() > 2 + && (Float) newList.get(1).get(2) == MISSING_VAL + && i + 2 < size) { + if ((Date) newList.get(0).get(1) == (Date) newList.get( + 2).get(1) + && (Float) newList.get(2).get(2) != MISSING_VAL) { + bestList.add(newList.get(2)); + } else { + bestList.add(newList.get(0)); + } + } + } + } + } + + return bestList; + } + + /* + * fill time tag gaps, return fullBestList + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + public List fillHDTimeGaps(List bestList) { + List fullBestList = new ArrayList(); + + // fill missing in the beginning + Date date = (Date) bestList.get(0).get(0); // bestList.get(i) eq. + // newList. + int min0 = date.getMinutes(); + + if (min0 != 0) { + for (int k = 0; k < min0; k++) { + List newList2 = new ArrayList(); // eq. newList + + Date dateNew = (Date) date.clone(); + dateNew.setMinutes(k); + + newList2.add(dateNew); + newList2.add(MISSING_VAL); + newList2.add(MISSING_VAL); + fullBestList.add(newList2); + + } + } + + // fill missing in the middle + for (int j = 0; j < bestList.size(); j++) { // i=0 first non missing + // data + + Date date0 = (Date) bestList.get(j).get(0);// dateList.get(i); + fullBestList.add(bestList.get(j)); + + if (j + 1 < bestList.size()) { + Date date1 = (Date) bestList.get(j + 1).get(0);// dateList.get(i+1); + int diffMin = (int) (date1.getTime() - date0.getTime()) + / (60 * 1000); + + if (diffMin != 1) { + for (int k = 0; k < diffMin - 1; k++) { + List newList2 = new ArrayList(); // eq. newList + + newList2.add(new Date(date0.getTime() + 60 * 1000 + * (k + 1))); + newList2.add(MISSING_VAL); + newList2.add(MISSING_VAL); + fullBestList.add(newList2); + + } + } + } + } + + // // fill missing in the end + // int latest = fullBestList.size(); + // if (latest < HOURS*MINUTES*HD_DATA_RANGE) { + // for (int k = latest; k < HOURS*MINUTES*HD_DATA_RANGE; k++) { + // List newList2 = new ArrayList(); + // Date d = (Date)fullBestList.get(0).get(latest-1); + // + // newList2.add(new Date(d.getTime() + 60*1000*(k+1))); + // newList2.add(MISSING_VAL); + // newList2.add(MISSING_VAL); + // fullBestList.add( newList2); + // } + // } + + return fullBestList; + } + + /* + * when uri time is 59 min past the hour, calculate the averages and write + * to geomat_houravg + */ + public void calcSimpleHourAvg(String[] dataURIs) throws StorageException { + + if (dao != null && dataURIs != null) { + for (String dataURI : dataURIs) { + String stationCode = CalcUtil.getStationFromUri(dataURI); + + Date time = null; + + try { + time = CalcUtil.getTimeFromUri(dataURI); + } catch (ParseException e) { + e.printStackTrace(); + } + + int min = time.getMinutes(); + + List dataList = null; + if (min == 59) + dataList = DatabaseUtil.retrieveUriForAvg(dao, dataURI, + time); + else + continue; + + if (dataList != null && dataList.size() != 0) { + List bestList = getBestObserv(dataList); + + float[] hrAvg = CalcEach3hr.getSimpleHourAvg(bestList); + + GeoMagAvg recAvg = new GeoMagAvg(); + + // look the avg table to see if the avg already exists + time.setMinutes(30); + List avgList = DatabaseUtil.retrieveSingleAvg( + dataURI, time); + + if (avgList != null && avgList.size() != 0) { + for (int i = 0; i < avgList.size(); i++) { // 1 + GeoMagAvg row = avgList.get(i); + List idList = new ArrayList(); + idList.add((Integer) row.getId()); + recAvg.setId((int) idList.get(0)); + + } + } + + recAvg.setAvgTime(time); + recAvg.setInsertTime(Calendar.getInstance().getTime()); + recAvg.setStationCode(stationCode); + recAvg.sethHrAvg(hrAvg[0]); + recAvg.setdHrAvg(hrAvg[1]); + + GeoMagAvgDao avgDao = new GeoMagAvgDao(); + avgDao.persist(recAvg); + + } + } + } + + } + + /* + * Write to geomag_k1min + */ + public void calcK(String[] dataURIs) { + + if (dataURIs != null) { + for (String dataURI : dataURIs) { + + String stationCode = CalcUtil.getStationFromUri(dataURI); + String source = CalcUtil.getSourceFromUri(dataURI); + + Date timeBy3 = null; + + try { + timeBy3 = CalcUtil.getTimeFromUri(dataURI); + } catch (ParseException e) { + e.printStackTrace(); + } + + int hour = timeBy3.getHours(); + int min = timeBy3.getMinutes(); + + /* + * Read average + */ + Date spTime = CalcUtil.getSPTime(timeBy3); + + List dataList = null; + + dataList = DatabaseUtil.retrieveUriBy3hr(dataURI, + CalcUtil.getSPTime(timeBy3)); + + // dataList size (avg) < 24, can't calculate dB[j] + if (dataList.size() <= HOURS) + continue; + + if (dataList != null && dataList.size() >= 5) { + List dateListFinal = new ArrayList(); + List hHrAvgListFinal = new ArrayList(); + List dHrAvgListFinal = new ArrayList(); + + DatabaseUtil.fillHrAvgTimeGaps(dataList, dateListFinal, + hHrAvgListFinal, dHrAvgListFinal, spTime); + + float[] hHrAvgs = CalcUtil.toFloatArray(hHrAvgListFinal); + float[] dHrAvgs = CalcUtil.toFloatArray(dHrAvgListFinal); + + float[] dB = CalcEach3hr.getDisturbanceLevel(hHrAvgs, + dHrAvgs); + + @SuppressWarnings("unchecked") + Map dBsmall = CalcEach3hr + .getSmallDisturbanceLevel(dB); + + float[] quietHHrAvg = CalcEach3hr.getQuietLevelHourAvg( + dBsmall, hHrAvgs); + float[] quietDHrAvg = CalcEach3hr.getQuietLevelHourAvg( + dBsmall, dHrAvgs); + + // added from FMIQDCRT11_3hr.pro + for (int k = 0; k < quietHHrAvg.length; k++) { + if (quietHHrAvg[k] == MISSING_VAL + || quietDHrAvg[k] == MISSING_VAL) { + quietHHrAvg[k] = CalcUtil.getMedian(quietHHrAvg); + quietDHrAvg[k] = CalcUtil.getMedian(quietDHrAvg); + } + } + + float[] qha = CalcEach3hr.getQHA(quietHHrAvg); + float[] qda = CalcEach3hr.getQHA(quietDHrAvg); + + float[] hQdc = CalcEach1min.getHarmonicFit(qha);// [1440] + float[] dQdc = CalcEach1min.getHarmonicFit(qda); + + float[] qhaQdc = CalcEach1min.getQHAQDC(hQdc);// [1440] + float[] qdaQdc = CalcEach1min.getQHAQDC(dQdc); + + /* + * Read H and D + */ + Map> kIndexMap = new HashMap>(); + + Date timeBy1 = null; + try { + timeBy1 = CalcUtil.getTimeFromUri(dataURI); + + } catch (ParseException e) { + e.printStackTrace(); + } + + Date epTime = CalcUtil.getEPTime(timeBy1); + int epHour = epTime.getHours(); + + /* + * change epTime to current time + */ + List hdDataList = DatabaseUtil.retrieveUriForK1min(dao, + dataURI, timeBy1); + + if (hdDataList != null && hdDataList.size() != 0) { + // if dataList <= 1440, can't calculate k-index + if (hdDataList.size() <= HOURS * MINUTES) + continue; + + // gest best observation data + List bestList = getBestObserv(hdDataList); + if (bestList.size() <= HOURS * MINUTES) + continue; + + List bestListFull = fillHDTimeGaps(bestList); + + // get hdata, ddata + float[] hdata = new float[HD_DATA_RANGE * HOURS + * MINUTES]; + float[] ddata = new float[HD_DATA_RANGE * HOURS + * MINUTES]; + + Arrays.fill(hdata, MISSING_VAL); + Arrays.fill(ddata, MISSING_VAL); + + for (int i = 0; i < bestListFull.size(); i++) { + List list = (List) bestListFull + .get(i); + if (list != null && !list.isEmpty()) { + hdata[i] = list.get(1); + ddata[i] = list.get(2); + } + } + + defLength = CalcEach3hr.getDefLength(stationCode, + epHour); + + float[] hhdata = CalcEach1min.fillGaps(hdata); + float[] dddata = CalcEach1min.fillGaps(ddata); + + int currTimeIndex = CalcEach1min.getCurrTimeIndex(hour, + min, epHour); + + hhdata = CalcEach1min.getExtrapolation(hhdata, qhaQdc, + currTimeIndex); + dddata = CalcEach1min.getExtrapolation(dddata, qdaQdc, + currTimeIndex); + + float[] hDev = CalcEach1min.getDev(hhdata, hQdc);// [1440] + float[] dDev = CalcEach1min.getDev(dddata, dQdc); + + // already considered missing in getDev + + int[] kLimit = CalcUtil.getKLimit(stationCode); + + int missingFlag = 0; + List kList = CalcEach1min.getKIndex(hDev, + dDev, kLimit, missingFlag);// [8] + + float[] kIndex = kList.get(0); + float[] gamma = kList.get(1); + + float[] kLength = CalcUtil.geKLength();// [8] + float[] fitLength = CalcEach1min.getFitLength( + defLength, kIndex, kLength);// [24] + + float[] hcA = CalcEach1min.getCentHourAvg(hhdata, + fitLength, kIndex);// middle [24] + float[] dcA = CalcEach1min.getCentHourAvg(dddata, + fitLength, kIndex); + + hcA = CalcEach1min.adjustHrCentAvg(hcA, qha, gamma, + kLimit); + dcA = CalcEach1min.adjustHrCentAvg(dcA, qda, gamma, + kLimit); + + // Harmonic Fit to derive the qdc + int foundMiss = 0; + for (int i = 0; i < hcA.length; i++) { + if (hcA[i] == MISSING_VAL) { + foundMiss = 1; + break; + } + } + if (foundMiss == 0) + hQdc = CalcEach1min.getHarmonicFit(hcA); + + foundMiss = 0; + for (int i = 0; i < dcA.length; i++) { + if (dcA[i] == MISSING_VAL) { + foundMiss = 1; + break; + } + } + if (foundMiss == 0) + dQdc = CalcEach1min.getHarmonicFit(dcA); + + /* + * Do a few iterations. check for convergence of k_index + * and exit loop Done before ITERATIONS if you see two + * passes with identical values for k_index + */ + float[] last_kindex = new float[8]; + Arrays.fill(last_kindex, -1); + + /* + * Check for convergence of k_index and exit loop before + * ITERATIONS are done if you see two passes with + * identical values for k_index + */ + for (int num = 0; num < ITERATIONS; num++) { + float kchange = 0; + hDev = CalcEach1min.getDev(hhdata, hQdc); + dDev = CalcEach1min.getDev(dddata, dQdc); + + kList = CalcEach1min.getKIndex(hDev, dDev, kLimit, + missingFlag); + kIndex = kList.get(0); + gamma = kList.get(1); + + // Check for convergence of k_index + if (kIndex.length == 8 && last_kindex.length == 8) + for (int i = 0; i < last_kindex.length; i++) { + kchange += Math.abs(kIndex[i] + - last_kindex[i]); + // System.out.println("**kchange "+ + // kIndex[i] +" " + // +last_kindex[i]+" "+(kIndex[i] + // -last_kindex[i])); + } + if (kchange == 0) + break; + + fitLength = CalcEach1min.getFitLength(defLength, + kIndex, kLength); + + hcA = CalcEach1min.getCentHourAvg(hhdata, + fitLength, kIndex); + dcA = CalcEach1min.getCentHourAvg(dddata, + fitLength, kIndex); + + hcA = CalcEach1min.adjustHrCentAvg(hcA, qha, gamma, + kLimit); + dcA = CalcEach1min.adjustHrCentAvg(dcA, qda, gamma, + kLimit); + + // Harmonic Fit to derive the qdc + foundMiss = 0; + for (int i = 0; i < hcA.length; i++) { + if (hcA[i] == MISSING_VAL) { + foundMiss = 1; + break; + } + } + if (foundMiss == 0) + hQdc = CalcEach1min.getHarmonicFit(hcA); + + foundMiss = 0; + for (int i = 0; i < dcA.length; i++) { + if (dcA[i] == MISSING_VAL) { + foundMiss = 1; + break; + } + } + if (foundMiss == 0) + dQdc = CalcEach1min.getHarmonicFit(dcA); + + last_kindex = kIndex.clone(); + } + + // Now do the calculation using the original data + // (hdata, ddata) + hDev = CalcEach1min.getDev(hdata, hQdc);// [1440] + dDev = CalcEach1min.getDev(ddata, dQdc); + + kList = CalcEach1min.getKIndex(hDev, dDev, kLimit, + missingFlag); + kIndex = kList.get(0); + gamma = kList.get(1); + + float[] hkIndex = kList.get(2); + float[] hGamma = kList.get(3); + float[] dkIndex = kList.get(4); + float[] dGamma = kList.get(5); + int lastHCount = 0; + int lastDCount = 0; + + for (int i = 2700; i < 2880; i++) { + if (hdata[i] != MISSING_VAL) + lastHCount++; + if (hdata[i] != MISSING_VAL) + lastDCount++; + } + + float[] count = new float[2]; + count[0] = lastHCount; + count[1] = lastDCount; + kList.add(6, count); + kIndexMap.put(stationCode + source, kList); + + // float[] kest = CalcKp.getKest(stationCode, + // kList.get(0), kList.get(1)); + + float ks = 0; + try { + ks = CalcKp.getKs(stationCode, (int) kIndex[7], + (Date) timeBy1.clone()); // 7 is last point + // kIndex + } catch (ParseException e) { + e.printStackTrace(); + } + + int a_est = CalcKp + .getAest(stationCode, (int) kIndex[7]); + + // 1 min + int kest_index = (int) kIndex[7]; + float kest_real = CalcKp.getKest(stationCode, + (int) kIndex[7], gamma[7]); + float kest_gamma = gamma[7]; + float hk_real = CalcKp.getKest(stationCode, + (int) hkIndex[7], hGamma[7]); + float hgamma = hGamma[7]; + float dk_real = CalcKp.getKest(stationCode, + (int) dkIndex[7], dGamma[7]); + float dgamma = dGamma[7]; + int hkindex = (int) hkIndex[7]; + int dkindex = (int) dkIndex[7]; + int countH = (int) count[0]; + int countD = (int) count[1]; + float ksArray = ks; + int aestArray = a_est; + + GeoMagK1min recK1min = new GeoMagK1min(); + + List k1minList = DatabaseUtil + .retrieveSingleK1min(dataURI, timeBy1); + + if (k1minList != null && k1minList.size() != 0) { + for (int i = 0; i < k1minList.size(); i++) { // 1 + GeoMagK1min row = k1minList.get(i); + + int id = (Integer) row.getId(); + if (id != 0) + recK1min.setId(id); + } + } + + recK1min.setRefTime(timeBy1); + recK1min.setLastUpdate(Calendar.getInstance().getTime()); + recK1min.setStationCode(stationCode); + recK1min.setKestIndex(kest_index); + recK1min.setKestReal(kest_real); + recK1min.setKestGamma(kest_gamma); + recK1min.setHkIndex(hkindex); + recK1min.setHkReal(hk_real); + recK1min.setHkGamma(hgamma); + recK1min.setDkIndex(dkindex); + recK1min.setDkReal(dk_real); + recK1min.setDkGamma(dgamma); + recK1min.setKs(ksArray); + recK1min.setAest(aestArray); + recK1min.sethCount(countH); + recK1min.setdCount(countD); + + GeoMagK1minDao k1minDao = new GeoMagK1minDao(); + k1minDao.persist(recK1min); + + calcK3h(dataURI, kest_index, kest_real, kest_gamma); + + } // end of for dataURI + } + } + } + } + + /* + * write to geomag_k3hr + */ + public void calcK3h(String dataURI, int kest_index, float kest_real, + float kest_gamma) { + List idDb = new ArrayList(); + List dateDb = new ArrayList(); + List kIndexDb = new ArrayList(); + List kGammaDb = new ArrayList(); + List kestIndexDb = new ArrayList(); + + int aRun = 0; + + String stationCode = CalcUtil.getStationFromUri(dataURI); + + Date currTime = null; + try { + currTime = CalcUtil.getTimeFromUri(dataURI); + } catch (ParseException e) { + e.printStackTrace(); + } + + int hour = currTime.getHours(); + int min = currTime.getMinutes(); + Date epTime = CalcUtil.getEPTime(currTime); + + GeoMagK3hr recK3hr = new GeoMagK3hr(); + + List k3hrList = DatabaseUtil.retrieveUriForK3hr(dataURI, + epTime); // epTime not in the list + + if (k3hrList != null && k3hrList.size() != 0) { + + for (int i = 0; i < k3hrList.size(); i++) { + + GeoMagK3hr row = (GeoMagK3hr) k3hrList.get(i); + + dateDb.add(row.getRefTime()); + idDb.add(row.getId()); + kIndexDb.add(row.getKIndex()); + kGammaDb.add(row.getKGamma()); + kestIndexDb.add(row.getKestIndex()); + + } + + DatabaseUtil.sort(dateDb, idDb, kIndexDb, kGammaDb, kestIndexDb); + + } + + List k3hrAtPoint = DatabaseUtil.retrieveSingleK3hr(dataURI, + epTime); + + if (k3hrAtPoint == null || k3hrAtPoint.size() == 0) { + + // calculate aRunning, aFinalRunning + // only need first 7 k + int sum = 0; + for (int k = 0; k < kestIndexDb.size(); k++) { + int a_est = CalcKp.getAest(stationCode, kestIndexDb.get(k)); + sum += a_est; + } + sum += CalcKp.getAest(stationCode, kest_index); + aRun = (int) sum / (kestIndexDb.size() + 1); + + recK3hr.setRefTime(epTime); + recK3hr.setLastUpdate(Calendar.getInstance().getTime()); + recK3hr.setStationCode(stationCode); + recK3hr.setKestIndex(kest_index); + recK3hr.setKestReal(kest_real); + recK3hr.setKestGamma(kest_gamma); + recK3hr.setARunning(aRun); + + GeoMagK3hrDao k3hrDao = new GeoMagK3hrDao(); + k3hrDao.persist(recK3hr); + } + + else { + GeoMagK3hr row = (GeoMagK3hr) k3hrAtPoint.get(0); + int idCurr = row.getId(); + int kIndexCurr = row.getKIndex(); + float kGammaCurr = row.getKGamma(); + float kRealCurr = row.getKReal(); + int aFinalRunCurr = row.getAFinalRunning(); + int manualCurr = row.getIsManual(); + + if ((hour + 1) % 3 == 0 && (min + 1) % 60 == 0) { + + // calculate aFinalRunning, aFinalRunning + int sumEnd = 0; + for (int k = 0; k < kIndexDb.size(); k++) { + int a_est = CalcKp.getAest(stationCode, kIndexDb.get(k)); + sumEnd += a_est; + } + + if (manualCurr == 0) + sumEnd += CalcKp.getAest(stationCode, kest_index); + else + sumEnd += CalcKp.getAest(stationCode, kIndexCurr); + + int aFinalRun = (int) sumEnd / (kIndexDb.size() + 1); + + recK3hr.setAFinalRunning(aFinalRun); + recK3hr.setIsManual(manualCurr); + + if (manualCurr == 0) { + recK3hr.setKIndex(kest_index); + recK3hr.setKReal(kest_real); + recK3hr.setKGamma(kest_gamma); + } else { + + recK3hr.setKIndex(kIndexCurr); + recK3hr.setKReal(kRealCurr); + recK3hr.setKGamma(kGammaCurr); + } + + } else { + + recK3hr.setKIndex(kIndexCurr); + recK3hr.setKReal(kRealCurr); + recK3hr.setKGamma(kGammaCurr); + recK3hr.setAFinalRunning(aFinalRunCurr); + recK3hr.setIsManual(manualCurr); + } + + // calculate aRunning, aFinalRunning + // only need first 7 k + int sum = 0; + for (int k = 0; k < kestIndexDb.size(); k++) { + int a_est = CalcKp.getAest(stationCode, kestIndexDb.get(k)); + sum += a_est; + } + sum += CalcKp.getAest(stationCode, kest_index); + aRun = (int) sum / (kestIndexDb.size() + 1); + + if (idCurr != 0) + recK3hr.setId(idCurr); + recK3hr.setRefTime(epTime); + recK3hr.setLastUpdate(Calendar.getInstance().getTime()); + recK3hr.setStationCode(stationCode); + recK3hr.setKestIndex(kest_index); + recK3hr.setKestReal(kest_real); + recK3hr.setKestGamma(kest_gamma); + recK3hr.setARunning(aRun); + + GeoMagK3hrDao k3hrDao = new GeoMagK3hrDao(); + k3hrDao.persist(recK3hr); + } + + } } diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcEach1min.java b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcEach1min.java deleted file mode 100644 index 4ab0a6d79e..0000000000 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcEach1min.java +++ /dev/null @@ -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. - * - *
- * SOFTWARE HISTORY
- *                   
- * ate          Ticket#     Engineer   Description
- * -----------  ----------  ---------- --------------------------
- * 05/14/2013   #989        qzhou      Initial Creation
- * 
- * - * @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 list = new ArrayList(); - - // 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; - } -} - diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcEach3hr.java b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcEach3hr.java deleted file mode 100644 index 9e8be980ee..0000000000 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcEach3hr.java +++ /dev/null @@ -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. - * - *
- * SOFTWARE HISTORY
- *                   
- * ate          Ticket#     Engineer   Description
- * -----------  ----------  ---------- --------------------------
- * 05/14/2013   #989        qzhou      Initial Creation
- * 
- * - * @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 list = (List) 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 dBSmall = new HashMap(); - - 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 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 mEntry = (Map.Entry) 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; - } - -} - - diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcKp.java b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcKp.java deleted file mode 100644 index 088831cdda..0000000000 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcKp.java +++ /dev/null @@ -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. - * - *
- * SOFTWARE HISTORY
- *                   
- * ate          Ticket#     Engineer   Description
- * -----------  ----------  ---------- --------------------------
- * 05/14/2013   #989        qzhou      Initial Creation
- * 
- * - * @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 getKsThreeList(String station) { - - ArrayList threeKsList = CalcUtil.getStationCoeff().getStationByCode(station).getKsThree();//size 24 - - return threeKsList; - } - - public static List getKsThree(Date time, String station, int k) { - List ks = new ArrayList();; - //KsThree ksThree = null; - ArrayList 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 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 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 mEntry = (Map.Entry) 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 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 mEntry = (Map.Entry) 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 coeff = CalcUtil.getCoeffW(station[i]); - int j = 0; - - Iterator iter = coeff.entrySet().iterator(); - while (iter.hasNext()) { - wcoeff[i][j] = ((Map.Entry) 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 coeff = CalcUtil.getCoeffW(station[i]); - int j = 0; - Iterator iter = coeff.entrySet().iterator(); - while (iter.hasNext()) { - if (((Map.Entry) iter.next()).getKey().equalsIgnoreCase(fitTime)) { - wcoeff[i] = ((Map.Entry) 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; - } -} - diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcUtil.java b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcUtil.java deleted file mode 100644 index c3ee0b897f..0000000000 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/calculation/CalcUtil.java +++ /dev/null @@ -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. - * - *
- * SOFTWARE HISTORY
- *                   
- * ate          Ticket#     Engineer   Description
- * -----------  ----------  ---------- --------------------------
- * 05/14/2013   #989        qzhou      Initial Creation
- * 
- * - * @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 getCoeffAandB(String station) { - Map abCoeff = new HashMap(); - - List 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 stationMap = look.getStationsByCodeMap(); -// int size = 1+31; -// stations = new ArrayList();//String[]: station, uri0, ...uri31 -// for (Map.Entry entry : stationMap.entrySet()) { -// List astation = new ArrayList(); -// 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 getCoeffW(String station) { - Map wCoeff = new HashMap(); - - List 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 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 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 newArray = new ArrayList(); - 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; - } -} - diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/handler/RetrieveHrAvgRequestHandler.java b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/handler/RetrieveHrAvgRequestHandler.java new file mode 100644 index 0000000000..7879797c8f --- /dev/null +++ b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/handler/RetrieveHrAvgRequestHandler.java @@ -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 + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * 2014/02/12   #1110      qzhou       Init
+ * 
+ * 
+ * + * @author qzhou + * @version 1.0 + */ +public class RetrieveHrAvgRequestHandler implements + IRequestHandler { + + private static Logger logger = Logger + .getLogger(RetrieveHrAvgRequestHandler.class.toString()); + + private GeoMagAvgDao dao; + + @Override + public Object handleRequest(RetrieveHrAvgRequest request) throws Exception { + List 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; + } +} diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/handler/RetrieveK1minRequestHandler.java b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/handler/RetrieveK1minRequestHandler.java new file mode 100644 index 0000000000..12190bce7c --- /dev/null +++ b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/src/gov/noaa/nws/ncep/edex/plugin/geomag/handler/RetrieveK1minRequestHandler.java @@ -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 + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * 2014/02/12   #1110      qzhou       Init
+ * 
+ * 
+ * + * @author qzhou + * @version 1.0 + */ +public class RetrieveK1minRequestHandler implements + IRequestHandler { + + private static Logger logger = Logger + .getLogger(RetrieveK1minRequestHandler.class.toString()); + + private GeoMagK1minDao dao; + + @Override + public Object handleRequest(RetrieveK1minRequest request) throws Exception { + List 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; + } +} diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/utility/common_static/base/purge/geomagPurgeRules.xml b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/utility/common_static/base/purge/geomagPurgeRules.xml index ebe800b2e3..c85755e9ac 100644 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/utility/common_static/base/purge/geomagPurgeRules.xml +++ b/ncep/gov.noaa.nws.ncep.edex.plugin.geomag/utility/common_static/base/purge/geomagPurgeRules.xml @@ -1,6 +1,6 @@ - 30-00:00:00 + 60-00:00:00 diff --git a/ncep/gov.noaa.nws.ncep.edex.plugin.solarimage/src/gov/noaa/nws/ncep/edex/plugin/solarimage/SolarImageDecoder.java b/ncep/gov.noaa.nws.ncep.edex.plugin.solarimage/src/gov/noaa/nws/ncep/edex/plugin/solarimage/SolarImageDecoder.java index f006244bc0..d3a83ec4e0 100644 --- a/ncep/gov.noaa.nws.ncep.edex.plugin.solarimage/src/gov/noaa/nws/ncep/edex/plugin/solarimage/SolarImageDecoder.java +++ b/ncep/gov.noaa.nws.ncep.edex.plugin.solarimage/src/gov/noaa/nws/ncep/edex/plugin/solarimage/SolarImageDecoder.java @@ -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). * * * @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); } diff --git a/ncep/gov.noaa.nws.ncep.viz.rsc.solarimage/localization/ncep/styleRules/solarImageryStyleRules.xml b/ncep/gov.noaa.nws.ncep.viz.rsc.solarimage/localization/ncep/styleRules/solarImageryStyleRules.xml index 85e57fe010..88f003471e 100644 --- a/ncep/gov.noaa.nws.ncep.viz.rsc.solarimage/localization/ncep/styleRules/solarImageryStyleRules.xml +++ b/ncep/gov.noaa.nws.ncep.viz.rsc.solarimage/localization/ncep/styleRules/solarImageryStyleRules.xml @@ -768,12 +768,12 @@ - 400 - 16000 + -300 + 300 SolarImage/Gray - 2000 6000 10000 14000 + -200 -100 0 100 200 @@ -784,16 +784,15 @@ - 400 - 16000 + -600 + 600 SolarImage/Gray - 2000 6000 10000 14000 + -400 -200 0 200 400 - + - SECCHI diff --git a/ncep/gov.noaa.nws.ncep.viz.rsc.solarimage/src/gov/noaa/nws/ncep/viz/rsc/solarimage/wcs/WCSConverter.java b/ncep/gov.noaa.nws.ncep.viz.rsc.solarimage/src/gov/noaa/nws/ncep/viz/rsc/solarimage/wcs/WCSConverter.java index 438a4690d4..41fc9eabab 100644 --- a/ncep/gov.noaa.nws.ncep.viz.rsc.solarimage/src/gov/noaa/nws/ncep/viz/rsc/solarimage/wcs/WCSConverter.java +++ b/ncep/gov.noaa.nws.ncep.viz.rsc.solarimage/src/gov/noaa/nws/ncep/viz/rsc/solarimage/wcs/WCSConverter.java @@ -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 * * * @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++) {