solstice-pp

Post-processing utilities for the solstice app
git clone git://git.meso-star.com/solstice-pp.git
Log | Files | Refs | README | LICENSE

solmaps.c (2969B)


      1 /* Copyright (C) 2017, 2018, 2025 |Méso|Star>
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "solpp.h"
     17 
     18 #include <string.h>
     19 
     20 int
     21 main(int argc, char** argv)
     22 {
     23   char s[128];
     24   buf_char_T buf = BUF_NULL;
     25   FILE* input = stdin;
     26   char* line = NULL;
     27   double azim, elev, lat, lon;
     28   int case_dir = 0;
     29   char time[64];
     30   buf_char_T dont_read = BUF_NULL;
     31 
     32   if(argc > 1 && !(input = fopen(argv[1], "r"))) {
     33     fprintf(stderr, "Could not open the file `%s'.\n", argv[1]);
     34     return 1;
     35   }
     36 
     37   line = read_line(&buf, input, &dont_read);
     38 
     39   while(line) {
     40     if(!strncmp(line, "#--- Sun direction:", 19)) {
     41       case_dir = 1;
     42       /* Get the solar direction */
     43       CHK(sscanf(line+19, "%lf %lf (%*f %*f %*f)", &azim, &elev) == 2);
     44       line = read_line(&buf, input, &dont_read);
     45     } else if(!strncmp(line, "#--- Sun location and time:", 27)) {
     46       /* Get the location and time */
     47       case_dir = 0;
     48       memset(time, 0, sizeof(time));
     49       CHK(strlen(line+27) < sizeof(time));
     50       CHK(sscanf(line+27, "%lf %lf %s (%*f %*f %*f)", &lat, &lon, time) == 3);
     51       line = read_line(&buf, input, &dont_read);
     52     } else if(!strncmp(line, "# vtk", 5)) {
     53       char* header = NULL;
     54       char* rcv_name = NULL;
     55       FILE* output;
     56 
     57       CHK(header = strdup(line)); /* Duplicate the current line */
     58       CHK(line = read_line(&buf, input, &dont_read));
     59       CHK(rcv_name = strdup(line)); /* Duplicate the line of the receiver name */
     60 
     61       /* Create the name of the destination file */
     62       if(case_dir) {
     63         CHK(snprintf(s, sizeof(s), "%g-%g-%s.vtk", azim, elev, rcv_name)
     64             < (long)sizeof(s));
     65       } else {
     66         CHK(snprintf(s, sizeof(s), "%g-%g-%s-%s.vtk", lat, lon, time, rcv_name)
     67             < (long)sizeof(s));
     68       }
     69       printf("Writing `%s'\n", s);
     70       CHK(output = fopen(s, "w"));
     71 
     72       /* Write the map data into `output' */
     73       fprintf(output, "%s\n", header);
     74       fprintf(output, "%s\n", rcv_name);
     75       while((line = read_line(&buf, input, &dont_read)) && line[0] != '#') {
     76         fprintf(output, "%s\n", line);
     77       }
     78 
     79       /* Clean up temporary variable and close the destination file */
     80       fclose(output);
     81       free(header);
     82       free(rcv_name);
     83     } else {
     84       line = read_line(&buf, input, &dont_read);
     85     }
     86   }
     87 
     88   BUF_RELEASE(buf);
     89   if(input && input!=stdin) fclose(input);
     90   return 0;
     91 }
     92