|
| 1 | +/**+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 2 | +
|
| 3 | +This file is part of FORCE - Framework for Operational Radiometric |
| 4 | +Correction for Environmental monitoring. |
| 5 | +
|
| 6 | +Copyright (C) 2013-2021 David Frantz |
| 7 | +
|
| 8 | +FORCE is free software: you can redistribute it and/or modify |
| 9 | +it under the terms of the GNU General Public License as published by |
| 10 | +the Free Software Foundation, either version 3 of the License, or |
| 11 | +(at your option) any later version. |
| 12 | +
|
| 13 | +FORCE is distributed in the hope that it will be useful, |
| 14 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +GNU General Public License for more details. |
| 17 | +
|
| 18 | +You should have received a copy of the GNU General Public License |
| 19 | +along with FORCE. If not, see <http://www.gnu.org/licenses/>. |
| 20 | +
|
| 21 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/ |
| 22 | + |
| 23 | +/**+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 24 | +This program initializes a datacube-definition.prj |
| 25 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/ |
| 26 | + |
| 27 | + |
| 28 | +#include <stdio.h> // core input and output functions |
| 29 | +#include <stdlib.h> // standard general utilities library |
| 30 | + |
| 31 | +#include <ctype.h> // testing and mapping characters |
| 32 | +#include <unistd.h> // standard symbolic constants and types |
| 33 | + |
| 34 | +#include "../cross-level/const-cl.h" |
| 35 | +#include "../cross-level/konami-cl.h" |
| 36 | +#include "../cross-level/cube-cl.h" |
| 37 | +//#include "../cross-level/warp-cl.h" |
| 38 | +#include "../lower-level/param-ll.h" |
| 39 | +#include "../lower-level/cube-ll.h" |
| 40 | + |
| 41 | + |
| 42 | +typedef struct { |
| 43 | + int n; |
| 44 | + double geo[2]; // lon/lat |
| 45 | + double tilesize; |
| 46 | + double chunksize; |
| 47 | + char dcube[NPOW_10]; |
| 48 | + char proj[NPOW_10]; |
| 49 | +} args_t; |
| 50 | + |
| 51 | + |
| 52 | +void usage(char *exe, int exit_code){ |
| 53 | + |
| 54 | + |
| 55 | + printf("Usage: %s [-h] [-v] [-i] [-d datacube-dir] [-o lon/lat] \n", exe); |
| 56 | + printf(" [-t tile-size] [-c chunk-size] projection\n"); |
| 57 | + printf("\n"); |
| 58 | + printf(" -h = show this help\n"); |
| 59 | + printf(" -v = show version\n"); |
| 60 | + printf(" -i = show program's purpose\n"); |
| 61 | + printf("\n"); |
| 62 | + printf(" -d datacube-dir = output directory for datacube definition\n"); |
| 63 | + printf(" default: current working directory\n"); |
| 64 | + printf("\n"); |
| 65 | + printf(" -o lon,lat = origin coordinates of the grid\n"); |
| 66 | + printf(" use geographic coordinates!\n"); |
| 67 | + printf(" longitude is X!\n"); |
| 68 | + printf(" latitude is Y!\n"); |
| 69 | + printf(" default: -25,60, is ignored for pre-defined projections!\n"); |
| 70 | + printf("\n"); |
| 71 | + printf(" -t tile-size\n"); |
| 72 | + printf(" default: 30km, is ignored for pre-defined projections!\n"); |
| 73 | + printf("\n"); |
| 74 | + printf(" -c chunk-size\n"); |
| 75 | + printf(" default: 3km, is ignored for pre-defined projections!\n"); |
| 76 | + printf("\n"); |
| 77 | + printf(" Positional arguments:\n"); |
| 78 | + printf(" - Projection (custom WKT string or built-in projection\n"); |
| 79 | + printf("\n"); |
| 80 | + |
| 81 | + exit(exit_code); |
| 82 | + return; |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +void parse_args(int argc, char *argv[], args_t *args){ |
| 87 | +int opt; |
| 88 | +char buffer[NPOW_10]; |
| 89 | +char *ptr = NULL; |
| 90 | +const char *separator = ","; |
| 91 | +int i; |
| 92 | + |
| 93 | + |
| 94 | + opterr = 0; |
| 95 | + |
| 96 | + // default parameters |
| 97 | + args->tilesize = 30000; |
| 98 | + args->chunksize = 3000; |
| 99 | + args->geo[_X_] = -25; |
| 100 | + args->geo[_Y_] = 60; |
| 101 | + copy_string(args->dcube, 1024, "."); |
| 102 | + |
| 103 | + // optional parameters |
| 104 | + while ((opt = getopt(argc, argv, "hvid:o:t:c:")) != -1){ |
| 105 | + switch(opt){ |
| 106 | + case 'h': |
| 107 | + usage(argv[0], SUCCESS); |
| 108 | + case 'v': |
| 109 | + printf("FORCE version: %s\n", _VERSION_); |
| 110 | + exit(SUCCESS); |
| 111 | + case 'i': |
| 112 | + printf("Initialize a datacube definition\n"); |
| 113 | + exit(SUCCESS); |
| 114 | + case 'd': |
| 115 | + copy_string(args->dcube, NPOW_10, optarg); |
| 116 | + break; |
| 117 | + case 'o': |
| 118 | + copy_string(buffer, NPOW_10, optarg); |
| 119 | + ptr = strtok(buffer, separator); |
| 120 | + i = 0; |
| 121 | + while (ptr != NULL){ |
| 122 | + if (i < 2) args->geo[i] = atof(ptr); |
| 123 | + ptr = strtok(NULL, separator); |
| 124 | + i++; |
| 125 | + } |
| 126 | + if (i != 2){ |
| 127 | + fprintf(stderr, "Coordinate must have 2 numbers.\n"); |
| 128 | + usage(argv[0], FAILURE); |
| 129 | + } |
| 130 | + break; |
| 131 | + case 't': |
| 132 | + args->tilesize = atof(optarg); |
| 133 | + if (args->tilesize <= 0){ |
| 134 | + fprintf(stderr, "Tile size must be > 0.\n"); |
| 135 | + usage(argv[0], FAILURE); |
| 136 | + } |
| 137 | + break; |
| 138 | + case 'c': |
| 139 | + args->chunksize = atof(optarg); |
| 140 | + if (args->chunksize <= 0){ |
| 141 | + fprintf(stderr, "Chunk size must be > 0.\n"); |
| 142 | + usage(argv[0], FAILURE); |
| 143 | + } |
| 144 | + break; |
| 145 | + case '?': |
| 146 | + if (isprint(optopt)){ |
| 147 | + fprintf(stderr, "Unknown option `-%c'.\n", optopt); |
| 148 | + } else { |
| 149 | + fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); |
| 150 | + } |
| 151 | + usage(argv[0], FAILURE); |
| 152 | + default: |
| 153 | + fprintf(stderr, "Error parsing arguments.\n"); |
| 154 | + usage(argv[0], FAILURE); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // non-optional parameters |
| 159 | + args->n = 1; |
| 160 | + |
| 161 | + if (optind < argc){ |
| 162 | + konami_args(argv[optind]); |
| 163 | + if (argc-optind == args->n){ |
| 164 | + copy_string(args->proj, NPOW_10, argv[optind++]); |
| 165 | + } else if (argc-optind < args->n){ |
| 166 | + fprintf(stderr, "some non-optional arguments are missing.\n"); |
| 167 | + usage(argv[0], FAILURE); |
| 168 | + } else if (argc-optind > args->n){ |
| 169 | + fprintf(stderr, "too many non-optional arguments.\n"); |
| 170 | + usage(argv[0], FAILURE); |
| 171 | + } |
| 172 | + } else { |
| 173 | + fprintf(stderr, "non-optional arguments are missing.\n"); |
| 174 | + usage(argv[0], FAILURE); |
| 175 | + } |
| 176 | + |
| 177 | + return; |
| 178 | +} |
| 179 | + |
| 180 | + |
| 181 | +int main(int argc, char *argv[]){ |
| 182 | +args_t args; |
| 183 | +par_ll_t pl2; |
| 184 | +multicube_t *multicube = NULL; |
| 185 | + |
| 186 | + |
| 187 | + parse_args(argc, argv, &args); |
| 188 | + |
| 189 | + pl2.res = 1; |
| 190 | + pl2.doreproj = true; |
| 191 | + pl2.dotile = true; |
| 192 | + |
| 193 | + pl2.d_level2 = args.dcube; |
| 194 | + copy_string(pl2.proj, NPOW_10, args.proj); |
| 195 | + pl2.tilesize = args.tilesize; |
| 196 | + pl2.chunksize = args.chunksize; |
| 197 | + pl2.orig_lon = args.geo[_X_]; |
| 198 | + pl2.orig_lat = args.geo[_Y_]; |
| 199 | + |
| 200 | + if ((multicube = start_multicube(&pl2, NULL)) == NULL){ |
| 201 | + printf("Starting datacube(s) failed.\n"); return FAILURE;} |
| 202 | + |
| 203 | + free_multicube(multicube); |
| 204 | + |
| 205 | + return SUCCESS; |
| 206 | +} |
| 207 | + |
0 commit comments