<commit_before>package org.agmip.translators.dssat;

import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import org.agmip.core.types.AdvancedHashMap;

/**
 * DSSAT Weather Data I/O API Class
 * 
 * @author <NAME>
 * @version 1.0
 */
public class DssatWeatherInput extends DssatCommonInput {

    /**
     * Constructor with no parameters
     * Set jsonKey as "weather"
     * 
     */
    public DssatWeatherInput() {
        super();
        jsonKey = "weather";
    }

    /**
     * DSSAT Weather Data input method for Controller using
     * 
     * @param brMap  The holder for BufferReader objects for all files
     * @return result data holder object
     */
    @Override
    protected AdvancedHashMap readFile(HashMap brMap) throws IOException {

        AdvancedHashMap ret = new AdvancedHashMap();
        ArrayList files = new ArrayList();
        ArrayList daily = new ArrayList();
        ArrayList titles = new ArrayList();
        AdvancedHashMap file;
        String line;
        BufferedReader brW = null;
        char[] buf;
        LinkedHashMap mapW;
        LinkedHashMap formats = new LinkedHashMap();
        String dailyKey = "data";  // TODO the key name might change

        mapW = (LinkedHashMap) brMap.get("W");

        // If Weather File is no been found
        if (mapW.isEmpty()) {
            // TODO reprot file not exist error
            return ret;
        }

        for (Object key : mapW.keySet()) {

            buf = (char[]) mapW.get(key);
            brW = new BufferedReader(new CharArrayReader(buf));
            file = new AdvancedHashMap();
            daily = new ArrayList();
            titles = new ArrayList();
            files.add(file);
            file.put(dailyKey, daily);

            while ((line = brW.readLine()) != null) {

                // Get content type of line
                judgeContentType(line);

                // Read Weather File Info
                if (flg[0].equals("weather") && flg[1].equals("") && flg[2].equals("data")) {

                    // header info
                    file.put("wst_name", line.replaceFirst("\\*[Ww][Ee][Aa][Tt][Hh][Ee][Rr]\\s*[Dd][Aa][Tt][Aa]\\s*:?", "").trim());

                } // Read Weather Data
                else if (flg[2].equals("data")) {

                    // Weather station info
                    if (flg[1].contains("insi ")) {

                        // Set variables' formats
                        formats.clear();
                        formats.put("wst_insi", 6);
                        formats.put("wst_lat", 9);
                        formats.put("wst_long", 9);
                        formats.put("elev", 6);
                        formats.put("tav", 6);
                        formats.put("tamp", 6);
                        formats.put("refht", 6);
                        formats.put("wndht", 6);
                        // Read line and save into return holder
                        file.put(readLine(line, formats));

                    } // Weather daily data
                    else if (flg[1].startsWith("date ")) {

                        // Set variables' formats
                        formats.clear();
                        formats.put("w_date", 5);
                        for (int i = 0; i < titles.size(); i++) {
                            formats.put(titles.get(i), 6);
                        }
                        // TODO wait for confirmation about how to handle the unknown field in the daily section
//                        formats.put("srad", 6);
//                        formats.put("tmax", 6);
//                        formats.put("tmin", 6);
//                        formats.put("rain", 6);
//                        formats.put("tdew", 6);
//                        formats.put("wind", 6);
//                        formats.put("pard", 6);
                        // Read line and save into return holder
                        AdvancedHashMap tmp = readLine(line, formats);
                        // translate date from yyddd format to yyyymmdd format
                        tmp.put("w_date", translateDateStr((String) tmp.get("w_date")));
                        daily.add(tmp);

                    } else {
                    }
                } // Data Title Info<commit_msg>1. delete unused comment code<commit_after>package org.agmip.translators.dssat;

import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import org.agmip.core.types.AdvancedHashMap;

/**
 * DSSAT Weather Data I/O API Class
 * 
 * @author <NAME>
 * @version 1.0
 */
public class DssatWeatherInput extends DssatCommonInput {

    /**
     * Constructor with no parameters
     * Set jsonKey as "weather"
     * 
     */
    public DssatWeatherInput() {
        super();
        jsonKey = "weather";
    }

    /**
     * DSSAT Weather Data input method for Controller using
     * 
     * @param brMap  The holder for BufferReader objects for all files
     * @return result data holder object
     */
    @Override
    protected AdvancedHashMap readFile(HashMap brMap) throws IOException {

        AdvancedHashMap ret = new AdvancedHashMap();
        ArrayList files = new ArrayList();
        ArrayList daily = new ArrayList();
        ArrayList titles = new ArrayList();
        AdvancedHashMap file;
        String line;
        BufferedReader brW = null;
        char[] buf;
        LinkedHashMap mapW;
        LinkedHashMap formats = new LinkedHashMap();
        String dailyKey = "data";  // P.S. the key name might change

        mapW = (LinkedHashMap) brMap.get("W");

        // If Weather File is no been found
        if (mapW.isEmpty()) {
            // TODO reprot file not exist error
            return ret;
        }

        for (Object key : mapW.keySet()) {

            buf = (char[]) mapW.get(key);
            brW = new BufferedReader(new CharArrayReader(buf));
            file = new AdvancedHashMap();
            daily = new ArrayList();
            titles = new ArrayList();
            files.add(file);
            file.put(dailyKey, daily);

            while ((line = brW.readLine()) != null) {

                // Get content type of line
                judgeContentType(line);

                // Read Weather File Info
                if (flg[0].equals("weather") && flg[1].equals("") && flg[2].equals("data")) {

                    // header info
                    file.put("wst_name", line.replaceFirst("\\*[Ww][Ee][Aa][Tt][Hh][Ee][Rr]\\s*[Dd][Aa][Tt][Aa]\\s*:?", "").trim());

                } // Read Weather Data
                else if (flg[2].equals("data")) {

                    // Weather station info
                    if (flg[1].contains("insi ")) {

                        // Set variables' formats
                        formats.clear();
                        formats.put("wst_insi", 6);
                        formats.put("wst_lat", 9);
                        formats.put("wst_long", 9);
                        formats.put("elev", 6);
                        formats.put("tav", 6);
                        formats.put("tamp", 6);
                        formats.put("refht", 6);
                        formats.put("wndht", 6);
                        // Read line and save into return holder
                        file.put(readLine(line, formats));

                    } // Weather daily data
                    else if (flg[1].startsWith("date ")) {

                        // Set variables' formats
                        formats.clear();
                        formats.put("w_date", 5);
                        for (int i = 0; i < titles.size(); i++) {
                            formats.put(titles.get(i), 6);
                        }
                        // Read line and save into return holder
                        AdvancedHashMap tmp = readLine(line, formats);
                        // translate date from yyddd format to yyyymmdd format
                        tmp.put("w_date", translateDateStr((String) tmp.get("w_date")));
                        daily.add(tmp);

                    } else {
                    }
                } // Data Title Info
{'fraction_non_alphanumeric': 0.11770323504834666, 'fraction_numerical': 0.005491225975886356, 'mean_word_length': 1.384856248220894, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5043815', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2129, 'n_words': 568}