001/* 002 * This file is part of McIDAS-V 003 * 004 * Copyright 2007-2023 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 http://www.gnu.org/licenses. 027 */ 028 029package edu.wisc.ssec.mcidasv.data.hydra; 030 031import java.util.Comparator; 032 033/** 034 * @author tommyj 035 * Sorts NOAA format VIIRS variable names. For example, ensures: 036 * 037 * All_Data/VIIRS-M2-SDR_All/Radiance 038 * 039 * appears before: 040 * 041 * All_Data/VIIRS-M16-SDR_All/Radiance 042 * 043 * Using natural ordering, it will not, since the band numbers are 044 * not zero-padded. 045 * 046 */ 047 048public class VIIRSSort implements Comparator<String> { 049 050 @Override 051 public int compare(String v1, String v2) { 052 int result = 0; 053 String band1 = null; 054 String band2 = null; 055 int index1 = -1; 056 int index2 = -1; 057 058 // Do the regular natural ordering check first 059 if ((v1 != null) && (v2 != null)) { 060 061 result = v1.compareTo(v2); 062 063 // Assume caller is testing on NOAA-format VIIRS data, 064 // but do some basic checks just in case. If true, 065 // apply a further filter based on Band/Product token 066 067 if ((v1.contains("VIIRS")) && (v2.contains("VIIRS"))) { 068 069 // pull band out of 1st variable name 070 index1 = v1.indexOf('-'); 071 index2 = v1.indexOf('-', index1 + 1); 072 073 // band or product is there if both indices are non-negative 074 if ((index1 >= 0) && (index2 >= 0)) { 075 band1 = v1.substring(index1 + 1, index2); 076 } 077 078 // pull band out of 2nd variable name 079 index1 = v2.indexOf('-'); 080 index2 = v2.indexOf('-', index1 + 1); 081 082 // band or product is there if both indices are non-negative 083 if ((index1 >= 0) && (index2 >= 0)) { 084 band2 = v2.substring(index1 + 1, index2); 085 } 086 087 // if band variables are still null, we can try one more thing - 088 // an underscore instead of dash for delimiter in variable name 089 if ((band1 == null) && (band2 == null)) { 090 091 // pull band out of 1st variable name 092 index1 = v1.indexOf('_'); 093 index2 = v1.indexOf('_', index1 + 1); 094 095 // band or product is there if both indices are non-negative 096 if ((index1 >= 0) && (index2 >= 0)) { 097 band1 = v1.substring(index1 + 1, index2); 098 } 099 100 // pull band out of 2nd variable name 101 index1 = v2.indexOf('_'); 102 index2 = v2.indexOf('_', index1 + 1); 103 104 // band or product is there if both indices are non-negative 105 if ((index1 >= 0) && (index2 >= 0)) { 106 band2 = v2.substring(index1 + 1, index2); 107 } 108 109 } 110 111 if ((band1 != null) && (band2 != null)) { 112 // zero pad if needed 113 if (band1.length() == 2) { 114 band1 = String.join("0", band1.substring(0, 1), band1.substring(1)); 115 } 116 if (band2.length() == 2) { 117 band2 = String.join("0", band2.substring(0, 1), band2.substring(1)); 118 } 119 result = band1.compareTo(band2); 120 } 121 } 122 } 123 124 return result; 125 } 126 127}