22
33import com .bc .fiduceo .core .Dimension ;
44import com .bc .fiduceo .core .Interval ;
5+ import com .bc .fiduceo .core .NodeType ;
56import com .bc .fiduceo .geometry .Polygon ;
67import com .bc .fiduceo .location .PixelLocator ;
78import com .bc .fiduceo .reader .AcquisitionInfo ;
89import com .bc .fiduceo .reader .time .TimeLocator ;
10+ import com .bc .fiduceo .reader .time .TimeLocator_MillisSince1970 ;
11+ import com .bc .fiduceo .util .NetCDFUtils ;
12+ import com .bc .fiduceo .util .TimeUtils ;
913import com .bc .fiduceo .util .VariableProxy ;
1014import org .esa .snap .core .util .StringUtils ;
1115import ucar .ma2 .Array ;
1519import ucar .nc2 .Attribute ;
1620import ucar .nc2 .Variable ;
1721
22+ import java .io .BufferedReader ;
1823import java .io .File ;
24+ import java .io .FileReader ;
1925import java .io .IOException ;
2026import java .util .ArrayList ;
2127import java .util .Calendar ;
28+ import java .util .Date ;
2229import java .util .List ;
2330
2431import static com .bc .fiduceo .util .NetCDFUtils .*;
@@ -39,18 +46,47 @@ class NdbcSMReader extends NdbcReader {
3946
4047 private static StationDatabase stationDatabase ;
4148
49+ private ArrayList <SmRecord > records ;
50+ private TimeLocator timeLocator ;
51+
4252 @ Override
4353 public void open (File file ) throws IOException {
4454 ensureStationDatabase ();
55+ loadStation (file , stationDatabase );
56+ parseFile (file );
4557 }
4658
4759 @ Override
4860 public void close () throws IOException {
61+ if (records != null ) {
62+ records .clear ();
63+ records = null ;
64+ }
65+
66+ station = null ;
4967 }
5068
5169 @ Override
5270 public AcquisitionInfo read () throws IOException {
53- throw new RuntimeException ("not implemented" );
71+ final AcquisitionInfo acquisitionInfo = new AcquisitionInfo ();
72+
73+ int minTime = Integer .MAX_VALUE ;
74+ int maxTime = Integer .MIN_VALUE ;
75+ for (final SmRecord record : records ) {
76+ if (record .utc < minTime ) {
77+ minTime = record .utc ;
78+ }
79+ if (record .utc > maxTime ) {
80+ maxTime = record .utc ;
81+ }
82+ }
83+
84+ acquisitionInfo .setSensingStart (new Date (minTime * 1000L ));
85+ acquisitionInfo .setSensingStop (new Date (maxTime * 1000L ));
86+
87+ acquisitionInfo .setNodeType (NodeType .UNDEFINED );
88+
89+ return acquisitionInfo ;
5490 }
5591
5692 @ Override
@@ -70,27 +106,81 @@ public PixelLocator getSubScenePixelLocator(Polygon sceneGeometry) throws IOExce
70106
71107 @ Override
72108 public TimeLocator getTimeLocator () throws IOException {
73- throw new RuntimeException ("not implemented" );
74- }
109+ if (timeLocator == null ) {
110+ createTimeLocator ();
111+ }
75112
76- @ Override
77- public int [] extractYearMonthDayFromFilename (String fileName ) {
78- throw new RuntimeException ("not implemented" );
113+ return timeLocator ;
79114 }
80115
81116 @ Override
82117 public Array readRaw (int centerX , int centerY , Interval interval , String variableName ) throws IOException , InvalidRangeException {
83- throw new RuntimeException ("not implemented" );
118+ final SmRecord record = records .get (centerY );
119+
120+ switch (variableName ) {
121+ case STATION_ID :
122+ final Array resultArray = Array .factory (DataType .STRING , new int []{1 , 1 });
123+ resultArray .setObject (0 , station .getId ());
124+ return resultArray ;
125+ case STATION_TYPE :
126+ final StationType type = station .getType ();
127+ return createResultArray (toByte (type ), -1 , DataType .BYTE , interval );
128+ case MEASUREMENT_TYPE :
129+ final MeasurementType measurementType = station .getMeasurementType ();
130+ return createResultArray (toByte (measurementType ), -1 , DataType .BYTE , interval );
131+ case LONGITUDE :
132+ return createResultArray (station .getLon (), Float .NaN , DataType .FLOAT , interval );
133+ case LATITUDE :
134+ return createResultArray (station .getLat (), Float .NaN , DataType .FLOAT , interval );
135+ case ANEMOMETER_HEIGHT :
136+ return createResultArray (station .getAnemometerHeight (), Float .NaN , DataType .FLOAT , interval );
137+ case AIR_TEMP_HEIGHT :
138+ return createResultArray (station .getAirTemperatureHeight (), Float .NaN , DataType .FLOAT , interval );
139+ case BAROMETER_HEIGHT :
140+ return createResultArray (station .getBarometerHeight (), Float .NaN , DataType .FLOAT , interval );
141+ case SST_DEPTH :
142+ return createResultArray (station .getSSTDepth (), Float .NaN , DataType .FLOAT , interval );
143+ case TIME :
144+ return createResultArray (record .utc , NetCDFUtils .getDefaultFillValue (int .class ), DataType .INT , interval );
145+ case WDIR :
146+ return createResultArray (record .windDir , 999 , DataType .SHORT , interval );
147+ case WSPD :
148+ return createResultArray (record .windSpeed , 99.f , DataType .FLOAT , interval );
149+ case GST :
150+ return createResultArray (record .gustSpeed , 99.f , DataType .FLOAT , interval );
151+ case WVHT :
152+ return createResultArray (record .waveHeight , 99.f , DataType .FLOAT , interval );
153+ case DPD :
154+ return createResultArray (record .domWavePeriod , 99.f , DataType .FLOAT , interval );
155+ case APD :
156+ return createResultArray (record .avgWavePeriod , 99.f , DataType .FLOAT , interval );
157+ case MWD :
158+ return createResultArray (record .waveDir , 999 , DataType .SHORT , interval );
159+ case PRES :
160+ return createResultArray (record .seaLevelPressure , 9999.f , DataType .FLOAT , interval );
161+ case ATMP :
162+ return createResultArray (record .airTemp , 999.f , DataType .FLOAT , interval );
163+ case DEWP :
164+ return createResultArray (record .dewPointTemp , 999.f , DataType .FLOAT , interval );
165+ case VIS :
166+ return createResultArray (record .visibility , 99.f , DataType .FLOAT , interval );
167+ case TIDE :
168+ return createResultArray (record .tideLevel , 99.f , DataType .FLOAT , interval );
169+ }
170+
171+ return null ;
84172 }
85173
86174 @ Override
87175 public Array readScaled (int centerX , int centerY , Interval interval , String variableName ) throws IOException , InvalidRangeException {
88- throw new RuntimeException ( "not implemented" );
176+ return readRaw ( centerX , centerY , interval , variableName );
89177 }
90178
91179 @ Override
92180 public ArrayInt .D2 readAcquisitionTime (int x , int y , Interval interval ) throws IOException , InvalidRangeException {
93- throw new RuntimeException ("not implemented" );
181+ final Array timeArray = readRaw (x , y , interval , TIME );
182+
183+ return (ArrayInt .D2 ) timeArray ;
94184 }
95185
96186 @ Override
@@ -190,17 +280,7 @@ public List<Variable> getVariables() throws InvalidRangeException, IOException {
190280
191281 @ Override
192282 public Dimension getProductSize () throws IOException {
193- throw new RuntimeException ("not implemented" );
194- }
195-
196- @ Override
197- public String getLongitudeVariableName () {
198- throw new RuntimeException ("not implemented" );
199- }
200-
201- @ Override
202- public String getLatitudeVariableName () {
203- throw new RuntimeException ("not implemented" );
283+ return new Dimension ("product_size" , 1 , records .size ());
204284 }
205285
206286 private void ensureStationDatabase () throws IOException {
@@ -239,4 +319,34 @@ SmRecord parseLine(String line, Calendar calendar) {
239319
240320 return record ;
241321 }
322+
323+ private void parseFile (File file ) throws IOException {
324+ records = new ArrayList <>();
325+ try (final FileReader fileReader = new FileReader (file )) {
326+ final Calendar calendar = TimeUtils .getUTCCalendar ();
327+ final BufferedReader bufferedReader = new BufferedReader (fileReader );
328+ String line ;
329+ while ((line = bufferedReader .readLine ()) != null ) {
330+ if (line .startsWith ("#" )) {
331+ // skip comment lines tb 2023-02-27
332+ continue ;
333+ }
334+
335+ final SmRecord smRecord = parseLine (line , calendar );
336+ records .add (smRecord );
337+ }
338+ }
339+ }
340+
341+ private void createTimeLocator () {
342+ long [] timeArray = new long [records .size ()];
343+
344+ int i = 0 ;
345+ for (final SmRecord record : records ) {
346+ timeArray [i ] = record .utc * 1000L ;
347+ i ++;
348+ }
349+
350+ timeLocator = new TimeLocator_MillisSince1970 (timeArray );
351+ }
242352}
0 commit comments