/* * [CleanBoC.java] * * Summary: Bank Of Canada on 2011-12-29 screwed up the file format of the daily exchange rates. * * Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.1 2015-03-30 handle _NOON suffix */ package com.mindprod.currcon; import com.mindprod.common18.EIO; import com.mindprod.common18.ST; import com.mindprod.csv.CSVReader; import com.mindprod.csv.CSVWriter; import java.io.EOFException; import java.io.File; import java.io.IOException; import static java.lang.System.*; /** * Bank Of Canada on 2011-12-29 screwed up the file format of the daily exchange rates. *

* This program repairs the damage. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2015-03-30 handle _NOON suffix * @since 2011-12-29 */ @SuppressWarnings( { "InfiniteLoopStatement" } ) public final class CleanBoC { private static final int FIRST_COPYRIGHT_YEAR = 2011; /** * undisplayed copyright notice */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; @SuppressWarnings( { "UnusedDeclaration" } ) private static final String RELEASE_DATE = "2011-12-29"; /** * embedded version string. */ private static final String VERSION_STRING = "1.0"; /** * Tidy boc-stripped.csv to boc-clean.csv * * @param args not used */ public static void main( String[] args ) { try { final CSVReader r = new CSVReader( EIO.getBufferedReader( new File( "boc-stripped.csv" ), 4 * 1024, EIO.UTF8 ) ); final CSVWriter w = new CSVWriter( EIO.getPrintWriter( new File( "boc-clean.csv" ), 4 * 1024, EIO.UTF8 ) ); try { // Then by stripping out all but cols 1 and 8 with CSVReshape it is converted to: //# The daily noon exchange rates for major foreign currencies are published every business day at // about 12:30 //# p.m. EST. They are obtained from market or official sources around noon and show the rates for the //# various currencies in Canadian dollars converted from US dollars. The rates are nominal quotations - //# neither buying nor selling rates - and are intended for statistical or analytical purposes. Rates //# available from financial institutions will differ. //# // ISO4217,2012-01-05 // USD,1.0197 <-- relative to Canada // USD,1.0191 // IEXE0124,0.21 // IEXE0125,0.39 // ARS,0.2127 // AUD,Bank holiday // Then using CleanBoC, we clean it to: // USD, 1.0197 // ARS, 0.2127 r.getAllFieldsInLine(); // bypass date line, skipToNextLine confused by comments. String[] fields = r.getAllFieldsInLine(); w.put( "USD" ); w.put( fields[ 1 ] ); w.nl(); r.skipToNextLine(); // bypass USD close while ( true ) { // include everything except IEXE record final String currCode = ST.chopTrailingString( r.get(), "_NOON" ); if ( !currCode.startsWith( "IEXE" ) ) { final String rate = r.get(); // might be malformed eg. "Not available", "Bank Holiday" try { Double.parseDouble( rate ); w.put( currCode ); w.put( rate ); w.nl(); } catch ( NumberFormatException e ) { err.println( "ignoring: " + currCode + ", " + rate ); } } r.skipToNextLine(); } } catch ( EOFException e ) { r.close(); w.close(); } } catch ( IOException e ) { err.println(); e.printStackTrace( err ); err.println(); } } }