001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2024
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas/
009 * 
010 * All Rights Reserved
011 * 
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.  
014 * 
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 * 
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023 * GNU Lesser Public License for more details.
024 * 
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program.  If not, see https://www.gnu.org/licenses/.
027 */
028
029package edu.wisc.ssec.mcidasv.adt;
030
031import java.io.DataInputStream;
032import java.io.IOException;
033import java.io.InputStream;
034
035public class Topo {
036
037    public Topo() {
038    }
039
040    public static int ReadTopoFile(String topofile, double inputlat, double inputlon)
041            throws IOException {
042
043        int num_lon_elev = 5760;
044        int num_lat_elev = 2880;
045        double first_lon_elev = 0.0;
046        double first_lat_elev = 90.0;
047        double last_lon_elev = 360.0;
048        double last_lat_elev = -90.0;
049
050        double ax = inputlat;
051        double bx = inputlon; /* to make mcidas compliant */
052        System.out.printf("TOPO: lat: %f  lon: %f\n", ax, bx);
053
054        InputStream filestream = Topo.class.getResourceAsStream(topofile);
055        DataInputStream dis = new DataInputStream(filestream);
056
057        double del_lon_elev = (last_lon_elev - first_lon_elev) / num_lon_elev;
058        double del_lat_elev = (first_lat_elev - last_lat_elev) / num_lat_elev;
059        System.out.printf("TOPO: dlat: %f  dlon: %f\n", del_lon_elev, del_lat_elev);
060
061        int ay = (int) ((90.0 - ax) / del_lat_elev);
062        if (bx < 0.0)
063            bx = bx + 360.0;
064        int by = (int) (bx / del_lon_elev);
065        System.out.printf("TOPO: lat: %d  lon: %d \n", ay, by);
066        long position = (long) (2 * ((ay * ((double) num_lon_elev)) + by));
067        System.out.printf("TOPO: position=%d\n", position);
068        // filestream.seek(position+1);
069        dis.skip(position);
070        System.err.println("After skip, about to read val...");
071
072        int i = dis.readShort();
073        System.err.println("After read, val: " + i);
074
075        int ichar = (i == 0) ? 2 : 1;
076        System.err.println("After read, returning: " + ichar);
077        System.out.printf("TOPO: position=%d Value=%d landflag=%d \n ", position, i, ichar);
078        filestream.close();
079
080        return ichar;
081    }
082
083}