/*
 * Copyright 2014 Patrik Karlsson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.trixon.toolbox.io.file.asc;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Patrik Karlsson <patrik@trixon.se>
 */
public class Asc {

    private AscHeader mAscHeader;
    private Charset mCharset = Charset.forName("US-ASCII");
    private double[][] mData;
    private Path mPath;
    private BufferedReader mReader;

    public Asc() {
        mAscHeader = new AscHeader();
    }

    public Asc(Charset charset) {
        this();
        mCharset = charset;
    }

    public static FileNameExtensionFilter getFileNameExtensionFilter() {
        return new FileNameExtensionFilter("*.asc", "asc");
    }

    public double[][] getData() {
        return mData;
    }

    public File getFile() {
        return mPath.toFile();
    }

    public AscHeader getHeader() {
        return mAscHeader;
    }

    public Path getPath() {
        return mPath;
    }

    public boolean isValid() {
        return (mAscHeader != null
                && mAscHeader.getNcols() > 0
                && mAscHeader.getNrows() > 0
                && mAscHeader.getCellSize() > Double.MIN_VALUE
                && mAscHeader.getXllcorner() > Double.MIN_VALUE
                && mAscHeader.getYllcorner() > Double.MIN_VALUE);
    }

    public void read(File file, boolean... headerOnly) throws IOException, NumberFormatException {
        boolean quickRead = headerOnly.length > 0 && headerOnly[0] == true;

        if (file.isFile() && file.exists()) {
            mPath = file.toPath();
            mReader = Files.newBufferedReader(mPath, mCharset);
            mAscHeader = new AscHeader(mReader, mPath);
            if (!quickRead) {
                mData = new double[mAscHeader.getNcols()][mAscHeader.getNrows()];
                for (int i = 0; i < mAscHeader.getNrows(); i++) {
                    String[] rowData = mReader.readLine().split(" ");
                    for (int j = 0; j < mAscHeader.getNcols(); j++) {
                        mData[i][j] = Double.parseDouble(rowData[j]);
                    }
                }
            }
        } else {
            throw new IOException();
        }
    }

    public void readHeader(File file) throws IOException, NumberFormatException {
        read(file, true);
    }

    public void setData(double[][] data) {
        mData = data;
    }
}

@@ -665,16 +665,47 @@
 Reader;%0A
+import java.io.BufferedWriter;%0A
 import j
@@ -717,16 +717,16 @@
 o.File;%0A
-
 import j
@@ -833,16 +833,41 @@
 e.Path;%0A
+import java.util.Locale;%0A
 import j
@@ -1175,16 +1175,52 @@
 mReader;
+%0A    private BufferedWriter mWriter;
 %0A%0A    pu
@@ -1494,32 +1494,115 @@
  %22asc%22);%0A    %7D%0A%0A
+    public void closeWriter() throws IOException %7B%0A        mWriter.close();%0A    %7D%0A%0A
     public doubl
@@ -2188,32 +2188,857 @@
 _VALUE);%0A    %7D%0A%0A
+    public void openWriter(File file) throws IOException %7B%0A        StringBuilder builder = new StringBuilder();%0A        builder.append(String.format(Locale.ENGLISH, %22ncols %25d%5Cr%5Cn%22, mAscHeader.getNcols()));%0A        builder.append(String.format(Locale.ENGLISH, %22nrows %25d%5Cr%5Cn%22, mAscHeader.getNrows()));%0A        builder.append(String.format(Locale.ENGLISH, %22xllcorner %25f%5Cr%5Cn%22, mAscHeader.getXllcorner()));%0A        builder.append(String.format(Locale.ENGLISH, %22yllcorner %25f%5Cr%5Cn%22, mAscHeader.getYllcorner()));%0A        builder.append(String.format(Locale.ENGLISH, %22cellsize %25f%5Cr%5Cn%22, mAscHeader.getCellSize()));%0A        builder.append(String.format(Locale.ENGLISH, %22nodata_value %25f%5Cr%5Cn%22, mAscHeader.getNodata()));%0A%0A        mWriter = Files.newBufferedWriter(file.toPath(), mCharset);%0A        mWriter.write(builder.toString());%0A    %7D%0A%0A
     public void 
@@ -4065,24 +4065,24 @@
 %5D%5B%5D data) %7B%0A
-
         mDat
@@ -4096,11 +4096,496 @@
 a;%0A    %7D
+%0A%0A    public void write(File file) throws IOException %7B%0A        openWriter(file);%0A%0A        for (int row = 0; row %3C mData.length; row++) %7B%0A            StringBuilder builder = new StringBuilder();%0A%0A            for (double value : mData%5Brow%5D) %7B%0A                builder.append(value).append(%22 %22);%0A            %7D%0A%0A            builder.deleteCharAt(builder.length() - 1);%0A            builder.append(%22%5Cr%5Cn%22);%0A            mWriter.write(builder.toString());%0A        %7D%0A        closeWriter();%0A    %7D
 %0A%7D%0A

Save asc