00001
00002
00003 #include "jsoc_main.h"
00004 #include "drms_types.h"
00005
00006 #if 0
00007 #include "cfitsio.h"
00008 #include "fitsio.h"
00009 #endif
00010
00011
00012 #define NONDAILY 0
00013
00014 #define NEWMAP 0
00015
00016 int set_statistics(DRMS_Segment_t *, DRMS_Array_t *, int);
00017
00018 char *module_name = "mhdtxt2jsoc";
00019 #define DIE(msg) {fflush(stdout);fprintf(stderr,"%s, status=%d\n",msg,status); return(status);}
00020 ModuleArgs_t module_args[] =
00021 {
00022 #if NONDAILY == 1
00023 {ARG_STRING, "in", "d3equidist144x72.txt", "Input text file."},
00024 {ARG_STRING, "out", "hmi.MHDcorona", "Target DRMS data series."},
00025 #if NEWMAP == 1
00026 {ARG_STRING, "synomap", "hmi.BrBlossynop_720s", "synoptic map series as string"},
00027 #else
00028 {ARG_STRING, "synomap", "hmi.Synoptic_Mr_720s", "synoptic map series as string"},
00029 #endif
00030 {ARG_STRING, "crdate", "-1", "date or CR as string"},
00031 {ARG_INT, "sphindx", "-1", "terms of pfss spherical harmonic poly. (negative for raw map)"},
00032 {ARG_INT, "inx", "144", "long. grid size of input MHD data, in the text file"},
00033 {ARG_INT, "iny", "72", "lat. grid size of input MHD data, in the text file"},
00034 {ARG_INT, "inz", "80", "rad. grid size of input MHD data, in the text file"},
00035 #else
00036 {ARG_STRING, "in", "d3equidist.txt", "Input text file."},
00037 {ARG_STRING, "out", "hmi.MHDcorona_daily_nrt", "Target DRMS data series."},
00038 #if NEWMAP == 1
00039 {ARG_STRING, "synomap", "hmi.BrBlosdailysynframe_720s_nrt", "synoptic map series as string"},
00040 #else
00041 {ARG_STRING, "synomap", "hmi.Mrdailysynframe_720s_nrt", "synoptic map series as string"},
00042 #endif
00043 {ARG_STRING, "crdate", "$", "date as string"},
00044 {ARG_INT, "sphindx", "5", "terms of pfss spherical harmonic poly. (negative for raw map)"},
00045 {ARG_INT, "inx", "72", "long. grid size of input MHD data, in the text file"},
00046 {ARG_INT, "iny", "36", "lat. grid size of input MHD data, in the text file"},
00047 {ARG_INT, "inz", "80", "rad. grid size of input MHD data, in the text file"},
00048 #endif
00049 {ARG_INT, "ichrbnd", "0", "index for choice of sub-alfvenic boundary treatement"},
00050 {ARG_END}
00051 };
00052
00053 int DoIt(void)
00054 {
00055 CmdParams_t *params = &cmdparams;
00056 const char *instrc = params_get_str(params, "in");
00057 const char *outstrc = params_get_str(params, "out");
00058 const char *crdatestrc = params_get_str(params, "crdate");
00059 const char *synostrc = params_get_str(params, "synomap");
00060 const int sphindxc = params_get_int(params, "sphindx");
00061 const int inxc = params_get_int(params, "inx");
00062 const int inyc = params_get_int(params, "iny");
00063 const int inzc = params_get_int(params, "inz");
00064 const int ichrbndc = params_get_int(params, "ichrbnd");
00065
00066 char *instr, *outstr, *crdatestr, *synostr;
00067 instr = strdup(instrc);
00068 outstr = strdup(outstrc);
00069 crdatestr = strdup(crdatestrc);
00070 synostr = strdup(synostrc);
00071 int isphindx = sphindxc;
00072 int nx = inxc;
00073 int ny = inyc;
00074 int nz = inzc;
00075 int ichrbnd = ichrbndc;
00076
00077
00078 DRMS_RecordSet_t *inRS, *outRS;
00079 DRMS_Record_t *outRec, *inRec;
00080 DRMS_Segment_t *outSeg, *inSeg;
00081 DRMS_Array_t *inArray, *outArray;
00082
00083
00084 int status;
00085 int i, j, k, n;
00086 double ddummy;
00087 char *strdummy;
00088
00089 char *mhdcorona_version();
00090
00091 #if NONDAILY == 1
00092 int ncr = atoi(crdatestr);
00093 if (ncr < 0)
00094 {
00095 printf("CR is not given, so quit. %d %s\n",ncr,crdatestr);
00096 return (-1);
00097 }
00098 #endif
00099
00100
00101 printf("Nx Ny Nz = %d %d %d\n",nx,ny,nz);
00102 float *mhd8all;
00103 mhd8all=(float *)malloc(sizeof(float)*(nx*ny*nz*8));
00104
00105
00106 int icount=0;
00107 {
00108 FILE *fp=NULL;
00109 fp=fopen(instr,"r");
00110 char line[256];
00111 if(fp == NULL)
00112 {
00113 printf("Fail to open file %s\n",instr);
00114 return 1;
00115 }
00116 while(fgets(line,256,fp) != NULL)
00117 {
00118 float rdummy;
00119 sscanf(line,"%e",&rdummy);
00120 mhd8all[icount]=rdummy;
00121 icount=icount+1;
00122 }
00123 fclose(fp);
00124 }
00125 printf("Num of data expected in text file %s : %d \n",instr,nx*ny*nz*8);
00126 printf("Num of data loaded : %d \n",icount);
00127
00128 for(k = 0; k < nz; k++){for(j = 0; j < ny; j++){for(i = 0; i < nx; i++){
00129 int idrs = i + j * nx + k * nx * ny;
00130 mhd8all[idrs+0*nx*ny*nz] = mhd8all[idrs+0*nx*ny*nz] / 1.0e8;
00131 mhd8all[idrs+3*nx*ny*nz] = - mhd8all[idrs+3*nx*ny*nz];
00132 mhd8all[idrs+6*nx*ny*nz] = - mhd8all[idrs+6*nx*ny*nz];
00133 }}}
00134
00135
00136 double lonfirst,lonlast,carrtime;
00137 char *trecstr, *tobsstr;
00138 int carrot, cols, rows;
00139 char *mapcver, *mapbldv;
00140
00141 char strtmp[100];
00142 sprintf(strtmp,"%s[%s]",synostr,crdatestr);
00143 char *inQuery=strdup(strtmp);
00144 printf("now looking for synoptic map %s\n",inQuery);
00145 inRS = drms_open_records(drms_env, inQuery, &status);
00146 if (status || inRS->n == 0) {DIE("No input synoptic data found, so quit. Bye.");}
00147 int nrecs = inRS->n;
00148 if (nrecs > 1) {printf(" num. of data available is more than 1, %3d !!! Will use the first one anyway.\n",nrecs);}
00149 inRec = inRS->records[0];
00150 #if NEWMAP == 1
00151 inSeg = drms_segment_lookup(inRec,"Br");
00152 #else
00153 inSeg = drms_segment_lookupnum (inRec, 0);
00154 #endif
00155 cols = inSeg->axis[0];
00156 rows = inSeg->axis[1];
00157 lonfirst= drms_getkey_double(inRec,"LON_FRST",&status);
00158 lonlast = drms_getkey_double(inRec,"LON_LAST",&status);
00159 carrtime= drms_getkey_double(inRec,"CARRTIME",&status);
00160 printf(" LON_FRST of the syn.mag : %f\n",lonfirst);
00161 printf(" LON_LAST : %f\n",lonlast);
00162 printf(" CARRTIME : %f\n",carrtime);
00163 {
00164 TIME t_rec = drms_getkey_time(inRec,"T_REC",&status);
00165 TIME t_obs = drms_getkey_time(inRec,"T_OBS",&status);
00166 char timestr[26];
00167 sprint_time(timestr,t_rec,"TAI",0);
00168 trecstr=strdup(timestr);
00169 sprint_time(timestr,t_obs,"TAI",0);
00170 tobsstr=strdup(timestr);
00171 printf(" T_REC : %s\n",trecstr);
00172 printf(" T_OBS : %s\n",tobsstr);
00173 }
00174 carrot = drms_getkey_int(inRec,"CAR_ROT",&status);
00175 printf(" CAR_ROT : %d\n",carrot);
00176
00177
00178
00179
00180
00181
00182
00183
00184 mapcver=drms_getkey_string(inRec,"CODEVER",&status);
00185 mapbldv=drms_getkey_string(inRec,"BLD_VERS",&status);
00186
00187
00188 printf(" Now open and create output at series %s\n",outstr);
00189 outRec = drms_create_record (drms_env, outstr, DRMS_PERMANENT, &status);
00190 if (!outRec) {fprintf (stderr, "Error creating record in series %s; abandoned\n",outstr);return 1;}
00191
00192
00193 drms_copykeys(outRec, inRec, 0, kDRMS_KeyClass_Explicit);
00194
00195
00196 drms_setkey_string(outRec,"MAP_BLDV" ,mapbldv);
00197 drms_setkey_string(outRec,"MAP_CVER" ,mapcver);
00198 #if NONDAILY == 1
00199 lonfirst = (double)(carrot-1)*360.0 + 360.0/((double)(nx))*0.5;
00200 lonlast = (double)(carrot )*360.0 - 360.0/((double)(nx))*0.5;
00201 #else
00202 lonfirst = lonfirst;
00203 lonlast = lonlast ;
00204 #endif
00205 printf(" LON_FRST of the MHD : %f\n",lonfirst);
00206 printf(" LON_LAST : %f\n",lonlast);
00207 drms_setkey_double(outRec,"LON_FRST", lonfirst);
00208 drms_setkey_double(outRec,"LON_LAST", lonlast);
00209
00210 {
00211 char timestr[26];
00212 TIME t_rec = drms_getkey_time(inRec,"DATE",&status);
00213 sprint_time(timestr,t_rec,"TAI",0);
00214 drms_setkey_string(outRec,"MAP_DATE" ,timestr);
00215 sprint_time(timestr,CURRENT_SYSTEM_TIME,"UTC",1);
00216 drms_setkey_string(outRec,"DATE" ,timestr);
00217 }
00218
00219 ddummy=((double)(nx))*0.5+0.5 ; drms_setkey_double(outRec,"CRPIX1",ddummy);
00220 ddummy=((double)(ny))*0.5+0.5 ; drms_setkey_double(outRec,"CRPIX2",ddummy);
00221 ddummy=((double)(nz))*0.5+0.5 ; drms_setkey_double(outRec,"CRPIX3",ddummy);
00222 ddummy=180.0+(double)(carrot-1)*360.0 ; drms_setkey_double(outRec,"CRVAL1",ddummy);
00223 ddummy=0.0 ; drms_setkey_double(outRec,"CRVAL2",ddummy);
00224 ddummy=(1.0+5.0)/2.0 ; drms_setkey_double(outRec,"CRVAL3",ddummy);
00225 ddummy=-360.0/((double)(nx)) ; drms_setkey_double(outRec,"CDELT1",ddummy);
00226 ddummy= 180.0/((double)(ny)) ; drms_setkey_double(outRec,"CDELT2",ddummy);
00227 ddummy= 4.000/((double)(nz)) ; drms_setkey_double(outRec,"CDELT3",ddummy);
00228 strdummy="degree" ; drms_setkey_string(outRec,"CUNIT1", strdummy);
00229 strdummy="degree" ; drms_setkey_string(outRec,"CUNIT2", strdummy);
00230 strdummy="solRad" ; drms_setkey_string(outRec,"CUNIT3", strdummy);
00231 strdummy="CRLN-CAR" ; drms_setkey_string(outRec,"CTYPE1", strdummy);
00232 strdummy="CRLT-CAR" ; drms_setkey_string(outRec,"CTYPE2", strdummy);
00233 strdummy="HECR" ; drms_setkey_string(outRec,"CTYPE3", strdummy);
00234 strdummy="3D-SPHERICAL" ; drms_setkey_string(outRec,"WCSNAME",strdummy);
00235 strdummy = mhdcorona_version() ; drms_setkey_string(outRec,"MHD_VER1",strdummy);
00236 #if NONDAILY == 1
00237 strdummy="v1.0, Jan. 10, 2014" ; drms_setkey_string(outRec,"MHD_VER2",strdummy);
00238 #else
00239 strdummy="v1.0, Dec. 10, 2013" ; drms_setkey_string(outRec,"MHD_VER2",strdummy);
00240 #endif
00241
00242
00243 {
00244 char *inputmapstr;
00245 char strtmp[100];
00246 #if NONDAILY == 1
00247 #if NEWMAP == 1
00248 sprintf(strtmp,"%s[%d]{Br}",synostr,carrot);
00249 #else
00250 sprintf(strtmp,"%s[%d]",synostr,carrot);
00251 #endif
00252 #else
00253 #if NEWMAP == 1
00254 sprintf(strtmp,"%s[%s]{Br}",synostr,trecstr);
00255 #else
00256 sprintf(strtmp,"%s[%s]",synostr,trecstr);
00257 #endif
00258 #endif
00259 inputmapstr=strdup(strtmp);
00260 drms_setkey_string(outRec,"INPUTMAP", inputmapstr);
00261 }
00262
00263 strdummy = "3D version of the code in ApJS (2005) vol.161, 480";
00264 drms_setkey_string(outRec,"MHDMODEL",strdummy);
00265 strdummy = "Polytropic, with specific heat ratio 1.05.";
00266 drms_setkey_string(outRec,"MHD_SET1", strdummy);
00267 strdummy = " ";
00268 if (ichrbnd == 0) {strdummy=" sub-Alfvenic boundary treatment: case O";}
00269 if (ichrbnd == 1) {strdummy=" sub-Alfvenic boundary treatment: case AB'";}
00270 if (ichrbnd == 6) {strdummy=" sub-Alfvenic boundary treatment: case A";}
00271 drms_setkey_string(outRec,"MHD_SET2", strdummy);
00272 strdummy = " ";
00273 drms_setkey_string(outRec,"MHD_SET3", strdummy);
00274 if (isphindx < 0)
00275 {
00276 strdummy = "PFSS with iterative Laplace solver";
00277 }
00278 else
00279 {
00280 char strtmp[100];
00281 sprintf(strtmp,"PFSS with %d-order spherical harmonics polynomial",isphindx);
00282 strdummy = strdup(strtmp);
00283 }
00284 drms_setkey_string(outRec,"MHDIBMAG", strdummy);
00285 drms_setkey_int(outRec,"MHDMGIDX", isphindx);
00286
00287 strdummy = " ";
00288 drms_setkey_string(outRec,"COMMENT", strdummy);
00289
00290
00291 drms_setkey_string(outRec,"BUNIT_000","1e8/cc");
00292 drms_setkey_string(outRec,"BUNIT_001","1e6K");
00293 drms_setkey_string(outRec,"BUNIT_002","km/s");
00294 drms_setkey_string(outRec,"BUNIT_003","km/s");
00295 drms_setkey_string(outRec,"BUNIT_004","km/s");
00296 drms_setkey_string(outRec,"BUNIT_005","Gauss");
00297 drms_setkey_string(outRec,"BUNIT_006","Gauss");
00298 drms_setkey_string(outRec,"BUNIT_007","Gauss");
00299 drms_setkey_string(outRec,"DSCRPT_000","number density");
00300 drms_setkey_string(outRec,"DSCRPT_001","temperature");
00301 drms_setkey_string(outRec,"DSCRPT_002","radial component of plasma flow");
00302 drms_setkey_string(outRec,"DSCRPT_003","latitudinal component of V (positive for northward)");
00303 drms_setkey_string(outRec,"DSCRPT_004","longitudinal component of V");
00304 drms_setkey_string(outRec,"DSCRPT_005","radial component of magnetic field");
00305 drms_setkey_string(outRec,"DSCRPT_006","latitudinal component of B (positive for northward)");
00306 drms_setkey_string(outRec,"DSCRPT_007","longitudinal component of B");
00307
00308
00309 char *Resname[] = {"N","T","Vr","Vt","Vp","Br","Bt","Bp"};
00310 int ivar;
00311 for(ivar = 0; ivar < 8; ivar++)
00312 {
00313 float *dat1;
00314 int axes[3];
00315 axes[0] = nx;
00316 axes[1] = ny;
00317 axes[2] = nz;
00318 dat1 = (float *)calloc(nx*ny*nz, sizeof(float));
00319 int ndatsize=nx*ny*nz;
00320 for(n = 0; n < ndatsize; n++){dat1[n] = mhd8all[(n+ndatsize*ivar)];}
00321 outArray = drms_array_create (DRMS_TYPE_FLOAT, 3, axes, dat1, &status);
00322 #if 0
00323 outArray->israw = 0;
00324 outArray->bzero = bzero[j];
00325 outArray->bscale = bscal[j];
00326 #endif
00327 outSeg = drms_segment_lookup (outRec, Resname[ivar]);
00328
00329 if (!outSeg)
00330 {
00331 printf("Error in setting data segment %s\n", Resname[ivar]);
00332 DIE(" bye \n")
00333 }
00334 else
00335 {
00336 set_statistics(outSeg, outArray, 1);
00337 printf("Now writing segment : %s\n", Resname[ivar]);
00338 if (drms_segment_write (outSeg, outArray, 0))
00339 {
00340 printf("Error in writing to %dth segment %s\n", ivar,Resname[ivar]);
00341 DIE(" bye \n")
00342 }
00343 }
00344 }
00345 printf("Done \n");
00346
00347
00348 drms_free_array(outArray);
00349 drms_close_record (outRec, DRMS_INSERT_RECORD);
00350 drms_close_records(inRS, DRMS_FREE_RECORD);
00351 return (DRMS_SUCCESS);
00352 }
00353
00354
00355
00356 char *mhdcorona_version(){return strdup("$Id: mhdtxt2jsoc_daily_32.c,v 1.4 2014/08/09 01:52:40 keiji Exp $");}
00357
00358