FormattedOutput.java [src/java/lamps/io] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lamps.io;

import csip.SessionLogger;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import lamps.CSIP_Const;
import methods.objects.ERU;

/**
 * Output methods for various formats.
 *
 * @author od
 */
public class FormattedOutput {

    /**
     *
     * @param workspace
     * @param hrus
     * @param LOG
     * @throws IOException
     */
    public static void writeNASS(File workspace, List<ERU> hrus, SessionLogger LOG) throws IOException {
        //w_log.println("===>      AOI_NASS_Plain_result");
        LOG.info("===>      AOI_NASS_Plain_result");
        //w_log.flush();
        List<String> records = new ArrayList<>();
        records.add("# NASS CropScape Data \r\n");
        records.add("# Plain Data without confidence or accuracy \r\n");
        records.add("# For AOI in square meter ");
        records.add(",ID,YEAR,TOTAL_AREA_GRID,TOTAL_AREA_POLYGON,NAME,AREA \r\n");
        for (int i = 0; i < hrus.size(); i++) {
            List<String> NASS_list = hrus.get(i).nass_plain_list;
            for (String line : NASS_list) {
                records.add(line + " \r\n");
            }
        }
        try (final BufferedWriter buffWriter = new BufferedWriter(new FileWriter(new File(workspace, CSIP_Const.AOI_NASS_Plain_result)))) {
            for (String record : records) {
                buffWriter.write(record);
            }
        }
    }


    /**
     *
     * @param workspace
     * @param OP_map
     * @param OP_Till_Man
     * @throws IOException
     */
    public static void writeAGESTillageFile(File workspace, HashMap<String, String[]> OP_map, HashMap<String, Integer> OP_Till_Man) throws IOException {
        try (PrintWriter w = new PrintWriter(new FileWriter(new File(workspace, CSIP_Const.AOI_AGESW_TILLAGE)))) {
            for (String basic_non_crop_management_info : CSIP_Const.basic_non_crop_tillage_info) {
                w.println(basic_non_crop_management_info);
            }
            //        basic_non_crop_tillage_info.add("@H,TID,tillnm,desc,effmix,deptil");
            //        basic_non_crop_tillage_info.add(",1,FALLPLOW,GENERIC FALL PLOWING OPERATION,0.95,150");
            int i = CSIP_Const.TID_Start;
            for (String OPKey : OP_map.keySet()) {
                String[] till = OP_map.get(OPKey);
                w.println("," + i + "," + till[0] + "," + till[1] + "," + till[2] + "," + till[3]);
                OP_Till_Man.put(OPKey, i);
                i++;
            }
        }
    }


