check_ready.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #include "global.h"
  2. int check_ready(void)
  3. {
  4. int retval;
  5. flg.overwrlist = 0;
  6. retval = 0;
  7. if (check_required() || check_names() || check_io())
  8. retval = 1;
  9. if (!retval) {
  10. if (flg.overwrlist & FILL)
  11. G_remove("cell", map.fill);
  12. if (flg.overwrlist & DIR)
  13. G_remove("cell", map.dir);
  14. if (flg.overwrlist & BELEV)
  15. G_remove("cell", map.belev);
  16. if (flg.overwrlist & TOPIDX)
  17. G_remove("cell", map.topidx);
  18. if (flg.overwrlist & IDXSTATS)
  19. unlink(file.idxstats);
  20. if (flg.overwrlist & OUTPUT)
  21. unlink(file.output);
  22. }
  23. return retval;
  24. }
  25. int check_required(void)
  26. {
  27. int retval;
  28. retval = 0;
  29. if (!flg.input) {
  30. if (!map.elev) {
  31. G_warning("elevation required");
  32. retval = 1;
  33. }
  34. if (!map.basin) {
  35. G_warning("basin required");
  36. retval = 1;
  37. }
  38. if (!map.belev) {
  39. G_warning("belevation required");
  40. retval = 1;
  41. }
  42. if (!map.topidx) {
  43. G_warning("topidx required");
  44. retval = 1;
  45. }
  46. if (map.fill && !map.dir) {
  47. G_warning("direction required " "if depressionless is given");
  48. retval = 1;
  49. }
  50. if (map.dir && !map.fill) {
  51. G_warning("depressionless required " "if direction is given");
  52. retval = 1;
  53. }
  54. }
  55. else {
  56. if (map.belev && !map.topidx) {
  57. G_warning("topidx required " "if belevation is given");
  58. retval = 1;
  59. }
  60. }
  61. return retval;
  62. }
  63. int check_names(void)
  64. {
  65. int retval;
  66. retval = 0;
  67. if (!flg.input) {
  68. if (map.elev) {
  69. if (map.basin && !strcmp(map.elev, map.basin)) {
  70. G_warning("elevation == basin");
  71. retval = 1;
  72. }
  73. if (map.fill && !strcmp(map.elev, map.fill)) {
  74. G_warning("elevation == " "depressionless");
  75. retval = 1;
  76. }
  77. if (map.dir && !strcmp(map.elev, map.dir)) {
  78. G_warning("elevation == direction");
  79. retval = 1;
  80. }
  81. if (map.belev && !strcmp(map.elev, map.belev)) {
  82. G_warning("elevation == belevation");
  83. retval = 1;
  84. }
  85. if (map.topidx && !strcmp(map.elev, map.topidx)) {
  86. G_warning("elevation == topidx");
  87. retval = 1;
  88. }
  89. }
  90. if (map.basin) {
  91. if (map.fill && !strcmp(map.basin, map.fill)) {
  92. G_warning("basin == depressionless");
  93. retval = 1;
  94. }
  95. if (map.dir && !strcmp(map.basin, map.dir)) {
  96. G_warning("basin == direction");
  97. retval = 1;
  98. }
  99. if (map.belev && !strcmp(map.basin, map.belev)) {
  100. G_warning("basin == belevation");
  101. retval = 1;
  102. }
  103. if (map.topidx && !strcmp(map.basin, map.topidx)) {
  104. G_warning("basin == topidx");
  105. retval = 1;
  106. }
  107. }
  108. if (map.fill) {
  109. if (map.dir && !strcmp(map.fill, map.dir)) {
  110. G_warning("depressionless == " "direction");
  111. retval = 1;
  112. }
  113. if (map.belev && !strcmp(map.fill, map.belev)) {
  114. G_warning("depressionless == " "belevation");
  115. retval = 1;
  116. }
  117. if (map.topidx && !strcmp(map.fill, map.topidx)) {
  118. G_warning("depressionless == topidx");
  119. retval = 1;
  120. }
  121. }
  122. if (map.dir) {
  123. if (map.belev && !strcmp(map.dir, map.belev)) {
  124. G_warning("direction == belevation");
  125. retval = 1;
  126. }
  127. if (map.topidx && !strcmp(map.dir, map.topidx)) {
  128. G_warning("direction == topidx");
  129. retval = 1;
  130. }
  131. }
  132. }
  133. if (map.belev) {
  134. if (map.topidx && !strcmp(map.belev, map.topidx)) {
  135. G_warning("belevation == topidx");
  136. retval = 1;
  137. }
  138. }
  139. if (!strcmp(file.idxstats, file.params)) {
  140. G_warning("idxstats == parameters");
  141. retval = 1;
  142. }
  143. if (!strcmp(file.idxstats, file.input)) {
  144. G_warning("idxstats == input");
  145. retval = 1;
  146. }
  147. if (!strcmp(file.idxstats, file.output)) {
  148. G_warning("idxstats == output");
  149. retval = 1;
  150. }
  151. if (file.Qobs && !strcmp(file.idxstats, file.Qobs)) {
  152. G_warning("idxstats == Qobs");
  153. retval = 1;
  154. }
  155. if (!strcmp(file.params, file.input)) {
  156. G_warning("parameters == input");
  157. retval = 1;
  158. }
  159. if (!strcmp(file.params, file.output)) {
  160. G_warning("parameters == output");
  161. retval = 1;
  162. }
  163. if (file.Qobs && !strcmp(file.params, file.Qobs)) {
  164. G_warning("parameters == Qobs");
  165. retval = 1;
  166. }
  167. if (!strcmp(file.input, file.output)) {
  168. G_warning("input == output");
  169. retval = 1;
  170. }
  171. if (file.Qobs && !strcmp(file.input, file.Qobs)) {
  172. G_warning("input == Qobs");
  173. retval = 1;
  174. }
  175. if (file.Qobs && !strcmp(file.output, file.Qobs)) {
  176. G_warning("output == Qobs");
  177. retval = 1;
  178. }
  179. return retval;
  180. }
  181. int check_io(void)
  182. {
  183. int retval;
  184. FILE *fp;
  185. retval = 0;
  186. if (!flg.input) {
  187. if (map.elev && !G_find_raster(map.elev, mapset)) {
  188. G_warning("%s - not exists", map.elev);
  189. retval = 1;
  190. }
  191. if (map.basin && !G_find_raster(map.basin, mapset)) {
  192. G_warning("%s - not exists", map.basin);
  193. retval = 1;
  194. }
  195. if (map.fill && G_find_raster(map.fill, mapset)) {
  196. if (flg.overwr) {
  197. flg.overwrlist |= FILL;
  198. }
  199. else {
  200. G_warning("%s - already exists", map.fill);
  201. retval = 1;
  202. }
  203. }
  204. if (map.dir && G_find_raster(map.dir, mapset)) {
  205. if (flg.overwr) {
  206. flg.overwrlist |= DIR;
  207. }
  208. else {
  209. G_warning("%s - already exists", map.dir);
  210. retval = 1;
  211. }
  212. }
  213. if (map.belev && G_find_raster(map.belev, mapset)) {
  214. if (flg.overwr) {
  215. flg.overwrlist |= BELEV;
  216. }
  217. else {
  218. G_warning("%s - already exists", map.belev);
  219. retval = 1;
  220. }
  221. }
  222. if (map.topidx && G_find_raster(map.topidx, mapset)) {
  223. if (flg.overwr) {
  224. flg.overwrlist |= TOPIDX;
  225. }
  226. else {
  227. G_warning("%s - already exists", map.topidx);
  228. retval = 1;
  229. }
  230. }
  231. if (file.idxstats && (fp = fopen(file.idxstats, "r"))) {
  232. fclose(fp);
  233. if (flg.overwr) {
  234. flg.overwrlist |= IDXSTATS;
  235. }
  236. else {
  237. G_warning("%s - file already exists", file.idxstats);
  238. retval = 1;
  239. }
  240. }
  241. }
  242. else {
  243. if (map.belev) {
  244. if (!G_find_raster(map.belev, mapset)) {
  245. G_warning("%s - not exists", map.belev);
  246. retval = 1;
  247. }
  248. else {
  249. if (map.topidx && G_find_raster(map.topidx, mapset)) {
  250. if (flg.overwr) {
  251. flg.overwrlist |= TOPIDX;
  252. }
  253. else {
  254. G_warning("%s - " "already exists", map.topidx);
  255. retval = 1;
  256. }
  257. }
  258. if (file.idxstats && (fp = fopen(file.idxstats, "r"))) {
  259. fclose(fp);
  260. if (flg.overwr) {
  261. flg.overwrlist |= IDXSTATS;
  262. }
  263. else {
  264. G_warning("%s - file "
  265. "already exists", file.idxstats);
  266. retval = 1;
  267. }
  268. }
  269. }
  270. }
  271. else if (map.topidx) {
  272. if (!G_find_raster(map.topidx, mapset)) {
  273. G_warning("%s - not exists", map.topidx);
  274. retval = 1;
  275. }
  276. else {
  277. if (file.idxstats && (fp = fopen(file.idxstats, "r"))) {
  278. fclose(fp);
  279. if (flg.overwr) {
  280. flg.overwrlist |= IDXSTATS;
  281. }
  282. else {
  283. G_warning("%s - file "
  284. "already exists", file.idxstats);
  285. retval = 1;
  286. }
  287. }
  288. }
  289. }
  290. else if (file.idxstats) {
  291. if (!(fp = fopen(file.idxstats, "r"))) {
  292. G_warning("%s - file not exists", file.idxstats);
  293. retval = 1;
  294. }
  295. else {
  296. fclose(fp);
  297. }
  298. }
  299. }
  300. if (file.params) {
  301. if (!(fp = fopen(file.params, "r"))) {
  302. G_warning("%s - file not exists", file.params);
  303. retval = 1;
  304. }
  305. else {
  306. fclose(fp);
  307. }
  308. }
  309. if (file.input) {
  310. if (!(fp = fopen(file.input, "r"))) {
  311. G_warning("%s - file not exists", file.input);
  312. retval = 1;
  313. }
  314. else {
  315. fclose(fp);
  316. }
  317. }
  318. if (file.output && (fp = fopen(file.output, "r"))) {
  319. fclose(fp);
  320. if (flg.overwr) {
  321. flg.overwrlist |= OUTPUT;
  322. }
  323. else {
  324. G_warning("%s - file already exists", file.output);
  325. retval = 1;
  326. }
  327. }
  328. if (file.Qobs) {
  329. if (!(fp = fopen(file.Qobs, "r"))) {
  330. G_warning("%s - file not exists", file.Qobs);
  331. retval = 1;
  332. }
  333. else {
  334. fclose(fp);
  335. }
  336. }
  337. return retval;
  338. }