void timenow (int modo) { int sec,min,hour; sec=read_sec(); min=read_min(); hour=read_hour(); if (modo==0) { itoa(hour,16); orario[0]=stringa[0]; orario[1]=stringa[1]; orario[2]=':'; itoa(min,16); orario[3]=stringa[0]; orario[4]=stringa[1]; orario[5]=':'; itoa(sec,16); orario[6]=stringa[0]; orario[7]=stringa[1]; orario[8]='\0'; } if (modo==1) { itoa(hour,16); orario[0]=stringa[0]; orario[1]=stringa[1]; orario[2]='.'; itoa(min,16); orario[3]=stringa[0]; orario[4]=stringa[1]; orario[5]='\0'; } }
void ds1307_init (void) { short int secondi; secondi=read_sec(); i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(0); i2c_outbyte(secondi & 0x7F); i2c_stop(); i2c_start(); }
//INIZIALIZZAZIONE RTC_DS1307 void ds1307_init (void) { unsigned char secondi; secondi=read_sec(); i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(0); i2c_outbyte(secondi & 0x7F); i2c_stop(); i2c_start(); }
int main (void) { if (i2c_open()<0) { printf("Apertura del bus I2C fallita\n"); return 1; } ds1307_init(); input_data(); set_data(giorno_in,mese_in,anno_in); printf("Data sul DS1307: %d/%d/%d\r\n",read_day(),read_month(),read_year()); input_ora(); set_ora(ora_in,minuto_in); while (1){ printf("Data sul DS1307: %02X/%02X/%02X\r\n", read_day(), read_month(), read_year()); printf("Ora sul DS1307: %02X:%02X:%02X\r\n",read_hour(),read_min(),read_sec()); sleep(10); } return 1; }
void VarDriverXY::preProcessMetObs() { vector<real> rhoP; // Check the data directory for files QDir dataPath("./vardata"); dataPath.setFilter(QDir::Files); dataPath.setSorting(QDir::Name); QStringList filenames = dataPath.entryList(); int processedFiles = 0; QList<MetObs>* metData = new QList<MetObs>; cout << "Found " << filenames.size() << " data files to read..." << endl; for (int i = 0; i < filenames.size(); ++i) { metData->clear(); QString file = filenames.at(i); QStringList fileparts = file.split("."); if (fileparts.isEmpty()) { cout << "Unknown file! " << file.toAscii().data() << endl; continue; } QString suffix = fileparts.last(); QString prefix = fileparts.first(); if (prefix == "swp") { // Switch it to suffix suffix = "swp"; } cout << "Processing " << file.toAscii().data() << " of type " << suffix.toAscii().data() << endl; QFile metFile(dataPath.filePath(file)); // Read different types of files switch (dataSuffix.value(suffix)) { case (frd): if (!read_frd(metFile, metData)) cout << "Error reading frd file" << endl; break; case (cls): if (!read_cls(metFile, metData)) cout << "Error reading frd file" << endl; break; case (sec): if (!read_sec(metFile, metData)) cout << "Error reading min file" << endl; break; case (ten): if (!read_ten(metFile, metData)) cout << "Error reading ten file" << endl; break; case (swp): if (!read_dorade(metFile, metData)) cout << "Error reading swp file" << endl; break; case (sfmr): if (!read_sfmr(metFile, metData)) cout << "Error reading sfmr file" << endl; break; case (wwind): if (!read_wwind(metFile, metData)) cout << "Error reading wwind file" << endl; break; case (qscat): if (!read_qscat(metFile, metData)) cout << "Error reading wwind file" << endl; break; case (ascat): if (!read_ascat(metFile, metData)) cout << "Error reading wwind file" << endl; break; case (nopp): if (!read_nopp(metFile, metData)) cout << "Error reading wwind file" << endl; break; case (cen): continue; default: cout << "Unknown data type, skipping..." << endl; continue; } processedFiles++; // Process the metObs into Observations QDateTime startTime = frameVector.front().getTime(); QDateTime endTime = frameVector.back().getTime(); for (int i = 0; i < metData->size(); ++i) { // Make sure the ob is within the time limits MetObs metOb = metData->at(i); QDateTime obTime = metOb.getTime(); QString obstring = obTime.toString(Qt::ISODate); QString tcstart = startTime.toString(Qt::ISODate); QString tcend = endTime.toString(Qt::ISODate); if ((obTime < startTime) or (obTime > endTime)) continue; int tci = startTime.secsTo(obTime); if ((tci < 0) or (tci > (int)frameVector.size())) { cout << "Time problem with observation " << tci << endl; continue; } // Our generic observation Observation varOb; // Get the X, Y & Z double latrad = frameVector[tci].getLat() * Pi/180.0; double fac_lat = 111.13209 - 0.56605 * cos(2.0 * latrad) + 0.00012 * cos(4.0 * latrad) - 0.000002 * cos(6.0 * latrad); double fac_lon = 111.41513 * cos(latrad) - 0.09455 * cos(3.0 * latrad) + 0.00012 * cos(5.0 * latrad); double obY = (metOb.getLat() - frameVector[tci].getLat())*fac_lat; double obX = (metOb.getLon() - frameVector[tci].getLon())*fac_lon; double height = metOb.getAltitude(); // Make sure the ob is in the domain if ((obX < x.front()) or (obX > x.back()) or (obY < y.front()) or (obY > y.back()) or (abs(height - zLevel) > 50)) continue; varOb.setCartesianX(obX); varOb.setCartesianY(obY); real Um = frameVector[tci].getUmean(); real Vm = frameVector[tci].getVmean(); varOb.setAltitude(height); // Reference states real rhoBar = rhoBase*exp(-rhoInvScaleHeight*height); real qBar = 19.562 - 0.004066*height + 7.8168e-7*height*height; real hBar = 3.5e5; /* Use bilinear interpolation here too for now, eventually probably a spline real rhoaBG = bilinearField(obX, obY, 4)/100. + rhoBar; real qBG = bilinearField(obX, obY, 3) + qBar; real rhoBG = rhoaBG*(1+qBG/1000.); */ // Initialize the weights varOb.setWeight(0., 0); varOb.setWeight(0., 1); varOb.setWeight(0., 2); varOb.setWeight(0., 3); varOb.setWeight(0., 4); varOb.setWeight(0., 5); double u, v, w, rho, rhoa, qv, energy, rhov, rhou, rhow, wspd, vBG; // uBG not used in SFMR calculation switch (metOb.getObType()) { case (MetObs::dropsonde): varOb.setType(MetObs::dropsonde); u = metOb.getCartesianUwind(); v = metOb.getCartesianVwind(); w = metOb.getVerticalVelocity(); rho = metOb.getMoistDensity(); rhoa = metOb.getAirDensity(); qv = metOb.getQv(); energy = metOb.getMoistStaticEnergy(); // Separate obs for each measurement // rho v 1 m/s error if ((u != -999) and (rho != -999)) { varOb.setWeight(1., 0); rhov = rho*(v - Vm); varOb.setOb(rhov); varOb.setError(1.0); obVector.push_back(varOb); varOb.setWeight(0., 0); // rho u 1 m/s error varOb.setWeight(1., 1); rhou = rho*(u - Um); //cout << "RhoU: " << rhou << endl; varOb.setOb(rhou); varOb.setError(1.0); obVector.push_back(varOb); varOb.setWeight(0., 1); } if ((w != -999) and (rho != -999)) { // rho w 1.5 m/s error varOb.setWeight(1., 2); rhow = rho*w; varOb.setOb(rhow); varOb.setError(1.5); obVector.push_back(varOb); varOb.setWeight(0., 2); } if (energy != -999) { // energy 5 kJ error varOb.setWeight(1., 3); varOb.setOb((energy - hBar)*1.e-3); varOb.setError(5.0); obVector.push_back(varOb); varOb.setWeight(0., 3); } if (qv != -999) { // Qv 2 g/kg error varOb.setWeight(1., 4); varOb.setOb(qv-qBar); varOb.setError(2.0); obVector.push_back(varOb); varOb.setWeight(0., 4); } if (rhoa != -999) { // Rho prime .1 kg/m^3 error varOb.setWeight(1., 5); varOb.setOb((rhoa-rhoBar)*100); varOb.setError(1.0); obVector.push_back(varOb); varOb.setWeight(0., 5); } break; case (MetObs::flightlevel): varOb.setType(MetObs::flightlevel); u = metOb.getCartesianUwind(); v = metOb.getCartesianVwind(); w = metOb.getVerticalVelocity(); rho = metOb.getMoistDensity(); rhoa = metOb.getAirDensity(); qv = metOb.getQv(); energy = metOb.getMoistStaticEnergy(); // Separate obs for each measurement // rho v 1 m/s error if ((u != -999) and (rho != -999)) { varOb.setWeight(1., 0); rhov = rho*(v - Vm); varOb.setOb(rhov); varOb.setError(1.0); obVector.push_back(varOb); varOb.setWeight(0., 0); // rho u 1 m/s error varOb.setWeight(1., 1); rhou = rho*(u - Um); varOb.setOb(rhou); varOb.setError(1.0); obVector.push_back(varOb); varOb.setWeight(0., 1); } if ((w != -999) and (rho != -999)) { // rho w 1 dm/s error varOb.setWeight(1., 2); rhow = rho*w; varOb.setOb(rhow); varOb.setError(0.25); obVector.push_back(varOb); varOb.setWeight(0., 2); } if (energy != -999) { // energy 5 kJ error varOb.setWeight(1., 3); varOb.setOb((energy - hBar)*1.e-3); varOb.setError(5.0); obVector.push_back(varOb); varOb.setWeight(0., 3); } if (qv != -999) { // Qv 2 g/kg error varOb.setWeight(1., 4); varOb.setOb(qv-qBar); varOb.setError(2.0); obVector.push_back(varOb); varOb.setWeight(0., 4); } if (rhoa != -999) { // Rho prime .1 kg/m^3 error varOb.setWeight(1., 5); varOb.setOb((rhoa-rhoBar)*100); varOb.setError(1.0); obVector.push_back(varOb); varOb.setWeight(0., 5); } break; case (MetObs::sfmr): varOb.setType(MetObs::sfmr); wspd = metOb.getWindSpeed(); // This needs to be redone for the Cartesian case vBG = 1.e3*bilinearField(obX, obY, 0); //uBG = -1.e5*bilinearField(obX, 20., 1)/(rad*20.); varOb.setWeight(1., 0); //varOb.setWeight(1., 1); varOb.setOb(wspd); varOb.setError(10.0); obVector.push_back(varOb); break; case (MetObs::qscat): varOb.setType(MetObs::qscat); u = metOb.getCartesianUwind(); v = metOb.getCartesianVwind(); if (u != -999) { varOb.setWeight(1., 0); // Multiply by rho later from grid values rhov = (v - Vm); varOb.setOb(rhov); varOb.setError(2.5); obVector.push_back(varOb); varOb.setWeight(0., 0); // rho u 1 m/s error varOb.setWeight(1., 1); rhou = (u - Um); //cout << "RhoU: " << rhou << endl; varOb.setOb(rhou); varOb.setError(2.5); obVector.push_back(varOb); varOb.setWeight(0., 1); } break; case (MetObs::ascat): varOb.setType(MetObs::ascat); u = metOb.getCartesianUwind(); v = metOb.getCartesianVwind(); if (u != -999) { varOb.setWeight(1., 0); // Multiply by rho later from grid values rhov = (v - Vm); varOb.setOb(rhov); varOb.setError(2.5); obVector.push_back(varOb); varOb.setWeight(0., 0); // rho u 1 m/s error varOb.setWeight(1., 1); rhou = (u - Um); //cout << "RhoU: " << rhou << endl; varOb.setOb(rhou); varOb.setError(2.5); obVector.push_back(varOb); varOb.setWeight(0., 1); } break; case (MetObs::radar): varOb.setType(MetObs::radar); // Geometry terms double az = metOb.getAzimuth()*Pi/180.; double el = metOb.getElevation()*Pi/180.; double uWgt = sin(az)*cos(el); double vWgt = cos(az)*cos(el); double wWgt = sin(el); // Fall speed double Z = metOb.getReflectivity(); double H = metOb.getAltitude(); double ZZ=pow(10.0,(Z*0.1)); double hlow= 5600 - 1000 * .5; double hhi= hlow + 1000; /* density correction term (rhoo/rho)*0.45 [rho(Z)=rho_o exp-(z/H), where H is the scale height = 9.58125 from Gray's inner 2 deg composite] 0.45 density correction from Beard (1985, JOAT pp 468-471) Adjusted to use dunion_mt hydrostatic scale height -MB */ double DCOR=exp(0.45*metOb.getAltitude()*0.0001068); // The snow relationship (Atlas et al., 1973) --- VT=0.817*Z**0.063 (m/s) double VTS=-DCOR * (0.817*pow(ZZ,(double)0.063)); // The rain relationship (Joss and Waldvogel,1971) --- VT=2.6*Z**.107 (m/s) */ double VTR=-DCOR * (2.6*pow(ZZ,(double).107)); /* Test if height is in the transition region between SNOW and RAIN defined as hlow in km < H < hhi in km if in the transition region do a linear weight of VTR and VTS */ if ((Z > 20) and (Z <= 30)) { double WEIGHTR=(Z-20)/(10); double WEIGHTS=1.-WEIGHTR; VTS=(VTR*WEIGHTR+VTS*WEIGHTS)/(WEIGHTR+WEIGHTS); } else if (Z > 30) { VTS=VTR; } double w_term=VTR*(hhi-H)/1000 + VTS*(H-hlow)/1000; if (H < hlow) w_term=VTR; if (H > hhi) w_term=VTS; double Vdopp = metOb.getRadialVelocity() - w_term*sin(el) - Um*sin(az)*cos(el) - Vm*cos(az)*cos(el); varOb.setWeight(vWgt, 0); varOb.setWeight(uWgt, 1); varOb.setWeight(wWgt, 2); // Theoretically, rhoPrime could be included as a prognostic variable here... // However, adding another unknown without an extra equation makes the problem even more underdetermined // so assume it is small and ignore it // double rhopWgt = -Vdopp; //varOb.setWeight(rhopWgt, 5); // Set the error according to the spectrum width and potential fall speed error (assume 2 m/s?) double DopplerError = metOb.getSpectrumWidth() + fabs(wWgt)*2.; if (DopplerError < 1.0) DopplerError = 1.0; varOb.setError(DopplerError); varOb.setOb(Vdopp); obVector.push_back(varOb); break; } } cout << obVector.size() << " total observations." << endl; } delete metData; // Write the Obs to a summary text file ofstream obstream("Observations.in"); // Header messes up reload /*ostream_iterator<string> os(obstream, "\t "); *os++ = "Type"; *os++ = "r"; *os++ = "z"; *os++ = "NULL"; *os++ = "Observation"; *os++ = "Inverse Error"; *os++ = "Weight 1"; *os++ = "Weight 2"; *os++ = "Weight 3"; *os++ = "Weight 4"; *os++ = "Weight 5"; *os++ = "Weight 6"; obstream << endl; */ ostream_iterator<double> od(obstream, "\t "); for (unsigned int i=0; i < obVector.size(); i++) { Observation ob = obVector.at(i); *od++ = ob.getType(); *od++ = ob.getCartesianX(); *od++ = ob.getCartesianY(); // NULL 3rd dimension *od++ = -999.; *od++ = ob.getOb(); *od++ = ob.getInverseError(); for (unsigned int var = 0; var < numVars; var++) *od++ = ob.getWeight(var); obstream << endl; } // Load the observations into a vector obs = new real[obVector.size()*12]; for (unsigned int m=0; m < obVector.size(); m++) { int n = m*12; Observation ob = obVector.at(m); obs[n] = ob.getOb(); obs[n+1] = ob.getInverseError(); for (unsigned int var = 0; var < numVars; var++) { obs[n+2+var] = ob.getWeight(var); } obs[n+2+numVars] = ob.getCartesianX(); obs[n+3+numVars] = ob.getCartesianY(); obs[n+4+numVars] = ob.getAltitude(); obs[n+5+numVars] = ob.getType(); } // All done preprocessing if (!processedFiles) { cout << "No files processed, nothing to do :(" << endl; // return 0; } else { cout << "Finished preprocessing " << processedFiles << " files." << endl; } }
bool fill_sec(int sn) { int nf = 0, i, i1, n1; char s[81], s1[81]; WFindFile fnd; bool bFound = false; gfilerec *g = read_sec(sn, &n1); sprintf(s1, "%s%s%c*.*", syscfg.gfilesdir, gfilesec[sn].filename, File::pathSeparatorChar); bFound = fnd.open(s1, 0); bool ok = true; int chd = 0; while ((bFound) && (!hangup) && (nf < gfilesec[sn].maxfiles) && (ok)) { if (fnd.GetFileName()[0] == '.') { bFound = fnd.next(); continue; } strcpy(s, fnd.GetFileName()); align(s); i = 1; for (i1 = 0; i1 < nf; i1++) { if (compare(fnd.GetFileName(), g[i1].filename)) { i = 0; } } if (i) { bout << "|#2" << s << " : "; inputl(s1, 60); if (s1[0]) { chd = 1; i = 0; while (wwiv::strings::StringCompare(s1, g[i].description) > 0 && i < nf) { ++i; } for (i1 = nf; i1 > i; i1--) { g[i1] = g[i1 - 1]; } ++nf; gfilerec g1; strcpy(g1.filename, fnd.GetFileName()); strcpy(g1.description, s1); g1.daten = static_cast<long>(time(nullptr)); g[i] = g1; } else { ok = false; } } bFound = fnd.next(); } if (!ok) { bout << "|#6Aborted.\r\n"; } if (nf >= gfilesec[sn].maxfiles) { bout << "Section full.\r\n"; } if (chd) { char szFileName[ MAX_PATH ]; sprintf(szFileName, "%s%s.gfl", syscfg.datadir, gfilesec[sn].filename); File gflFile(szFileName); gflFile.Open(File::modeReadWrite | File::modeBinary | File::modeCreateFile | File::modeTruncate); gflFile.Write(g, nf * sizeof(gfilerec)); gflFile.Close(); WStatus *pStatus = application()->GetStatusManager()->BeginTransaction(); pStatus->SetGFileDate(date()); application()->GetStatusManager()->CommitTransaction(pStatus); } free(g); return !ok; }