    /**
     *
     * @param workspace
     * @param hrus
     * @param OP_Till_Man
     * @param DB_map_managements
     * @param DB_map_managements_acr
     * @param LMOD_AGES_CID_map
     * @param LMOD_AGES_MID_map
     * @param only_veg_map
     * @throws IOException
     */
    public static void writeAGESManagementFile(File workspace, List<ERU> hrus, HashMap<String, Integer> OP_Till_Man, HashMap<String, ArrayList<String[]>> DB_map_managements, HashMap<String, String[]> DB_map_managements_acr, HashMap<String, String> LMOD_AGES_CID_map, HashMap<String, String> LMOD_AGES_MID_map, HashMap<String, String> only_veg_map, HashMap<String, String> only_landuse_map) throws IOException {

        HashMap<Integer, Integer> MID_RID_map = new HashMap<>();
        HashMap<String, Integer> ManKey_MID_map = new HashMap<>();
        PrintWriter w = new PrintWriter(new FileWriter(new File(workspace, CSIP_Const.AOI_AGESW_MANAGMENT)));
        PrintWriter w1 = new PrintWriter(new FileWriter(new File(workspace, CSIP_Const.AOI_AGESW_CROPROTATION)));

        for (String basic_non_crop_management_info : CSIP_Const.basic_non_crop_management_info) {
            w.println(basic_non_crop_management_info);
        }
        for (String basic_non_crop_management_info : CSIP_Const.basic_croprotation_info) {
            w1.println(basic_non_crop_management_info);
        }

        int rid = 1;
        for (String mid : LMOD_AGES_MID_map.keySet()) {
            w1.println("," + rid + "," + mid);
            if (MID_RID_map.get(Integer.parseInt(mid)) == null) {
                MID_RID_map.put(Integer.parseInt(mid), rid);
            }
            rid++;
        }
        int i = CSIP_Const.MID_Start;
        for (String key : DB_map_managements.keySet()) {
            String AgES_CID = LMOD_AGES_CID_map.get((DB_map_managements_acr.get(key)[6]).split(";")[0]);
            String old_veg = AgES_CID;
            if (old_veg == null || old_veg.equals("") || old_veg.isEmpty()) {
                System.out.println(" old WRONG " + key + " !!!!!!!!!!!!!");
                System.out.println(" Key: " + key + " DB_map_managements_acr[6].split(;)[0]: " + (DB_map_managements_acr.get(key)[6]).split(";")[0]);
                old_veg = "1";
            }
            ArrayList<String[]> Management_data = DB_map_managements.get(key);
            if (ManKey_MID_map.get(key) == null) {
                ManKey_MID_map.put(key, i);
            }
            for (int j = 0; j < Management_data.size(); j++) {
                //("@H,MID,CID,Date,TID,FID,FAmount,PLANT,HARVEST,FRACHARV,Description");
                String harv = Management_data.get(j)[4].equals("Harvest") ? "1" : "-";
                String seed = Management_data.get(j)[4].equals("Seeding") ? "1" : "-";
                String frac = Management_data.get(j)[6].equals("") ? "-" : Management_data.get(j)[6];
                String new_veg = "";
                if ((Management_data.get(j)[5].isEmpty() ? "-" : LMOD_AGES_CID_map.get(Management_data.get(j)[5])) != null) {
                    new_veg = Management_data.get(j)[5].isEmpty() ? "-" : LMOD_AGES_CID_map.get(Management_data.get(j)[5]);
                } else {
                    System.out.println(" new WRONG " + key + " !!!!!!!!!!!!!");
                }
                if (new_veg == null || new_veg.equals("") || new_veg.isEmpty()) {
                    System.out.println(" new WRONG " + key + " !!!!!!!!!!!!!");
                }
                String tid_op = OP_Till_Man.get(Management_data.get(j)[0]) == null ? "-" : OP_Till_Man.get(Management_data.get(j)[0]).toString();
                if (!old_veg.equals(new_veg) && !new_veg.equals("-")) {
                    // ckeck hrus NullPointerException
                    old_veg = new_veg;
                }
                if (harv.equals("1") && frac.equals("-")) {
                    frac = "0.9";
                }
                w.println("," + i + "," + old_veg + ", " + Management_data.get(j)[1].split("\\.")[1] + "-" + Management_data.get(j)[1].split("\\.")[2] + "," + tid_op + ",-,-," + seed + "," + harv + "," + frac + "," + Management_data.get(j)[3]);
            }
            w1.println("," + rid + "," + i);
            if (MID_RID_map.get(i) == null) {
                MID_RID_map.put(i, rid);
            }
            i++;
            rid++;
        }
        w.close();
        w1.close();

        w = new PrintWriter(new FileWriter(new File(workspace, CSIP_Const.AOI_AGESW_HRUS_ROT)));
        for (String basic_non_crop_management_info : CSIP_Const.basic_hrurotation_info) {
            w.println(basic_non_crop_management_info);
        }
        //hrus and rid binding here
        for (int a = 0; a < hrus.size(); a++) {
            String Irri = "0";
            if (hrus.get(a).irrigated) {
                Irri = "1";
            }
            if (hrus.get(a).Matching_Result.Rotation) {
                Integer mid = ManKey_MID_map.get(hrus.get(a).Matching_Result.LMOD_Key);
                w.println("," + hrus.get(a).ID + "," + MID_RID_map.get(mid) + ",1," + Irri + ",7");
            } else {
                w.println("," + hrus.get(a).ID + "," + only_veg_map.get(hrus.get(a).Matching_Result.Vegetation_Sequence) + ",1," + Irri + "," + only_landuse_map.get(hrus.get(a).Matching_Result.Vegetation_Sequence));
            }
        }
        w.close();
    }

}