66import com .bc .fiduceo .geometry .Polygon ;
77import com .bc .fiduceo .location .PixelLocator ;
88import com .bc .fiduceo .reader .AcquisitionInfo ;
9+ import com .bc .fiduceo .reader .insitu .UniqueIdVariable ;
910import com .bc .fiduceo .reader .netcdf .NetCDFReader ;
1011import com .bc .fiduceo .reader .time .TimeLocator ;
1112import com .bc .fiduceo .util .TimeUtils ;
1213import ucar .ma2 .Array ;
1314import ucar .ma2 .ArrayInt ;
15+ import ucar .ma2 .DataType ;
1416import ucar .ma2 .InvalidRangeException ;
1517import ucar .nc2 .Variable ;
1618
1719import java .io .IOException ;
20+ import java .util .ArrayList ;
1821import java .util .Calendar ;
1922import java .util .List ;
2023
2124public class SirdsInsituReader extends NetCDFReader {
2225
2326 private static final String REGEX = "SSTCCI2_refdata_[a-z]+(_[a-z]+)?_\\ d{6}.nc" ;
27+ private static final String UNIQUE_ID = "unique_id" ;
28+
2429 private int [] timeMinMax ;
30+ private Array uniqueIdData ;
2531
2632 @ Override
2733 public AcquisitionInfo read () throws IOException {
@@ -91,12 +97,36 @@ public int[] extractYearMonthDayFromFilename(String fileName) {
9197
9298 @ Override
9399 public Array readRaw (int centerX , int centerY , Interval interval , String variableName ) throws IOException , InvalidRangeException {
94- throw new RuntimeException ("not implemented" );
100+ final Array sourceArray ;
101+ final Number fillValue ;
102+ if (variableName .equals (UNIQUE_ID )) {
103+ ensureUniqueIdData ();
104+ sourceArray = uniqueIdData ;
105+ fillValue = UniqueIdVariable .FILL_VALUE ;
106+ } else {
107+ sourceArray = arrayCache .get (variableName );
108+ fillValue = getFillValue (variableName );
109+ }
110+
111+ final int windowWidth = interval .getX ();
112+ final int windowHeight = interval .getY ();
113+ final int windowCenterX = windowWidth / 2 ;
114+ final int windowCenterY = windowHeight / 2 ;
115+
116+ final int [] shape = {windowWidth , windowHeight };
117+ final Array windowArray = Array .factory (sourceArray .getDataType (), shape );
118+ for (int y = 0 ; y < windowHeight ; y ++) {
119+ for (int x = 0 ; x < windowWidth ; x ++) {
120+ windowArray .setObject (windowWidth * y + x , fillValue );
121+ }
122+ }
123+ windowArray .setObject (windowWidth * windowCenterY + windowCenterX , sourceArray .getObject (centerY ));
124+ return windowArray ;
95125 }
96126
97127 @ Override
98128 public Array readScaled (int centerX , int centerY , Interval interval , String variableName ) throws IOException , InvalidRangeException {
99- throw new RuntimeException ( "not implemented" );
129+ return readRaw ( centerX , centerY , interval , variableName );
100130 }
101131
102132 @ Override
@@ -106,7 +136,27 @@ public ArrayInt.D2 readAcquisitionTime(int x, int y, Interval interval) throws I
106136
107137 @ Override
108138 public List <Variable > getVariables () throws InvalidRangeException , IOException {
109- throw new RuntimeException ("not implemented" );
139+ final List <Variable > fileVariables = netcdfFile .getVariables ();
140+ final List <Variable > listVariables = new ArrayList <>();
141+ for (final Variable variable : fileVariables ) {
142+ final String variableName = variable .getShortName ();
143+ if ("DAY" .equals (variableName ) ||
144+ "HOUR" .equals (variableName ) ||
145+ "MINUTE" .equals (variableName ) ||
146+ "MONTH" .equals (variableName ) ||
147+ "OB_ID" .equals (variableName ) ||
148+ "PLAT_ID" .equals (variableName ) ||
149+ "SECOND" .equals (variableName ) ||
150+ "YEAR" .equals (variableName )) {
151+ continue ;
152+ } else {
153+ listVariables .add (variable );
154+ }
155+ }
156+
157+ listVariables .add (new UniqueIdVariable (UNIQUE_ID ));
158+
159+ return listVariables ;
110160 }
111161
112162 @ Override
@@ -143,6 +193,34 @@ static int[] extractMinMax(int[] timeStamps) {
143193 @ Override
144194 public void close () throws IOException {
145195 timeMinMax = null ;
196+ uniqueIdData = null ;
146197 super .close ();
147198 }
199+
200+ private void ensureUniqueIdData () throws IOException {
201+ if (uniqueIdData != null ) {
202+ return ;
203+ }
204+
205+ final Array year = arrayCache .get ("YEAR" );
206+ final Array month = arrayCache .get ("MONTH" );
207+ final Array ob_id = arrayCache .get ("OB_ID" );
208+
209+ final int [] shape = year .getShape ();
210+ uniqueIdData = Array .factory (DataType .LONG , shape );
211+ final int fillValue = getFillValue ("OB_ID" ).intValue ();
212+ for (int i = 0 ; i < shape [0 ]; i ++) {
213+ final int id = ob_id .getInt (i );
214+ if (id == fillValue ) {
215+ uniqueIdData .setLong (i , UniqueIdVariable .FILL_VALUE );
216+ continue ;
217+ }
218+
219+ final int yearVal = year .getInt (i );
220+ final int monthVal = month .getInt (i );
221+ final int year_month = monthVal + yearVal * 100 ;
222+ final long uniqueId = (long ) id + (long ) year_month * 10000000000L ;
223+ uniqueIdData .setLong (i , uniqueId );
224+ }
225+ }
148226}
0 commit comments